annotate SMART/Java/Python/ComputeCoverage.py @ 20:c265f13066d8

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:24:43 -0400
parents 94ab73e8a190
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-2011
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 import os, random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from optparse import OptionParser, OptionGroup
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 class CoverageComputer(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 def __init__(self, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 self.queryReader = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self.referenceReader = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 self.outputWriter = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self.introns = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 self.nbNucleotides = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 self.nbCovered = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 def setInputQueryFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.queryReader = TranscriptContainer(fileName, format, self.verbosity-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 def setInputReferenceFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.referenceReader = TranscriptContainer(fileName, format, self.verbosity-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 def includeIntrons(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.introns = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 def setOutputFileName(self, fileName, title="S-MART", feature="transcript", featurePart="exon"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self.outputWriter = Gff3Writer(fileName, self.verbosity-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self.outputWriter.setTitle(title)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.outputWriter.setFeature(feature)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self.outputWriter.setFeaturePart(featurePart)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 def readReference(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self.coveredRegions = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 progress = Progress(self.referenceReader.getNbTranscripts(), "Reading reference file", self.verbosity-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 for transcript in self.referenceReader.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 if chromosome not in self.coveredRegions:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.coveredRegions[chromosome] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 if self.introns:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 transcript.removeExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 for exon in transcript.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 for position in range(exon.getStart(), exon.getEnd()+1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self.coveredRegions[chromosome][position] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def readQuery(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 progress = Progress(self.queryReader.getNbTranscripts(), "Reading query file", self.verbosity-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 for transcript in self.queryReader.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 if chromosome not in self.coveredRegions:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 if self.introns:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 transcript.removeExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 for exon in transcript.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 for position in range(exon.getStart(), exon.getEnd()+1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self.nbNucleotides += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 self.nbCovered += self.coveredRegions[chromosome].get(position, 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 def write(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 progress = Progress(self.queryReader.getNbTranscripts(), "Writing output file", self.verbosity-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 for transcript in self.queryReader.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 if self.introns:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 transcript.removeExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 size = transcript.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 coverage = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 for exon in transcript.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 for position in range(exon.getStart(), exon.getEnd()+1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 coverage += self.coveredRegions[chromosome].get(position, 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 transcript.setTagValue("coverage", 0 if size == 0 else float(coverage) / size * 100)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 self.outputWriter.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 def sumUp(self):
769e306b7933 Change the repository level.
yufei-luo
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)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 self.readReference()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self.readQuery()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 if self.outputWriter != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 self.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 self.sumUp()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 description = "Compute Coverage v1.0.1: Compute the coverage of a set with respect to another set. [Category: Personal]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 parser.add_option("-t", "--introns", dest="introns", action="store_true", default=False, help="also include introns [format: boolean] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
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]")
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
133 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [default: 1] [format: int]")
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 computer = CoverageComputer(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 computer.setInputQueryFile(options.inputFileName1, options.format1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 computer.setInputReferenceFile(options.inputFileName2, options.format2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 computer.includeIntrons(options.introns)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 computer.setOutputFileName(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 computer.run()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142