-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGTEX and TCGA.R
194 lines (161 loc) · 9.17 KB
/
GTEX and TCGA.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
#################################################################################################
#########################################CLEAN SCRIPT############################################
#################################################################################################
setwd("...")
##Packages and libraries
install.packages("d3heatmap")
install.packages("gplots")
install.packages("corrgram")
install.packages("corrplot")
install.packages("mclust")
library(d3heatmap)
library(gplots)
library(corrgram)
library(corrplot)
library(mclust)
# GTEX data
mydata = read.csv("xxx/GTexDataComplete.csv", header=TRUE)
# TCGA data
tcgadata = read.csv("xxx/SeteroidogenicGenesV2.csv")
########
#Gtex with organs to compare
df3 = mydata[mydata$SiteOrgan %in% c("Adrenal Gland", "Bladder", "Blood", "Brain", "Breast", "Cervix Uteri", "Colon", "Kidney", "Lung", "Muscle", "Ovary", "Pancreas", "Prostate", "Skin", "Spleen", "Testis", "Thyroid", "Uterus"),]
df3_matrix = data.matrix(df3[,7:ncol(df3)])# convert to matrix
## first heatmat
#Variables for clustering
distance = dist(df3_matrix, method = "manhattan")
cluster = hclust(distance, method = "ward")
row_distance = dist(df3_matrix, method = "manhattan")
row_cluster = hclust(row_distance, method = "ward")
col_distance = dist(t(df3_matrix), method = "manhattan")
col_cluster = hclust(col_distance, method = "ward")
# creates a own color palette from red to green
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(-1,0,length=100), # for red
seq(0,0.8,length=100), # for yellow
seq(0.8,1,length=100)) # for green
RowSideColors = c( # grouping row-variables into different
rep("gray", 3), # categories, Measurement 1-3: green
rep("blue", 3), # Measurement 4-6: blue
rep("black", 4))
#heatmap
heatmap.2(df3_matrix,
cellnote = df3_matrix, # same data set for cell labels
main = "Correlation between normal mRNA and Transduceosome genes - GTEx Data", # heat map title
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(8,12), # widens margins around plot
col=my_palette, # use on color palette defined earlier
#breaks=col_breaks, # enable color transition at specified limits
#dendrogram="row", # only draw a row dendrogram
Rowv = as.dendrogram(row_cluster),
Colv = as.dendrogram(col_cluster))
##############
########## Kidney Only
##GTEX RPKM values for Normal Kidney
KidneyGTEX = mydata[mydata$SiteOrgan %in% c("Kidney"),]
KidneyGTEXmatrix = KidneyGTEX[,c(7:35)] #pull numerical columns only
KidneyGTEXmatrix = scale(KidneyGTEXmatrix, center = TRUE, scale = TRUE) #Get Z scores using scale
KidneyGTEXmatrix = data.matrix(KidneyGTEXmatrix)# matrix for plot
col.pal <- RColorBrewer::brewer.pal(9, "Reds")
NormalPlot = d3heatmap(KidneyGTEXmatrix, colors = col.pal, scale = "row", dendrogram = "row", k_row = 3)
##TCGA RSEM values for Kidney Tumors
KidneyTumors = tcgadata[tcgadata$Cancer %in% c("ccRCC", "chrRCC", "papRCC"),]
KidneyTumors = KidneyTumors[complete.cases(KidneyTumors),]#to remove NA's and incomplete cases
KidneyTumorsmatrix = KidneyTumors[,c(4:32)] #pull numerical columns only
KidneyTumorsmatrix = scale(KidneyTumorsmatrix, center = TRUE, scale = TRUE)##Get Z scores using scale
KidneyTumorsmatrix = data.matrix(KidneyTumorsmatrix) # matrix for plot
TumorsPlot = d3heatmap(KidneyTumorsmatrix, colors = col.pal, scale = "row", dendrogram = "row", k_row = 3)
NormalPlot
TumorsPlot
DataframeWithEquivalentNormalTissue = mydata[mydata$SiteOrgan %in% c("Adrenal Gland", "Bladder", "Blood", "Brain", "Breast", "Cervix Uteri", "Colon", "Kidney", "Lung", "Muscle", "Ovary", "Pancreas", "Prostate", "Skin", "Spleen", "Testis", "Thyroid", "Uterus"),]
DataframeWithEquivalentTumors = tcgadata[tcgadata$Cancer %in% c("ACC", "UC", "AML", "GBM", "Glioma", "Breast", "SqCCx", "Colon", "ccRCC", "chrRCC", "papRCC", "LungADC", "Skin-Melanoma", "SqCLung", "Sarcoma", "Serous", "Pancreas", "Cholangio", "Pheo", "Prostate", "DLBCL", "GCT-Testis", "Thyroid", "Endometrial", "Uterine-Carcinosarc"),]
DataframeWithEquivalentNormalTissue = mydata[mydata$SiteOrgan %in% c("Adrenal Gland", "Bladder", "Blood", "Brain", "Breast", "Cervix Uteri", "Colon", "Kidney", "Lung", "Muscle", "Ovary", "Pancreas", "Prostate", "Skin", "Spleen", "Testis", "Thyroid", "Uterus"),]
gtexmatrix = DataframeWithEquivalentNormalTissue[,c(7:35)]
gtexmatrix = scale(gtexmatrix, center = TRUE, scale = TRUE)
d3heatmap(gtexmatrix, colors = col.pal, scale = "row", dendrogram = "row", k_row = 3)
DataframeWithEquivalentTumors = tcgadata[tcgadata$Cancer %in% c("ACC", "UC", "AML", "GBM", "Glioma", "Breast", "SqCCx", "Colon", "ccRCC", "chrRCC", "papRCC", "LungADC", "Skin-Melanoma", "SqCLung", "Sarcoma", "Serous", "Pancreas", "Cholangio", "Pheo", "Prostate", "DLBCL", "GCT-Testis", "Thyroid", "Endometrial", "Uterine-Carcinosarc"),]
TCGATumorsmatrix = DataframeWithEquivalentTumors[,c(4:32)] #pull numerical columns only
TCGATumorsmatrix = scale(TCGATumorsmatrix, center = TRUE, scale = TRUE)
d3heatmap(TCGATumorsmatrix, colors = col.pal, scale = "row", dendrogram = "row", k_row = 3)
###I concatenated the files below to end up with the FINAL files with Z scores to compare
write.csv(gtexmatrix, "H:/Documents/Gtex data/GTEX Z SCORES.csv")
write.csv(TCGATumorsmatrix, "H:/Documents/Gtex data/TCGA Z SCORES.csv")
write.csv(DataframeWithEquivalentNormalTissue, "H:/Documents/Gtex data/GTEX DATA FRAME.csv")
write.csv(DataframeWithEquivalentTumors, "H:/Documents/Gtex data/TCGA DATA FRAME.csv")
#################################################################################################
#########################################Normalized DATA#########################################
#################################################################################################
#################################################################################################
# GTEX data
gtex = read.csv("xxx/GTEX DATA FRAME WITH Z SCORES_FINAL.csv", header = TRUE)
# TCGA data
tcga = read.csv("xxx/TCGA DATA FRAME WITH Z SCORES_FINAL.csv", header = TRUE)
#REMOVE na's
tcga = tcga[complete.cases(tcga),]#to remove NA's
#Matrix
gtexZscores = gtex[,c(7:35)]
gtexmatrix = data.matrix(gtexZscores)
tcgaZscores = tcga[,c(4:32)] #pull numerical columns only
tcgamatrix = data.matrix(tcgaZscores)
gtex2 = cor(gtexmatrix)
corrgram(gtexmatrix, order=TRUE, lower.panel=panel.shade,
upper.panel=panel.pie, text.panel=panel.txt,
main="Correlation of Z-Scores of mRNA expression in Normal Tissues")
tcga2 = cor(tcgamatrix)
corrgram(tcgamatrix, order=TRUE, lower.panel=panel.shade,
upper.panel=panel.pie, text.panel=panel.txt,
main="Correlation of Z-Scores of mRNA expression in TCGA Tumors")
head(round(tcga2,2))
#FUNCTION TO CALCULATE P-VALUES
# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
cor.mtest <- function(mat, ...) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp <- cor.test(mat[, i], mat[, j], ...)
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}
# matrix of the p-value of the correlation
pvalgtex = cor.mtest(gtexmatrix)
head(pvalgtex[, 1:5])
pvaltcga = cor.mtest(tcgamatrix)
head(pvaltcga[, 1:5])
GTEXPLOT = corrplot(gtex2, method="circle", type="upper", order="hclust", tl.col="black", tl.srt=45,
p.mat = pvalgtex, sig.level = 0.05, addCoef.col = "black", hclust.method="centroid",
insig = "blank", diag=FALSE, main="Normal Tissues")
TCGAPLOT = corrplot(tcga2, method="circle", type="upper", order="hclust", tl.col="black", tl.srt=45,
p.mat = pvaltcga, sig.level = 0.05, addCoef.col = "black", hclust.method="centroid",
insig = "blank", diag=FALSE, main="Tumors")
#
col = colorRampPalette(c("blue", "white", "red"))(20)
#heatmap(x = gtexmatrix, col = col, symm = TRUE)
hc_dist= dist(gtexmatrix)
hr_dist= dist(t(gtexmatrix))
hc_clust = hclust(hc_dist)
hr_clust = hclust(hr_dist)
heatmap.2(gtexmatrix, col=col, labRow=NA, density.info="none", scale="row",trace="none",
Colv=as.dendrogram(hr_clust),
Rowv=as.dendrogram(hc_clust))
#heatmap(x = tcgamatrix, col = col, symm = TRUE)
hc_dist2= dist(tcgamatrix)
hr_dist2= dist(t(tcgamatrix))
hc_clust2 = hclust(hc_dist2)
hr_clust2 = hclust(hr_dist2)
heatmap.2(tcgamatrix, col=col, labRow=NA, density.info="none", scale="row",trace="none",
Colv=as.dendrogram(hr_clust2),
Rowv=as.dendrogram(hc_clust2))
#2*pnorm(-abs(gtexZscores$HSD3B2))
#2*pnorm(-abs(tcgaZscores$HSD3B2))
#plot(gtexZscores$HSD3B2)
#plot(tcgaZscores$HSD3B2)