-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.bds
More file actions
443 lines (411 loc) · 15.5 KB
/
tab.bds
File metadata and controls
443 lines (411 loc) · 15.5 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#! /usr/bin/env bds
#----------------------------------------------------------------------------------------------------
# Map Reads to the reference genome
#----------------------------------------------------------------------------------------------------
#------------------------------
# Serine #MonashFlavour
#------------------------------
#----------------------------------------------------------------------------------------------------
# About STARmergeNalign.bds
# - takes a whole directory that contatins fastq files and maps to the reference genome
# - identifies samples that span different lanes and merges then together during STAR run,
# outputting sigle bam file per sample.
# - auto matches paired end
# - creates strandedNo and strandedReverse directories under the current directory and
# performce read count using htseq-count outputting results in corresponding directories.
#----------------------------------------------------------------------------------------------------
#------------------------------
# Input Parametrs
#------------------------------
helpUnsorted := true
#sys sleep 40
bool star help flag if you like to align with STAR
string fqDir help path to the directory with fastq file
string genomeIndex help path to the directory with genome index for the coresponding species
bool count help flag if you like to count reads with featureCount
string gtfFile help path to the directory with gtf file
bool prePro help flag if you like to preprocess your bam files -must for RNA-SeQC report
string fastaRef help path to the reference fasta file, required when doing preprocessing for RNA-SeQC run
bool fastqc help flag if you like to run fastQC report on the files
bool clip help flag if you like to trim your fastq files before processing further
string fqRegex
string extn
#------------------------------
# Default parameters
#------------------------------
#if(fqRegex.isEmpty()) fqRegex = "_L[0-9]{3}_R[0-9]_[0-9]{3}.fastq.gz$"
#if(fqRegex.isEmpty()) fqRegex = "_L[0-9]{3}_R[0-9].fastq.gz$"
if(fqRegex.isEmpty()) fqRegex = "_R[0-9]_[0-9]{3}.fastq.gz$"
if(extn.isEmpty()) extn = "*fastq.gz"
#------------------------------
# Make directories as required
#------------------------------
string bamFiles = "bamFiles"
string preqcBamFiles = "preqcBamFiles"
string featureNo = "featureNo"
string featureReverse = "featureReverse"
string fastqcReport = "fastqcReport"
string clippedFastq = "clippedFastq"
if((!bamFiles.exists()) && (star)) bamFiles.mkdir()
#if(!bamFiles.exists() && star) || (!bwa.isEmpty())) bamFiles.mkdir()
if((!preqcBamFiles.exists()) && (prePro)) preqcBamFiles.mkdir()
if((!clippedFastq.exists()) && (clip)) clippedFastq.mkdir()
if((!featureNo.exists()) && (count)) featureNo.mkdir()
if((!featureReverse.exists()) && (count)) featureReverse.mkdir()
if((!fastqcReport.exists()) && (fastqc)) fastqcReport.mkdir()
#------------------------------
# Sanity check
#------------------------------
if(fastqc) {
if(fqDir.isEmpty()) error "Usage: -fqDir /path/to/fastq_reads/"
}
if(clip) {
if(fqDir.isEmpty()) error "Usage: -fqDir /path/to/fastq_reads/"
}
if(star) {
if(fqDir.isEmpty()) error "Usage: -fqDir /path/to/fastq_reads/"
if(genomeIndex.isEmpty()) error "Usage: -genomeIndex /path/to/genome_index/"
}
if(count) {
if(gtfFile.isEmpty()) error "Usage: -gtfFile /path/to/gtf_file/"
}
if(prePro) {
if(fastaRef.isEmpty()) error "Usage: -fastaRef /path/to/reference_fasta_file"
}
if( args.isEmpty() ) {
printHelp()
exit(1)
}
#------------------------------
# Global variables
#------------------------------
string nesoniReads
string bamFile
#----------------------------------------------------------------------------------------------------
# Data generation loop
#----------------------------------------------------------------------------------------------------
string{} uniqueNamesMap = getUniqueNames(fqDir)
#loop over each key in the map
for(string fqread : uniqueNamesMap.keys()) {
# get value for each key and break values into list
string mapValue = uniqueNamesMap{fqread}
string[] getList = mapValue.split(",")
#------------------------------
# fastQC report
#------------------------------
if(fastqc) {
sys module load fastqc
string fastqcFiles = getList.join(" ")
task fastqc -o $fastqcReport -f fastq $fastqcFiles
}
#------------------------------
# Nesoni clip
#------------------------------
if(clip) {
sys source $HOME/pypyenv/bin/activate
string[] reads = getNesoniReads(getList)
nesoniReads = NesoniClip(reads, fqread)
}
#------------------------------
# STAR alignment
#------------------------------
if(star) {
if(clip) {
bamFile = mapSTAR(genomeIndex, nesoniReads, fqread)
}
else {
string reads = getSTARreads(getList)
bamFile = mapSTAR(genomeIndex, reads, fqread)
}
}
#----------------------------------------------------------------------
# piacrd secssion for bam file preprocessing for RNA-SeQC report
#----------------------------------------------------------------------
if(prePro) {
# put newly generated *sorted.bam files into preqcBamFiles directory
string sortedName = preqcBamFiles+"/"+bamFile.baseName().swapExt("Aligned.out.bam", "sorted.bam")
task(sortedName <- bamFile) {
sys picard SortSam TMP_DIR=tmp/ \
VALIDATION_STRINGENCY=LENIENT \
INPUT=$bamFile \
OUTPUT=$sortedName \
SORT_ORDER=coordinate
}
string reorderedName = sortedName.swapExt(".bam", "_reordered.bam")
task(reorderedName <- sortedName) {
sys picard ReorderSam TMP_DIR=tmp/ \
VALIDATION_STRINGENCY=LENIENT \
INPUT=$sortedName \
OUTPUT=$reorderedName \
REFERENCE=$fastaRef
}
string mdupsName = reorderedName.swapExt(".bam", "_marked_dups.bam")
string metricsName = reorderedName.swapExt(".bam", "_marked_dups.metrics")
task(mdupsName <- reorderedName) {
sys picard MarkDuplicates TMP_DIR=tmp/ \
VALIDATION_STRINGENCY=LENIENT \
CREATE_INDEX=true \
INPUT=$reorderedName \
OUTPUT=$mdupsName \
METRICS_FILE=$metricsName
}
}
#------------------------------
# featureCount
#------------------------------
if(count) {
string reverseFeature = featureReverse+"/"+fqread.baseName().swapExt("_Aligned.out.bam", ".txt")
task (reverseFeature <- bamFile) {
sys featureCounts -a $gtfFile \
-o $reverseFeature \
$bamFile \
-s 2
}
string noFeature = featureNo+"/"+fqread.baseName().swapExt("_Aligned.out.bam", ".txt")
task (noFeature <- bamFile) {
sys featureCounts -a $gtfFile \
-o $noFeature \
$bamFile \
-s 0
}
}
}
#----------------------------------------------------------------------------------------------------
# End
#----------------------------------------------------------------------------------------------------
#------------------------------
# Functions
#------------------------------
string{} getUniqueNames(string fqDir) {
# Map with unique root key
string{} uniqueNamesMap
for(string fq : fqDir.dir(extn)) {
# make sure we have fastq files only
string getR1Index = fq.lastIndexOf("R1")
if(getR1Index != -1) {
# get the root of the file name with regex
string[] getFile = fq.split(fqRegex)
string rootName = getFile[0]
# append to the list if it isn't in the list already
if(!uniqueNamesMap.hasKey(rootName)) uniqueNamesMap{rootName}=""
uniqueNamesMap{rootName}+=fq+","
}
}
return uniqueNamesMap
}
##----------------------------------------------------------------------------------------------------
## Deve section
##----------------------------------------------------------------------------------------------------
#string[] getReadsTest(string[] readsList) {
#
# string[] orederedReadsList
# bool checkPair
# int getListSize = readsList.size()-1
#
# for(int i : range(0,getListSize)) {
# # get fastq reads and get it pair
# string singleRead = readsList[i]
# string[] getLastBit = singleRead.split("R1")
# string lastBit = getLastBit.pop()
# string rOne = "R1"+lastBit
# string rTwo = "R2"+lastBit
# string pairedEnd = singleRead.swapExt(rOne, rTwo)
#
# orederedReadsList.add(singleRead)
# checkPair = pairedEnd.isFile()
# if(checkPair) orederedReadsList.add(pairedEnd)
# }
# orederedReadsList.add(checkPair)
# return orederedReadsList
#}
#
#string getSTARreadsTest(string[] readsList) {
#
# string[] listR1
# string[] listR2
#
# string boolItem = readsList.pop()
# bool checkPair = boolItem.parseBool()
# int getListSize = readsList.size()
#
# for(string read : readsList) {
# if(checkPair){
# string checkR1 = read.lastIndexOf("R1")
# if(checkR1 != -1) listR1.add(read)
# else listR2.add(read)
# }
# else{
# if(getListSize == 1) return readsList[0] # else listR1.add(read)
# }
#
# if(listR2) {
# string read1 = listR1.join(",")
# string read2 = listR2.join(",")
# return "$read1 $read2"
# }
#}
#
#string[] getNesoniReads(string[] readsList) {
#
# string[] nesoniList
# string[] items
# bool checkPair
# int getListSize = readsList.size()
#
# for(int i : range(0,getListSize-1)) {
# string singleRead = readsList[i]
# string[] getLastBit = singleRead.split("R1")
# string lastBit = getLastBit.pop()
# string rOne = "R1"+lastBit
# string rTwo = "R2"+lastBit
# string pairedEnd = singleRead.swapExt(rOne, rTwo)
# checkPair = pairedEnd.isFile()
#
# if(checkPair){
# nesoniList.add("pairs:")
# nesoniList.add(singleRead)
# nesoniList.add(pairedEnd)
# }
# else{
# nesoniList.add("reads:")
# nesoniList.add(singleRead)
# }
# }
# string reads = nesoniList.join(" ")
# items.add(reads)
# items.add(checkPair)
# return items
#}
##----------------------------------------------------------------------------------------------------
string getSTARreads(string[] readsList) {
string[] listR1, listR2, laneList
string read1
string read2
string lanes
string preFix
bool checkPair
int getListSize = readsList.size()-1
for(int i : range(0,getListSize)) {
# get fastq reads and get it pair
string singleRead = readsList[i]
string[] getLastBit = singleRead.split("R1")
string lastBit = getLastBit.pop()
string rOne = "R1"+lastBit
string rTwo = "R2"+lastBit
string pairedEnd = singleRead.swapExt(rOne, rTwo)
checkPair = pairedEnd.isFile()
# exctract lanes information from the file name
#string[] tmpLane = singleRead.split("_")
#string tmpLane2 = tmpLane.pop()
#string getLane = tmpLane.pop()
listR1.add(singleRead)
listR2.add(pairedEnd)
}
if(checkPair){
#laneList.add(getLane)
# concatenate for STAR usage
read1 = listR1.join(",")
read2 = listR2.join(",")
#lanes = laneList.join(",")
return "$read1 $read2"
}
else{
read1 = listR1.join(",")
return read1
}
}
string mapSTAR(string genomeIndex, string reads, string rootName) {
string rootBase = rootName.baseName()
string preFix = bamFiles+"/"+rootBase+"_"
string bamOut = preFix+"Aligned.out.bam"
task(bamOut <- reads) {
sys STAR --runThreadN 24 \
--genomeDir $genomeIndex \
--outSAMtype BAM Unsorted \
--outSAMunmapped Within \
--outSAMattrRGline ID:001 CN:Unknown DS:RNA-seq PL:ILLUMINA PM:MiSeq SM:$rootBase \
--readFilesCommand zcat \
--readFilesIn $reads \
--outFileNamePrefix $preFix
}
wait
# you can remove wait statement and reduce number of threads
# however this will run all your fastq files in parallel
# doing this I find a bit impractical on our server because of
# limited RAM - only 128 Gb
return bamOut
}
string[] breakIntoDep(string reads) {
string[] splitList = reads.split(" ")
string checkLength = splitList.size()
string read1 = splitList[0]
string checkLanes = read1.lastIndexOf(",")
if(checkLength > 1){
if(checkLanes != -1){
return splitList
}
else{
string[] read = read1.split(",")
return read
}
}
else{
return splitList
}
}
string[] getNesoniReads(string[] readsList) {
string[] nesoniList
string[] items
bool checkPair
int getListSize = readsList.size()
for(int i : range(0,getListSize-1)) {
string singleRead = readsList[i]
string[] getLastBit = singleRead.split("R1")
string lastBit = getLastBit.pop()
string rOne = "R1"+lastBit
string rTwo = "R2"+lastBit
string pairedEnd = singleRead.swapExt(rOne, rTwo)
checkPair = pairedEnd.isFile()
if(checkPair){
nesoniList.add("pairs:")
nesoniList.add(singleRead)
nesoniList.add(pairedEnd)
}
else{
nesoniList.add("reads:")
nesoniList.add(singleRead)
}
}
string reads = nesoniList.join(" ")
items.add(reads)
items.add(checkPair)
return items
}
string NesoniClip(string[] items, string rootName) {
string reads = items[0]
bool checkPair = items[1].parseBool()
string preFix = clippedFastq+"/"+rootName.baseName()
string outfq = preFix+"_single.fq.gz"
string outfq1 = preFix+"_R1.fq.gz"
string outfq2 = preFix+"_R2.fq.gz"
string logFile = preFix+"_log.txt"
if(checkPair) {
task([outfq, outfq1, outfq2, logFile] <- reads){
sys nesoni clip: --quality 28 \
--out-separate yes \
$preFix \
$reads
}
return "$outfq1 $outfq2"
}
else {
task([outfq, logFile] <- reads){
sys nesoni clip: --quality 28 \
--out-separate yes \
$preFix \
$reads
}
return outfq
}
}
#----------------------------------------------------------------------------------------------------