-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.1_timepointSpecificGenes.Rmd
568 lines (477 loc) · 27.8 KB
/
1.1_timepointSpecificGenes.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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
---
title: "RNA-seq Mouse Placenta"
author: "Ha T. H. Vu"
output: html_document
---
```{r setup, include=FALSE}
options(max.print = "75")
knitr::opts_chunk$set(
echo = TRUE,
collapse = TRUE,
comment = "#>",
fig.path = "Files/",
fig.width = 8,
prompt = FALSE,
tidy = FALSE,
message = FALSE,
warning = TRUE
)
knitr::opts_knit$set(width = 75)
```
Documentation for the analysis of mouse placental RNA-seq data at e7.5, e8.5 and e9.5.
## 1. Hierarchical clustering analysis
We cluster transcripts with hierarchical clustering. Transcripts with low raw counts (mean raw counts < 20 in more than one time point) were filtered out, and expression data (in TPM) was scaled and re-centered. Hierarchical clustering was performed in the top 75% most variable protein coding transcripts (23,571 transcripts total). <br>
First, we filter transcripts based on raw counts.
```{r}
library("dplyr", suppressMessages())
library("tidyverse", suppressMessages())
library("dendextend", suppressMessages())
library("matrixStats", suppressMessages())
###building transcript names =====
t2g <- read.table("Files/t2g.txt", header = T, sep = "\t")
t2g <- t2g[order(t2g$target_id),]
#load protein-coding transcripts
coding <- read.table("Files/Mus_musculus_grcm38_coding_transcripts.txt", header = F)
###filter based on raw est. counts and remove outliers =====
est_counts <- read.csv("Files/estCounts_allSamples.tsv", header = TRUE, sep = "\t", stringsAsFactors=FALSE)
#reformat count file
est_counts <- est_counts[order(est_counts$target_id),]
rownames(est_counts) <- est_counts$target_id #make transcript names to be row names
est_counts <- est_counts[, -c(1, 20, 21)] #exclude non-count columns
est_counts <- est_counts[, -which(names(est_counts) %in% c("E7.5_1", "E9.5_6"))] #remove outliers
mean_cts <- data.frame(trans=rownames(est_counts),
E7.5_mean=NA,
E8.5_mean=NA,
E9.5_mean=NA)
mean_cts$E7.5_mean <- rowMeans(est_counts[, c("E7.5_2", "E7.5_3", "E7.5_4", "E7.5_5", "E7.5_6")])
mean_cts$E8.5_mean <- rowMeans(est_counts[, c("E8.5_1", "E8.5_2", "E8.5_3", "E8.5_4", "E8.5_5", "E8.5_6")])
mean_cts$E9.5_mean <- rowMeans(est_counts[, c("E9.5_1", "E9.5_2", "E9.5_3", "E9.5_4", "E9.5_5")])
row.names(mean_cts) <- mean_cts$trans
mean_cts <- mean_cts[,2:ncol(mean_cts)]
#filter low count transcripts
basic_filter <- function (row, min_reads = 20, min_prop = 1/3) {
mean(row >= min_reads) >= min_prop
}
keep <- apply(mean_cts, 1, basic_filter)
```
Next, transform TPM values of transcripts for clustering.
```{r}
###load TPM file in to do clustering =====
tpm <- read.csv("Files/TPM_allSamples.tsv", header = TRUE, sep = "\t", stringsAsFactors=FALSE)
tpm <- tpm[order(tpm$target_id),]
row.names(tpm) <- tpm$target_id
tpm <- tpm[,-c(1, 20, 21)]
tpm <- tpm[, -which(names(tpm) %in% c("E7.5_1", "E9.5_6"))]
tpm[] <- lapply(tpm, function(x) as.numeric(as.character(x)))
dim(tpm)[1] #number of transcripts originally
#filter out the low count transcripts according to the est_counts filtering above
tpm <- tpm[keep, ]
dim(tpm)[1] #number of transcripts after basic filtering
mean_tpm <- data.frame(trans=rownames(tpm),
E7.5_mean=NA,
E8.5_mean=NA,
E9.5_mean=NA)
mean_tpm$E7.5_mean <- rowMeans(tpm[, c("E7.5_2", "E7.5_3", "E7.5_4", "E7.5_5", "E7.5_6")])
mean_tpm$E8.5_mean <- rowMeans(tpm[, c("E8.5_1", "E8.5_2", "E8.5_3", "E8.5_4", "E8.5_5", "E8.5_6")])
mean_tpm$E9.5_mean <- rowMeans(tpm[, c("E9.5_1", "E9.5_2", "E9.5_3", "E9.5_4", "E9.5_5")])
row.names(mean_tpm) <- mean_tpm$trans
mean_tpm <- mean_tpm[,2:ncol(mean_tpm)]
mean_tpm$var <- rowVars(as.matrix(mean_tpm)) #calculate the variance
mean_tpm_keep <- subset(mean_tpm, mean_tpm$var > quantile(mean_tpm$var, 0.25)) #keep the top 75% most variable transcripts
tpm1 <- subset(tpm, rownames(tpm) %in% rownames(mean_tpm_keep)) #keep the top 75% most variable transcripts
dim(tpm1)[1]
tpm1 <- tpm1[which(row.names(tpm1) %in% coding$V1),] #keep only coding transcripts
tpm1[] <- lapply(tpm1, function(x) as.numeric(as.character(x)))
#center and scale the data, for further analysis
tpm2 <- apply(tpm1, 1, function(x) (x-mean(x))/sd(x))
tpm2 <- t(tpm2)
tpm2 <- as.data.frame(tpm2)
dim(tpm2)[1] #number of transcripts for downstream
#write.table(tpm2, "Files/tpmForClustering.txt", quote = F, row.names = T, sep = "\t")
```
After filtering and getting 23571 protein-coding transcripts, we carry out hierarchical clustering.
```{r}
###hierarchical clustering =====
hc <- hclust(dist(tpm2, method = "euclidean"), "complete")
#save(hc, file = "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2B_hierarchicalClustering/hierClust.rda")
dend <- as.dendrogram(hc)
#save(dend, file = "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2B_hierarchicalClustering/dendrogram.rda")
```
To identify groups, we cut the tree at the highest level and obtain 3 groups. The number of transcripts in each group is: <br>
- Group e7.5: 8242 transcripts (= 5566 genes) <br>
- Group e8.5: 8091 transcripts (= 5536 genes) <br>
- Group e9.5: 7238 transcripts (= 5347 genes) <br>
```{r}
#cutree to make clusters
trans_cluster <- cutree(hc, k = 3) %>% enframe()
table(trans_cluster$value) #number of transcripts in each group
```
Next, we summarize the transcripts per group with mean scaled TPM for plotting purposes.
```{r, eval = F}
###summarize the time points by mean scaled tpm ====
tpm3 <- tpm2
tpm3 <- cbind(rownames(tpm2), tpm3)
rownames(tpm3) <- 1:nrow(tpm3)
colnames(tpm3)[1] <- "transcripts"
E7.5_mean <- rowMeans(tpm3[,c("E7.5_2", "E7.5_3", "E7.5_4", "E7.5_5", "E7.5_6")])
E8.5_mean <- rowMeans(tpm3[,c("E8.5_1", "E8.5_2", "E8.5_3", "E8.5_4", "E8.5_5", "E8.5_6")])
E9.5_mean <- rowMeans(tpm3[,c("E9.5_1", "E9.5_2", "E9.5_3", "E9.5_4", "E9.5_5")])
#e7.5
e7.5_table <- data.frame(tpm3$transcripts, E7.5_mean)
e7.5_table$time <- c("e7.5")
rownames(e7.5_table) <- 1:nrow(e7.5_table)
colnames(e7.5_table) <- c("transcripts", "mean_cts_scaled", "time")
#e8.5
e8.5_table <- data.frame(tpm3$transcripts, E8.5_mean)
e8.5_table$time <- c("e8.5")
rownames(e8.5_table) <- 1:nrow(e8.5_table)
colnames(e8.5_table) <- c("transcripts", "mean_cts_scaled", "time")
#e9.5
e9.5_table <- data.frame(tpm3$transcripts, E9.5_mean)
e9.5_table$time <- c("e9.5")
rownames(e9.5_table) <- 1:nrow(e9.5_table)
colnames(e9.5_table) <- c("transcripts", "mean_cts_scaled", "time")
summary <- rbind(rbind(e7.5_table, e8.5_table), e9.5_table)
summary <- summary[order(summary$transcripts),]
row.names(summary) <- 1:nrow(summary)
summary <- inner_join(summary, trans_cluster, by = c("transcripts" = "name"))
summary <- inner_join(summary, t2g, by = c("transcripts" = "target_id"))
#write.table(summary, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2B_hierarchicalClustering/transcriptGroups.txt", sep = "\t", row.names = F, quote = F)
#group 1
geneGroup1 <- subset(summary, summary$value == "1")
length(unique(geneGroup1$transcripts))
length(unique(geneGroup1$ens_gene))
#write.table(geneGroup1, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2B_hierarchicalClustering/e8.5Group.txt", sep = " ", quote = F)
#group 2
geneGroup2 <- subset(summary, summary$value == "2")
length(unique(geneGroup2$transcripts))
length(unique(geneGroup2$ens_gene))
#write.table(geneGroup2, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2B_hierarchicalClustering/e9.5Group.txt", sep = " ", quote = F)
#group 3
geneGroup3 <- subset(summary, summary$value == "3")
length(unique(geneGroup2$transcripts))
length(unique(geneGroup3$ens_gene))
#write.table(geneGroup3, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2B_hierarchicalClustering/e7.5Group.txt", sep = " ", quote = F)
```
## 2. Differential expression analysis:
We carry out differential expression analysis across the three timepoints and identified transcripts and genes with the strongest changes over time. It is possible to run this analysis with a personal laptop, but I'd recommend running it on HPC. <br>
We set up the analysis by loading some necessary files.
```{r, eval = F}
library("sleuth")
library("dplyr")
set.seed(123)
###building sample description
dir <- "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/1_kallisto/official/"
sample_id <- dir(file.path(dir))
sample_id <- sample_id[grep("^S", sample_id)] #remove the unrelated file if there is
kal_dirs <- file.path(dir, sample_id, "abundance.h5")
desc <- read.table("Files/0.0_sampleNameMapping.csv", sep = "\t", header = F)
desc$condition <- substr(desc$V1, 1, 4)
desc <- desc[order(desc$V2),]
desc <- dplyr::mutate(desc, path = kal_dirs)
desc <- desc[,2:ncol(desc)]
colnames(desc) <- c("sample", "condition", "path")
desc <- desc[order(desc$condition), ]
desc <- subset(desc, !(desc$ID %in% c("S55", "S56"))) #remove sample S55, S56 outlier
e7.5vsE8.5 <- subset(desc, (desc$condition %in% c("E7.5", "E8.5")))
e8.5vsE9.5 <- subset(desc, (desc$condition %in% c("E9.5", "E8.5")))
e7.5vsE9.5 <- subset(desc, (desc$condition %in% c("E7.5", "E9.5")))
###building transcript name table. Since the object is presaved, just load it here
t2g <- read.table("Files/t2g.txt", header = T, sep = "\t")
#load protein-coding transcripts
coding <- read.table("Files/Mus_musculus_grcm38_coding_transcripts.txt", header = F)
```
Build functions to run DEG analysis. The `deg()` function below is one which uses likelihood ratio test in Sleuth to identify DE genes and transcripts. It will return a result table (named `deg`) with DE protein-coding transcripts that belong to DE genes, and a table of all transcripts + necessary metrics (named `trans`), for plotting.
```{r, eval = F}
deg <- function(description, targetMap, con1, con2){
#### 1.1. gene level
so <- sleuth_prep(description, target_mapping = targetMap, aggregation_column = "ens_gene", extra_bootstrap_summary = TRUE)
so <- sleuth_fit(so, ~condition, 'full')
so <- sleuth_fit(so, ~1, 'reduced')
so <- sleuth_lrt(so, 'reduced', 'full')
sleuth_table <- sleuth_results(so, 'reduced:full', 'lrt', show_all = FALSE, pval_aggregate = so$pval_aggregate)
sleuth_table <- dplyr::filter(sleuth_table, qval <= 0.05)
#### 1.2. transcript level
soTrans <- sleuth_prep(description, target_mapping = targetMap, extra_bootstrap_summary = TRUE)
soTrans <- sleuth_fit(soTrans, ~condition, 'full')
soTrans <- sleuth_fit(soTrans, ~1, 'reduced')
soTrans <- sleuth_lrt(soTrans, 'reduced', 'full')
sleuth_tableTrans <- sleuth_results(soTrans, 'reduced:full', 'lrt', show_all = FALSE, pval_aggregate = soTrans$pval_aggregate)
## finding up/down regulated transcripts
cond1 <- description$sample[description$condition == con1]
cond2 <- description$sample[description$condition == con2]
raw_cond1 <- subset(soTrans$obs_raw, soTrans$obs_raw$sample %in% cond1)
raw_cond2 <- subset(soTrans$obs_raw, soTrans$obs_raw$sample %in% cond2)
#calculate mean TPM of each transcript across all samples
means_cond1 <- aggregate(tpm~target_id, data = raw_cond1, FUN=function(x) c(mean=mean(x)))
colnames(means_cond1) <- c("target_id", "TPM_con1")
means_cond2 <- aggregate(tpm~target_id, data = raw_cond2, FUN=function(x) c(mean=mean(x)))
colnames(means_cond2) <- c("target_id", "TPM_con2")
merged_means <- merge(means_cond1, means_cond2, by = c("target_id"))
sleuth_tableTrans_TPM <- left_join(sleuth_tableTrans, merged_means)
#calculate FC between 2 condition
sleuth_tableTrans_TPM$TPM_con1 <- sleuth_tableTrans_TPM$TPM_con1 + 1
sleuth_tableTrans_TPM$TPM_con2 <- sleuth_tableTrans_TPM$TPM_con2 + 1
sleuth_tableTrans_TPM <- transform(sleuth_tableTrans_TPM, log2FC = log2(sleuth_tableTrans_TPM$TPM_con2 / sleuth_tableTrans_TPM$TPM_con1))
#keep only DE transcripts of DE genes
sleuth_tableTrans_TPM <- subset(sleuth_tableTrans_TPM,
sleuth_tableTrans_TPM$ens_gene %in% sleuth_table$target_id
& sleuth_tableTrans_TPM$qval <= 0.05
& abs(sleuth_tableTrans_TPM$log2FC) >= log2(1.5))
sleuth_tableTrans_TPM <- sleuth_tableTrans_TPM[order(sleuth_tableTrans_TPM$ext_gene),]
#filter out non coding transcripts
sleuth_tableTrans_TPM <- sleuth_tableTrans_TPM[sleuth_tableTrans_TPM$target_id %in% coding$V1,]
sleuth_tableTrans <- sleuth_tableTrans[sleuth_tableTrans$target_id %in% coding$V1,]
results <- list("deg" = sleuth_tableTrans_TPM, "trans" = sleuth_tableTrans)
return(results)
}
```
Use the `deg()` function to analyze e7.5 vs e8.5, e8.5 vs e9.5, and e7.5 vs e9.5. Notes: There may be some warnings like this:
"NA values were found during variance shrinkage estimation due to mean observation values outside of the range used for the LOESS fit". Since the number of NA transcripts is small (1 to 6 transcripts), and the NA values won't affect the adjustment of p-values, we may ignore the warnings. <br>
For **e7.5 vs e8.5** test, run the following code. In the end, we obtain: <br>
+ Total number of DE transcripts: 2703 <br>
+ Total number of DE genes: 2325 (this number may not be equal genes-up-at-E7.5 + genes-up-at-E8.5 since one gene can be up-regulated at both timepoints if it has different transcripts up-regulated at these timepoints.) <br>
+ Number of transcripts up at e7.5: 740 <br>
+ Number of genes up at e7.5: 652 <br>
+ Number of transcripts up at e8.5: 1963 <br>
+ Number of genes up at e8.5: 1700 <br>
```{r, eval = F}
# e7.5 vs e8.5 test
e7.5_e8.5 <- deg(e7.5vsE8.5, t2g, "e7.5", "e8.5")
e7.5vsE8.5deg <- e7.5_e8.5$deg
length(unique(e7.5vsE8.5deg$target_id)) #check total number of DE transcripts
length(unique(e7.5vsE8.5deg$ens_gene)) #check total number of DE genes
e7.5_e7.5vsE8.5 <- e7.5vsE8.5deg[e7.5vsE8.5deg$log2FC <= -log2(1.5),]
length(unique(e7.5_e7.5vsE8.5$target_id)) #check total number of transcripts up-regulated at e7.5, compared to e8.5
length(unique(e7.5_e7.5vsE8.5$ens_gene)) #check total number of genes up-regulated at e7.5, compared to e8.5
#write.table(e7.5_e7.5vsE8.5, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e7.5_e7.5vsE8.5_DEtransGenes.txt", row.names = F, quote = F, sep = "\t")
e8.5_e7.5vsE8.5 <- e7.5vsE8.5deg[e7.5vsE8.5deg$log2FC >= log2(1.5),]
length(unique(e8.5_e7.5vsE8.5$target_id)) #check total number of transcripts up-regulated at e8.5, compared to e7.5
length(unique(e8.5_e7.5vsE8.5$ens_gene)) #check total number of genes up-regulated at e8.5, compared to e7.5
#write.table(e8.5_e7.5vsE8.5, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e8.5_e7.5vsE8.5_DEtransGenes.txt", row.names = F, quote = F, sep = "\t")
```
For **e7.5 vs e9.5** test: <br>
+ Number of DE transcripts: 3948 <br>
+ Number of DE genes: 3190 <br>
+ Number of transcripts up at e7.5: 1154 <br>
+ Number of genes up at e7.5: 935 <br>
+ Number of transcripts up at e9.5: 2794 <br>
+ Number of genes up at e9.5: 2264 <br>
```{r, eval=F}
# e7.5 vs e9.5 test
e7.5_e9.5 <- deg(e7.5vsE9.5, t2g, "e7.5", "e9.5")
e7.5vse9.5deg <- e7.5_e9.5$deg
length(unique(e7.5vse9.5deg$target_id)) #check total number of DE transcripts
length(unique(e7.5vse9.5deg$ens_gene)) #check total number of DE genes
e7.5_e7.5vse9.5 <- e7.5vse9.5deg[e7.5vse9.5deg$log2FC <= -log2(1.5),]
length(unique(e7.5_e7.5vse9.5$target_id)) #check total number of transcripts up-regulated at e7.5, compared to e9.5
length(unique(e7.5_e7.5vse9.5$ens_gene)) #check total number of genes up-regulated at e7.5, compared to e9.5
#write.table(e7.5_e7.5vse9.5, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e7.5_e7.5vse9.5_DEtransGenes.txt", row.names = F, quote = F, sep = "\t")
e9.5_e7.5vse9.5 <- e7.5vse9.5deg[e7.5vse9.5deg$log2FC >= log2(1.5),]
length(unique(e9.5_e7.5vse9.5$target_id)) #check total number of transcripts up-regulated at e9.5, compared to e7.5
length(unique(e9.5_e7.5vse9.5$ens_gene)) #check total number of genes up-regulated at e9.5, compared to e7.5
#write.table(e9.5_e7.5vse9.5, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e9.5_e7.5vse9.5_DEtransGenes.txt", row.names = F, quote = F, sep = "\t")
```
For **e8.5 vs e9.5** test: <br>
+ Number of DE transcripts: 1255 <br>
+ Number of DE genes: 1124 <br>
+ Number of transcripts up at e8.5: 358 <br>
+ Number of genes up at e8.5: 321 <br>
+ Number of transcripts up at e9.5: 897 <br>
+ Number of genes up at e9.5: 806 <br>
```{r, eval=F}
# e8.5 vs e9.5 test
e8.5_e9.5 <- deg(e8.5vsE9.5, t2g, "e8.5", "e9.5")
e8.5vse9.5deg <- e8.5_e9.5$deg
length(unique(e8.5vse9.5deg$target_id)) #check total number of DE transcripts
length(unique(e8.5vse9.5deg$ens_gene)) #check total number of DE genes
e8.5_e8.5vse9.5 <- e8.5vse9.5deg[e8.5vse9.5deg$log2FC <= -log2(1.5),]
length(unique(e8.5_e8.5vse9.5$target_id)) #check total number of transcripts up-regulated at e8.5, compared to e9.5
length(unique(e8.5_e8.5vse9.5$ens_gene)) #check total number of genes up-regulated at e8.5, compared to e9.5
#write.table(e8.5_e8.5vse9.5, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e8.5_e8.5vse9.5_DEtransGenes.txt", row.names = F, quote = F, sep = "\t")
e9.5_e8.5vse9.5 <- e8.5vse9.5deg[e8.5vse9.5deg$log2FC >= log2(1.5),]
length(unique(e9.5_e8.5vse9.5$target_id)) #check total number of transcripts up-regulated at e9.5, compared to e8.5
length(unique(e9.5_e8.5vse9.5$ens_gene)) #check total number of genes up-regulated at e9.5, compared to e8.5
#write.table(e9.5_e8.5vse9.5, "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e9.5_e8.5vse9.5_DEtransGenes.txt", row.names = F, quote = F, sep = "\t")
```
This part has function `deg2()` which is for plotting purpose, as for volcano plot we need insignificant transcripts also.
```{r, eval = F}
#function deg2 is for plotting purpose, as for volcano plot I needed insignificant transcripts also
deg2 <- function(description, targetMap, con1, con2){
#### 1.2. transcript level
so1trans <- sleuth_prep(description, target_mapping = targetMap, extra_bootstrap_summary = TRUE)
so1trans <- sleuth_fit(so1trans, ~condition, 'full')
so1trans <- sleuth_fit(so1trans, ~1, 'reduced')
so1trans <- sleuth_lrt(so1trans, 'reduced', 'full')
sleuth_table1trans <- sleuth_results(so1trans, 'reduced:full', 'lrt', show_all = FALSE, pval_aggregate = so1trans$pval_aggregate)
## finding up/down regulated transcripts
cond1 <- description$sample[description$condition == con1]
cond2 <- description$sample[description$condition == con2]
raw_cond1 <- subset(so1trans$obs_raw, so1trans$obs_raw$sample %in% cond1)
raw_cond2 <- subset(so1trans$obs_raw, so1trans$obs_raw$sample %in% cond2)
#calculate mean TPM of each transcript across all samples
means_cond1 <- aggregate(tpm~target_id, data = raw_cond1, FUN=function(x) c(mean=mean(x)))
colnames(means_cond1) <- c("target_id", "TPM_con1")
means_cond2 <- aggregate(tpm~target_id, data = raw_cond2, FUN=function(x) c(mean=mean(x)))
colnames(means_cond2) <- c("target_id", "TPM_con2")
merged_means <- merge(means_cond1, means_cond2, by = c("target_id"))
sleuth_tableTrans_TPM <- left_join(sleuth_table1trans, merged_means)
#calculate FC between 2 condition
sleuth_tableTrans_TPM$TPM_con1 <- sleuth_tableTrans_TPM$TPM_con1 + 1
sleuth_tableTrans_TPM$TPM_con2 <- sleuth_tableTrans_TPM$TPM_con2 + 1
sleuth_tableTrans_TPM <- transform(sleuth_tableTrans_TPM, log2FC = log2(sleuth_tableTrans_TPM$TPM_con2 / sleuth_tableTrans_TPM$TPM_con1))
return(sleuth_tableTrans_TPM)
}
e7.5_e8.5 <- deg2(e7.5vsE8.5, t2g, "e7.5", "e8.5")
#save(e7.5_e8.5, file = "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e7.5_e8.5_allTrans-forPlots.rda")
e7.5_e9.5 <- deg2(e7.5vsE9.5, t2g, "e7.5", "e9.5")
#save(e7.5_e9.5, file = "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e7.5_e9.5_allTrans-forPlots.rda")
e8.5_e9.5 <- deg2(e8.5vsE9.5, t2g, "e8.5", "e9.5")
#save(e8.5_e9.5, file = "/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/2A_sleuth/e8.5_e9.5_allTrans-forPlots.rda")
```
## 3. Timepoint-specific genes
Last, we integrate results from hierarchical clustering and DEG analysis to get timepoint-specific genes, defined as the following. <br>
a. E8.5-specific transcripts: transcripts in e8.5 hierarchical cluster, are up-regulated at e8.5 (compared to e7.5) or are up-regulated at e8.5 (compared to e9.5). E8.5-specific genes are ones associated with e8.5-specific transcripts. <br>
b. E7.5-specific transcripts: transcripts in e7.5 hierarchical cluster, are up-regulated at e7.5 (compared to e9.5), and are not in e8.5-specific group. E7.5-specific genes are ones associated with e7.5-specific transcripts. <br>
c. E9.5-specific transcripts: transcripts in e9.5 hierarchical cluster, are up-regulated at e9.5 (compared to e7.5), and are not in e8.5-specific group. E9.5-specific genes are ones associated with e9.5-specific transcripts. <br>
Eventually, we should get the number of timepoint-specific transcripts (and genes): <br>
+ e7.5: 1138 transcripts (922 genes) <br>
+ e8.5: 998 transcripts (915 genes) <br>
+ e9.5: 2356 transcripts (1952 genes) <br>
In the following code, we also get TF/co-factor genes specific to each timepoint for downstream analyses. The TF and co-factor was obtained from AnimalTFDB (version 3.0).
```{r}
TF_cofactors <- read.table("Files/mm10_TF_coTF_EnsemblID.txt")
#e8.5 specific genes
e8.5Hier <- read.table("Files/e8.5Group.txt", header = T)
e8.5_e7.5vsE8.5 <- read.table("Files/e8.5_e7.5vsE8.5_DEtransGenes.txt", header = T)
e8.5_e8.5vsE9.5 <- read.table("Files/e8.5_e8.5vse9.5_DEtransGenes.txt", header = T)
e8.5specificTrans <- intersect(union(e8.5_e7.5vsE8.5$target_id, e8.5_e8.5vsE9.5$target_id), unique(e8.5Hier$transcripts))
length(e8.5specificTrans)
#write.table(e8.5specificTrans, "Files/e8.5specific_trans.txt", row.names = F, quote = F)
e8.5specific <- unique(t2g[t2g$target_id %in% e8.5specificTrans, "ens_gene"])
length(e8.5specific)
#write.table(e8.5specific, "Files/e8.5specific_ensGenes.txt", row.names = F, quote = F)
e8.5TFs <- intersect(e8.5specific, TF_cofactors$V1)
length(e8.5TFs)
#write.table(e8.5TFs, "Files/e8.5specific_TFensGenes.txt", row.names = F, quote = F)
```
```{r}
#e7.5 specific genes
e7.5Hier <- read.table("Files/e7.5Group.txt", header = T)
e7.5_e7.5vsE9.5 <- read.table("Files/e7.5_e7.5vse9.5_DEtransGenes.txt", header = T)
e7.5specificTrans <- setdiff(intersect(e7.5_e7.5vsE9.5$target_id, e7.5Hier$transcripts), e8.5specificTrans)
length(e7.5specificTrans)
#write.table(e7.5specificTrans, "Files/e7.5specific_trans.txt", row.names = F, quote = F)
e7.5specific <- unique(t2g[t2g$target_id %in% e7.5specificTrans, "ens_gene"])
length(e7.5specific)
#write.table(e7.5specific, "Files/e7.5specific_ensGenes.txt", row.names = F, quote = F)
e7.5TFs <- intersect(e7.5specific, TF_cofactors$V1)
length(e7.5TFs)
#write.table(e7.5TFs, "Files/e7.5specific_TFensGenes.txt", row.names = F, quote = F)
```
```{r}
#e9.5 specific genes
e9.5Hier <- read.table("Files/e9.5Group.txt", header = T)
e9.5_e7.5vsE9.5 <- read.table("Files/e9.5_e7.5vse9.5_DEtransGenes.txt", header = T)
e9.5specificTrans <- setdiff(intersect(e9.5_e7.5vsE9.5$target_id, e9.5Hier$transcripts), e8.5specificTrans)
length(e9.5specificTrans)
#write.table(e9.5specificTrans, "Files/e9.5specific_trans.txt", row.names = F, quote = F)
e9.5specific <- unique(t2g[t2g$target_id %in% e9.5specificTrans, "ens_gene"])
length(e9.5specific)
#write.table(e9.5specific, "Files/e9.5specific_ensGenes.txt", row.names = F, quote = F)
e9.5TFs <- intersect(e9.5specific, TF_cofactors$V1)
length(e9.5TFs)
#write.table(e9.5TFs, "Files/e9.5specific_TFensGenes.txt", row.names = F, quote = F)
```
Gene ontology analyses for time-point specific genes:
```{r}
library("clusterProfiler")
library("org.Mm.eg.db")
go <- function(subnetworkGenes) {
GO <- enrichGO(gene = subnetworkGenes, OrgDb=org.Mm.eg.db, ont = "BP", qvalueCutoff = 1, pvalueCutoff = 1, maxGSSize=1000, readable = T, keyType = "ENSEMBL")
goSimplify <- GO
simplify2 <- as.data.frame(goSimplify)
simplify2$GeneRatio <- sapply(strsplit(simplify2$GeneRatio, "/"), function(x) as.numeric(x[1])/as.numeric(x[2]))
simplify2$BgRatio <- sapply(strsplit(simplify2$BgRatio, "/"), function(x) as.numeric(x[1])/as.numeric(x[2]))
simplify2$Fold <- as.numeric(simplify2$GeneRatio)/as.numeric(simplify2$BgRatio)
simplify2 <- simplify2[simplify2$qvalue <= 0.05 & simplify2$Fold >= 2 & simplify2$Count >= 5,]
return(simplify2)
}
#specific genes
for (i in c("e7.5", "e8.5", "e9.5")) {
genes <- read.table(paste0("/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/PlacentaRNA-seq/Files/", i, "specific_TSS.bed"), header = F)
res <- go(genes$V4)
write.table(res, paste0("/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/PlacentaRNA-seq/Files/additionalAnalyses/", i, "specific_BP_filtered.txt"), quote = F, sep = "\t", row.names = F)
}
```
E7.5-specific genes in general, and e7.5-specific genes that are unique to e7.5 timepoint (not present in any other timepoint).
```{r}
e8.5 <- read.table("Files/e8.5specific_ensGenes.txt", header = F)
e7.5 <- read.table("Files/e7.5specific_ensGenes.txt", header = F)
e9.5 <- read.table("Files/e9.5specific_ensGenes.txt", header = F)
trueE7.5 <- setdiff(setdiff(e7.5$V1, e8.5$V1), e9.5$V1)
length(e7.5$V1)
length(trueE7.5)
trueE7.5go <- go(trueE7.5)
write.table(trueE7.5go, "Files/trueE7.5go.txt", sep = "\t", quote = F, row.names = F)
multiE7.5 <- setdiff(e7.5$V1, trueE7.5)
intersect(multiE7.5, e8.5$V1)
intersect(multiE7.5, e9.5$V1)
e7.5specificTrans <- read.table("Files/e7.5specific_trans.txt", header = F)
#write.table(t2g[t2g$ens_gene %in% multiE7.5 & t2g$target_id %in% e7.5specificTrans$V1,], "Files/multiE7.5.txt", sep = "\t", quote = F, row.names = F)
multiE7.5go <- go(multiE7.5)
#write.table(multiE7.5go, "Files/multiE7.5go.txt", sep = "\t", quote = F, row.names = F)
i<-"e7.5"
genes <- read.table(paste0("/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/PlacentaRNA-seq/Files/", i, "specific_TSS.bed"), header = F)
res <- go(genes$V4)
setdiff(trueE7.5go$Description, res$Description)
setdiff(res$Description, trueE7.5go$Description)
```
E8.5-specific genes in general, and e8.5-specific genes that are unique to e8.5 timepoint (not present in any other timepoint).
```{r}
trueE8.5 <- setdiff(setdiff(e8.5$V1, e7.5$V1), e9.5$V1)
length(e8.5$V1)
length(trueE8.5)
trueE8.5go <- go(trueE8.5)
write.table(trueE8.5go, "Files/trueE8.5go.txt", sep = "\t", quote = F, row.names = F)
multiE8.5 <- setdiff(e8.5$V1, trueE8.5)
intersect(multiE8.5, e7.5$V1)
intersect(multiE8.5, e9.5$V1)
e8.5specificTrans <- read.table("Files/e8.5specific_trans.txt", header = F)
write.table(t2g[t2g$ens_gene %in% multiE8.5 & t2g$target_id %in% e8.5specificTrans$V1,], "Files/multiE8.5.txt", sep = "\t", quote = F, row.names = F)
multiE8.5go <- go(multiE8.5)
#write.table(multiE8.5go, "Files/multiE8.5go.txt", sep = "\t", quote = F, row.names = F)
i<-"e8.5"
genes <- read.table(paste0("/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/PlacentaRNA-seq/Files/", i, "specific_TSS.bed"), header = F)
res <- go(genes$V4)
setdiff(trueE8.5go$Description, res$Description)
setdiff(res$Description, trueE8.5go$Description) #only here has labyrinth terms
length(intersect(e8.5$V1, e7.5$V1))
length(intersect(e8.5$V1, e9.5$V1))
```
E9.5-specific genes in general, and e9.5-specific genes that are unique to e9.5 timepoint (not present in any other timepoint).
```{r}
trueE9.5 <- setdiff(setdiff(e9.5$V1, e8.5$V1), e7.5$V1)
length(e9.5$V1)
length(trueE9.5)
trueE9.5go <- go(trueE9.5)
write.table(trueE9.5go, "Files/trueE9.5go.txt", sep = "\t", quote = F, row.names = F)
multiE9.5 <- setdiff(e9.5$V1, trueE9.5)
intersect(multiE9.5, e7.5$V1)
intersect(multiE9.5, e8.5$V1)
e9.5specificTrans <- read.table("Files/e9.5specific_trans.txt", header = F)
write.table(t2g[t2g$ens_gene %in% multiE9.5 & t2g$target_id %in% e9.5specificTrans$V1,], "Files/multiE9.5.txt", sep = "\t", quote = F, row.names = F)
#write.table(distinct(t2g[t2g$ens_gene %in% multiE9.5, 2:3]), "Files/multiE9.5.txt", sep = "\t", quote = F, row.names = F)
multiE9.5go <- go(multiE9.5)
write.table(multiE9.5go, "Files/multiE9.5go.txt", sep = "\t", quote = F, row.names = F)
multiE9.5_e8.5go <- go(intersect(multiE9.5, e8.5$V1))
write.table(multiE9.5_e8.5go, "Files/multiE9.5_e8.5go.txt", sep = "\t", quote = F, row.names = F)
i<-"e9.5"
genes <- read.table(paste0("/work/LAS/geetu-lab/hhvu/project1_2/rna-seq/PlacentaRNA-seq/Files/", i, "specific_TSS.bed"), header = F)
res <- go(genes$V4)
setdiff(trueE9.5go$Description, res$Description)
setdiff(res$Description, trueE9.5go$Description)
```
```{r}
sessionInfo()
```