annotate commons/core/parsing/WigParser.py @ 22:1e3f2c2657a3

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:26:07 -0400
parents 94ab73e8a190
children
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)
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
126 m6 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s+span=(\d+)\s*$", line)
6
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:
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
159 if m6.group(2) != "1":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
160 raise Exception("Error! Cannot parse variable step WIG files with step > 1 or span > 1")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
161 newChromosome = m6.group(1)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
162 if newChromosome != chromosome:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
163 if outputFile != None:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
164 outputFile.close()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
165 outputFile = open(self.makeIndexName(newChromosome), "wb")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
166 index = 0
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
167 outputFile.write(self.filler)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
168 chromosome = newChromosome
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 elif (len(line) == 0) or line[0] == "#" or line.startswith("track"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 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
173
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 mark = inputFile.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 line = inputFile.readline().strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 inputFile.close
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 if outputFile != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 outputFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 self.indexBuilt = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 def getIndexFileHandle(self, chromosome, strand = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 Get the handle of an index file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 indexFileKey = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 if strand != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 indexFileKey += "+" if strand == 1 else "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 if indexFileKey in self.indexFiles:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 return self.indexFiles[indexFileKey]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 indexFileName = self.makeIndexName(chromosome, strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 if not self.findIndexFile(chromosome, strand):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 self.makeIndexFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 if not os.path.exists(indexFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 print "Warning! Index for chromosome %s, strand %s does not exist." % (chromosome, STRANDTOSTR[strand])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 indexFile = open(indexFileName, "rb")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 if len(self.indexFiles.keys()) > nbOpenHandles:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 removedKey = set(self.indexFiles.keys()).pop()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 self.indexFiles[removedKey].close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 del self.indexFiles[removedKey]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 self.indexFiles[indexFileKey] = indexFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 return indexFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 def findIndex(self, chromosome, start, strand = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 Find the point where to start reading file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 sizeOfLong = struct.calcsize("Q")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 empty = int(struct.unpack("Q", self.filler)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 offset = empty
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 indexFile = self.getIndexFileHandle(chromosome, strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 if not indexFile:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 return (None, None)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 while offset == empty:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 address = start * sizeOfLong
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 indexFile.seek(address, os.SEEK_SET)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 buffer = indexFile.read(sizeOfLong)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 if len(buffer) != sizeOfLong:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 if buffer == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 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
232 return (None, None)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 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
235
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 offset = int(struct.unpack("Q", buffer)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 start += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 start -= 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 return (offset, start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 def getRange(self, chromosome, start, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 Parse a wig file and output a range
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 arrays = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 strands = {1: "+", -1: "-"} if self.strands else {0: ""}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 for strand in strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 array = [self.defaultValue] * (end - start + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 file = open(self.fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 offset, index = self.findIndex(chromosome, start, strand if self.strands else None)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 if offset == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 arrays[strand] = array
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 file.seek(offset, os.SEEK_SET)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 for line in file:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 m1 = re.search(r"^\s*(-?\d+\.?\d*)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 m2 = re.search(r"^\s*(\d+)\s+(-?\d+\.?\d*)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 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
267 m4 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 if m1 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 if index > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 if index >= start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 array[index - start] = float(m1.group(1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 index += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 elif m2 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 index = int(m2.group(1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 if index > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 if index >= start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 array[index - start] = float(m2.group(2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 index += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 elif m3 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 if m3.group(1) != "%s%s" % (chromosome, strands[strand]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 index = int(m3.group(2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 elif m4 != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 if m4.group(1) != "%s%s" % (chromosome, strands[strand]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 elif (len(line) == 0) or (line[0] == "#") or line.startswith("track"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 raise Exception("Error! Cannot read line '%s' of wig file" % (line))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 file.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 arrays[strand] = array
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 if self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 return arrays
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 return array
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 def skipFirstLines(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 def parseLine(self, line):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 if line.startswith("track"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 m = re.search(r"^\s*variableStep\s+chrom=(\S+)", line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 if m != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 chromosome = m.group(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 if chromosome.endswith("+"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 self.currentStrand = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 self.currentChromosome = chromosome[:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 elif chromosome.endswith("-"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 self.currentStrand = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 self.currentChromosome = chromosome[:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 self.currentStrand = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 self.currentChromosome = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 position, value = line.split()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 position = int(position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 value = float(value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 transcript.setChromosome(self.currentChromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 transcript.setStart(position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 transcript.setEnd(position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 transcript.setDirection(self.currentStrand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 transcript.setTagValue("ID", "wig_%s_%d_%d" % (self.currentChromosome, self.currentStrand, position))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 transcript.setTagValue("nbElements", value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 return transcript