comparison commons/core/parsing/Soap2Parser.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children
comparison
equal deleted inserted replaced
5:ea3082881bf8 6:769e306b7933
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 SMART.Java.Python.structure.SubMapping import SubMapping
34 from commons.core.parsing.MapperParser import MapperParser
35
36
37 def mappingToSubMapping(mapping):
38 subMapping = SubMapping()
39 subMapping.targetInterval.copy(mapping.targetInterval)
40 subMapping.queryInterval.copy(mapping.queryInterval)
41 subMapping.setDirection(mapping.getDirection())
42 subMapping.size = mapping.size
43 subMapping.tags = mapping.tags
44 return subMapping
45
46
47
48 class Soap2Parser(MapperParser):
49 """A class that parses the output of SOAP2"""
50
51 def __init__(self, fileName, verbosity = 0):
52 super(Soap2Parser, self).__init__(fileName, verbosity)
53
54
55 def __del__(self):
56 super(Soap2Parser, self).__del__()
57
58
59 def getFileFormats():
60 return ["soap2"]
61 getFileFormats = staticmethod(getFileFormats)
62
63
64 def skipFirstLines(self):
65 pass
66
67
68 def getIterator(self):
69 self.reset()
70 currentName = None
71 currentMappings = []
72 for line in self.handle:
73 mapping = self.parseLine(line)
74 name = mapping.queryInterval.name
75 if name == currentName:
76 if mapping.getTagValue("end") == "a":
77 currentMappings.append(mapping)
78 else:
79 otherEndMapping = currentMappings.pop(0)
80
81 newMapping = Mapping()
82 subMappingA = mappingToSubMapping(otherEndMapping)
83 subMappingB = mappingToSubMapping(mapping)
84 subMappingB.queryInterval.setDirection(subMappingA.queryInterval.getDirection())
85
86 newMapping.addSubMapping(subMappingA)
87 newMapping.addSubMapping(subMappingB)
88
89 newMapping.tags = otherEndMapping.tags
90 newMapping.setSize(otherEndMapping.size + mapping.size)
91 newMapping.setNbMismatches(otherEndMapping.getTagValue("nbMismatches") + mapping.getTagValue("nbMismatches"))
92 print otherEndMapping.getTagValue("nbMismatches")
93 print mapping.getTagValue("nbMismatches")
94 print newMapping.getTagValue("nbMismatches")
95 sys.exit()
96 newMapping.setTagValue("qualityString", otherEndMapping.getTagValue("qualityString") + mapping.getTagValue("qualityString"))
97 newMapping.setTagValue("occurrence", "%d" % (newMapping.getTagValue("nbOccurrences") - len(currentMappings)))
98 newMapping.setTagValue("ID", "%s-%s" % (name, newMapping.getTagValue("occurrence")))
99 del newMapping.tags["end"]
100 yield newMapping
101 else:
102 currentName = mapping.queryInterval.name
103 for currentMapping in currentMappings:
104 yield currentMapping
105 currentMappings = [mapping]
106 self.currentLineNb += 1
107
108
109 def parseLine(self, line):
110 m = re.search(r"^\s*(\S+)\s+(\w+)\s+(\S+)\s+(\d+)\s+([ab])\s+(\d+)\s+([+-])\s+(\w+)\s+(\d+)\s+(\d+)\s+", line)
111 if m == None:
112 sys.exit("\nLine %d '%s' does not have a SOAP2 format" % (self.currentLineNb, line))
113
114 name = m.group(1)
115 read = m.group(2)
116 qualityString = m.group(3)
117 nbOccurrences = int(m.group(4))
118 end = m.group(5)
119 size = int(m.group(6))
120 direction = m.group(7)
121 chromosome = m.group(8)
122 genomeStart = int(m.group(9))
123 nbMismatches = int(m.group(10))
124
125 mapping = Mapping()
126 if name.endswith("/1") or name.endswith("/2"):
127 name = name[:-2]
128
129 mapping.queryInterval.name = name
130 mapping.queryInterval.setDirection(direction)
131 mapping.queryInterval.setStart(1)
132 mapping.queryInterval.setEnd(size)
133
134 mapping.targetInterval.setChromosome(chromosome)
135 mapping.targetInterval.setStart(genomeStart)
136 mapping.targetInterval.setSize(size)
137
138 mapping.setDirection(direction)
139 mapping.setSize(size)
140
141 mapping.setNbMismatches(nbMismatches)
142 mapping.setNbGaps(0)
143 mapping.setTagValue("qualityString", qualityString)
144 mapping.setTagValue("nbOccurrences", nbOccurrences)
145 mapping.setTagValue("end", end)
146
147 return mapping
148