annotate SMART/Java/Python/ncList/ConvertToNCList.py @ 24:452e051f6562

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:28:24 -0400
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-2012
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 random, os, time, shutil
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.structure.Interval import Interval
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.ncList.NCList import NCList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.ncList.NCListFilePickle import NCListFilePickle, NCListFileUnpickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.ncList.FileSorter import FileSorter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.ncList.NCListMerger import NCListMerger
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 try:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 import cPickle as pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 except:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 import pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 class ConvertToNCList(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 def __init__(self, verbosity = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self._parsers = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self._sortedFileNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self._inputFileName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self._outputFileName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self._index = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self._ncLists = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self._splittedFileNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self._nbElements = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self._nbElementsPerChromosome = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self._randomNumber = random.randint(0, 10000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self._sorted = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self._verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 def setInputFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 self._inputFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 chooser = ParserChooser(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self._parser = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 def setOutputFileName(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self._outputFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 fileNameNoExtension = os.path.splitext(fileName)[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 baseName = "%s_%d" % (fileNameNoExtension, self._randomNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self._directory = "%s_files" % (baseName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 if not os.path.exists(self._directory):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 os.makedirs(self._directory)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self._sortedFileNames = os.path.join(self._directory, baseName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def setIndex(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self._index = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 def setSorted(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self._sorted = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 def sortFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 print "%s file %s..." % ("Rewriting" if self._sorted else "Sorting", self._inputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 fs = FileSorter(self._parser, self._verbosity-4)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 fs.setPresorted(self._sorted)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 fs.perChromosome(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 fs.setOutputFileName(self._sortedFileNames)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 fs.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 self._splittedFileNames = fs.getOutputFileNames()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 self._nbElementsPerChromosome = fs.getNbElementsPerChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 self._nbElements = fs.getNbElements()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 print " ...done (%ds)" % (endTime - startTime)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 def createNCLists(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 self._ncLists = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 print "Creating NC-list for %s..." % (self._inputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 for chromosome, fileName in self._splittedFileNames.iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 if self._verbosity > 3:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 print " chromosome %s" % (chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 ncList = NCList(self._verbosity)
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 ncList.createIndex(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 ncList.setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 ncList.setFileName(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 ncList.setNbElements(self._nbElementsPerChromosome[chromosome])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 ncList.buildLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 self._ncLists[chromosome] = ncList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 print " ...done (%ds)" % (endTime - startTime)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 def writeOutputFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 merger = NCListMerger(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 merger.setFileName(self._outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 merger.addIndex(self._index)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 merger.setNCLists(self._ncLists)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 merger.merge()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 def cleanFiles(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 shutil.rmtree(self._directory)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self.sortFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 self.createNCLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 self.writeOutputFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 self.cleanFiles()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 def getSortedFileNames(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 return self._splittedFileNames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 def getNbElements(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 return self._nbElements
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 def getNbElementsPerChromosome(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 return self._nbElementsPerChromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 def getNCLists(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 return self._ncLists
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 def getTmpDirectory(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 return self._directory
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 description = "Convert To NC-List v1.0.0: Convert a mapping or transcript file into a NC-List. [Category: NC-List]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="Query input file [compulsory] [format: file in transcript format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 parser.add_option("-d", "--index", dest="index", action="store_true", default=False, help="create an index [default: false] [format: boolean]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="Output file [compulsory] [format: output file in NCList format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 parser.add_option("-s", "--sorted", dest="sorted", action="store_true", default=False, help="input file is already sorted [format: boolean] [default: False]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="Trace level [format: int] [default: 1]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 ctncl = ConvertToNCList(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 ctncl.setInputFileName(options.inputFileName, options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 ctncl.setOutputFileName(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 ctncl.setIndex(options.index)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 ctncl.setSorted(options.sorted)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 ctncl.run()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172