| 18 | 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 GetIntersection(object): | 
|  | 63 | 
|  | 64 	def __init__(self, verbosity): | 
|  | 65 		self.verbosity              = verbosity | 
|  | 66 		self.nbQueries              = 0 | 
|  | 67 		self.nbRefs	                = 0 | 
|  | 68 		self.nbWritten              = 0 | 
|  | 69 		self.bins	                = {} | 
|  | 70 | 
|  | 71 	def setReferenceFile(self, fileName, format): | 
|  | 72 		chooser = ParserChooser(self.verbosity) | 
|  | 73 		chooser.findFormat(format) | 
|  | 74 		self.refParser = chooser.getParser(fileName) | 
|  | 75 | 
|  | 76 	def setQueryFile(self, fileName, format): | 
|  | 77 		chooser = ParserChooser(self.verbosity) | 
|  | 78 		chooser.findFormat(format) | 
|  | 79 		self.queryParser = chooser.getParser(fileName) | 
|  | 80 | 
|  | 81 	def setOutputFile(self, fileName): | 
|  | 82 		self.writer = TranscriptWriter(fileName, "gff3", self.verbosity) | 
|  | 83 | 
|  | 84 	def loadRef(self): | 
|  | 85 		progress = UnlimitedProgress(10000, "Reading references", self.verbosity) | 
|  | 86 		for transcript in self.refParser.getIterator(): | 
|  | 87 			if transcript.__class__.__name__ == "Mapping": | 
|  | 88 				transcript = transcript.getTranscript() | 
|  | 89 			chromosome = transcript.getChromosome() | 
|  | 90 			bin		   = getBin(transcript.getStart(), transcript.getEnd()) | 
|  | 91 			if chromosome not in self.bins: | 
|  | 92 				self.bins[chromosome] = {} | 
|  | 93 			if bin not in self.bins[chromosome]: | 
|  | 94 				self.bins[chromosome][bin] = [] | 
|  | 95 			self.bins[chromosome][bin].append(transcript) | 
|  | 96 			self.nbRefs += 1 | 
|  | 97 			progress.inc() | 
|  | 98 		progress.done() | 
|  | 99 | 
|  | 100 	def _compareTranscript(self, queryTranscript): | 
|  | 101 		queryChromosome = queryTranscript.getChromosome() | 
|  | 102 		if queryChromosome not in self.bins: | 
|  | 103 			return None | 
|  | 104 		queryStart = queryTranscript.getStart() | 
|  | 105 		queryEnd   = queryTranscript.getEnd() | 
|  | 106 		bins	   = getOverlappingBins(queryStart, queryEnd) | 
|  | 107 		overlaps   = [] | 
|  | 108 		for binRange in bins: | 
|  | 109 			for bin in range(binRange[0], binRange[1]+1): | 
|  | 110 				if bin not in self.bins[queryChromosome]: | 
|  | 111 					continue | 
|  | 112 				for refTranscript in self.bins[queryChromosome][bin]: | 
|  | 113 					newTranscript = queryTranscript.getIntersection(refTranscript) | 
|  | 114 					if newTranscript != None: | 
|  | 115 						overlaps.append(newTranscript) | 
|  | 116 		if not overlaps: | 
|  | 117 			return None | 
|  | 118 		newTranscript = overlaps[0] | 
|  | 119 		for transcript in overlaps[1:]: | 
|  | 120 			newTranscript.merge(transcript) | 
|  | 121 		return newTranscript | 
|  | 122 | 
|  | 123 	def compare(self): | 
|  | 124 		progress = UnlimitedProgress(10000, "Comparing queries", self.verbosity) | 
|  | 125 		for queryTranscript in self.queryParser.getIterator(): | 
|  | 126 			if queryTranscript.__class__.__name__ == "Mapping": | 
|  | 127 				queryTranscript = queryTranscript.getTranscript() | 
|  | 128 			progress.inc() | 
|  | 129 			self.nbQueries += 1 | 
|  | 130 			newTranscript = self._compareTranscript(queryTranscript) | 
|  | 131 			if newTranscript != None: | 
|  | 132 				self.writer.addTranscript(queryTranscript) | 
|  | 133 				self.nbWritten += 1 | 
|  | 134 		progress.done() | 
|  | 135 		self.writer.close() | 
|  | 136 | 
|  | 137 	def displayResults(self): | 
|  | 138 		print "# queries:  %d" % (self.nbQueries) | 
|  | 139 		print "# refs:     %d" % (self.nbRefs) | 
|  | 140 		print "# written:  %d" % (self.nbWritten) | 
|  | 141 | 
|  | 142 	def run(self): | 
|  | 143 		self.loadRef() | 
|  | 144 		self.compare() | 
|  | 145 		self.displayResults() | 
|  | 146 | 
|  | 147 if __name__ == "__main__": | 
|  | 148 | 
|  | 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]" | 
|  | 150 | 
|  | 151 	parser = OptionParser(description = description) | 
|  | 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]") | 
|  | 153 	parser.add_option("-f", "--format1",        dest="format1",		  action="store",			           type="string", help="format of previous file [compulsory] [format: transcript file format]") | 
|  | 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]") | 
|  | 155 	parser.add_option("-g", "--format2",        dest="format2",		  action="store",			           type="string", help="format of previous file [compulsory] [format: transcript file format]") | 
|  | 156 	parser.add_option("-o", "--output",	        dest="outputFileName", action="store",			           type="string", help="output file [format: output file in GFF3 format]") | 
|  | 157 	parser.add_option("-v", "--verbosity",      dest="verbosity",	   action="store",      default=1,     type="int",	 help="trace level [format: int]") | 
|  | 158 	(options, args) = parser.parse_args() | 
|  | 159 | 
|  | 160 	gi = GetIntersection(options.verbosity) | 
|  | 161 	gi.setQueryFile(options.inputFileName1, options.format1) | 
|  | 162 	gi.setReferenceFile(options.inputFileName2, options.format2) | 
|  | 163 	gi.setOutputFile(options.outputFileName) | 
|  | 164 	gi.run() |