comparison commons/core/parsing/NCListParser.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -0400
parents 769e306b7933
children
comparison
equal deleted inserted replaced
37:d22fadc825e3 38:2c0c0a89fad7
1 #
2 # Copyright INRA-URGI 2009-2012
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.TranscriptListParser import TranscriptListParser
33 from SMART.Java.Python.structure.Transcript import Transcript
34 from SMART.Java.Python.structure.Interval import Interval
35 from SMART.Java.Python.ncList.NCList import NCList
36 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
37 from SMART.Java.Python.ncList.NCListFilePickle import NCListFileUnpickle
38 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
39 try:
40 import cPickle as pickle
41 except:
42 import pickle
43
44
45 class NCListParser(TranscriptListParser):
46
47
48 def __init__(self, fileName, verbosity = 0):
49 self.title = None
50 TranscriptListParser.__init__(self, fileName, verbosity)
51 self.parse()
52
53 def getFileFormats():
54 return ["nclist"]
55 getFileFormats = staticmethod(getFileFormats)
56
57 def skipFirstLines(self):
58 return
59
60 def parse(self):
61 handle = open(self.fileName)
62 self.sortedFileNames = pickle.load(handle)
63 self.nbElements = pickle.load(handle)
64 self.nbElementsPerChromosome = pickle.load(handle)
65 self.ncLists = pickle.load(handle)
66 for ncList in self.ncLists.values():
67 ncList._reopenFiles()
68 handle.close()
69 self.chromosomes = sorted(self.nbElementsPerChromosome.keys())
70 self.fileNames = dict([chromosome, self.ncLists[chromosome]._transcriptFileName] for chromosome in self.chromosomes)
71 self.currentReader = None
72 self.currentChrIndex = 0
73
74 def getSortedFileNames(self):
75 return self._sortedFileNames
76
77 def getNbElements(self):
78 return self._nbElements
79
80 def getNbElementsPerChromosome(self):
81 return self._nbElementsPerChromosome
82
83 def getNCLists(self):
84 return self._ncLists
85
86 def reset(self):
87 self.currentChrIndex = 0
88 self.currentReader = None
89
90 def gotoAddress(self, address):
91 self.currentReader.gotoAddress(address)
92
93 def getCurrentAddress(self):
94 return self.getCurrentTranscriptAddress()
95
96 def getCurrentTranscriptAddress(self):
97 if self.currentReader == None:
98 return 0
99 return self.currentReader.getCurrentTranscriptAddress()
100
101 def getNextTranscript(self):
102 if self.currentReader == None:
103 self.currentReader = NCListFileUnpickle(self.fileNames[self.chromosomes[0]])
104 transcript = self.currentReader.getNextTranscript()
105 if transcript == False:
106 self.currentChrIndex += 1
107 if self.currentChrIndex >= len(self.chromosomes):
108 return None
109 self.currentReader = NCListFileUnpickle(self.fileNames[self.chromosomes[self.currentChrIndex]])
110 transcript = self.currentReader.getNextTranscript()
111 return transcript
112
113 def getInfos(self):
114 self.size = 0
115 self.reset()
116 progress = UnlimitedProgress(100000, "Getting information on %s." % (self.fileName), self.verbosity-9)
117 transcript = self.getNextTranscript()
118 for transcript in self.getIterator():
119 self.size += transcript.getSize()
120 progress.inc()
121 progress.done()
122 self.reset()
123
124 def getNbTranscripts(self):
125 return self.nbElements