annotate commons/core/parsing/CoordsParser.py @ 36:44d5973c188c

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 15:02:29 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from SMART.Java.Python.structure.Mapping import Mapping
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.parsing.MapperParser import MapperParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from SMART.Java.Python.structure.SubMapping import SubMapping
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.misc import Utils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 class CoordsParser(MapperParser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 """A class that parses the .coords output of Nucmer"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 def __init__(self, fileName, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 self._lineParseRe = re.compile(r"^\s*(?P<tStart>\d+)\s+(?P<tEnd>\d+)\s+\|\s+(?P<qStart>\d+)\s+(?P<qEnd>\d+)\s+\|\s+(?P<tLength>\d+)\s+(?P<qLength>\d+)\s+\|\s+(?P<identity>\d+\.?\d*)\s+\|\s+(?P<tName>[\w\|\:\-]+)\s+(?P<qName>.*)\s*$")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 self._lineParseRe2 = re.compile(r"^\s*(?P<tStart>\d+)\s+(?P<tEnd>\d+)\s+(?P<qStart>\d+)\s+(?P<qEnd>\d+)\s+(?P<tLength>\d+)\s+(?P<qLength>\d+)\s+(?P<identity>\d+\.?\d*)\s+(?P<rlen>\d+\.?\d*)\s+(?P<qlen>\d+\.?\d*)\s+(?P<rcov>\d+\.?\d*)\s+(?P<qcov>\d+\.?\d*)\s+(?P<rframe>[-]?\d+\.?\d*)\s+(?P<qframe>[-]?\d+\.?\d*)\s+(?P<tName>[\w\|\:\-]+)\s+(?P<qName>.*)\s*$")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self._lineParseRe3 = re.compile(r"^\s*(?P<tStart>\d+)\s+(?P<tEnd>\d+)\s+\|\s+(?P<qStart>\d+)\s+(?P<qEnd>\d+)\s+\|\s+(?P<tLength>\d+)\s+(?P<qLength>\d+)\s+\|\s+(?P<identity>\d+\.?\d*)\s+(?P<sim>\d+\.?\d*)\s+(?P<stp>\d+\.?\d*)\s+\|\s+(?P<rframe>[-]?\d+\.?\d*)\s+(?P<qframe>[-]?\d+\.?\d*)\s+(?P<tName>[\w\|\:\-]+)\s+(?P<qName>.*)\s*$")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 self._lineParseRe4 = re.compile(r"^\s*(?P<tStart>\d+)\s+(?P<tEnd>\d+)\s+(?P<qStart>\d+)\s+(?P<qEnd>\d+)\s+(?P<tLength>\d+)\s+(?P<qLength>\d+)\s+(?P<identity>\d+\.?\d*)\s+(?P<sim>\d+\.?\d*)\s+(?P<stp>\d+\.?\d*)\s+(?P<rlen>\d+\.?\d*)\s+(?P<qlen>\d+\.?\d*)\s+(?P<rcov>\d+\.?\d*)\s+(?P<qcov>\d+\.?\d*)\s+(?P<rframe>[-]?\d+\.?\d*)\s+(?P<qframe>[-]?\d+\.?\d*)\s+(?P<tName>[\w\|\:\-]+)\s+(?P<qName>.*)\s*$")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self.lineType = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 MapperParser.__init__(self, fileName, verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 def getFileFormats():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 return ["coords"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 getFileFormats = staticmethod(getFileFormats)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 def skipFirstLines(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 line = self.handle.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self.currentLineNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 if "=====" in line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 if "[S1]\t[E1]\t[S2]\t[E2]\t[LEN 1]\t[LEN 2]\t[% IDY]\t[LEN R]\t[LEN Q]\t[COV R]\t[COV Q]\t[FRM]\t[TAGS]" in line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.lineType = 2
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 if "[S1] [E1] | [S2] [E2] | [LEN 1] [LEN 2] | [% IDY] [% SIM] [% STP] | [FRM] [TAGS]" in line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self.lineType = 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 if "[% IDY]\t[% SIM]\t[% STP]" in line and "[LEN Q]"in line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.lineType = 4
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 def parseLine(self, line):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 if self.lineType == 1 :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 m = self._lineParseRe.search(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 elif self.lineType == 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 m = self._lineParseRe2.search(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 elif self.lineType == 3:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 m = self._lineParseRe3.search(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 elif self.lineType == 4:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 m = self._lineParseRe4.search(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if m == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 sys.exit("\nLine %d '%s' does not have a NucMer format" % (self.currentLineNb, line))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 mapping = Mapping()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 subMapping = SubMapping()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 subMapping.queryInterval.setName(m.group("qName"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 subMapping.queryInterval.setStart(min(int(m.group("qStart")), int(m.group("qEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 subMapping.queryInterval.setEnd(max(int(m.group("qStart")), int(m.group("qEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 subMapping.queryInterval.setSize(int(m.group("qLength")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 subMapping.queryInterval.setDirection(int(m.group("qEnd")) - int(m.group("qStart")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 subMapping.targetInterval.setChromosome(m.group("tName"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 subMapping.targetInterval.setStart(min(int(m.group("tStart")), int(m.group("tEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 subMapping.targetInterval.setEnd(max(int(m.group("tStart")), int(m.group("tEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 subMapping.targetInterval.setSize(int(m.group("tLength")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 subMapping.targetInterval.setDirection(int(m.group("tEnd")) - int(m.group("tStart")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 subMapping.setDirection(int(m.group("qEnd")) - int(m.group("qStart")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 subMapping.setSize(min(int(m.group("qLength")), int(m.group("tLength"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 subMapping.setIdentity(float(m.group("identity")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 mapping.addSubMapping(subMapping)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 mapping.targetInterval.setStart(min(int(m.group("tStart")), int(m.group("tEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 mapping.targetInterval.setEnd(max(int(m.group("tStart")), int(m.group("tEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 mapping.targetInterval.setSize(int(m.group("tLength")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 mapping.targetInterval.setChromosome(m.group("tName"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 mapping.queryInterval.setStart(min(int(m.group("qStart")), int(m.group("qEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 mapping.queryInterval.setEnd(max(int(m.group("qStart")), int(m.group("qEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 mapping.queryInterval.setSize(int(m.group("qLength")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 mapping.queryInterval.setName(m.group("qName"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 mapping.setDirection(int(m.group("qEnd")) - int(m.group("qStart")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 mapping.setSize(min(int(m.group("qLength")), int(m.group("tLength"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 mapping.setIdentity(float(m.group("identity")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 mapping.setTagValue("feature", "match")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 mapping.setTagValue("Target", "%s %d %d" % (m.group("qName"), int(m.group("qStart")), int(m.group("qEnd"))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 if self.lineType ==2 or self.lineType ==4:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 mapping.setTagValue("target_pident", float(m.group("identity")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 mapping.setTagValue("target_pcover", float(m.group("qcov")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 mapping.setTagValue("target_length", int(m.group("qlen")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 # Specific to Mark Work. Commented lines because of possible slowdown.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 # for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 # string1 = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 # self.currentLineNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 # break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 # for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 # string2 = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 # self.currentLineNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 # break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 # print(len(string1),len(string2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 # mapping.setNbMismatches(Utils.getHammingDistance(string1, string2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 mapping.setNbGaps(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 return mapping