annotate SMART/Java/Python/ComputeCoverage.py @ 44:5f796c5c579f

Uploaded
author m-zytnicki
date Wed, 18 Sep 2013 08:32:38 -0400
parents 44d5973c188c
children 169d364ddd91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #! /usr/bin/env python
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2011
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 import os, random
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32 from optparse import OptionParser, OptionGroup
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34 from SMART.Java.Python.misc.Progress import Progress
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35 from commons.core.writer.Gff3Writer import Gff3Writer
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38 class CoverageComputer(object):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40 def __init__(self, verbosity = 0):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41 self.verbosity = verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42 self.queryReader = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 self.referenceReader = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44 self.outputWriter = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45 self.introns = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46 self.nbNucleotides = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 self.nbCovered = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 def setInputQueryFile(self, fileName, format):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50 self.queryReader = TranscriptContainer(fileName, format, self.verbosity-1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 def setInputReferenceFile(self, fileName, format):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 self.referenceReader = TranscriptContainer(fileName, format, self.verbosity-1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55 def includeIntrons(self, boolean):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56 self.introns = boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58 def setOutputFileName(self, fileName, title="S-MART", feature="transcript", featurePart="exon"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59 self.outputWriter = Gff3Writer(fileName, self.verbosity-1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 self.outputWriter.setTitle(title)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61 self.outputWriter.setFeature(feature)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62 self.outputWriter.setFeaturePart(featurePart)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 def readReference(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65 self.coveredRegions = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66 progress = Progress(self.referenceReader.getNbTranscripts(), "Reading reference file", self.verbosity-1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67 for transcript in self.referenceReader.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68 chromosome = transcript.getChromosome()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69 if chromosome not in self.coveredRegions:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70 self.coveredRegions[chromosome] = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71 if self.introns:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72 transcript.removeExons()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 for exon in transcript.getExons():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74 for position in range(exon.getStart(), exon.getEnd()+1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75 self.coveredRegions[chromosome][position] = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79 def readQuery(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80 progress = Progress(self.queryReader.getNbTranscripts(), "Reading query file", self.verbosity-1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81 for transcript in self.queryReader.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83 chromosome = transcript.getChromosome()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 if chromosome not in self.coveredRegions:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85 continue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 if self.introns:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 transcript.removeExons()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88 for exon in transcript.getExons():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 for position in range(exon.getStart(), exon.getEnd()+1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 self.nbNucleotides += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 self.nbCovered += self.coveredRegions[chromosome].get(position, 0)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94 def write(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95 progress = Progress(self.queryReader.getNbTranscripts(), "Writing output file", self.verbosity-1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 for transcript in self.queryReader.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 chromosome = transcript.getChromosome()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98 if self.introns:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 transcript.removeExons()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 size = transcript.getSize()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 coverage = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 for exon in transcript.getExons():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103 for position in range(exon.getStart(), exon.getEnd()+1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 coverage += self.coveredRegions[chromosome].get(position, 0)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105 transcript.setTagValue("coverage", 0 if size == 0 else float(coverage) / size * 100)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106 self.outputWriter.addTranscript(transcript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110 def sumUp(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 print "%d nucleotides in query, %d (%.f%%) covered" % (self.nbNucleotides, self.nbCovered, 0 if self.nbNucleotides == 0 else float(self.nbCovered) / self.nbNucleotides * 100)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 def run(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 self.readReference()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 self.readQuery()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 if self.outputWriter != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117 self.write()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118 self.sumUp()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 if __name__ == "__main__":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 # parse command line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 description = "Compute Coverage v1.0.1: Compute the coverage of a set with respect to another set. [Category: Personal]"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 parser = OptionParser(description = description)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 parser.add_option("-i", "--input1", dest="inputFileName1", action="store", type="string", help="input query file [compulsory] [format: file in transcript format given by -f]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of the first file [compulsory] [format: transcript file format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 parser.add_option("-j", "--input2", dest="inputFileName2", action="store", type="string", help="input reference file [compulsory] [format: file in transcript format given by -f]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of the second file [compulsory] [format: transcript file format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 parser.add_option("-t", "--introns", dest="introns", action="store_true", default=False, help="also include introns [format: boolean] [default: false]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 parser.add_option("-o", "--output", dest="outputFileName", action="store", default=None, type="string", help="output file [format: output file in GFF3 format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [default: 1] [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 (options, args) = parser.parse_args()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 computer = CoverageComputer(options.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 computer.setInputQueryFile(options.inputFileName1, options.format1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 computer.setInputReferenceFile(options.inputFileName2, options.format2)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 computer.includeIntrons(options.introns)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 computer.setOutputFileName(options.outputFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 computer.run()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142