-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget_reads_from_sam.py
executable file
·42 lines (33 loc) · 1.07 KB
/
get_reads_from_sam.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
#! /usr/local/bin/python
'''Reads BAM file and filter out unmapped reads and write them in FASTA
format. Paired-end reads without proper mate-pair are filtered out.
Reads are written to standard output.
Author: Likit Preeyanon
Email: [email protected]
'''
import pysam
import sys
def write_fasta(read):
print '>%s\n%s' % (read.qname, read.seq)
def writeReads(infile):
samfile = pysam.Samfile(infile, 'rb')
for read in samfile.fetch():
if read.is_unmapped:
continue
if read.is_paired:
if read.is_proper_pair:
if read.is_read1:
read.qname = read.qname + '/1'
elif read.is_read2:
read.qname = read.qname + '/2'
else:
raise ValueError, 'Unrecognized read'
write_fasta(read)
else:
write_fasta(read)
if __name__=='__main__':
try:
writeReads(sys.argv[1])
except IOError, IndexError:
print >> sys.stderr, 'Usage: python get_reads_from_sam.py <bam file>'
raise SystemExit