-
Notifications
You must be signed in to change notification settings - Fork 47
/
callBowtie2.py
506 lines (368 loc) · 15.1 KB
/
callBowtie2.py
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/python
#given a fastq location, create a .sh script to run mapping through generation of sorted bam
'''
The MIT License (MIT)
Copyright (c) 2013 Charles Lin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
#===================================================================
#========================MODULES AND DEPENDENCIES===================
#===================================================================
import string
import random
import utils
#===================================================================
#==========================GLOBAL PARAMETERS========================
#===================================================================
#command arguments
bowtieString = 'bowtie2'
samtoolsString = 'samtools'
tempParentFolder = '/grail/BOWTIE_TEMP/'
fastqcString = '/usr/local/FastQC/fastqc'
fastqDelimiter = '::'
#tempParentFolder = '/mnt/d0-0/share/bradnerlab/projects/anna/BOWTIE_TEMP/'
#===================================================================
#=============================FUNCTIONS=============================
#===================================================================
def stripExtension(fileName):
'''
tries to strip the extension of a filename
can strip .tar.gz, .txt, .fastq,.gz,.zip
'''
extensionList = ['.tar.gz','.tar', '.txt','.fastq','.fasta','.gz','.zip']
for extension in extensionList:
fileName = fileName.replace(extension,'')
return fileName
#print stripExtension('CGATGT-s_8_1_sequence.txt')
def makeFileNameDict(fastqFile,genome,tempString,tempParentFolder,finalFolder,linkFolder,uniqueID='',pairedEnd = False):
'''
creates a dictionary w/ all filenames
'''
fileNameDict = {}
if pairedEnd:
fastqFileList = fastqFile.split(fastqDelimiter)
fileNameDict['fastqFile_1'] = fastqFileList[0]
fileNameDict['fastqFile_2'] = fastqFileList[1]
else:
fileNameDict['fastqFile'] = fastqFile
if uniqueID == '':
fastqName = fastqFile.split('/')[-1]
fastqName = stripExtension(fastqName)
else:
fastqName = uniqueID
fileNameDict['fastqName'] = fastqName
#make the temp Folder
tempFolder = tempParentFolder + 'bwt2_' + fastqName + tempString + '/'
fileNameDict['tempFolder'] = tempFolder
if pairedEnd:
tempFastqFile1 = tempFolder + fastqName + '_1.rawFastq'
fileNameDict['tempFastqFile_1'] = tempFastqFile1
tempFastqFile2 = tempFolder + fastqName + '_2.rawFastq'
fileNameDict['tempFastqFile_2'] = tempFastqFile2
else:
tempFastqFile = tempFolder + fastqName + '.rawFastq'
fileNameDict['tempFastqFile'] = tempFastqFile
tempSamFile = tempFolder + fastqName + '.sam'
fileNameDict['tempSamFile'] = tempSamFile
tempBamFile = tempFolder + fastqName + '.bam'
fileNameDict['tempBamFile'] = tempBamFile
tempSortedBamFile = tempFolder + fastqName + '.%s.bwt2.sorted' % (genome)
fileNameDict['tempSortedBamFile'] = tempSortedBamFile
sortedSamFile = fastqName + '.%s.bwt2.sorted.sam' % (genome)
fileNameDict['sortedSamFile'] = sortedSamFile
groupHeader = tempFolder + fastqName + '.%s.bwt2' % (genome)
fileNameDict['groupHeader'] = groupHeader
fileNameDict['finalFolder'] = finalFolder
fileNameDict['linkFolder'] = linkFolder
return fileNameDict
def extractFastqCmd(fileNameDict,pairedEnd = False):
'''
creates a command to extract/copy the fastq to a temp location
'''
if pairedEnd:
fastqList = []
fastqFile1 = fileNameDict['fastqFile_1']
tempFastqFile1 = fileNameDict['tempFastqFile_1']
fastqList.append([fastqFile1,tempFastqFile1])
fastqFile2 = fileNameDict['fastqFile_2']
tempFastqFile2 = fileNameDict['tempFastqFile_2']
fastqList.append([fastqFile2,tempFastqFile2])
else:
fastqFile = fileNameDict['fastqFile']
tempFastqFile = fileNameDict['tempFastqFile']
fastqList = [[fastqFile,tempFastqFile]]
cmdList = []
for [fastqFile,tempFastqFile] in fastqList:
#there are 3 possibilities, a gzipped, tarballed, or naked fastq
if string.lower(fastqFile).count('tar.gz') == 1:
cmd = "tar --strip-components 5 --to-stdout -xzvf %s > %s" % (fastqFile,tempFastqFile)
elif string.lower(fastqFile).count('tar') == 1:
cmd = "tar -xzvf %s > %s" % (fastqFile,tempFastqFile)
elif string.lower(fastqFile.split('.')[-1]) == 'gz':
cmd = 'cp %s %s.gz\n' % (fastqFile,tempFastqFile)
cmd+= 'gunzip %s.gz' % (tempFastqFile)
else:
cmd = 'cp %s %s' % (fastqFile,tempFastqFile)
cmdList.append(cmd)
fullCmd = string.join(cmdList,'\n')
return fullCmd
def runFastQC(fastqcString,fileNameDict,pairedEnd = False):
'''
cmd to run fastqc
'''
if pairedEnd:
fastqName = fileNameDict['fastqName']
tempFastqFile1 = fileNameDict['tempFastqFile_1']
tempFastqFile2 = fileNameDict['tempFastqFile_2']
finalFolder = fileNameDict['finalFolder']
if finalFolder[-1] != '/':
finalFolder+='/'
finalFolder1 = finalFolder + '%s_1_fastqc' % (fastqName)
finalFolder2 = finalFolder + '%s_2_fastqc' % (fastqName)
cmd = 'mkdir %s\n' % (finalFolder1)
cmd += 'mkdir %s\n' % (finalFolder2)
cmd += '%s -o %s %s\n' % (fastqcString,finalFolder1,tempFastqFile1)
cmd += '%s -o %s %s' % (fastqcString,finalFolder2,tempFastqFile2)
else:
fastqName = fileNameDict['fastqName']
tempFastqFile = fileNameDict['tempFastqFile']
finalFolder = fileNameDict['finalFolder']
if finalFolder[-1] != '/':
finalFolder+='/'
finalFolder += '%s_fastqc' % (fastqName)
cmd = 'mkdir %s\n' % (finalFolder)
cmd += '%s -o %s %s' % (fastqcString,finalFolder,tempFastqFile)
return cmd
def bowtieCmd(bowtieString,paramString,bowtieIndex,fileNameDict,pairedEnd=False):
'''
creates the bowtie command call
'''
#calling bowtie
if pairedEnd:
tempFastqFile1 = fileNameDict['tempFastqFile_1']
tempFastqFile2 = fileNameDict['tempFastqFile_2']
tempSamFile = fileNameDict['tempSamFile']
cmd = "%s %s -x %s -1 %s -2 %s -S %s" % (bowtieString,paramString,bowtieIndex,tempFastqFile1,tempFastqFile2,tempSamFile)
else:
tempFastqFile = fileNameDict['tempFastqFile']
tempSamFile = fileNameDict['tempSamFile']
cmd = "%s %s -x %s -U %s -S %s" % (bowtieString,paramString,bowtieIndex,tempFastqFile,tempSamFile)
return cmd
def removeTempFastqCmd(fileNameDict,pairedEnd = False):
'''
removes the temp fastq
'''
if pairedEnd:
tempFastqFile1 = fileNameDict['tempFastqFile_1']
tempFastqFile2 = fileNameDict['tempFastqFile_2']
cmd = '/bin/rm -f %s\n' % (tempFastqFile1)
cmd += '/bin/rm -f %s' % (tempFastqFile2)
else:
tempFastqFile = fileNameDict['tempFastqFile']
cmd = '/bin/rm -f %s' % (tempFastqFile)
return cmd
#generate a bam file
def generateTempBamCmd(samtoolsString,fileNameDict):
'''
uses samtools to convert the sam to a bam
'''
tempSamFile = fileNameDict['tempSamFile']
tempBamFile = fileNameDict['tempBamFile']
cmd = "%s view -bS '%s' > '%s'" % (samtoolsString,tempSamFile,tempBamFile)
return cmd
#change into temp directory
def changeTempDir(fileNameDict):
'''
changes into the temp directory
'''
tempFolder = fileNameDict['tempFolder']
cmd = "cd %s" % (tempFolder)
return cmd
#sort
def sortBamCmd(samtoolsString,fileNameDict):
'''
uses smatools to sort the bam
'''
tempBamFile = fileNameDict['tempBamFile']
tempSortedBamFile = fileNameDict['tempSortedBamFile']
cmd = "%s sort '%s' '%s'" % (samtoolsString,tempBamFile,tempSortedBamFile)
return cmd
#index
def indexBamCmd(samtoolsString,fileNameDict):
'''
uses samtools to index the bam
'''
tempSortedBamFile=fileNameDict['tempSortedBamFile']
cmd = "%s index '%s.bam'" % (samtoolsString,tempSortedBamFile)
return cmd
def rmSamCmd(fileNameDict):
'''
remove the sam
'''
tempSamFile = fileNameDict['tempSamFile']
cmd = "/bin/rm -f '%s'" % (tempSamFile)
return cmd
def mvSamCmd(fileNameDict):
'''
rename and move the sam
'''
tempSamFile = fileNameDict['tempSamFile']
finalFolder = fileNameDict['finalFolder']
sortedSamFile=fileNameDict['sortedSamFile']
cmd = "mv %s %s%s" % (tempSamFile,finalFolder,sortedSamFile)
return cmd
def mvBamCmd(fileNameDict):
'''
moves and renames the bam w/o the temp string
'''
groupHeader = fileNameDict['groupHeader']
finalFolder = fileNameDict['finalFolder']
cmd = "mv %s* %s" % (groupHeader,finalFolder)
return cmd
def linkBamCmd(fileNameDict):
'''
moves and renames the bam w/o the temp string
'''
groupHeader = fileNameDict['groupHeader']
finalFolder = fileNameDict['finalFolder']
linkFolder = fileNameDict['linkFolder']
groupName = groupHeader.split('/')[-1]
cmd = "ln %s%s* %s" % (finalFolder,groupName,linkFolder)
return cmd
def rmTempFiles(fileNameDict):
'''
removes everything left in the temp folder
'''
groupHeader = fileNameDict['groupHeader']
cmd = "/bin/rm -f '%s*'" % (groupHeader)
return cmd
#===================================================================
#=============================MAIN==================================
#===================================================================
def main():
'''
main run function
'''
from optparse import OptionParser
usage = "usage: %prog [options] -f [FASTQFILE] -g [GENOME] -u [UNIQUEID] -o [OUTPUTFOLDER]"
parser = OptionParser(usage = usage)
#required flags
parser.add_option("-f","--fastq", dest="fastq",nargs = 1, default=None,
help = "Enter the full path of a fastq file to be mapped")
parser.add_option("-g","--genome",dest="genome",nargs =1, default = None,
help = "specify a genome, options are hg19,hg18, mm9 or geckov2 right now")
parser.add_option("-u","--unique",dest="unique",nargs =1, default = None,
help = "specify a uniqueID")
parser.add_option("-o","--output",dest="output",nargs =1, default = None,
help = "Specify an output folder")
#optional arguments
parser.add_option("--param",dest="paramString",nargs =1, default = '',
help = "A string of bowtie parameters")
parser.add_option("--link-folder",dest="linkFolder",nargs =1, default = None,
help = "Specify a folder to symlink the bam")
parser.add_option("-p","--paired",dest="paired",action='store_true',default = False,
help = "Flag for paired end data")
parser.add_option("-S","--sam",dest="sam",action='store_true',default = False,
help = "Flag to save sam")
parser.add_option("-q","--qc",dest="qc",action='store_true',default = False,
help = "Flag to run fastqc")
(options,args) = parser.parse_args()
if not options.fastq or not options.genome or not options.unique or not options.output:
parser.print_help()
exit()
#retrive the arguments
fastqFile = options.fastq
genome = string.lower(options.genome)
uniqueID = options.unique
outputFolder = options.output
#make the output folder
outputFolder = utils.formatFolder(outputFolder,True)
#retrieve optional arguments
paramString = options.paramString
if options.linkFolder:
linkFolder = options.linkFolder
else:
linkFolder =''
pairedEnd = options.paired
#get the bowtie index
bowtieDict = {
'mm9':'/raider/index/mm9/Bowtie2Index/genome',
'hg19':'/raider/index/hg19/Bowtie2Index/genome',
'hg18':'/grail/genomes/Homo_sapiens/human_gp_mar_06_no_random/bowtie/hg18',
'geckov2':'/grail/genomes/gecko/GeCKOv2/Sequence/Bowtie2Index/gecko',
'ribo':'/raider/temp/rDNA/hg19_45S_index/genome',
'hg19_ribo':'/grail/genomes/Homo_sapiens/UCSC/hg19/Sequence/Bowtie2Index_ribo/genome',
}
bowtieIndex = bowtieDict[string.lower(genome)]
#get the temp string
tempString = '_%s' % str(random.randint(1,10000))
fileNameDict = makeFileNameDict(fastqFile,genome,tempString,tempParentFolder,outputFolder,linkFolder,uniqueID,pairedEnd)
#open the bashfile to write to
bashFileName = "%s%s_bwt2.sh" % (outputFolder,uniqueID)
bashFile = open(bashFileName,'w')
#shebang
bashFile.write('#!/usr/bin/bash\n')
#make temp directory
cmd = 'mkdir %s' % (fileNameDict['tempFolder'])
bashFile.write(cmd+'\n')
#extract fastq
cmd = extractFastqCmd(fileNameDict,pairedEnd)
bashFile.write(cmd+'\n')
#call fastqc
if options.qc:
cmd =runFastQC(fastqcString,fileNameDict,pairedEnd)
bashFile.write(cmd+'\n')
#call bowtie
cmd = bowtieCmd(bowtieString,paramString,bowtieIndex,fileNameDict,pairedEnd)
bashFile.write(cmd+'\n')
#remove temp fastq
cmd = removeTempFastqCmd(fileNameDict,pairedEnd)
bashFile.write(cmd+'\n')
#generate a bam
cmd = generateTempBamCmd(samtoolsString,fileNameDict)
bashFile.write(cmd+'\n')
#change into the temp directory
cmd = changeTempDir(fileNameDict)
bashFile.write(cmd+'\n')
#sort the bam
cmd = sortBamCmd(samtoolsString,fileNameDict)
bashFile.write(cmd+'\n')
#index
cmd = indexBamCmd(samtoolsString,fileNameDict)
bashFile.write(cmd+'\n')
#remove sam
if not options.sam:
cmd = rmSamCmd(fileNameDict)
bashFile.write(cmd+'\n')
#or move the sam
if options.sam:
cmd = mvSamCmd(fileNameDict)
bashFile.write(cmd+'\n')
#mv bams
cmd = mvBamCmd(fileNameDict)
bashFile.write(cmd+'\n')
#link bams
if options.linkFolder:
cmd = linkBamCmd(fileNameDict)
bashFile.write(cmd+'\n')
#cleanup
cmd = rmTempFiles(fileNameDict)
bashFile.write(cmd+'\n')
bashFile.close()
print "Wrote mapping command to %s" % (bashFileName)
if __name__ == "__main__":
main()