36
+ − 1 #! /usr/bin/env python
+ − 2 #
+ − 3 # Copyright INRA-URGI 2009-2011
+ − 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 from optparse import OptionParser
+ − 32 from commons.core.parsing.ParserChooser import ParserChooser
+ − 33 from commons.core.writer.TranscriptWriter import TranscriptWriter
+ − 34 from SMART.Java.Python.structure.Interval import Interval
+ − 35 from SMART.Java.Python.structure.Transcript import Transcript
+ − 36 from SMART.Java.Python.structure.Mapping import Mapping
+ − 37 from SMART.Java.Python.misc.Progress import Progress
+ − 38 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
+ − 39
+ − 40 MINBIN = 3
+ − 41 MAXBIN = 7
+ − 42 REFERENCE = 0
+ − 43 QUERY = 1
+ − 44
+ − 45 def getBin(start, end):
+ − 46 for i in range(MINBIN, MAXBIN + 1):
+ − 47 binLevel = 10 ** i
+ − 48 if int(start / binLevel) == int(end / binLevel):
+ − 49 return int(i * 10 ** (MAXBIN + 1) + int(start / binLevel))
+ − 50 return int((MAXBIN + 1) * 10 ** (MAXBIN + 1))
+ − 51
+ − 52 def getOverlappingBins(start, end):
+ − 53 array = []
+ − 54 bigBin = int((MAXBIN + 1) * 10 ** (MAXBIN + 1))
+ − 55 for i in range(MINBIN, MAXBIN + 1):
+ − 56 binLevel = 10 ** i
+ − 57 array.append((int(i * 10 ** (MAXBIN + 1) + int(start / binLevel)), int(i * 10 ** (MAXBIN + 1) + int(end / binLevel))))
+ − 58 array.append((bigBin, bigBin))
+ − 59 return array
+ − 60
+ − 61
+ − 62 class CompareOverlappingSmallRef(object):
+ − 63
+ − 64 def __init__(self, verbosity):
+ − 65 self.verbosity = verbosity
+ − 66 self.tableNames = {}
+ − 67 self.nbQueries = 0
+ − 68 self.nbRefs = 0
+ − 69 self.nbWritten = 0
+ − 70 self.nbOverlaps = 0
+ − 71 self.invert = False
+ − 72 self.antisense = False
+ − 73 self.collinear = False
+ − 74 self.distance = None
+ − 75 self.pcOverlapQuery = False
+ − 76 self.pcOverlapRef = False
+ − 77 self.bins = {}
+ − 78 self.notOverlapping = False
+ − 79
+ − 80 def setReferenceFile(self, fileName, format):
+ − 81 chooser = ParserChooser(self.verbosity)
+ − 82 chooser.findFormat(format)
+ − 83 self.refParser = chooser.getParser(fileName)
+ − 84
+ − 85 def setQueryFile(self, fileName, format):
+ − 86 chooser = ParserChooser(self.verbosity)
+ − 87 chooser.findFormat(format)
+ − 88 self.queryParser = chooser.getParser(fileName)
+ − 89
+ − 90 def setOutputFile(self, fileName):
+ − 91 self.writer = TranscriptWriter(fileName, "gff3", self.verbosity)
+ − 92
+ − 93 def setDistance(self, distance):
+ − 94 self.distance = distance
+ − 95
+ − 96 def setCollinear(self, boolean):
+ − 97 self.collinear = boolean
+ − 98
+ − 99 def setAntisense(self, boolean):
+ − 100 self.antisense = boolean
+ − 101
+ − 102 def setInvert(self, boolean):
+ − 103 self.invert = boolean
+ − 104
+ − 105 def setMinPercentOverlap(self, pcOverlapQuery, pcOverlapRef):
+ − 106 self.pcOverlapQuery = pcOverlapQuery
+ − 107 self.pcOverlapRef = pcOverlapRef
+ − 108
+ − 109 def includeNotOverlapping(self, boolean):
+ − 110 self.notOverlapping = boolean
+ − 111
+ − 112 def loadRef(self):
+ − 113 progress = UnlimitedProgress(10000, "Reading references", self.verbosity)
+ − 114 for transcript in self.refParser.getIterator():
+ − 115 if transcript.__class__.__name__ == "Mapping":
+ − 116 transcript = transcript.getTranscript()
+ − 117 transcript = self._alterTranscript(transcript, REFERENCE)
+ − 118 chromosome = transcript.getChromosome()
+ − 119 bin = getBin(transcript.getStart(), transcript.getEnd())
+ − 120 if chromosome not in self.bins:
+ − 121 self.bins[chromosome] = {}
+ − 122 if bin not in self.bins[chromosome]:
+ − 123 self.bins[chromosome][bin] = []
+ − 124 self.bins[chromosome][bin].append(transcript)
+ − 125 self.nbRefs += 1
+ − 126 progress.inc()
+ − 127 progress.done()
+ − 128
+ − 129 def _alterTranscript(self, transcript, type):
+ − 130 if type == REFERENCE:
+ − 131 if self.distance != None:
+ − 132 transcript.extendExons(self.distance)
+ − 133 return transcript
+ − 134
+ − 135 def _compareTwoTranscripts(self, queryTranscript, refTranscript):
+ − 136 if not queryTranscript.overlapWithExon(refTranscript):
+ − 137 return False
+ − 138 if self.collinear and queryTranscript.getDirection() != refTranscript.getDirection():
+ − 139 return False
+ − 140 if self.antisense and queryTranscript.getDirection() == refTranscript.getDirection():
+ − 141 return False
+ − 142 querySize = queryTranscript.getSize()
+ − 143 if self.pcOverlapQuery and not queryTranscript.overlapWithExon(refTranscript, int(querySize * self.pcOverlapQuery / 100.0)):
+ − 144 return False
+ − 145 refSize = refTranscript.getSize()
+ − 146 if self.pcOverlapRef and not queryTranscript.overlapWithExon(refTranscript, int(refSize * self.pcOverlapRef / 100.0)):
+ − 147 return False
+ − 148 return True
+ − 149
+ − 150 def _compareTranscript(self, queryTranscript):
+ − 151 queryChromosome = queryTranscript.getChromosome()
+ − 152 if queryChromosome not in self.bins:
+ − 153 return []
+ − 154 queryStart = queryTranscript.getStart()
+ − 155 queryEnd = queryTranscript.getEnd()
+ − 156 bins = getOverlappingBins(queryStart, queryEnd)
+ − 157 overlaps = {}
+ − 158 for binRange in bins:
+ − 159 for bin in range(binRange[0], binRange[1]+1):
+ − 160 if bin not in self.bins[queryChromosome]:
+ − 161 continue
+ − 162 for refTranscript in self.bins[queryChromosome][bin]:
+ − 163 if self._compareTwoTranscripts(queryTranscript, refTranscript):
+ − 164 nbElements = int(float(refTranscript.getTagValue("nbElements"))) if "nbElements" in refTranscript.getTagNames() else 1
+ − 165 overlaps[refTranscript.getName()] = int(float(refTranscript.getTagValue("nbElements"))) if "nbElements" in refTranscript.getTagNames() else 1
+ − 166 self.nbOverlaps += nbElements
+ − 167 return overlaps
+ − 168
+ − 169 def _updateTranscript(self, queryTranscript, overlaps):
+ − 170 if overlaps:
46
+ − 171 queryTranscript.setTagValue("nbOverlaps", sum(overlaps.values()))
36
+ − 172 queryTranscript.setTagValue("overlapsWith", "--".join(overlaps.keys())[:100])
46
+ − 173 else:
+ − 174 queryTranscript.setTagValue("nbOverlaps", "0")
36
+ − 175 return queryTranscript
+ − 176
+ − 177 def compare(self):
+ − 178 progress = UnlimitedProgress(10000, "Comparing queries", self.verbosity)
+ − 179 for queryTranscript in self.queryParser.getIterator():
+ − 180 if queryTranscript.__class__.__name__ == "Mapping":
+ − 181 queryTranscript = queryTranscript.getTranscript()
+ − 182 progress.inc()
+ − 183 self.nbQueries += 1
+ − 184 overlaps = self._compareTranscript(queryTranscript)
+ − 185 if self.notOverlapping or (overlaps and not self.invert) or (not overlaps and self.invert):
+ − 186 if not self.invert:
+ − 187 queryTranscript = self._updateTranscript(queryTranscript, overlaps)
+ − 188 self.writer.addTranscript(queryTranscript)
+ − 189 self.nbWritten += 1
+ − 190 progress.done()
+ − 191 self.writer.close()
+ − 192
+ − 193 def displayResults(self):
+ − 194 if self.verbosity > 0:
+ − 195 print "# queries: %d" % (self.nbQueries)
+ − 196 print "# refs: %d" % (self.nbRefs)
+ − 197 print "# written: %d (%d overlaps)" % (self.nbWritten, self.nbOverlaps)
+ − 198
+ − 199 def run(self):
+ − 200 self.loadRef()
+ − 201 self.compare()
+ − 202 self.displayResults()
+ − 203
+ − 204 if __name__ == "__main__":
+ − 205
+ − 206 description = "Compare Overlapping Small Reference v1.0.1: Provide the queries that overlap with a reference, when the reference is small. [Category: Data Comparison]"
+ − 207
+ − 208 parser = OptionParser(description = description)
+ − 209 parser.add_option("-i", "--input1", dest="inputFileName1", action="store", type="string", help="query input file [compulsory] [format: file in transcript format given by -f]")
+ − 210 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
+ − 211 parser.add_option("-j", "--input2", dest="inputFileName2", action="store", type="string", help="reference input file [compulsory] [format: file in transcript format given by -g]")
+ − 212 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
+ − 213 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [format: output file in GFF3 format]")
46
+ − 214 parser.add_option("-O", "--notOverlapping", dest="notOverlapping", action="store_true", default=False, help="also output not overlapping data [format: bool] [default: false]")
+ − 215 parser.add_option("-d", "--distance", dest="distance", action="store", default=0, type="int", help="accept some distance between query and reference [format: int]")
+ − 216 parser.add_option("-c", "--collinear", dest="collinear", action="store_true", default=False, help="provide collinear features [format: bool] [default: false]")
+ − 217 parser.add_option("-a", "--antisense", dest="antisense", action="store_true", default=False, help="provide antisense features [format: bool] [default: false]")
+ − 218 parser.add_option("-p", "--pcOverlapQuery", dest="pcOverlapQuery", action="store", default=False, type="int", help="min. % overlap of the query [format: bool] [default: false]")
+ − 219 parser.add_option("-P", "--pcOverlapRef", dest="pcOverlapRef", action="store", default=False, type="int", help="min. % overlap of the reference [format: bool] [default: false]")
+ − 220 parser.add_option("-x", "--exclude", dest="exclude", action="store_true", default=False, help="invert the match [format: bool] [default: false]")
+ − 221 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
36
+ − 222 (options, args) = parser.parse_args()
+ − 223
+ − 224 cosr = CompareOverlappingSmallRef(options.verbosity)
+ − 225 cosr.setQueryFile(options.inputFileName1, options.format1)
+ − 226 cosr.setReferenceFile(options.inputFileName2, options.format2)
+ − 227 cosr.setOutputFile(options.outputFileName)
+ − 228 cosr.includeNotOverlapping(options.notOverlapping)
+ − 229 cosr.setDistance(options.distance)
+ − 230 cosr.setAntisense(options.antisense)
+ − 231 cosr.setInvert(options.exclude)
+ − 232 cosr.setMinPercentOverlap(options.pcOverlapQuery, options.pcOverlapRef)
+ − 233 cosr.run()
+ − 234