forked from rli012/BioinfoHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess_TCGA_Data.R
182 lines (121 loc) · 5.86 KB
/
Process_TCGA_Data.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
setwd('~/bigdata/TCGA/')
BiocManager::install('GDCRNATools')
library(GDCRNATools)
library(rtracklayer)
library(tibble)
#===============================================================================================
project <- 'TCGA-PRAD'
rnadir <- paste('data', project, 'RNAseq', sep='/')
#mirdir <- paste('data', project, 'miRNAs', sep='/')
####### Download RNAseq data #######
gdcRNADownload(project.id = project,
data.type = 'RNAseq',
write.manifest = FALSE,
method = 'gdc-client',
directory = rnadir)
####### Download mature miRNA data #######
#gdcRNADownload(project.id = project,
# data.type = 'miRNAs',
# write.manifest = FALSE,
# method = 'gdc-client',
# directory = mirdir)
####### Download clinical data #######
clinicaldir <- paste('data', project, 'Clinical', sep='/')
gdcClinicalDownload(project.id = project,
write.manifest = FALSE,
method = 'gdc-client',
directory = clinicaldir)
####### Parse RNAseq metadata #######
metaMatrix.RNA <- gdcParseMetadata(project.id = project,
data.type = 'RNAseq',
write.meta = FALSE)
####### Filter duplicated samples in RNAseq metadata #######
metaMatrix.RNA <- gdcFilterDuplicate(metaMatrix.RNA)
####### Filter non-Primary Tumor and non-Solid Tissue Normal samples in RNAseq metadata #######
metaMatrix.RNA <- gdcFilterSampleType(metaMatrix.RNA)
####### Parse miRNAs metadata #######
#metaMatrix.MIR <- gdcParseMetadata(project.id = project,
# data.type = 'miRNAs',
# write.meta = FALSE)
####### Filter duplicated samples in miRNAs metadata #######
#metaMatrix.MIR <- gdcFilterDuplicate(metaMatrix.MIR)
####### Filter non-Primary Tumor and non-Solid Tissue Normal samples in miRNAs metadata #######
#metaMatrix.MIR <- gdcFilterSampleType(metaMatrix.MIR)
####### Merge RNAseq data #######
rnaCounts <- gdcRNAMerge(metadata = metaMatrix.RNA,
path = rnadir, # the folder in which the data stored
organized = FALSE, # if the data are in separate folders
data.type = 'RNAseq')
View(rnaCounts[1:5,])
saveRDS(rnaCounts, file=paste0('data/RNAseq_Counts_', gsub('-', '_', project), '.RDS'))
####### Merge miRNAs data #######
#mirCounts <- gdcRNAMerge(metadata = metaMatrix.MIR,
# path = mirdir, # the folder in which the data stored
# organized = FALSE, # if the data are in separate folders
# data.type = 'miRNAs')
#saveRDS(rnaCounts, file=paste0('data/miRNAs_Counts_', gsub('-', '_', project), '.RDS'))
####### Merge clinical data #######
clinicalDa <- gdcClinicalMerge(path = clinicaldir, key.info = TRUE)
clinicalDa[1:6,5:10]
View(clinicalDa)
saveRDS(clinicalDa, file=paste0('data/Clinical_', gsub('-', '_', project), '.RDS'))
#########################################################################################
## submitter id
gdcRNAMerge <- function(metadata, path, data.type, organized=FALSE) {
#if (endsWith(path, '/')) {
# path = substr(path, 1, nchar(path)-1)
#}
if (organized==TRUE) {
filenames <- file.path(path, metadata$file_name,
fsep = .Platform$file.sep)
} else {
filenames <- file.path(path, metadata$file_id, metadata$file_name,
fsep = .Platform$file.sep)
}
if (data.type=='RNAseq') {
message ('############### Merging RNAseq data ################\n',
'### This step may take a few minutes ###\n')
rnaMatrix <- do.call("cbind", lapply(filenames, function(fl)
read.table(gzfile(fl))$V2))
rownames(rnaMatrix) <- read.table(gzfile(filenames[1]))$V1
rownames(rnaMatrix) <- unlist(lapply(strsplit(rownames(rnaMatrix),
'.', fixed=TRUE), function(gene) gene[1]))
#colnames(rnaMatrix) <- metadata$sample
colnames(rnaMatrix) <- metadata$submitter_id
#rnaMatrix <- rnaMatrix[biotype$ensemblID,]
nSamples = ncol(rnaMatrix)
nGenes = nrow(rnaMatrix)
message (paste('Number of samples: ', nSamples, '\n', sep=''),
paste('Number of genes: ', nGenes, '\n', sep=''))
return (rnaMatrix)
} else if (data.type=='pre-miRNAs') {
message ('############### Merging pre-miRNAs data ################\n',
'### This step may take a few minutes ###\n')
rnaMatrix <- do.call("cbind", lapply(filenames, function(fl)
read.delim(fl)$read_count))
rownames(rnaMatrix) <- read.delim(filenames[1])$miRNA_ID
colnames(rnaMatrix) <- metadata$sample
nSamples = ncol(rnaMatrix)
nGenes = nrow(rnaMatrix)
message (paste('Number of samples: ', nSamples, '\n', sep=''),
paste('Number of genes: ', nGenes, '\n', sep=''))
return (rnaMatrix)
} else if (data.type=='miRNAs') {
message ('############### Merging miRNAs data ###############\n')
mirMatrix <- lapply(filenames, function(fl) cleanMirFun(fl))
#mirs <- sort(unique(names(unlist(mirMatrix))))
mirs <- rownames(mirbase)
mirMatrix <- do.call('cbind', lapply(mirMatrix,
function(expr) expr[mirs]))
rownames(mirMatrix) <- mirbase$v21[match(mirs,rownames(mirbase))]
colnames(mirMatrix) <- metadata$sample
mirMatrix[is.na(mirMatrix)] <- 0
nSamples = ncol(mirMatrix)
nGenes = nrow(mirMatrix)
message (paste('Number of samples: ', nSamples, '\n', sep=''),
paste('Number of miRNAs: ', nGenes, '\n', sep=''))
return (mirMatrix)
} else {
return ('error !!!')
}
}