comparison SMART/Java/Python/ncList/NCListHandler.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 import struct
33 try:
34 import cPickle as pickle
35 except:
36 import pickle
37 from SMART.Java.Python.ncList.NCList import NCList
38 from SMART.Java.Python.ncList.NCIndex import NCIndex
39 from SMART.Java.Python.ncList.NCListFilePickle import NCListFileUnpickle
40
41 LONG_SIZE = struct.calcsize('l')
42
43 INFO_PER_NCLIST = 5
44 H_FILE = 0
45 L_FILE = 1
46 G_FILE = 2
47 FIRST_LIST_SIZE = 3
48 INDEX = 4
49
50 H = 0
51 L = 1
52 T = 2
53 G = 3
54
55 def pack(input):
56 return struct.pack("l", long(input))
57 def unpack(input):
58 return struct.unpack("l", input)[0]
59
60
61 class NCListHandler(object):
62
63 def __init__(self, verbosity):
64 self._verbosity = verbosity
65 self._index = False
66
67 def setFileName(self, fileName):
68 self._fileName = fileName
69 self._handle = open(fileName, "rb")
70
71 def loadData(self):
72 self._chromosomes = pickle.load(self._handle)
73 self._nbElements = 0
74 self._nbElementsPerChromosome = {}
75 self._ncLists = {}
76 for chromosome in self._chromosomes:
77 self._nbElementsPerChromosome[chromosome] = unpack(self._handle.read(LONG_SIZE))
78 self._nbElements += self._nbElementsPerChromosome[chromosome]
79 self._headerPos = self._handle.tell()
80 for i, chromosome in enumerate(self._chromosomes):
81 ncList = NCList(self._verbosity)
82 ncList._hHandle = self._handle
83 ncList._lHandle = self._handle
84 ncList._parser = NCListFileUnpickle(self._fileName)
85 self._handle.seek(self._headerPos + i * INFO_PER_NCLIST * LONG_SIZE + H_FILE * LONG_SIZE)
86 ncList.setOffset(H, unpack(self._handle.read(LONG_SIZE)))
87 self._handle.seek(self._headerPos + i * INFO_PER_NCLIST * LONG_SIZE + L_FILE * LONG_SIZE)
88 ncList.setOffset(L, unpack(self._handle.read(LONG_SIZE)))
89 self._handle.seek(self._headerPos + i * INFO_PER_NCLIST * LONG_SIZE + G_FILE * LONG_SIZE)
90 ncList.setOffset(G, unpack(self._handle.read(LONG_SIZE)))
91 self._handle.seek(self._headerPos + i * INFO_PER_NCLIST * LONG_SIZE + FIRST_LIST_SIZE * LONG_SIZE)
92 ncList._sizeFirstList = unpack(self._handle.read(LONG_SIZE))
93 self._handle.seek(self._headerPos + i * INFO_PER_NCLIST * LONG_SIZE + INDEX * LONG_SIZE)
94 indices = unpack(self._handle.read(LONG_SIZE))
95 if indices != -1:
96 self._handle.seek(indices)
97 data = pickle.load(self._handle)
98 index = NCIndex(self._verbosity)
99 index._indices = data
100 ncList._index = index
101 self._ncLists[chromosome] = ncList
102
103 def getChromosomes(self):
104 return self._chromosomes
105
106 def getNbElements(self):
107 return self._nbElements
108
109 def getNbElementsPerChromosome(self):
110 return self._nbElementsPerChromosome
111
112 def getNCLists(self):
113 return self._ncLists
114
115 def getParser(self, chromosome = None):
116 parser = NCListFileUnpickle(self._fileName)
117 if chromosome == None:
118 parser.setInitAddress(unpack(self._handle, self._headerPos + G_FILE * LONG_SIZE))
119 return parser
120 i = self._chromosomes.index(chromosome)
121 self._handle.seek(self._headerPos + i * INFO_PER_NCLIST * LONG_SIZE + G_FILE * LONG_SIZE)
122 pos = unpack(self._handle.read(LONG_SIZE))
123 parser.setInitAddress(pos)
124 parser.setChromosome(chromosome)
125 return parser