comparison commons/core/parsing/ParserChooser.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children 94ab73e8a190
comparison
equal deleted inserted replaced
5:ea3082881bf8 6:769e306b7933
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.NCListParser import NCListParser
57 from commons.core.parsing.PklParser import PklParser
58
59 #Attention!! Do not delete the imports!! They are used to know the type of file format!!!
60
61 class ParserChooser(object):
62 """
63 A class that finds the correct parser
64 @ivar format: the format
65 @type format: string
66 @ivar type: transcript / mapping / sequence parser
67 @type type: string
68 @ivar parser: the parser
69 @type parser: object
70 @ivar verbosity: verbosity
71 @type verbosity: int
72 """
73
74 def __init__(self, verbosity = 0):
75 """
76 Constructor
77 @param verbosity: verbosity
78 @type verbosity: int
79 """
80 self.type = None
81 self.parserClass = None
82 self.verbosity = verbosity
83
84
85 def findFormat(self, format, type = None):
86 """
87 Find the correct parser
88 @ivar format: the format
89 @type format: string
90 @ivar type: transcript / mapping / sequence parser (None is all)
91 @type type: string
92 @return: a parser
93 """
94 classes = {}
95 if (type == "transcript"):
96 classes = {TranscriptListParser: "transcript"}
97 elif (type == "mapping"):
98 classes = {MapperParser: "mapping"}
99 elif (type == "sequence"):
100 classes = {SequenceListParser: "sequence"}
101 elif (type == None):
102 classes = {TranscriptListParser: "transcript", MapperParser: "mapping", SequenceListParser: "sequence"}
103 else:
104 raise Exception("Do not understand format type '%s'" % (type))
105
106 for classType in classes:
107 for parserClass in classType.__subclasses__():
108 if format in parserClass.getFileFormats():
109 self.parserClass = parserClass
110 self.type = classes[classType]
111 return
112 raise Exception("Cannot get parser for format '%s'" % (format))
113
114
115 def getParser(self, fileName):
116 """
117 Get the parser previously found
118 @return: the parser
119 """
120 return self.parserClass(fileName, self.verbosity)
121
122
123 def getType(self):
124 """
125 Get the type of parser previously found
126 @return: the type of parser
127 """
128 return self.type