-
Notifications
You must be signed in to change notification settings - Fork 2
/
loc.py
927 lines (674 loc) · 23.2 KB
/
loc.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
# -*- coding: utf-8 -*-
"""
Utilities for locating samples and variants.
See also the examples at:
- http://nbviewer.ipython.org/github/alimanfoo/anhima/blob/master/examples/loc.ipynb
""" # noqa
from __future__ import division, print_function, absolute_import
# standard library dependencies
import random
# third party dependencies
import numpy as np
import numexpr
import matplotlib.pyplot as plt
import scipy.stats
def view_sample(a, selection, all_samples=None):
"""View a single column from the array `a` corresponding to a selected
sample.
Parameters
----------
a : array_like
An array with 2 or more dimensions, where the second dimension
corresponds to samples.
selection : int or object
A sample identifier or column index.
all_samples : sequence, optional
A sequence (e.g., list) of sample identifiers corresponding to the
second dimension of `a`, used to map `selection` to a column index. If
not given, assume `selection` is a column index.
Returns
-------
b : ndarray
An array obtained from `a` by taking the column corresponding to the
selected sample.
"""
# check a is an array of 2 or more dimensions
a = np.asarray(a)
assert a.ndim > 1
if all_samples is None:
# assume selection is an index
index = selection
else:
# check length of samples dimension is as expected
assert a.shape[1] == len(all_samples)
# make sure it's a list
all_samples = list(all_samples)
# check selection is in all_samples
assert selection in all_samples
# determine indices for selected samples
index = all_samples.index(selection)
# view a column from the array
b = a[:, index, ...]
return b
def take_samples(a, selection, all_samples=None):
"""Extract columns from the array `a` corresponding to selected samples.
Parameters
----------
a : array_like
An array with 2 or more dimensions, where the second dimension
corresponds to samples.
selection : sequence of ints or objects
A sequence of sample identifiers or column indices.
all_samples : sequence, optional
A sequence (e.g., list) of sample identifiers corresponding to the
second dimension of `a`, used to map `selection` to column indices. If
not given, assume `selection` is a sequence of column indices.
Returns
-------
b : ndarray
An array obtained from `a` by taking columns corresponding to the
selected samples.
"""
# check a is an array of 2 or more dimensions
a = np.asarray(a)
assert a.ndim > 1
if all_samples is None:
# assume selection is column indices
indices = selection
else:
# check length of samples dimension is as expected
assert a.shape[1] == len(all_samples)
# make sure it's a list
all_samples = list(all_samples)
# check selections are in all_samples
assert all([s in all_samples for s in selection])
# determine indices for selected samples
indices = [all_samples.index(s) for s in selection]
# take columns from the array
b = np.take(a, indices, axis=1)
return b
def query_variants(expression, variants):
"""Evaluate `expression` with respect to the given `variants`.
Parameters
----------
expression : string
The query expression to apply. The expression will be evaluated by
:mod:`numexpr` against the provided `variants`.
variants : dict-like
The variables to include in scope for the expression evaluation.
Returns
-------
result : ndarray
The result of evaluating `expression` against `variants`.
"""
result = numexpr.evaluate(expression, local_dict=variants)
return result
def compress_variants(a, condition):
"""Extract rows from the array `a` corresponding to a boolean `condition`.
Parameters
----------
a : array_like
An array to extract rows from (e.g., genotypes).
condition : array_like, bool
A 1-D boolean array of the same length as the first dimension of `a`.
Returns
-------
b : ndarray
An array obtained from `a` by taking rows corresponding to the
selected variants.
See Also
--------
take_variants, numpy.compress
"""
# check dimensions and sizes
a = np.asarray(a)
condition = np.asarray(condition)
assert a.ndim >= 1
assert condition.ndim == 1
assert a.shape[0] == condition.shape[0]
# compress rows from the input array
b = np.compress(condition, a, axis=0)
return b
def take_variants(a, indices, mode='raise'):
"""Extract rows from the array `a` corresponding to `indices`.
Parameters
----------
a : array_like
An array to extract rows from (e.g., genotypes).
indices : sequence of integers
The variant indices to extract.
mode : {'raise', 'wrap', 'clip'}, optional
Specifies how out-of-bounds indices will behave.
Returns
-------
b : ndarray
An array obtained from `a` by taking rows corresponding to the
selected variants.
See Also
--------
compress_variants, numpy.take
"""
# check dimensions and sizes
a = np.asarray(a)
assert a.ndim >= 1
# take rows from the input array
b = np.take(a, indices, axis=0, mode=mode)
return b
def locate_position(pos, p):
"""Locate the index of coordinate `p` within sorted array of genomic
positions `pos`.
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig, with no duplicates.
p : int
The position to locate.
Returns
-------
index : int or None
The index of `p` in `pos` if present, else None.
See Also
--------
locate_positions, locate_interval, locate_intervals
"""
# check inputs
pos = np.asarray(pos)
# find position
index = np.searchsorted(pos, p)
if index < pos.size and pos[index] == p:
return index
else:
return None
def view_position(a, pos, p):
"""View a slice along the first dimension of `a` corresponding to a
genome position.
Parameters
----------
a : array_like
The array to extract from.
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig, with no duplicates.
p : int
The position to locate.
Returns
-------
b : ndarray
A view of `a` obtained by slicing along the first dimension.
See Also
--------
locate_position
"""
# normalise inputs
a = np.asarray(a)
# determine region slice
index = locate_position(pos, p)
if index is not None:
return a[index, ...]
else:
return None
def locate_interval(pos, start_position=0, stop_position=None):
"""Locate the start and stop indices within the `pos` array that include all
positions within the `start_position` and `stop_position` range.
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
start_position : int
Start position of interval.
stop_position : int
Stop position of interval
Returns
-------
loc : slice
A slice object with the start and stop indices that include all
positions within the interval.
See Also
--------
locate_position, locate_positions, locate_intervals
"""
# check inputs
pos = np.asarray(pos)
# locate start and stop indices
start_index = np.searchsorted(pos, start_position)
stop_index = np.searchsorted(pos, stop_position, side='right') \
if stop_position is not None else None
loc = slice(start_index, stop_index)
return loc
def view_interval(a, pos, start_position, stop_position):
"""View a contiguous slice along the first dimension of `a`
corresponding to a genome interval defined by `start_position` and
`stop_position`.
Parameters
----------
a : array_like
The array to extract from.
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
start_position : int
Start position of interval.
stop_position : int
Stop position of interval
Returns
-------
b : ndarray
A view of `a` obtained by slicing along the first dimension.
See Also
--------
locate_interval
"""
# normalise inputs
a = np.asarray(a)
# determine region slice
loc = locate_interval(pos, start_position, stop_position)
return a[loc, ...]
def locate_positions(pos1, pos2):
"""Find the intersection of two sets of positions.
Parameters
----------
pos1, pos2 : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig, with no duplicates.
Returns
-------
cond1 : ndarray, bool
An array of the same length as `pos1` where an element is True if the
corresponding item in `pos1` is also found in `pos2`.
cond2 : ndarray, bool
An array of the same length as `pos2` where an element is True if the
corresponding item in `pos2` is also found in `pos1`.
See Also
--------
locate_position, locate_interval, locate_intervals
"""
# check inputs
pos1 = np.asarray(pos1)
pos2 = np.asarray(pos2)
# find intersection
cond1 = np.in1d(pos1, pos2, assume_unique=True)
cond2 = np.in1d(pos2, pos1, assume_unique=True)
return cond1, cond2
def locate_intervals(pos, start_positions, stop_positions):
"""Locate items within the `pos` array that fall within any of the
intervals given by `start_positions` and `stop_positions`.
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
start_positions : array_like, int
Start positions of intervals.
stop_positions : array_like, int
Stop positions of intervals
Returns
-------
cond1 : ndarray, bool
An array of the same length as `pos` where an element is True if the
corresponding item in `pos` is also found in any of the intervals.
cond2 : ndarray, bool
An array of the same length as the number of intervals, where an
element is True if the corresponding interval contains one or more
positions in `pos`.
See Also
--------
locate_position, locate_positions, locate_interval
"""
# check inputs
pos = np.asarray(pos)
assert pos.ndim == 1
start_positions = np.asarray(start_positions)
stop_positions = np.asarray(stop_positions)
assert start_positions.ndim == stop_positions.ndim == 1
assert start_positions.shape[0] == stop_positions.shape[0]
# find indices of start and stop positions in pos
start_indices = np.searchsorted(pos, start_positions)
stop_indices = np.searchsorted(pos, stop_positions, side='right')
# find intervals overlapping at least one position
cond2 = start_indices < stop_indices
# find positions within at least one interval
cond1 = np.zeros_like(pos, dtype=np.bool)
for i, j in zip(start_indices[cond2], stop_indices[cond2]):
cond1[i:j] = True
return cond1, cond2
def plot_variant_locator(pos, step=1, ax=None, start_position=None,
stop_position=None, flip=False, line_args=None):
"""
Plot lines indicating the physical genome location of variants. By
default the top x axis is in variant index space, and the bottom x axis
is in genome position space.
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
step : int, optional
Plot a line for every `step` variants.
ax : axes, optional
The axes on which to draw. If not provided, a new figure will be
created.
start_position : int, optional
The start position for the region over which to work.
stop_position : int, optional
The stop position for the region over which to work.
flip : bool, optional
Flip the plot upside down.
line_args : dict-like
Additional keyword arguments passed through to `plt.Line2D`.
Returns
-------
ax : axes
The axes on which the plot was drawn
"""
# set up axes
if ax is None:
fig = plt.figure(figsize=(7, 1))
ax = fig.add_subplot(111)
# determine x axis limits
if start_position is None:
start_position = np.min(pos)
if stop_position is None:
stop_position = np.max(pos)
ax.set_xlim(start_position, stop_position)
# plot the lines
if line_args is None:
line_args = dict()
line_args.setdefault('linewidth', .5)
n_variants = len(pos)
for i, p in enumerate(pos[::step]):
xfrom = p
xto = (
start_position +
((i * step / n_variants) * (stop_position-start_position))
)
l = plt.Line2D([xfrom, xto], [0, 1], **line_args)
ax.add_line(l)
# invert?
if flip:
ax.invert_yaxis()
ax.xaxis.tick_top()
else:
ax.xaxis.tick_bottom()
# tidy up
ax.set_yticks([])
for l in 'left', 'right':
ax.spines[l].set_visible(False)
return ax
def windowed_variant_counts(pos, window_size, start_position=None,
stop_position=None):
"""Count variants in non-overlapping windows over the genome.
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
window_size : int
The size in base-pairs of the windows.
start_position : int, optional
The start position for the region over which to work.
stop_position : int, optional
The stop position for the region over which to work.
Returns
-------
counts : ndarray, int
The number of variants in each window.
bin_edges : ndarray, int
The edge positions of each window. Note that this has length
``len(counts)+1``. To determine bin centers use
``(bin_edges[:-1] + bin_edges[1:]) / 2``. To determine bin widths use
``np.diff(bin_edges)``.
See Also
--------
windowed_variant_counts_plot, windowed_variant_density
"""
# determine bins
if stop_position is None:
stop_position = np.max(pos)
if start_position is None:
start_position = np.min(pos)
bin_edges = np.append(
np.arange(start_position, stop_position, window_size), stop_position
)
# make a histogram of positions
counts, _ = np.histogram(pos, bins=bin_edges)
return counts, bin_edges
def plot_windowed_variant_counts(pos, window_size, start_position=None,
stop_position=None,
ax=None, plot_kwargs=None):
"""Plot windowed variant counts.
Parameters
----------
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
window_size : int
The size in base-pairs of the windows.
start_position : int, optional
The start position for the region over which to work.
stop_position : int, optional
The stop position for the region over which to work.
ax : axes, optional
The axes on which to draw. If not provided, a new figure will be
created.
plot_kwargs : dict-like
Additional keyword arguments passed through to `plt.plot`.
Returns
-------
ax : axes
The axes on which the plot was drawn.
See Also
--------
windowed_variant_counts, windowed_variant_density_plot
"""
# set up axes
if ax is None:
fig = plt.figure(figsize=(7, 2))
ax = fig.add_subplot(111)
# count variants
y, bin_edges = windowed_variant_counts(pos, window_size,
start_position=start_position,
stop_position=stop_position)
# calculate bin centers
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
# plot data
if plot_kwargs is None:
plot_kwargs = dict()
plot_kwargs.setdefault('linestyle', '-')
plot_kwargs.setdefault('marker', None)
ax.plot(bin_centers, y, **plot_kwargs)
# tidy up
ax.set_ylim(bottom=0)
ax.set_xlabel('position')
ax.set_ylabel('count')
if start_position is None:
start_position = np.min(pos)
if stop_position is None:
stop_position = np.max(pos)
ax.set_xlim(start_position, stop_position)
return ax
def windowed_variant_density(pos, window_size, start_position=None,
stop_position=None):
"""Calculate per-base-pair density of variants in non-overlapping windows
over the genome.
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
window_size : int
The size in base-pairs of the windows.
start_position : int, optional
The start position for the region over which to work.
stop_position : int, optional
The stop position for the region over which to work.
Returns
-------
density : ndarray, int
The density of variants in each window.
bin_edges : ndarray, int
The edge positions of each window. Note that this has length
``len(density)+1``. To determine bin centers use
``(bin_edges[:-1] + bin_edges[1:]) / 2``. To determine bin widths use
``np.diff(bin_edges)``.
See Also
--------
windowed_variant_density_plot, windowed_variant_counts
"""
# count variants in windows
counts, bin_edges = windowed_variant_counts(pos, window_size,
start_position=start_position,
stop_position=stop_position)
bin_widths = np.diff(bin_edges)
# convert to per-base-pair density
density = counts / bin_widths
return density, bin_edges
def plot_windowed_variant_density(pos, window_size, start_position=None,
stop_position=None,
ax=None, plot_kwargs=None):
"""Plot windowed variant density.
Parameters
----------
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
window_size : int
The size in base-pairs of the windows.
start_position : int, optional
The start position for the region over which to work.
stop_position : int, optional
The stop position for the region over which to work.
ax : axes, optional
The axes on which to draw. If not provided, a new figure will be
created.
plot_kwargs : dict-like
Additional keyword arguments passed through to `plt.plot`.
Returns
-------
ax : axes
The axes on which the plot was drawn.
See Also
--------
windowed_variant_density, windowed_variant_counts_plot
"""
# set up axes
if ax is None:
fig = plt.figure(figsize=(7, 2))
ax = fig.add_subplot(111)
# count variants
y, bin_edges = windowed_variant_density(pos, window_size,
start_position=start_position,
stop_position=stop_position)
# calculate bin centers
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
# plot data
if plot_kwargs is None:
plot_kwargs = dict()
plot_kwargs.setdefault('linestyle', '-')
plot_kwargs.setdefault('marker', None)
ax.plot(bin_centers, y, **plot_kwargs)
# tidy up
ax.set_ylim(bottom=0)
ax.set_xlabel('position')
ax.set_ylabel('density')
if start_position is None:
start_position = np.min(pos)
if stop_position is None:
stop_position = np.max(pos)
ax.set_xlim(start_position, stop_position)
return ax
def windowed_statistic(pos, values, window_size,
start_position=None,
stop_position=None,
statistic='mean'):
"""Calculate a statistic for `values` binned in non-overlapping windows
over the genome.
Parameters
----------
pos : array_like
A sorted 1-dimensional array of genomic positions from a single
chromosome/contig.
values : array_like
A 1-D array of the same length as `pos`.
window_size : int
The size in base-pairs of the windows.
start_position : int, optional
The start position for the region over which to work.
stop_position : int, optional
The stop position for the region over which to work.
statistic : string or function
The function to apply to values in each bin.
Returns
-------
stats : ndarray
The values of the statistic within each bin.
bin_edges : ndarray
The edge positions of each window. Note that this has length
``len(stats)+1``. To determine bin centers use
``(bin_edges[:-1] + bin_edges[1:]) / 2``. To determine bin widths use
``np.diff(bin_edges)``.
"""
# determine bins
if stop_position is None:
stop_position = np.max(pos)
if start_position is None:
start_position = np.min(pos)
bin_edges = np.append(
np.arange(start_position, stop_position, window_size), stop_position
)
# compute binned statistic
stats, _, _ = scipy.stats.binned_statistic(pos, values=values,
statistic=statistic,
bins=bin_edges)
return stats, bin_edges
def evenly_downsample_variants(a, k):
"""Evenly downsample an array along the first dimension to length `k` (or
as near as possible), assuming the first dimension corresponds to variants.
Parameters
----------
a : array_like
The array to downsample.
k : int
The target number of variants.
Returns
-------
b : array_like
A downsampled view of `a`.
"""
# normalise inputs
a = np.asarray(a)
# determine length of first dimension
n_variants = a.shape[0]
# determine step
step = max(1, int(n_variants/k))
# take slice
b = a[::step, ...]
return b
def randomly_downsample_variants(a, k):
"""Evenly downsample an array along the first dimension to length `k`,
assuming the first dimension corresponds to variants.
Parameters
----------
a : array_like
The array to downsample.
k : int
The k number of variants.
Returns
-------
b : array_like
A downsampled copy of `a`.
"""
# normalise inputs
a = np.asarray(a)
# determine length of first dimension
n_variants = a.shape[0]
# sample indices
indices = sorted(random.sample(range(n_variants), k))
# apply selection
b = np.take(a, indices, axis=0)
return b