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