comparison SMART/Java/Python/ncList/NCListFilePickle.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 #! /usr/bin/env python
2 #
3 # Copyright INRA-URGI 2009-2010
4 #
5 # This software is governed by the CeCILL license under French law and
6 # abiding by the rules of distribution of free software. You can use,
7 # modify and/ or redistribute the software under the terms of the CeCILL
8 # license as circulated by CEA, CNRS and INRIA at the following URL
9 # "http://www.cecill.info".
10 #
11 # As a counterpart to the access to the source code and rights to copy,
12 # modify and redistribute granted by the license, users are provided only
13 # with a limited warranty and the software's author, the holder of the
14 # economic rights, and the successive licensors have only limited
15 # liability.
16 #
17 # In this respect, the user's attention is drawn to the risks associated
18 # with loading, using, modifying and/or developing or reproducing the
19 # software by the user in light of its specific status of free software,
20 # that may mean that it is complicated to manipulate, and that also
21 # therefore means that it is reserved for developers and experienced
22 # professionals having in-depth computer knowledge. Users are therefore
23 # encouraged to load and test the software's suitability as regards their
24 # requirements in conditions enabling the security of their systems and/or
25 # data to be ensured and, more generally, to use and operate it in the
26 # same conditions as regards security.
27 #
28 # The fact that you are presently reading this means that you have had
29 # knowledge of the CeCILL license and that you accept its terms.
30 #
31
32 try:
33 import cPickle as pickle
34 except:
35 import pickle
36 from SMART.Java.Python.structure.Transcript import Transcript
37
38
39 class NCListFilePickle(object):
40
41 def __init__(self, fileName, verbosity = 1):
42 self.fileName = fileName
43 self.handle = open(fileName, "wb")
44 self.verbosity = verbosity
45
46 def __del__(self):
47 if self.handle != None:
48 self.handle.close()
49
50 def addTranscript(self, transcript):
51 pickle.dump(transcript, self.handle, -1)
52
53 def write(self):
54 pass
55
56 def close(self):
57 self.__del__()
58
59
60 class NCListFileUnpickle(object):
61
62 def __init__(self, fileName, verbosity = 1):
63 self.handle = open(fileName, "rb")
64 self.verbosity = verbosity
65 self.initAddress = 0
66 self.address = self.initAddress
67 self.nbTranscripts = None
68 self.fileName = fileName
69 self.over = False
70 self.chromosome = None
71
72 def __del__(self):
73 if self.handle != None:
74 self.handle.close()
75
76 def reset(self):
77 self.handle.seek(0)
78 self.initAddress = 0
79
80 def setChromosome(self, chromosome):
81 self.chromosome = chromosome
82
83 def getNbTranscripts(self):
84 if self.nbTranscripts != None:
85 return self._nbTranscripts
86 self.nbTranscripts = 0
87 for transcript in self.getIterator():
88 self_nbTranscripts += 1
89 return self.nbTranscripts
90
91 def gotoAddress(self, address):
92 self.handle.seek(address)
93 self.address = address
94
95 def getNextTranscript(self):
96 self.address = self.handle.tell()
97 try:
98 transcript = pickle.load(self.handle)
99 if self.chromosome != None and transcript.getChromosome() != self.chromosome:
100 self.over = True
101 return False
102 return transcript
103 except EOFError:
104 self.over = True
105 return False
106
107 def getIterator(self):
108 self.gotoAddress(self.initAddress)
109 while True:
110 transcript = self.getNextTranscript()
111 if not transcript:
112 self.over = True
113 return
114 yield transcript
115
116 def setInitAddress(self, address):
117 self.initAddress = address
118
119 def getCurrentTranscriptAddress(self):
120 return self.address
121
122 def isOver(self):
123 return self.over