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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
|
# cython: language_level=3
# cython: profile=True
# Time-stamp: <2022-09-15 17:17:37 Tao Liu>
"""Module for FWTrack classes.
This code is free software; you can redistribute it and/or modify it
under the terms of the BSD License (see the file LICENSE included with
the distribution).
"""
# ------------------------------------
# python modules
# ------------------------------------
import sys
import io
from copy import copy
from collections import Counter
# ------------------------------------
# MACS3 modules
# ------------------------------------
from MACS3.Utilities.Constants import *
from MACS3.Signal.SignalProcessing import *
from MACS3.IO.PeakIO import PeakIO
from MACS3.Signal.Pileup import se_all_in_one_pileup, over_two_pv_array
# ------------------------------------
# Other modules
# ------------------------------------
from cpython cimport bool
cimport cython
import numpy as np
cimport numpy as np
from numpy cimport uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float32_t, float64_t
# ------------------------------------
# constants
# ------------------------------------
__version__ = "FixWidthTrack $Revision$"
__author__ = "Tao Liu <taoliu@jimmy.harvard.edu>"
__doc__ = "FWTrack class"
cdef INT_MAX = <int32_t>((<uint32_t>(-1))>>1)
# ------------------------------------
# Misc functions
# ------------------------------------
# ------------------------------------
# Classes
# ------------------------------------
cdef class FWTrack:
"""Fixed Width Locations Track class along the whole genome
(commonly with the same annotation type), which are stored in a
dict.
Locations are stored and organized by sequence names (chr names) in a
dict. They can be sorted by calling self.sort() function.
"""
cdef:
dict __locations
dict __pointer
dict __buf_size
bool __sorted
bool __destroyed
dict rlengths
public int64_t buffer_size
public int64_t total
public object annotation
public object dups
public int32_t fw
public int64_t length
def __init__ (self, int32_t fw=0, char * anno="", int64_t buffer_size = 100000 ):
"""fw is the fixed-width for all locations.
"""
self.fw = fw
self.__locations = {} # location pairs: two strands
self.__pointer = {} # location pairs
self.__buf_size = {} # location pairs
self.__sorted = False
self.total = 0 # total tags
self.annotation = anno # need to be figured out
self.rlengths = {} # lengths of reference sequences, e.g. each chromosome in a genome
self.buffer_size = buffer_size
self.length = 0
self.__destroyed = False
cpdef void destroy ( self ):
"""Destroy this object and release mem.
"""
cdef:
set chrs
bytes chromosome
chrs = self.get_chr_names()
for chromosome in sorted(chrs):
if chromosome in self.__locations:
self.__locations[chromosome][0].resize( self.buffer_size, refcheck=False )
self.__locations[chromosome][0].resize( 0, refcheck=False )
self.__locations[chromosome][1].resize( self.buffer_size, refcheck=False )
self.__locations[chromosome][1].resize( 0, refcheck=False )
self.__locations[chromosome] = [None, None]
self.__locations.pop(chromosome)
self.__destroyed = True
return
cpdef void add_loc ( self, bytes chromosome, int32_t fiveendpos, int32_t strand ):
"""Add a location to the list according to the sequence name.
chromosome -- mostly the chromosome name
fiveendpos -- 5' end pos, left for plus strand, right for minus strand
strand -- 0: plus, 1: minus
"""
cdef:
int32_t i
int32_t b
np.ndarray arr
if chromosome not in self.__locations:
self.__buf_size[chromosome] = [ self.buffer_size, self.buffer_size ]
self.__locations[chromosome] = [ np.zeros(self.buffer_size, dtype='int32'), np.zeros(self.buffer_size, dtype='int32') ] # [plus,minus strand]
self.__pointer[chromosome] = [ 0, 0 ]
self.__locations[chromosome][strand][0] = fiveendpos
self.__pointer[chromosome][strand] = 1
else:
i = self.__pointer[chromosome][strand]
b = self.__buf_size[chromosome][strand]
arr = self.__locations[chromosome][strand]
if b == i:
b += self.buffer_size
arr.resize( b, refcheck = False )
self.__buf_size[chromosome][strand] = b
arr[i]= fiveendpos
self.__pointer[chromosome][strand] += 1
return
cpdef void finalize ( self ):
""" Resize np arrays for 5' positions and sort them in place
Note: If this function is called, it's impossible to append more files to this FWTrack object. So remember to call it after all the files are read!
"""
cdef:
int32_t i
bytes c
set chrnames
self.total = 0
chrnames = self.get_chr_names()
for c in chrnames:
self.__locations[c][0].resize( self.__pointer[c][0], refcheck=False )
self.__locations[c][0].sort()
self.__locations[c][1].resize( self.__pointer[c][1], refcheck=False )
self.__locations[c][1].sort()
self.total += self.__locations[c][0].size + self.__locations[c][1].size
self.__sorted = True
self.length = self.fw * self.total
return
cpdef bint set_rlengths ( self, dict rlengths ):
"""Set reference chromosome lengths dictionary.
Only the chromosome existing in this fwtrack object will be updated.
If chromosome in this fwtrack is not covered by given
rlengths, and it has no associated length, it will be set as
maximum integer.
"""
cdef:
set valid_chroms, missed_chroms, extra_chroms
bytes chrom
valid_chroms = set(self.__locations.keys()).intersection(rlengths.keys())
for chrom in sorted(valid_chroms):
self.rlengths[chrom] = rlengths[chrom]
missed_chroms = set(self.__locations.keys()).difference(rlengths.keys())
for chrom in sorted(missed_chroms):
self.rlengths[chrom] = INT_MAX
return True
cpdef dict get_rlengths ( self ):
"""Get reference chromosome lengths dictionary.
If self.rlength is empty, create a new dict where the length of
chromosome will be set as the maximum integer.
"""
if not self.rlengths:
self.rlengths = dict([(k, INT_MAX) for k in self.__locations.keys()])
return self.rlengths
cpdef get_locations_by_chr ( self, bytes chromosome ):
"""Return a tuple of two lists of locations for certain chromosome.
"""
if chromosome in self.__locations:
return self.__locations[chromosome]
else:
raise Exception("No such chromosome name (%s) in TrackI object!\n" % (chromosome))
cpdef set get_chr_names ( self ):
"""Return all the chromosome names stored in this track object.
"""
return set(sorted(self.__locations.keys()))
cpdef void sort ( self ):
"""Naive sorting for locations.
"""
cdef:
int32_t i
bytes c
set chrnames
chrnames = self.get_chr_names()
for c in chrnames:
self.__locations[c][0].sort()
self.__locations[c][1].sort()
self.__sorted = True
return
@cython.boundscheck(False) # do not check that np indices are valid
cpdef uint64_t filter_dup ( self, int32_t maxnum = -1):
"""Filter the duplicated reads.
Run it right after you add all data into this object.
Note, this function will *throw out* duplicates
permenantly. If you want to keep them, use separate_dups
instead.
"""
cdef:
int32_t p, m, n, current_loc
# index for old array, and index for new one
uint64_t i_old, i_new, size, new_size
bytes k
np.ndarray[int32_t, ndim=1] plus, new_plus, minus, new_minus
set chrnames
if maxnum < 0: return self.total # do nothing
if not self.__sorted:
self.sort()
self.total = 0
self.length = 0
chrnames = self.get_chr_names()
for k in chrnames:
# for each chromosome.
# This loop body is too big, I may need to split code later...
# + strand
i_new = 0
plus = self.__locations[k][0]
size = plus.shape[0]
if len(plus) <= 1:
new_plus = plus # do nothing
else:
new_plus = np.zeros( self.__pointer[k][0] + 1,dtype='int32' )
new_plus[ i_new ] = plus[ i_new ] # first item
i_new += 1
n = 1 # the number of tags in the current location
current_loc = plus[0]
for i_old in range( 1, size ):
p = plus[ i_old ]
if p == current_loc:
n += 1
else:
current_loc = p
n = 1
if n <= maxnum:
new_plus[ i_new ] = p
i_new += 1
new_plus.resize( i_new, refcheck=False )
self.total += i_new
self.__pointer[k][0] = i_new
# free memory?
# I know I should shrink it to 0 size directly,
# however, on Mac OSX, it seems directly assigning 0
# doesn't do a thing.
plus.resize( self.buffer_size, refcheck=False )
plus.resize( 0, refcheck=False )
# hope there would be no mem leak...
# - strand
i_new = 0
minus = self.__locations[k][1]
size = minus.shape[0]
if len(minus) <= 1:
new_minus = minus # do nothing
else:
new_minus = np.zeros( self.__pointer[k][1] + 1,dtype='int32' )
new_minus[ i_new ] = minus[ i_new ] # first item
i_new += 1
n = 1 # the number of tags in the current location
current_loc = minus[0]
for i_old in range( 1, size ):
p = minus[ i_old ]
if p == current_loc:
n += 1
else:
current_loc = p
n = 1
if n <= maxnum:
new_minus[ i_new ] = p
i_new += 1
new_minus.resize( i_new, refcheck=False )
self.total += i_new
self.__pointer[k][1] = i_new
# free memory ?
# I know I should shrink it to 0 size directly,
# however, on Mac OSX, it seems directly assigning 0
# doesn't do a thing.
minus.resize( self.buffer_size, refcheck=False )
minus.resize( 0, refcheck=False )
# hope there would be no mem leak...
self.__locations[k]=[new_plus,new_minus]
self.length = self.fw * self.total
return self.total
cpdef void sample_percent (self, float32_t percent, int32_t seed = -1 ):
"""Sample the tags for a given percentage.
Warning: the current object is changed!
"""
cdef:
int32_t num, i_chrom # num: number of reads allowed on a certain chromosome
bytes k
set chrnames
self.total = 0
self.length = 0
chrnames = self.get_chr_names()
if seed >= 0:
np.random.seed(seed)
for k in chrnames:
# for each chromosome.
# This loop body is too big, I may need to split code later...
num = <int32_t>round(self.__locations[k][0].shape[0] * percent, 5 )
np.random.shuffle( self.__locations[k][0] )
self.__locations[k][0].resize( num, refcheck=False )
self.__locations[k][0].sort()
self.__pointer[k][0] = self.__locations[k][0].shape[0]
num = <int32_t>round(self.__locations[k][1].shape[0] * percent, 5 )
np.random.shuffle( self.__locations[k][1] )
self.__locations[k][1].resize( num, refcheck=False )
self.__locations[k][1].sort()
self.__pointer[k][1] = self.__locations[k][1].shape[0]
self.total += self.__pointer[k][0] + self.__pointer[k][1]
self.length = self.fw * self.total
return
cpdef void sample_num (self, uint64_t samplesize, int32_t seed = -1):
"""Sample the tags for a given percentage.
Warning: the current object is changed!
"""
cdef:
float32_t percent
percent = <float32_t>(samplesize)/self.total
self.sample_percent ( percent, seed )
return
cpdef void print_to_bed (self, fhd=None):
"""Output FWTrack to BED format files. If fhd is given,
write to a file, otherwise, output to standard output.
"""
cdef:
int32_t i, i_chrom, p
bytes k
set chrnames
if not fhd:
fhd = sys.stdout
assert isinstance(fhd,io.IOBase)
assert self.fw > 0, "FWTrack object .fw should be set larger than 0!"
chrnames = self.get_chr_names()
for k in chrnames:
# for each chromosome.
# This loop body is too big, I may need to split code later...
plus = self.__locations[k][0]
for i in range(plus.shape[0]):
p = plus[i]
fhd.write("%s\t%d\t%d\t.\t.\t%s\n" % (k.decode(),p,p+self.fw,"+") )
minus = self.__locations[k][1]
for i in range(minus.shape[0]):
p = minus[i]
fhd.write("%s\t%d\t%d\t.\t.\t%s\n" % (k.decode(),p-self.fw,p,"-") )
return
cpdef tuple extract_region_tags ( self, bytes chromosome, int32_t startpos, int32_t endpos ):
cdef:
int32_t i, pos
np.ndarray[int32_t, ndim=1] rt_plus, rt_minus
list temp
set chrnames
if not self.__sorted: self.sort()
chrnames = self.get_chr_names()
assert chromosome in chrnames, "chromosome %s can't be found in the FWTrack object." % chromosome
(plus, minus) = self.__locations[chromosome]
temp = []
for i in range(plus.shape[0]):
pos = plus[i]
if pos < startpos:
continue
elif pos > endpos:
break
else:
temp.append(pos)
rt_plus = np.array(temp)
temp = []
for i in range(minus.shape[0]):
pos = minus[i]
if pos < startpos:
continue
elif pos > endpos:
break
else:
temp.append(pos)
rt_minus = np.array(temp)
return (rt_plus, rt_minus)
cpdef list compute_region_tags_from_peaks ( self, peaks, func, int32_t window_size = 100, float32_t cutoff = 5 ):
"""Extract tags in peak, then apply func on extracted tags.
peaks: redefined regions to extract raw tags in PeakIO type: check cPeakIO.pyx.
func: a function to compute *something* from tags found in a predefined region
window_size: this will be passed to func.
cutoff: this will be passed to func.
func needs the fixed number of parameters, so it's not flexible. Here is an example:
wtd_find_summit(chrom, plus, minus, peak_start, peak_end, name , window_size, cutoff):
"""
cdef:
int32_t m, i, j, pre_i, pre_j, pos, startpos, endpos
np.ndarray[int32_t, ndim=1] plus, minus, rt_plus, rt_minus
bytes chrom, name
list temp, retval
set pchrnames, chrnames
pchrnames = peaks.get_chr_names()
retval = []
# this object should be sorted
if not self.__sorted: self.sort()
# PeakIO object should be sorted
peaks.sort()
chrnames = self.get_chr_names()
for chrom in sorted(pchrnames):
assert chrom in chrnames, "chromosome %s can't be found in the FWTrack object." % chrom
(plus, minus) = self.__locations[chrom]
cpeaks = peaks.get_data_from_chrom(chrom)
prev_i = 0
prev_j = 0
for m in range(len(cpeaks)):
startpos = cpeaks[m]["start"] - window_size
endpos = cpeaks[m]["end"] + window_size
name = cpeaks[m]["name"]
temp = []
for i in range(prev_i,plus.shape[0]):
pos = plus[i]
if pos < startpos:
continue
elif pos > endpos:
prev_i = i
break
else:
temp.append(pos)
rt_plus = np.array(temp, dtype="int32")
temp = []
for j in range(prev_j,minus.shape[0]):
pos = minus[j]
if pos < startpos:
continue
elif pos > endpos:
prev_j = j
break
else:
temp.append(pos)
rt_minus = np.array(temp, dtype="int32")
retval.append( func(chrom, rt_plus, rt_minus, startpos, endpos, name = name, window_size = window_size, cutoff = cutoff) )
# rewind window_size
for i in range(prev_i, 0, -1):
if plus[prev_i] - plus[i] >= window_size:
break
prev_i = i
for j in range(prev_j, 0, -1):
if minus[prev_j] - minus[j] >= window_size:
break
prev_j = j
# end of a loop
return retval
cpdef list pileup_a_chromosome ( self, bytes chrom, list ds, list scale_factor_s, float32_t baseline_value = 0.0, bint directional = True, int32_t end_shift = 0 ):
"""pileup a certain chromosome, return [p,v] (end position and value) list.
ds : tag will be extended to this value to 3' direction,
unless directional is False. Can contain multiple extension
values. Final pileup will the maximum.
scale_factor_s : linearly scale the pileup value applied to each d in ds. The list should have the same length as ds.
baseline_value : a value to be filled for missing values, and will be the minimum pileup.
directional : if False, the strand or direction of tag will be ignored, so that extension will be both sides with d/2.
end_shift : move cutting ends towards 5->3 direction if value is positive, or towards 3->5 direction if negative. Default is 0 -- no shift at all.
p and v are numpy.ndarray objects.
"""
cdef:
int64_t d
int64_t five_shift, three_shift # adjustment to 5' end and 3' end positions to make a fragment
dict chrlengths = self.get_rlengths ()
int64_t rlength = chrlengths[chrom]
object ends
list five_shift_s = []
list three_shift_s = []
list tmp_pileup, prev_pileup
assert len(ds) == len(scale_factor_s), "ds and scale_factor_s must have the same length!"
# adjust extension length according to 'directional' and 'halfextension' setting.
for d in ds:
if directional:
# only extend to 3' side
five_shift_s.append( - end_shift )
three_shift_s.append( end_shift + d)
else:
# both sides
five_shift_s.append( d//2 - end_shift )
three_shift_s.append( end_shift + d - d//2)
prev_pileup = None
for i in range(len(ds)):
five_shift = five_shift_s[i]
three_shift = three_shift_s[i]
scale_factor = scale_factor_s[i]
tmp_pileup = se_all_in_one_pileup ( self.__locations[chrom][0], self.__locations[chrom][1], five_shift, three_shift, rlength, scale_factor, baseline_value )
if prev_pileup:
prev_pileup = over_two_pv_array ( prev_pileup, tmp_pileup, func="max" )
else:
prev_pileup = tmp_pileup
return prev_pileup
cdef inline int32_t left_sum ( data, int32_t pos, int32_t width ):
"""
"""
return sum([data[x] for x in data if x <= pos and x >= pos - width])
cdef inline int32_t right_sum ( data, int32_t pos, int32_t width ):
"""
"""
return sum([data[x] for x in data if x >= pos and x <= pos + width])
cdef inline int32_t left_forward ( data, int32_t pos, int32_t window_size ):
return data.get(pos,0) - data.get(pos-window_size, 0)
cdef inline int32_t right_forward ( data, int32_t pos, int32_t window_size ):
return data.get(pos + window_size, 0) - data.get(pos, 0)
|