comparison commons/core/writer/WriterChooser.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children
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.writer.TranscriptListWriter import TranscriptListWriter
32 from commons.core.writer.SequenceListWriter import SequenceListWriter
33 from commons.core.writer.BedWriter import BedWriter
34 from commons.core.writer.CsvWriter import CsvWriter
35 from commons.core.writer.EmblWriter import EmblWriter
36 from commons.core.writer.FastaWriter import FastaWriter
37 from commons.core.writer.FastqWriter import FastqWriter
38 from commons.core.writer.GbWriter import GbWriter
39 from commons.core.writer.Gff2Writer import Gff2Writer
40 from commons.core.writer.SamWriter import SamWriter
41 from commons.core.writer.UcscWriter import UcscWriter
42 from commons.core.writer.WigWriter import WigWriter
43 from commons.core.writer.Gff3Writer import Gff3Writer
44 from commons.core.writer.GtfWriter import GtfWriter
45 from commons.core.writer.MapWriter import MapWriter
46
47
48 class WriterChooser(object):
49 """
50 A class that finds the correct writer
51 @ivar type: transcript / sequence writer
52 @type type: string
53 @ivar format: the format of the writer
54 @type format: string
55 @ivar writerClass: the class of the writer
56 @type writerClass: string
57 @ivar extension: default extension of the file
58 @type extension: string
59 @ivar verbosity: verbosity
60 @type verbosity: int
61 """
62
63 def __init__(self, verbosity = 0):
64 """
65 Constructor
66 @param verbosity: verbosity
67 @type verbosity: int
68 """
69 self.type = None
70 self.format = None
71 self.writerClass = None
72 self.extension = None
73 self.verbosity = verbosity
74
75
76 def findFormat(self, format, type = None):
77 """
78 Find the correct parser
79 @ivar format: the format
80 @type format: string
81 @ivar type: transcript sequence parser (None is all)
82 @type type: string
83 @return: a parser
84 """
85 classes = {}
86 if (type == "transcript"):
87 classes = {TranscriptListWriter: "transcript"}
88 elif (type == "sequence"):
89 classes = {SequenceListWriter: "sequence"}
90 elif (type == None):
91 classes = {TranscriptListWriter: "transcript", SequenceListWriter: "sequence"}
92 else:
93 sys.exit("Do not understand format type '%s'" % (type))
94
95 for classType in classes:
96 for writerClass in classType.__subclasses__():
97 if format in writerClass.getFileFormats():
98 self.writerClass = writerClass
99 self.extension = writerClass.getExtension()
100 self.type = classes[classType]
101 return
102 sys.exit("Cannot get writer for format '%s'" % (format))
103
104
105 def getWriter(self, fileName):
106 """
107 Get the writer previously found
108 @return: the writer
109 """
110 return self.writerClass(fileName, self.verbosity)
111
112
113 def getType(self):
114 """
115 Get the type of writer previously found
116 @return: the type of writer
117 """
118 return self.type
119
120
121 def getExtension(self):
122 """
123 Get the default extension of writer previously found
124 @return: the extension
125 """
126 return self.extension
127