File: sam2bed.py

package info (click to toggle)
python-seqcluster 1.2.9%2Bds-3
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 113,624 kB
  • sloc: python: 5,308; makefile: 184; sh: 122; javascript: 55
file content (44 lines) | stat: -rw-r--r-- 1,138 bytes parent folder | download | duplicates (3)
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