forked from pr0kary0te/minimalmarkers
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathminimalmarkers.py
1285 lines (976 loc) · 41.6 KB
/
minimalmarkers.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
import os as _os
try:
from dataclasses import dataclass as _dataclass
from typing import List as _List
from typing import Dict as _Dict
from typing import Tuple as _Tuple
except Exception:
raise ImportError(
"This script requires newer features of Python, as provided "
"by the dataclasses and typing modules. Please upgrade to "
"at least Python 3.6 and try again. If you are using "
"Python 3.6 then make sure that the 'dataclasses' module "
"has been installed.")
try:
if "NO_NUMBA" in _os.environ and _os.environ["NO_NUMBA"] == "1":
# used for debugging - disable numba
raise ImportError("Disabling numba")
import numba as _numba
_MIN_CHUNK_SIZE = 1000
except Exception:
print("WARNING: numba is not available. This is used to accelerate "
"this script. We really recommend installing numba, "
"e.g. via `pip install numba` or `conda install numba` "
"as this will make this script run significantly (hundreds "
"of times!) faster.")
_MIN_CHUNK_SIZE = 10
# This is a fake numba module which will enable the numba'd code
# to run without requiring the module. Note that it won't be
# compiled, so will run really slowly!
class _numba:
@staticmethod
def jit(**kwargs):
def wrapped(func):
return func
return wrapped
@staticmethod
def prange(*args, **kwargs):
return range(*args, **kwargs)
class CONFIG:
NUMBA_NUM_THREADS = 1
config = CONFIG()
try:
import numpy as _np
except Exception:
raise ImportError(
"This script requires numpy to accelerate key parts. "
"Please install numpy (e.g. via 'conda install numpy' or "
"'pip install numpy' and try again.")
def _no_progress_bar(x, **kwargs):
return x
try:
if "NO_PROGRESS" in _os.environ and _os.environ["NO_PROGRESS"] == "1":
# Option to completely disable progress bars (some people
# really dislike them)
raise ImportError("Disabling progress bars")
from tqdm import tqdm as _progress_bar
# This will fail on older versions of tqdm
_p = _progress_bar(range(1, 10), unit="patterns", delay=1)
except Exception:
_progress_bar = _no_progress_bar
@_dataclass
class Patterns:
"""This class holds all of the data relating to the patterns
that distinguish between varieties that will be searched
"""
"""numpy 2D array of integers holding the patterns to be searched"""
patterns = None
"""The IDs of the patterns, in the same order as the rows in
the numpy 2D array"""
ids: _List[str] = None
"""The varieties to be distinguised, in the same order as the
columns in the numpy 2D array"""
varieties: _List[str] = None
"""The minor allele frequency for each pattern, in the same order
as the rows in the numpy 2D array. Note that this array should
be sorted in order of decreasing MAF"""
mafs: _List[float] = None
"""If there are any duplicate patterns, then this dictionary
contains the ID of the canonical pattern as the key,
with the value being the IDs of all of the other duplicates"""
duplicates: _Dict[str, _List[str]] = None
def __init__(self, patterns, ids, varieties, mafs, duplicates):
self.patterns = patterns
self.ids = ids
self.varieties = varieties
self.mafs = mafs
self.duplicates = duplicates
def get_pattern_as_string(self, i):
"""Return the ith pattern as a string"""
return _get_pattern_from_array(self.patterns[i])
def _get_pattern_from_array(array) -> str:
"""Return a pattern encoded as an integer array
into a string - this converts values less than
0 into x
"""
# Common formats are A, B for homozygotes (AA and BB) and AB for
# heterozygotes or 0,1,2 for hom AA, het AB and hom BB.
# We're going to convert A AB B to 0,1,2 format from here on
values = {"0": "0",
"1": "1",
"2": "2",
0: "0",
1: "1",
2: "2",
"AB": "1",
"A": "0",
"B": "2"}
pattern = [values.get(x, "x") for x in array]
return "".join(pattern)
def convert_vcf_to_genotypes(input_file: str) -> str:
"""Convert the passed input file in VCF format into the genotypes
format required by this program. This will write the output
file into a new file called '{input_file}.genotypes', and will
return that filename
input_file: str The full path to the input file to convert
returns:
The full path to the converted file
"""
import re
lines = open(input_file, "r").readlines()
i = 0
for line in lines:
line = line.strip()
if line.startswith("#CHR"):
# we have found the CHROM line
break
i += 1
if not line.startswith("#CHR"):
print(f"WARNING: This does not look like a valid VCF file!")
return None
parts = line.split("\t")
if len(parts) < 10:
print(f"WARNING: Corruption on line {i+1} of the VCF file!")
return None
head = "\t".join(parts[9:])
output_file = f"{input_file}.genotypes"
FILE = open(output_file, "w")
FILE.write(f"Marker\t{head}\n")
i += 1
while i < len(lines):
line = lines[i].strip()
parts = line.split("\t")
if len(parts) < 10:
print(f"WARNING: Corruption on line {i+1} of the VCF file!")
FILE.close()
os.unlink(output_file)
return None
chr = parts[0]
pos = parts[1]
data = parts[9:]
FILE.write(f"{chr}_{pos}")
for cell in data:
field = cell.split(":")[0]
m = re.search(r"^(\d)[\/\|](\d)", field)
if m:
a = int(m.groups()[0])
b = int(m.groups()[1])
if a == 0 and b == 1:
FILE.write("\t1")
elif a == 0 and b == 0:
FILE.write("\t0")
elif a == 1 and b == 1:
FILE.write("\t2")
else:
print(f"WARNING: Unrecognised pattern {field} on "
f"line {i+1}.")
FILE.write("\t-1")
else:
print(f"WARNING: Unrecognised pattern {field} on "
f"line {i+1}. Expecting 0/1, 1|1, 0/0 or equivalent.")
FILE.write("\t-1")
FILE.write("\n")
i += 1
FILE.close()
return output_file
@_numba.jit(nopython=True, nogil=True, fastmath=True,
parallel=True, cache=True)
def _calculate_mafs(data,
min_call_rate: float = 0.9,
min_maf: float = 0.001,
print_progress: bool = False):
"""Now loop over the distinct SNP patterns to calculate their
Minor Allele Frequency (MAF) score.
min_maf: MAF is minor allele frequency. This is set to a low level
to include as many markers as possible but exclude rare
error calls. It probably needs optimising for your data.
This will return a numpy array of the scores for all of the patterns.
in the order they appear in the data. A score of 0 is given for
any skipped or invalid patterns. Note that, for speed, the score is
returned as an array of integers. You need to divide this by the number
of varieties to get the proper MAF.
"""
if data is None:
return None
nrows: int = data.shape[0]
ncols: int = data.shape[1]
if nrows == 0 or ncols == 0:
return None
mafs = _np.zeros((nrows), _np.int32)
# Go through in parallel and calculate maf for
# each pattern. Patterns that should be skipped
# will be given a maf of 0
for i in _numba.prange(0, nrows):
fails: int = 0
zeros: int = 0
ones: int = 0
twos: int = 0
for j in range(0, ncols):
x: int = data[i, j]
if x == -1:
fails += 1
elif x == 0:
zeros += 1
elif x == 1:
ones += 1
else:
twos += 1
nalleles: int = (zeros != 0) + (ones != 0) + (twos != 0)
call_rate: float = float(ncols - fails) / ncols
if nalleles <= 1 or call_rate < min_call_rate:
mafs[i] = 0
else:
# Logic steps to work out which is the second most common
# call, which we'll define as the minor allele.
if ones >= zeros and zeros >= twos:
minor: int = zeros
elif zeros >= ones and ones >= twos:
minor: int = ones
elif zeros >= twos and twos >= ones:
minor: int = twos
elif ones >= twos and twos >= zeros:
minor: int = twos
elif twos >= ones and ones >= zeros:
minor: int = ones
elif twos >= zeros and zeros >= ones:
minor: int = zeros
else:
print("PROGRAM BUG!!! INVALID CONDITION!")
minor: int = 0
if minor < min_maf * ncols:
# eliminate patterns with too low a maf
minor = 0
mafs[i] = minor
return mafs
@_numba.jit(nopython=True, nogil=True, fastmath=True, cache=True)
def _sort_patterns(data, mafs,
max_markers: int,
print_progress: bool = False):
"""Return a numpy array of indicies that would represent
the array of data sorted by maf. Note that this will
remove duplicates and anything that has a maf score
of zero
"""
nrows: int = len(mafs)
ncols: int = data.shape[1]
if data.shape[0] != nrows:
print("CORRUPT DATA!")
return (None, None, None)
if nrows == 0:
return (None, None, None)
# Get the indicies of the sorted mafs. Numpy sorts in increasing
# order, but we need decreasing order (hence the [::-1])
sorted_idxs = _np.argsort(mafs)[::-1]
# We have to assume that the data is already sorted - need
# to find and remove duplicates, plus ones where the
# maf is zero.
if mafs[sorted_idxs[0]] == 0:
return (None, None, None)
order = _np.full(nrows, fill_value=-1, dtype=_np.int32)
duplicates = _np.full(nrows, fill_value=-1, dtype=_np.int32)
order[0] = sorted_idxs[0]
npatterns: int = 1
nduplicates: int = 0
for i in range(1, nrows):
if npatterns >= max_markers:
if i != nrows-1:
print("Maximum marker count reached! "
"Ignoring further markers.")
break
idx: int = sorted_idxs[i]
maf: int = mafs[idx]
if maf == 0:
continue
# is this a duplicate of any that are above with the same
# maf score?
new_pattern: int = 1
for j in range(i-1, -1, -1):
last_idx: int = sorted_idxs[j]
if maf != mafs[last_idx]:
# there are no more patterns with the same maf score
break
elif duplicates[last_idx] == -1:
# is this equal to any of the previous patterns
# that has the same maf score?
all_same: int = 1
for k in range(0, ncols):
if data[idx, k] != data[last_idx, k]:
all_same = 0
break
if all_same == 1:
# this is a duplicate pattern
new_pattern = 0
duplicates[idx] = last_idx
break
if new_pattern == 1:
order[npatterns] = idx
npatterns += 1
else:
nduplicates += 1
# remove invalid patterns
order = order[order != -1]
npatterns: int = len(order)
# now copy the patterns, in order, to the output array
patterns = _np.zeros((npatterns, ncols), _np.int8)
for i in range(0, npatterns):
idx = order[i]
for j in range(0, ncols):
patterns[i, j] = data[idx, j]
return (patterns, order, duplicates)
def load_patterns(input_file: str,
min_call_rate: float = 0.9,
min_maf: float = 0.001,
max_markers: int = 1000000000000,
print_progress: bool = False) -> Patterns:
"""Load all of the patterns from the passed file.
The patterns will be converted to the correct format,
including cleaning / conversion of A, B, AB converted
to 0, 1, 2 format.
min_call_rate: Ignore markers with less than this proportion
of valid (0, 1 of 2 ) calls.
min_maf: Ignore patterns with a MAF below this value
max_markers: Ignore more than this number of patterns
(extra patterns with lower MAFs will be ignored)
print_progress: Print the progress of reading / processing
the patterns to the screen. If False then
this function will not print anything.
This will return a valid Patterns object that will contain
all of the data for the patterns, sorted in decreasing
MAF order
"""
# Read in the data - assume this is comma separated for now. Can
# easily add a test to change to tab separated if needed
if print_progress:
progress = _progress_bar
print(f"Loading '{input_file}'...")
else:
progress = _no_progress_bar
import csv
lines = open(input_file, "r").readlines()
dialect = csv.Sniffer().sniff(lines[0], delimiters=[" ", ",", "\t"])
# the varieties are the column headers (minus the first column
# which is the ID code for the pattern)
varieties = []
for variety in list(csv.reader([lines[0]], dialect=dialect))[0][1:]:
varieties.append(variety.lstrip().rstrip())
ids = []
nrows = len(lines) - 1
ncols = len(varieties)
data = _np.full((nrows, ncols), -1, _np.int8)
if print_progress:
print(f"Reading {nrows} patterns for {ncols} varieties...")
progress = _progress_bar
else:
progress = _no_progress_bar
npatterns = 0
irow = _np.full(ncols, -1, _np.int8)
for i in progress(range(1, nrows+1), unit="patterns", delay=1):
parts = list(csv.reader([lines[i]], dialect=dialect))[0]
if len(parts) != ncols+1:
print("WARNING - invalid row! "
f"'{parts}' : {len(parts)} vs {ncols}")
else:
ids.append(parts[0])
row = _np.asarray(parts[1:], _np.string_)
pattern = data[npatterns]
pattern[(row == b'0') | (row == b'A')] = 0
pattern[(row == b'1') | (row == b'AB')] = 1
pattern[(row == b'2') | (row == b'B')] = 2
npatterns += 1
if print_progress:
print(f"Successfully read {npatterns} patterns.\n")
print(f"Calculating MAFs, removing duplicates and sorting patterns...")
mafs = _calculate_mafs(data, min_call_rate=min_call_rate,
min_maf=min_maf, print_progress=print_progress)
if print_progress:
num_eliminated: int = len(mafs[mafs == 0])
print("\nNumber of eliminated patterns (by min_call_rate, min_maf or "
f"requiring more than one allele) is {num_eliminated}.")
(patterns, order, dups) = _sort_patterns(data, mafs,
max_markers=max_markers,
print_progress=print_progress)
if patterns is None:
if print_progress:
print("\nWARNING! There are no patterns that exceeded "
f"the required criteria (min_maf = {min_maf}, "
f"min_call_rate = {min_call_rate}).\n")
return None
sorted_ids = []
sorted_mafs = []
duplicates = {}
for idx in order:
sorted_ids.append(ids[idx])
sorted_mafs.append(mafs[idx] / ncols)
for i, dup in enumerate(dups):
if dup != -1:
same = _np.array_equal(data[dup], data[i])
if not same:
print("WARNING: Program bug. Two patterns which are "
"flagged as identical aren't the same! "
f"{dup} and {i}")
assert(same)
canonical = ids[dup]
if canonical in duplicates:
duplicates[canonical].append(ids[i])
else:
duplicates[canonical] = [ids[i]]
if print_progress:
print(f"\nLoaded marker data for {patterns.shape[0]} "
"distinct patterns that have a sufficiently high "
"MAF and call rate to be worth including.")
assert(len(sorted_ids) == patterns.shape[0])
assert(len(varieties) == patterns.shape[1])
return Patterns(patterns=patterns,
ids=sorted_ids,
varieties=varieties,
mafs=sorted_mafs,
duplicates=duplicates)
@_numba.jit(nopython=True, nogil=True, fastmath=True,
parallel=True, cache=True)
def _calculate_best_possible_score(patterns, start: int, end: int):
"""Calculate the best possible score that could be achieved
using all of the patterns
"""
npatterns: int = patterns.shape[0]
score: int = 0 # reduction variable
# see http://numba.pydata.org/numba-doc/0.12.1/prange.html?highlight=parallel
# for variable privatization rules
for i in _numba.prange(start, end):
for j in range(0, i):
for p in range(0, npatterns):
ival: int = patterns[p, i]
jval: int = patterns[p, j]
if ival != -1 and jval != -1 and ival != jval:
score += 1
break # stop early if cell contains a one
return score
def calculate_best_possible_score(patterns: Patterns,
print_progress: bool = False) -> int:
"""Calculate the best possible score for the passed Patterns
object.
patterns: The Patterns object containing the patterns to search
print_progress: Whether or not to print any progress status
to output
This returns the best possible score
"""
if type(patterns) != Patterns:
raise TypeError("This function requires a valid Patterns object!")
patterns = patterns.patterns
ncols: int = patterns.shape[1]
if print_progress:
chunk_size = min(_MIN_CHUNK_SIZE, ncols)
progress = _progress_bar
else:
chunk_size = ncols
progress = _no_progress_bar
nchunks: int = int(ncols / chunk_size)
while nchunks*chunk_size < ncols:
nchunks += 1
if nchunks < 4:
nchunks = 1
chunk_size = ncols
progress = _no_progress_bar
score: int = 0
for i in progress(range(0, nchunks), delay=1,
unit="variety", unit_scale=chunk_size):
start: int = i * chunk_size
end: int = min((i+1)*chunk_size, ncols)
score += _calculate_best_possible_score(patterns, start, end)
return score
@_numba.jit(nopython=True, nogil=True, fastmath=True,
parallel=True, cache=True)
def _chunked_first_score_patterns(patterns, skip_patterns,
scores, start, end):
ncols: int = patterns.shape[1]
for p in _numba.prange(start, end):
if not skip_patterns[p]:
score: int = 0
# If we were to sort the rows by value the resulting scoring
# matrix would be a matrix of ones, with a block diagonal of
# zeros, each block being having the dimension corresponding to
# the count of the identifer. We can therefore skip the O(n^2)
# visiting each element and instead calculate the score directly.
bins = _np.zeros(4, _np.int32)
for i in range(0, ncols):
# Here we take advantage of the identifiers being -1, 0, 1, 2 to
# use them to directly index the counter. We take advanage of
# wrap-around indexing so that -1 ends up in index 3.
# We could instead have used numpy.bincount, but then
# we would have to recode or remove -1 values first.
bins[patterns[p, i]] += 1
# Examining the matrix we can see that the score can be calculated directly as:
score = bins[0] * bins[1] + (bins[0] + bins[1]) * bins[2]
# If we wanted to handle more than three groups this formula could be extended
# We skip the invalid -1 code by ignoring the last bin value in this
# calculation.
scores[p] = score
if score == 0:
skip_patterns[p] = 1
def _first_score_patterns(patterns, skip_patterns,
print_progress: bool = True):
"""Do the work of scoring all of the passed patterns against
the current value of the matrix. This returns a tuple
of the best score and the index of the pattern with
that best score
"""
npatterns: int = patterns.shape[0]
scores = _np.zeros(npatterns, _np.int32)
if print_progress:
chunk_size = min(_MIN_CHUNK_SIZE, npatterns)
progress = _progress_bar
else:
chunk_size = npatterns
progress = _no_progress_bar
nchunks: int = int(npatterns / chunk_size)
while nchunks*chunk_size < npatterns:
nchunks += 1
if nchunks < 4:
nchunks = 1
chunk_size = npatterns
progress = _no_progress_bar
for i in progress(range(0, nchunks), delay=1,
unit="patterns", unit_scale=chunk_size):
start: int = i * chunk_size
end: int = min((i+1)*chunk_size, npatterns)
_chunked_first_score_patterns(patterns, skip_patterns,
scores, start, end)
return scores
@_numba.jit(nopython=True, cache=True)
def _unmatched_element_indices(pattern, expected_size):
"""Calculate the co-ordinates of the elements with value
zero in the matrix corresponding to the passed in pattern.
Returns
=======
indices : N x 2 numpy array containing row/column of each zero
"""
indices = _np.empty((expected_size, 2), dtype=_np.int64)
nele: int = pattern.size
idx: int = 0 # shared across iterations, so can't parallelise the loops
for i in range(0, nele):
ival: int = pattern[i]
for j in range(0, i):
jval: int = pattern[j]
if ival == -1 or jval == -1 or ival == jval:
indices[idx, 0] = i
indices[idx, 1] = j
idx += 1
assert(idx == expected_size)
return indices
@_numba.jit(nopython=True, nogil=True, fastmath=True,
parallel=True, cache=True)
def _chunked_rescore_patterns(patterns, indices, skip_patterns,
scores, sorted_idxs,
best_score: int,
start: int, end: int):
ncols: int = patterns.shape[1]
nidx: int = indices.shape[0]
nthreads: int = _numba.config.NUMBA_NUM_THREADS
best_scores = _np.zeros(nthreads, _np.int32)
for thread_id in _numba.prange(0, nthreads):
my_best_score: int = best_score
for chunk in range(start, end, nthreads):
idx: int = chunk + thread_id
if idx >= end:
break
p: int = sorted_idxs[idx]
if skip_patterns[p]:
continue
current_score: int = scores[p]
if current_score < my_best_score:
# there are no more patterns with a better score
break
# this pattern could be the best scoring pattern...
score: int = 0
for idx in range(0, nidx):
i: int = indices[idx, 0]
j: int = indices[idx, 1]
ival: int = patterns[p, i]
jval: int = patterns[p, j]
if ival != -1 and jval != -1 and ival != jval:
score += 1
scores[p] = score
if score == 0:
skip_patterns[p] = 1
if score > my_best_score:
my_best_score = score
best_scores[thread_id] = my_best_score
best_score = 0
for score in best_scores:
if score > best_score:
best_score = score
return best_score
def _rescore_patterns(patterns, indices, skip_patterns, scores, sorted_idxs,
print_progress: bool = False):
"""Do the work of scoring all of the passed patterns against
the current value of the matrix. This returns a tuple
of the best score and the index of the pattern with
that best score
"""
npatterns: int = patterns.shape[0]
if print_progress:
chunk_size = min(_MIN_CHUNK_SIZE, npatterns)
progress = _progress_bar
else:
chunk_size = npatterns
progress = _no_progress_bar
nchunks: int = int(npatterns / chunk_size)
while nchunks*chunk_size < npatterns:
nchunks += 1
if nchunks < 4:
nchunks = 1
chunk_size = npatterns
progress = _no_progress_bar
best_score: int = 0
for i in progress(range(0, nchunks), delay=1,
unit="patterns", unit_scale=chunk_size):
start: int = i * chunk_size
end: int = min((i+1)*chunk_size, npatterns)
best_score: int = _chunked_rescore_patterns(patterns, indices,
skip_patterns,
scores, sorted_idxs,
best_score,
start, end)
return _np.argsort(scores)[::-1]
@_numba.jit(nopython=True, cache=True)
def _remove_matched_indices(data, indices, num_fewer):
"""Removes indices from the input array corresponding to
ones in the matrix generated from the data array
Returns
=======
new_indices : N x 2 numpy array containing the new indices
"""
num_old: int = indices.shape[0]
new_size: int = num_old - num_fewer
nrows: int = data.shape[0]
new_indices = _np.empty((new_size, 2), dtype=_np.int64)
new_idx: int = 0 # shared accross iterations, so can't parallelise the loop
for old_idx in range(0, num_old):
i: int = indices[old_idx, 0]
j: int = indices[old_idx, 1]
ival: int = data[i]
jval: int = data[j]
if ival == -1 or jval == -1 or ival == jval:
new_indices[new_idx] = [i, j]
new_idx += 1
assert(new_idx == new_size)
return new_indices
@_numba.jit(nopython=True, fastmath=True, nogil=True,
parallel=True, cache=True)
def _create_matrix(pattern, matrix):
"""Create the stencil matrix for this pattern, based on the
current value of the combined stencil matrix (i.e. only
cover up holes that are not already covered
"""
n = len(pattern)
m = _np.zeros((n, n), _np.int8)
for i in _numba.prange(0, n):
ival = pattern[i]
if ival != -1:
for j in range(i+1, n):
jval = pattern[j]
# If this cell in the matrix is currently set to zero,
# i.e. this pair of varieties (i and j) are unresolved,
# and their genotypes are valid and different, then we can
# set this cell in the test matrix to 1 (= resolved) -
# otherwise it remains set to zero.
if jval != -1 and ival != jval and matrix[i, j] == 0:
m[i, j] = 1
return m
@_numba.jit(nopython=True, fastmath=True, nogil=True,
parallel=True, cache=True)
def _calculate_score(matrix):
ncols: int = matrix.shape[1]
test_score: int = 0
for i in _numba.prange(0, ncols):
for j in range(i+1, ncols):
test_score += matrix[i, j]
return test_score
@_numba.jit(nopython=True, fastmath=True, nogil=True,
parallel=False, cache=True)
def _get_unresolved(matrix):
ncols: int = matrix.shape[1]
unresolved = _np.zeros((ncols, 2), _np.int32)
num_unresolved: int = 0
for i in range(0, ncols):
for j in range(i+1, ncols):
if matrix[i, j] == 0:
unresolved[num_unresolved, 0] = i
unresolved[num_unresolved, 1] = j
num_unresolved += 1
return (num_unresolved, unresolved)
def get_unresolved(patterns: Patterns,
best_patterns: _List[_Tuple[int, int]],
print_progress: bool = False):
"""Return the IDs of any of the varieties that are
not resolved by the set of best patterns output
by the 'find_best_patterns' function.
patterns: Patterns
The input data to process
best_patterns: List[Tuple[int, int]]
The collection of best patterns output from the
find_best_patterns function
returns: List[Tuple[str, str]]
The lists of pairs of varieties that cannot be
distinguished from one another. The names
of the varieties are returned
"""
ncols: int = len(patterns.varieties)
# first, build the matrix of what is resolved
matrix = _np.zeros((ncols, ncols), _np.int8)
if print_progress:
print("Identifying unresolved varieties...")
progress = _progress_bar
else:
progress = _no_progress_bar
for i in progress(range(0, len(best_patterns)), delay=1,
unit="patterns"):
(pattern, score) = best_patterns[i]
matrix += _create_matrix(patterns.patterns[pattern], matrix)
# now find the number and ID of unresolved varieties from this matrix
(num_unresolved, unresolved) = _get_unresolved(matrix)
unresolved_varieties = {}
result = []
for idx in range(0, num_unresolved):
i: int = unresolved[idx, 0]
j: int = unresolved[idx, 1]
var1 = patterns.varieties[i]
var2 = patterns.varieties[j]
unresolved_varieties[i] = 1
unresolved_varieties[j] = 1
result.append((var1, var2))
return result
def find_best_patterns(patterns: Patterns,
print_progress: bool = False) -> _List[_Tuple[int,
int]]:
"""This is the main function where we iterate through all of the
available rows of SNP data and find the one that adds the most
new "1s" to the overall scoring matrix. This function will return
when the current_score value is zero - i.e. adding
another row doesn't add anything to the overall matrix score.
This returns the best patterns with their associated score,
as a list of tuples, e.g.
[ (index of best pattern, score of best pattern),
(index of second best pattern, score of second best pattern),
...
(index of last pattern, score of last pattern)
]
"""
# create the scoring matrix
ncols: int = len(patterns.patterns[0])
perfect_score: int = int((ncols * (ncols-1)) / 2)