Skip to content

Commit

Permalink
First commit of code and notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Apr 12, 2019
1 parent 159122e commit 0ff1530
Show file tree
Hide file tree
Showing 6 changed files with 2,179 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
packrat/lib*/
71 changes: 71 additions & 0 deletions immunotherapy notebook.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "Immunotherapy Notebook"
output:
html_document:
df_print: paged
---

## Introduction

Inspired by the blog post at https://blogs.rstudio.com/tensorflow/posts/2018-01-29-dl-for-cancer-immunotherapy/

## Packages

Packages for sequence logos and peptides

```r
devtools::install_github("omarwagih/ggseqlogo")
devtools::install_github("leonjessen/PepTools")
```

## Analysis

```{r}
library(keras)
library(tidyverse)
library(PepTools)
```

Download and cache the data

```{r}
pep_file <- get_file(
"ran_peps_netMHCpan40_predicted_A0201_reduced_cleaned_balanced.tsv",
origin = "https://git.io/vb3Xa"
)
```

Read and display some data

```{r}
pep_dat <- read_tsv(file = pep_file)
pep_dat %>% head(5)
```

Summarize

```{r}
pep_dat %>%
group_by(label_chr, data_type) %>%
tally() %>%
arrange(desc(n))
```

Plot sequence logo

```{r}
pep_dat %>%
filter(label_chr == "SB") %>%
pull(peptide) %>%
ggseqlogo()
```

Plot peptide image

```{r}
pep_dat %>%
filter(label_chr == "SB") %>%
head(1) %>%
pull(peptide) %>%
pep_plot_images()
```
1,887 changes: 1,887 additions & 0 deletions immunotherapy notebook.nb.html

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions immunotherapy.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
71 changes: 71 additions & 0 deletions immunotherapy_notebook.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "Immunotherapy Notebook"
output:
html_document:
df_print: paged
---

## Introduction

Inspired by the blog post at https://blogs.rstudio.com/tensorflow/posts/2018-01-29-dl-for-cancer-immunotherapy/

## Packages

Packages for sequence logos and peptides

```r
devtools::install_github("omarwagih/ggseqlogo")
devtools::install_github("leonjessen/PepTools")
```

## Analysis

```{r}
library(keras)
library(tidyverse)
library(PepTools)
```

Download and cache the data

```{r}
pep_file <- get_file(
"ran_peps_netMHCpan40_predicted_A0201_reduced_cleaned_balanced.tsv",
origin = "https://git.io/vb3Xa"
)
```

Read and display some data

```{r}
pep_dat <- read_tsv(file = pep_file)
pep_dat %>% head(5)
```

Summarize

```{r}
pep_dat %>%
group_by(label_chr, data_type) %>%
tally() %>%
arrange(desc(n))
```

Plot sequence logo

```{r}
pep_dat %>%
filter(label_chr == "SB") %>%
pull(peptide) %>%
ggseqlogo()
```

Plot peptide image

```{r}
pep_dat %>%
filter(label_chr == "SB") %>%
head(1) %>%
pull(peptide) %>%
pep_plot_images()
```
129 changes: 129 additions & 0 deletions train_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# https://blogs.rstudio.com/tensorflow/posts/2018-01-29-dl-for-cancer-immunotherapy/

# # Keras + TensorFlow and it's dependencies
# install.packages("keras")
# library(keras)
# install_keras()
#
# # Tidyverse (readr, ggplot2, etc.)
# install.packages("tidyverse")
#

library(keras)
library(tfdeploy)
library(tidyverse)
library(PepTools)

pep_file <- get_file(
"ran_peps_netMHCpan40_predicted_A0201_reduced_cleaned_balanced.tsv",
origin = "https://git.io/vb3Xa"
)
pep_dat <- read_tsv(file = pep_file)

pep_dat %>% head()


x_train <- pep_dat %>%
filter(data_type == "train") %>%
pull(peptide) %>%
pep_encode()

y_train <- pep_dat %>%
filter(data_type == "train") %>%
pull(label_num) %>%
array()

x_test <- pep_dat %>%
filter(data_type == "test") %>%
pull(peptide) %>%
pep_encode()

y_test <- pep_dat %>%
filter(data_type == "test") %>%
pull(label_num) %>%
array()

dim(x_train)


x_train <- array_reshape(x_train, c(nrow(x_train), 9 * 20))
x_test <- array_reshape(x_test, c(nrow(x_test), 9 * 20))

y_train <- to_categorical(y_train, num_classes = 3)
y_test <- to_categorical(y_test, num_classes = 3)

model <- keras_model_sequential() %>%
layer_dense(units = 180, activation = "relu", input_shape = 180) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 90, activation = "relu") %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 3, activation = "softmax")

summary(model)

model %>%
compile(
loss = "categorical_crossentropy",
optimizer = optimizer_rmsprop(),
metrics = c("accuracy")
)

history <- model %>%
fit(
x_train, y_train,
epochs = 20,
batch_size = 64,
validation_split = 0.2
)



library(ggplot2)
plot(history)

perf <- model %>%
evaluate(x_test, y_test)
perf


y_pred <- model %>%
predict_classes(x_test)

y_real <- y_test %>%
apply(1, function(x) {
return(which(x == 1) - 1)
})

results <- tibble(
y_real = y_real %>% factor(levels = 2:0),
y_pred = y_pred %>% factor(levels = 0:2),
Correct = if_else(y_real == y_pred, "yes", "no") %>% factor()
)

library(glue)

results %>%
ggplot(aes(x = y_pred, y = y_real, fill = Correct)) +
# geom_point() +
geom_tile(aes(fill = Correct), stat = "sum") +
geom_jitter(alpha = 0.5) +
ggtitle(
label = "Performance on 10% unseen data - Feed Forward Neural Network",
subtitle = glue::glue("Accuracy = {round(perf$acc, 3) * 100}%")
) +
xlab(
"Measured\n(Real class, as predicted by netMHCpan-4.0)"
) +
ylab(
"Predicted\n(Class assigned by Keras / TensorFlow model)"
) +
scale_fill_manual(
labels = c("No", "Yes"),
values = c("red", "blue")
) +
theme_bw()


# save model for deployment -----------------------------------------------

model %>% tfdeploy::export_savedmodel("saved_models")

0 comments on commit 0ff1530

Please sign in to comment.