| 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 """Restrict a transcript list with some parameters (regions)""" | 
|  | 32 | 
|  | 33 from optparse import OptionParser | 
|  | 34 from SMART.Java.Python.structure.Transcript import Transcript | 
|  | 35 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer | 
|  | 36 from SMART.Java.Python.structure.TranscriptListsComparator import TranscriptListsComparator | 
|  | 37 from commons.core.writer.Gff3Writer import Gff3Writer | 
|  | 38 from commons.core.parsing.FastaParser import FastaParser | 
|  | 39 from SMART.Java.Python.misc.Progress import Progress | 
|  | 40 | 
|  | 41 class DifferenceGetter(object): | 
|  | 42 | 
|  | 43     def __init__(self, verbosity): | 
|  | 44         self.verbosity        = verbosity | 
|  | 45         self.annotationParser = None | 
|  | 46         self.referenceParser  = None | 
|  | 47         self.sequenceParser   = None | 
|  | 48         self.transcriptCount  = 1 | 
|  | 49         self.split            = False | 
|  | 50 | 
|  | 51     def createTranscript(self, chromosome, start, end): | 
|  | 52         transcript = Transcript() | 
|  | 53         transcript.setChromosome(chromosome) | 
|  | 54         transcript.setDirection("+") | 
|  | 55         transcript.setStart(start) | 
|  | 56         transcript.setEnd(end) | 
|  | 57         transcript.setName("region_%d" % self.transcriptCount) | 
|  | 58         transcript.setTagValue("ID", "region_%d" % self.transcriptCount) | 
|  | 59         self.transcriptCount += 1 | 
|  | 60         return transcript | 
|  | 61 | 
|  | 62     def setSplit(self, split): | 
|  | 63         self.split = split | 
|  | 64 | 
|  | 65     def setAnnotationFile(self, fileName, format): | 
|  | 66         if fileName != None: | 
|  | 67             self.annotationParser = TranscriptContainer(fileName, format, self.verbosity) | 
|  | 68 | 
|  | 69     def setReferenceFile(self, fileName, format): | 
|  | 70         if fileName != None: | 
|  | 71             self.referenceParser = TranscriptContainer(fileName, format, self.verbosity) | 
|  | 72 | 
|  | 73     def setSequenceFile(self, fileName): | 
|  | 74         if fileName != None: | 
|  | 75             self.sequenceParser = FastaParser(fileName, self.verbosity) | 
|  | 76 | 
|  | 77     def setOutputFile(self, fileName): | 
|  | 78         self.writer = Gff3Writer(fileName, self.verbosity) | 
|  | 79 | 
|  | 80     def initialize(self): | 
|  | 81         self.presence = {} | 
|  | 82         for chromosome in self.sequenceParser.getRegions(): | 
|  | 83             self.presence[chromosome] = [[1, self.sequenceParser.getSizeOfRegion(chromosome)]] | 
|  | 84 | 
|  | 85     def readTranscripts(self): | 
|  | 86         nbTranscripts = self.annotationParser.getNbTranscripts() | 
|  | 87         progress      = Progress(nbTranscripts, "Parsing annotation file" , self.verbosity) | 
|  | 88         for transcript in self.annotationParser.getIterator(): | 
|  | 89             chromosome   = transcript.getChromosome() | 
|  | 90             toBeDeleted  = [] | 
|  | 91             toBeAppended = [] | 
|  | 92             for i, element in enumerate(self.presence[chromosome]): | 
|  | 93                 start, end = element | 
|  | 94                 if start <= transcript.getEnd() and transcript.getStart() <= end: | 
|  | 95                     toBeDeleted.append(i) | 
|  | 96                     if start < transcript.getStart(): | 
|  | 97                         toBeAppended.append([start, transcript.getStart() - 1]) | 
|  | 98                     if end > transcript.getEnd(): | 
|  | 99                         toBeAppended.append([transcript.getEnd() + 1, end]) | 
|  | 100             for i in reversed(toBeDeleted): | 
|  | 101                 del self.presence[chromosome][i] | 
|  | 102             self.presence[chromosome].extend(toBeAppended) | 
|  | 103             progress.inc() | 
|  | 104         progress.done() | 
|  | 105 | 
|  | 106     def writeOutput(self): | 
|  | 107         for chromosome in self.presence: | 
|  | 108             for element in self.presence[chromosome]: | 
|  | 109                 start, end = element | 
|  | 110                 self.writer.addTranscript(self.createTranscript(chromosome, start, end)) | 
|  | 111         self.writer.write() | 
|  | 112 | 
|  | 113     def compareToSequence(self): | 
|  | 114         self.initialize() | 
|  | 115         self.readTranscripts() | 
|  | 116         self.writeOutput() | 
|  | 117 | 
|  | 118     def compareToAnnotation(self): | 
|  | 119         transcriptListComparator = TranscriptListsComparator(None, self.verbosity) | 
|  | 120         transcriptListComparator.setSplitDifference(self.split) | 
|  | 121         transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.QUERY, self.annotationParser) | 
|  | 122         transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.REFERENCE, self.referenceParser) | 
|  | 123         transcriptListComparator.setOutputWriter(self.writer) | 
|  | 124         transcriptListComparator.getDifferenceTranscriptList() | 
|  | 125 | 
|  | 126     def run(self): | 
|  | 127         if self.referenceParser != None: | 
|  | 128             self.compareToAnnotation() | 
|  | 129         else: | 
|  | 130             self.compareToSequence() | 
|  | 131 | 
|  | 132 | 
|  | 133 if __name__ == "__main__": | 
|  | 134 | 
|  | 135     # parse command line | 
|  | 136     description = "Get Difference v1.0.1: Get all the regions of the genome, except the one given or get all the elements from the first set which does not ovelap with the second set (at the nucleotide level). [Category: Data Comparison]" | 
|  | 137 | 
|  | 138     parser = OptionParser(description = description) | 
|  | 139     parser.add_option("-i", "--input1",    dest="inputFileName1",   action="store",                     type="string", help="input file [compulsory] [format: file in transcript format given by -f]") | 
|  | 140     parser.add_option("-f", "--format1",   dest="format1",          action="store",                     type="string", help="format [compulsory] [format: transcript file format]") | 
|  | 141     parser.add_option("-j", "--input2",    dest="inputFileName2",   action="store",      default=None,  type="string", help="reference file [format: file in transcript format given by -g]") | 
|  | 142     parser.add_option("-g", "--format2",   dest="format2",          action="store",      default=None,  type="string", help="format of the reference file [format: transcript file format]") | 
|  | 143     parser.add_option("-s", "--sequence",  dest="sequenceFileName", action="store",      default=None,  type="string", help="sequence file [format: file in FASTA format]") | 
|  | 144     parser.add_option("-p", "--split",     dest="split",            action="store_true", default=False,                help="when comparing to a set of genomic coordinates, do not join [format: boolean] [default: False") | 
|  | 145     parser.add_option("-o", "--output",    dest="outputFileName",   action="store",                     type="string", help="output file [format: output file in GFF3 format]") | 
|  | 146     parser.add_option("-v", "--verbosity", dest="verbosity",        action="store",      default=1,     type="int",    help="trace level [format: int]") | 
|  | 147     (options, args) = parser.parse_args() | 
|  | 148 | 
|  | 149     getter = DifferenceGetter(options.verbosity) | 
|  | 150     getter.setSplit(options.split) | 
|  | 151     getter.setAnnotationFile(options.inputFileName1, options.format1) | 
|  | 152     getter.setSequenceFile(options.sequenceFileName) | 
|  | 153     getter.setReferenceFile(options.inputFileName2, options.format2) | 
|  | 154     getter.setOutputFile(options.outputFileName) | 
|  | 155     getter.run() |