annotate SMART/Java/Python/restrictSequenceList.py @ 58:5f5c9b74c2dd

Uploaded
author m-zytnicki
date Fri, 07 Feb 2014 11:53:36 -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 """Restrict a sequence list with some names"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.writer.WriterChooser import WriterChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.misc import Utils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 class RestrictSequenceList(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 def __init__(self, verbosity):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self.exclude = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def setInputFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 chooser = ParserChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self.parser = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 def setExclusion(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.exclude = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 def setOutputFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 chooser = WriterChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.writer = chooser.getWriter(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 def setNamesFileName(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self.namesFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 def _readNames(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self.names = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 handle = open(self.namesFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 for name in handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self.names.append(name.strip())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 handle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 def _write(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 nbElements = self.parser.getNbItems()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 progress = Progress(nbElements, "Parsing input file", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 nbRead = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 nbWritten = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 for element in self.parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 name = element.getName()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 nbRead += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 if Utils.xor(name in self.names, self.exclude):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.writer.addElement(element)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 nbWritten += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 if name in self.names:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self.names.remove(name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 if self.verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 print "%d read" % (nbRead)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 print "%d written (%d%%)" % (nbWritten, 0 if nbRead == 0 else round(float(nbWritten) / nbRead * 100))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 self._readNames()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self._write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 if self.names:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 print "Some names are not present in the file: %s" % ", ".join(self.names)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 description = "Restrict Sequence List v1.0.1: Keep the elements of a list of sequences whose name is mentionned in a given file. [Category: Data Selection]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 parser.add_option("-i", "--input", dest="inputFile", action="store", type="string", help="input file [compulsory] [format: file in sequence format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 parser.add_option("-f", "--format", dest="format", action="store", default="fasta", type="string", help="format of the input and output files [compulsory] [format: sequence file format] [default: fasta]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 parser.add_option("-n", "--name", dest="names", action="store", type="string", help="names of the transcripts [compulsory] [format: file in TXT format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 parser.add_option("-o", "--output", dest="outputFile", action="store", type="string", help="output file [format: output file in sequence format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 parser.add_option("-x", "--exclude", dest="exclude", action="store_true", default=False, help="output all those whose name is NOT on the list [format: boolean]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 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
106 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 rsl = RestrictSequenceList(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 rsl.setInputFileName(options.inputFile, options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 rsl.setOutputFileName(options.outputFile, options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 rsl.setNamesFileName(options.names)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 rsl.setExclusion(options.exclude)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 rsl.run()