-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsra.R
executable file
·170 lines (124 loc) · 5.19 KB
/
sra.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
################################################################################
#
# File name: sra.R
#
# Authors: Jacek Marzec ( [email protected] )
#
# Barts Cancer Institute,
# Queen Mary, University of London
# Charterhouse Square, London EC1M 6BQ
#
################################################################################
################################################################################
#
# Description: Getting data from SRA using SRAdb package
#
# Command line use: R --file=./sra.R --args "E-MTAB-567" "Y" "/scratch/jack/data/PhD/raw/Illum_HiSeq2000_RNAseq" - this will get the raw files if they exist
#
# First arg: accession number
# Second arg: tidy data arg = Y will try to remove extra names from GEO patient ids
# Third arg: the directory for the data
#
################################################################################
#===============================================================================
# Functions
#===============================================================================
##### Returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
##### Get the study design of study recoreded in GEO
getGEODetails <- function(geo_eset) {
print(geo_eset)
Name=as.character(pData(phenoData(geo_eset))[,c(1)])
if(TidyNames == "Y") {
Name=gsub("-","_",Name)
Name=gsub(" ","_",Name)
}
##### Get sample source name
Characteristics=sapply(pData(phenoData(eset))[,names(pData(phenoData(eset)))=="source_name_ch1"],trim)
##### Get sample characteristics
for (i in 1:length(names(pData(phenoData(eset)))) ) {
if ( length(grep("characteristics",names(pData(phenoData(eset)))[i])) != 0 ) {
Characteristics=data.frame(Characteristics,pData(phenoData(eset))[i])
}
}
names(Characteristics)[1]="Source_name"
phenodata=data.frame(Name,Characteristics)
print(phenodata)
sampleNames(geo_eset)=gsub(" ","_",as.character(pData(phenoData(geo_eset))[,c(1)]))
return(phenodata)
}
#===============================================================================
# Main
#===============================================================================
library("SRAdb")
Args <- commandArgs();
Acc=Args[4]
TidyNames=Args[5]
DataDir=paste(Args[6], Acc, sep="/")
print(Acc)
print(DataDir)
##### Set/create a directory for data download
if (file.exists(DataDir)){
setwd(DataDir)
} else {
dir.create(DataDir, recursive=TRUE);
setwd(DataDir)
}
##### Report used parameters to a file
write(Args, file = "parameters.txt", append = FALSE, sep="\t")
##### Download the most recent SRAdb database file
sqlfile = getSRAdbFile()
##### Connect the SRAdb database to R
sra_con = dbConnect(SQLite(), sqlfile)
##### Search SRA database with specified query
rs = getSRA(search_terms = Acc, out_types = c("run", "study"), sra_con = sra_con)
cat(paste("Study accession number:", rs$study[1], "\n", sep=" "))
##### Retrieve data file information
SRAfiles <- listSRAfile(rs$study[1], sra_con = sra_con, fileType = "fastq", srcType = "ftp")
cat(paste(nrow(SRAfiles), "fastq files", "for", nrow(rs), "samples\n", sep=" "))
##### Check the number of fastq files
if ( nrow(SRAfiles)/2 < nrow(rs) ) {
cat(" !!! WARNING: Some fastq files may be missing !!!")
}
##### Get fastq files info
FASTQinfo <- getFASTQinfo(in_acc = rs$study[1], srcType = "ftp")
missingFiles=NULL
existingFiles=NULL
##### Get fastq files from SRA
for (i in 1:nrow(FASTQinfo)) {
##### Check if there are any files missing
if ( FASTQinfo$file.name[i]=="Not available" ) {
cat(" ##################################################\n", "#\n")
cat(" # !!! fastq file", "for", FASTQinfo$run[i], "is missing !!!\n #\n")
cat(" ##################################################", "\n\n")
missingFiles <- rbind(missingFiles, FASTQinfo[i,])
##### Check if the files for current run were already downloaded
} else if ( file.exists(paste(DataDir,FASTQinfo$file.name[i],sep="/")) ) {
cat(" ##################################################\n", "#\n")
cat(" # ! fastq file", "for run ", FASTQinfo$run[i], "already exist !\n #\n")
cat(" ##################################################", "\n\n")
} else {
cat(" Downloading fastq files for run:", FASTQinfo$run[i], "\n")
getFASTQfile(in_acc = FASTQinfo$run[i], destDir = getwd(),srcType = "ftp")
##### Record samples for which fastq files were downloaded
existingFiles <- c(existingFiles, FASTQinfo$run[i])
}
}
write.table(data.frame(missingFiles),file=paste(DataDir,"/missing_fastq_files_", Acc,".txt",sep=""),sep="\t",row.names=FALSE,quote=FALSE)
##### Get experiment info for records in GEO
if ( length(grep("^GSE", Acc)) == 1 ) {
library("GEOquery")
gse_eset <- getGEO(Acc,GSEMatrix=TRUE)
##### Get the first platform here
eset=gse_eset[[1]]
phenodata=getGEODetails(eset)
write.table(phenodata,file=paste("phenodata_",Acc,".txt",sep=""),sep="\t",row.names=FALSE,quote=FALSE)
write.table(FASTQinfo,file=paste(Acc,".sdrf.txt", sep=""),sep="\t",row.names=FALSE,quote=FALSE)
##### Get experiment info for records in ArrayExpress
} else {
library("ArrayExpress")
rawset = ArrayExpress(Acc, path = DataDir, save = FALSE)
}
##### Remove the SRAdb database file
SRAdb2rm=paste("rm", sqlfile, sep=" ")
system( SRAdb2rm )