-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectNormalizeGeneExonCountsSH.R
328 lines (235 loc) · 12.5 KB
/
selectNormalizeGeneExonCountsSH.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
library(matrixStats)
library(edgeR)
library(WGCNA)
library(biomaRt)
library(plotrix)
library(foreach)
library(doMC)
registerDoMC()
library(proxy)
library(sgof)
library(multtest)
getDoParWorkers()
options(cores=14)
getDoParWorkers()
# linux does not work with box so this code has a local data directory
setwd("/home/dan/workDir/networkAnalysis")
source("/home/dan/workDir/networkAnalysis/functionDefinitions.R")
try(dir.create("resultsCoexpr_SH"), silent = F)
try( dir.create("figuresCoexpr_SH"), silent = F)
try(dir.create("resultsCoSplicEx_SH"), silent = F)
try( dir.create("figuresCoSplicEx_SH"), silent = F)
# read raw data - can be improved by using read.table from original .txt file
geneReadsRaw=as.matrix(read.table("RNASeq019 HSCC Alex/SH/RNASeq019_SH_mm10_gene_reads_not_normalized.txt"))
geneNames=rownames(geneReadsRaw)
# read sample info - sample names need to be inspected and categories extracted differently for each dataset !!!!!!!!!
sampleKey=read.csv("RNASeq019 HSCC Alex/SH/SHsampleKey.csv", header=T)
sampleKey[,"Shell"]=paste("S", sampleKey[,"Shell"], sep = "")
# other projects might mean strsplit
# splitIDs=mapply(strsplit, sample_info, MoreArgs=list(split="_", fixed = FALSE, perl = FALSE, useBytes = FALSE))
# sampleInfo=unlist(lapply(splitIDs, "[[", 3))
samplesHigh=sampleKey[grep("H",sampleKey[,"Line"] ), "Shell"]
samplesLow=sampleKey[grep("L",sampleKey[,"Line"] ), "Shell"]
##################################################################################################################
# divide the data in different groups and perform DE with edgeR
HSCC_H=geneReadsRaw[,samplesHigh ]
HSCC_L=geneReadsRaw[,samplesLow]
groupSelection=c(rep("HSCC_H",dim(HSCC_H)[2]),rep("HSCC_L",dim(HSCC_L)[2]))
groupSelection =factor(groupSelection)
d=DGEList(counts= cbind(HSCC_H, HSCC_L), group= groupSelection)
d <- estimateCommonDisp(d)
d <- estimateTagwiseDisp(d)
de.tgw <- exactTest(d, dispersion="tagwise")
########################################################################################
# use sgof package for multiple comparison correction
# results from sgof come out sorted but un-named (!!!!) so the original pvalues and geneNames need to be sorted
pValues_DE=de.tgw$table$PValue
names(pValues_DE)=geneNames
sortIndexes=sort.int(pValues_DE, decreasing = F, index.return=T)$ix
sortedGeneNames=geneNames[sortIndexes]
adjustedResults<-SGoF(u=pValues_DE)
summary(adjustedResults)
sortedAdjustedPvals_DE=adjustedResults$Adjusted.pvalues
names(sortedAdjustedPvals_DE)=sortedGeneNames
fileConnSummary<-file("resultsCoexpr_SH/SummaryResultsCoexpr.txt", open="wt")
writeLines(paste("Number of genes with >1 CPM that are DE at FDR=0.05: ", sum(sortedAdjustedPvals_DE<0.05), sep=' '), fileConnSummary)
close(fileConnSummary)
# some sanity checks
#plot(pValues[sortedGeneNames], adjustedResults$data) # should be straight line
#plot(pValues[sortedGeneNames], adjustedResults$data) # should be straight line
##############################################################################
###################################################################################
# calculate edgeR normalization factors and normalize the data
UQnormFactors=calcNormFactors(geneReadsRaw, method=c("upperquartile"))
effectiveLibrarySizes= UQnormFactors*colSums(geneReadsRaw)
meanEffLibSize=mean(effectiveLibrarySizes)
countNormFactor= meanEffLibSize/effectiveLibrarySizes
normalizedGeneCountsUQ=0* geneReadsRaw
for (sample in 1:dim(normalizedGeneCountsUQ)[2]){
normalizedGeneCountsUQ[,sample]= geneReadsRaw[, sample]* countNormFactor[sample]
}
# verify that smaller libraries are multiplied by bigger normalization factors
plot(colSums(geneReadsRaw), countNormFactor, xlab="Un-normalized library sizes", ylab="Normalization factors")
# plot original library sizes versus normalized
#range of data much smaller in normalized data
xylim=c(min(colSums(geneReadsRaw), colSums(normalizedGeneCountsUQ)), max(colSums(geneReadsRaw), colSums(normalizedGeneCountsUQ)))
plot(colSums(geneReadsRaw), colSums(normalizedGeneCountsUQ), xlim=xylim, ylim=xylim)
# interquartile range of normalized libraries should me much smaller
IQR(colSums(geneReadsRaw))
IQR(colSums(normalizedGeneCountsUQ))
###################################################################################
###################################################################################
# select genes with logCPM > 0 (equivalent to CPM>1)
geneNamesHighCPM=geneNames[de.tgw$table[,"logCPM"]>0]
geneCountsHighCPM=normalizedGeneCountsUQ[geneNamesHighCPM,]
meanCounts_H=rowMeans(HSCC_H[geneNamesHighCPM,])
meanCounts_L=rowMeans(HSCC_L[geneNamesHighCPM,])
sdCounts_H=apply(HSCC_H[geneNamesHighCPM,],1, sd)
sdCounts_L=apply(HSCC_L[geneNamesHighCPM,],1, sd)
##############################################################################
# find differentially variable genes
pvalVar=rep(1, length(geneNamesHighCPM))
names(pvalVar)=geneNamesHighCPM
for (gene in geneNamesHighCPM){
pvalVar[gene]=var.test(x=HSCC_H[gene,], y=HSCC_L[gene,])$p.value
}
pvalVar[is.na(pvalVar)]=1
pValues=pvalVar
names(pValues)=geneNamesHighCPM
sortIndexes=sort.int(pValues, decreasing = F, index.return=T)$ix
sortedGeneNames=geneNamesHighCPM[sortIndexes]
adjustedResults<-SGoF(u=pValues)
summary(adjustedResults)
sortedAdjustedPvals_DV=adjustedResults$Adjusted.pvalues
names(sortedAdjustedPvals_DV)=sortedGeneNames
fileConnSummary<-file("resultsCoexpr_SH/SummaryResultsCoexpr.txt", open="at")
writeLines(paste("Number of genes with >1 CPM that are DV at FDR=0.05: ", sum(sortedAdjustedPvals_DV<0.05), sep=' '), fileConnSummary)
close(fileConnSummary)
geneNamesDE=sortedGeneNames[sortedAdjustedPvals_DE < 0.05]
geneNamesDV=sortedGeneNames[sortedAdjustedPvals_DV < 0.05]
write.csv(geneNamesDE, file="resultsCoexpr_SH/geneNamesDE.csv")
write.csv(geneNamesDV, file="resultsCoexpr_SH/geneNamesDV.csv")
setdiff(geneNamesHighCPM, geneNames)
###########################################################################33
results_highCPMgenes=cbind(de.tgw$table[geneNamesHighCPM,], meanCounts_L[geneNamesHighCPM], meanCounts_H[geneNamesHighCPM], pValues_DE[geneNamesHighCPM], sortedAdjustedPvals_DE[geneNamesHighCPM],sdCounts_L[geneNamesHighCPM],sdCounts_H[geneNamesHighCPM], pvalVar, sortedAdjustedPvals_DV[geneNamesHighCPM] )
results_highCPMgenes=round(results_highCPMgenes,3)
colnames(results_highCPMgenes)=c(colnames(de.tgw$table), c("mean counts L", "mean counts H", " p val DE", " adj p DE", "sd L", "sd H", "p val DV", "adj p val DV"))
rownames(results_highCPMgenes)=geneNamesHighCPM
#this will be collected in Supplemental Table 1
write.csv(results_highCPMgenes, file="resultsCoexpr_SH/resultsDEDV_highCPM.csv")
#######################################################################################3
# possibly swithch to bicor correlation in the future
#adjCoexpr=adjacency(t(geneCountsHighCPM), corFnc = "bicor", type="unsigned", power=6)
adjCoexpr=adjacency(t(geneCountsHighCPM), type="unsigned", power=6)
adjCoexpr[is.na(adjCoexpr)]=0
#sanity check, should be 0
sum(adjCoexpr<0, na.rm=T)
connCoexpr=rowSums(adjCoexpr, na.rm = T)-1
connCoexpr_WGCNA=softConnectivity(t(geneCountsHighCPM), type="unsigned", power=6)
plot(connCoexpr, connCoexpr_WGCNA)
names(connCoexpr)=geneNamesHighCPM
connCoexpr=connCoexpr[connCoexpr > 0]
connCoexpr[is.na(connCoexpr)]=0
sortedConn=sort(connCoexpr, decreasing = T)
sum(connCoexpr < 0, na.rm=T)
totalConn=sum(sortedConn)
cumulativeConnFraction=0*sortedConn
for (i in 1:length(sortedConn)){
cumulativeConnFraction[i]=sum(sortedConn[1:i])/totalConn
}
lastGene=min(which(cumulativeConnFraction > .9))
highConnGenes=names(sortedConn)[1:lastGene]
plot(cumulativeConnFraction, xlab="", ylab="")
title(xlab="Number of genes \n ranked by connectivity", cex.lab=1.25, font.lab=2)
title(ylab="Fraction total connectivity", cex.lab=1.25, font.lab=2)
abline(v=lastGene)
abline(h=0.9)
text(x=8100, y=0.8, labels=paste(lastGene, " genes", sep=""))
text(x=3100, y=0.95, labels="90% connectivity captured")
title(main="Selecting the network size\n starting from genes with > 1 CPM", cex.lab=1.5, font.lab=2)
# export figure
#############################################################################################################
adjCoexprHighConn=adjCoexpr[highConnGenes,highConnGenes]
selectedGeneCounts=normalizedGeneCountsUQ[highConnGenes,]
###################################################################################
exonCounts=read.table("RNASeq019 HSCC Alex/SH/RNASeq019_SH_mm10_exon_reads_not_normalized.txt")
splitIDs=mapply(strsplit, rownames(exonCounts), MoreArgs=list(split="_", fixed = FALSE, perl = FALSE, useBytes = FALSE))
exonGeneName=unlist(lapply(splitIDs, "[[", 4))
exon_start=unlist(lapply(splitIDs, "[[", 2))
#sanity check
colnames(exonCounts)==colnames(geneReadsRaw)
# sanity check
sum(exonCounts[exonGeneName=="Drd2",])
sum(selectedGeneCounts["Drd2",])
normExonCounts=0* exonCounts
for (sample in 1:dim(exonCounts)[2]){
normExonCounts[,sample]= exonCounts[, sample]* countNormFactor[sample]
}
normExonCounts =round(normExonCounts)
rownames(normExonCounts)=rownames(exonCounts)
# select exons from genes with at least 1 CPM
exonCountsHighCounts=normExonCounts[which(exonGeneName %in% geneNamesHighCPM),]
exonGeneNamesHighCounts=exonGeneName[exonGeneName %in% geneNamesHighCPM]
# compute Canberra distances
canberraListExons=foreach (geneName = geneNamesHighCPM, .inorder=T, .verbose = T) %dopar% {
currExonCounts= exonCountsHighCounts[which(exonGeneNamesHighCounts==geneName),]
if (is.null(dim(currExonCounts))){
exonDistMatrix=as.matrix(dist(as.matrix(currExonCounts), method="canberra"))
} else {
exonDistMatrix=as.matrix(dist(t(as.matrix(currExonCounts)), method="canberra"))
}
# colnames(exonDistMatrix)=exonColnames
# rownames(exonDistMatrix)=exonColnames
exonDistMatrix
}
names(canberraListExons)=geneNamesHighCPM
save(canberraListExons, file="resultsCoSplicEx_SH/canberraListExonsSH.RData")
load("resultsCoSplicEx_SH/canberraListExonsSH.RData")
########################################################################################################
nGenes=length(canberraListExons)
gene_indexes=1:nGenes
# reformat the data so one can use WGCNA adjacency function to construct CoSplicEx adjacency matrix
lengthVector=length(as.vector(as.dist(canberraListExons[[1]])))
distData=matrix(data=0, nrow=lengthVector, ncol=length(canberraListExons))
colnames(distData)=names(canberraListExons)
for(gene in names(canberraListExons)) {
distData[,gene]=as.vector(as.dist(canberraListExons[[gene]]))
}
##########################################################################################################
# compute connectivity in large CoSplicEx network
#######################################################################################3
adjCoSplicEx_raw=adjacency(distData, type="unsigned", power=6)
adjCoSplicEx_raw[is.na(adjCoSplicEx_raw)]=0
#sanity check, should be 0
sum(adjCoSplicEx_raw<0, na.rm=T)
connCoSplicEx=rowSums(adjCoSplicEx_raw, na.rm = T)-1
names(connCoSplicEx)=geneNamesHighCPM
connCoSplicEx=connCoSplicEx[connCoexpr > 0]
connCoSplicEx[is.na(connCoSplicEx)]=0
sortedConn=sort(connCoSplicEx, decreasing = T)
sum(connCoSplicEx < 0, na.rm=T)
totalConn=sum(sortedConn)
cumulativeConnFraction=0*sortedConn
for (i in 1:length(sortedConn)){
cumulativeConnFraction[i]=sum(sortedConn[1:i])/totalConn
}
lastGene=min(which(cumulativeConnFraction > .9))
highConnGenes=names(sortedConn)[1:lastGene]
plot(cumulativeConnFraction, xlab="", ylab="")
title(xlab="Number of genes \n ranked by connectivity", cex.lab=1.25, font.lab=2)
title(ylab="Fraction total connectivity", cex.lab=1.25, font.lab=2)
abline(v=lastGene)
abline(h=0.9)
text(x=6100, y=0.8, labels=paste(lastGene, " genes", sep=""))
text(x=3100, y=0.95, labels="90% connectivity captured")
title(main="Selecting the network size\n starting from genes with > 1 CPM", cex.lab=1.5, font.lab=2)
# export figure
#############################################################################################################
adjCoSplicEx=adjCoSplicEx_raw[highConnGenes,highConnGenes]
canberraListSelected=canberraListExons[highConnGenes]
exonGeneNameSelected=highConnGenes
selectedExonCounts=normExonCounts[which(exonGeneName %in% exonGeneNameSelected),]
########################################################################################################
save(selectedGeneCounts, canberraListSelected,adjCoSplicEx,selectedExonCounts, exonGeneNameSelected, groupSelection, samplesHigh, samplesLow, file="selectedData_SH.RData")
#load("data/selectedData.RData")
############################################################################################