-
Notifications
You must be signed in to change notification settings - Fork 19
/
make_rdata.R
535 lines (476 loc) · 14.4 KB
/
make_rdata.R
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
library(readr)
library(rjson)
library(RCurl)
library(ggplot2)
library(cowplot)
library(scales)
# source("https://bioconductor.org/biocLite.R")
# biocLite("org.Hs.eg.db")
library(org.Hs.eg.db)
#
# ITFP ------------------------------------------------------------------------
dat <- read.delim(
"data-raw/ITFP/regulated_genes.tsv.gz", stringsAsFactors = FALSE)
# TF_name TF_target score target_type
# 1 AAAS ACSF3 0.0431869 normal gene
# 2 AAAS APRT 0.0458595 normal gene
# 3 AAAS C12orf52 0.0437940 normal gene
# 4 AAAS FBXL19 0.0442063 transcription factor
# 5 AAAS G6PC3 0.0466908 normal gene
# 6 AAMP NLE1 0.0431282 transcription factor
# Make a list of vectors.
ITFP <- list()
for (tf in unique(dat$TF_name)) {
targets <- sort(unique(dat[dat$TF_name == tf, "TF_target"]))
ITFP[[tf]] <- as.character(targets)
}
d <- data.frame(Length = sapply(ITFP, length))
ggplot() +
geom_histogram(
data = d,
mapping = aes(x = Length),
binwidth = 0.05
) +
geom_rug(
data = d,
mapping = aes(x = Length),
color = "grey30"
) +
scale_x_log10(labels = comma) +
scale_y_continuous(labels = round) +
theme_cowplot(font_size = 20) +
labs(
x = "Target Genes",
y = "Transcription Factors",
title = "Number of target genes per transcription factor"
)
ggsave(
filename = "figures/ITFP_histogram.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
#
# TRED ------------------------------------------------------------------------
dat <- read.delim("data-raw/TRED/targets.tsv.gz", stringsAsFactors = FALSE)
gid <- read.delim("data-raw/TRED/gid_entrez.tsv.gz", stringsAsFactors = FALSE)
# Factor Gene Species Map_Location Best_Promoter Best_Promoter_Quality Best_Binding_Quality gid
# 1 ESR2 GRIN2D human, Homo sapiens 19q13.1-ter 21058 3.1: refseq,predicted known 14701
# 2 ESR2 XPMC2H human, Homo sapiens 9q34.3 42252 2: known likely 29252
# 3 ESR2 C3 human, Homo sapiens 19p13.3-13.2 22660 2: known known 15701
# 4 ESR2 SMARCA1 human, Homo sapiens Xq25 44093 3.1: refseq,predicted likely 30486
# 5 ESR2 MKNK2 human, Homo sapiens 19p13.3 22816 3.1: refseq,predicted known 15811
# 6 ESR2 OXT human, Homo sapiens 20p13 26006 2: known likely 18040
# Discard non-human.
# table(dat$Species)
dat <- dat[dat$Species == "human, Homo sapiens", ]
dat <- merge(dat, gid, by = "gid")
sum(is.na(dat$entrez)) # 202
dat[is.na(dat$entrez), c("Factor", "Gene", "Map_Location", "gid", "entrez")]
# Manually enter missing Entrez Gene IDs.
# dat[is.na(dat$entrez), c("Factor", "Gene", "gid", "entrez")]
dat[dat$Gene == "PCNA p120", ]$entrez <- 5111
dat[dat$Gene == "DHFR", ]$entrez <- 1719
dat[dat$Gene == "INSIG1", ]$entrez <- 3638
dat[dat$Gene == "SSX8", ]$entrez <- 280659
dat[dat$Gene == "CYP11B2", ]$entrez <- 1585
dat[dat$Gene == "ERVWE1", ]$entrez <- 30816
dat[dat$Gene == "ACDC", ]$entrez <- 9370
dat[dat$Gene == "TRPM2", ]$entrez <- 7226
dat[dat$Gene == "PIGB", ]$entrez <- 9488
dat[dat$Gene == "TU12B1-TY", ]$entrez <- 51559
dat[dat$Gene == "IKBKG", ]$entrez <- 8517
dat[dat$Gene == "SPRR1A", ]$entrez <- 6698
# Fix erroneous Entrez Gene IDs.
# dat[dat$Factor == "c-Myc", ]$entrez <- 4609
# Discard records without an Entrez Gene ID.
dat <- dat[!is.na(dat$entrez), ]
TRED <- list()
for (tf in unique(dat$Factor)) {
targets <- sort(unique(dat[dat$Factor == tf, "entrez"]))
TRED[[tf]] <- as.character(targets)
}
TRED_entrezids <- mapIds(
x = org.Hs.eg.db,
keys = names(TRED),
column = "ENTREZID",
keytype = "ALIAS"
)
d <- data.frame(Length = sapply(TRED, length))
ggplot() +
geom_histogram(
data = d,
mapping = aes(x = Length),
binwidth = 0.05
) +
geom_rug(
data = d,
mapping = aes(x = Length),
color = "grey30"
) +
scale_x_log10(labels = comma) +
scale_y_continuous(labels = round) +
theme_cowplot(font_size = 20) +
labs(
x = "Target Genes",
y = "Transcription Factors",
title = "Number of target genes per transcription factor"
)
ggsave(
filename = "figures/TRED_histogram.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
#
# ENCODE ----------------------------------------------------------------------
library(GenomicRanges)
genes <- read.delim("data-raw/UCSC/knownGene.txt.gz", header = FALSE)
entrez_ids <- read.delim("data-raw/UCSC/knownToLocusLink.txt.gz", header = FALSE)
genes <- merge(genes, entrez_ids, by = "V1")
genes <- GRanges(
seqnames = Rle(genes$V2.x),
ranges = IRanges(
start = genes$V4,
end = genes$V5
),
strand = Rle(genes$V3),
mcols = data.frame(
ucsc_id = genes$V1,
entrez_id = genes$V2.y
)
)
# Add 2000 bases upstream of each gene.
genes <- resize(genes, width(genes) + 2000L, fix = "end")
# Add 200 bases downstream of each gene.
genes <- resize(genes, width(genes) + 200L, fix = "start")
peaks <- read.delim(
"data-raw/UCSC/wgEncodeRegTfbsClusteredV3.bed.gz",
header = FALSE, stringsAsFactors = FALSE,
colClasses = c("character", "numeric", "numeric", "character", "integer",
"integer", "character", "character")
)
colnames(peaks) <- c(
"chrom", "start", "end", "name", "score", "expCount", "expNums", "expScores")
peaks <- GRanges(
seqnames = Rle(peaks$chrom),
ranges = IRanges(
start = peaks$start,
end = peaks$end
),
mcols = data.frame(
TF = peaks$name,
Score = peaks$score,
expCount = peaks$expCount
)
)
fibroblast_dnase <- read.delim(
"~/work/fibroblasts/inst/extdata/ENCFF001UUQ.narrowPeak.gz",
header = FALSE, stringsAsFactors = FALSE,
colClasses = c("character", "numeric", "numeric", "character", "integer",
"character", "numeric", "numeric", "numeric", "numeric")
)
colnames(fibroblast_dnase) <- c(
"chrom", "start", "end", "name", "score", "strand",
"signalValue", "pValue", "qValue", "peak")
f_peaks <- GRanges(
seqnames = Rle(fibroblast_dnase$chrom),
ranges = IRanges(
start = fibroblast_dnase$start,
end = fibroblast_dnase$end
),
mcols = data.frame(
Score = fibroblast_dnase$score,
pValue = fibroblast_dnase$pValue
)
)
# Overlap fibroblast Dnase-seq peaks with ChIP-seq peaks.
# Filters:
# - Require a p-value < 1e-8.
peaks_subset <- subsetByOverlaps(
peaks,
f_peaks[f_peaks$mcols.pValue > 8, ]
)
# Scores are scaled such that 1000 is the mean score + 1 standard deviation.
# Also, scores are capped at 1000.
# Filters:
# - Require a score of 1000. This is probably too strict: you can change it.
# - Require that the peak is found in at least 5 experiments.
hits <- findOverlaps(
genes,
peaks_subset[
peaks_subset$mcols.expCount > 10 & peaks_subset$mcols.Score >= 1000, ]
)
dat <- data.frame(
Gene = genes[queryHits(hits)]$mcols.entrez_id,
TF = peaks[subjectHits(hits)]$mcols.TF
)
ENCODE <- list()
for (tf in unique(dat$TF)) {
targets <- sort(unique(dat[dat$TF == tf, "Gene"]))
ENCODE[[tf]] <- as.character(targets)
}
ENCODE_entrezids <- mapIds(
x = org.Hs.eg.db,
keys = names(ENCODE),
column = "ENTREZID",
keytype = "ALIAS"
)
d <- data.frame(Length = sapply(ENCODE, length))
ggplot() +
geom_histogram(
data = d,
mapping = aes(x = Length),
binwidth = 0.05
) +
geom_rug(
data = d,
mapping = aes(x = Length),
color = "grey30"
) +
scale_x_log10(labels = comma) +
scale_y_continuous(labels = round) +
theme_cowplot(font_size = 20) +
labs(
x = "Target Genes",
y = "Transcription Factors",
title = "Number of target genes per transcription factor\nCell type: fibroblast"
)
ggsave(
filename = "figures/ENCODE_histogram.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
#
# Neph2012 --------------------------------------------------------------------
celltypes <- basename(list.dirs("data-raw/Neph2012/human_2013-09-16/"))
celltypes <- celltypes[2:length(celltypes)]
# Convert gene symbols to Entrez Gene IDs.
# (I used mygene.info to do the conversion. See "mygene" at Bioconductor.)
dat <- read.delim(
"data-raw/Neph2012/human_2013-09-16/gene2entrez.tsv",
stringsAsFactors = FALSE
)
gene2entrez <- dat$entrezgene
names(gene2entrez) <- dat$query
Neph2012 <- list()
# allgenes <- c()
for (celltype in celltypes) {
dat <- read.delim(file.path(
"data-raw/Neph2012/human_2013-09-16", celltype, "genes.regulate.genes.bz2"
), header = FALSE, stringsAsFactors = FALSE)
for (g1 in unique(dat$V1)) {
targets <- sort(unique(dat[dat$V1 == g1, "V2"]))
targets <- targets[targets %in% names(gene2entrez)]
targets <- sort(unique(unname(gene2entrez[targets])))
Neph2012[[celltype]][[g1]] <- as.character(targets)
}
# allgenes <- sort(unique(c(allgenes, dat$V1, dat$V2)))
}
d <- data.frame(Length = sapply(Neph2012[[1]], length))
ggplot() +
geom_histogram(
data = d,
mapping = aes(x = Length),
binwidth = 0.05
) +
geom_rug(
data = d,
mapping = aes(x = Length),
color = "grey30"
) +
scale_x_log10(labels = comma) +
theme_cowplot(font_size = 20) +
labs(
x = "Target Genes",
y = "Transcription Factors",
title = sprintf(
"Number of target genes per transcription factor\nCell type: %s",
names(Neph2012)[1]
)
)
ggsave(
filename = "figures/Neph2012_1_histogram.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
#
# StringDB --------------------------------------------------------------------
# http://string-db.org/api/psi-mi-tab/interactionsList?identifiers=9606.ENSP00000239849&required_score=900
# library(biomaRt)
# mart <- useMart(biomart = "ensembl", dataset = "hsapiens_gene_ensembl")
# results <- getBM(
# attributes = c("ensembl_gene_id", "ensembl_peptide_id"),
# # filters = "ensembl_gene_id",
# # values = "ENSG00000136997",
# filters = "ensembl_peptide_id",
# values = "ENSP00000364895",
# mart = mart
# )
# results
ensembl <- read_tsv(
file = "data-raw/string-db/ensembl_gene_protein_transcript.txt.gz")
colnames(ensembl) <- c("gene", "protein", "transcript")
ensembl <- ensembl[ensembl$protein != "",]
# gene_to_protein <- with(ensembl, split(protein, gene))
# hist(sapply(gene_to_protein, length))
# table(sapply(gene_to_protein, length))
protein_to_gene <- with(ensembl, split(gene, protein))
protein_to_gene <- protein_to_gene[sapply(protein_to_gene, length) == 1]
protein_to_gene <- unlist(protein_to_gene)
# x <- one_to_one(ensembl, "protein", "gene", exclude = TRUE)
links <- read_delim(
file = "data-raw/string-db/9606.protein.links.v10.txt.gz", delim = " ")
links <- subset(links, combined_score > 500)
links$protein1 <- substr(links$protein1, 6, 100)
links$protein2 <- substr(links$protein2, 6, 100)
links <- links[links$protein1 %in% names(protein_to_gene),]
links <- links[links$protein2 %in% names(protein_to_gene),]
links$gene1 <- protein_to_gene[links$protein1]
links$gene2 <- protein_to_gene[links$protein2]
stringdb <- split(links$gene1, links$gene2)
stringdb <- lapply(stringdb, function(x) sort(x))
# stringdb2 <- split(links$gene2, links$gene1)
# stringdb2 <- lapply(stringdb2, function(x) sort(x))
#
# all.equal(stringdb, stringdb2) # TRUE
# length(stringdb) # 2649
# hist(sapply(stringdb, length))
#
# TRRUST ----------------------------------------------------------------------
tr_file <- "data-raw/TRRUST/trrust_rawdata.txt.gz"
tr <- read_tsv(tr_file, col_names = c("tf", "target", "type", "pubmed"))
TRRUST <- split(tr$target, tr$tf)
TRRUST_TYPE <- split(tr$type, tr$tf)
TRRUST_PUBMED <- split(tr$pubmed, tr$tf)
d <- data.frame(Length = sapply(TRRUST, length))
ggplot() +
geom_histogram(
data = d,
mapping = aes(x = Length),
binwidth = 0.05
) +
geom_rug(
data = d,
mapping = aes(x = Length),
color = "grey30"
) +
scale_x_log10(labels = comma) +
theme_cowplot(font_size = 20) +
labs(
x = "Target Genes",
y = "Transcription Factors",
title = "Number of target genes per transcription factor"
)
ggsave(
filename = "figures/TRRUST_histogram.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
#
# Marbach2016 -----------------------------------------------------------------
#
# http://regulatorycircuits.org
#
# Tissue-specific regulatory circuits reveal variable modular perturbations
# across complex diseases. (PDF, SI)
# Marbach D, Lamparter D, Quon G, Kellis M, Kutalik Z, and Bergmann S.
# Nature Methods, 13, 366-370, 2016.
regc_file <- "data-raw/Marbach2016/FANTOM5_individual_networks/394_individual_networks/synoviocyte.txt.bz2"
regc <- read_tsv(regc_file, col_names = c("tf", "target", "weight"))
Marbach2016 <- split(regc$target, regc$tf)
Marbach2016_weight <- split(regc$weight, regc$tf)
d <- data.frame(Length = sapply(Marbach2016, length))
ggplot() +
geom_histogram(
data = d,
mapping = aes(x = Length),
binwidth = 0.05
) +
geom_rug(
data = d,
mapping = aes(x = Length),
color = "grey30"
) +
scale_x_log10(labels = comma) +
theme_cowplot(font_size = 20) +
labs(
x = "Target Genes",
y = "Transcription Factors",
title = "Number of target genes per transcription factor"
)
ggsave(
filename = "figures/Marbach2016_histogram.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
d <- data.frame(Weight = regc$weight)
ggplot() +
geom_histogram(
data = d,
mapping = aes(x = Weight),
binwidth = 0.05
) +
scale_x_log10(labels = comma) +
scale_y_continuous(labels = comma) +
theme_cowplot(font_size = 20) +
labs(
x = "Target Gene Weight",
y = "Genes",
title = "Distribution of all target gene weights"
)
ggsave(
filename = "figures/Marbach2016_weights_histogram.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
d <- data.frame(
x = sapply(Marbach2016, length),
y = sapply(Marbach2016_weight, mean)
)
ggplot() +
geom_point(
data = d,
mapping = aes(x, y)
) +
scale_x_log10(breaks = log_breaks(n = 6, base = 10), labels = comma) +
scale_y_log10(breaks = pretty_breaks(n = 6)) +
theme_cowplot(font_size = 20) +
labs(
x = "Number of Target Genes",
y = "Mean Target Gene Weight",
title = "Number of Target Genes vs. Mean Weight of Target Genes"
)
ggsave(
filename = "figures/Marbach2016_targets_vs_weight.png",
width = 12,
height = 6,
units = "in",
dpi = 72
)
#
# Save the workspace ----------------------------------------------------------
save(
list = c(
"ITFP",
"TRED",
"ENCODE",
"Neph2012",
"TRRUST",
"Marbach2016"
),
compress = "bzip2",
file = "data/tftargets.rda"
)