| 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 """Compare overlap of a transcript list and list of read, and get some info depending on the coverage""" | 
|  | 32 | 
|  | 33 import os | 
|  | 34 from optparse import OptionParser | 
|  | 35 from commons.core.parsing.SequenceListParser import * | 
|  | 36 from commons.core.writer.Gff3Writer import * | 
|  | 37 from SMART.Java.Python.mySql.MySqlConnection import * | 
|  | 38 from SMART.Java.Python.structure.TranscriptListsComparator import * | 
|  | 39 from SMART.Java.Python.misc.RPlotter import * | 
|  | 40 from SMART.Java.Python.misc.Progress import * | 
|  | 41 | 
|  | 42 | 
|  | 43 if __name__ == "__main__": | 
|  | 44 | 
|  | 45     # parse command line | 
|  | 46     description = "Get Info per Coverage v1.0.1: Get a list of information clustered by the density of the coverage on a genome. [Category: Personnal]" | 
|  | 47 | 
|  | 48     parser = OptionParser(description = description) | 
|  | 49     parser.add_option("-i", "--input1",                     dest="inputFileName1", action="store",                                         type="string", help="input file 1 [compulsory] [format: file in transcript format given by -f]") | 
|  | 50     parser.add_option("-f", "--format1",                    dest="format1",                action="store",                                         type="string", help="format of file 1 [compulsory] [format: transcript file format]") | 
|  | 51     parser.add_option("-j", "--input2",                     dest="inputFileName2", action="store",                                         type="string", help="input file 2 [compulsory] [format: file in transcript format given by -g]") | 
|  | 52     parser.add_option("-g", "--format2",                    dest="format2",                action="store",                                         type="string", help="format of file 2 [compulsory] [format: transcript file format]") | 
|  | 53     parser.add_option("-o", "--output",                     dest="output",                 action="store",            default=None,    type="string", help="output file [compulsory] [format: output file in TXT format]") | 
|  | 54     parser.add_option("-v", "--verbosity",                dest="verbosity",            action="store",            default=1,         type="int",        help="trace level [format: int]") | 
|  | 55     parser.add_option("-l", "--log",                            dest="log",                        action="store",            default=None,    type="string", help="write a log file [format: bool] [default: false]") | 
|  | 56     (options, args) = parser.parse_args() | 
|  | 57 | 
|  | 58     logHandle = None | 
|  | 59     if options.log != None: | 
|  | 60         logHandle = open(options.log, "w") | 
|  | 61 | 
|  | 62     transcriptContainer1 = TranscriptContainer(options.inputFileName1, options.format1, options.verbosity) | 
|  | 63     transcriptContainer2 = TranscriptContainer(options.inputFileName2, options.format2, options.verbosity) | 
|  | 64 | 
|  | 65     transcriptListComparator = TranscriptListsComparator(logHandle, options.verbosity) | 
|  | 66     transcriptListComparator.restrictToStart(transcriptListComparator.REFERENCE, 10) | 
|  | 67     transcriptListComparator.getColinearOnly(True) | 
|  | 68     transcriptListComparator.computeOddsPerTranscript(True) | 
|  | 69     transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.QUERY, transcriptContainer1) | 
|  | 70     transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.REFERENCE, transcriptContainer2) | 
|  | 71     transcriptListComparator.compareTranscriptList() | 
|  | 72     transcriptTables = transcriptListComparator.getOutputTables() | 
|  | 73 | 
|  | 74     sizesWithIntrons                     = {} | 
|  | 75     sizesWithoutIntrons                = {} | 
|  | 76     nbExons                                        = {} | 
|  | 77     averageSizesWithIntrons        = {} | 
|  | 78     averageSizesWithoutIntrons = {} | 
|  | 79     averageNbExons                         = {} | 
|  | 80     sumSizesWithIntrons                = {} | 
|  | 81     sumSizesWithoutIntrons         = {} | 
|  | 82     sumSizesNbExons                        = {} | 
|  | 83     coverages                                    = transcriptListComparator.getOddsPerTranscript() | 
|  | 84 | 
|  | 85     progress = Progress(transcriptContainer2.getNbTranscripts(), "Reading transcript file again", options.verbosity) | 
|  | 86     for transcript in transcriptContainer2.getIterator(): | 
|  | 87         if transcript.name in coverages: | 
|  | 88             if transcript.getSizeWithIntrons() not in averageSizesWithIntrons: | 
|  | 89                 averageSizesWithIntrons[transcript.getSizeWithIntrons()] = coverages[transcript.name] | 
|  | 90             else: | 
|  | 91                 averageSizesWithIntrons[transcript.getSizeWithIntrons()] += coverages[transcript.name] | 
|  | 92             if transcript.getSizeWithIntrons() not in sumSizesWithIntrons: | 
|  | 93                 sumSizesWithIntrons[transcript.getSizeWithIntrons()] = 1 | 
|  | 94             else: | 
|  | 95                 sumSizesWithIntrons[transcript.getSizeWithIntrons()] += 1 | 
|  | 96             if transcript.getSize() not in averageSizesWithoutIntrons: | 
|  | 97                 averageSizesWithoutIntrons[transcript.getSize()] = coverages[transcript.name] | 
|  | 98             else: | 
|  | 99                 averageSizesWithoutIntrons[transcript.getSize()] += coverages[transcript.name] | 
|  | 100             if transcript.getSize() not in sumSizesWithoutIntrons: | 
|  | 101                 sumSizesWithoutIntrons[transcript.getSize()] = 1 | 
|  | 102             else: | 
|  | 103                 sumSizesWithoutIntrons[transcript.getSize()] += 1 | 
|  | 104             if transcript.getNbExons() not in averageNbExons: | 
|  | 105                 averageNbExons[transcript.getNbExons()] = coverages[transcript.name] | 
|  | 106             else: | 
|  | 107                 averageNbExons[transcript.getNbExons()] += coverages[transcript.name] | 
|  | 108             if transcript.getNbExons() not in sumSizesNbExons: | 
|  | 109                 sumSizesNbExons[transcript.getNbExons()] = 1 | 
|  | 110             else: | 
|  | 111                 sumSizesNbExons[transcript.getNbExons()] += 1 | 
|  | 112             sizesWithIntrons[transcript.name]        = (transcript.getSizeWithIntrons(), coverages[transcript.name]) | 
|  | 113             sizesWithoutIntrons[transcript.name] = (transcript.getSize(), coverages[transcript.name]) | 
|  | 114             nbExons[transcript.name]                         = (transcript.getNbExons(), coverages[transcript.name]) | 
|  | 115         progress.inc() | 
|  | 116     progress.done() | 
|  | 117 | 
|  | 118     plotterSizeWithIntrons = RPlotter("%sWithIntrons.png" % (options.output), options.verbosity) | 
|  | 119     plotterSizeWithIntrons.setPoints(True) | 
|  | 120     plotterSizeWithIntrons.setMaximumX(10000) | 
|  | 121     plotterSizeWithIntrons.setMaximumY(1000) | 
|  | 122     plotterSizeWithIntrons.setLog("y") | 
|  | 123     plotterSizeWithIntrons.addLine(sizesWithIntrons) | 
|  | 124     plotterSizeWithIntrons.plot() | 
|  | 125 | 
|  | 126     plotterSizeWithoutIntrons = RPlotter("%sWithoutIntrons.png" % (options.output), options.verbosity) | 
|  | 127     plotterSizeWithoutIntrons.setPoints(True) | 
|  | 128     plotterSizeWithoutIntrons.setMaximumX(10000) | 
|  | 129     plotterSizeWithoutIntrons.setMaximumY(1000) | 
|  | 130     plotterSizeWithoutIntrons.setLog("y") | 
|  | 131     plotterSizeWithoutIntrons.addLine(sizesWithoutIntrons) | 
|  | 132     plotterSizeWithoutIntrons.plot() | 
|  | 133 | 
|  | 134     plotterNbExons = RPlotter("%sNbExons.png" % (options.output), options.verbosity) | 
|  | 135     plotterNbExons.setPoints(True) | 
|  | 136     plotterNbExons.addLine(nbExons) | 
|  | 137     plotterNbExons.plot() | 
|  | 138 | 
|  | 139     for element in averageSizesWithIntrons: | 
|  | 140         averageSizesWithIntrons[element] = int(float(averageSizesWithIntrons[element]) / sumSizesWithIntrons[element]) | 
|  | 141     plotterAverageSizeWithIntrons = RPlotter("%sAverageWithIntrons.png" % (options.output), options.verbosity) | 
|  | 142     plotterAverageSizeWithIntrons.setMaximumX(10000) | 
|  | 143     plotterAverageSizeWithIntrons.setMaximumY(1000) | 
|  | 144     plotterAverageSizeWithIntrons.setLog("y") | 
|  | 145     plotterAverageSizeWithIntrons.addLine(averageSizesWithIntrons) | 
|  | 146     plotterAverageSizeWithIntrons.plot() | 
|  | 147     print "min/avg/med/max sizes with introns: %d/%.2f/%.1f/%d" % Utils.getMinAvgMedMax(averageSizesWithIntrons) | 
|  | 148 | 
|  | 149     for element in averageSizesWithoutIntrons: | 
|  | 150         averageSizesWithoutIntrons[element] = int(float(averageSizesWithoutIntrons[element]) / sumSizesWithoutIntrons[element]) | 
|  | 151     plotterAverageSizeWithoutIntrons = RPlotter("%sAverageWithoutIntrons.png" % (options.output), options.verbosity) | 
|  | 152     plotterAverageSizeWithoutIntrons.setMaximumX(10000) | 
|  | 153     plotterAverageSizeWithoutIntrons.setMaximumY(1000) | 
|  | 154     plotterAverageSizeWithoutIntrons.setLog("y") | 
|  | 155     plotterAverageSizeWithoutIntrons.addLine(averageSizesWithoutIntrons) | 
|  | 156     plotterAverageSizeWithoutIntrons.plot() | 
|  | 157     print "min/avg/med/max sizes without introns: %d/%.2f/%.1f/%d" % Utils.getMinAvgMedMax(averageSizesWithoutIntrons) | 
|  | 158 | 
|  | 159     for element in averageNbExons: | 
|  | 160         averageNbExons[element] = int(float(averageNbExons[element]) / sumSizesNbExons[element]) | 
|  | 161     plotterAverageNbExons = RPlotter("%sAverageNbExons.png" % (options.output), options.verbosity) | 
|  | 162     plotterAverageNbExons.addLine(averageNbExons) | 
|  | 163     plotterAverageNbExons.plot() | 
|  | 164     print "min/avg/med/max # exons: %d/%.2f/%.1f/%d" % Utils.getMinAvgMedMax(averageNbExons) | 
|  | 165 | 
|  | 166     if options.log: | 
|  | 167         logHandle.close() |