-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFinal_methylation_RNAseq_signatures.Rmd
186 lines (125 loc) · 6.57 KB
/
Final_methylation_RNAseq_signatures.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
---
title: "R Notebook"
output: html_notebook
---
### To do different gene expression by limma package
```{r}
DGEs <- function(data, Phenotype, lFC, FDR, method){
require(limma)
require(dplyr)
#require(dendextend)
message("[===========================]")
message("[<<<<<<< DGEs START >>>>>>>>>]")
message("[<<<< Pairwise analysis >>>>>]")
message("------------------------------")
# This function creates the pairs for the pairwise matrices
design.pairs <- function(levels) {
n <- length(levels)
design <- matrix(0,n,choose(n,2))
rownames(design) <- levels
colnames(design) <- 1:choose(n,2)
k <- 0
for (i in 1:(n - 1))
for (j in (i + 1):n) {
k <- k + 1
design[i,k] <- 1
design[j,k] <- -1
colnames(design)[k] <- paste(levels[i], "-", levels[j],sep = "")
}
design
}
# This function creates the pairs for the pairwise matrices
design <- model.matrix(~0 + Phenotype)
contr.matrix <- design.pairs(levels(factor(Phenotype)))
colnames(design) <- rownames(contr.matrix)
# Removing heteroscedascity from data
v <- voom(log2(data+1), design, plot = F)
if (method == "Blood") {
# Fitting linear models for comparisons of interest
Fit <- lmFit(v, design) %>%
contrasts.fit(., contr.matrix) %>%
eBayes(.)
} else if (method == "Cancer") {
Fit <- lmFit(v, design) %>%
contrasts.fit(., contr.matrix) %>%
eBayes(.) %>% treat(., lfc = lFC)
}
FitList <- list()
for (i in 1:ncol(contr.matrix)) {
FitList[[i]] <- topTreat(Fit[which(decideTests(Fit)[,i] != 0),], coef = i, adjust.method = "BH", number = nrow(v$E)) %>% mutate(ID = rownames(.)) %>% filter(adj.P.Val < FDR)
}
names(FitList) <- colnames(contr.matrix)
message("thank you for you waiting")
return(FitList)
}
Signfeature <- function(data_TPM, Phenotype, FitList, FCcuoff ,MaxDMRs) {
require(dplyr)
require(matrixStats)
require(limma)
message("[===========================]")
message("[<<<< Signature START >>>>>]")
message("-----------------------------")
# design <- model.matrix(~0 + Phenotype)
# v_expr <- voom(data, design, plot = FALSE)
# using positive FC (=over-expressed in cell type of interest)
# FitList is DGEs which calculate by DGEs function
A1 <- list()
for (i in 1:length(FitList)) {
A1[[i]] <- filter(FitList[[i]], abs(logFC) > FCcuoff) %>% arrange(., desc(logFC)) #%>% top_n(ceiling(n()*0.5), wt = logFC)
if (nrow(A1[[i]]) > MaxDMRs) {
A1[[i]] <- A1[[i]][1:MaxDMRs,]
}
}
sig <- lapply(A1, function(x) dplyr::select(x,ID))
sig <- do.call(rbind, sig)
sig <- filter(sig, !duplicated(ID))
data1 <- data_TPM[rownames(data_TPM) %in% sig$ID,]
# Print number of selected probes (signature)
nrow(data1)
result <- getMedVal(data1, Phenotype)
message("[===========================]")
message("[<<<< Signature END >>>>>]")
message("-----------------------------")
return(result)
}
getMedVal <- function(data, Phenotype){
library(matrixStats)
Trans <- data.frame(t(data))
Mt.Split <- split(Trans, Phenotype)
Mt.Split <- lapply(Mt.Split, function(x) colMedians(data.matrix(x)))
Mt.Split <- do.call(cbind, Mt.Split)
rownames(Mt.Split) <- rownames(data)
return(Mt.Split)
}
### To choose suitable threshold of LogFC (save as PDF file)
DGEs.QC <- function(FitList, FDR) {
require(ggpubr)
p <- list()
for (i in 1:length(FitList)) {
p[[i]] <- ggplot(FitList[[i]][FitList[[i]]$adj.P.Val < FDR,], aes(x = logFC)) + geom_density(colour = "red") + labs(title = names(FitList)[i], x = "logFC", y = "Density") + theme(plot.title = element_text(hjust = 0.5))
}
return(do.call(ggarrange, p))
}
getExpMatrix <- function(geneList, expression){
df_out <- expression[geneList, ]
return(df_out)
}
```
### BPRNA signature
```{r}
blueprint_DGEs_blood <- DGEs(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,7178:7216], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[7178:7216], FDR = 0.05, method = "Blood")
Mediant_blueprint_blood <- getMedVal(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,7178:7216], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[7178:7216])
BPRNA <- Signfeature(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,c(7178:7216)], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[c(7178:7216)], FitList = blueprint_DGEs_blood, FCcuoff = 2.5, MaxDMRs = 200)
```
### BPRNACan signature
```{r}
TCGA_normal_cancer_WB_DGEs <- DGEs(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,c(1:7164)], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[1:7164], lFC = 1.5, FDR = 0.05, method = "Cancer")
blueprint_DGEs <- DGEs(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,7165:7216], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[7165:7216], lFC = 2, FDR = 0.05, method = "Cancer")
TCGA_cancer_blueprint_DGEs <- DGEs(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,c(684:6778, 7165:7216)], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[c(684:6778, 7165:7216)], lFC = 2, FDR = 0.05, method = "Cancer")
Mediant_TCGA_normal_cancer_WB <- getMedVal(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,c(1:7164)], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[1:7164])
Mediant_TCGA_cancer_blueprint <- getMedVal(data = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,c(684:6778, 7165:7216)], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[c(684:6778, 7165:7216)])
Tumor.sig <- Signfeature(data_TPM = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,c(1:7164)], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[1:7164], FitList = TCGA_normal_cancer_WB_DGEs, FCcuoff = 3, MaxDMRs = 100)
TIL.sig <- Signfeature(data_TPM = TCGA_normal_cancer_WB_blueprint_TPM_normalization[,c(684:6778, 7165:7216)], Phenotype = TCGA_normal_cancer_WB_blueprint_phenotype[c(684:6778, 7165:7216)], FitList = blueprint_DGEs, FCcuoff = 2, MaxDMRs = 150)
Immune_Tumor_CpGs <- unique(c(rownames(Tumor.sig), rownames(TIL.sig)))
BPRNACan <- getExpMatrix(geneList = Immune_Tumor_CpGs, expression = Mediant_TCGA_cancer_blueprint)
```