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