annotate commons/core/writer/TranscriptWriter.py @ 71:d96f6c9a39e0 draft default tip

Removed pyc files.
author m-zytnicki
date Thu, 07 Apr 2016 09:25:18 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from commons.core.writer.WriterChooser import WriterChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.writer.MySqlTranscriptWriter import MySqlTranscriptWriter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 class TranscriptWriter(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 An interface class that writes a list of transcripts, handle different formats
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 @ivar container: container of the data
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 @type container: L{TranscriptContainer<TranscriptContainer>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 @ivar format: format of the data to be printed
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 @type format: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 @ivar file: the file where to print
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 @type file: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 @ivar type: type of the data (transcripts, mappings or mySQL)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 @type type: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 @ivar writer: a transcript list writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 @type writer: L{TranscriptListWriter<TranscriptListWriter>} or None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 @ivar mode: use a container or enter transcript one by one
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 @type mode: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 @ivar verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 def __init__(self, file, format, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 @param container: container of the data
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 @type container: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 @param format: format of the data
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 @type format: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 @param file: file where to print
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 @type file: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 @param verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 self.container = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.format = format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self.file = file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self.type = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self.writer = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.mode = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 if self.format == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 sys.exit("Error! Writer input format is empty!")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 if self.format == "sql":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self.type = "sql"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 pos = self.file.rfind(os.sep)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if pos > -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self.file = self.file[pos+1:]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 self.writer = MySqlTranscriptWriter(self.file, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 writerChooser = WriterChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 writerChooser.findFormat(self.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 self.writer = writerChooser.getWriter(self.file)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 self.type = writerChooser.getType()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 def close(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 Close writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 if self.writer != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 self.writer.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 def setContainer(self, container):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 Set a container for the data
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 @param container: container of the data
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 @type container: class L{TranscriptContainer<TranscriptContainer>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 self.container = container
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 if self.mode == "transcript":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 raise Exception("Error! TranscriptWriter '%s' on 'transcript' mode is currently used on 'container' mode." % (self.file))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 self.mode = "container"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 def addTranscript(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 Add a transcript to write
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 @param transcript: a transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self.writer.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 if self.mode == "container":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 sys.exit("Error! TranscriptWriter '%s' on 'container' mode is currently used on 'transcript' mode." % (self.file))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 self.mode = "transcript"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 def addElement(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 Same as addTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 self.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 def setTitle(self, title):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 Possibly write a title for the list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 @param title: a title for the list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 @type title: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 if self.writer != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 self.writer.setTitle(title)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 def setFeature(self, feature):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 Possibly Set the name of the feature
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 @param title: the title of the feature
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 @type feature: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 if self.writer != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 self.writer.setFeature(feature)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 def setFeaturePart(self, featurePart):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 Possibly Set the name of the feature part
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 @param title: the title of the feature part
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 @type featurePart: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 if self.writer != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 self.writer.setFeaturePart(featurePart)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 def setStrands(self, strands):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 Possibly consider both strands separately
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 @param strands: whether both strands should be considered separately
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 @type strands: boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 if self.writer != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 self.writer.setStrands(strands)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 def write(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 Write the content and possibly convert data
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 if self.type == "transcript" or self.type == "sequence":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 if self.mode == "container":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 self.writer.addTranscriptList(self.container)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 if self.mode == "transcript" or self.type == "sequence":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 self.writer.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 if self.container.format != "sql":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 self.container.storeIntoDatabase()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 tables = self.container.getTables()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 for chromosome in tables:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 tables[chromosome].rename("%s_%s" % (self.file, chromosome))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 def addSequenceFile(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 self.writer.addSequenceFile(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189