-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselene_utils2.py
1307 lines (1166 loc) · 52.9 KB
/
selene_utils2.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
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
"""
This module provides the selene-based utilities for training and using
Orca sequence models for multiscale genome interaction prediction. This
module contains code from selene.
"""
import os
from collections import namedtuple
import sys
import pkg_resources
from functools import wraps
import pandas as pd
import numpy as np
import pyfaidx
from cooltools.lib.numutils import adaptive_coarsegrain
import cooler
import pyranges
import torch
from torch.utils.data import DataLoader
import torch.utils.data as data
from selene_sdk.sequences import Genome
from selene_sdk.samplers import OnlineSampler
from selene_sdk.utils import get_indices_and_probabilities
from selene_sdk.targets import Target
SampleIndices = namedtuple("SampleIndices", ["indices", "weights"])
import random
import tabix
class MemmapGenome(Genome):
"""
Memmapped version of selene.sequence.Genome. Faster for sequence
retrieval by storing all precomputed one-hot encodings in a memmapped
file (~40G for human genome).
The memmapfile can be an exisiting memmapped file or a path where you
want to create the memmapfile. If the specified memmapfile does not
exist, it will be created the first time you call any method of
MemmapGenome or if MemmapGenome is initialized with `init_unpickable=True`.
Therefore the first call will take some time for the
creation of memmapfile if it does not exist. Also, if
memmapfile has not been created, be careful not to run multiple
instances of MemmapGenome in parallel (such as with Dataloader),
because as each process will try to create the file.
Parameters
----------
input_path : str
Path to an indexed FASTA file, that is, a `*.fasta` file with
a corresponding `*.fai` file in the same directory. This file
should contain the target organism's genome sequence.
init_unpickleable : bool, optional
Default is False. If False, delay part of initialization code
to executed only when a relevant method is called. This enables
the object to be pickled after instantiation. `init_unpickleable` should
be `False` when used when multi-processing is needed e.g. DataLoader.
memmapfile : str or None, optional
Specify the numpy.memmap file for storing the encoding
of the genome. If memmapfile does not exist, it will be
created when the encoding is requested for the first time.
Attributes
----------
genome : pyfaidx.Fasta
The FASTA file containing the genome sequence.
chrs : list(str)
The list of chromosome names.
len_chrs : dict
A dictionary mapping the names of each chromosome in the file to
the length of said chromosome.
"""
def __init__(
self,
input_path,
blacklist_regions=None,
bases_order=None,
init_unpicklable=False,
memmapfile=None,
):
super().__init__(
input_path, blacklist_regions=blacklist_regions, bases_order=bases_order,
)
self.memmapfile = memmapfile
if init_unpicklable:
self._unpicklable_init()
def _unpicklable_init(self):
if not self.initialized:
self.genome = pyfaidx.Fasta(self.input_path)
self.chrs = sorted(self.genome.keys())
self.len_chrs = self._get_len_chrs()
self._blacklist_tabix = None
if self.blacklist_regions == "hg19":
self._blacklist_tabix = tabix.open(
pkg_resources.resource_filename(
"selene_sdk", "sequences/data/hg19_blacklist_ENCFF001TDO.bed.gz"
)
)
elif self.blacklist_regions == "hg38":
self._blacklist_tabix = tabix.open(
pkg_resources.resource_filename(
"selene_sdk", "sequences/data/hg38.blacklist.bed.gz"
)
)
elif self.blacklist_regions is not None: # user-specified file
self._blacklist_tabix = tabix.open(self.blacklist_regions)
self.lens = np.array([self.len_chrs[c] for c in self.chrs])
self.inds = {
c: ind for c, ind in zip(self.chrs, np.concatenate([[0], np.cumsum(self.lens)]))
}
if self.memmapfile is not None and os.path.isfile(self.memmapfile):
# load memmap file
self.sequence_data = np.memmap(self.memmapfile, dtype="float32", mode="r")
self.sequence_data = np.reshape(
self.sequence_data, (4, int(self.sequence_data.shape[0] / 4))
)
else:
# convert all sequences into encoding
self.sequence_data = np.zeros((4, self.lens.sum()), dtype=np.float32)
for c in self.chrs:
print("Converting " + c + " to encoding...")
sequence = self.genome[c][:].seq
encoding = self.sequence_to_encoding(sequence)
self.sequence_data[
:, self.inds[c] : self.inds[c] + self.len_chrs[c]
] = encoding.T
if self.memmapfile is not None:
# create memmap file
print("Creating memmap...\n" +
"This may take a while (e.g. ~hours for human genome).\n" +
"If the process is interrupted or killed, the .mmap file will be incorrect,\n" +
"in which case, delete the mmap file and try again."
)
mmap = np.memmap(
self.memmapfile, dtype="float32", mode="w+", shape=self.sequence_data.shape
)
mmap[:] = self.sequence_data
self.sequence_data = np.memmap(
self.memmapfile, dtype="float32", mode="r", shape=self.sequence_data.shape
)
self.initialized = True
def init(func):
# delay initlization to allow multiprocessing
@wraps(func)
def dfunc(self, *args, **kwargs):
self._unpicklable_init()
return func(self, *args, **kwargs)
return dfunc
@init
def get_encoding_from_coords(self, chrom, start, end, strand="+", pad=False):
"""
Gets the one-hot encoding of the genomic sequence at the
queried coordinates.
Parameters
----------
chrom : str
The name of the chromosome or region, e.g. "chr1".
start : int
The 0-based start coordinate of the first position in the
sequence.
end : int
One past the 0-based last position in the sequence.
strand : {'+', '-', '.'}, optional
Default is '+'. The strand the sequence is located on. '.' is
treated as '+'.
pad : bool, optional
Default is `False`. Pad the output sequence with 'N' if `start`
and/or `end` are out of bounds to return a sequence of length
`end - start`.
Returns
-------
numpy.ndarray, dtype=numpy.float32
The :math:`L \\times 4` encoding of the sequence, where
:math:`L = end - start`.
Raises
------
AssertionError
If it cannot retrieve encoding that matches the length `L = end - start`
such as when end > chromosome length and pad=False
"""
if pad:
# padding with 0.25 if coordinates extend beyond chr boundary
if end > self.len_chrs[chrom]:
pad_right = end - self.len_chrs[chrom]
qend = self.len_chrs[chrom]
else:
qend = end
pad_right = 0
if start < 0:
pad_left = 0 - start
qstart = 0
else:
pad_left = 0
qstart = start
encoding = np.hstack(
[
np.ones((4, pad_left)) * 0.25,
self.sequence_data[:, self.inds[chrom] + qstart : self.inds[chrom] + qend],
np.ones((4, pad_right)) * 0.25,
]
)
else:
assert end <= self.len_chrs[chrom] and start >= 0
encoding = self.sequence_data[:, self.inds[chrom] + start : self.inds[chrom] + end]
if strand == "-":
encoding = encoding[::-1, ::-1]
assert encoding.shape[1] == end - start
return encoding.T
@init
def get_encoding_from_coords_check_unk(self, chrom, start, end, strand="+", pad=False):
"""Gets the one-hot encoding of the genomic sequence at the
queried coordinates and check whether the sequence contains
unknown base(s).
Parameters
----------
chrom : str
The name of the chromosome or region, e.g. "chr1".
start : int
The 0-based start coordinate of the first position in the
sequence.
end : int
One past the 0-based last position in the sequence.
strand : {'+', '-', '.'}, optional
Default is '+'. The strand the sequence is located on. '.' is
treated as '+'.
pad : bool, optional
Default is `False`. Pad the output sequence with 'N' if `start`
and/or `end` are out of bounds to return a sequence of length
`end - start`.
Returns
-------
tuple(numpy.ndarray, bool)
* `tuple[0]` is the :math:`L \\times 4` encoding of the sequence, where
:math:`L = end - start`.
`L` = 0 for the NumPy array returned.
* `tuple[1]` is the boolean value that indicates whether the
sequence contains any unknown base(s) specified in self.UNK_BASE
Raises
------
AssertionError
If it cannot retrieve encoding that matches the length `L = end - start`
such as when end > chromosome length and pad=False
"""
encoding = self.get_encoding_from_coords(chrom, start, end, strand=strand, pad=strand)
return encoding, np.any(encoding[0, :] == 0.25)
def adaptive_coarsegrain_gpu(ar, countar, cutoff=5, max_levels=8, min_shape=8):
"""
Adaptively coarsegrain a Hi-C matrix based on local neighborhood pooling
of counts.
Parameters
----------
ar : torch.Tensor, shape (n, n)
A square Hi-C matrix to coarsegrain. Usually this would be a balanced
matrix.
countar : torch.Tensor, shape (n, n)
The raw count matrix for the same area. Has to be the same shape as the
Hi-C matrix.
cutoff : float, optional
A minimum number of raw counts per pixel required to stop 2x2 pooling.
Larger cutoff values would lead to a more coarse-grained, but smoother
map. 3 is a good default value for display purposes, could be lowered
to 1 or 2 to make the map less pixelated. Setting it to 1 will only
ensure there are no zeros in the map.
max_levels : int, optional
How many levels of coarsening to perform. It is safe to keep this
number large as very coarsened map will have large counts and no
substitutions would be made at coarser levels.
min_shape : int, optional
Stop coarsegraining when coarsegrained array shape is less than that.
Returns
-------
Smoothed array, shape (n, n)
Notes
-----
The algorithm works as follows:
First, it pads an array with NaNs to the nearest power of two. Second, it
coarsens the array in powers of two until the size is less than minshape.
Third, it starts with the most coarsened array, and goes one level up.
It looks at all 4 pixels that make each pixel in the second-to-last
coarsened array. If the raw counts for any valid (non-NaN) pixel are less
than ``cutoff``, it replaces the values of the valid (4 or less) pixels
with the NaN-aware average. It is then applied to the next
(less coarsened) level until it reaches the original resolution.
In the resulting matrix, there are guaranteed to be no zeros, unless very
large zero-only areas were provided such that zeros were produced
``max_levels`` times when coarsening.
Examples
--------
>>> c = cooler.Cooler("/path/to/some/cooler/at/about/2000bp/resolution")
>>> # sample region of about 6000x6000
>>> mat = c.matrix(balance=True).fetch("chr1:10000000-22000000")
>>> mat_raw = c.matrix(balance=False).fetch("chr1:10000000-22000000")
>>> mat_cg = adaptive_coarsegrain(mat, mat_raw)
>>> plt.figure(figsize=(16,7))
>>> ax = plt.subplot(121)
>>> plt.imshow(np.log(mat), vmax=-3)
>>> plt.colorbar()
>>> plt.subplot(122, sharex=ax, sharey=ax)
>>> plt.imshow(np.log(mat_cg), vmax=-3)
>>> plt.colorbar()
"""
#TODO: do this better without sideeffect
torch.set_default_tensor_type(torch.cuda.FloatTensor)
with torch.no_grad():
def _coarsen(ar, operation=torch.sum, min_nan=False):
"""Coarsegrains an array by a factor of 2"""
M = ar.shape[0] // 2
newar = ar.reshape(M, 2, M, 2)
if min_nan:
newar = torch.nan_to_num(newar,nan=float('inf'))
cg = operation(newar, axis=1)[0]
cg = operation(cg, axis=2)[0]
else:
cg = operation(newar, axis=1)
cg = operation(cg, axis=2)
return cg
def _expand(ar, counts=None):
"""
Performs an inverse of nancoarsen
"""
N = ar.shape[0] * 2
newar = torch.zeros((N, N),dtype=ar.dtype)
newar[::2, ::2] = ar
newar[1::2, ::2] = ar
newar[::2, 1::2] = ar
newar[1::2, 1::2] = ar
return newar
# defining arrays, making sure they are floats
# ar = np.asarray(ar, float)
# ar = torch.from_numpy(ar)
# countar = np.asarray(countar, float)
# countar = torch.from_numpy(countar)
# TODO: change this to the nearest shape correctly counting the smallest
# shape the algorithm will reach
Norig = ar.shape[0]
Nlog = np.log2(Norig)
if not np.allclose(Nlog, np.rint(Nlog)):
newN = np.int(2 ** np.ceil(Nlog)) # next power-of-two sized matrix
newar = torch.empty((newN, newN), dtype=torch.float) # fitting things in there
newar[:] = np.nan
newcountar = torch.zeros((newN, newN), dtype=torch.float)
newar[:Norig, :Norig] = torch.from_numpy(ar)
newcountar[:Norig, :Norig] = torch.from_numpy(countar)
ar = newar
countar = newcountar
armask = torch.isfinite(ar) # mask of "valid" elements
countar[~armask] = 0
ar[~armask] = 0
assert torch.isfinite(countar).all()
assert countar.shape == ar.shape
# We will be working with three arrays.
ar_cg = [ar] # actual Hi-C data
countar_cg = [countar] # counts contributing to Hi-C data (raw Hi-C reads)
armask_cg = [armask] # mask of "valid" pixels of the heatmap
# 1. Forward pass: coarsegrain all 3 arrays
for i in range(max_levels):
if countar_cg[-1].shape[0] > min_shape:
countar_cg.append(_coarsen(countar_cg[-1]))
armask_cg.append(_coarsen(armask_cg[-1]))
ar_cg.append(_coarsen(ar_cg[-1]))
# Get the most coarsegrained array
ar_cur = ar_cg.pop()
countar_cur = countar_cg.pop()
armask_cur = armask_cg.pop()
# 2. Reverse pass: replace values starting with most coarsegrained array
# We have 4 pixels that were coarsegrained to one pixel.
# Let V be the array of values (ar), and C be the array of counts of
# valid pixels. Then the coarsegrained values and valid pixel counts
# are:
# V_{cg} = V_{0,0} + V_{0,1} + V_{1,0} + V_{1,1}
# C_{cg} = C_{0,0} + C_{0,1} + C_{1,0} + C_{1,1}
# The average value at the coarser level is V_{cg} / C_{cg}
# The average value at the finer level is V_{0,0} / C_{0,0}, etc.
#
# We would replace 4 values with the average if counts for either of the
# 4 values are less than cutoff. To this end, we perform nanmin of raw
# Hi-C counts in each 4 pixels
# Because if counts are 0 due to this pixel being invalid - it's fine.
# But if they are 0 in a valid pixel - we replace this pixel.
# If we decide to replace the current 2x2 square with coarsegrained
# values, we need to make it produce the same average value
# To this end, we would replace V_{0,0} with V_{cg} * C_{0,0} / C_{cg} and
# so on.
for i in range(len(countar_cg)):
ar_next = ar_cg.pop()
countar_next = countar_cg.pop()
armask_next = armask_cg.pop()
# obtain current "average" value by dividing sum by the # of valid pixels
val_cur = ar_cur / armask_cur
# expand it so that it is the same shape as the previous level
val_exp = _expand(val_cur)
# create array of substitutions: multiply average value by counts
addar_exp = val_exp * armask_next
# make a copy of the raw Hi-C array at current level
countar_next_mask = countar_next.clone()
countar_next_mask[armask_next == 0] = np.nan # fill nans
countar_exp = _expand(_coarsen(countar_next, operation=torch.min,min_nan=True))
curmask = countar_exp < cutoff # replacement mask
ar_next[curmask] = addar_exp[curmask] # procedure of replacement
ar_next[armask_next == 0] = 0 # now setting zeros at invalid pixels
# prepare for the next level
ar_cur = ar_next
countar_cur = countar_next
armask_cur = armask_next
ar_next[armask_next == 0] = np.nan
ar_next = ar_next[:Norig, :Norig]
torch.set_default_tensor_type(torch.FloatTensor)
return ar_next.detach().cpu().numpy()
def _adaptive_coarsegrain(ar, countar, max_levels=12, cuda=False):
"""
Wrapper for cooltools adaptive coarse-graining to add support
for non-square input for interchromosomal predictions.
"""
global adaptive_coarsegrain_fn
if cuda:
adaptive_coarsegrain_fn = adaptive_coarsegrain_gpu
else:
adaptive_coarsegrain_fn = adaptive_coarsegrain
assert np.all(ar.shape == countar.shape)
if ar.shape[0] < 9 and ar.shape[1] < 9:
ar_padded = np.empty((9, 9))
ar_padded.fill(np.nan)
ar_padded[: ar.shape[0], : ar.shape[1]] = ar
countar_padded = np.empty((9, 9))
countar_padded.fill(np.nan)
countar_padded[: countar.shape[0], : countar.shape[1]] = countar
return adaptive_coarsegrain_fn(ar_padded, countar_padded, max_levels=max_levels)[
: ar.shape[0], : ar.shape[1]
]
if ar.shape[0] == ar.shape[1]:
return adaptive_coarsegrain_fn(ar, countar, max_levels=max_levels)
elif ar.shape[0] > ar.shape[1]:
padding = np.empty((ar.shape[0], ar.shape[0] - ar.shape[1]))
padding.fill(np.nan)
return adaptive_coarsegrain_fn(
np.hstack([ar, padding]), np.hstack([countar, padding]), max_levels=max_levels
)[:, : ar.shape[1]]
elif ar.shape[0] < ar.shape[1]:
padding = np.empty((ar.shape[1] - ar.shape[0], ar.shape[1]))
padding.fill(np.nan)
return adaptive_coarsegrain_fn(
np.vstack([ar, padding]), np.vstack([countar, padding]), max_levels=max_levels
)[: ar.shape[0], :]
class Genomic2DFeatures(Target):
"""
Stores one or multple datasets of Hi-C style 2D data in cooler format.
Parameters
----------
input_paths : list(str) or str
List of paths to the Cooler datasets or a path to a single
Cooler dataset. For mcool files,
the path should include the resolution. Please refer to
cooler.Cooler documentation for support of mcool files.
features : list(str) or str
The list of dataset names that should match the `input_path`.
shape : tuple(int, int)
The shape of the output array (# of bins by # of bins).
cg : bool, optional
If `yes`, adpative coarse-graining is applied to the output.
Attributes
----------
data : list(cooler.Cooler)
The list of Cooler objects for the cooler files.
n_features : int
The number of cooler files.
feature_index_dict : dict
A dictionary mapping feature names (`str`) to indices (`int`),
where the index is the position of the feature in `features`.
shape : tuple(int, int)
The shape of the output array (# of bins by # of bins).
cg : bool
Whether adpative coarse-graining is applied to the output.
cuda : bool
Whether to use cuda for adaptive coarsegraining. Fast but requires
a lot of GPU memory.
"""
def __init__(self, input_paths, features, shape, cg=False, cuda=False):
"""
Constructs a new `Genomic2DFeatures` object.
"""
if isinstance(input_paths, str) and isinstance(features, str):
input_paths = [input_paths]
features = [features]
self.input_paths = input_paths
self._initialized = False
self.n_features = len(features)
self.feature_index_dict = dict([(feat, index) for index, feat in enumerate(features)])
self.shape = shape
self.cg = cg
self.cuda = cuda
def get_feature_data(self, chrom, start, end, chrom2=None, start2=None, end2=None):
if not self._initialized:
data= self.input_paths[0]
self.data = [cooler.Cooler(data)]
self._initialized = True
self.chrom = chrom
self.start = start
self.end = end
if chrom2 is not None and start2 is not None and end2 is not None:
query = ((chrom, start, end), (chrom2, start2, end2))
else:
query = ((chrom, start, end),)
if self.cg:
out = [
_adaptive_coarsegrain(
c.matrix(balance=True).fetch(*query), c.matrix(balance=False).fetch(*query), cuda=self.cuda
).astype(np.float32)
for c in self.data
]
else:
out = [c.matrix(balance=True).fetch(*query).astype(np.float32) for c in self.data]
if len(out) == 1:
out = out[0]
else:
out = np.concatenate([o[None, :, :] for o in out], axis=0)
return out
class MultibinGenomicFeatures(Target):
"""
Multibin version of selene.targets.GenomicFeatures
Stores the dataset specifying features for genomic regions.
Accepts a `*.bed` file with the following columns,
in order:
::
[chrom, start, end, strand, feature]
`start` and `end` is 0-based as in bed file format.
Note that unlike selene_sdk.targets.GenomicFeatures which queries
the tabix data file out-of-core, MultibinGenomicFeatures requires
more memory as it loads the entire bed file in memory as a pyranges
table for higher query speed.
Parameters
----------
input_path : str
Path to the bed file.
features : list(str)
The non-redundant list of genomic features names. The output array
will have the same feature order as specified in this list.
bin_size : int
The length of the bin(s) in which we check for features
step_size : int
The interval between two adjacent bins.
shape : tuple(int, int)
The shape of the output array (n_features by n_bins).
mode : str, optional
For `mode=='any'`, any overlap will get 1, and no overlap will get 0.
For `mode=='center', only overlap with the center basepair of each bin
will get 1, otherwise 0.
For `mode=='proportion'`, the proportion of overlap will be returned.
Attributes
----------
data : pyranges.PyRanges
The data stored in PyRanges object.
n_features : int
The number of distinct features.
feature_index_dict : dict
A dictionary mapping feature names (`str`) to indices (`int`),
where the index is the position of the feature in `features`.
index_feature_dict : dict
A dictionary mapping indices (`int`) to feature names (`str`),
where the index is the position of the feature in the input
features.
bin_size : int
The length of the bin(s) in which we check for features
step_size : int
The interval between two adjacent bins.
shape : tuple(int, int)
The shape of the output array (n_features by n_bins).
mode : str
- For `mode=='any'`, any overlap will get assigned 1, and no overlap will
get assigned 0.
- For `mode=='center', only overlap with the center basepair of each bin
will get assigned 1, otherwise assigned 0.
- For `mode=='proportion'`, the proportion of overlap will be assigned.
"""
def __init__(self, input_path, features, bin_size, step_size, shape, mode="center"):
"""
Constructs a new `MultibinGenomicFeatures` object.
"""
self.input_path = input_path
self.n_features = len(features)
self.feature_index_dict = dict([(feat, index) for index, feat in enumerate(features)])
self.index_feature_dict = dict(list(enumerate(features)))
self.bin_size = bin_size
self.step_size = step_size
self.initialized = False
self.shape = shape
self.mode = mode
def init(func):
# delay initlization to allow multiprocessing (not necessary here
# but kept for consistency)
@wraps(func)
def dfunc(self, *args, **kwargs):
if not self.initialized:
self.data = pyranges.read_bed(self.input_path)
self.initialized = True
return func(self, *args, **kwargs)
return dfunc
@init
def get_feature_data(self, chrom, start, end):
"""
For a genomic region specified, return a `number of features`
by `number of bins` array for overlap of each genomic bin and
each feature. How the overlap is quantified depends on the
`mode` attribute specified during initialization.
For `mode=='any'`, any overlap will get assigned 1, and no overlap will
get assigned 0.
For `mode=='center', only overlap with the center basepair of each bin
will get assigned 1, otherwise assigned 0.
For `mode=='proportion'`, the proportion of overlap will be assigned.
Parameters
----------
chrom : str
The name of the region (e.g. '1', '2', ..., 'X', 'Y').
start : int
The 0-based first position in the region.
end : int
One past the 0-based last position in the region.
Returns
-------
numpy.ndarray
:math:`L \\times N` array, where :math:`L = ``number of bins`
and :math:`N =` `self.n_features`.
"""
n_bins = int((end - start - self.bin_size) / self.step_size) + 1
targets = np.zeros((self.n_features, n_bins), dtype=np.float32)
if self.mode == "center":
b = pyranges.PyRanges(
pd.DataFrame(
dict(
Chromosome=chrom,
Start=start
+ np.linspace(0, n_bins * self.bin_size, n_bins + 1)[:-1]
+ self.bin_size / 2,
End=start
+ np.linspace(0, n_bins * self.bin_size, n_bins + 1)[:-1]
+ self.bin_size / 2
+ 1,
Index=np.arange(n_bins),
)
)
)
else:
b = pyranges.PyRanges(
pd.DataFrame(
dict(
Chromosome=chrom,
Start=start + np.linspace(0, n_bins * self.bin_size, n_bins + 1)[:-1],
End=start
+ np.linspace(0, n_bins * self.bin_size, n_bins + 1)[:-1]
+ self.bin_size,
Index=np.arange(n_bins),
)
)
)
rows = self.data.join(b)
if len(rows) > 0:
rows_featurename = np.array(rows.Name)
rows_index = np.array(rows.Index)
if self.mode == "proportion":
rows_start = np.array(rows.Start)
rows_end = np.array(rows.End)
for i in range(len(rows)):
targets[self.feature_index_dict[rows_featurename[i]], rows_index[i]] += (
rows_end[i] - rows_start[i]
) / self.bin_size
else:
for i in range(len(rows)):
targets[self.feature_index_dict[rows_featurename[i]], rows_index[i]] = 1
return targets.astype(np.float32)
class RandomPositionsSamplerHiC(OnlineSampler):
"""This sampler randomly selects a region in the genome and retrieves
sequence and relevant Hi-C and optionally multibin genomic
data from that region. This implementation is modified based on
selene_sdk.samplers.RandomPositionSampler.
Parameters
----------
reference_sequence : selene_sdk.sequences.Genome
A genome to retrieve sequence from.
target : Genomic2DFeatures
Genomic2DFeatures object that loads the cooler files.
features : list(str)
List of names that correspond to the cooler files.
target_1d : MultibinGenomicFeatures or None, optional
MultibinGenomicFeatures object that loads 1D genomic feature data.
background_cis_file : str or None, optional
Path to the numpy file that stores the distance-based
expected background balanced scores for cis-interactions. If
specified with background_trans_file, the sampler will
return corresponding background array that matches with
the 2D feature retrieved.
background_trans_file : str or None, optional
Path to the numpy file that stores the expected background
balanced scores for trans-interactions. See doc for
`background_cis_file` for more detail.
seed : int, optional
Default is 436. Sets the random seed for sampling.
validation_holdout : list(str), optional
Default is `['chr6', 'chr7']`. Holdout can be regional or
proportional. If regional, expects a list (e.g. `['chrX', 'chrY']`).
Regions must match those specified in the first column of the
tabix-indexed BED file. If proportional, specify a percentage
between (0.0, 1.0). Typically 0.10 or 0.20.
test_holdout : list(str), optional
Default is `['chr8', 'chr9']`. See documentation for
`validation_holdout` for additional information.
sequence_length : int, optional
Default is 1000000. Model is trained on sequences of size
`sequence_length` where genomic features are retreived
for the same regions as the sequences.
max_seg_length : int or None, optional
Default is None. If specified and cross_chromosome is True,
bound the maximum length of each sequence segment.
length_schedule : list(float, list(int, int)) or None, optional
Default is None. If specified and cross_chromosome is True,
decide the sequence segment length to sample according to the
length schedule (before trimming to fit in the sequence length).
The length schedule is in the format of `[p, [min_len, max_len]]`,
which means, with probability `p`, decide the length by randomly
sampling an integer between `min_len` and `max_len`, and retrieve
the maximal remaining length as default with probability `1-p`.
position_resolution : int, optional
Default is 1. Preprocess the sampled start position by
`start = start - start % position_resolution`. Useful for binned
data.
random_shift : int, optional
Default is 0. Shift the coordinates to retrieve
sequence by a random integer in the range of [-random_shift, random_shift).
random_strand : bool, optional
Default is True. If True, randomly select the strand of the
sequence, otherwise alway use the '+' strand.
cross_chromosome : bool, optional
Default is True. If True, allows sampling multiple segments of
sequences and the corresponding features. The default is sampling
the maximum length allowed by sequence_length, thus multiple segments
will only be sampled if `sequence_length` is larger than the minimum
chromosome length or when max_seg_length and length_schedule is specified
to limit the sequence segment length.
permute_segments : bool, optional
Default is False. If True, permute the order of segments when
multiple segments are sampled.
mode : {'train', 'validate', 'test'}
Default is `'train'`. The mode to run the sampler in.
Attributes
----------
reference_sequence : selene_sdk.sequences.Genome
A genome to retrieve sequence from.
target : selene_sdk.targets.Target
The `selene_sdk.targets.Target` object holding the features that we
would like to predict.
target_1d : MultibinGenomicFeatures or None, optional
MultibinGenomicFeatures object that loads 1D genomic feature data.
background_cis : numpy.ndarray
One-dimensional numpy.ndarray that stores the distance-based
expected background balanced scores for cis-interactions.
background_trans : float
The expected background balanced score for trans-interactions.
bg : bool
Whether the sample will retrieve background arrays.
validation_holdout : list(str)
The samples to hold out for validating model performance. These
can be "regional" or "proportional". If regional, this is a list
of region names (e.g. `['chrX', 'chrY']`). These regions must
match those specified in the first column of the tabix-indexed
BED file. If proportional, this is the fraction of total samples
that will be held out.
test_holdout : list(str)
The samples to hold out for testing model performance. See the
documentation for `validation_holdout` for more details.
sequence_length : int
Model is trained on sequences of size
`sequence_length` where genomic features are retreived
for the same regions as the sequences.
max_seg_length : int or None
Default is None. If specified and cross_chromosome is True,
bound the maximum length of each sequence segment.
length_schedule : list(float, list(int, int)) or None
Default is None. If specified and cross_chromosome is True,
decide the sequence segment length to sample according to the
length schedule (before trimming to fit in the sequence length).
The length schedule is in the format of `[p, [min_len, max_len]]`,
which means, with probability `p`, decide the length by randomly
sampling an integer between `min_len` and `max_len`, and retrieve
the maximal remaining length as default with probability `1-p`.
position_resolution : int
Default is 1. Preprocess the sampled start position by
`start = start - start % position_resolution`. Useful for binned
data.
random_shift : int
Default is 0. Shift the coordinates to retrieve
sequence by a random integer in the range of [-random_shift, random_shift).
random_strand : bool
Default is True. If True, randomly select the strand of the
sequence, otherwise alway use the '+' strand.
cross_chromosome : bool
Default is True. If True, allows sampling multiple segments of
sequences and the corresponding features. The default is sampling
the maximum length allowed by sequence_length, thus multiple segments
will only be sampled if `sequence_length` is larger than the minimum
chromosome length or when max_seg_length and length_schedule is specified
to limit the sequence segment length.
permute_segments : bool
Default is False. If True, permute the order of segments when
multiple segments are sampled.
modes : list(str)
The list of modes that the sampler can be run in.
mode : str
The current mode that the sampler is running in. Must be one of
the modes listed in `modes`.
"""
def __init__(
self,
reference_sequence,
target,
features,
target_1d=None,
background_cis_file=None,
background_trans_file=None,
seed=436,
validation_holdout=["chr6", "chr7"],
test_holdout=["chr8", "chr9"],
sequence_length=1000000,
max_seg_length=None,
length_schedule=None,
position_resolution=1,
random_shift=0,
random_strand=True,
cross_chromosome=True,
permute_segments=False,
mode="train",
):
super(RandomPositionsSamplerHiC, self).__init__(
reference_sequence,
target,
features,
seed=seed,
validation_holdout=validation_holdout,
test_holdout=test_holdout,
sequence_length=sequence_length,
center_bin_to_predict=sequence_length,
mode=mode,
)
self._sample_from_mode = {}
self._randcache = {}
for mode in self.modes:
self._sample_from_mode[mode] = None
self._randcache[mode] = {"cache_indices": None, "sample_next": 0}
self.sample_from_intervals = []
self.interval_lengths = []
self.initialized = False
self.position_resolution = position_resolution
self.random_shift = random_shift
self.random_strand = random_strand
if background_cis_file is not None and background_trans_file is not None:
self.background_cis = np.hstack(
[np.exp(np.load(background_cis_file)), np.repeat(np.nan, 2000)]
)
self.background_trans = np.exp(np.load(background_trans_file))
self.bg = True
else:
self.bg = False
self.max_seg_length = max_seg_length
self.length_schedule = length_schedule
self.target=target
self.target_1d = target_1d
self.cross_chromosome = cross_chromosome
self.permute_segments = permute_segments
if len(validation_holdout) == 0:
self.modes = ["train"]
def init(func):
# delay initlization to allow multiprocessing
@wraps(func)
def dfunc(self, *args, **kwargs):
if not self.initialized:
self._partition_genome_by_chromosome()
for mode in self.modes:
self._update_randcache(mode=mode)
self.initialized = True
return func(self, *args, **kwargs)
return dfunc
def _partition_genome_by_chromosome(self):
for mode in self.modes:
self._sample_from_mode[mode] = SampleIndices([], [])
for index, (chrom, len_chrom) in enumerate(self.reference_sequence.get_chr_lens()):
if chrom in self.validation_holdout:
self._sample_from_mode["validate"].indices.append(index)
elif self.test_holdout and chrom in self.test_holdout:
self._sample_from_mode["test"].indices.append(index)
else:
self._sample_from_mode["train"].indices.append(index)
self.sample_from_intervals.append((chrom, 0, len_chrom))
self.interval_lengths.append(len_chrom)
for mode in self.modes:
sample_indices = self._sample_from_mode[mode].indices
indices, weights = get_indices_and_probabilities(self.interval_lengths, sample_indices)
self._sample_from_mode[mode] = self._sample_from_mode[mode]._replace(