comparison commons/core/writer/WigWriter.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 from commons.core.writer.TranscriptListWriter import TranscriptListWriter
31
32
33 class WigWriter(TranscriptListWriter):
34 """
35 A class that writes a transcript list into a file with WIGGLE 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 @ivar header: first lines of the file
41 @type header: string
42 """
43
44
45 def __init__(self, fileName, verbosity = 0):
46 """
47 Constructor
48 @param fileName: name of the file
49 @type fileName: string
50 @param verbosity: verbosity
51 @type verbosity: int
52 """
53 self.fileName = fileName
54 self.verbosity = verbosity
55 self.data = {-1: {}, 0: {}, 1: {}}
56 self.title = "Reads"
57 self.strands = False
58 self.handle = None
59
60
61 def __del__(self):
62 """
63 Destructor
64 Actually print the file
65 """
66 strand2string = {-1: "-", 1: "+", 0: ""}
67 self.handle = open(self.fileName, "w")
68 self.handle.write("track type=wiggle_0 name=\"%s\"\n" % (self.title))
69 for strand in self.data:
70 for chromosome in sorted(self.data[strand]):
71 self.handle.write("variableStep chrom=%s%s\n" % (chromosome, strand2string[strand]))
72 for pos in sorted(self.data[strand][chromosome]):
73 self.handle.write("%d\t%d\n" % (pos, self.data[strand][chromosome][pos]))
74 self.handle.close()
75
76
77 @staticmethod
78 def getFileFormats():
79 """
80 Get the format of the file
81 """
82 return ["wig", "wiggle"]
83
84
85 @staticmethod
86 def getExtension():
87 """
88 Get the usual extension for the file
89 """
90 return "wig"
91
92
93 def setTitle(self, title):
94 """
95 Set the title of the track
96 @param title: the title of the track
97 @type title: string
98 """
99 if title != None:
100 self.title = title
101
102
103 def setStrands(self, strands):
104 """
105 Consider each strand separately
106 @param boolean: whether each strand should be considered separately
107 @type boolean: boolean
108 """
109 self.strands = strands
110
111
112 def copyProperties(self, parser):
113 """
114 Copy the properties collected by a parser, to produce a similar output
115 @param bedParser: a parser
116 @type bedParser: class L{TranscriptListWriter<TranscriptListWriter>}
117 """
118 self.setTitle(parser.title)
119
120
121 def addTranscript(self, transcript):
122 """
123 Export the given transcript with GBrowse format
124 @param transcript: transcript to be printed
125 @type transcript: class L{Transcript<Transcript>}
126 @return: a string
127 """
128 chromosome = transcript.getChromosome()
129 direction = transcript.getDirection()
130 if not self.strands:
131 direction = 0
132 if chromosome not in self.data[direction]:
133 self.data[direction][chromosome] = {}
134 for exon in transcript.getExons():
135 for pos in range(exon.getStart(), exon.getEnd()+1):
136 if pos not in self.data[direction][chromosome]:
137 self.data[direction][chromosome][pos] = 1
138 else:
139 self.data[direction][chromosome][pos] += 1