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