annotate SMART/Java/Python/ncList/NCListMerger.py @ 69:1473ab954708 draft

Corrected bug in "CollapsedReads" XML file.
author m-zytnicki
date Wed, 18 Nov 2015 10:59:02 -0500
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #! /usr/bin/env python
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import struct, os, shutil
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 try:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import cPickle as pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 except:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 import pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 LONG_SIZE = struct.calcsize('l')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 INFO_PER_NCLIST = 5
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 H_FILE = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 L_FILE = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 G_FILE = 2
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 FIRST_LIST_SIZE = 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 INDEX = 4
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 def pack(input):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 return struct.pack("l", long(input))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 def unpack(input):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 return struct.unpack("l", input)[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 class NCListMerger(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 def __init__(self, verbosity):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self._verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self._index = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 def setFileName(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self._handle = open(fileName, "wb")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 def setNCLists(self, ncLists):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self._ncLists = ncLists
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self._chromosomes = sorted(self._ncLists.keys())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 def addIndex(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self._index = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 def merge(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self._writeHeader()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self._addNCLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self._handle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self._removeInputFiles()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 def _writeHeader(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 pickle.dump(self._chromosomes, self._handle, -1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 for chromosome in self._chromosomes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self._handle.write(pack(self._ncLists[chromosome]._nbLines))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 self._headerPos = self._handle.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 for chromosome in self._chromosomes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 for i in range(INFO_PER_NCLIST):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 self._handle.write(pack(-1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 def _addInHeader(self, i, info, value = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 currentPos = self._handle.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 if value == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 value = currentPos
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 self._handle.seek(self._headerPos + i * INFO_PER_NCLIST * LONG_SIZE + info * LONG_SIZE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self._handle.write(pack(value))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self._handle.seek(currentPos)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 def _addNCLists(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self._inputFileNames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 for i, chromosome in enumerate(self._chromosomes):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 ncList = self._ncLists[chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 self._addInHeader(i, H_FILE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 hFile = open(ncList._hFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 shutil.copyfileobj(hFile, self._handle)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 hFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 self._inputFileNames.append(ncList._hFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 for i, chromosome in enumerate(self._chromosomes):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 ncList = self._ncLists[chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 self._addInHeader(i, L_FILE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 lFile = open(ncList._lFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 shutil.copyfileobj(lFile, self._handle)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 lFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 self._inputFileNames.append(ncList._lFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 for i, chromosome in enumerate(self._chromosomes):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 ncList = self._ncLists[chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 self._addInHeader(i, FIRST_LIST_SIZE, ncList.getSizeFirstList())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 if self._index:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 for i, chromosome in enumerate(self._chromosomes):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 ncList = self._ncLists[chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 self._addInHeader(i, INDEX)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 pickle.dump(ncList.getIndex()._indices, self._handle, -1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 for i, chromosome in enumerate(self._chromosomes):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 ncList = self._ncLists[chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 self._addInHeader(i, G_FILE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 tFile = open(ncList._transcriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 shutil.copyfileobj(tFile, self._handle)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 tFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 self._inputFileNames.append(ncList._transcriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 def _removeInputFiles(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 for fileName in self._inputFileNames:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 os.remove(fileName)