comparison smart_toolShed/commons/core/parsing/PslParser.py @ 0:e0f8dcca02ed

Uploaded S-MART tool. A toolbox manages RNA-Seq and ChIP-Seq data.
author yufei-luo
date Thu, 17 Jan 2013 10:52:14 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e0f8dcca02ed
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 commons.core.parsing.MapperParser import MapperParser
33 from SMART.Java.Python.structure.Mapping import Mapping
34 from SMART.Java.Python.structure.SubMapping import SubMapping
35 from SMART.Java.Python.structure.Interval import Interval
36 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
37
38 class PslParser(MapperParser):
39 """A class that parses the output of PSL format (of SSAHA and BLAT)"""
40
41 def __init__(self, fileName, verbosity = 0):
42 super(PslParser, self).__init__(fileName, verbosity)
43
44
45 def __del__(self):
46 super(PslParser, self).__del__()
47
48
49 def getFileFormats():
50 return ["psl"]
51 getFileFormats = staticmethod(getFileFormats)
52
53
54 def getInfos(self):
55 self.chromosomes = set()
56 self.nbMappings = 0
57 self.size = 0
58 self.reset()
59 progress = UnlimitedProgress(100000, "Getting info on PSL file, # mappings read:", self.verbosity)
60 for line in self.handle:
61 progress.inc()
62 line = line.strip()
63 if line == "":
64 continue
65 parts = line.split("\t")
66 chromosome = parts[13]
67 self.chromosomes.add(chromosome)
68 self.nbMappings += 1
69 self.size += len(parts[0])
70 self.reset()
71 progress.done()
72
73
74 def skipFirstLines(self):
75 while "------" not in self.handle.readline():
76 self.currentLineNb += 1
77 pass
78
79 def _computeStarts(self,seqSize,blockSize,start,targetStrand):
80 if targetStrand == "+":
81 pass
82 else:
83 start = seqSize-blockSize-start
84 return start
85
86
87
88 def parseLine(self, line):
89 m = re.search(r"^\s*(psl:\s+)?(\d+)\s+(\d+)\s+(\d+)\s+\d+\s+\d+\s+(\d+)\s+\d+\s+(\d+)\s+([+-]{1,2})\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s*$", line)
90 if m == None:
91 raise Exception("\nLine %d '%s' does not have a PSL format" % (self.currentLineNb, line))
92
93 mapping = Mapping()
94
95 queryStrand = m.group(7)[0]
96
97 if len(m.group(7)) == 1:
98 targetStrand = "+"
99 else:
100 targetStrand = m.group(7)[1]
101
102
103 for i in range(0, int(m.group(16))):
104 size = int(m.group(17).split(",")[i])
105 queryStart = int(m.group(18).split(",")[i])
106 targetStart = int(m.group(19).split(",")[i])
107 querySize = int(m.group(9))
108 targetSize = int(m.group(13))
109
110 subMapping = SubMapping()
111 subMapping.setSize(size)
112 subMapping.setDirection(m.group(7)[0])
113
114 queryInterval = Interval()
115 targetInterval = Interval()
116
117 queryInterval.setName(m.group(8))
118 queryStart = self._computeStarts(querySize,size,queryStart,targetStrand)
119 queryInterval.setStart(queryStart + 1)
120 queryInterval.setEnd(queryStart + size)
121 queryInterval.setDirection(queryStrand)
122
123 targetInterval.setChromosome(m.group(12))
124 targetStart = self._computeStarts(targetSize,size,targetStart,targetStrand)
125 targetInterval.setStart(targetStart + 1)
126 targetInterval.setEnd(targetStart + size)
127 targetInterval.setDirection(targetStrand)
128
129 subMapping.setQueryInterval(queryInterval)
130 subMapping.setTargetInterval(targetInterval)
131 mapping.addSubMapping(subMapping)
132
133 mapping.setSize(int(m.group(2)) + int(m.group(3)) + int(m.group(4)))
134 mapping.setNbMismatches(int(m.group(3)) + int(m.group(4)))
135 mapping.setNbGaps(int(m.group(5)))
136 mapping.setDirection(queryStrand)
137
138 queryInterval = Interval()
139 targetInterval = Interval()
140
141 queryInterval.setName(m.group(8))
142 queryInterval.setStart(min(int(m.group(10)), int(m.group(11))))
143 queryInterval.setEnd( max(int(m.group(10)), int(m.group(11))))
144 queryInterval.setDirection(queryStrand)
145
146 targetInterval.setChromosome(m.group(12))
147 targetInterval.setStart(min(int(m.group(14))+1, int(m.group(15))))
148 targetInterval.setEnd( max(int(m.group(14))+1, int(m.group(15))))
149 targetInterval.setDirection(targetStrand)
150
151 mapping.setQueryInterval(queryInterval)
152 mapping.setTargetInterval(targetInterval)
153
154 return mapping
155