Mercurial > repos > yufei-luo > s_mart
comparison SMART/Java/Python/SelectByTag.py @ 6:769e306b7933
Change the repository level.
author | yufei-luo |
---|---|
date | Fri, 18 Jan 2013 04:54:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:ea3082881bf8 | 6:769e306b7933 |
---|---|
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 """Select the transcript such that a tag value is not less than a given threshold""" | |
32 import os | |
33 import sys | |
34 from optparse import OptionParser | |
35 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer | |
36 from commons.core.writer import MySqlTranscriptWriter | |
37 from commons.core.writer.Gff3Writer import Gff3Writer | |
38 from SMART.Java.Python.misc.Progress import Progress | |
39 from SMART.Java.Python.misc.RPlotter import RPlotter | |
40 | |
41 class SelectByTag(object): | |
42 | |
43 def __init__(self, verbosity = 1): | |
44 self.input = None | |
45 self.format = None | |
46 self.tag = None | |
47 self.value = None | |
48 self.min = None | |
49 self.max = None | |
50 self.default = None | |
51 self.output = None | |
52 self.mysql = None | |
53 self.verbosity = verbosity | |
54 | |
55 self.parser = None | |
56 self.writer = None | |
57 self.mysqlWriter = None | |
58 self.nbElements = None | |
59 self.nbWritten = 0 | |
60 | |
61 | |
62 def setParser(self): | |
63 self.parser = TranscriptContainer(self.input, self.format, self.verbosity) | |
64 self.nbElements = self.parser.getNbTranscripts() | |
65 | |
66 | |
67 def setWriter(self): | |
68 self.writer = Gff3Writer(self.output, self.verbosity) | |
69 if self.mysql: | |
70 self.mysqlWriter = MySqlTranscriptWriter(self.output, self.verbosity) | |
71 | |
72 | |
73 def isAccepted(self, transcript): | |
74 value = transcript.getTagValue(self.tag) | |
75 if value == None: | |
76 if self.default != None: | |
77 value = self.default | |
78 else: | |
79 raise Exception("Error! Transcript %s no tag called '%s'" % (transcript, self.tag)) | |
80 if self.value != None: | |
81 if self.value == str(value): | |
82 return True | |
83 return self.value.isdigit() and value == float(self.value) | |
84 value = float(value) | |
85 return (self.min == None or self.min <= value) and (self.max == None or self.max >= value) | |
86 | |
87 | |
88 def readInputFile(self): | |
89 progress = Progress(self.parser.getNbTranscripts(), "Writing transcripts", self.verbosity) | |
90 for transcript in self.parser.getIterator(): | |
91 if self.isAccepted(transcript): | |
92 self.writer.addTranscript(transcript) | |
93 if self.mysql: | |
94 self.mysqlWriter.addTranscript(transcript) | |
95 self.nbWritten += 1 | |
96 progress.inc() | |
97 progress.done() | |
98 | |
99 | |
100 def writeFile(self): | |
101 self.writer.write() | |
102 if self.mysql: | |
103 self.mysqlWriter.write() | |
104 | |
105 | |
106 def run(self): | |
107 self.setParser() | |
108 self.setWriter() | |
109 self.readInputFile() | |
110 self.writeFile() | |
111 if self.verbosity > 0: | |
112 print "%d input" % (self.nbElements) | |
113 if self.nbElements != 0: | |
114 print "%d output (%.2f%%)" % (self.nbWritten, float(self.nbWritten) / self.nbElements * 100) | |
115 | |
116 | |
117 | |
118 if __name__ == "__main__": | |
119 | |
120 # parse command line | |
121 description = "Select by Tag v1.0.2: Keep the genomic coordinates such that a the value of a given tag is between two limits. [Category: Data Selection]" | |
122 | |
123 parser = OptionParser(description = description) | |
124 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in transcript format given by -f]") | |
125 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of the input [compulsory] [format: transcript file format]") | |
126 parser.add_option("-g", "--tag", dest="tag", action="store", default=None, type="string", help="the tag [compulsory] [format: string]") | |
127 parser.add_option("-a", "--value", dest="value", action="store", default=None, type="string", help="the value to be found [format: string]") | |
128 parser.add_option("-m", "--min", dest="min", action="store", default=None, type="float", help="the minimum threshold [format: float]") | |
129 parser.add_option("-M", "--max", dest="max", action="store", default=None, type="float", help="the maximum threshold [format: float]") | |
130 parser.add_option("-d", "--default", dest="default", action="store", default=None, type="float", help="value if tag is not present [format: float]") | |
131 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [format: output file in GFF3 format]") | |
132 parser.add_option("-y", "--mysql", dest="mysql", action="store_true", default=False, help="write output into MySQL tables [format: boolean] [default: False]") | |
133 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]") | |
134 (options, args) = parser.parse_args() | |
135 | |
136 selectByTag = SelectByTag(options.verbosity) | |
137 selectByTag.input = options.inputFileName | |
138 selectByTag.format = options.format | |
139 selectByTag.tag = options.tag | |
140 selectByTag.value = options.value | |
141 selectByTag.min = options.min | |
142 selectByTag.max = options.max | |
143 selectByTag.default = options.default | |
144 selectByTag.output = options.outputFileName | |
145 selectByTag.mysql = options.mysql | |
146 selectByTag.run() | |
147 | |
148 |