[d7cf27]: / src / janggu / data / genomicarray.py

Download this file

1208 lines (1008 with data), 46.8 kB

   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
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
"""Genomic arrays"""
import hashlib
import os
from collections import OrderedDict
import h5py
import numpy as np
from pybedtools import Interval
from scipy import sparse
from janggu.utils import _get_output_data_location
from janggu.utils import _iv_to_str
from janggu.utils import _str_to_iv
def _get_iv_length(length, resolution):
"""obtain the chromosome length for a given resolution."""
if resolution is None:
return 1
return int(np.ceil(float(length)/resolution))
def get_collapser(method):
"""Get collapse method."""
if method is None:
return None
elif callable(method):
return method
elif method == 'mean':
return lambda x: x.mean(axis=1)
elif method == 'sum':
return lambda x: x.sum(axis=1)
elif method == 'max':
return lambda x: x.max(axis=1)
raise ValueError('Unknown method: {}'.format(method))
def create_sha256_cache(data, parameters):
"""Cache file determined from files and parameters."""
sha256_hash = hashlib.sha256()
# add file content to hash
for datum in data or []:
if isinstance(datum, str) and os.path.exists(datum):
with open(datum, 'rb') as file_:
for bblock in iter(lambda: file_.read(1024**2), b""):
sha256_hash.update(bblock)
elif isinstance(datum, np.ndarray):
sha256_hash.update(datum.tobytes())
else:
sha256_hash.update(str(datum).encode('utf-8'))
# add parameter settings to hash
sha256_hash.update(str(parameters).encode('utf-8'))
return sha256_hash.hexdigest()
def _get_cachefile(cachestr, tags, fileending):
""" Determine cache file location """
filename = None
if cachestr is not None:
memmap_dir = _get_output_data_location(tags)
if not os.path.exists(memmap_dir):
os.makedirs(memmap_dir)
filename = str(cachestr) + fileending
filename = os.path.join(memmap_dir, filename)
return filename
return None
def _load_data(cachestr, tags, fileending):
""" loading data from scratch or from cache """
filename = _get_cachefile(cachestr, tags, fileending)
if filename is not None and os.path.exists(filename):
return False
return True
class GenomicArray(object): # pylint: disable=too-many-instance-attributes
"""GenomicArray stores multi-dimensional genomic information.
It acts as a dataset for holding genomic data. For instance,
coverage along an entire genome composed of arbitrary length chromosomes
as well as for multiple cell-types and conditions simultaneously.
Inspired by the HTSeq analog, the array can hold the data in different
storage modes, including ndarray, hdf5 or as sparse dataset.
Parameters
----------
stranded : bool
Consider stranded profiles. Default: True.
conditions : list(str) or None
List of cell-type or condition labels associated with the corresponding
array dimensions. Default: None means a one-dimensional array is produced.
typecode : str
Datatype. Default: 'd'.
resolution : int or None
Resolution for storing the genomic array. Only relevant for the use
with Cover Datasets. Default: 1.
padding_value : float
Padding value. Default: 0.
order : int
Order of the alphabet size. Only relevant for Bioseq Datasets. Default: 1.
collapser : None or callable
Method to aggregate values along a given interval.
"""
handle = OrderedDict()
_condition = None
_resolution = None
_order = None
region2index = None
def __init__(self, stranded=True, conditions=None, typecode='d',
resolution=1, padding_value=0.,
order=1, store_whole_genome=True, collapser=None):
self.stranded = stranded
if conditions is None:
conditions = ['sample']
self.condition = conditions
self.order = order
self.padding_value = padding_value
if not isinstance(order, int) or order < 1:
raise Exception('order must be a positive integer')
self.resolution = resolution
self.typecode = typecode
self._full_genome_stored = store_whole_genome
self.collapser = collapser
def _get_indices(self, interval, arraylen):
"""Given the original genomic coordinates,
the array indices of the reference dataset (garray.handle)
and the array indices of the view are returned.
Parameters
----------
interval : Interval
Interval containing (chr, start, end)
arraylen : int
Length of the numpy target array.
Returns
-------
tuple
Tuple of indices corresponding to the slice in the arrays
(ref_start, ref_end, array_start, array_end).
ref_[start,end] indicate the slice in garray.handle
while array_[start,end] indicates the slice in the
target array / view.
"""
chrom = interval.chrom
start = self.get_iv_start(interval.start)
end = self.get_iv_end(interval.end) - self.order + 1
if start >= self.handle[chrom].shape[0]:
return 0, 0, 0, 0
else:
array_start = 0
ref_start = start
if end > self.handle[chrom].shape[0]:
array_end = arraylen - (end - self.handle[chrom].shape[0])
ref_end = self.handle[chrom].shape[0]
else:
array_end = arraylen
ref_end = end
return ref_start, ref_end, array_start, array_end
def __setitem__(self, index, value):
interval = index[0]
condition = index[1]
if isinstance(condition, slice) and value.ndim != 3:
raise ValueError('Expected 3D array with condition slice.')
if isinstance(condition, slice):
condition = slice(None, value.shape[-1], None)
if self.stranded and value.shape[1] != 2:
raise ValueError('If genomic array is in stranded mode, shape[-1] == 2 is expected')
if not self.stranded and value.shape[1] != 1:
value = value.sum(axis=1, keepdims=True)
if isinstance(interval, Interval) and isinstance(condition, (int, slice)):
start = self.get_iv_start(interval.start)
end = self.get_iv_end(interval.end)
length = end - start - self.order + 1
# value should be a 2 dimensional array
# it will be reshaped to a 2D array where the collapse operation is performed
# along the second dimension.
value = self._do_collapse(interval, value)
try:
self._setitem(interval, condition, length, value)
except KeyError:
# we end up here if the peak regions are not a subset of
# the regions of interest. that might be the case if
# peaks from the holdout proportion of the genome are tried
# to be added.
# unfortunately, it is also possible that store_whole_genome=False
# and the peaks and regions of interest are just not synchronized
# in which case nothing (or too few peaks) are added. in the latter
# case an error would help actually, but I am not sure how to
# check if the first or the second is the case here.
pass
else:
raise IndexError("Cannot interpret interval and condition: {}".format((interval, condition)))
def _setitem(self, interval, condition, length, value):
if not self._full_genome_stored:
idx = self.region2index[_iv_to_str(interval.chrom, interval.start,
interval.end)]
# correcting for the overshooting starts and ends is not necessary
# for partially loaded data
self.handle['data'][idx, :length, :, condition] = value
else:
ref_start, ref_end, array_start, \
array_end = self._get_indices(interval, value.shape[0])
self.handle[interval.chrom][ref_start:ref_end, :, condition] = \
value[array_start:array_end]
def _do_collapse(self, interval, value):
if self.collapser is not None:
if self.resolution is None and value.shape[0] == 1 or \
self.resolution is not None and \
value.shape[0] == interval.length//self.resolution:
# collapsing becomes obsolete, because the data has already
# the expected shape (after collapsing)
pass
else:
if self.resolution is None:
# collapse along the entire interval
value = value.reshape((1,) + value.shape)
else:
# if the array shape[0] is a multipe of resolution,
# it can simply be reshaped. otherwise,
# it needs to be resized before.
if value.shape[0] % self.resolution > 0:
value = np.resize(value, (
int(np.ceil(value.shape[0]/float(self.resolution))*self.resolution),) +
value.shape[1:])
# collapse in bins of size resolution
value = value.reshape((value.shape[0]//min(self.resolution,
value.shape[0]),
min(self.resolution, value.shape[0]),) + \
value.shape[1:])
value = self.collapser(value)
return value
def __getitem__(self, index):
# for now lets ignore everything except for chrom, start and end.
if isinstance(index, Interval):
interval = index
chrom = interval.chrom
start = self.get_iv_start(interval.start)
end = self.get_iv_end(interval.end)
# original length
length = end-start - self.order + 1
if not self._full_genome_stored:
idx = self.region2index[_iv_to_str(chrom, interval.start, interval.end)]
# correcting for the overshooting starts and ends is not necessary
# for partially loaded data
return self._reshape(self.handle['data'][idx],
(length, 2 if self.stranded else 1,
len(self.condition)))
if chrom not in self.handle:
return np.ones((length, 2 if self.stranded else 1,
len(self.condition)),
dtype=self.typecode) * self.padding_value
if start >= 0 and end <= self.handle[chrom].shape[0]:
end = end - self.order + 1
# this is a short-cut, which does not require zero-padding
return self._reshape(self.handle[chrom][start:end],
(end-start, 2 if self.stranded else 1,
len(self.condition)))
# below is some functionality for zero-padding, in case the region
# reaches out of the chromosome size
if self.padding_value == 0.0:
data = np.zeros((length, 2 if self.stranded else 1,
len(self.condition)),
dtype=self.typecode)
else:
data = np.ones((length, 2 if self.stranded else 1,
len(self.condition)),
dtype=self.typecode) * self.padding_value
ref_start, ref_end, array_start, array_end = self._get_indices(interval, data.shape[0])
data[array_start:array_end, :, :] = self._reshape(self.handle[chrom][ref_start:ref_end],
(ref_end - ref_start,
2 if self.stranded else 1,
len(self.condition)))
return data
raise IndexError("Cannot interpret interval: {}".format(index))
@property
def condition(self):
"""condition"""
return self._condition
@condition.setter
def condition(self, conditions):
self._condition = conditions
@property
def resolution(self):
"""resolution"""
return self._resolution
@resolution.setter
def resolution(self, value):
if value is not None and value <= 0:
raise ValueError('resolution>0 required')
self._resolution = value
def _reshape(self, data, shape):
# shape not necessary here,
# data should just fall through
return data
def interval_length(self, chrom):
"""Method returns the interval lengths."""
# extract the length by the interval length
# or by the array shape
locus = _str_to_iv(chrom)
if len(locus) > 1:
return locus[2] - locus[1]
return self.resolution
def scale_by_region_length(self):
""" This method scales the regions by the region length ."""
for chrom in self.handle:
if self._full_genome_stored:
self.handle[chrom][:] /= self.interval_length(chrom)
else:
for rstr, idx in self.region2index.items():
self.handle[chrom][idx] /= self.interval_length(rstr)
def weighted_mean(self):
""" Base pair resolution mean weighted by interval length
"""
# summing the signal
sums = [self.sum(chrom) for chrom in self.handle]
sums = np.asarray(sums).sum(axis=0)
# weights are determined by interval and chromosome length
weights = [np.prod(self.handle[chrom].shape[:-1]) \
for chrom in self.handle]
weights = np.asarray(weights).sum()
return sums / weights
def shift(self, means):
"""Centering the signal by the weighted mean"""
#means = self.weighted_mean()
for chrom in self.handle:
# adjust base pair resoltion mean to interval length
self.handle[chrom][:] -= means
def rescale(self, scale):
""" Method to rescale the signal """
for chrom in self.handle:
self.handle[chrom][:] /= scale
def sum(self, chrom=None):
"""Sum signal across chromosomes."""
if chrom is not None:
return self.handle[chrom][:]\
.sum(axis=tuple(range(self.handle[chrom].ndim - 1)))
return np.asarray([self.handle[chrom][:]\
.sum(axis=tuple(range(self.handle[chrom].ndim - 1)))
for chrom in self.handle])
def weighted_sd(self):
""" Interval scaled standard deviation """
# summing the squared signal signal
sums = [np.square(self.handle[chrom][:, :, :]).sum(
axis=tuple(range(self.handle[chrom].ndim - 1))) \
for chrom in self.handle]
sums = np.asarray(sums).sum(axis=0)
# weights are determined by interval and chromosome length
weights = [np.prod(self.handle[chrom].shape[:-1]) \
for chrom in self.handle]
weights = np.asarray(weights).sum()
return np.sqrt(sums / (weights - 1.))
@property
def order(self):
"""order"""
return self._order
@order.setter
def order(self, order):
if order <= 0:
raise ValueError('order>0 required')
self._order = order
def get_iv_end(self, end):
"""obtain the chromosome length for a given resolution."""
return _get_iv_length(end, self.resolution)
def get_iv_start(self, start):
"""obtain the chromosome length for a given resolution."""
if self.resolution is None:
return 0
return start // self.resolution
def init_with_padding_value(padding_value, shape, dtype):
""" create array with given padding value. """
if padding_value == 0.0:
return np.zeros(shape, dtype)
else:
return np.ones(shape, dtype) * padding_value
class HDF5GenomicArray(GenomicArray):
"""HDF5GenomicArray stores multi-dimensional genomic information.
Implements GenomicArray.
Parameters
----------
gsize : GenomicIndexer or callable
GenomicIndexer containing the genome sizes or a callable that
returns a GenomicIndexer to enable lazy loading.
stranded : bool
Consider stranded profiles. Default: True.
conditions : list(str) or None
List of cell-type or condition labels associated with the corresponding
array dimensions. Default: None means a one-dimensional array is produced.
typecode : str
Datatype. Default: 'd'.
datatags : list(str) or None
Tags describing the dataset. This is used to store the cache file.
resolution : int
Resolution for storing the genomic array. Only relevant for the use
with Cover Datasets. Default: 1.
order : int
Order of the alphabet size. Only relevant for Bioseq Datasets. Default: 1.
store_whole_genome : boolean
Whether to store the entire genome or only the regions of interest.
Default: True
padding_value : float
Padding value. Default: 0.
cache : str or None
Hash string of the data and parameters to cache the dataset. If None,
caching is deactivated. Default: None.
overwrite : boolean
Whether to overwrite the cache. Default: False
loader : callable or None
Function to be called for loading the genomic array.
collapser : None or callable
Method to aggregate values along a given interval.
verbose : boolean
Verbosity. Default: False
"""
def __init__(self, gsize, # pylint: disable=too-many-locals
stranded=True,
conditions=None,
typecode='d',
datatags=None,
resolution=1,
order=1,
padding_value=0.,
store_whole_genome=True,
cache=None,
overwrite=False, loader=None,
normalizer=None,
collapser=None,
verbose=False):
super(HDF5GenomicArray, self).__init__(stranded, conditions, typecode,
resolution,
order=order,
padding_value=padding_value,
store_whole_genome=store_whole_genome,
collapser=collapser)
if cache is None:
raise ValueError('cache=True required for HDF format')
gsize_ = None
if not store_whole_genome:
gsize_ = gsize() if callable(gsize) else gsize
self.region2index = {_iv_to_str(region.chrom,
region.start,
region.end): i \
for i, region in enumerate(gsize_)}
cachefile = _get_cachefile(cache, datatags, '.h5')
load_from_file = _load_data(cache, datatags, '.h5')
if load_from_file:
if gsize_ is None:
gsize_ = gsize() if callable(gsize) else gsize
h5file = h5py.File(cachefile, 'w')
if store_whole_genome:
for region in gsize_:
shape = (_get_iv_length(region.length - self.order + 1, self.resolution),
2 if stranded else 1, len(self.condition))
h5file.create_dataset(str(region.chrom), shape,
dtype=self.typecode,
data=init_with_padding_value(padding_value,
shape,
self.typecode))
self.handle = h5file
else:
shape = (len(gsize_),
_get_iv_length(gsize_.binsize + 2*gsize_.flank - self.order + 1,
self.resolution),
2 if stranded else 1, len(self.condition))
h5file.create_dataset('data', shape,
dtype=self.typecode,
data=init_with_padding_value(padding_value,
shape,
self.typecode))
self.handle = h5file
# invoke the loader
if loader:
loader(self)
for norm in normalizer or []:
get_normalizer(norm)(self)
h5file.close()
if verbose: print('reload {}'.format(cachefile))
h5file = h5py.File(cachefile, 'a', driver='stdio')
self.handle = h5file
class NPGenomicArray(GenomicArray):
"""NPGenomicArray stores multi-dimensional genomic information.
Implements GenomicArray.
Parameters
----------
gsize : GenomicIndexer or callable
GenomicIndexer containing the genome sizes or a callable that
returns a GenomicIndexer to enable lazy loading.
stranded : bool
Consider stranded profiles. Default: True.
conditions : list(str) or None
List of cell-type or condition labels associated with the corresponding
array dimensions. Default: None means a one-dimensional array is produced.
typecode : str
Datatype. Default: 'd'.
datatags : list(str) or None
Tags describing the dataset. This is used to store the cache file.
resolution : int
Resolution for storing the genomic array. Only relevant for the use
with Cover Datasets. Default: 1.
order : int
Order of the alphabet size. Only relevant for Bioseq Datasets. Default: 1.
store_whole_genome : boolean
Whether to store the entire genome or only the regions of interest.
Default: True
padding_value : float
Padding value. Default: 0.
cache : str or None
Hash string of the data and parameters to cache the dataset. If None,
caching is deactivated. Default: None.
overwrite : boolean
Whether to overwrite the cache. Default: False
loader : callable or None
Function to be called for loading the genomic array.
normalizer : callable or None
Normalization to be applied. This argumenet can be None,
if no normalization is applied, or a callable that takes
a garray and returns a normalized garray.
Default: None.
collapser : None or callable
Method to aggregate values along a given interval.
verbose : boolean
Verbosity. Default: False
"""
def __init__(self, gsize, # pylint: disable=too-many-locals
stranded=True,
conditions=None,
typecode='d',
datatags=None,
resolution=1,
order=1,
padding_value=0.0,
store_whole_genome=True,
cache=None,
overwrite=False, loader=None,
normalizer=None, collapser=None,
verbose=False):
super(NPGenomicArray, self).__init__(stranded, conditions, typecode,
resolution,
order=order,
padding_value=padding_value,
store_whole_genome=store_whole_genome,
collapser=collapser)
gsize_ = None
if not store_whole_genome:
gsize_ = gsize() if callable(gsize) else gsize
self.region2index = {_iv_to_str(region.chrom,
region.start,
region.end): i \
for i, region in enumerate(gsize_)}
cachefile = _get_cachefile(cache, datatags, '.npz')
load_from_file = _load_data(cache, datatags, '.npz')
if load_from_file:
if gsize_ is None:
gsize_ = gsize() if callable(gsize) else gsize
if store_whole_genome:
data = {str(region.chrom): init_with_padding_value(
padding_value,
shape=(_get_iv_length(region.length - self.order + 1,
self.resolution),
2 if stranded else 1,
len(self.condition)),
dtype=self.typecode) for region in gsize_}
names = [str(region.chrom) for region in gsize_]
self.handle = data
else:
data = {'data': init_with_padding_value(
padding_value,
shape=(len(gsize_),
_get_iv_length(gsize_.binsize + 2*gsize_.flank - self.order + 1,
self.resolution) if self.resolution is not None else 1,
2 if stranded else 1,
len(self.condition)),
dtype=self.typecode)}
names = ['data']
self.handle = data
# invoke the loader
if loader:
loader(self)
if cachefile is not None:
np.savez(cachefile, **data)
if cachefile is not None:
if verbose: print('reload {}'.format(cachefile))
data = np.load(cachefile)
names = [x for x in data]
# here we get either the freshly loaded data or the reloaded
# data from np.load.
self.handle = {key: data[key] for key in names}
for norm in normalizer or []:
get_normalizer(norm)(self)
class SparseGenomicArray(GenomicArray):
"""SparseGenomicArray stores multi-dimensional genomic information.
Implements GenomicArray.
Parameters
----------
gsize : GenomicIndexer or callable
GenomicIndexer containing the genome sizes or a callable that
returns a GenomicIndexer to enable lazy loading.
stranded : bool
Consider stranded profiles. Default: True.
conditions : list(str) or None
List of cell-type or condition labels associated with the corresponding
array dimensions. Default: None means a one-dimensional array is produced.
typecode : str
Datatype. Default: 'd'.
datatags : list(str) or None
Tags describing the dataset. This is used to store the cache file.
resolution : int
Resolution for storing the genomic array. Only relevant for the use
with Cover Datasets. Default: 1.
order : int
Order of the alphabet size. Only relevant for Bioseq Datasets. Default: 1.
store_whole_genome : boolean
Whether to store the entire genome or only the regions of interest.
Default: True
padding_value : float
Padding value. Default: 0.
cache : str or None
Hash string of the data and parameters to cache the dataset. If None,
caching is deactivated. Default: None.
overwrite : boolean
Whether to overwrite the cache. Default: False
loader : callable or None
Function to be called for loading the genomic array.
normalizer : callable or None
Normalization to be applied. This argumenet can be None,
if no normalization is applied, or a callable that takes
a garray and returns a normalized garray.
Default: None.
collapser : None or callable
Method to aggregate values along a given interval.
verbose : boolean
Verbosity. Default: False
"""
def __init__(self, gsize, # pylint: disable=too-many-locals
stranded=True,
conditions=None,
typecode='d',
datatags=None,
resolution=1,
order=1,
store_whole_genome=True,
cache=None,
padding_value=0.0,
overwrite=False,
loader=None,
collapser=None,
verbose=False):
super(SparseGenomicArray, self).__init__(stranded, conditions,
typecode,
resolution,
order=order,
padding_value=padding_value,
store_whole_genome=store_whole_genome,
collapser=collapser)
cachefile = _get_cachefile(cache, datatags, '.npz')
load_from_file = _load_data(cache, datatags, '.npz')
gsize_ = None
if not store_whole_genome:
gsize_ = gsize() if callable(gsize) else gsize
self.region2index = {_iv_to_str(region.chrom,
region.start,
region.end): i \
for i, region in enumerate(gsize_)}
if load_from_file:
if gsize_ is None:
gsize_ = gsize() if callable(gsize) else gsize
if store_whole_genome:
data = {str(region.chrom): sparse.dok_matrix(
(_get_iv_length(region.length - self.order + 1,
resolution),
(2 if stranded else 1) * len(self.condition)),
dtype=self.typecode)
for region in gsize_}
else:
data = {'data': sparse.dok_matrix(
(len(gsize_),
(_get_iv_length(gsize_.binsize + 2*gsize_.flank - self.order + 1,
self.resolution) if self.resolution is not None else 1) *
(2 if stranded else 1) * len(self.condition)),
dtype=self.typecode)}
self.handle = data
# invoke the loader
if loader:
loader(self)
data = self.handle
data = {chrom: data[chrom].tocoo() for chrom in data}
storage = {chrom: np.column_stack([data[chrom].data,
data[chrom].row,
data[chrom].col]) \
for chrom in data}
for region in gsize_:
if store_whole_genome:
storage[region.chrom + '__length__'] = region.length
names = [name for name in storage]
if cachefile is not None:
np.savez(cachefile, **storage)
if cachefile is not None:
if verbose: print('reload {}'.format(cachefile))
storage = np.load(cachefile)
names = [name for name in storage if '__length__' not in name]
if store_whole_genome:
self.handle = {name: sparse.coo_matrix(
(storage[name][:, 0],
(storage[name][:, 1].astype('int'),
storage[name][:, 2].astype('int'))),
shape=(_get_iv_length(storage[str(name)+'__length__'], resolution),
(2 if stranded else 1) * len(self.condition))).tocsr()
for name in names}
else:
# gsize_ is always available for store_whole_genome=False
self.handle = {name: sparse.coo_matrix(
(storage[name][:, 0],
(storage[name][:, 1].astype('int'),
storage[name][:, 2].astype('int'))),
shape=(len(gsize_),
(_get_iv_length(gsize_.binsize + 2*gsize_.flank, resolution)
if self.resolution is not None else 1) *
(2 if stranded else 1) * len(self.condition))).tocsr()
for name in names}
def _reshape(self, data, shape):
# what to do with zero padding
data = data.toarray()
if self._full_genome_stored:
return data.reshape(data.shape[0], data.shape[1]//(shape[-1]), shape[-1])
else:
return data.reshape(data.shape[1]//(shape[-2]*shape[-1]), shape[-2], shape[-1])
def _setitem(self, interval, condition, length, value):
if not self._full_genome_stored:
regidx = self.region2index[_iv_to_str(interval.chrom, interval.start, interval.end)]
nconditions = len(self.condition)
ncondstrand = len(self.condition) * value.shape[-1]
#end = end - self.order + 1
idxs = np.where(value > 0)
for idx in zip(*idxs):
basepos = idx[0] * ncondstrand
strand = idx[1] * nconditions
cond = condition if isinstance(condition, int) else idx[2]
self.handle['data'][regidx,
basepos + strand + cond] = value[idx]
else:
ref_start, ref_end, array_start, _ = self._get_indices(interval, value.shape[0])
idxs = np.where(value > 0)
iarray = np.arange(ref_start, ref_end)
for idx in zip(*idxs):
cond = condition if isinstance(condition, int) else idx[2]
self.handle[interval.chrom][iarray[idx[0]],
idx[1] * len(self.condition)
+ cond] = value[idx[0] + array_start][idx[1:]]
class PercentileTrimming(object):
"""Percentile trimming normalization.
This class performs percentile trimming of a GenomicArray to aleviate
the effect of outliers.
All values that exceed the value associated with the given percentile
are set to be equal to the percentile.
Parameters
----------
percentile : float
Percentile at which to perform chromosome-level trimming.
"""
def __init__(self, percentile):
self.percentile = percentile
def __call__(self, garray):
quants = np.percentile(np.concatenate(np.asarray([garray.handle[chrom] for \
chrom in garray.handle]), axis=0),
self.percentile, axis=(0, 1))
for icond, quant in enumerate(quants):
for chrom in garray.handle:
arr = garray.handle[chrom][:, :, icond]
arr[arr > quant] = quant
garray.handle[chrom][:, :, icond] = arr
return garray
def __str__(self): # pragma: no cover
return 'PercentileTrimming({})'.format(self.percentile)
def __repr__(self): # pragma: no cover
return str(self)
class RegionLengthNormalization(object):
""" Normalization for variable-region length.
This class performs region length normalization of a GenomicArray.
This is relevant when genomic features are of variable size, e.g.
enhancer regions of different width or when using variable length genes.
Parameters
----------
regionmask : str or GenomicIndexer, None
A bed file or a genomic indexer that contains the masking region
that is considered for the signal. For instance, when normalizing
gene expression to TPM, the mask contains exons. Otherwise, the
TPM would normalize for the full length gene annotation.
If None, no mask is included.
"""
def __init__(self, regionmask=None):
self.regionmask = regionmask
def __call__(self, garray):
# length scaling
garray.scale_by_region_length()
return garray
def __str__(self): # pragma: no cover
return 'RegionLengthNormalization({})'.format(self.regionmask)
def __repr__(self): # pragma: no cover
return str(self)
class ZScore(object):
"""ZScore normalization.
This class performs ZScore normalization of a GenomicArray.
It automatically adjusts for variable interval lenths.
Parameters
----------
means : float or None
Provided means will be applied for zero-centering.
If None, the means will be determined
from the GenomicArray and then applied.
Default: None.
stds : float or None
Provided standard deviations will be applied for scaling.
If None, the stds will be determined
from the GenomicArray and then applied.
Default: None.
"""
def __init__(self, mean=None, std=None):
self.mean = mean
self.std = std
def __call__(self, garray):
# determine the mean signal per condition
if self.mean is None:
self.mean = garray.weighted_mean()
# centering to zero-mean
garray.shift(self.mean)
# determines standard deviation per contition
if self.std is None:
self.std = garray.weighted_sd()
# rescale by standard deviation
garray.rescale(self.std)
return garray
def __str__(self): # pragma: no cover
return 'ZScore({},{})'.format(self.mean, self.std)
def __repr__(self): # pragma: no cover
return str(self)
class LogTransform(object):
"""Log transformation of intput signal.
This class performs log-transformation
of a GenomicArray using log(x + 1.) to avoid NAN's from zeros.
"""
def __init__(self):
pass
def __call__(self, garray):
for chrom in garray.handle:
garray.handle[chrom][:] = np.log(garray.handle[chrom][:] + 1.)
return garray
def __str__(self): # pragma: no cover
return 'Log'
def __repr__(self): # pragma: no cover
return str(self)
class ZScoreLog(object):
"""ZScore normalization after log transformation.
This class performs ZScore normalization after log-transformation
of a GenomicArray using log(x + 1.) to avoid NAN's from zeros.
It automatically adjusts for variable interval lenths.
Parameters
----------
means : float or None
Provided means will be applied for zero-centering.
If None, the means will be determined
from the GenomicArray and then applied.
Default: None.
stds : float or None
Provided standard deviations will be applied for scaling.
If None, the stds will be determined
from the GenomicArray and then applied.
Default: None.
"""
def __init__(self, mean=None, std=None):
self.logtr = LogTransform()
self.zscore = ZScore(mean, std)
def __call__(self, garray):
return self.zscore(self.logtr(garray))
def __str__(self): # pragma: no cover
return str(self.zscore) + str(self.logtr)
def __repr__(self): # pragma: no cover
return str(self)
def normalize_garray_tpm(garray):
"""This function performs TPM normalization
for a given GenomicArray.
"""
# rescale by region lengths in bp
garray = RegionLengthNormalization()(garray)
# recale to kb
garray.rescale(1e-3)
# compute scaling factor
scale = garray.sum() # per chromsome sum
scale = scale.sum(axis=0) # sum across chroms
scale /= 1e6 # rescale by million
# divide by scaling factor
garray.rescale(scale)
return garray
def get_normalizer(normalizer):
""" maps built-in normalizers by name and
returns the respective function """
if callable(normalizer):
return normalizer
elif normalizer == 'zscore':
return ZScore()
elif normalizer == 'zscorelog':
return ZScoreLog()
elif normalizer == 'binsizenorm':
return RegionLengthNormalization()
elif normalizer == 'perctrim':
return PercentileTrimming(99)
elif normalizer == 'tpm':
return normalize_garray_tpm
raise ValueError('unknown normalizer: {}'.format(normalizer))
def create_genomic_array(chroms, stranded=True, conditions=None, typecode='float32',
storage='hdf5', resolution=1,
order=1,
padding_value=0.0,
store_whole_genome=True,
datatags=None, cache=None, overwrite=False,
loader=None,
normalizer=None, collapser=None,
verbose=False):
"""Factory function for creating a GenomicArray.
This function creates a genomic array for a given storage mode.
Parameters
----------
chroms : dict
Dictionary with chromosome names as keys and chromosome lengths
as values.
stranded : bool
Consider stranded profiles. Default: True.
conditions : list(str) or None
List of cell-type or condition labels associated with the corresponding
array dimensions. Default: None means a one-dimensional array is produced.
typecode : str
Datatype. Default: 'float32'.
storage : str
Storage type can be 'ndarray', 'hdf5' or 'sparse'.
Numpy loads the entire dataset into the memory. HDF5 keeps
the data on disk and loads the mini-batches from disk.
Sparse maintains sparse matrix representation of the dataset
in the memory.
Usage of numpy will require high memory consumption, but allows fast
slicing operations on the dataset. HDF5 requires low memory consumption,
but fetching the data from disk might be time consuming.
sparse will be a good compromise if the data is indeed sparse. In this
case, memory consumption will be low while slicing will still be fast.
datatags : list(str) or None
Tags describing the dataset. This is used to store the cache file.
resolution : int
Resolution for storing the genomic array. Only relevant for the use
with Cover Datasets. Default: 1.
order : int
Order of the alphabet size. Only relevant for Bioseq Datasets. Default: 1.
padding_value : float
Padding value. Default: 0.0
store_whole_genome : boolean
Whether to store the entire genome or only the regions of interest.
Default: True
cache : str or None
Hash string of the data and parameters to cache the dataset. If None,
caching is deactivated. Default: None.
overwrite : boolean
Whether to overwrite the cache. Default: False
loader : callable or None
Function to be called for loading the genomic array.
normalizer : callable or None
Normalization to be applied. This argumenet can be None,
if no normalization is applied, or a callable that takes
a garray and returns a normalized garray.
Default: None.
collapser : str, callable or None
Collapse method defines how the signal is aggregated for resolution>1 or resolution=None.
For example, by summing the signal over a given interval.
verbose : boolean
Verbosity. Default: False
"""
# check if collapser available
if (resolution is None or resolution > 1) and collapser is None:
raise ValueError('Requiring collapser due to resolution=None or resolution>1')
# force store_whole_genome=False if resolution=None
if resolution is None and store_whole_genome:
print('store_whole_genome=True ignored, because it is not compatible'
'with resolution=None. store_whole_genome=False is used instead.')
store_whole_genome = False
if storage == 'hdf5':
return HDF5GenomicArray(chroms, stranded=stranded,
conditions=conditions,
typecode=typecode,
datatags=datatags,
resolution=resolution,
order=order,
store_whole_genome=store_whole_genome,
cache=cache,
padding_value=padding_value,
overwrite=overwrite,
loader=loader,
normalizer=normalizer,
collapser=get_collapser(collapser),
verbose=verbose)
elif storage == 'ndarray':
return NPGenomicArray(chroms, stranded=stranded,
conditions=conditions,
typecode=typecode,
datatags=datatags,
resolution=resolution,
order=order,
store_whole_genome=store_whole_genome,
cache=cache,
padding_value=padding_value,
overwrite=overwrite,
loader=loader,
normalizer=normalizer,
collapser=get_collapser(collapser),
verbose=verbose)
elif storage == 'sparse':
return SparseGenomicArray(chroms, stranded=stranded,
conditions=conditions,
typecode=typecode,
datatags=datatags,
resolution=resolution,
order=order,
store_whole_genome=store_whole_genome,
cache=cache,
padding_value=padding_value,
overwrite=overwrite,
loader=loader,
collapser=get_collapser(collapser),
verbose=verbose)
raise Exception("Storage type must be 'hdf5', 'ndarray' or 'sparse'")