annotate SMART/Java/Python/ncList/FindOverlapsWithSeveralIntervals.py @ 22:1e3f2c2657a3

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:26:07 -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-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 os, struct, time
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.ncList.NCList import NCList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.ncList.NCListFilePickle import NCListFilePickle, NCListFileUnpickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.ncList.FileSorter import FileSorter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 from SMART.Java.Python.ncList.FindOverlapsWithOneInterval import FindOverlapsWithOneInterval
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 REFERENCE = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 QUERY = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 TYPETOSTRING = {0: "reference", 1: "query"}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 class FindOverlapsWithSeveralIntervals(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._outputFileName = "outputOverlaps.gff3"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self._iWriter = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self._nbLines = {REFERENCE: 0, QUERY: 0}
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._ncLists = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self._sortedRefFileNames = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self._transQueryFileName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self._cursors = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self._iFowoi = FindOverlapsWithOneInterval(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 for fileName in (self._sortedRefFileNames, self._transQueryFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 if os.path.exists(fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 def close(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self._iFowoi.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 def setRefFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.setFileName(fileName, format, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self._sortedRefFileNames = "%s_ref_sorted.pkl" % (os.path.splitext(fileName)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 def setQueryFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.setFileName(fileName, format, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self._transQueryFileName = "%s_query_trans.pkl" % (os.path.splitext(fileName)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def setFileName(self, fileName, format, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 chooser = ParserChooser(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self._parsers[type] = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 def setOutputFileName(self, outputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 self._iFowoi.setOutputFileName(outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 def _sortRefFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 fs = FileSorter(self._parsers[REFERENCE], self._verbosity-4)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 fs.perChromosome(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 fs.setOutputFileName(self._sortedRefFileNames)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 fs.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self._nbLines[REFERENCE] = fs.getNbElements()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 self._nbRefLinesPerChromosome = fs.getNbElementsPerChromosome()
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
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 def _translateQueryFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 pickler = NCListFilePickle(self._transQueryFileName, self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 progress = UnlimitedProgress(1000, "Translating query data", self._verbosity-4)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 cpt = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 for queryTranscript in self._parsers[QUERY].getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 pickler.addTranscript(queryTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 self._nbLines[QUERY] = cpt
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 self._parsers[QUERY] = NCListFileUnpickle(self._transQueryFileName, self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 def prepareIntermediateFiles(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 self._sortRefFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 self._translateQueryFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 def createNCLists(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 self._ncLists = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self._indices = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self._cursors = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 for chromosome, fileName in self._splittedFileNames.iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 if self._verbosity > 3:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 print " chromosome %s" % (chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 ncList = NCList(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 ncList.createIndex(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 ncList.setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 ncList.setFileName(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 ncList.setNbElements(self._nbRefLinesPerChromosome[chromosome])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 ncList.buildLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 self._ncLists[chromosome] = ncList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 cursor = NCListCursor(None, ncList, 0, self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 self._cursors[chromosome] = cursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 self._indices[chromosome] = ncList.getIndex()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 def compare(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 progress = Progress(self._nbLines[QUERY], "Comparing data", self._verbosity-3)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 for cpt, queryTranscript in enumerate(self._parsers[QUERY].getIterator()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 chromosome = queryTranscript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 if chromosome not in self._ncLists:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self._iFowoi.setNCList(self._ncLists[chromosome], self._indices[chromosome])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 self._iFowoi.setTranscript(queryTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 self._iFowoi.compare()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 self._iFowoi.dumpWriter()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 self._timeSpent = endTime - startTime
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 print "Creating NC-list..."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 self.prepareIntermediateFiles()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 self.createNCLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 print " ...done (%.2gs)" % (endTime - startTime)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 self.compare()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 self.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 if self._verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 print "# queries: %d" % (self._nbLines[QUERY])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 print "# refs: %d" % (self._nbLines[REFERENCE])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 print "# written: %d (%d overlaps)" % (self._iFowoi._nbWritten, self._iFowoi._nbOverlaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 print "time: %.2gs" % (self._timeSpent)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 description = "FindOverlaps With Several Intervals v1.0.0: Finds overlaps with several query intervals. [Category: Data comparison]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 parser.add_option("-i", "--query", dest="inputQueryFileName", 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
171 parser.add_option("-f", "--queryFormat", dest="queryFormat", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 parser.add_option("-j", "--ref", dest="inputRefFileName", action="store", type="string", help="Reference input file [compulsory] [format: file in transcript format given by -g]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 parser.add_option("-g", "--refFormat", dest="refFormat", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="Output file [compulsory] [format: output file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 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
176 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 iFWSI = FindOverlapsWithSeveralIntervals(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 iFWSI.setRefFileName(options.inputRefFileName, options.refFormat)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 iFWSI.setQueryFileName(options.inputQueryFileName, options.queryFormat)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 iFWSI.setOutputFileName(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 iFWSI.run()