6
|
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 os
|
|
31 import sys
|
|
32 from commons.core.writer.WriterChooser import WriterChooser
|
|
33 from commons.core.writer.MySqlTranscriptWriter import MySqlTranscriptWriter
|
|
34
|
|
35 class TranscriptWriter(object):
|
|
36 """
|
|
37 An interface class that writes a list of transcripts, handle different formats
|
|
38 @ivar container: container of the data
|
|
39 @type container: L{TranscriptContainer<TranscriptContainer>}
|
|
40 @ivar format: format of the data to be printed
|
|
41 @type format: string
|
|
42 @ivar file: the file where to print
|
|
43 @type file: string
|
|
44 @ivar type: type of the data (transcripts, mappings or mySQL)
|
|
45 @type type: string
|
|
46 @ivar writer: a transcript list writer
|
|
47 @type writer: L{TranscriptListWriter<TranscriptListWriter>} or None
|
|
48 @ivar mode: use a container or enter transcript one by one
|
|
49 @type mode: string
|
|
50 @ivar verbosity: verbosity
|
|
51 @type verbosity: int
|
|
52 """
|
|
53
|
|
54 def __init__(self, file, format, verbosity = 0):
|
|
55 """
|
|
56 Constructor
|
|
57 @param container: container of the data
|
|
58 @type container: string
|
|
59 @param format: format of the data
|
|
60 @type format: string
|
|
61 @param file: file where to print
|
|
62 @type file: string
|
|
63 @param verbosity: verbosity
|
|
64 @type verbosity: int
|
|
65 """
|
|
66 self.container = None
|
|
67 self.format = format
|
|
68 self.file = file
|
|
69
|
|
70 self.verbosity = verbosity
|
|
71 self.type = None
|
|
72 self.writer = None
|
|
73 self.mode = None
|
|
74 if self.format == None:
|
|
75 sys.exit("Error! Writer input format is empty!")
|
|
76
|
|
77 if self.format == "sql":
|
|
78 self.type = "sql"
|
|
79 pos = self.file.rfind(os.sep)
|
|
80 if pos > -1:
|
|
81 self.file = self.file[pos+1:]
|
|
82 self.writer = MySqlTranscriptWriter(self.file, self.verbosity)
|
|
83 else:
|
|
84 writerChooser = WriterChooser(self.verbosity)
|
|
85 writerChooser.findFormat(self.format)
|
|
86 self.writer = writerChooser.getWriter(self.file)
|
|
87 self.type = writerChooser.getType()
|
|
88
|
|
89
|
|
90 def close(self):
|
|
91 """
|
|
92 Close writer
|
|
93 """
|
|
94 if self.writer != None:
|
|
95 self.writer.close()
|
|
96
|
|
97
|
|
98 def setContainer(self, container):
|
|
99 """
|
|
100 Set a container for the data
|
|
101 @param container: container of the data
|
|
102 @type container: class L{TranscriptContainer<TranscriptContainer>}
|
|
103 """
|
|
104 self.container = container
|
|
105 if self.mode == "transcript":
|
|
106 raise Exception("Error! TranscriptWriter '%s' on 'transcript' mode is currently used on 'container' mode." % (self.file))
|
|
107 self.mode = "container"
|
|
108
|
|
109
|
|
110 def addTranscript(self, transcript):
|
|
111 """
|
|
112 Add a transcript to write
|
|
113 @param transcript: a transcript
|
|
114 @type transcript: class L{Transcript<Transcript>}
|
|
115 """
|
|
116 self.writer.addTranscript(transcript)
|
|
117 if self.mode == "container":
|
|
118 sys.exit("Error! TranscriptWriter '%s' on 'container' mode is currently used on 'transcript' mode." % (self.file))
|
|
119 self.mode = "transcript"
|
|
120
|
|
121
|
|
122 def addElement(self, transcript):
|
|
123 """
|
|
124 Same as addTranscript
|
|
125 """
|
|
126 self.addTranscript(transcript)
|
|
127
|
|
128
|
|
129 def setTitle(self, title):
|
|
130 """
|
|
131 Possibly write a title for the list
|
|
132 @param title: a title for the list
|
|
133 @type title: string
|
|
134 """
|
|
135 if self.writer != None:
|
|
136 self.writer.setTitle(title)
|
|
137
|
|
138 def setFeature(self, feature):
|
|
139 """
|
|
140 Possibly Set the name of the feature
|
|
141 @param title: the title of the feature
|
|
142 @type feature: string
|
|
143 """
|
|
144 if self.writer != None:
|
|
145 self.writer.setFeature(feature)
|
|
146
|
|
147 def setFeaturePart(self, featurePart):
|
|
148 """
|
|
149 Possibly Set the name of the feature part
|
|
150 @param title: the title of the feature part
|
|
151 @type featurePart: string
|
|
152 """
|
|
153 if self.writer != None:
|
|
154 self.writer.setFeaturePart(featurePart)
|
|
155
|
|
156 def setStrands(self, strands):
|
|
157 """
|
|
158 Possibly consider both strands separately
|
|
159 @param strands: whether both strands should be considered separately
|
|
160 @type strands: boolean
|
|
161 """
|
|
162 if self.writer != None:
|
|
163 self.writer.setStrands(strands)
|
|
164
|
|
165
|
|
166 def write(self):
|
|
167 """
|
|
168 Write the content and possibly convert data
|
|
169 """
|
|
170 if self.type == "transcript" or self.type == "sequence":
|
|
171 if self.mode == "container":
|
|
172 self.writer.addTranscriptList(self.container)
|
|
173 return
|
|
174
|
|
175 if self.mode == "transcript" or self.type == "sequence":
|
|
176 self.writer.write()
|
|
177 return
|
|
178
|
|
179 if self.container.format != "sql":
|
|
180 self.container.storeIntoDatabase()
|
|
181 tables = self.container.getTables()
|
|
182 for chromosome in tables:
|
|
183 tables[chromosome].rename("%s_%s" % (self.file, chromosome))
|
|
184 return
|
|
185
|
|
186
|
|
187 def addSequenceFile(self, fileName):
|
|
188 self.writer.addSequenceFile(fileName)
|
|
189 |