comparison commons/core/parsing/SequenceListParser.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 sys
31 from SMART.Java.Python.structure.SequenceList import SequenceList
32 from SMART.Java.Python.misc.Progress import Progress
33
34 class SequenceListParser(object):
35 """
36 A virtual class that reads a list of sequences
37 @ivar verbosity: verbosity
38 @type verbosity: int
39 @ivar fileName: name of the file to parse
40 @type fileName: string
41 @ivar handle: file to parse
42 @type handle: file
43 @ivar nbSequences: number of sequences in the file
44 @type nbSequences: int
45 @ivar nbReadSequences: number of sequences read
46 @type nbReadSequences: int
47 @ivar currentLine: line currently read
48 @type currentLine: string
49 @ivar size: total number of nucleotides in the sequences
50 @type size: int
51 @ivar sizes: number of nucleotides per sequences
52 @type sizes: dict of string to int
53 """
54
55 def __init__(self, fileName, verbosity = 0):
56 """
57 Constructor
58 @param verbosity: verbosity
59 @type verbosity: int
60 @param fileName: name of the file to parse
61 @type fileName: string
62 """
63 self.verbosity = verbosity
64 self.fileName = fileName
65 self.nbSequences = None
66 self.nbReadSequences = 0
67 self.currentLine = None
68 self.size = None
69 self.sizes = None
70 try:
71 self.handle = open(self.fileName, "rb")
72 except IOError:
73 raise Exception("Error! Sequence file '%s' does not exist! Exiting..." % (self.fileName))
74
75
76 def __del__(self):
77 """
78 Destructor
79 """
80 if not self.handle.closed:
81 self.handle.close()
82
83
84 def close(self):
85 """
86 Close file handle
87 """
88 self.handle.close()
89
90
91 def reset(self):
92 """
93 Prepare the file to be read again from start
94 """
95 self.handle.seek(0)
96 self.currentLine = None
97 self.nbReadSequences = 0
98
99
100 def getFileFormats(self):
101 pass
102 getFileFormats = staticmethod(getFileFormats)
103
104
105 def parse(self):
106 """
107 Parse the whole file in one shot
108 @return: a list of sequence
109 """
110 sequenceList = SequenceList()
111 progress = Progress(self.getNbSequences(), "Reading %s" % (self.fileName), self.verbosity)
112 for sequence in self.getIterator():
113 sequenceList.addSequence(sequence)
114 progress.inc()
115 progress.done()
116 return sequenceList
117
118
119 def getIterator(self):
120 """
121 Iterate on the file, sequence by sequence
122 @return: an iterator to sequences
123 """
124 self.reset()
125 sequence = self.parseOne()
126 while sequence != None:
127 self.nbReadSequences += 1
128 yield sequence
129 sequence = self.parseOne()
130
131
132 def getInfos(self):
133 """
134 Get some generic information about the sequences
135 """
136 self.nbSequences = 0
137 self.size = 0
138 self.reset()
139 if self.verbosity >= 10:
140 print "Getting information on %s." % (self.fileName)
141 for sequence in self.getIterator():
142 self.nbSequences += 1
143 self.size += sequence.getSize()
144 if self.verbosity >= 10 and self.nbSequences % 100000 == 0:
145 sys.stdout.write(" %d sequences read\r" % (self.nbSequences))
146 sys.stdout.flush()
147 self.reset()
148 if self.verbosity >= 10:
149 print " %d sequences read" % (self.nbSequences)
150 print "Done."
151
152
153 def getNbSequences(self):
154 """
155 Get the number of sequences in the file
156 @return: the number of sequences
157 """
158 if self.nbSequences != None:
159 return self.nbSequences
160 self.getInfos()
161 return self.nbSequences
162
163
164 def getNbItems(self):
165 """
166 Get the number of sequences in the file
167 @return: the number of sequences
168 """
169 return self.getNbSequences()
170
171
172 def getSize(self):
173 """
174 Get the size of all the sequences
175 @return: the size
176 """
177 if self.size != None:
178 return self.size
179 self.getInfos()
180 return self.size
181
182
183 def getRegions(self):
184 """
185 Get the names of the sequences
186 @return: the names
187 """
188 if self.sizes != None:
189 return self.sizes.keys()
190
191 self.sizes = {}
192 self.reset()
193 if self.verbosity >= 10:
194 print "Getting information on %s." % (self.fileName)
195 self.nbSequences = 0
196 for sequence in self.getIterator():
197 self.sizes[sequence.name] = sequence.getSize()
198 self.nbSequences += 1
199 if self.verbosity >= 10 and self.nbSequences % 100000 == 0:
200 sys.stdout.write(" %d sequences read\r" % (self.nbSequences))
201 sys.stdout.flush()
202 self.reset()
203 if self.verbosity >= 10:
204 print " %d sequences read" % (self.nbSequences)
205 print "Done."
206 return self.sizes.keys()
207
208
209 def getSizeOfRegion(self, region):
210 """
211 Get the size of a sequence
212 @param region: the name of the sequence
213 @type region: string
214 @return: the size of the sequence
215 """
216 if self.sizes != None:
217 if region not in self.sizes:
218 raise Exception("Region %s is not found" % region)
219 return self.sizes[region]
220
221 self.getRegions()
222 if region not in self.sizes:
223 raise Exception("Region %s is not found" % region)
224
225 def __eq__(self, o):
226 if o == None:
227 return False
228 return self.fileName == o.fileName and self.nbSequences == o.nbSequences