Mercurial > repos > yufei-luo > s_mart
comparison commons/core/writer/EmblWriter.py @ 36:44d5973c188c
Uploaded
author | m-zytnicki |
---|---|
date | Tue, 30 Apr 2013 15:02:29 -0400 |
parents | 769e306b7933 |
children |
comparison
equal
deleted
inserted
replaced
35:d94018ca4ada | 36:44d5973c188c |
---|---|
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 from commons.core.writer.TranscriptListWriter import TranscriptListWriter | |
31 | |
32 | |
33 class EmblWriter(TranscriptListWriter): | |
34 """ | |
35 A class that writes a transcript list into several files with EMBL format | |
36 @ivar fileName: name of the file | |
37 @type fileName: string | |
38 @ivar handle: handle to the file | |
39 @type handle: file handle | |
40 """ | |
41 | |
42 | |
43 def __init__(self, fileName, verbosity = 0): | |
44 """ | |
45 Constructor | |
46 @param fileName: name of the file | |
47 @type fileName: string | |
48 @param verbosity: verbosity | |
49 @type verbosity: int | |
50 """ | |
51 self.fileName = fileName | |
52 self.verbosity = verbosity | |
53 self.handles = {} | |
54 self.handle = None | |
55 | |
56 | |
57 def __del__(self): | |
58 """ | |
59 Destructor | |
60 Trick to append the sequences at the end of the EMBL files | |
61 """ | |
62 handle = open(self.sequenceFileName) | |
63 currentHandle = None | |
64 for line in handle: | |
65 if line[0] == ">": | |
66 chromosome = line[1:].strip() | |
67 if chromosome in self.handles: | |
68 currentHandle = self.handles[chromosome] | |
69 else: | |
70 currentHandle = None | |
71 else: | |
72 if currentHandle != None: | |
73 currentHandle.write(line) | |
74 handle.close() | |
75 for handle in self.handles.values(): | |
76 handle.close() | |
77 | |
78 | |
79 @staticmethod | |
80 def getFileFormats(): | |
81 """ | |
82 Get the format of the file | |
83 """ | |
84 return ["embl"] | |
85 | |
86 | |
87 @staticmethod | |
88 def getExtension(): | |
89 """ | |
90 Get the usual extension for the file | |
91 """ | |
92 return "embl" | |
93 | |
94 | |
95 def addTranscript(self, transcript): | |
96 """ | |
97 Add a transcript to the list of transcripts to be written | |
98 @param transcript: transcript to be written | |
99 @type transcript: class L{Transcript<Transcript>} | |
100 """ | |
101 chromosome = transcript.getChromosome() | |
102 if chromosome not in self.handles: | |
103 self.handles[chromosome] = open("%s%s.embl" % (self.fileName[:-len(".embl")], chromosome.title()), "w") | |
104 self.handles[chromosome].write(self.printTranscript(transcript)) | |
105 | |
106 | |
107 def printTranscript(self, transcript): | |
108 """ | |
109 Export the given transcript with GFF2 format | |
110 @param transcript: transcript to be printed | |
111 @type transcript: class L{Transcript<Transcript>} | |
112 @return: a string | |
113 """ | |
114 return transcript.printEmbl() | |
115 | |
116 |