comparison smart_toolShed/commons/core/parsing/TranscriptListParser.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 sys
31 from SMART.Java.Python.structure.TranscriptList import TranscriptList
32 from SMART.Java.Python.misc.Progress import Progress
33 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
34
35 class TranscriptListParser(object):
36 """A (quite generic) class that reads a list of transcripts"""
37
38 def __init__(self, fileName, verbosity = 0):
39 self.verbosity = verbosity
40 self.fileName = fileName
41 self.nbTranscripts = None
42 self.size = None
43 self.chromosomes = None
44 self.currentTranscript = None
45 self.currentLineNb = 0
46 self.previousTranscriptAddress = None
47 try:
48 self.handle = open(self.fileName)
49 except IOError:
50 raise Exception("Error! Transcript file '%s' does not exist! Exiting..." % (self.fileName))
51 self.skipFirstLines()
52
53
54 def __del__(self):
55 self.close()
56
57
58 def getFileFormats():
59 pass
60 getFileFormats = staticmethod(getFileFormats)
61
62
63 def close(self):
64 if self.handle != None and not self.handle.close:
65 self.handle.close()
66 self.handle = None
67
68
69 def reset(self):
70 self.handle.seek(0)
71 self.skipFirstLines()
72 self.currentTranscript = None
73 self.currentLineNb = 0
74 self.currentTranscriptAddress = self.handle.tell()
75 self.currentAddress = self.handle.tell()
76
77
78 def gotoAddress(self, address):
79 self.reset()
80 self.handle.seek(address)
81 self.currentTranscriptAddress = address
82 self.currentAddress = address
83
84
85 def parse(self):
86 transcriptList = TranscriptList()
87 progress = Progress(self.getNbTranscripts(), "Reading %s" % (self.fileName), self.verbosity)
88 for line in self.handle:
89 self.currentLineNb += 1
90 transcript = self.parseLine(line)
91 transcriptList.addTranscript(transcript)
92 progress.inc()
93 progress.done()
94 return transcriptList
95
96
97 def getIterator(self):
98 self.reset()
99 transcript = self.getNextTranscript()
100 while transcript != None:
101 yield transcript
102 transcript = self.getNextTranscript()
103
104
105 def getCurrentAddress(self):
106 return self.currentAddress
107
108
109 def getCurrentTranscriptAddress(self):
110 return self.currentTranscriptAddress
111
112
113 def getNextTranscript(self):
114 self.currentAddress = self.handle.tell()
115 line = self.handle.readline()
116 while line != "":
117 line = line.strip()
118 self.currentLineNb += 1
119 transcript = self.parseLine(line)
120 if transcript != None:
121 return transcript
122 self.currentAddress = self.handle.tell()
123 line = self.handle.readline()
124 transcript = self.currentTranscript
125 self.currentTranscriptAddress = self.previousTranscriptAddress
126 self.currentTranscript = None
127 return transcript
128
129
130 def getInfos(self):
131 self.chromosomes = set()
132 self.nbTranscripts = 0
133 self.size = 0
134 self.reset()
135 progress = UnlimitedProgress(100000, "Getting information on %s." % (self.fileName), self.verbosity-9)
136 transcript = self.getNextTranscript()
137 for transcript in self.getIterator():
138 self.chromosomes.add(transcript.getChromosome())
139 self.nbTranscripts += 1
140 self.size += transcript.getSize()
141 progress.inc()
142 progress.done()
143 self.reset()
144
145
146 def getNbTranscripts(self):
147 if self.nbTranscripts != None:
148 return self.nbTranscripts
149 self.getInfos()
150 return self.nbTranscripts
151
152
153 def getNbItems(self):
154 return self.getNbTranscripts()
155
156
157 def getChromosomes(self):
158 if self.chromosomes != None:
159 return self.chromosomes
160 self.getInfos()
161 return self.chromosomes
162
163
164 def getSize(self):
165 if self.size != None:
166 return self.size
167 self.getInfos()
168 return self.size
169
170
171 def getNbNucleotides(self):
172 return self.getSize()
173
174
175 def setDefaultTagValue(self, name, value):
176 for transcript in self.getIterator():
177 transcript.setTag(name, value)
178
179 def __eq__(self, o):
180 if o == None:
181 return False
182 return self.fileName == o.fileName and self.nbTranscripts == o.nbTranscripts and self.size == o.size and self.chromosomes == o.chromosomes