annotate commons/core/parsing/WigParser.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children 94ab73e8a190
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 re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import os.path
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import struct
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.parsing.TranscriptListParser import TranscriptListParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 STRANDTOSTR = {1: "(+)", 0: "(=)", None: "(=)", -1: "(-)"}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 nbOpenHandles = 30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 class WigParser(TranscriptListParser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 """A class that parses a big WIG file, creates an index and make it possible to quickly retrieve some data"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def __init__(self, fileName, verbosity = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 self.fileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 self.filler = "\xFF" * struct.calcsize('Q')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self.strands = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.indexFiles = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.indexBuilt = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.defaultValue = 0.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.currentChromosome = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.currentStrand = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 super(WigParser, self).__init__(fileName, verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 def __def__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 for file in self.indexFiles.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 file.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 def setStrands(self, strands):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self.strands = strands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 def setDefaultValue(self, value):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self.defaultValue = value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 def getFileFormats():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 return ["wig"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 getFileFormats = staticmethod(getFileFormats)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 def setStrands(self, strands):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 Consider both strands separately
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self.strands = strands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 def makeIndexName(self, chromosome, strand = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 Create an index name for a file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 directoryName = os.path.dirname(self.fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 if strand == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 strandName = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 strandName = "+" if strand == 1 else "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 indexName = os.path.join(directoryName, ".%s%s.index" % (chromosome, strandName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 return indexName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 def findIndexFile(self, chromosome, strand = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 Check if the index of a file exists
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 indexName = self.makeIndexName(chromosome, strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 if os.path.exists(indexName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 return indexName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 def makeIndexFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 Create the index for a file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 if self.indexBuilt:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 inputFile = open(self.fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 outputFile = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 index = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 mark = inputFile.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 line = inputFile.readline().strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 chromosome = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 while line != "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 m1 = re.search(r"^\s*-?\d+\.?\d*\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 m2 = re.search(r"^\s*(\d+)\s+-?\d+\.?\d*\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 m3 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=(\d+)\s+step=1\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 m4 = re.search(r"^\s*fixedStep\s+chrom=\S+\s+start=\d+\s+step=\d+\s+span=\d+\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 m5 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 m6 = re.search(r"^\s*variableStep\s+chrom=(\S+)span=(\d+)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 if m1 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 outputFile.write(struct.pack("Q", mark))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 index += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 elif m2 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 nextIndex = int(m2.group(1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 if index < nextIndex - 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 outputFile.write(self.filler * (nextIndex - index - 1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 outputFile.write(struct.pack("Q", mark))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 index = nextIndex
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 elif m3 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 newChromosome = m3.group(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 if newChromosome != chromosome:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 if outputFile != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 outputFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 outputFile = open(self.makeIndexName(newChromosome), "wb")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 chromosome = newChromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 nextIndex = int(m3.group(2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 outputFile.write(self.filler * (nextIndex - index))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 index = nextIndex
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 elif m4 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 raise Exception("Error! Cannot parse fixed step WIG files with step > 1 or span > 1")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 elif m5 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 newChromosome = m5.group(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 if newChromosome != chromosome:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 if outputFile != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 outputFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 outputFile = open(self.makeIndexName(newChromosome), "wb")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 index = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 outputFile.write(self.filler)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 chromosome = newChromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 elif m6 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 raise Exception("Error! Cannot parse variable step WIG files with step > 1 or span > 1")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 elif (len(line) == 0) or line[0] == "#" or line.startswith("track"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 raise Exception("Error! Cannot understand line '%s' of WIG file while creating index file! Aborting." % (line))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 mark = inputFile.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 line = inputFile.readline().strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 inputFile.close
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 if outputFile != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 outputFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 self.indexBuilt = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 def getIndexFileHandle(self, chromosome, strand = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 Get the handle of an index file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 indexFileKey = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 if strand != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 indexFileKey += "+" if strand == 1 else "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 if indexFileKey in self.indexFiles:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 return self.indexFiles[indexFileKey]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 indexFileName = self.makeIndexName(chromosome, strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 if not self.findIndexFile(chromosome, strand):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 self.makeIndexFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 if not os.path.exists(indexFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 print "Warning! Index for chromosome %s, strand %s does not exist." % (chromosome, STRANDTOSTR[strand])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 indexFile = open(indexFileName, "rb")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 if len(self.indexFiles.keys()) > nbOpenHandles:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 removedKey = set(self.indexFiles.keys()).pop()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 self.indexFiles[removedKey].close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 del self.indexFiles[removedKey]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 self.indexFiles[indexFileKey] = indexFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 return indexFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 def findIndex(self, chromosome, start, strand = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 Find the point where to start reading file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 sizeOfLong = struct.calcsize("Q")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 empty = int(struct.unpack("Q", self.filler)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 offset = empty
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 indexFile = self.getIndexFileHandle(chromosome, strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 if not indexFile:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 return (None, None)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 while offset == empty:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 address = start * sizeOfLong
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 indexFile.seek(address, os.SEEK_SET)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 buffer = indexFile.read(sizeOfLong)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 if len(buffer) != sizeOfLong:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 if buffer == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 print "Warning! Index position %d of chromosome %s on strand %s seems out of range!" % (start, chromosome, STRANDTOSTR[strand])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 return (None, None)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 raise Exception("Problem fetching position %d of chromosome %s on strand %s seems out of range!" % (start, chromosome, STRANDTOSTR[strand]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 offset = int(struct.unpack("Q", buffer)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 start += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 start -= 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 return (offset, start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 def getRange(self, chromosome, start, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 Parse a wig file and output a range
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 arrays = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 strands = {1: "+", -1: "-"} if self.strands else {0: ""}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 for strand in strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 array = [self.defaultValue] * (end - start + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 file = open(self.fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 offset, index = self.findIndex(chromosome, start, strand if self.strands else None)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 if offset == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 arrays[strand] = array
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 file.seek(offset, os.SEEK_SET)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 for line in file:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 m1 = re.search(r"^\s*(-?\d+\.?\d*)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 m2 = re.search(r"^\s*(\d+)\s+(-?\d+\.?\d*)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 m3 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=(\d+)\s+step=\d+\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 m4 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 if m1 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 if index > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 if index >= start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 array[index - start] = float(m1.group(1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 index += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 elif m2 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 index = int(m2.group(1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 if index > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 if index >= start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 array[index - start] = float(m2.group(2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 index += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 elif m3 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 if m3.group(1) != "%s%s" % (chromosome, strands[strand]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 index = int(m3.group(2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 elif m4 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 if m4.group(1) != "%s%s" % (chromosome, strands[strand]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 elif (len(line) == 0) or (line[0] == "#") or line.startswith("track"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 raise Exception("Error! Cannot read line '%s' of wig file" % (line))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 file.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 arrays[strand] = array
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 if self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 return arrays
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 return array
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 def skipFirstLines(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 def parseLine(self, line):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 if line.startswith("track"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 m = re.search(r"^\s*variableStep\s+chrom=(\S+)", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 if m != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 chromosome = m.group(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 if chromosome.endswith("+"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 self.currentStrand = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 self.currentChromosome = chromosome[:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 elif chromosome.endswith("-"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 self.currentStrand = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 self.currentChromosome = chromosome[:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 self.currentStrand = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 self.currentChromosome = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 position, value = line.split()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 position = int(position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 value = float(value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 transcript.setChromosome(self.currentChromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 transcript.setStart(position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 transcript.setEnd(position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 transcript.setDirection(self.currentStrand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 transcript.setTagValue("ID", "wig_%s_%d_%d" % (self.currentChromosome, self.currentStrand, position))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 transcript.setTagValue("nbElements", value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 return transcript