annotate commons/core/parsing/ParserChooser.py @ 68:85e80c21b1f7 draft

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