-
Notifications
You must be signed in to change notification settings - Fork 47
/
bamToGFF.py
executable file
·426 lines (343 loc) · 17.4 KB
/
bamToGFF.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
#!/usr/bin/python
#bamToGFF.py
'''
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.
'''
#script to grab reads from a bam that align to a .gff file
from utils import *
from collections import defaultdict
import os
import string
def parseSamHeader(samFile):
'''parses any sam type file with a 3 column tab del header'''
samDict = {}
sam = open(samFile,'r')
for line in sam:
if line[0] == '@':
headerLine = line[:-1].split('\t')
samDict[headerLine[1]] = headerLine[2]
else:
break
sam.close()
return samDict
#THIS FUNCTION USED TO TRY TO LOOK UP THE UNIQUE NUMBER OF READS OR COMPUTE IT BUT WAS LATER EDITED
#BRIAN BEING THAT GUY, YOU CAN FIX IT UP AND MAKE IT FIND YOUR UNIQUE MMR
def getUniquelyMappingReads(bamFile):
'''
scripts designed to return the total number of uniquely mapping sequence tags from a bam file,
first by looking for a corresponding stats file in the folder, and second by manually computing the number.
'''
#a uniquely mapping sequence tag is defined by collapsing all tags that map to the exact same location with the exact same sequence
#first try to extract the number from a stats file
fullPath = os.path.abspath(bamFile)
bamName = fullPath.split('/')[-1].split('.')[0]
pathFolder = join(fullPath.split('/')[0:-1],'/')
bamFiles = os.listdir(pathFolder)
statsFile = filter(lambda x: x.count(bamName) ==1 and x.count('stats.') ==1,bamFiles)
if len(statsFile) == 1:
print('USING STATS FILE %s' % (pathFolder+'/'+statsFile[0]))
samDict = parseSamHeader(pathFolder+'/'+statsFile[0])
return int(samDict['UniquelyMappingSequenceTags'])
elif len(statsFile) > 1:
statsFile = filter(lambda x: x.count(bamName) ==1 and x.count('stats.concise') ==1,bamFiles)
print('USING STATS FILE %s' % (pathFolder+'/'+statsFile[0]))
samDict = parseSamHeader(pathFolder+'/'+statsFile[0])
return int(samDict['UniquelyMappingSequenceTags'])
else:
print('no precomputed stats file found for %s.' % (bamFile))
return None
def mapBamToGFF(bamFile,gff,sense = 'both',unique = 0,extension = 200,floor = 0,density = False,rpm = False,binSize = 25,clusterGram = None,matrix = None,raw = False,includeJxnReads = False):
'''maps reads from a bam to a gff'''
floor = int(floor)
bam = Bam(bamFile)
#if cluster is specified, override certain flags
if clusterGram:
density =True
binSize = int(binSize)
else:
binSize = 25
if matrix:
density = True
#new GFF to write to
newGFF = []
#millionMappedReads
#SECTION CHANGED FOR BRIAN BEING THAT GUY
if float(unique) == 0.0:
unique = False
if rpm:
MMR= round(float(bam.getTotalReads('mapped'))/1000000,4)
else:
MMR = 1
else:
if rpm:
MMR = float(unique)
else:
MMR = 1
unique = True
print('using a MMR value of %s' % (MMR))
senseTrans = maketrans('-+.','+-+')
if type(gff) == str:
gff = parseTable(gff,'\t')
#setting up a clustergram table
if clusterGram:
#first grab a header line
line = gff[0]
gffLocus = Locus(line[0],int(line[3]),int(line[4]),line[6],line[1])
nBins = gffLocus.len()/binSize
binSizeList = [nBins]
#now go through each line of the gff and make sure they're all the same length
for i in range(0,len(gff),1):
line = gff[i]
gffLocus = Locus(line[0],int(line[3]),int(line[4]),line[6],line[1])
binSizeList.append(gffLocus.len()/binSize)
binSizeList = uniquify(binSizeList)
if len(binSizeList) > 1:
print('WARNING: lines in gff are of different length. Output clustergram will have variable row length')
newGFF.append(['GENE_ID','locusLine'] + [str(x*binSize)+'_'+bamFile.split('/')[-1] for x in range(1,max(binSizeList)+1,1)])
#setting up a maxtrix table
if matrix:
newGFF.append(['GENE_ID','locusLine'] + ['bin_'+str(n)+'_'+bamFile.split('/')[-1] for n in range(1,int(matrix)+1,1)])
#getting and processing reads for gff lines
ticker = 0
print('Number lines processed')
for line in gff:
line = line[0:9]
if ticker%100 == 0:
print ticker
ticker+=1
gffLocus = Locus(line[0],int(line[3]),int(line[4]),line[6],line[1])
searchLocus = makeSearchLocus(gffLocus,int(extension),int(extension))
reads = bam.getReadsLocus(searchLocus,'both',unique,'none',includeJxnReads)
#now extend the reads and make a list of extended reads
extendedReads = []
for locus in reads:
if locus.sense() == '+' or locus.sense() == '.':
locus = Locus(locus.chr(),locus.start(),locus.end()+extension,locus.sense(), locus.ID())
if locus.sense() == '-':
locus = Locus(locus.chr(),locus.start()-extension,locus.end(),locus.sense(),locus.ID())
extendedReads.append(locus)
if gffLocus.sense() == '+' or gffLocus.sense == '.':
senseReads = filter(lambda x:x.sense() == '+' or x.sense() == '.',extendedReads)
antiReads = filter(lambda x:x.sense() == '-',extendedReads)
else:
senseReads = filter(lambda x:x.sense() == '-' or x.sense() == '.',extendedReads)
antiReads = filter(lambda x:x.sense() == '+',extendedReads)
#at this point can output starts onto the GFF unless density is called
if density:
senseHash = defaultdict(int)
antiHash = defaultdict(int)
#filling in the readHashes
if sense == '+' or sense == 'both' or sense =='.':
for read in senseReads:
for x in range(read.start(),read.end()+1,1):
senseHash[x]+=1
if sense == '-' or sense == 'both' or sense == '.':
#print('foo')
for read in antiReads:
for x in range(read.start(),read.end()+1,1):
antiHash[x]+=1
#now apply flooring and filtering for coordinates
keys = uniquify(senseHash.keys()+antiHash.keys())
if floor > 0:
keys = filter(lambda x: (senseHash[x]+antiHash[x]) > floor,keys)
#coordinate filtering
keys = filter(lambda x: gffLocus.start() < x < gffLocus.end(),keys)
if clusterGram or matrix:
clusterLine = [gffLocus.ID(),gffLocus.__str__()]
if matrix:
binSize = (gffLocus.len()-1)/int(matrix)
nBins = int(matrix)
if clusterGram:
nBins = gffLocus.len()/binSize
if binSize == 0:
clusterLine+=['NA']*int(matrix)
newGFF.append(clusterLine)
continue
n=0
if gffLocus.sense() == '+' or gffLocus.sense() =='.' or gffLocus.sense() == 'both':
i = gffLocus.start()
while n <nBins:
n+=1
binKeys = filter(lambda x: i < x < i+binSize,keys)
binDen = float(sum([senseHash[x]+antiHash[x] for x in binKeys]))/binSize
clusterLine+=[round(binDen/MMR,4)]
i = i+binSize
else:
i = gffLocus.end()
while n < nBins:
n+=1
binKeys = filter(lambda x: i-binSize < x < i,keys)
binDen = float(sum([senseHash[x]+antiHash[x] for x in binKeys]))/binSize
clusterLine+=[round(binDen/MMR,4)]
i = i-binSize
newGFF.append(clusterLine)
#for regular old density calculation
else:
senseTotalDen = float(sum([senseHash[x] for x in keys]))/gffLocus.len()
antiTotalDen = float(sum([antiHash[x] for x in keys]))/gffLocus.len()
if rpm:
senseTotalDen = senseTotalDen/MMR
antiTotalDen = antiTotalDen/MMR
if sense == 'both' or sense == '.':
if gffLocus.sense() == '+' or gffLocus.sense() == '.':
readLine = '+'+':%s' % (round(senseTotalDen,4)) + ';' +'-' + ':%s' % (round(antiTotalDen,4))
else:
readLine = '+'+':%s' % (round(antiTotalDen,4)) + ';' +'-' + ':%s' % (round(senseTotalDen,4))
elif sense == '+':
readLine = '+'+':%s' % (round(senseTotalDen,4))
elif sense == '-':
readLine = '-' + ':%s' % (round(antiTotalDen,4))
newGFF.append(line + [readLine])
#if not cluster or density simply return reads
elif raw:
if sense == 'both' or sense == '.':
if gffLocus.sense() == '+' or gffLocus.sense() == '.':
readLine = '+'+':'+ join([str(locus.start()) for locus in senseReads],',') +';' + '-'+':'+ join([str(locus.start()) for locus in antiReads],',')
else:
readLine = '+'+':'+ join([str(locus.start()) for locus in antiReads],',')+';'+'-'+':'+ join([str(locus.start()) for locus in senseReads],',')
elif sense == '+':
readLine = gffLocus.sense()+':'+ join([str(locus.start()) for locus in senseReads],',')
elif sense == '-':
readLine = string.translate(gffLocus.sense(),senseTrans)+':'+ join([str(locus.start()) for locus in antiReads],',')
newGFF.append(line+[readLine])
#if not raw and not density gives total
else:
if sense == 'both' or sense == '.':
readLine = str((len(antiReads) + len(senseReads))/MMR)
elif sense == '+':
readLine = str(len(senseReads)/MMR)
elif sense == '-':
readLine = str(len(antiReads)/MMR)
newGFF.append(line+[readLine])
return newGFF
def convertEnrichedRegionsToGFF(enrichedRegionFile):
'''converts a young lab enriched regions file into a gff'''
newGFF = []
enrichedRegions = open(enrichedRegionFile,'r')
header = enrichedRegions.readline()
i = 0
for line in enrichedRegions:
line = line[:-1].split('\t')
newLine = ['chr'+line[0],'row_'+str(i),line[4],line[1],line[2],'','.','','row_'+str(i),'']
newGFF.append(newLine)
i+=1
return newGFF
#python bamToGFF.py --density --floor 0 -b test.sam.sorted.bam -g pol2_sample.gff -o pol2_sample_mapped.gff
def main():
from optparse import OptionParser
usage = "usage: %prog [options] -b [SORTED BAMFILE] -i [INPUTFILE] -o [OUTPUTFILE]"
parser = OptionParser(usage = usage)
#required flags
parser.add_option("-b","--bam", dest="bam",nargs = 1, default=None,
help = "Enter .bam file to be processed.")
parser.add_option("-i","--input", dest="input",nargs = 1, default=None,
help = "Enter .gff or ENRICHED REGION file to be processed.")
#output flag
parser.add_option("-o","--output", dest="output",nargs = 1, default=None,
help = "Enter the output filename.")
#additional options
parser.add_option("-s","--sense", dest="sense",nargs = 1, default='both',
help = "Map to '+','-' or 'both' strands. Default maps to both.")
parser.add_option("-u","--unique", dest="unique",nargs = 1, default=0,
help = "Takes only unique sequence tags (avoids pcr repeats). must provide number of million uniquely mapping reads") #BJA changed default from None to 0
parser.add_option("-d","--density", dest="density",action='store_true', default=False,
help = "Calculates a read density for each region, returns a single value per region")
parser.add_option("-f","--floor", dest="floor",nargs =1, default=0,
help = "Sets a read floor threshold necessary to count towards density")
parser.add_option("-e","--extension", dest="extension",nargs = 1, default=200,
help = "Extends reads by n bp. Default value is 200bp")
parser.add_option("-r","--rpm", dest="rpm",action = 'store_true', default=False,
help = "Normalizes density to reads per million (rpm)")
parser.add_option("-t","--total", dest="total",action = 'store_true', default=False,
help = "Gives the total read count in the region. Overrides density, floor, and rpm flags")
parser.add_option("-c","--cluster", dest="cluster",nargs = 1, default=None,
help = "Outputs a fixed bin size clustergram. user must specify bin size.")
parser.add_option("-m","--matrix", dest="matrix",nargs = 1, default=None,
help = "Outputs a variable bin sized matrix. User must specify number of bins.")
parser.add_option("-j","--jxn", dest="jxn",action = 'store_true', default=False,
help = "if flagged, includes jxn reads")
(options,args) = parser.parse_args()
print(options)
print(args)
if options.bam:
bamFile = options.bam
fullPath = os.path.abspath(bamFile)
bamName = fullPath.split('/')[-1].split('.')[0]
pathFolder = join(fullPath.split('/')[0:-1],'/')
fileList = os.listdir(pathFolder)
hasBai = False
for fileName in fileList:
if fileName.count(bamName) == 1 and fileName.count('.bai') == 1:
hasBai = True
if not hasBai:
print('ERROR: no associated .bai file found with bam. Must use a sorted bam with accompanying index file')
parser.print_help()
exit()
if options.sense:
if ['+','-','.','both'].count(options.sense) == 0:
print('ERROR: sense flag must be followed by +,-,.,both')
parser.print_help()
exit()
if options.cluster and options.matrix:
print('ERROR: Cannot specify both matrix and clustergram flags.')
parser.print_help()
exit()
if options.matrix:
try:
int(options.matrix)
except:
print('ERROR: User must specify an integer bin number for matrix (try 50)')
parser.print_help()
exit()
if options.cluster:
try:
int(options.cluster)
except:
print('ERROR: User must specify an integer bin size for clustergram (try 25)')
parser.print_help()
exit()
if options.input and options.bam:
inputFile = options.input
if inputFile.split('.')[-1] != 'gff':
print('converting file to a .gff')
gffFile = convertEnrichedRegionsToGFF(inputFile)
else:
gffFile = inputFile
bamFile = options.bam
if options.output == None:
output = os.getcwd() + inputFile.split('/')[-1]+'.mapped'
else:
output = options.output
if options.cluster:
print('mapping to GFF and making clustergram with fixed bin width')
newGFF = mapBamToGFF(bamFile,gffFile,options.sense,options.unique,int(options.extension),options.floor,options.density,options.rpm,options.cluster,True,None,False,options.jxn)
elif options.matrix:
print('mapping to GFF and making a matrix with fixed bin number')
newGFF = mapBamToGFF(bamFile,gffFile,options.sense,options.unique,int(options.extension),options.floor,options.density,options.rpm,25,None,options.matrix,False,options.jxn)
else:
print('mapping to GFF and returning reads')
if options.total:
newGFF = mapBamToGFF(bamFile,gffFile,options.sense,options.unique,int(options.extension),options.floor,options.density,options.rpm,25,None,None,False,options.jxn)
else:
newGFF = mapBamToGFF(bamFile,gffFile,options.sense,options.unique,int(options.extension),options.floor,options.density,options.rpm,25,None,None,True)
unParseTable(newGFF,output,'\t')
else:
parser.print_help()
if __name__ == "__main__":
main()