Mercurial > repos > yufei-luo > s_mart
comparison commons/core/parsing/ParserChooser.py @ 38:2c0c0a89fad7
Uploaded
| author | m-zytnicki |
|---|---|
| date | Thu, 02 May 2013 09:56:47 -0400 |
| parents | 44d5973c188c |
| children |
comparison
equal
deleted
inserted
replaced
| 37:d22fadc825e3 | 38:2c0c0a89fad7 |
|---|---|
| 1 # | |
| 2 # Copyright INRA-URGI 2009-2010 | |
| 3 # | |
| 4 # This software is governed by the CeCILL license under French law and | |
| 5 # abiding by the rules of distribution of free software. You can use, | |
| 6 # modify and/ or redistribute the software under the terms of the CeCILL | |
| 7 # license as circulated by CEA, CNRS and INRIA at the following URL | |
| 8 # "http://www.cecill.info". | |
| 9 # | |
| 10 # As a counterpart to the access to the source code and rights to copy, | |
| 11 # modify and redistribute granted by the license, users are provided only | |
| 12 # with a limited warranty and the software's author, the holder of the | |
| 13 # economic rights, and the successive licensors have only limited | |
| 14 # liability. | |
| 15 # | |
| 16 # In this respect, the user's attention is drawn to the risks associated | |
| 17 # with loading, using, modifying and/or developing or reproducing the | |
| 18 # software by the user in light of its specific status of free software, | |
| 19 # that may mean that it is complicated to manipulate, and that also | |
| 20 # therefore means that it is reserved for developers and experienced | |
| 21 # professionals having in-depth computer knowledge. Users are therefore | |
| 22 # encouraged to load and test the software's suitability as regards their | |
| 23 # requirements in conditions enabling the security of their systems and/or | |
| 24 # data to be ensured and, more generally, to use and operate it in the | |
| 25 # same conditions as regards security. | |
| 26 # | |
| 27 # The fact that you are presently reading this means that you have had | |
| 28 # knowledge of the CeCILL license and that you accept its terms. | |
| 29 # | |
| 30 import sys | |
| 31 from commons.core.parsing.TranscriptListParser import TranscriptListParser | |
| 32 from commons.core.parsing.MapperParser import MapperParser | |
| 33 from commons.core.parsing.SequenceListParser import SequenceListParser | |
| 34 from commons.core.parsing.BedParser import BedParser | |
| 35 from commons.core.parsing.GffParser import GffParser | |
| 36 from commons.core.parsing.MapperParser import MapperParser | |
| 37 from commons.core.parsing.CoordsParser import CoordsParser | |
| 38 from commons.core.parsing.SeqmapParser import SeqmapParser | |
| 39 from commons.core.parsing.SoapParser import SoapParser | |
| 40 from commons.core.parsing.Soap2Parser import Soap2Parser | |
| 41 from commons.core.parsing.BlastParser import BlastParser | |
| 42 from commons.core.parsing.PslParser import PslParser | |
| 43 from commons.core.parsing.RmapParser import RmapParser | |
| 44 from commons.core.parsing.ShrimpParser import ShrimpParser | |
| 45 from commons.core.parsing.AxtParser import AxtParser | |
| 46 from commons.core.parsing.ExoParser import ExoParser | |
| 47 from commons.core.parsing.MaqParser import MaqParser | |
| 48 from commons.core.parsing.SamParser import SamParser | |
| 49 from commons.core.parsing.BamParser import BamParser | |
| 50 from commons.core.parsing.BowtieParser import BowtieParser | |
| 51 from commons.core.parsing.ElandParser import ElandParser | |
| 52 from commons.core.parsing.GtfParser import GtfParser | |
| 53 from commons.core.parsing.FastaParser import FastaParser | |
| 54 from commons.core.parsing.FastqParser import FastqParser | |
| 55 from commons.core.parsing.MapParser import MapParser | |
| 56 from commons.core.parsing.WigParser import WigParser | |
| 57 from commons.core.parsing.NCListParser import NCListParser | |
| 58 from commons.core.parsing.PklParser import PklParser | |
| 59 | |
| 60 #Attention!! Do not delete the imports!! They are used to know the type of file format!!! | |
| 61 | |
| 62 class ParserChooser(object): | |
| 63 """ | |
| 64 A class that finds the correct parser | |
| 65 @ivar format: the format | |
| 66 @type format: string | |
| 67 @ivar type: transcript / mapping / sequence parser | |
| 68 @type type: string | |
| 69 @ivar parser: the parser | |
| 70 @type parser: object | |
| 71 @ivar verbosity: verbosity | |
| 72 @type verbosity: int | |
| 73 """ | |
| 74 | |
| 75 def __init__(self, verbosity = 0): | |
| 76 """ | |
| 77 Constructor | |
| 78 @param verbosity: verbosity | |
| 79 @type verbosity: int | |
| 80 """ | |
| 81 self.type = None | |
| 82 self.parserClass = None | |
| 83 self.verbosity = verbosity | |
| 84 | |
| 85 | |
| 86 def findFormat(self, format, type = None): | |
| 87 """ | |
| 88 Find the correct parser | |
| 89 @ivar format: the format | |
| 90 @type format: string | |
| 91 @ivar type: transcript / mapping / sequence parser (None is all) | |
| 92 @type type: string | |
| 93 @return: a parser | |
| 94 """ | |
| 95 classes = {} | |
| 96 if (type == "transcript"): | |
| 97 classes = {TranscriptListParser: "transcript"} | |
| 98 elif (type == "mapping"): | |
| 99 classes = {MapperParser: "mapping"} | |
| 100 elif (type == "sequence"): | |
| 101 classes = {SequenceListParser: "sequence"} | |
| 102 elif (type == None): | |
| 103 classes = {TranscriptListParser: "transcript", MapperParser: "mapping", SequenceListParser: "sequence"} | |
| 104 else: | |
| 105 raise Exception("Do not understand format type '%s'" % (type)) | |
| 106 | |
| 107 for classType in classes: | |
| 108 for parserClass in classType.__subclasses__(): | |
| 109 if format in parserClass.getFileFormats(): | |
| 110 self.parserClass = parserClass | |
| 111 self.type = classes[classType] | |
| 112 return | |
| 113 raise Exception("Cannot get parser for format '%s'" % (format)) | |
| 114 | |
| 115 | |
| 116 def getParser(self, fileName): | |
| 117 """ | |
| 118 Get the parser previously found | |
| 119 @return: the parser | |
| 120 """ | |
| 121 return self.parserClass(fileName, self.verbosity) | |
| 122 | |
| 123 | |
| 124 def getType(self): | |
| 125 """ | |
| 126 Get the type of parser previously found | |
| 127 @return: the type of parser | |
| 128 """ | |
| 129 return self.type |
