annotate commons/core/parsing/AxtParser.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
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 SMART.Java.Python.structure.SubMapping import SubMapping
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.parsing.MapperParser import MapperParser
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 from SMART.Java.Python.misc.Utils import getHammingDistance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 class AxtParser(MapperParser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 """A class that parses AXT (as given by Mosaik)"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 def __init__(self, fileName, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 super(AxtParser, self).__init__(fileName, verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 self.queryLine = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self.subjectLine = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 super(AxtParser, self).__del__()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 def getFileFormats():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 return ["axt"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 getFileFormats = staticmethod(getFileFormats)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 def skipFirstLines(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 def getInfos(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.chromosomes = set()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self.nbMappings = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self.size = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 cpt = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self.reset()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 if line == "": continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 if cpt % 3 == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 parts = line.split(" ")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self.chromosomes.add(parts[1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.size += int(parts[6])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.nbMappings += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 if self.verbosity >= 10 and self.nbMappings % 100000 == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 sys.stdout.write(" %d mappings read\r" % (self.nbMappings))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 self.reset()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if self.verbosity >= 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 print " %d mappings read" % (self.nbMappings)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 print "Done."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 def parseLine(self, line):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 if line.strip() == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self.currentLineNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if line.strip() == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 m = re.search(r"^\s*\d+\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+([+-])\s+\d+\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 if m != None:
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
96 #sys.exit("\nLine %d '%s' does not have an AXT format" % (self.currentLineNb, line))
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
97
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 mapping = Mapping()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 subMapping = SubMapping()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
101 offset = -1 if m.group(7) == "-" else 0
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 subMapping.queryInterval.setName(m.group(4))
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
103 subMapping.queryInterval.setStart(min(int(m.group(5)), int(m.group(6)))-1)
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
104 subMapping.queryInterval.setEnd(max(int(m.group(5)), int(m.group(6)))-1)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 subMapping.queryInterval.setDirection(m.group(7))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 subMapping.targetInterval.setChromosome(m.group(1))
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
108 subMapping.targetInterval.setStart(min(int(m.group(2)), int(m.group(3))) + offset)
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
109 subMapping.targetInterval.setEnd(max(int(m.group(2)), int(m.group(3))) + offset)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 subMapping.targetInterval.setDirection(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 subMapping.setSize(min(subMapping.targetInterval.getSize(), subMapping.queryInterval.getSize()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 subMapping.setDirection(m.group(7))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 mapping.addSubMapping(subMapping)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 mapping.setDirection(m.group(7))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 mapping.targetInterval.setChromosome(m.group(1))
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
119 mapping.targetInterval.setStart(min(int(m.group(2)), int(m.group(3))) + offset)
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
120 mapping.targetInterval.setEnd(max(int(m.group(2)), int(m.group(3))) + offset)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 mapping.queryInterval.setName(m.group(4))
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
123 mapping.queryInterval.setStart(min(int(m.group(5)), int(m.group(6)))-1)
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
124 mapping.queryInterval.setEnd(max(int(m.group(5)), int(m.group(6)))-1)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 mapping.setSize(min(mapping.targetInterval.getSize(), mapping.queryInterval.getSize()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
128 for line in self.handle:
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
129 string1 = line.strip()
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
130 self.currentLineNb += 1
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
131 break
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
132 for line in self.handle:
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
133 string2 = line.strip()
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
134 self.currentLineNb += 1
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
135 break
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
136 mapping.setNbMismatches(Utils.getHammingDistance(string1, string2))
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
137 mapping.setNbGaps(0)
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
138
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self.currentMapping = mapping
31
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
140 else:
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
141 if self.queryLine == None:
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
142 self.queryLine = line
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
143 else:
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
144 self.subjectLine = line
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
145 seqLen = float(len(self.subjectLine))
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
146 dist = float(getHammingDistance(self.queryLine, self.subjectLine))
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
147 identity = ((seqLen-dist)/seqLen) *100
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
148 self.currentMapping.setIdentity(identity)
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
149 self.queryLine = None
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
150 self.subjectLine = None
0ab839023fe4 Uploaded
m-zytnicki
parents: 18
diff changeset
151 return self.currentMapping
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154