-
Notifications
You must be signed in to change notification settings - Fork 2
/
trackIO.py
404 lines (355 loc) · 15 KB
/
trackIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python
#Copyright (C) 2013 by Glenn Hickey
#
#Released under the MIT license, see LICENSE.txt
#!/usr/bin/env python
import os
import sys
import string
import random
import logging
import array
import numpy as np
from pybedtools import BedTool, Interval
from .common import runShellCommand, logger, getLocalTempPath
""" all track-data specific io code goes here. Just BED implemented for now,
will eventually add WIG and maybe eventually bigbed / bigwig """
###########################################################################
def readTrackData(trackPath, chrom=None, start=None, end=None, **kwargs):
""" read genome annotation track into python list of values. a value
is returned for every element in range (default value is None). The
type of file is detected from the extension"""
data = None
if not os.path.isfile(trackPath):
raise RuntimeError("Track file not found %s\n" % trackPath)
trackExt = os.path.splitext(trackPath)[1]
tempPath = None
if trackExt == ".bw" or trackExt == ".bigwig" or trackExt == ".wg" or\
trackExt == ".bb" or trackExt == "bigbed":
tempPath = getLocalTempPath("Temp_" + os.path.splitext(
os.path.basename(trackPath))[0], ".bed")
logger.info("Extracting %s to temp bed %s. Make sure to erase"
" in event of crash" % (trackExt,
os.path.abspath(tempPath)))
coords = ""
if chrom is not None:
assert start is not None and end is not None
coords = "-chrom=%s -start=%d -end=%d" % (chrom, start ,end)
tool = "bigWigToBedGraph"
if trackExt == ".bb" or trackExt == ".bigbed":
tool = "bigBedToBed"
runShellCommand("%s %s %s %s" % (tool, trackPath, tempPath, coords))
trackExt = ".bed"
trackPath = tempPath
if (kwargs is None):
kwargs = dict()
kwargs["needIntersect"] = False
if trackExt == ".bed":
data = readBedData(trackPath, chrom, start, end, **kwargs)
elif len(trackExt) >= 3 and trackExt[:3].lower() == ".fa":
data = readFastaData(trackPath, chrom, start, end, **kwargs)
else:
sys.stderr.write("Warning: non-BED, non-FASTA file skipped %s\n" %
trackPath)
if tempPath is not None:
runShellCommand("rm -f %s" % tempPath)
return data
###########################################################################
def readBedData(bedPath, chrom, start, end, **kwargs):
""" Read a bed file into an array with one entry per base, doing
name mapping if specified.
Returns a list of values. Note that the buffer used can be passed
with outBuffer argument"""
valCol = None
sort = False
ignoreBed12 = True
needIntersect = True
if kwargs is not None and "valCol" in kwargs:
valCol = int(kwargs["valCol"])
valMap = None
if kwargs is not None and "valMap" in kwargs:
valMap = kwargs["valMap"]
defVal = None
if valMap is not None:
defVal = valMap.getMissingVal()
updateMap = False
if kwargs is not None and "updateValMap" in kwargs:
updateMap = kwargs["updateValMap"]
if kwargs is not None and "sort" in kwargs:
sort = kwargs["sort"] == True
if kwargs is not None and "ignoreBed12" in kwargs:
ignoreBed12 = kwargs["ignoreBed12"] == True
if kwargs is not None and "needIntersect" in kwargs:
needIntersect = kwargs["needIntersect"]
useDelta = False
if kwargs is not None and "useDelta" in kwargs:
useDelta = kwargs["useDelta"]
outputBuf = None
def clamp(x):
return x
if kwargs is not None and "outputBuf" in kwargs:
outputBuf = kwargs["outputBuf"]
npi = np.iinfo
if outputBuf.dtype == np.float or outputBuf.dtype == np.float32 \
or outputBuf.dtype == np.float16:
npi = np.finfo
maxVal = npi(outputBuf.dtype).max
minVal = npi(outputBuf.dtype).min
def clamp(x):
if x < minVal or x > maxVal:
y = min(max(x, minVal), maxVal)
logger.warning("Clamping track data value %s to %s" % x, y)
return y
return x
if outputBuf is None:
data = [defVal] * (end - start)
else:
data = outputBuf
logger.debug("readBedData(%s, update=%s)" % (bedPath, updateMap))
intersectionPath = bedPath
if needIntersect is True:
interval = Interval(chrom, start, end)
logger.debug("intersecting (%s,%d,%d) and %s" % (
chrom, start, end, bedPath))
# Below, we try switching from all_hits to intersect()
# all_hits seems to leak a ton of memory for big files, so
# we hope intersect (which creates a temp file) will be better
#intersections = bedTool.all_hits(interval)
# bedtool intersect still leaking files. comment out
# and try command-line intersect command instead, removing
# BedTool entirely from this method
#tempTool = BedTool(str(interval), from_string = True)
#intersections = bedTool.intersect(tempTool)
#tempTool.delete_temporary_history(ask=False)
#tempTool = None
tempFileIntIn = getLocalTempPath("Temp_intin", ".bed")
tempFileIntOut = getLocalTempPath("Temp_intout", ".bed")
runShellCommand("echo \"%s\" > %s && intersectBed -a %s "
"-b %s | sortBed > %s && rm -f %s" % (
str(interval), tempFileIntIn, bedPath,
tempFileIntIn, tempFileIntOut, tempFileIntIn))
intersectionPath = tempFileIntOut
else:
intersectionPath = bedPath
if sort is True:
logger.debug("sortBed(%s)" % bedPath)
tempFileSortOut = getLocalTempPath("Temp_sortout", ".bed")
runShellCommand("sortBed -i %s > %s" % (bedPath, tempFileSortOut))
intersectionPath = tempFileSortOut
if ignoreBed12 is False:
logger.debug("bed6(%s)" % bedPath)
tempFileBed6Out = getLocalTempPath("Temp_b6out", ".bed")
runShellCommand("bed12ToBed6 -i %s > %s" % (intersectionPath,
tempFileBed6Out))
if intersectionPath != bedPath:
runShellCommand("rm -f %s" % intersectionPath)
intersectionPath = tempFileBed6Out
intersections = bedRead(intersectionPath)
if intersectionPath != bedPath:
runShellCommand("rm -f %s" % intersectionPath)
logger.debug("loading data from intersections")
basesRead = 0
# prevInterval / prevVal only updated in useDelta mode
prevInterval = None
prevVal = 0
for overlap in intersections:
oStart = max(start, int(overlap[1]))
oEnd = min(end, int(overlap[2]))
if valCol is not None:
if valCol == 0:
val = 1
elif valCol == 4:
assert overlap[4] is not None and overlap[4] != ""
val = overlap[4]
else:
assert valCol == 3
assert overlap[3] is not None and overlap[3] != ""
val = overlap[3]
else:
val = overlap[3]
val0 = val
if useDelta is True:
if prevInterval is not None and int(overlap[1]) == int(prevInterval[2]) \
and prevInterval[0] == overlap[0]:
try: # numeric delta
val0 = float(val) - float(prevVal)
except: # fallback to 0 : same 1 : different
val0 = int(val != prevVal)
prevVal = val
prevInterval = overlap
val = 0
if valMap is not None:
val = valMap.getMap(val, update=updateMap)
val0 = valMap.getMap(val0, update=updateMap)
data[oStart - start] = val0
for i in xrange(1, oEnd - oStart):
data[i + oStart - start] = val
basesRead += oEnd - oStart
logger.debug("done readBedData(%s). %d bases read" % (bedPath, basesRead))
return data
###########################################################################
def readBedIntervals(bedPath, ncol = 3,
chrom = None, start = None, end = None,
sort = False, ignoreBed12 = True):
""" Read bed intervals from a bed file (or a specifeid range therein).
NOTE: intervals are sorted by their coordinates"""
if not os.path.isfile(bedPath):
raise RuntimeError("Bed interval file %s not found" % bedPath)
assert ncol == 3 or ncol == 4 or ncol == 5
outIntervals = []
logger.debug("readBedIntervals(%s)" % bedPath)
bedTool = BedTool(bedPath)
if sort is True:
bedTool = bedTool.sort()
logger.debug("sortBed(%s)" % bedPath)
if ignoreBed12 is False:
bedTool = bedTool.bed6()
logger.debug("bed6(%s)" % bedPath)
if chrom is None:
bedIntervals = bedTool
else:
assert start is not None and end is not None
interval = Interval(chrom, start, end)
logger.debug("intersecting (%s,%d,%d) and %s" % (chrom, start, end,
bedPath))
# Below, we try switching from all_hits to intersect()
# all_hits seems to leak a ton of memory for big files, so
# we hope intersect (which creates a temp file) will be better
#bedIntervals = bedTool.all_hits(interval)
tempTool = BedTool(str(interval), from_string = True)
bedIntervals = bedTool.intersect(tempTool)
tempTool.delete_temporary_history(ask=False)
logger.debug("appending bed intervals")
for feat in bedIntervals:
outInterval = (feat.chrom, feat.start, feat.end)
if ncol >= 4:
outInterval += (feat.name,)
if ncol >= 5:
outInterval += (feat.score,)
outIntervals.append(outInterval)
logger.debug("finished readBedIntervals(%s)" % bedPath)
return outIntervals
###########################################################################
def getMergedBedIntervals(bedPath, ncol=3, sort = False, ignoreBed12 = True):
""" Merge all contiguous and overlapping intervals"""
if not os.path.isfile(bedPath):
raise RuntimeError("Bed interval file %s not found" % bedPath)
logger.debug("mergeBedIntervals(%s)" % bedPath)
outIntervals = []
bedTool = BedTool(bedPath)
if sort is True:
bedTool = bedTool.sort()
logger.debug("sortBed(%s)" % bedPath)
if ignoreBed12 is False:
logger.debug("bed6(%s)" % bedPath)
bedTool = bedTool.bed6()
for feat in bedTool.merge():
outInterval = (feat.chrom, feat.start, feat.end)
if ncol >= 4:
outInterval += (feat.name,)
if ncol >= 5:
outInterval += (feat.score,)
outIntervals.append(outInterval)
logger.debug("finished mergeBedIntervals(%s)" % bedPath)
return outIntervals
###########################################################################
def writeBedIntervals(intervals, outPath):
""" write bed intervals to disk """
outFile = open(outPath, "w")
for interval in intervals:
name, score = None, None
if len(interval) > 3:
name = str(interval[3])
if len(interval) > 4:
score = str(interval[4])
bi = Interval(interval[0], interval[1], interval[2], name=name,
score=score)
outFile.write(str(bi))
###########################################################################
def readFastaData(faPath, chrom, start, end, **kwargs):
valMap = None
if kwargs is not None and "valMap" in kwargs:
valMap = kwargs["valMap"]
defVal = None
if valMap is not None:
defVal = valMap.getMissingVal()
updateMap = False
if kwargs is not None and "updateValMap" in kwargs:
updateMap = kwargs["updateValMap"]
caseSensitive = False
if kwargs is not None and "caseSensitive" in kwargs:
caseSensitive = kwargs["caseSensitive"]
outputBuf = None
if kwargs is not None and "outputBuf" in kwargs:
outputBuf = kwargs["outputBuf"]
if outputBuf is None:
data = [defVal] * (end - start)
else:
data = outputBuf
logger.debug("readFastaData(%s, update=%s)" % (faPath, updateMap))
faFile = open(faPath, "r")
basesRead = 0
# skip to sequence using readline (faster than fastaRead)
pos = faFile.tell()
assert pos == 0
chromLen = len(chrom) + 1
while True:
line = faFile.readline()
if len(line) == 0 or (line[0] == '>' and line[1:chromLen] == chrom):
break
pos = faFile.tell()
faFile.seek(pos)
for seqName, seqString in fastaRead(faFile):
if seqName == chrom:
for i in xrange(start, end):
if i >= len(seqString):
break
val = seqString[i]
if caseSensitive is False:
val = val.upper()
if valMap is not None:
val = valMap.getMap(val, update=updateMap)
data[basesRead] = val
basesRead += 1
break
faFile.close()
logger.debug("done readFastaData(%s). %d bases read" % (faPath,
basesRead))
return data
###########################################################################
# Copied from bioio.py from sonLib (https://github.com/benedictpaten/sonLib):
# Copyright (C) 2006-2012 by Benedict Paten ([email protected])
# Released under the MIT license, see LICENSE.txt
def fastaRead(fileHandle):
"""iteratively a sequence for each '>' it encounters, ignores '#' lines
"""
line = fileHandle.readline()
while line != '':
if line[0] == '>':
name = line[1:-1]
line = fileHandle.readline()
seq = array.array('c')
while line != '' and line[0] != '>':
if line[0] != '#':
seq.extend([ i for i in line[:-1] if i != '\t' and i != ' ' ])
line = fileHandle.readline()
for i in seq:
#For safety and sanity I only allows roman alphabet characters in fasta sequences.
if not ((i >= 'A' and i <= 'Z') or (i >= 'a' and i <= 'z') or i == '-'):
raise RuntimeError("Invalid FASTA character, ASCII code = \'%d\', found in input sequence %s" % (ord(i), name))
yield name, seq.tostring()
else:
line = fileHandle.readline()
def bedRead(filePath):
""" pyBedTools leaks files and I can't figure out what to do about it,
other than just replace it insdead the readBedData loop..."""
bf = open(filePath, "r")
intervals = []
for line in bf:
if len(line) > 0 and line[0] != "#":
interval = line[:-1].split("\t")
if len(interval) > 2:
intervals.append(interval)
# man I wish I could figure out just how to close a BedTool?!:
bf.close()
return intervals