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