-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01_preprocessing.Rmd
193 lines (156 loc) · 4.28 KB
/
01_preprocessing.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
---
title: "Data preprocessing"
author: "Sergio Picart-Armada"
date: "October 2, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Getting started
```{r}
library(STRINGdb)
library(igraph)
library(ggplot2)
library(diffuStats)
source("config.R")
colname_symbol <- "gene_symbol"
# compute largest connected component
largest_cc <- function(g) {
gclust <- igraph::clusters(g)
igraph::induced.subgraph(
g, which(gclust$membership == which.max(gclust$csize))
)
}
```
# Reading STRING network and disease genes
```{r}
# read tabular file with gene-disease data
# IMPORTANT: keep quote = "" to disable quotation, as
# gene names can have quotes that malform the object
df_raw_alzh <- read.table(
file = file_alzh, header = T, sep = "\t",
stringsAsFactors = FALSE, quote = "")
colnames(df_raw_alzh)[1] <- colname_symbol
sum(is.na(df_raw_alzh))
# retrieve string db
string_db <- do.call(
STRINGdb$new, params_string
)
# network object
string_g <- string_db$get_graph()
string_g
```
# Mapping genes to ENSEMBL
```{r}
# Map genes to STRING
df_map <- string_db$map(
df_raw_alzh,
my_data_frame_id_col_names = colname_symbol)
nrow(na.omit(df_map))/nrow(df_map)
# we only lose 3% of the ids
df_map <- na.omit(df_map)
nrow(df_map)
# are ensembl ids unique?
# stopifnot(length(unique(df_map$STRING_id)) == nrow(df_map))
View(plyr::ddply(df_map, "STRING_id", function(df) if (nrow(df) > 1) df))
df_map <- plyr::ddply(
df_map, "STRING_id",
function(df) {
n <- nrow(df)
if (n == 1) return(df)
ord <- order(
df$known_drug_binary, # pick genes with drugs in case of ties
runif(n), # break ties at random
decreasing = TRUE)
df[ord[1], , drop = FALSE]
},
.progress = "text"
)
dim(df_map)
```
# Filtering network and assessing coverage
```{r}
# same filters as in takeda
list.edge.filters <- list(
Net1 = "combined_score < 400 | experiments < 1",
Net2 = "experiments < 600",
Net3 = "experiments < 400 & database < 400",
Net4 = "combined_score < 700 | (experiments < 1 & database < 1)",
Net5 = "combined_score < 700 | (experiments < 1 & database < 1 & textmining < 900)",
Net6 = "database < 400"
)
df_coverage <- plyr::ldply(
list.edge.filters,
function(quote.char) {
# browser()
# quote.char2 <- quote.char
g <- delete_edges(
string_g,
E(string_g)[eval(parse(text = quote.char), envir = environment())]
) %>% largest_cc
data.frame(
filter = quote.char,
nodes = vcount(g),
edges = ecount(g),
coverage = sum(df_map$STRING_id %in% V(g)$name),
stringsAsFactors = FALSE
)
},
.id = "network"
)
df_coverage
# Plot the coverage to decide
ggplot(df_coverage, aes(x = nodes, y = coverage)) +
geom_text(aes(label = network)) +
geom_smooth(method = "lm")
```
# Save network
```{r}
# Pick network 4: equilibrium between mapped genes and simplicity
choice_net <- "Net4"
choice_filter <- list.edge.filters[choice_net]
# delete edges not meeting the filter
g_filter <- delete_edges(
string_g,
E(string_g)[eval(parse(text = choice_filter),
envir = environment())]
) %>% largest_cc
E(g_filter)$weight <- E(g_filter)$combined_score
g_filter$choice_filter <- choice_filter
g_filter$params <- params_string
# final graph
g_filter
stopifnot(is.simple(g_filter))
stopifnot(is.connected(g_filter))
# add the dataset that maps to the graph
g_filter$dataset <- subset(df_map, STRING_id %in% V(g_filter)$name)
# delete the edge attributes other than the weight
attr_edge <- list.edge.attributes(g_filter)
for (att in setdiff(attr_edge, "weight")) {
g_filter <- delete_edge_attr(g_filter, att)
}
# save the graph with the dataset
save(g_filter, file = graph_alzh)
```
# Compute kernel (computationally intensive)
```{r}
if (!file.exists(file_kernel)) {
# Compute kernel
# last time: ~1000s
kernel_time <- system.time({
K <- diffuStats::regularisedLaplacianKernel(
graph = g_filter, normalized = FALSE
)
})
kernel_time
# save it. Standard compression; tried xz but it is not
# worth it and takes too long
save(K, file = paste0(dir_kernel, "/", choice_net, ".RData"))
}
```
# Reproducibility
```{r}
out <- capture.output(sessionInfo())
writeLines(out, con = paste0(dir_metadata, "/01_sessionInfo.txt"))
```