annotate commons/core/parsing/WigParser.py @ 46:169d364ddd91

Uploaded
author m-zytnicki
date Mon, 30 Sep 2013 03:19:26 -0400
parents 44d5973c188c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30 import re
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 import sys
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32 import os.path
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 import struct
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34 from commons.core.parsing.TranscriptListParser import TranscriptListParser
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37 STRANDTOSTR = {1: "(+)", 0: "(=)", None: "(=)", -1: "(-)"}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39 nbOpenHandles = 30
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42 class WigParser(TranscriptListParser):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 """A class that parses a big WIG file, creates an index and make it possible to quickly retrieve some data"""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45 def __init__(self, fileName, verbosity = 1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46 self.fileName = fileName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 self.filler = "\xFF" * struct.calcsize('Q')
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48 self.strands = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 self.indexFiles = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50 self.indexBuilt = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51 self.defaultValue = 0.0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 self.currentChromosome = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 self.currentStrand = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54 self.verbosity = verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55 super(WigParser, self).__init__(fileName, verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58 def __def__(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59 for file in self.indexFiles.values():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 file.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63 def setStrands(self, strands):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 self.strands = strands
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67 def setDefaultValue(self, value):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68 self.defaultValue = value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71 def getFileFormats():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72 return ["wig"]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 getFileFormats = staticmethod(getFileFormats)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76 def setStrands(self, strands):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78 Consider both strands separately
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80 self.strands = strands
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83 def makeIndexName(self, chromosome, strand = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85 Create an index name for a file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 directoryName = os.path.dirname(self.fileName)
46
169d364ddd91 Uploaded
m-zytnicki
parents: 36
diff changeset
88 baseName = os.path.splitext(os.path.basename(self.fileName))[0]
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 if strand == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 strandName = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 strandName = "+" if strand == 1 else "-"
46
169d364ddd91 Uploaded
m-zytnicki
parents: 36
diff changeset
93 indexName = os.path.join(directoryName, ".%s.%s%s.index" % (baseName, chromosome, strandName))
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94 return indexName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 def findIndexFile(self, chromosome, strand = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 Check if the index of a file exists
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 indexName = self.makeIndexName(chromosome, strand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 if os.path.exists(indexName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103 return indexName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 return False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 def makeIndexFile(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 Create the index for a file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 if self.indexBuilt:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 inputFile = open(self.fileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 outputFile = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 index = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117 mark = inputFile.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118 line = inputFile.readline().strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119 chromosome = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 while line != "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 m1 = re.search(r"^\s*-?\d+\.?\d*\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 m2 = re.search(r"^\s*(\d+)\s+-?\d+\.?\d*\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 m3 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=(\d+)\s+step=1\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125 m4 = re.search(r"^\s*fixedStep\s+chrom=\S+\s+start=\d+\s+step=\d+\s+span=\d+\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 m5 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 m6 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s+span=(\d+)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 if m1 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 outputFile.write(struct.pack("Q", mark))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 index += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 elif m2 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 nextIndex = int(m2.group(1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 if index < nextIndex - 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 outputFile.write(self.filler * (nextIndex - index - 1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 outputFile.write(struct.pack("Q", mark))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 index = nextIndex
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 elif m3 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 newChromosome = m3.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 if newChromosome != chromosome:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 outputFile = open(self.makeIndexName(newChromosome), "wb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 chromosome = newChromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145 nextIndex = int(m3.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 outputFile.write(self.filler * (nextIndex - index))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147 index = nextIndex
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 elif m4 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149 raise Exception("Error! Cannot parse fixed step WIG files with step > 1 or span > 1")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150 elif m5 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151 newChromosome = m5.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 if newChromosome != chromosome:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155 outputFile = open(self.makeIndexName(newChromosome), "wb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 index = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157 outputFile.write(self.filler)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 chromosome = newChromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159 elif m6 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160 if m6.group(2) != "1":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161 raise Exception("Error! Cannot parse variable step WIG files with step > 1 or span > 1")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162 newChromosome = m6.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 if newChromosome != chromosome:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 outputFile = open(self.makeIndexName(newChromosome), "wb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167 index = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168 outputFile.write(self.filler)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 chromosome = newChromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 elif (len(line) == 0) or line[0] == "#" or line.startswith("track"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171 pass
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173 raise Exception("Error! Cannot understand line '%s' of WIG file while creating index file! Aborting." % (line))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
174
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
175 mark = inputFile.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
176 line = inputFile.readline().strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
177
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
178 inputFile.close
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
179 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
180 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
181 self.indexBuilt = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
182
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
183
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
184 def getIndexFileHandle(self, chromosome, strand = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
185 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
186 Get the handle of an index file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
187 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
188 indexFileKey = chromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
189 if strand != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
190 indexFileKey += "+" if strand == 1 else "-"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
191 if indexFileKey in self.indexFiles:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
192 return self.indexFiles[indexFileKey]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
193
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
194 indexFileName = self.makeIndexName(chromosome, strand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
195 if not self.findIndexFile(chromosome, strand):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
196 self.makeIndexFile()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
197
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
198 if not os.path.exists(indexFileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
199 print "Warning! Index for chromosome %s, strand %s does not exist." % (chromosome, STRANDTOSTR[strand])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
200 return False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
201 indexFile = open(indexFileName, "rb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
202
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
203 if len(self.indexFiles.keys()) > nbOpenHandles:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
204 removedKey = set(self.indexFiles.keys()).pop()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
205 self.indexFiles[removedKey].close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
206 del self.indexFiles[removedKey]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
207 self.indexFiles[indexFileKey] = indexFile
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
208 return indexFile
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
209
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
210
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
211
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
212 def findIndex(self, chromosome, start, strand = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
213 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
214 Find the point where to start reading file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
215 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
216
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
217 sizeOfLong = struct.calcsize("Q")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
218 empty = int(struct.unpack("Q", self.filler)[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
219 offset = empty
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
220 indexFile = self.getIndexFileHandle(chromosome, strand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
221
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
222 if not indexFile:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
223 return (None, None)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
224
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
225 while offset == empty:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
226 address = start * sizeOfLong
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
227 indexFile.seek(address, os.SEEK_SET)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
228
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
229 buffer = indexFile.read(sizeOfLong)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
230 if len(buffer) != sizeOfLong:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
231 if buffer == "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
232 print "Warning! Index position %d of chromosome %s on strand %s seems out of range!" % (start, chromosome, STRANDTOSTR[strand])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
233 return (None, None)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
234 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
235 raise Exception("Problem fetching position %d of chromosome %s on strand %s seems out of range!" % (start, chromosome, STRANDTOSTR[strand]))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
236
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
237 offset = int(struct.unpack("Q", buffer)[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
238 start += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
239
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
240 start -= 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
241 return (offset, start)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
242
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
243
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
244
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
245 def getRange(self, chromosome, start, end):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
246 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
247 Parse a wig file and output a range
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
248 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
249 arrays = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
250 strands = {1: "+", -1: "-"} if self.strands else {0: ""}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
251
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
252 for strand in strands:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
253
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
254 array = [self.defaultValue] * (end - start + 1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
255 file = open(self.fileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
256 offset, index = self.findIndex(chromosome, start, strand if self.strands else None)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
257 if offset == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
258 arrays[strand] = array
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
259 continue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
260 file.seek(offset, os.SEEK_SET)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
261
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
262 for line in file:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
263 line = line.strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
264
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
265 m1 = re.search(r"^\s*(-?\d+\.?\d*)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
266 m2 = re.search(r"^\s*(\d+)\s+(-?\d+\.?\d*)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
267 m3 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=(\d+)\s+step=\d+\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
268 m4 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
269
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
270 if m1 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
271 if index > end:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
272 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
273 if index >= start:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
274 array[index - start] = float(m1.group(1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
275 index += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
276 elif m2 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
277 index = int(m2.group(1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
278 if index > end:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
279 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
280 if index >= start:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
281 array[index - start] = float(m2.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
282 index += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
283 elif m3 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
284 if m3.group(1) != "%s%s" % (chromosome, strands[strand]):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
285 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
286 index = int(m3.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
287 elif m4 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
288 if m4.group(1) != "%s%s" % (chromosome, strands[strand]):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
289 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
290 elif (len(line) == 0) or (line[0] == "#") or line.startswith("track"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
291 pass
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
292 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
293 raise Exception("Error! Cannot read line '%s' of wig file" % (line))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
294
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
295 file.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
296
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
297 arrays[strand] = array
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
298
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
299 if self.strands:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
300 return arrays
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
301 return array
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
302
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
303
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
304 def skipFirstLines(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
305 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
306
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
307
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
308 def parseLine(self, line):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
309 if line.startswith("track"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
310 return None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
311 m = re.search(r"^\s*variableStep\s+chrom=(\S+)", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
312 if m != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
313 chromosome = m.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
314 if chromosome.endswith("+"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
315 self.currentStrand = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
316 self.currentChromosome = chromosome[:-1]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
317 elif chromosome.endswith("-"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
318 self.currentStrand = -1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
319 self.currentChromosome = chromosome[:-1]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
320 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
321 self.currentStrand = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
322 self.currentChromosome = chromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
323 return None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
324 position, value = line.split()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
325 position = int(position)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
326 value = float(value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
327 transcript = Transcript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
328 transcript.setChromosome(self.currentChromosome)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
329 transcript.setStart(position)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
330 transcript.setEnd(position)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
331 transcript.setDirection(self.currentStrand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
332 transcript.setTagValue("ID", "wig_%s_%d_%d" % (self.currentChromosome, self.currentStrand, position))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
333 transcript.setTagValue("nbElements", value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
334 return transcript