forked from AlphaGenes/tinyhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombinedHMM.py
624 lines (457 loc) · 26.5 KB
/
CombinedHMM.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
from numba import jit
import numpy as np
from . import NumbaUtils
from . import ProbMath
class HaploidMarkovModel :
def __init__(self, n_loci, error, recombination_rate = None):
self.update_paramaters(n_loci, error, recombination_rate)
self.directional_smoothing = self.create_directional_smoothing()
self.apply_smoothing = self.create_apply_smoothing()
self.apply_viterbi = self.create_viterbi_algorithm()
self.apply_sampling = self.create_sampling_algorithm(NumbaUtils.multinomial_sample)
def update_paramaters(self, n_loci, error, recombination_rate = None):
self.n_loci = n_loci
if type(error) is float:
self.error = np.full(n_loci, error, dtype=np.float32)
else:
self.error = error
if recombination_rate is None:
recombination_rate = 1.0/n_loci
if type(recombination_rate) is float:
self.recombination_rate = np.full(n_loci, recombination_rate, dtype=np.float32)
else:
self.recombination_rate = recombination_rate
def get_mask(self, called_haplotypes):
return np.all(called_haplotypes != 9, axis = 0)
# def get_run_option(default_arg, alternative_arg):
# # Return the default arg as true if it is supplied, otherwise return the alternative arg.
# if default_arg is not None:
# if alternative_arg is not None:
# if default_arg and alternative_arg:
# print("Both arguments are true, returning default")
# if not default_arg and not alternative_arg:
# print("Both arguments are false, returning default")
# return default_arg
# else:
# if alternative_arg is None:
# return True
# else:
# return not alternative_arg
def run_HMM(self, point_estimates = None, algorithm = "marginalize", **kwargs):
# return_called_values = get_run_option(return_called_values, return_genotype_probabilities)
if point_estimates is None:
point_estimates = self.get_point_estimates(**kwargs)
if algorithm == "marginalize":
total_probs = self.apply_smoothing(point_estimates, self.recombination_rate)
genotype_probabilities = self.calculate_genotype_probabilities(total_probs, **kwargs)
elif algorithm == "viterbi":
total_probs = self.apply_viterbi(point_estimates, self.recombination_rate)
genotype_probabilities = self.calculate_genotype_probabilities(total_probs, **kwargs)
elif algorithm == "sample":
total_probs = self.apply_sampling(point_estimates, self.recombination_rate)
genotype_probabilities = self.calculate_genotype_probabilities(total_probs, **kwargs)
else:
print(f"Valid alrogithm option not given: {alrogithm}")
return genotype_probabilities
def call_genotype_probabilities(self, genotype_probabilities, threshold = 0.1):
return ProbMath.call_genotype_probs(genotype_probabilities, threshold)
def get_point_estimates(self, individual, haplotype_library, library_calling_threshold = 0.95, **kwargs) :
called_haplotypes = haplotype_library.get_called_haplotypes(threshold = library_calling_threshold)
mask = self.get_mask(called_haplotypes)
point_estimates = self.njit_get_point_estimates(individual.genotypes, called_haplotypes, self.error, mask)
return point_estimates
@staticmethod
@jit(nopython=True, nogil=True)
def njit_get_point_estimates(genotypes, haplotypes, error, mask):
nHap, nLoci = haplotypes.shape
point_estimates = np.full((nLoci, nHap), 1, dtype = np.float32)
for i in range(nLoci):
if genotypes[i] != 9 and mask[i]:
for j in range(nHap):
sourceGeno = haplotypes[j, i]
if 2*sourceGeno == genotypes[i]:
point_estimates[i, j] = 1-error[i]
else:
point_estimates[i, j] = error[i]
return point_estimates
@staticmethod
@jit(nopython=True, nogil=True)
def transmission(cummulative_probabilities, previous_point_probability, recombination_rate, output):
output[:] = cummulative_probabilities * previous_point_probability
normalize(output)
output[:] *= (1-recombination_rate)
output[:] += recombination_rate
def create_directional_smoothing(self) :
transmission = self.transmission
@jit(nopython=True, nogil=True)
def directional_smoothing(point_estimate, recombination_rate, forward = False, backward = False):
output = np.full(point_estimate.shape, 1, dtype = np.float32)
n_loci = point_estimate.shape[0]
if forward:
start = 1
stop = n_loci
step = 1
if backward:
start = n_loci - 2
stop = -1
step = -1
for i in range(start, stop, step):
transmission(output[i-step,:], point_estimate[i - step,:], recombination_rate[i], output[i,:])
return output
return directional_smoothing
def create_apply_smoothing(self):
directional_smoothing = self.directional_smoothing
@jit(nopython=True, nogil=True)
def apply_smoothing(point_estimate, recombination_rate):
"""Calculate normalized state probabilities at each loci using the forward-backward algorithm"""
est = ( point_estimate *
directional_smoothing(point_estimate, recombination_rate, forward = True) *
directional_smoothing(point_estimate, recombination_rate, backward = True) )
# Return normalized probabilities
normalize_along_first_axis(est)
return est
return apply_smoothing
def create_sampling_algorithm(self, selection_function):
directional_smoothing = self.directional_smoothing
transmission = self.transmission
@jit(nopython=True, nogil=True)
def sample_path(point_estimate, recombination_rate):
"""Calculate normalized state probabilities at each loci using the forward-backward algorithm"""
# Right now using a matrix output; will improve later.
n_loci = point_estimate.shape[0]
output = np.full(point_estimate.shape, 0, dtype = np.float32)
forward_and_point_estimate = point_estimate * directional_smoothing(point_estimate, recombination_rate, forward = True)
# First index.
selected_index = selection_function(forward_and_point_estimate[-1].ravel())
output[- 1].ravel()[selected_index] = 1 # Set the output value at the selected_index to 1.
# Always sample backward (for tradition mostly).
locus_estimate = np.full(point_estimate[0].shape, 0, dtype = np.float32)
matrix_ones = np.full(point_estimate[0].shape, 1, dtype = np.float32)
start = n_loci - 2
stop = -1
step = -1
for i in range(start, stop, step):
# Pass along sampled value at the locus.
transmission(output[i-step,:], matrix_ones, recombination_rate[i], locus_estimate)
# Combine forward_estimate with backward_estimate
locus_estimate *= forward_and_point_estimate[i,:]
selected_index = selection_function(locus_estimate.ravel())
output[i].ravel()[selected_index] = 1 # Set the output value at the selected_index to 1.
# Return probabilities
return output
return sample_path
def create_viterbi_algorithm(self):
maximum_likelihood_step = self.maximum_likelihood_step
@jit(nopython=True, nogil=True)
def viterbi_path(point_estimate, recombination_rate):
"""Calculate normalized state probabilities at each loci using the forward-backward algorithm"""
# Right now using a matrix output; will improve later.
n_loci = point_estimate.shape[0]
path_score = np.full(point_estimate.shape, 0, dtype = np.float32)
previous_index = np.full(point_estimate.shape, 0, dtype = np.int64)
output = np.full(point_estimate.shape, 0, dtype = np.float32)
path_score[0] = point_estimate[0]
start = 1; stop = n_loci; step = 1
for i in range(start, stop, step):
# Pass along sampled value at the locus.
maximum_likelihood_step(path_score[i-step], recombination_rate[i], point_estimate[i], path_score[i], previous_index[i])
# Traceback
start_index = np.argmax(path_score[-1])
output[n_loci-1].ravel()[start_index] = 1
index = start_index
start = n_loci-2; stop = -1; step = -1
for i in range(start, stop, step):
index = previous_index[i-step].ravel()[index]
output[i].ravel()[index] = 1
return output
return viterbi_path
@staticmethod
@jit(nopython=True, nogil=True)
def maximum_likelihood_step(previous_path_score, recombination_rate, point_estimate, output_path_score, output_index):
best_index = np.argmax(previous_path_score)
best_score = previous_path_score[best_index]
n_hap = previous_path_score.shape[0]
for i in range(n_hap):
no_rec_score = (1-recombination_rate)*previous_path_score[i]
rec_score = best_score*recombination_rate
if no_rec_score > rec_score:
# No recombination
output_path_score[i] = no_rec_score*point_estimate[i]
output_index[i] = i
else:
# Recombination
output_path_score[i] = rec_score/n_hap*point_estimate[i]
output_index[i] = best_index
output_path_score /= np.sum(output_path_score)
def calculate_genotype_probabilities(self, total_probs, haplotype_library, **kwargs):
haplotype_dosages = haplotype_library.get_haplotypes()
return self.njit_calculate_genotype_probabilities(total_probs, haplotype_dosages)
@staticmethod
@jit(nopython=True, nogil=True)
def njit_calculate_genotype_probabilities(total_probs, reference_haplotypes) :
n_hap, n_loci = reference_haplotypes.shape
geno_probs = np.full((2, n_loci), 0.0000001, dtype = np.float32) # Adding a very small value as a prior incase all of the values are missing.
for i in range(n_loci):
for j in range(n_hap):
hap_value = reference_haplotypes[j, i]
prob_value = total_probs[i,j]
if hap_value != 9:
# Add in a sum of total_probs values.
geno_probs[0, i] += prob_value * (1-hap_value)
geno_probs[1, i] += prob_value * hap_value
geno_probs = geno_probs/np.sum(geno_probs, axis = 0)
return geno_probs
class DiploidMarkovModel(HaploidMarkovModel) :
def __init__(self, n_loci, error, recombination_rate = None):
HaploidMarkovModel.__init__(self, n_loci, error, recombination_rate)
def extract_reference_panels(self, haplotype_library = None, paternal_haplotype_library = None, maternal_haplotype_library = None) :
if maternal_haplotype_library is not None and paternal_haplotype_library is not None:
seperate_reference_panels = True
return paternal_haplotype_library, maternal_haplotype_library, seperate_reference_panels
else:
seperate_reference_panels = False
return haplotype_library, haplotype_library, seperate_reference_panels
def get_point_estimates(self, individual, library_calling_threshold= 0.95, **kwargs):
paternal_haplotype_library, maternal_haplotype_library, seperate_reference_panels = self.extract_reference_panels(**kwargs)
paternal_called_haplotypes = paternal_haplotype_library.get_called_haplotypes(threshold = library_calling_threshold)
maternal_called_haplotypes = maternal_haplotype_library.get_called_haplotypes(threshold = library_calling_threshold)
mask = self.get_mask(paternal_called_haplotypes) & self.get_mask(maternal_called_haplotypes)
return self.njit_get_point_estimates(individual.genotypes, paternal_called_haplotypes, maternal_called_haplotypes, self.error, mask)
@staticmethod
@jit(nopython=True, nogil=True)
def njit_get_point_estimates(indGeno, paternalHaplotypes, maternalHaplotypes, error, mask):
nPat, nLoci = paternalHaplotypes.shape
nMat, nLoci = maternalHaplotypes.shape
point_estimates = np.full((nLoci, nPat, nMat), 1, dtype = np.float32)
for i in range(nLoci):
if indGeno[i] != 9 and mask[i]:
for j in range(nPat):
for k in range(nMat):
sourceGeno = paternalHaplotypes[j, i] + maternalHaplotypes[k, i]
if sourceGeno == indGeno[i]:
point_estimates[i, j, k] = 1-error[i]
else:
point_estimates[i, j, k] = error[i]
return point_estimates
def calculate_genotype_probabilities(self, total_probs, haplotype_library = None, paternal_haplotype_library= None, maternal_haplotype_library= None, **kwargs):
paternal_haplotype_library, maternal_haplotype_library, seperate_reference_panels = self.extract_reference_panels(haplotype_library, paternal_haplotype_library, maternal_haplotype_library)
return self.njit_calculate_genotype_probabilities(total_probs, paternal_haplotype_library.get_haplotypes(), maternal_haplotype_library.get_haplotypes(), seperate_reference_panels)
@staticmethod
@jit(nopython=True, nogil=True)
def njit_calculate_genotype_probabilities(total_probs, paternal_haplotypes, maternal_haplotypes, seperate_reference_panels) :
n_pat, n_loci = paternal_haplotypes.shape
n_mat, n_loci = maternal_haplotypes.shape
geno_probs = np.full((4, n_loci), 0.00001, dtype = np.float32)
for i in range(n_loci):
for j in range(n_pat):
for k in range(n_mat):
# diploid case where the markers are assumed independent.
if seperate_reference_panels or j != k:
pat_value = paternal_haplotypes[j, i]
mat_value = maternal_haplotypes[k, i]
prob_value = total_probs[i,j,k]
if pat_value != 9 and mat_value != 9:
# Add in a sum of total_probs values.
geno_probs[0, i] += prob_value * (1-pat_value)*(1-mat_value) #aa
geno_probs[1, i] += prob_value * (1-pat_value)*mat_value #aA
geno_probs[2, i] += prob_value * pat_value*(1-mat_value) #Aa
geno_probs[3, i] += prob_value * pat_value*mat_value #AA
# Haploid case where the markers are not independent
else:
hap_value = paternal_haplotypes[j, i]
prob_value = total_probs[i,j,k]
if hap_value != 9:
geno_probs[0, i] += prob_value * (1-hap_value)
geno_probs[1, i] += 0
geno_probs[2, i] += 0
geno_probs[3, i] += prob_value * hap_value
geno_probs = geno_probs/np.sum(geno_probs, axis = 0)
return geno_probs
@staticmethod
@jit(nopython=True, nogil=True)
def transmission(cummulative_probabilities, previous_point_probability, recombination_rate, output):
output[:] = cummulative_probabilities * previous_point_probability
normalize(output)
row_sums = np.sum(output, 0)
col_sums = np.sum(output, 1)
output[:] *= (1 - recombination_rate)**2 # No recombination on either chromosome.
output[:] += np.expand_dims(row_sums, 0)/output.shape[0]*recombination_rate*(1-recombination_rate) # recombination on the maternal (second) chromosome)
output[:] += np.expand_dims(col_sums, 1)/output.shape[1]*recombination_rate*(1-recombination_rate) # recombination on the paternal (first) chromosome)
output[:] += recombination_rate**2/output.size # double recombination
@staticmethod
@jit(nopython=True, nogil=True)
def maximum_likelihood_step(previous_path_score, recombination_rate, point_estimate, output_path_score, output_index):
n_pat = previous_path_score.shape[0]
n_mat = previous_path_score.shape[1]
combined_max_index = np.argmax(previous_path_score)
combined_max_score = previous_path_score.ravel()[combined_max_index] * recombination_rate**2/(n_mat*n_pat)
paternal_max_index = np.full(n_pat, 0, dtype = np.int64)
paternal_max_value = np.full(n_pat, 0, dtype = np.float32)
maternal_max_index = np.full(n_mat, 0, dtype = np.int64)
maternal_max_value = np.full(n_mat, 0, dtype = np.float32)
# Recombination on the maternal side, paternal side is fixed
for i in range(n_pat):
index = np.argmax(previous_path_score[i,:])
paternal_max_value[i] = previous_path_score[i, index] * (1-recombination_rate)*recombination_rate/n_mat
paternal_max_index[i] = i*n_mat + index
# Recombination on the paternal side, maternal side is fixed
for j in range(n_mat):
index = np.argmax(previous_path_score[:, j])
maternal_max_value[j] = previous_path_score[index, j] * (1-recombination_rate)*recombination_rate/n_pat
maternal_max_index[j] = index*n_mat + j
for i in range(n_pat):
for j in range(n_mat):
best_score = (1-recombination_rate)**2*previous_path_score[i,j]
best_index = i*n_mat + j
# Paternal recombination
if paternal_max_value[i] > best_score:
best_score = paternal_max_value[i]
best_index = paternal_max_index[i]
if maternal_max_value[j] > best_score:
best_score = maternal_max_value[j]
best_index = maternal_max_index[j]
if combined_max_score > best_score:
best_score = combined_max_score
best_index = combined_max_index
output_path_score[i,j] = best_score*point_estimate[i,j]
output_index[i,j] = best_index
output_path_score /= np.sum(output_path_score)
class JointMarkovModel(HaploidMarkovModel) :
def __init__(self, n_loci, error, recombination_rate = None):
HaploidMarkovModel.__init__(self, n_loci, error, recombination_rate)
@staticmethod
@jit(nopython=True, nogil=True)
def njit_get_point_estimates(indGeno, haplotypes, error, mask):
n_hap, n_loci = haplotypes.shape
point_estimates = np.full((n_loci, n_hap, n_hap + 1), 1, dtype = np.float32)
diploid_section = point_estimates[:,:,0:-1]
haploid_section = point_estimates[:,:,-1]
# Diploid point estimates/Emission probabilities
for i in range(n_loci):
if indGeno[i] != 9 and mask[i]:
for j in range(n_hap):
for k in range(n_hap):
sourceGeno = haplotypes[j, i] + haplotypes[k, i]
if sourceGeno == indGeno[i]:
diploid_section[i, j, k] = 1-error[i]
else:
diploid_section[i, j, k] = error[i]
# Diploid point estimates/Emission probabilities
for i in range(n_loci):
if indGeno[i] != 9 and mask[i]:
for j in range(n_hap):
sourceGeno = 2*haplotypes[j, i]
if sourceGeno == indGeno[i]:
haploid_section[i, j] = 1-error[i]
else:
haploid_section[i, j] = error[i]
return point_estimates
@staticmethod
@jit(nopython=True, nogil=True)
def njit_calculate_genotype_probabilities(total_probs, reference_haplotypes) :
n_hap, n_loci = reference_haplotypes.shape
geno_probs = np.full((4, n_loci), 0.00001, dtype = np.float32)
diploid_section = total_probs[:,:,0:-1]
haploid_section = total_probs[:,:,-1]
for i in range(n_loci):
for j in range(n_hap):
for k in range(n_hap):
# diploid case where the markers are assumed independent.
if j != k:
pat_value = reference_haplotypes[j, i]
mat_value = reference_haplotypes[k, i]
prob_value = diploid_section[i,j,k]
if pat_value != 9 and mat_value != 9:
# Add in a sum of total_probs values.
geno_probs[0, i] += prob_value * (1-pat_value)*(1-mat_value) #aa
geno_probs[1, i] += prob_value * (1-pat_value)*mat_value #aA
geno_probs[2, i] += prob_value * pat_value*(1-mat_value) #Aa
geno_probs[3, i] += prob_value * pat_value*mat_value #AA
# markers are not independent
else:
hap_value = reference_haplotypes[j, i]
prob_value = diploid_section[i,j,k]
if hap_value != 9:
geno_probs[0, i] += prob_value * (1-hap_value)
geno_probs[1, i] += 0
geno_probs[2, i] += 0
geno_probs[3, i] += prob_value * hap_value
for i in range(n_loci):
for j in range(n_hap):
hap_value = reference_haplotypes[j, i]
prob_value = haploid_section[i,j]
if hap_value != 9:
geno_probs[0, i] += prob_value * (1-hap_value)
geno_probs[1, i] += 0
geno_probs[2, i] += 0
geno_probs[3, i] += prob_value * hap_value
geno_probs = geno_probs/np.sum(geno_probs, axis = 0)
return geno_probs
@staticmethod
@jit(nopython=True, nogil=True)
def transmission(cummulative_probabilities, previous_point_probability, recombination_rate, output):
output[:] = cummulative_probabilities * previous_point_probability
normalize(output)
diploid_section = output[:,0:-1]
haploid_section = output[:,-1]
diploid_weight = np.sum(diploid_section)
haploid_weight = np.sum(haploid_section)
row_sums = np.sum(diploid_section, 0)
col_sums = np.sum(diploid_section, 1)
diploid_section[:] *= (1 - recombination_rate)**2
diploid_section[:] += np.expand_dims(row_sums, 0)/diploid_section.shape[0]*recombination_rate*(1-recombination_rate) # recombination on the maternal (second) chromosome)
diploid_section[:] += np.expand_dims(col_sums, 1)/diploid_section.shape[1]*recombination_rate*(1-recombination_rate) # recombination on the paternal (first) chromosome)
diploid_section[:] += diploid_weight*recombination_rate**2/diploid_section.size # double recombination
haploid_section[:] *= (1 - recombination_rate)
haploid_section[:] += haploid_weight*recombination_rate/haploid_section.size
# loose the recombination to the haploid section and add the haploid recombination to diploid
diploid_section[:] *= (1 - recombination_rate)
diploid_section[:] += recombination_rate * haploid_weight/diploid_section.size
# loose the recombination to the haploid section and add the haploid recombination to diploid
haploid_section[:] *= (1 - recombination_rate)
haploid_section[:] += recombination_rate * diploid_weight/haploid_section.size
# @staticmethod
# @jit(nopython=True, nogil=True)
# def maximum_likelihood_step(previous_path_score, recombination_rate, point_estimate, output_path_score, output_index):
# n_pat = previous_path_score.shape[0]
# n_mat = previous_path_score.shape[1]
# combined_max_index = np.argmax(previous_path_score)
# combined_max_score = previous_path_score.ravel()[combined_max_index] * recombination_rate**2/(n_mat*n_pat)
# paternal_max_index = np.full(n_pat, 0, dtype = np.int64)
# paternal_max_value = np.full(n_pat, 0, dtype = np.float32)
# maternal_max_index = np.full(n_mat, 0, dtype = np.int64)
# maternal_max_value = np.full(n_mat, 0, dtype = np.float32)
# # Recombination on the maternal side, paternal side is fixed
# for i in range(n_pat):
# index = np.argmax(previous_path_score[i,:])
# paternal_max_value[i] = previous_path_score[i, index] * (1-recombination_rate)*recombination_rate/n_mat
# paternal_max_index[i] = i*n_mat + index
# # Recombination on the paternal side, maternal side is fixed
# for j in range(n_mat):
# index = np.argmax(previous_path_score[:, j])
# maternal_max_value[j] = previous_path_score[index, j] * (1-recombination_rate)*recombination_rate/n_pat
# maternal_max_index[j] = index*n_mat + j
# for i in range(n_pat):
# for j in range(n_mat):
# best_score = (1-recombination_rate)**2*previous_path_score[i,j]
# best_index = i*n_mat + j
# # Paternal recombination
# if paternal_max_value[i] > best_score:
# best_score = paternal_max_value[i]
# best_index = paternal_max_index[i]
# if maternal_max_value[j] > best_score:
# best_score = maternal_max_value[j]
# best_index = maternal_max_index[j]
# if combined_max_score > best_score:
# best_score = combined_max_score
# best_index = combined_max_index
# output_path_score[i,j] = best_score*point_estimate[i,j]
# output_index[i,j] = best_index
# output_path_score /= np.sum(output_path_score)
@jit(nopython=True, nogil=True)
def normalize(mat):
mat[:] /= np.sum(mat)
@jit(nopython=True, nogil=True)
def normalize_along_first_axis(mat):
for i in range(mat.shape[0]):
normalize(mat[i,:])