-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprogramFlow.R
307 lines (235 loc) · 12.1 KB
/
programFlow.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
bsub <- function(cpus=1, maxmem=NULL, wait=NULL, jobName=NULL, logFile=NULL, command=NULL){
stopifnot(!is.null(maxmem))
if(Sys.Date()>=as.Date("2015-06-01", "%Y-%m-%d")){
queue <- "normal"
}else{
queue <- "umem"
}
cmd <- paste0("bsub -q ", queue, " -n ", as.character(cpus), " -M ", maxmem)
if(!is.null(wait)){
cmd <- paste0(cmd, " -w \"", wait, "\"")
}
if(!is.null(jobName)){
cmd <- paste0(cmd, " -J \"", jobName, "\"")
}
if(!is.null(logFile)){
cmd <- paste0(cmd, " -o ", logFile)
}
cmd <- paste0(cmd, " ", command) #no default, should crash if no command provided
system(cmd)
}
#takes a textual genome identifier (ie. hg18) and turns it into the correct
#BSgenome object
get_reference_genome <- function(reference_genome) {
library("BSgenome")
pattern <- paste0("\\.", reference_genome, "$")
match_index <- which(grepl(pattern, installed.genomes()))
if (length(match_index) != 1) {
write("Installed genomes are:", stderr())
write(installed.genomes(), stderr())
stop(paste("Cannot find unique genome for", reference_genome))
}
BS_genome_full_name <- installed.genomes()[match_index]
library(BS_genome_full_name, character.only=T)
get(BS_genome_full_name)
}
alignSeqs <- function(){
# do alignments
toAlign <- system("ls */*.fa", intern=T)
alignFile <- toAlign[as.integer(system("echo $LSB_JOBINDEX", intern=T))]
alias <- strsplit(alignFile, "/")[[1]][1]
completeMetadata <- get(load("completeMetadata.RData"))
genome <- completeMetadata[completeMetadata$alias==alias,"refGenome"]
indexPath <- paste0(genome, ".2bit")
system(paste0("blat ", indexPath, " ", alignFile, " -ooc=", genome, ".11.ooc ", alignFile, ".psl -tileSize=11 -repMatch=112312 -t=dna -q=dna -minIdentity=85 -minScore=27 -dots=1000 -out=psl -noHead"))
system(paste0("gzip ", alignFile, ".psl"))
}
callIntSites <- function(){
codeDir <- get(load("codeDir.RData"))
source(paste0(codeDir, "/intSiteLogic.R"))
sampleID <- as.integer(system("echo $LSB_JOBINDEX", intern=T))
completeMetadata <- get(load("completeMetadata.RData"))[sampleID,]
status <- tryCatch(eval(as.call(append(processAlignments,
unname(as.list(completeMetadata[c("alias", "minPctIdent",
"maxAlignStart", "maxFragLength",
"refGenome")]))))),
error=function(e){print(paste0("Caught error: ", e$message))})
save(status, file="callStatus.RData") #working directory is changed while executing getTrimmedSeqs
}
cleanup <- function(){
cleanup <- get(load("cleanup.RData"))
if(cleanup){
system("rm *.2bit", ignore.stderr=T)
system("rm *.ooc", ignore.stderr=T)
system("rm *.RData", ignore.stderr=T)
system("rm Data/*.fasta", ignore.stderr=T)
system("rm */hits.R*.RData", ignore.stderr=T)
system("rm */R*.fa*", ignore.stderr=T)
system("rm */keys.RData", ignore.stderr=T)
system("rm */*Status.RData", ignore.stderr=T)
system("rm -r logs", ignore.stderr=T)
system("rm -r Data/demultiplexedReps", ignore.stderr=T)
}
}
demultiplex <- function(){
#Demultiplexing is currently a single-core process - perhaps it could be made
#more efficient by having each error-correct worker do its own
#mini-demultiplex with the barcodes that it error corrected, then write their
#own shorter fastq files which can be cat'd together after everything is done
library("ShortRead")
I1 <- readFasta(list.files("Data", pattern="correctedI1-.", full.names=T)) #readFasta("Data/correctedI1.fasta")
completeMetadata <- get(load("completeMetadata.RData"))
I1 <- I1[as.vector(sread(I1)) %in% completeMetadata$bcSeq]
samples <- completeMetadata[match(as.character(sread(I1)), completeMetadata$bcSeq), "alias"]
#only necessary if using native data - can parse out description w/ python
I1Names <- sapply(strsplit(as.character(ShortRead::id(I1)), " "), "[[", 1)#for some reason we can't dynamically set name/id on ShortRead!
rm(I1)
suppressWarnings(dir.create("Data/demultiplexedReps"))
#R1
R1 <- readFastq("Data/Undetermined_S0_L001_R1_001.fastq.gz")
R1Names <- sapply(strsplit(as.character(ShortRead::id(R1)), " "), "[[", 1)#for some reason we can't dynamically set name/id on ShortRead!
names(R1Names) <- NULL
R1 <- R1[match(I1Names, R1Names)]
R1 <- split(R1, samples)
for (i in 1:length(R1)){writeFastq(R1[[i]], paste0("Data/demultiplexedReps/", names(R1[i]), "_R1.fastq.gz"), mode="w")}
rm(R1, R1Names)
#R2
R2 <- readFastq("Data/Undetermined_S0_L001_R2_001.fastq.gz")
R2Names <- sapply(strsplit(as.character(ShortRead::id(R2)), " "), "[[", 1) #for some reason we can't dynamically set name/id on ShortRead!
names(R2Names) <- NULL
R2 <- R2[match(I1Names, R2Names)]
R2 <- split(R2, samples)
for (i in 1:length(R2)){writeFastq(R2[[i]], paste0("Data/demultiplexedReps/", names(R2[i]), "_R2.fastq.gz"), mode="w")}
rm(R2, R2Names, I1Names, samples)
}
errorCorrectBC <- function(){
library("ShortRead")
codeDir <- get(load("codeDir.RData"))
completeMetadata <- get(load("completeMetadata.RData"))
bushmanJobID <- get(load("bushmanJobID.RData"))
I1 <- readFastq("Data/Undetermined_S0_L001_I1_001.fastq.gz")
I1 <- trimTailw(I1, 2, "0", 12)
I1 <- I1[width(I1)==max(width(I1))]
I1 <- split(I1, ceiling(seq_along(I1)/500000))
for(chunk in names(I1)){
writeFasta(I1[[chunk]], file=paste0("Data/trimmedI1-", chunk, ".fasta"))
}
bsub(jobName=paste0("BushmanErrorCorrectWorker_", bushmanJobID, "[1-", length(I1),"]"),
maxmem=1000,
logFile="logs/errorCorrectWorkerOutput%I.txt",
command=paste0("python ", codeDir, "/errorCorrectIndices/processGolay.py")
)
bsub(wait=paste0("done(BushmanErrorCorrectWorker_", bushmanJobID, ")"),
jobName=paste0("BushmanDemultiplex_", bushmanJobID),
maxmem=64000, #just in case
logFile="logs/demultiplexOutput.txt",
command=paste0("Rscript -e \"source('", codeDir, "/programFlow.R'); demultiplex();\"")
)
#trim seqs
bsub(wait=paste0("done(BushmanDemultiplex_", bushmanJobID, ")"),
jobName=paste0("BushmanTrimReads_", bushmanJobID, "[1-", nrow(completeMetadata), "]"),
maxmem=6000,
logFile="logs/trimOutput%I.txt",
command=paste0("Rscript -e \"source('", codeDir, "/programFlow.R'); trimReads();\"")
)
#post-trim processing, also kicks off alignment and int site calling jobs
bsub(wait=paste0("done(BushmanTrimReads_", bushmanJobID, ")"),
jobName=paste0("BushmanPostTrimProcessing_", bushmanJobID),
maxmem=8000,
logFile="logs/postTrimOutput.txt",
command=paste0("Rscript -e \"source('", codeDir, "/programFlow.R'); postTrimReads();\"")
)
}
postTrimReads <- function(){
library("BSgenome")
library("rtracklayer") #needed for exporting genome to 2bit
completeMetadata <- get(load("completeMetadata.RData"))
codeDir <- get(load("codeDir.RData"))
bushmanJobID <- get(load("bushmanJobID.RData"))
numAliases <- nrow(completeMetadata)
numFastaFiles <- length(system("ls */*.fa", intern=T))
#make temp genomes
genomesToMake <- unique(completeMetadata$refGenome)
for(genome in genomesToMake){
export(get_reference_genome(genome), paste0(genome, ".2bit"))
system(paste0("blat ", genome, ".2bit /dev/null /dev/null -makeOoc=", genome, ".11.ooc"))
}
#align seqs
bsub(wait=paste0("done(BushmanPostTrimProcessing_", bushmanJobID, ")"),
jobName=paste0("BushmanAlignSeqs_", bushmanJobID, "[1", "-", numFastaFiles, "]"),
maxmem=8000,
logFile="logs/alignOutput%I.txt",
command=paste0("Rscript -e \"source('", codeDir, "/programFlow.R'); alignSeqs();\"")
)
#call int sites (have to find out which ones worked)
successfulTrims <- unname(sapply(completeMetadata$alias, function(x){
get(load(paste0(x, "/trimStatus.RData"))) == x
}))
bsub(wait=paste0("done(BushmanAlignSeqs_", bushmanJobID, ")"),
jobName=paste0("BushmanCallIntSites_", bushmanJobID, "[", paste(which(successfulTrims), collapse=","), "]"),
maxmem=24000, #multihits suck lots of memory
logFile="logs/callSitesOutput%I.txt",
command=paste0("Rscript -e \"source('", codeDir, "/programFlow.R'); callIntSites();\"")
)
bsub(wait=paste0("done(BushmanCallIntSites_", bushmanJobID, ")"),
jobName=paste0("BushmanCleanup_", bushmanJobID),
maxmem=1000,
logFile="logs/cleanupOutput.txt",
command=paste0("Rscript -e \"source('", codeDir, "/programFlow.R'); cleanup();\"")
)
}
trimReads <- function(){
codeDir <- get(load("codeDir.RData"))
source(paste0(codeDir, "/intSiteLogic.R"))
sampleID <- as.integer(system("echo $LSB_JOBINDEX", intern=T))
completeMetadata <- get(load("completeMetadata.RData"))[sampleID,]
alias <- completeMetadata$alias
suppressWarnings(dir.create(alias, recursive=TRUE))
status <- tryCatch(eval(as.call(append(getTrimmedSeqs,
unname(as.list(completeMetadata[c("qualityThreshold", "badQualityBases",
"qualitySlidingWindow", "primer", "ltrBit",
"largeLTRFrag", "linkerSequence", "linkerCommon",
"mingDNA", "read1", "read2", "alias", "vectorSeq")]))))),
error=function(e){print(paste0("Caught error: ", e$message))})
save(status, file="trimStatus.RData") #working directory is changed while executing getTrimmedSeqs
}
processMetadata <- function(){
bushmanJobID <- parsedArgs$jobID
#stop if any jobs already exist with the same job ID as this will confuse LSF
stopifnot(!any(grepl(bushmanJobID, suppressWarnings(system("bjobs -l | grep -o \"Job Name <[^>]*>\"", intern=T)))))
#expand codeDir to absolute path for saving
codeDir <- normalizePath(parsedArgs$codeDir)
cleanup <- parsedArgs$cleanup
#setting R's working dir also sets shell location for system calls, thus
#primaryAnalysisDir is propagated without being saved
setwd(parsedArgs$primaryAnalysisDir)
save(bushmanJobID, file=paste0(getwd(), "/bushmanJobID.RData"))
save(codeDir, file=paste0(getwd(), "/codeDir.RData"))
save(cleanup, file=paste0(getwd(), "/cleanup.RData"))
#mapping files must exist in given primary analysis dir
stopifnot(file.exists("sampleInfo.csv") & file.exists("processingParams.csv"))
sampleInfo <- read.csv("sampleInfo.csv", stringsAsFactors=F)
processingParams <- read.csv("processingParams.csv", stringsAsFactors=F)
#confirm that metadata is presented as we expect
stopifnot(nrow(sampleInfo) == nrow(processingParams))
stopifnot(!(is.null(sampleInfo$alias) | is.null(processingParams$alias)))
stopifnot(all(sampleInfo$alias %in% processingParams$alias))
completeMetadata <- merge(sampleInfo, processingParams, "alias")
#override because R thinks "F" means FALSE when all gender values are "F"
completeMetadata$gender[with(completeMetadata, gender==F)] <- "F"
completeMetadata$gender <- toupper(completeMetadata$gender)
completeMetadata$read1 <- paste0(getwd(), "/Data/demultiplexedReps/", completeMetadata$alias, "_R1.fastq.gz")
completeMetadata$read2 <- paste0(getwd(), "/Data/demultiplexedReps/", completeMetadata$alias, "_R2.fastq.gz")
stopifnot(all(c("qualityThreshold", "badQualityBases", "qualitySlidingWindow",
"primer", "ltrBit", "largeLTRFrag", "linkerSequence", "linkerCommon",
"mingDNA", "read1", "read2", "alias", "vectorSeq", "minPctIdent",
"maxAlignStart", "maxFragLength", "gender") %in% names(completeMetadata)))
save(completeMetadata, file="completeMetadata.RData")
suppressWarnings(dir.create("logs"))
#error-correct barcodes - kicks off subsequent steps
bsub(jobName=paste0("BushmanErrorCorrect_", bushmanJobID),
maxmem=6000,
logFile="logs/errorCorrectOutput.txt",
command=paste0("Rscript -e \"source('", codeDir, "/programFlow.R'); errorCorrectBC();\"")
)
}