-
Notifications
You must be signed in to change notification settings - Fork 1
/
processing TCGA files.txt
233 lines (212 loc) · 8.56 KB
/
processing TCGA files.txt
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
# RNA-seq data processing
mkdir genes
mv *.rsem.genes.results genes/
cd genes
sed -i 's/|/\t/g' *
sed -i 's/gene_id\traw_count\tscaled_estimate\ttranscript_id/HUGO\tgene_id\traw_count\tscaled_estimate\ttranscript_id/g' *
R
require(plyr)
data <- lapply(dir(), read.table, col.names=c("HUGO","gene_id","raw_count","scaled_estimate","transcript_id"), na.strings="-999.00",stringsAsFactors=FALSE)
counts <- as.data.frame(matrix(nrow = length(data[[1]][,1])-30, ncol = length(data)+1))
colnames(counts) <- c("HUGO",dir())
counts[,1] <- as.character(data[[1]][-(1:30),1])
for(i in 1:length(data)){
counts[,1+i] <- as.numeric(data[[i]][-(1:30),3])
}
counts[16272,1] <- "SLC35E2B"
rownames(counts) <- counts[,1]
counts <- counts[,-1]
scaled <- as.data.frame(matrix(nrow = length(data[[1]][,1])-30, ncol = length(data)+1))
colnames(scaled) <- c("HUGO",dir())
scaled[,1] <- as.character(data[[1]][-(1:30),1])
for(i in 1:length(data)){
scaled[,1+i] <- as.numeric(data[[i]][-(1:30),4])
}
scaled[16272,1] <- "SLC35E2B"
rownames(scaled) <- scaled[,1]
scaled <- scaled[,-1]
mapping <- read.delim("rnaseq_map.tab", header=F, stringsAsFactors=F)
ANs <- mapping[mapping[,2]=="AN",1]
Ts <- mapping[mapping[,2]=="T",1]
for (i in 1:ncol(counts)) {
colnames(counts)[i] <- mapping[which(mapping[,3]==colnames(counts)[i]),1]
colnames(scaled)[i] <- mapping[which(mapping[,3]==colnames(scaled)[i]),1]
}
true_ls <- apply(counts,2,sum)/apply(scaled,2,sum)
progressed <- c("",..)
nonProgressed <- c("",..)
# 18/01/2016 preparing the progression subset
save(progressed, nonProgressed, true_ls, counts, file="BRCA_progression.RData")
# another R session
load("BRCA_progression.RData")
colnames(counts) <- substr(colnames(counts),1,12)
names(true_ls) <- substr(names(true_ls),1,12)
counts <- counts[,c(progressed, nonProgressed)]
true_ls <- true_ls[c(progressed, nonProgressed)]
save.image("BRCA_progression.RData")
source("D:/TCGA PRAD/sampleNames.R")
counts <- counts_BRCA_all[,c(ANs,Ts)]
cpm_PRAD_plusOne <- counts_PRAD
for (i in 1:331) cpm_PRAD_plusOne[,i] <- (counts_BRCA_all[,i]+1)/(lib_size[i]/10^6)
require(edgeR)
targets <- rbind(cbind(ANs,"AN"),cbind(Ts,"T"))
targets <- as.data.frame(targets)
colnames(targets) <- c("ID","Type")
y <- DGEList(counts=counts_BRCA_all,group=targets$Type)
y <- estimateTagwiseDisp(y)
et <- exactTest(y,dispersion="tagwise")
et$table[,4] <- p.adjust(et$table[,3],method="BH")
colnames(et$table)[4] <- "BH.PValue"
result <- cbind(et$table,cpm_PRAD_plusOne)
write.table(result,col.names=TRUE,row.names=TRUE,sep="\t",file="PRAD_rna-seq.tab")
# 450k data
# first prepare the Sample Sheet (as required by IMA):
# zamienic _ na \t i w excel-u wybrac poj. linie, nastepnie wyciac kolumne z idat, dodac anotacje, upewnic sie, ze nie ma duplikatow (technicznych lub biologicznych replikatow)
# on server (lots of memory required)
/home/michal/R-3.0.1/bin/R # old version with minfi installed on VM
require(minfi)
targets <- read.450k.sheet(base=getwd())
RGset <- read.450k.exp(targets=targets)
MSet.raw <- preprocessRaw(RGset)
source("peakCorrection_functions.R")
load("fullannot.RData")
mmatrix <- log2((getMeth(MSet.raw)+1) / (getUnmeth(MSet.raw)+1))
colnames(mmatrix) <- targets[,1]
bmatrix <- M2Beta(mmatrix)
bmatrix_pc <- peak.correction(bmatrix,fullannot)
mmatrix_pc <- Beta2M(bmatrix_pc)
save(targets, mmatrix_pc, bmatrix_pc, file="450k_BRCA.RData") # change name here
# then load up both rna-seq and 450k data and make a bundle
#############################################################
# R functions required for peak-correction
peak.correction <- function (data, anno)
{
anno <- anno[row.names(data), ]
TI <- anno[, "INFINIUM_DESIGN_TYPE"] == "I"
TII <- anno[, "INFINIUM_DESIGN_TYPE"] == "II"
corrected.data <- apply(data, 2, function(B) {
SI <- summits(B[TI])
SII <- summits(B[TII])
BI <- correctI(as.vector(B[TI]), SI, SII)
BII <- correctII(as.vector(B[TII]), SI, SII)
return(c(BI, BII))
})
row.names(corrected.data) <- c(row.names(data[TI, ]), row.names(data[TII, ]))
return(corrected.data)
}
correctI <- function (BetaValues, SI, SII)
{
return(BetaValues)
}
correctII <- function (BetaValues, SI, SII)
{
M <- Beta2M(BetaValues)
sigma_u <- SII[1]/SI[1]
sigma_m <- SII[2]/SI[2]
M <- sapply(M, function(x) {
if (is.na(x)) return(NA) ##LS##
if (x < 0)
return(x/sigma_u)
else return(x/sigma_m)
})
return(M2Beta(M))
}
summits <- function (BetaValues)
{
d <- density(BetaValues,na.rm=TRUE)
yneg <- d$y[1:which(d$x > M2Beta(0))[1]]
ypos <- d$y[which(d$x > M2Beta(0))[1]:length(d$y)]
sa <- d$x[which(d$y == max(yneg))]
sb <- d$x[which(d$y == max(ypos))]
return(c(Beta2M(sa), Beta2M(sb)))
}
M2Beta <- function (M)
{
return((2^M)/(2^M + 1))
}
Beta2M <- function (B)
{
return(log2(B/(1 - B)))
}
############################################################
# manual recreation of the fullannot table, if the annotation is provided
# extract probe indexes from annotation file:
annot = annotation
name = "UCSC_REFGENE_NAME"
cpGsite = as.character(annot[, 1])
genelist = strsplit(as.character(annot[, name]), ";")
genelist[which(genelist == "character(0)")] = "NA"
name = "UCSC_REFGENE_GROUP"
refgene = strsplit(as.character(annot[, name]), ";")
refgene[which(refgene == "character(0)")] = "NA"
listlength = lapply(refgene, length)
listlength[listlength == 0] = 1
col0 = rep(1:nrow(annot), listlength)
col1 = rep(cpGsite, listlength)
col2 = unlist(genelist)
col3 = unlist(refgene)
col4 = rep(as.character(annotation[, "RELATION_TO_UCSC_CPG_ISLAND"]),
listlength)
col5 = rep(as.character(annotation[, "UCSC_CPG_ISLANDS_NAME"]),
listlength)
splitToRegionlist = function(grepname = c("TSS1500", "TSS200",
"5'UTR", "1stExon", "Gene Body", "3'UTR")) {
index = col3 == grepname
col1sub = col1[index]
col2sub = col2[index]
temp = split(col1sub, col2sub)
returnSID = lapply(temp, unique)
col0sub = col0[index]
temp = split(col0sub, col2sub)
returnPID = lapply(temp, unique)
return(Ind = list(SID = returnSID, PID = returnPID))
}
TSS1500Ind = splitToRegionlist(grepname = "TSS1500")
TSS200Ind = splitToRegionlist(grepname = "TSS200")
UTR5Ind = splitToRegionlist(grepname = "5'UTR")
EXON1Ind = splitToRegionlist(grepname = "1stExon")
GENEBODYInd = splitToRegionlist(grepname = "Body")
UTR3Ind = splitToRegionlist(grepname = "3'UTR")
cat("TSS1500 region contains:", length(TSS1500Ind$SID),
"UCSC REFGENE region \nTSS200 region contains:", length(TSS200Ind$SID),
"UCSC REFGENE region\n5'UTR region contains:", length(UTR5Ind$SID),
"UCSC REFGENE region\n1st Exon region contains:", length(EXON1Ind$SID),
"UCSC REFGENE region\nGene body region contains:", length(GENEBODYInd$SID),
"UCSC REFGENE region\n3'UTR region contains:", length(UTR3Ind$SID),
"UCSC REFGENE region\n")
cpGsite = as.character(fullannot[, 1])
genelist = strsplit(as.character(fullannot[, "UCSC_REFGENE_NAME"]), ";")
genelist[which(genelist == "character(0)")] = "NA"
refgene = strsplit(as.character(fullannot[, "UCSC_REFGENE_GROUP"]), ";")
refgene[which(refgene == "character(0)")] = "NA"
listlength = lapply(refgene, length)
listlength[listlength == 0] = 1
col0 = rep(1:nrow(fullannot), listlength)
col1 = rep(cpGsite, listlength)
col2 = unlist(genelist)
col3 = unlist(refgene)
splitToRegionlist = function(grepname = c("TSS1500", "TSS200",
"5'UTR", "1stExon", "Gene Body", "3'UTR")) {
index = col3 == grepname
col1sub = col1[index]
col2sub = col2[index]
temp = split(col1sub, col2sub)
returnSID = lapply(temp, unique)
col0sub = col0[index]
temp = split(col0sub, col2sub)
returnPID = lapply(temp, unique)
return(list(SID = returnSID, PID = returnPID))
}
TSS1500Ind = splitToRegionlist(grepname = "TSS1500")
TSS200Ind = splitToRegionlist(grepname = "TSS200")
UTR5Ind = splitToRegionlist(grepname = "5'UTR")
EXON1Ind = splitToRegionlist(grepname = "1stExon")
GENEBODYInd = splitToRegionlist(grepname = "Body")
UTR3Ind = splitToRegionlist(grepname = "3'UTR")
cat("TSS1500 region contains:", length(TSS1500Ind$SID),
"UCSC REFGENE region \nTSS200 region contains:", length(TSS200Ind$SID),
"UCSC REFGENE region\n5'UTR region contains:", length(UTR5Ind$SID),
"UCSC REFGENE region\n1st Exon region contains:", length(EXON1Ind$SID),
"UCSC REFGENE region\nGene body region contains:", length(GENEBODYInd$SID),
"UCSC REFGENE region\n3'UTR region contains:", length(UTR3Ind$SID),
"\n")