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
|
"""
sam2bed.py
Created by Aaron Quinlan on 2009-08-27.
Copyright (c) 2009 Aaron R. Quinlan. All rights reserved.
"""
from seqcluster.libs.classes import bedaligned
def processSAM(line):
"""
Load a SAM file and convert each line to BED format.
"""
samLine = splitLine(line.strip())
return makeBED(samLine)
def makeBED(samFields):
samFlag = int(samFields.flag)
# Only create a BED entry if the read was aligned
if (not (samFlag & 0x0004)) and samFields.pos:
name = samFields.qname
seq = name.split("-")[1]
chrom = samFields.rname
start = str(int(samFields.pos))
end = str(int(samFields.pos) + len(seq) - 1)
strand = getStrand(samFlag)
# Write out the BED entry
return bedaligned("%s\t%s\t%s\t%s\t.\t%s\n" % (chrom, start, end, name, strand))
else:
return False
def splitLine(line, delim="\t"):
splitline = line.split(delim)
return splitline
def getStrand(samFlag):
strand = "+"
if (samFlag & (0x10)): # minus strand if true.
strand = "-"
return strand
|