annotate commons/core/parsing/WigParser.py @ 42:2abc37c27164

Deleted selected files
author m-zytnicki
date Wed, 18 Sep 2013 07:45:59 -0400
parents 44d5973c188c
children 169d364ddd91
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)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88 if strand == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 strandName = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 strandName = "+" if strand == 1 else "-"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 indexName = os.path.join(directoryName, ".%s%s.index" % (chromosome, strandName))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93 return indexName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 def findIndexFile(self, chromosome, strand = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98 Check if the index of a file exists
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 indexName = self.makeIndexName(chromosome, strand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 if os.path.exists(indexName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 return indexName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103 return False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106 def makeIndexFile(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 Create the index for a file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110 if self.indexBuilt:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 inputFile = open(self.fileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 outputFile = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 index = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 mark = inputFile.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117 line = inputFile.readline().strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118 chromosome = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120 while line != "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 m1 = re.search(r"^\s*-?\d+\.?\d*\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 m2 = re.search(r"^\s*(\d+)\s+-?\d+\.?\d*\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 m3 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=(\d+)\s+step=1\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 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
125 m5 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 m6 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s+span=(\d+)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 if m1 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 outputFile.write(struct.pack("Q", mark))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 index += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 elif m2 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 nextIndex = int(m2.group(1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 if index < nextIndex - 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 outputFile.write(self.filler * (nextIndex - index - 1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 outputFile.write(struct.pack("Q", mark))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 index = nextIndex
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 elif m3 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 newChromosome = m3.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 if newChromosome != chromosome:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 outputFile = open(self.makeIndexName(newChromosome), "wb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 chromosome = newChromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 nextIndex = int(m3.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145 outputFile.write(self.filler * (nextIndex - index))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 index = nextIndex
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147 elif m4 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 raise Exception("Error! Cannot parse fixed step WIG files with step > 1 or span > 1")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149 elif m5 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150 newChromosome = m5.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151 if newChromosome != chromosome:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154 outputFile = open(self.makeIndexName(newChromosome), "wb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155 index = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 outputFile.write(self.filler)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157 chromosome = newChromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 elif m6 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159 if m6.group(2) != "1":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160 raise Exception("Error! Cannot parse variable step WIG files with step > 1 or span > 1")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161 newChromosome = m6.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162 if newChromosome != chromosome:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 outputFile = open(self.makeIndexName(newChromosome), "wb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 index = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167 outputFile.write(self.filler)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168 chromosome = newChromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 elif (len(line) == 0) or line[0] == "#" or line.startswith("track"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 pass
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172 raise Exception("Error! Cannot understand line '%s' of WIG file while creating index file! Aborting." % (line))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
174 mark = inputFile.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
175 line = inputFile.readline().strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
176
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
177 inputFile.close
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
178 if outputFile != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
179 outputFile.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
180 self.indexBuilt = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
181
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
182
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
183 def getIndexFileHandle(self, chromosome, strand = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
184 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
185 Get the handle of an index file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
186 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
187 indexFileKey = chromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
188 if strand != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
189 indexFileKey += "+" if strand == 1 else "-"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
190 if indexFileKey in self.indexFiles:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
191 return self.indexFiles[indexFileKey]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
192
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
193 indexFileName = self.makeIndexName(chromosome, strand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
194 if not self.findIndexFile(chromosome, strand):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
195 self.makeIndexFile()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
196
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
197 if not os.path.exists(indexFileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
198 print "Warning! Index for chromosome %s, strand %s does not exist." % (chromosome, STRANDTOSTR[strand])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
199 return False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
200 indexFile = open(indexFileName, "rb")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
201
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
202 if len(self.indexFiles.keys()) > nbOpenHandles:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
203 removedKey = set(self.indexFiles.keys()).pop()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
204 self.indexFiles[removedKey].close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
205 del self.indexFiles[removedKey]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
206 self.indexFiles[indexFileKey] = indexFile
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
207 return indexFile
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
208
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
209
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
210
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
211 def findIndex(self, chromosome, start, strand = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
212 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
213 Find the point where to start reading file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
214 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
215
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
216 sizeOfLong = struct.calcsize("Q")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
217 empty = int(struct.unpack("Q", self.filler)[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
218 offset = empty
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
219 indexFile = self.getIndexFileHandle(chromosome, strand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
220
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
221 if not indexFile:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
222 return (None, None)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
223
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
224 while offset == empty:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
225 address = start * sizeOfLong
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
226 indexFile.seek(address, os.SEEK_SET)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
227
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
228 buffer = indexFile.read(sizeOfLong)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
229 if len(buffer) != sizeOfLong:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
230 if buffer == "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
231 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
232 return (None, None)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
233 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
234 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
235
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
236 offset = int(struct.unpack("Q", buffer)[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
237 start += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
238
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
239 start -= 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
240 return (offset, start)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
241
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
242
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
243
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
244 def getRange(self, chromosome, start, end):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
245 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
246 Parse a wig file and output a range
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
247 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
248 arrays = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
249 strands = {1: "+", -1: "-"} if self.strands else {0: ""}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
250
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
251 for strand in strands:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
252
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
253 array = [self.defaultValue] * (end - start + 1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
254 file = open(self.fileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
255 offset, index = self.findIndex(chromosome, start, strand if self.strands else None)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
256 if offset == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
257 arrays[strand] = array
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
258 continue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
259 file.seek(offset, os.SEEK_SET)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
260
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
261 for line in file:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
262 line = line.strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
263
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
264 m1 = re.search(r"^\s*(-?\d+\.?\d*)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
265 m2 = re.search(r"^\s*(\d+)\s+(-?\d+\.?\d*)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
266 m3 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=(\d+)\s+step=\d+\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
267 m4 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
268
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
269 if m1 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
270 if index > end:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
271 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
272 if index >= start:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
273 array[index - start] = float(m1.group(1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
274 index += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
275 elif m2 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
276 index = int(m2.group(1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
277 if index > end:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
278 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
279 if index >= start:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
280 array[index - start] = float(m2.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
281 index += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
282 elif m3 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
283 if m3.group(1) != "%s%s" % (chromosome, strands[strand]):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
284 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
285 index = int(m3.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
286 elif m4 != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
287 if m4.group(1) != "%s%s" % (chromosome, strands[strand]):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
288 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
289 elif (len(line) == 0) or (line[0] == "#") or line.startswith("track"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
290 pass
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
291 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
292 raise Exception("Error! Cannot read line '%s' of wig file" % (line))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
293
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
294 file.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
295
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
296 arrays[strand] = array
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
297
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
298 if self.strands:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
299 return arrays
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
300 return array
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
301
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
302
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
303 def skipFirstLines(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
304 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
305
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
306
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
307 def parseLine(self, line):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
308 if line.startswith("track"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
309 return None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
310 m = re.search(r"^\s*variableStep\s+chrom=(\S+)", line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
311 if m != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
312 chromosome = m.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
313 if chromosome.endswith("+"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
314 self.currentStrand = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
315 self.currentChromosome = chromosome[:-1]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
316 elif chromosome.endswith("-"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
317 self.currentStrand = -1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
318 self.currentChromosome = chromosome[:-1]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
319 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
320 self.currentStrand = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
321 self.currentChromosome = chromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
322 return None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
323 position, value = line.split()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
324 position = int(position)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
325 value = float(value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
326 transcript = Transcript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
327 transcript.setChromosome(self.currentChromosome)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
328 transcript.setStart(position)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
329 transcript.setEnd(position)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
330 transcript.setDirection(self.currentStrand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
331 transcript.setTagValue("ID", "wig_%s_%d_%d" % (self.currentChromosome, self.currentStrand, position))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
332 transcript.setTagValue("nbElements", value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
333 return transcript