File: formats.py

package info (click to toggle)
smalt 0.7.6-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 72,528 kB
  • ctags: 3,006
  • sloc: ansic: 26,340; sh: 3,826; python: 1,464; makefile: 314
file content (156 lines) | stat: -rw-r--r-- 5,594 bytes parent folder | download | duplicates (6)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# parsing output formats

#############################################################################
#############################################################################
#                                                                           #
#  Copyright (C) 2013 - 2014 Genome Research Ltd.                           # 
#                                                                           #
#  Author: Hannes Ponstingl (hp3@sanger.ac.uk)                              #
#                                                                           #
#  This file is part of SMALT.                                              #
#                                                                           #
#  SMALT is free software: you can redistribute it and/or modify it under   #
#  the terms of the GNU General Public License as published by the Free     #
#  Software Foundation, either version 3 of the License, or (at your        #
#  option) any later version.                                               #
#                                                                           #
#  This program is distributed in the hope that it will be useful, but      #
#  WITHOUT ANY WARRANTY; without even the implied warranty of               #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU         #
#  General Public License for more details.                                 #
#                                                                           #
#  You should have received a copy of the GNU General Public License along  #
#  with this program.  If not, see <http://www.gnu.org/licenses/>.          #
#                                                                           #
#############################################################################
#############################################################################

class Cigar:
    from re import compile
    DEBUG = False
    CIGARSTR = compile("^cigar:([ABCDNRSP]):(\d{2})\s+(\S+)\s+(\d+)\s+(\d+)\s+([*+-])\s+" \
                       "(\S+)\s+(\d+)\s+(\d+)\s+\+\s+(\d+)\s+(.+)$")

    def __init__(self):
        self._blank()

    def next(self, infil):
        okflg = False
        isEOF = False
        while not okflg:
            lin = infil.readline()
            if not lin:
                isEOF = True
                break
            self.parse(lin)
            okflg = self.ok
            if Cigar.DEBUG:
                print "DEBUG:Cigar.next %s" % (okflg)
        if Cigar.DEBUG:
            print "DEBUG:Cigar.next returns %s" % (isEOF)
        return isEOF
    
    def parse(self, lin):
        m = Cigar.CIGARSTR.match(lin)
        if m:
            self.lin = lin.strip()
            self.mapcls = m.group(1) # class label (A,B,C,D,N,R,S)
            self.mapq = int(m.group(2)) # mapping quality score
            self.qnam = m.group(3)   # name of query (read)
            self.qseg = (int(m.group(4)), int(m.group(5)))
            self.sense = m.group(6)
            self.snam = m.group(7)   # name of subject (reference)
            self.sseg = (int(m.group(8)), int(m.group(9)))
            self.swatscor = int(m.group(10))
            self.cigar = m.group(11)
            self.cigar.strip()
            self.ok = True
            if Cigar.DEBUG:
                print "DEBUG:Cigar.parse('%s')::'%s'" % (self.lin, self.cigar)
        else:
            lin.strip()
            if lin[0] != '#':
                print "not parsed: %s" % (lin)
            self._blank()

    def getMateNo(self):
        mateno = 0
        if self.qnam[-2:] == "/1":
            mateno = 1
        elif  self.qnam[-2:] == "/2":
            mateno = 2
        return mateno
    
    def _blank(self):
        self.mapcls = ""
        self.mapq = 0
        self.qnam = ""
        self.qseg = (0,0)
        self.sense = ''
        self.snam = ""
        self.sseg = (0,0)
        self.swatscor = 0
        self.lin = ""
        self.ok = False

    def __cmp__(self, other):
        rv = cmp(self.snam, other.snam)
        if rv: return rv
        
        rv = cmp(self.sseg[0], other.sseg[0])
        if rv: return rv

        return cmp(other.sseg[1], self.sseg[1])
                
def getNextCigarPair(infil, cigA, cigB, mateno_check = True):
    isOk = False
    isEOF = cigA.next(infil)

    if not isEOF:
        if cigB.next(infil):
            exit("ERROR EOF when reading 2nd mate ...")
    
        if not cigA.ok:
            exit("ERROR when parsing 1st mate")
            
        if not cigB.ok:
            exit("ERROR: when parsing 2nd mate")
        isOk = True
    if not isEOF and mateno_check:
        if cigA.qnam[-2] != cigB.qnam[-2]:
            print "ERROR: read names don't match %s, %s" % (cigA.qnam, cigB.qnam)
            isOk = False
        elif cigA.getMateNo() != 1 or cigB.getMateNo() != 2:
            print "ERROR in mate number %s [1] and %s [2]" % (cigA.qnam, cigB.qnam)
            isOk = False
    
    return (isOk, isEOF)

def openFile(filnam, mode = 'r'):
    import gzip

    is_compressed = len(filnam) > 3 and filnam[-3:] == ".gz"
    try:
        if is_compressed:
            fil = gzip.open(filnam, mode)
        else:
            fil = open(filnam, mode)
    except:
        exit("ERROR when opening file '%s'" % filnam)

    return fil

if __name__ == "__main__":
    from sys import argv, exit

    if len(argv) < 2:
        exit("usage: %s <cigar file>" % argv[0])

    cig = Cigar()
    
    infil = openFile(argv[1])

    while not cig.next(infil):
        print cig.qnam
    
    infil.close()