annotate SMART/Java/Python/GetIntersection.py @ 56:97aa2e42bfdf

Uploaded
author m-zytnicki
date Wed, 05 Feb 2014 11:51:11 -0500
parents 94ab73e8a190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 #! /usr/bin/env python
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2011
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 # "http://www.cecill.info".
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 # liability.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 # same conditions as regards security.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 from optparse import OptionParser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 from commons.core.parsing.ParserChooser import ParserChooser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 from commons.core.writer.TranscriptWriter import TranscriptWriter
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 from SMART.Java.Python.structure.Interval import Interval
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 from SMART.Java.Python.structure.Mapping import Mapping
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 from SMART.Java.Python.misc.Progress import Progress
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 MINBIN = 3
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 MAXBIN = 7
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 REFERENCE = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 QUERY = 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 def getBin(start, end):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 for i in range(MINBIN, MAXBIN + 1):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 binLevel = 10 ** i
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 if int(start / binLevel) == int(end / binLevel):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 return int(i * 10 ** (MAXBIN + 1) + int(start / binLevel))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 return int((MAXBIN + 1) * 10 ** (MAXBIN + 1))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 def getOverlappingBins(start, end):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 array = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 bigBin = int((MAXBIN + 1) * 10 ** (MAXBIN + 1))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 for i in range(MINBIN, MAXBIN + 1):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 binLevel = 10 ** i
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 array.append((int(i * 10 ** (MAXBIN + 1) + int(start / binLevel)), int(i * 10 ** (MAXBIN + 1) + int(end / binLevel))))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 array.append((bigBin, bigBin))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 return array
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 class GetIntersection(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 def __init__(self, verbosity):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 self.verbosity = verbosity
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 self.nbQueries = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 self.nbRefs = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 self.nbWritten = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 self.bins = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 def setReferenceFile(self, fileName, format):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 chooser = ParserChooser(self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 chooser.findFormat(format)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 self.refParser = chooser.getParser(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 def setQueryFile(self, fileName, format):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 chooser = ParserChooser(self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 chooser.findFormat(format)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 self.queryParser = chooser.getParser(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 def setOutputFile(self, fileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 self.writer = TranscriptWriter(fileName, "gff3", self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 def loadRef(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 progress = UnlimitedProgress(10000, "Reading references", self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 for transcript in self.refParser.getIterator():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 if transcript.__class__.__name__ == "Mapping":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 transcript = transcript.getTranscript()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 chromosome = transcript.getChromosome()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 bin = getBin(transcript.getStart(), transcript.getEnd())
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 if chromosome not in self.bins:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 self.bins[chromosome] = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 if bin not in self.bins[chromosome]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 self.bins[chromosome][bin] = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 self.bins[chromosome][bin].append(transcript)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 self.nbRefs += 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 progress.inc()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 progress.done()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 def _compareTranscript(self, queryTranscript):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 queryChromosome = queryTranscript.getChromosome()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 if queryChromosome not in self.bins:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 return None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 queryStart = queryTranscript.getStart()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 queryEnd = queryTranscript.getEnd()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 bins = getOverlappingBins(queryStart, queryEnd)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 overlaps = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 for binRange in bins:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 for bin in range(binRange[0], binRange[1]+1):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 if bin not in self.bins[queryChromosome]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 continue
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 for refTranscript in self.bins[queryChromosome][bin]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 newTranscript = queryTranscript.getIntersection(refTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 if newTranscript != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 overlaps.append(newTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116 if not overlaps:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 return None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 newTranscript = overlaps[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 for transcript in overlaps[1:]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 newTranscript.merge(transcript)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 return newTranscript
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 def compare(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 progress = UnlimitedProgress(10000, "Comparing queries", self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 for queryTranscript in self.queryParser.getIterator():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 if queryTranscript.__class__.__name__ == "Mapping":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 queryTranscript = queryTranscript.getTranscript()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 progress.inc()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 self.nbQueries += 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 newTranscript = self._compareTranscript(queryTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131 if newTranscript != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 self.writer.addTranscript(queryTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133 self.nbWritten += 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134 progress.done()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 self.writer.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 def displayResults(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 print "# queries: %d" % (self.nbQueries)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 print "# refs: %d" % (self.nbRefs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140 print "# written: %d" % (self.nbWritten)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143 self.loadRef()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 self.compare()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 self.displayResults()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 if __name__ == "__main__":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149 description = "Get Intersection v1.0.0: Shrink the first data set so that all bases covered by the first data set is also covered by the second data set. [Category: Data Comparison]"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 parser = OptionParser(description = description)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152 parser.add_option("-i", "--input1", dest="inputFileName1", action="store", type="string", help="query input file [compulsory] [format: file in transcript format given by -f]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154 parser.add_option("-j", "--input2", dest="inputFileName2", action="store", type="string", help="reference input file [compulsory] [format: file in transcript format given by -g]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [format: output file in GFF3 format]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 (options, args) = parser.parse_args()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160 gi = GetIntersection(options.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 gi.setQueryFile(options.inputFileName1, options.format1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162 gi.setReferenceFile(options.inputFileName2, options.format2)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163 gi.setOutputFile(options.outputFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164 gi.run()