-
Notifications
You must be signed in to change notification settings - Fork 47
/
callBowtie.py
executable file
·368 lines (256 loc) · 10.2 KB
/
callBowtie.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
#!/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
#===================================================================
#==========================GLOBAL PARAMETERS========================
#===================================================================
#command arguments
bowtieString = 'bowtie2'
samtoolsString = 'samtools'
tempParentFolder = '/raider/BOWTIE_TEMP/'
fastqcString = '/usr/local/FastQC/fastqc'
#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,uniqueID=''):
'''
creates a dictionary w/ all filenames
'''
fileNameDict = {}
fileNameDict['fastqFile'] = fastqFile
if uniqueID == '':
fastqName = fastqFile.split('/')[-1]
fastqName = stripExtension(fastqName)
else:
fastqName = uniqueID
fileNameDict['fastqName'] = fastqName
#make the temp Folder
tempFolder = tempParentFolder + 'bwt_' + fastqName + tempString + '/'
fileNameDict['tempFolder'] = tempFolder
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.bwt.sorted' % (genome)
fileNameDict['tempSortedBamFile'] = tempSortedBamFile
groupHeader = tempFolder + fastqName + '.%s.bwt' % (genome)
fileNameDict['groupHeader'] = groupHeader
fileNameDict['finalFolder'] = finalFolder
return fileNameDict
def extractFastqCmd(fileNameDict):
'''
creates a command to extract/copy the fastq to a temp location
'''
fastqFile = fileNameDict['fastqFile']
tempFastqFile = fileNameDict['tempFastqFile']
#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)
return cmd
def runFastQC(fastqcString,fileNameDict):
'''
cmd to run fastqc
'''
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,seedLength,bowtieIndex,fileNameDict):
'''
creates the bowtie command call
'''
#calling bowtie
tempFastqFile = fileNameDict['tempFastqFile']
tempSamFile = fileNameDict['tempSamFile']
cmd = "%s -p 6 -X2000 -N %s -x %s -1 -2 -S %s" % (bowtieString,mismatchN,bowtieIndex,tempFastqFile,tempSamFile)
return cmd
def removeTempFastqCmd(fileNameDict):
'''
removes the temp fastq
'''
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 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 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] -l [SEEDLENGTH] -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 hg18 or mm9 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")
(options,args) = parser.parse_args()
if not options.fastq or not options.seedLength 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
#get the bowtie index
bowtieDict = {
'mm9':'/raider/index/mm9/Bowtie2Index/genome',
'hg19':'/raider/index/hg19/Bowtie2Index/genome',
}
bowtieIndex = bowtieDict[string.lower(genome)]
#get the temp string
tempString = '_%s' % str(random.randint(1,10000))
fileNameDict = makeFileNameDict(fastqFile,genome,tempString,tempParentFolder,outputFolder,uniqueID)
#open the bashfile to write to
bashFileName = "%s%s_bwt.sh" % (outputFolder,uniqueID)
bashFile = open(bashFileName,'w')
#make temp directory
cmd = 'mkdir %s' % (fileNameDict['tempFolder'])
bashFile.write(cmd+'\n')
#extract fastq
cmd = extractFastqCmd(fileNameDict)
bashFile.write(cmd+'\n')
#cmd =runFastQC(fastqcString,fileNameDict)
#bashFile.write(cmd+'\n')
#call bowtie
cmd = bowtieCmd(bowtieString,seedLength,bowtieIndex,fileNameDict)
bashFile.write(cmd+'\n')
#remove temp fastq
#cmd = removeTempFastqCmd(fileNameDict)
#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
cmd = rmSamCmd(fileNameDict)
bashFile.write(cmd+'\n')
#mv bams
cmd = mvBamCmd(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()