forked from brianpenghe/bamsam-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getBAMalignmentsFromCoordinates.py
42 lines (35 loc) · 1.03 KB
/
getBAMalignmentsFromCoordinates.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
##################################
# #
# Last modified 04/13/2012 #
# #
# Georgi Marinov #
# #
##################################
import pysam
import sys
import string
import math
def main(argv):
if len(argv) < 3:
print 'usage: python %s bedfilename BAMfilename outputfilename' % argv[0]
sys.exit(1)
bed = argv[1]
SAM = argv[2]
outfilename = argv[3]
samfile = pysam.Samfile(SAM, "rb" )
outfile = pysam.Samfile(outfilename, "wb", template=samfile)
lineslist = open(bed)
for line in lineslist:
if line[0]=='#':
continue
if line.startswith('track'):
continue
fields=line.strip().split('\t')
chr=fields[0]
start=int(fields[1])
stop=int(fields[2])
for alignedread in samfile.fetch(chr, start, stop):
outfile.write(alignedread)
outfile.close()
if __name__ == '__main__':
main(sys.argv)