forked from vipints/converters
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAM file processing according to AnnoJ data type.
- Loading branch information
Vipin T. Sreedharan
committed
May 18, 2012
1 parent
b6cd016
commit 2b70955
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/python | ||
""" | ||
Write a BAM file content to a plain text format. | ||
Usage: python bam_to_txt.py in.bam > out.txt | ||
""" | ||
import re, sys | ||
import pysam | ||
|
||
def AnnoJ_BAM(bam_read): | ||
for rec in sam_reader.fetch(): | ||
orient=None | ||
for atb in rec.tags: | ||
if atb[0]=='XS': | ||
orient=atb[1] | ||
print rec.qname, orient, rec.pos, rec.qlen, sam_reader.getrname(rec.rname), rec.query | ||
|
||
def sort_BAM_TAGS(bam_read): | ||
""" | ||
""" | ||
for line in sys.stdin: | ||
print line.strip('\n\r') | ||
outsam=pysam.Samfile("-", "w", template = bam_read) | ||
for rec in bam_read.fetch(): | ||
new_cont=rec.tags | ||
new_cont.insert(0, rec.tags[-1]) | ||
new_cont.pop() | ||
rec.tags=new_cont | ||
outsam.write(rec) | ||
outsam.close() | ||
|
||
def __main__(): | ||
|
||
try: | ||
bam_file=sys.argv[1] | ||
except: | ||
print __doc__ | ||
sys.exit(1) | ||
sam_reader = pysam.Samfile(bam_file, "rb") | ||
sort_BAM_TAGS(sam_reader) | ||
sam_reader.close() | ||
|
||
if __name__=="__main__":__main__() |