annotate SMART/Java/Python/getInfoPerCoverage.py @ 55:2ac71607aa60

Uploaded
author m-zytnicki
date Fri, 10 Jan 2014 08:59:23 -0500
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #! /usr/bin/env python
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 """Compare overlap of a transcript list and list of read, and get some info depending on the coverage"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.parsing.SequenceListParser import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.writer.Gff3Writer import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.mySql.MySqlConnection import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.structure.TranscriptListsComparator import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.misc.RPlotter import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.misc.Progress import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of file 1 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of file 2 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 parser.add_option("-o", "--output", dest="output", action="store", default=None, type="string", help="output file [compulsory] [format: output file in TXT format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 parser.add_option("-l", "--log", dest="log", action="store", default=None, type="string", help="write a log file [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 logHandle = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 if options.log != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 logHandle = open(options.log, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 transcriptContainer1 = TranscriptContainer(options.inputFileName1, options.format1, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 transcriptContainer2 = TranscriptContainer(options.inputFileName2, options.format2, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 transcriptListComparator = TranscriptListsComparator(logHandle, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 transcriptListComparator.restrictToStart(transcriptListComparator.REFERENCE, 10)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 transcriptListComparator.getColinearOnly(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 transcriptListComparator.computeOddsPerTranscript(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.QUERY, transcriptContainer1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.REFERENCE, transcriptContainer2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 transcriptListComparator.compareTranscriptList()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 transcriptTables = transcriptListComparator.getOutputTables()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 sizesWithIntrons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 sizesWithoutIntrons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 nbExons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 averageSizesWithIntrons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 averageSizesWithoutIntrons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 averageNbExons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 sumSizesWithIntrons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 sumSizesWithoutIntrons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 sumSizesNbExons = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 coverages = transcriptListComparator.getOddsPerTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 progress = Progress(transcriptContainer2.getNbTranscripts(), "Reading transcript file again", options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 for transcript in transcriptContainer2.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 if transcript.name in coverages:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 if transcript.getSizeWithIntrons() not in averageSizesWithIntrons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 averageSizesWithIntrons[transcript.getSizeWithIntrons()] = coverages[transcript.name]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 averageSizesWithIntrons[transcript.getSizeWithIntrons()] += coverages[transcript.name]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 if transcript.getSizeWithIntrons() not in sumSizesWithIntrons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 sumSizesWithIntrons[transcript.getSizeWithIntrons()] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 sumSizesWithIntrons[transcript.getSizeWithIntrons()] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 if transcript.getSize() not in averageSizesWithoutIntrons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 averageSizesWithoutIntrons[transcript.getSize()] = coverages[transcript.name]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 averageSizesWithoutIntrons[transcript.getSize()] += coverages[transcript.name]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 if transcript.getSize() not in sumSizesWithoutIntrons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 sumSizesWithoutIntrons[transcript.getSize()] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 sumSizesWithoutIntrons[transcript.getSize()] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 if transcript.getNbExons() not in averageNbExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 averageNbExons[transcript.getNbExons()] = coverages[transcript.name]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 averageNbExons[transcript.getNbExons()] += coverages[transcript.name]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 if transcript.getNbExons() not in sumSizesNbExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 sumSizesNbExons[transcript.getNbExons()] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 sumSizesNbExons[transcript.getNbExons()] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 sizesWithIntrons[transcript.name] = (transcript.getSizeWithIntrons(), coverages[transcript.name])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 sizesWithoutIntrons[transcript.name] = (transcript.getSize(), coverages[transcript.name])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 nbExons[transcript.name] = (transcript.getNbExons(), coverages[transcript.name])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 plotterSizeWithIntrons = RPlotter("%sWithIntrons.png" % (options.output), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 plotterSizeWithIntrons.setPoints(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 plotterSizeWithIntrons.setMaximumX(10000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 plotterSizeWithIntrons.setMaximumY(1000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 plotterSizeWithIntrons.setLog("y")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 plotterSizeWithIntrons.addLine(sizesWithIntrons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 plotterSizeWithIntrons.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 plotterSizeWithoutIntrons = RPlotter("%sWithoutIntrons.png" % (options.output), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 plotterSizeWithoutIntrons.setPoints(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 plotterSizeWithoutIntrons.setMaximumX(10000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 plotterSizeWithoutIntrons.setMaximumY(1000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 plotterSizeWithoutIntrons.setLog("y")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 plotterSizeWithoutIntrons.addLine(sizesWithoutIntrons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 plotterSizeWithoutIntrons.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 plotterNbExons = RPlotter("%sNbExons.png" % (options.output), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 plotterNbExons.setPoints(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 plotterNbExons.addLine(nbExons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 plotterNbExons.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 for element in averageSizesWithIntrons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 averageSizesWithIntrons[element] = int(float(averageSizesWithIntrons[element]) / sumSizesWithIntrons[element])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 plotterAverageSizeWithIntrons = RPlotter("%sAverageWithIntrons.png" % (options.output), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 plotterAverageSizeWithIntrons.setMaximumX(10000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 plotterAverageSizeWithIntrons.setMaximumY(1000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 plotterAverageSizeWithIntrons.setLog("y")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 plotterAverageSizeWithIntrons.addLine(averageSizesWithIntrons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 plotterAverageSizeWithIntrons.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 print "min/avg/med/max sizes with introns: %d/%.2f/%.1f/%d" % Utils.getMinAvgMedMax(averageSizesWithIntrons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 for element in averageSizesWithoutIntrons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 averageSizesWithoutIntrons[element] = int(float(averageSizesWithoutIntrons[element]) / sumSizesWithoutIntrons[element])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 plotterAverageSizeWithoutIntrons = RPlotter("%sAverageWithoutIntrons.png" % (options.output), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 plotterAverageSizeWithoutIntrons.setMaximumX(10000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 plotterAverageSizeWithoutIntrons.setMaximumY(1000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 plotterAverageSizeWithoutIntrons.setLog("y")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 plotterAverageSizeWithoutIntrons.addLine(averageSizesWithoutIntrons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 plotterAverageSizeWithoutIntrons.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 print "min/avg/med/max sizes without introns: %d/%.2f/%.1f/%d" % Utils.getMinAvgMedMax(averageSizesWithoutIntrons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 for element in averageNbExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 averageNbExons[element] = int(float(averageNbExons[element]) / sumSizesNbExons[element])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 plotterAverageNbExons = RPlotter("%sAverageNbExons.png" % (options.output), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 plotterAverageNbExons.addLine(averageNbExons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 plotterAverageNbExons.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 print "min/avg/med/max # exons: %d/%.2f/%.1f/%d" % Utils.getMinAvgMedMax(averageNbExons)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 if options.log:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 logHandle.close()