| 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 """ | 
|  | 32 Read a transcript file and convert it to another format | 
|  | 33 """ | 
|  | 34 | 
|  | 35 import os, re | 
|  | 36 from optparse import OptionParser | 
|  | 37 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer | 
|  | 38 from commons.core.writer.TranscriptWriter import TranscriptWriter | 
|  | 39 from SMART.Java.Python.misc.Progress import Progress | 
|  | 40 | 
|  | 41 | 
|  | 42 class ConvertTranscriptFile(object): | 
|  | 43     def __init__(self,inputFileName="", inputFormat ="", outputFileName="", outputFormat="", name="", sequenceFileName=None, strands=False, galaxy=False, feature=None, featurePart=None, verbosity=1): | 
|  | 44         self.inputFileName = inputFileName | 
|  | 45         self.inputFormat = inputFormat | 
|  | 46         self.outputFileName = outputFileName | 
|  | 47         self.outputFormat = outputFormat | 
|  | 48         self.name = name | 
|  | 49         self.sequenceFileName = sequenceFileName | 
|  | 50         self.strands = strands | 
|  | 51         self.galaxy = galaxy | 
|  | 52 | 
|  | 53         self.feature=feature | 
|  | 54         self.featurePart=featurePart | 
|  | 55 | 
|  | 56         self.verbosity = verbosity | 
|  | 57 | 
|  | 58     def setAttributesFromCmdLine(self): | 
|  | 59         description = "Convert Transcript File v1.0.3: Convert a file from a format to another. [Category: Conversion]" | 
|  | 60         parser = OptionParser(description = description) | 
|  | 61         parser.add_option("-i", "--input",        dest="inputFileName",    action="store",                       type="string", help="input file [compulsory] [format: file in format given by -f]") | 
|  | 62         parser.add_option("-f", "--inputFormat",  dest="inputFormat",      action="store",                       type="string", help="format of the input file [compulsory] [format: transcript or mapping file format]") | 
|  | 63         parser.add_option("-o", "--output",       dest="outputFileName",   action="store",                       type="string", help="output file [compulsory] [format: output file in format given by -g]") | 
|  | 64         parser.add_option("-g", "--outputFormat", dest="outputFormat",     action="store",                       type="string", help="format of the output file [compulsory] [format: transcript file format]") | 
|  | 65         parser.add_option("-n", "--name",         dest="name",             action="store",      default="SMART", type="string", help="name for the transcripts [format: string] [default: SMART]") | 
|  | 66         parser.add_option("-s", "--sequences",    dest="sequenceFileName", action="store",      default=None,    type="string", help="give the corresponding Multi-Fasta file (useful for EMBL format) [format: string]") | 
|  | 67         parser.add_option("-t", "--strands",      dest="strands",          action="store_true", default=False,                  help="consider the 2 strands as different (only useful for writing WIG files) [format: bool] [default: False]") | 
|  | 68         parser.add_option("-v", "--verbosity",    dest="verbosity",        action="store",      default=1,       type="int",    help="trace level [format: int] [default: 1]") | 
|  | 69         parser.add_option("-G", "--galaxy",       dest="galaxy",           action="store_true", default=False,                  help="used for galaxy [format: bool] [default: False]") | 
|  | 70         (options, args) = parser.parse_args() | 
|  | 71         self._setAttributesFromOptions(options) | 
|  | 72 | 
|  | 73     def _setAttributesFromOptions(self, options): | 
|  | 74         self.inputFileName = options.inputFileName | 
|  | 75         self.inputFormat = options.inputFormat | 
|  | 76         self.outputFileName = options.outputFileName | 
|  | 77         self.outputFormat = options.outputFormat | 
|  | 78         self.name = options.name | 
|  | 79         self.sequenceFileName = options.sequenceFileName | 
|  | 80         self.strands = options.strands | 
|  | 81         self.galaxy =  options.galaxy | 
|  | 82         self.verbosity = options.verbosity | 
|  | 83 | 
|  | 84     def run(self): | 
|  | 85         # create parser | 
|  | 86         parser = TranscriptContainer(self.inputFileName, self.inputFormat, self.verbosity) | 
|  | 87         # create writer | 
|  | 88         writer = TranscriptWriter(self.outputFileName, self.outputFormat, self.verbosity) | 
|  | 89         # connect parser and writer | 
|  | 90         writer.setContainer(parser) | 
|  | 91 | 
|  | 92         if self.name != None: | 
|  | 93             writer.setTitle(self.name) | 
|  | 94         if self.feature != None: | 
|  | 95             writer.setFeature(self.feature) | 
|  | 96         if self.featurePart != None: | 
|  | 97             writer.setFeaturePart(self.featurePart) | 
|  | 98         if self.sequenceFileName != None: | 
|  | 99             writer.addSequenceFile(self.sequenceFileName) | 
|  | 100 | 
|  | 101         nbItems = 0 | 
|  | 102         if self.verbosity > 0: | 
|  | 103             nbItems = parser.getNbItems() | 
|  | 104             print "%i items found" % (nbItems) | 
|  | 105 | 
|  | 106         if self.strands: | 
|  | 107             writer.setStrands(True) | 
|  | 108         # convert | 
|  | 109         writer.write() | 
|  | 110         writer.close() | 
|  | 111 | 
|  | 112 if __name__ == "__main__": | 
|  | 113     iConvertTranscriptFile = ConvertTranscriptFile() | 
|  | 114     iConvertTranscriptFile.setAttributesFromCmdLine() | 
|  | 115     iConvertTranscriptFile.run() |