# Path: seqcluster/libs/classes.py
# class bedaligned:
#     """
#     Object that has the bed format attributes
#     """
#     def __init__(self,l):
#         l = l.strip()
#         cols = l.split("\t")
#         self.chr = cols[0]
#         self.start = cols[1]
#         self.end = cols[2]
#         self.name = cols[3]
#         self.att = cols[4]
#         self.strand = cols[5]


from seqcluster.libs.classes import bedaligned
"""
sam2bed.py

Created by Aaron Quinlan on 2009-08-27.
Copyright (c) 2009 Aaron R. Quinlan. All rights reserved.
"""



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

# Path: seqcluster/libs/classes.py
# class bedaligned:
#     """
#     Object that has the bed format attributes
#     """
#     def __init__(self,l):
#         l = l.strip()
#         cols = l.split("\t")
#         self.chr = cols[0]
#         self.start = cols[1]
#         self.end = cols[2]
#         self.name = cols[3]
#         self.att = cols[4]
#         self.strand = cols[5]


# Path: seqcluster/libs/sam2bed.py
from seqcluster.libs.classes import bedaligned

"""
sam2bed.py

Created by Aaron Quinlan on 2009-08-27.
Copyright (c) 2009 Aaron R. Quinlan. All rights reserved.
"""



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))