-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path1_download_TCGA.R
executable file
·200 lines (183 loc) · 7.62 KB
/
1_download_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
195
196
197
198
199
#---------------------------------------------------------------------------------------------------
# Read in arguments from shell, load packages
args <- commandArgs(trailingOnly=TRUE)
cancer <- as.character(args[1])
# cancer <- "BLCA"
cat("***", cancer, "***\n")
user <- system("echo $USER", intern=TRUE)
if(user == "pauer") libpath <- "/raid-04/SPH/raua/R/x86_64-redhat-linux-gnu-library/3.2"
if(user == "raua") libpath <- "/home/raua/Data/R/x86_64-redhat-linux-gnu-library/3.2"
library(TCGA2STAT, lib=libpath)
#---------------------------------------------------------------------------------------------------
# Reformat one TCGA2STAT command to allow for Illumina GA miRNA-seq data to be downloaded
my_miRNASeq <- function (ddoc, dlinks, dataset, platform, type = "count")
{
if (!(type %in% c("count", "rpmmm"))) {
message("Error: Invalid type.")
gdat <- NULL
return(gdat)
}
if (type == "count") {
type = "read_count"
}
if (type == "rpmmm") {
type = "reads_per_million_miRNA_mapped"
}
keyWord = paste("", "Level_3__miR_gene_expression__data.Level_3",
sep = "")
keyWord = paste("//a[contains(@href, '", keyWord, "')]",
sep = "")
plinks = XML::xpathSApply(ddoc, keyWord, XML::xmlGetAttr,
"href")
if(platform == "hiseq") {
plinks = plinks[grepl(paste("*.", dataset, "[.]Merge_mirnaseq__.*.hiseq_mirnaseq__.*.tar[.]gz$",
sep = ""), plinks)]
}
if(platform == "ga") {
plinks = plinks[grepl(paste("*.", dataset, "[.]Merge_mirnaseq__.*.ga_mirnaseq__.*.tar[.]gz$",
sep = ""), plinks)]
}
if (length(plinks) == 0) {
message("Error: No data available for download. Please ensure the data is available from TCGA. \n")
dat <- NULL
return(dat)
}
timestamp <- unlist(strsplit(dlinks, "/"))
timestamp <- timestamp[length(timestamp)]
gdats <- list()
for (i in 1:length(plinks)) {
download_link = paste(dlinks, trim(plinks[i]), sep = "/")
message("miRNAseq data will be imported! This may take some time!")
utils::download.file(url = download_link, destfile = paste(dataset,
"-miRNAseqGene.tar.gz", sep = ""), method = "auto",
quiet = TRUE, mode = "w")
fileList <- utils::untar(paste(dataset, "-miRNAseqGene.tar.gz",
sep = ""), list = TRUE)
grepSearch = paste("*.", dataset, "[.]mirnaseq__.*.__Level_3__miR_gene_expression__data.data.txt$",
sep = "")
fileList = fileList[grepl(grepSearch, fileList)]
utils::untar(paste(dataset, "-miRNAseqGene.tar.gz", sep = ""),
files = fileList)
fname = paste(dataset, "_", timestamp, "-miRNAseqGene.txt",
sep = "")
file.rename(from = fileList, to = fname)
file.remove(paste(dataset, "-miRNAseqGene.tar.gz", sep = ""))
delFodler <- paste(getwd(), "/", strsplit(fileList, "/")[[1]][1],
sep = "")
unlink(delFodler, recursive = TRUE)
tmpCols = utils::read.delim(fname, nrows = 1, colClasses = "character")
tmpdat = utils::read.delim(fname, skip = 1, sep = "\t",
stringsAsFactors = F)
colOrder <- 1:ncol(tmpCols)
colOrder <- colOrder[tmpCols[1, ] == type]
gnames <- tmpdat[, 1]
badg <- which(duplicated(gnames))
if (length(badg) > 0) {
gdat <- tmpdat[-badg, colOrder]
colnames(gdat) <- colnames(tmpCols)[colOrder]
colnames(gdat) <- gsub("\\.", "-", colnames(gdat))
rownames(gdat) <- gnames[-badg]
}
if (length(badg) == 0) {
gdat <- tmpdat[, colOrder]
colnames(gdat) <- colnames(tmpCols)[colOrder]
colnames(gdat) <- gsub("\\.", "-", colnames(gdat))
rownames(gdat) <- gnames
}
message(paste(nrow(gdat), "genes have been imported!"))
gdats[[i]] <- as.matrix(gdat)
file.remove(fname)
}
if (length(gdats) == 1) {
gdats <- gdats[[1]]
}
return(gdats)
}
my_getTCGA <- function (disease = "GBM", data.type = "RNASeq2", type = "",
filter = "Y", p = getOption("mc.cores", 2), clinical = FALSE,
cvars = "OS", platform="hiseq")
{
data.good <- c("RNASeq2", "RNASeq", "miRNASeq", "CNA_SNP",
"CNV_SNP", "CNA_CGH", "Methylation", "Mutation", "mRNA_Array",
"miRNA_Array")
if (!(data.type %in% data.good)) {
message("Error: Not recognized datatype for Firehose\n")
dat <- NULL
return(dat)
}
ldoc <- tryCatch({
ldoc <- XML::htmlTreeParse("http://gdac.broadinstitute.org/runs/stddata__latest/",
useInternalNodes = T)
}, error = function(e) {
ldoc = NULL
})
if (is.null(ldoc)) {
message("Error: Problem connect to Firehose. Please ensure Internet connection is working.\n")
dat <- NULL
return(dat)
}
datasets <- XML::xpathSApply(ldoc, "//a[contains(@href, 'Standardized+Data+Run+Release+Notes')]",
XML::xmlValue)
if (!(disease %in% datasets)) {
message("Error: Not recognized Disease Abbreviation for Firehose\n")
dat <- NULL
return(dat)
}
if (Sys.getenv("TAR") == "" | Sys.getenv("R_GZIPCMD") ==
"") {
message("Error: TAR is not installed in the system. Data unzip failed.\n")
dat <- NULL
return(dat)
}
dataset <- disease
llinks = unlist(XML::xpathApply(ldoc, "//a[@href]", XML::xmlGetAttr,
"href"))
dlinks = llinks[grepl(paste("/data/", dataset, "/", sep = ""),
llinks)]
ddoc = XML::htmlTreeParse(dlinks, useInternalNodes = T)
if (data.type == "miRNASeq") {
if (type == "") {
dat <- my_miRNASeq(ddoc = ddoc, dlinks = dlinks, dataset = dataset, platform = platform)
}
else {
dat <- my_miRNASeq(ddoc = ddoc, dlinks = dlinks, dataset = dataset,
type = type, platform = platform)
}
if (is.null(dat)) {
return(dat)
}
if (!clinical) {
return(list(dat = dat, clinical = NULL, merged.dat = NULL))
}
if (clinical) {
gdats <- list()
cli <- Clinical(ddoc = ddoc, dlinks = dlinks, dataset = dataset)
mdat <- NULL
if (!is.null(cli)) {
mdat <- MatrixMerge(dat = dat, cli = cli, cvars = cvars)
}
return(list(dat = dat, clinical = cli, merged.dat = mdat))
}
}
}
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
#---------------------------------------------------------------------------------------------------
# Download data
## RSEM values for RNASeqV2
rnaseq <- getTCGA(disease=cancer, data.type="RNASeq2", clinical=TRUE)
clinical <- rnaseq$clinical
rnaseq <- rnaseq$dat
## By default, only Illumina HiSeq data are downloaded for V2 (RSEM values)
## We also add in Illumina GA data to increase sample size slightly
mirna_hiseq <- my_getTCGA(disease=cancer, data.type="miRNASeq", type="rpmmm", platform="hiseq")$dat
mirna_ga <- my_getTCGA(disease=cancer, data.type="miRNASeq", type="rpmmm", platform="ga")$dat
mut <- getTCGA(disease=cancer, data.type="Mutation", type="somatic")$dat
methyl <- getTCGA(disease=cancer, data.type="Methylation", type="450K")$dat
# Note that Y chromosomes are filtered out of CNA data
cna <- getTCGA(disease=cancer, data.type="CNA_SNP")$dat
#---------------------------------------------------------------------------------------------------
# Save data
remove <- ls()[which(!ls() %in% c("rnaseq", "clinical", "mirna_hiseq", "mirna_ga",
"mut", "methyl", "cna", "cancer"))]
rm(list=remove)
save.image(paste0(cancer, "/TCGA_formattedData.RData"))