-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDEE2_v2.r
269 lines (244 loc) · 8.55 KB
/
getDEE2_v2.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
#' Get DEE2 Metadata
#'
#' This function fetches the short metadata for the species of interest.
#' @param species A character string matching a species of interest.
#' @keywords metadata
#' @export
#' @examples
#' ecoli_metadatahea<-getDee2Metadata("ecoli")
getDee2Metadata<-function(species,outfile=NULL, ...){
orgs=c("athaliana","celegans","dmelanogaster","drerio","ecoli","hsapiens","mmusculus","rnorvegicus","scerevisiae")
if (species %in% orgs == FALSE ) {
message(paste("Provided species '",species,"' is not found in the list. Check spelling and try again" ,sep=""))
message(paste("Valid choices are '",paste(orgs,collapse = "', '"),"'."))
} else {
metadataURL=paste("http://dee2.io/metadata/",species,"_metadata.tsv.cut",sep="")
if(is.null(outfile)){
metadataname=tempfile()
} else {
metadataname=outfile
if(!grepl(".tsv$",metadataname)){metadataname=paste0(metadataname,".tsv")}
}
download.file(metadataURL, destfile=metadataname, mode = "wb", ...)
mdat<-read.table(metadataname,header=T,quote="",sep='\t',fill=FALSE)
if(is.null(outfile)){unlink(metadataname)}
return(mdat)
}
}
#' Query Whether a DEE2 Dataset is Available
#'
#' This function sends a query to check whether a dataset is available or not.
#' @param species A character string matching a species of interest.
#' @param SRRvec A character string or vector thereof of SRA run accession numbers.
#' @keywords query
#' @export
#' @examples
#' x<-queryDee2("ecoli",c("SRR1067773","SRR5350513"))
queryDee2<-function(species, SRRvec,metadata=NULL, ...) {
if(is.null(metadata)){
mdat<-getDee2Metadata(species, ...)
} else {
mdat<-metadata
}
present<-SRRvec[which(SRRvec %in% mdat$SRR_accession)]
absent<-SRRvec[-which(SRRvec %in% mdat$SRR_accession)]
dat <- list("present" = present, "absent" = absent)
return(dat)
}
#' Load Gene Counts
#'
#' This function loads STAR gene level counts from a downloaded zip file.
#' @param zipname Path to the zipfile.
#' @keywords Load Gene
#' @export
#' @examples
#' x<-loadGeneCounts("~/Downloads/Data.zip")
loadGeneCounts<-function(zipname){
CM="GeneCountMatrix.tsv"
TF=tempfile()
unzip(zipname, files = CM, exdir = tempdir() )
mxname<-paste0(tempdir(),"/",CM)
file.rename(mxname,TF)
dat <- read.table(TF,row.names=1,header=T)
unlink(TF)
return(dat)
}
#' Load Transcript Counts
#'
#' This function loads Kallisto transcript level counts from a downloaded zip file.
#' @param zipname Path to the zipfile.
#' @keywords Load Transcript
#' @export
#' @examples
#' x<-loadTxCounts("~/Downloads/Data.zip")
loadTxCounts<-function(zipname){
CM="TxCountMatrix.tsv"
TF=tempfile()
unzip(zipname, files = CM, exdir = tempdir() )
mxname<-paste0(tempdir(),"/",CM)
file.rename(mxname,TF)
dat <- read.table(TF,row.names=1,header=T)
unlink(TF)
return(dat)
}
#' Load Gene Info
#'
#' This function loads gene information. This information includes gene names and lengths which is useful for downstream analysis.
#' @param zipname Path to the zipfile.
#' @keywords Load Gene
#' @export
#' @examples
#' x<-loadGeneInfo("~/Downloads/Data.zip")
loadGeneInfo<-function(zipname){
CM="GeneInfo.tsv"
TF=tempfile()
unzip(zipname, files = CM, exdir = tempdir() )
mxname<-paste0(tempdir(),"/",CM)
file.rename(mxname,TF)
dat <- read.table(TF,row.names=1,header=T)
unlink(TF)
return(dat)
}
#' Load Transcript Info
#'
#' This function loads transcript information. This information includes transcript lengths, corresponding parent gene accession and gene symbol that might be useful for downstream analysis.
#' @param zipname Path to the zipfile.
#' @keywords Load Transcript
#' @export
#' @examples
#' x<-loadTxInfo("~/Downloads/Data.zip")
loadTxInfo<-function(zipname){
CM="TxInfo.tsv"
TF=tempfile()
unzip(zipname, files = CM, exdir = tempdir() )
mxname<-paste0(tempdir(),"/",CM)
file.rename(mxname,TF)
dat <- read.table(TF,row.names=1,header=T)
unlink(TF)
return(dat)
}
#' Load Quality Control Info
#'
#' This function loads quality control data. More information about the QC metrics is available from the project github page: https://github.com/markziemann/dee2/blob/master/qc/qc_metrics.md
#' @param zipname Path to the zipfile.
#' @keywords Load Qualiy Control QC
#' @export
#' @examples
#' x<-loadQcMx("~/Downloads/Data.zip")
loadQcMx<-function(zipname){
CM="QC_Matrix.tsv"
TF=tempfile()
unzip(zipname, files = CM, exdir = tempdir() )
mxname<-paste0(tempdir(),"/",CM)
file.rename(mxname,TF)
dat <- read.table(TF,row.names=1,header=T,fill=T)
unlink(TF)
message("For more information about DEE2 QC metrics, visit https://github.com/markziemann/dee2/blob/master/qc/qc_metrics.md")
return(dat)
}
#' Load Summary Metadata
#'
#' This function loads the summary metadata, which are the most relevant SRA accession numbers.
#' @param zipname Path to the zipfile.
#' @keywords Load Metadata
#' @export
#' @examples
#' x<-loadQcMx("~/Downloads/Data.zip")
loadSummaryMeta<-function(zipname){
CM="MetadataSummary.tsv"
TF=tempfile()
unzip(zipname, files = CM, exdir = tempdir() )
mxname<-paste0(tempdir(),"/",CM)
file.rename(mxname,TF)
dat <- read.table(TF,row.names=1,header=T,quote="",sep='\t',fill=FALSE)
unlink(TF)
return(dat)
}
#' Load Full Metadata
#'
#' This function loads the full metadata, which contains many fields.
#' @param zipname Path to the zipfile.
#' @keywords Load Metadata
#' @export
#' @examples
#' x<-loadQcMx("~/Downloads/Data.zip")
loadFullMeta<-function(zipname){
CM="MetadataFull.tsv"
TF=tempfile()
unzip(zipname, files = CM, exdir = tempdir() )
mxname<-paste0(tempdir(),"/",CM)
file.rename(mxname,TF)
dat <- read.table(TF,row.names=1,header=T,quote="",fill=T,sep='\t')
unlink(TF)
return(dat)
}
#' Aggregate Transcript Counts to Gene-Level Counts
#'
#' This function converts Kallisto transcript-level expression estimates to gene-level estimates. Counts for each transcript are summed to get an aggregated gene level score.
#' @param x a getDEE2 object.
#' @keywords Aggregate transcript gene
#' @export
#' @examples
#' x<-Tx2Gene(x)
Tx2Gene<-function(x){
y<-merge(x$TxInfo,x$TxCounts,by=0)
rownames(y)=y$Row.names
y$Row.names=y$GeneSymbol=y$TxLength=NULL
yy<-aggregate(. ~ GeneID,y,sum)
rownames(yy)<-yy$GeneID
yy$GeneID=NULL
x<-c(list("Tx2Gene"=yy),x)
}
#' Get DEE2 Gen Expression Data
#'
#' This function fetches gene expression data from the DEE2 database of RNA sequencing data.
#' @param species a character string matching a species of interest.
#' @param SRRvec a character string or vector of SRA run accession numbers
#' @keywords DEE2 RNA-seq database
#' @export
#' @examples
#' x<-getDEE2("ecoli",c("SRR1613487","SRR1613488"))
getDEE2<-function(species, SRRvec, outfile=NULL, #metadata=NULL,
baseURL="http://dee2.io/cgi-bin/request.sh?", ...){
# This is revised by Xijin Ge 6/4/2019 to bypass the metadata checking of presence and absence
#dat1<-queryDee2(species, SRRvec)
#if(is.null(metadata)){
#dat1<-queryDee2(species, SRRvec)
# } else {
#dat1<-queryDee2(species, SRRvec,metadata=metadata)
#}
#absent<-dat1$absent
#present<-dat1$present
absent<- c()
present<-SRRvec
if ( length(present) < 1 ) {
message("Error. None of the specified SRR accessions are present.")
} else {
# message(paste0("Warning, datasets not found: '",paste(absent,collapse=","),"'"))
SRRvec<-gsub(" ","",present)
llist<-paste0("&x=",paste(SRRvec,collapse = "&x="))
murl <- paste0(baseURL,"org=",species, llist)
if(is.null(outfile)){
zipname=tempfile()
} else {
zipname=outfile
if(!grepl(".zip$",zipname)){zipname=paste0(zipname,".zip")}
}
download.file(murl, destfile=zipname, mode = "wb", ...)
GeneCounts<-loadGeneCounts(zipname)
TxCounts<-loadTxCounts(zipname)
GeneInfo<-loadGeneInfo(zipname)
TxInfo<-loadTxInfo(zipname)
QcMx<-loadQcMx(zipname)
MetadataSummary<-loadSummaryMeta(zipname)
MetadataFull<-loadFullMeta(zipname)
dat <- list("GeneCounts" = GeneCounts, "TxCounts" = TxCounts, "GeneInfo" = GeneInfo,
"TxInfo" = TxInfo , "QcMx" = QcMx, "MetadataSummary" = MetadataSummary , "MetadataFull" = MetadataFull ,
"absent" = absent)
if(is.null(outfile)){unlink(zipname)}
if(length(absent)>0){
message(paste0("Warning, datasets not found: '",paste(absent,collapse=","),"'"))
}
return(dat)
}
}