-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
1467 lines (1276 loc) · 53.4 KB
/
model.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
r"""
=====
model
=====
Defines :class:`Model` objects.
"""
import math
import warnings
from functools import partial, reduce
import jax
import jax.numpy as jnp
import numpy as onp
import pandas as pd
import scipy
import seaborn as sns
from frozendict import frozendict
from jax.experimental import sparse
from jaxopt import ProximalGradient
from jaxopt.linear_solve import solve_normal_cg
from matplotlib import pyplot as plt
from scipy.stats import pearsonr
from multidms import Data
import multidms.biophysical
from multidms.plot import _lineplot_and_heatmap
class Model:
r"""
Represent one or more DMS experiments
to obtain tuned parameters that provide insight into
individual mutational effects and conditional shifts
of those effects on all non-reference conditions.
For more see the biophysical model documentation
Parameters
----------
data : multidms.Data
A reference to the dataset which will define the parameters
of the model to be fit.
epistatic_model : <class 'function'>
A function which will transform the latent
effects of mutations into a functional score.
See the biophysical model documentation
for more.
output_activation : <class 'function'>
A function which will transform the output of the
global epistasis function. Defaults to the identity function
(no activation). See the biophysical model documentation
conditional_shifts : bool
If true (default) initialize and fit the shift
parameters for each non-reference condition.
See Model Description section for more.
Defaults to True.
alpha_d : bool
If True introduce a latent offset parameter
for each condition.
See the biophysical docs section for more.
Defaults to True.
gamma_corrected : bool
If true (default), introduce the 'gamma' parameter
for each non-reference parameter to
account for differences between wild type
behavior relative to its variants. This
is essentially a bias added to the functional
scores during fitting.
See Model Description section for more.
Defaults to False.
PRNGKey : int
The initial seed key for random parameters
assigned to Betas and any other randomly
initialized parameters.
for more.
init_beta_naught : float
Initialize the latent offset parameter
applied to all conditions.
See the biophysical docs section for more.
init_theta_scale : float
Initialize the scaling parameter :math:`\theta_{\text{scale}}` of
a two-parameter epistatic model (Sigmoid or Softplus).
init_theta_bias : float
Initialize the bias parameter :math:`\theta_{\text{bias}}` of
a two parameter epistatic model (Sigmoid or Softplus).
n_hidden_units : int or None
If using :func:`multidms.biophysical.nn_global_epistasis`
as the epistatic model, this is the number of hidden units
used in the transform.
lower_bound : float or None
If using :func:`multidms.biophysical.softplus_activation`
as the output activation, this is the lower bound of the
softplus function.
name : str or None
Name of the Model object. If None, will be assigned
a unique name based upon the number of data objects
instantiated.
Example
-------
To create a :class:`Model` object, all you need is
the respective :class:`Data` object for parameter fitting.
>>> import multidms
>>> from tests.test_data import data
>>> model = multidms.Model(data)
Upon initialization, you will now have access to the underlying data
and parameters.
>>> model.data.mutations
('M1E', 'M1W', 'G3P', 'G3R')
>>> model.data.conditions
('a', 'b')
>>> model.data.reference
'a'
>>> model.data.condition_colors
{'a': '#0072B2', 'b': '#CC79A7'}
The mutations_df and variants_df may of course also be accessed.
First, we set pandas to display all rows and columns.
>>> import pandas as pd
>>> pd.set_option('display.max_rows', None)
>>> pd.set_option('display.max_columns', None)
>>> model.data.mutations_df # doctest: +NORMALIZE_WHITESPACE
mutation wts sites muts times_seen_a times_seen_b
0 M1E M 1 E 1 3.0
1 M1W M 1 W 1 0.0
2 G3P G 3 P 1 1.0
3 G3R G 3 R 1 2.0
However, if accessed directly through the :class:`Model` object, you will
get the same information, along with model/parameter specific
features included. These are automatically updated each time you
request the property.
>>> model.get_mutations_df() # doctest: +NORMALIZE_WHITESPACE
beta shift_b predicted_func_score_a predicted_func_score_b \
mutation
M1E 1.816086 0.0 1.800479 1.379661
M1W -0.754885 0.0 -0.901211 -1.322029
G3P 0.339889 0.0 0.420818 0.000000
G3R -0.534835 0.0 -0.653051 -1.073869
<BLANKLINE>
times_seen_a times_seen_b wts sites muts
mutation
M1E 1 3.0 M 1 E
M1W 1 0.0 M 1 W
G3P 1 1.0 G 3 P
G3R 1 2.0 G 3 R
Notice the respective single mutation effects (``"beta"``), conditional shifts
(``shift_d``),
and predicted functional score (``F_d``) of each mutation in the model are now
easily accessible. Similarly, we can take a look at the variants_df for the model,
>>> model.get_variants_df() # doctest: +NORMALIZE_WHITESPACE
condition aa_substitutions func_score var_wrt_ref predicted_latent \
0 a M1E 2.0 M1E 1.816086
1 a G3R -7.0 G3R -0.534835
2 a G3P -0.5 G3P 0.339889
3 a M1W 2.3 M1W -0.754885
4 b M1E 1.0 G3P M1E 1.816086
5 b P3R -5.0 G3R -0.874724
6 b P3G 0.4 -0.339889
7 b M1E P3G 2.7 M1E 1.476197
8 b M1E P3R -2.7 G3R M1E 0.941362
<BLANKLINE>
predicted_func_score
0 1.800479
1 -0.653051
2 0.420818
3 -0.901211
4 1.560311
5 -1.073869
6 -0.420818
7 1.379661
8 0.992495
We now have access to the predicted (and gamma corrected) functional scores
as predicted by the models current parameters.
So far, these parameters and predictions results from them have not been tuned
to the dataset. Let's take a look at the loss on the training dataset
given our initialized parameters
>>> model.loss
Array(4.7124467, dtype=float32)
Next, we fit the model with some chosen hyperparameters.
>>> model.fit(maxiter=1000, lasso_shift=1e-5)
>>> model.loss
Array(6.0517805e-06, dtype=float32)
The model tunes its parameters in place, and the subsequent call to retrieve
the loss reflects our models loss given its updated parameters.
""" # noqa: E501
counter = 0
def __init__(
self,
data: Data,
epistatic_model=multidms.biophysical.sigmoidal_global_epistasis,
output_activation=multidms.biophysical.identity_activation,
conditional_shifts=True,
alpha_d=False,
gamma_corrected=False,
PRNGKey=0,
init_beta_naught=0.0,
init_theta_scale=5.0,
init_theta_bias=-5.0,
n_hidden_units=5,
lower_bound=None,
name=None,
):
"""See class docstring."""
self.gamma_corrected = gamma_corrected
self.conditional_shifts = conditional_shifts
self.alpha_d = alpha_d
self._data = data
self._params = {}
key = jax.random.PRNGKey(PRNGKey)
# initialize beta and shift parameters
# note that the only option is the additive model
# as defined in multidms.biophysical.additive_model
latent_model = multidms.biophysical.additive_model
if latent_model == multidms.biophysical.additive_model:
n_beta_shift = len(self._data.mutations)
self._params["beta"] = jax.random.normal(shape=(n_beta_shift,), key=key)
for condition in data.conditions:
self._params[f"shift_{condition}"] = jnp.zeros(shape=(n_beta_shift,))
self._params[f"alpha_{condition}"] = jnp.zeros(shape=(1,))
self._params["beta_naught"] = jnp.array([init_beta_naught])
else:
raise ValueError(f"{latent_model} not recognized,")
# initialize theta parameters
if epistatic_model == multidms.biophysical.sigmoidal_global_epistasis:
self._params["theta"] = dict(
ge_scale=jnp.array([init_theta_scale]),
ge_bias=jnp.array([init_theta_bias]),
)
elif epistatic_model == multidms.biophysical.softplus_global_epistasis:
if output_activation != multidms.biophysical.softplus_activation:
warnings.warn(
"softplus_global_epistasis has no natural lower bound,"
" we highly suggest using a softplus output activation"
"with a lower bound specified when using this model."
)
self._params["theta"] = dict(
ge_scale=jnp.array([init_theta_scale]),
ge_bias=jnp.array([init_theta_bias]),
)
elif epistatic_model == multidms.biophysical.identity_activation:
self._params["theta"] = dict(ghost_param=jnp.zeros(shape=(1,)))
elif epistatic_model == multidms.biophysical.nn_global_epistasis:
key, key1, key2, key3, key4 = jax.random.split(key, num=5)
self._params["theta"] = dict(
p_weights_1=jax.random.normal(shape=(n_hidden_units,), key=key1).clip(
0
),
p_weights_2=jax.random.normal(shape=(n_hidden_units,), key=key2).clip(
0
),
p_biases=jax.random.normal(shape=(n_hidden_units,), key=key3),
output_bias=jax.random.normal(shape=(1,), key=key4),
)
else:
raise ValueError(f"{epistatic_model} not recognized,")
if output_activation == multidms.biophysical.softplus_activation:
if lower_bound is None:
raise ValueError(
"softplus activation requires a lower bound be specified"
)
if not isinstance(lower_bound, float):
raise ValueError("lower_bound must be a float")
output_activation = partial(
multidms.biophysical.softplus_activation, lower_bound=lower_bound
)
for condition in data.conditions:
self._params[f"gamma_{condition}"] = jnp.zeros(shape=(1,))
# compile the model components
pred = partial(
multidms.biophysical._abstract_epistasis, # abstract function to compile
latent_model,
epistatic_model,
output_activation,
)
from_latent = partial(
multidms.biophysical._abstract_from_latent,
epistatic_model,
output_activation,
)
cost = partial(multidms.biophysical._gamma_corrected_cost_smooth, pred)
self._model_components = frozendict(
{
"latent_model": multidms.biophysical.additive_model,
"g": epistatic_model,
"output_activation": output_activation,
"f": pred,
"from_latent": from_latent,
"objective": cost,
"proximal": multidms.biophysical._lasso_lock_prox,
}
)
self._name = name if isinstance(name, str) else f"Model-{Model.counter}"
Model.counter += 1
def __repr__(self):
"""Returns a string representation of the object."""
return f"{self.__class__.__name__}({self.name})"
def __str__(self):
"""Returns a string representation of the object."""
return f"{self.__class__.__name__}({self.name})"
@property
def name(self) -> str:
"""The name of the data object."""
return self._name
@property
def params(self) -> dict:
"""All current model parameters in a dictionary."""
return self._params
@property
def data(self) -> multidms.Data:
"""
multidms.Data Object this model references for fitting
its parameters.
"""
return self._data
@property
def model_components(self) -> frozendict:
"""
A frozendict which hold the individual components of the model
as well as the objective and forward functions.
"""
return self._model_components
@property
def loss(self) -> float:
"""
Compute model loss on all experimental training data
without ridge or lasso penalties included.
"""
kwargs = {
"scale_coeff_ridge_beta": 0.0,
"scale_coeff_ridge_shift": 0.0,
"scale_coeff_ridge_gamma": 0.0,
"scale_ridge_alpha_d": 0.0,
}
data = (self.data.training_data["X"], self.data.training_data["y"])
return jax.jit(self.model_components["objective"])(self.params, data, **kwargs)
@property
def conditional_loss(self) -> float:
"""Compute loss individually for each condition."""
kwargs = {
"scale_coeff_ridge_beta": 0.0,
"scale_coeff_ridge_shift": 0.0,
"scale_coeff_ridge_gamma": 0.0,
"scale_ridge_alpha_d": 0.0,
}
X, y = self.data.training_data["X"], self.data.training_data["y"]
loss_fxn = jax.jit(self.model_components["objective"])
ret = {}
for condition in self.data.conditions:
condition_data = ({condition: X[condition]}, {condition: y[condition]})
ret[condition] = float(loss_fxn(self.params, condition_data, **kwargs))
ret["total"] = sum(ret.values())
return ret
@property
def variants_df(self):
"""
Kept for backwards compatibility but will be removed in future versions.
Please use `get_variants_df` instead.
"""
warnings.warn("deprecated", DeprecationWarning)
return self.get_variants_df(phenotype_as_effect=False)
def get_variants_df(self, phenotype_as_effect=True):
"""
Training data with model predictions for latent,
and functional score phenotypes.
Parameters
----------
phenotype_as_effect : bool
if True, phenotypes (both latent, and func_score)
are calculated as the _difference_ between predicted
phenotype of a given variant and the respective experimental
wildtype prediction. Otherwise, report the unmodified
model prediction.
Returns
-------
pandas.DataFrame
A copy of the training data, `self.data.variants_df`,
with the phenotypes added. Phenotypes are predicted
based on the current state of the model.
"""
# this is what well update and return
variants_df = self._data.variants_df.copy()
# initialize new columns
for pheno in ["latent", "func_score"]:
variants_df[f"predicted_{pheno}"] = onp.nan
# if we're a gamma corrected model, also report the "corrected"
# observed func score, as we do during training.
if self.gamma_corrected:
variants_df["corrected_func_score"] = variants_df["func_score"]
# get the wildtype predictions for each condition
if phenotype_as_effect:
wildtype_df = self.wildtype_df
models = {
"latent": jax.jit(self.model_components["latent_model"]),
"func_score": jax.jit(self.model_components["f"]),
}
for condition, condition_df in variants_df.groupby("condition"):
d_params = self.get_condition_params(condition)
X = self._data.training_data["X"][condition]
# prediction and effect
for pheno in ["latent", "func_score"]:
Y_pred = onp.array(models[pheno](d_params, X))
if phenotype_as_effect:
Y_pred -= wildtype_df.loc[condition, f"predicted_{pheno}"]
variants_df.loc[condition_df.index, f"predicted_{pheno}"] = Y_pred
if self.gamma_corrected:
variants_df.loc[condition_df.index, "corrected_func_score"] += d_params[
"gamma_d"
]
return variants_df
@property
def mutations_df(self):
"""
Kept for backwards compatibility but will be removed in future versions.
Please use `get_mutations_df` instead.
"""
warnings.warn("deprecated", DeprecationWarning)
return self.get_mutations_df(phenotype_as_effect=False)
def get_mutations_df(
self,
phenotype_as_effect=True,
times_seen_threshold=0,
return_split=True,
):
"""
Mutation attributes and phenotypic effects.
Parameters
----------
phenotype_as_effect : bool, optional
if True, phenotypes (both latent, and func_score)
are calculated as the _difference_ between predicted
phenotype of a given variant and the respective experimental
wildtype prediction. Otherwise, report the unmodified
model prediction.
times_seen_threshold : int, optional
Only report mutations that have been seen at least
this many times in each condition. Defaults to 0.
return_split : bool, optional
If True, return the split mutations as separate columns:
'wts', 'sites', and 'muts'.
Defaults to True.
Returns
-------
pandas.DataFrame
A copy of the mutations data, `self.data.mutations_df`,
with the mutations column set as the index, and columns
with the mutational attributes (e.g. betas, shifts) and
conditional phenotypes (e.g. func_scores) added.
Phenotypes are predicted
based on the current state of the model.
"""
# we're updating this
mutations_df = self.data.mutations_df.set_index("mutation")
if not return_split:
mutations_df.drop(
["wts", "sites", "muts"],
axis=1,
inplace=True,
)
# make sure the mutations_df matches the binarymaps
for condition in self.data.conditions:
assert onp.all(
mutations_df.index.values == self.data.binarymaps[condition].all_subs
), f"mutations_df does not match binarymaps for condition {condition}"
# make sure the indices into the bmap are ordered 0-n
for i, sub in enumerate(mutations_df.index.values):
assert sub == self.data.binarymaps[self.data.reference].i_to_sub(
i
), f"mutation {sub} df index does not match binarymaps respective index"
# for effect calculation
if phenotype_as_effect:
wildtype_df = self.wildtype_df
# add betas i.e. 'latent effect'
mutations_df.loc[:, "beta"] = self._params["beta"]
X = sparse.BCOO.fromdense(onp.identity(len(self._data.mutations)))
for condition in self._data.conditions:
# shift of latent effect
if condition != self._data.reference:
mutations_df[f"shift_{condition}"] = self._params[f"shift_{condition}"]
Y_pred = self.phenotype_frombinary(X, condition)
if phenotype_as_effect:
Y_pred -= wildtype_df.loc[condition, "predicted_func_score"]
mutations_df[f"predicted_func_score_{condition}"] = Y_pred
# filter by times seen
if times_seen_threshold > 0:
for condition in self._data.conditions:
mutations_df = mutations_df[
mutations_df[f"times_seen_{condition}"] >= times_seen_threshold
]
col_order = (
["beta"]
+ [c for c in mutations_df.columns if "shift_" in c]
+ [c for c in mutations_df.columns if "predicted_" in c]
+ [c for c in mutations_df.columns if "times_seen_" in c]
)
if return_split:
col_order += ["wts", "sites", "muts"]
return mutations_df[col_order]
def get_df_loss(self, df, error_if_unknown=False, verbose=False, conditional=False):
"""
Get the loss of the model on a given data frame.
Parameters
----------
df : pandas.DataFrame
Data frame containing variants. Requirements are the same as
those used to initialize the `multidms.Data` object - except
the indices must be unique.
error_if_unknown : bool
If some of the substitutions in a variant are not present in
the model (not in :attr:`AbstractEpistasis.binarymap`)
then by default we do not include those variants
in the loss calculation. If `True`, raise an error.
verbose : bool
If True, print the number of valid and invalid variants.
conditional : bool
If True, return the loss for each condition as a dictionary.
If False, return the total loss.
Returns
-------
float or dict
The loss of the model on the given data frame.
"""
substitutions_col = "aa_substitutions"
condition_col = "condition"
func_score_col = "func_score"
ref_bmap = self.data.binarymaps[self.data.reference]
if substitutions_col not in df.columns:
raise ValueError("`df` lacks `substitutions_col` " f"{substitutions_col}")
if condition_col not in df.columns:
raise ValueError("`df` lacks `condition_col` " f"{condition_col}")
loss_fxn = jax.jit(self.model_components["objective"])
ret = {}
for condition, condition_df in df.groupby(condition_col):
X, y = {}, {}
variant_subs = condition_df[substitutions_col]
if condition not in self.data.reference_sequence_conditions:
variant_subs = condition_df.apply(
lambda x: self.data.convert_subs_wrt_ref_seq(
condition, x[substitutions_col]
),
axis=1,
)
# build binary variants as csr matrix, make prediction, and append
valid, invalid = 0, 0 # row indices of elements that are one
# binary_variants = []
variant_targets = []
row_ind = [] # row indices of elements that are one
col_ind = [] # column indices of elements that are one
for subs, target in zip(variant_subs, condition_df[func_score_col]):
try:
# binary_variants.append(ref_bmap.sub_str_to_binary(subs))
# variant_targets.append(target)
# valid += 1
for isub in ref_bmap.sub_str_to_indices(subs):
row_ind.append(valid)
col_ind.append(isub)
variant_targets.append(target)
valid += 1
except ValueError:
if error_if_unknown:
raise ValueError(
"Variant has substitutions not in model:"
f"\n{subs}\nMaybe use `unknown_as_nan`?"
)
else:
invalid += 1
if verbose:
print(
f"condition: {condition}, n valid variants: "
f"{valid}, n invalid variants: {invalid}"
)
# X[condition] = sparse.BCOO.from_scipy_sparse(
# scipy.sparse.csr_matrix(onp.vstack(binary_variants))
# )
X[condition] = sparse.BCOO.from_scipy_sparse(
scipy.sparse.csr_matrix(
(onp.ones(len(row_ind), dtype="int8"), (row_ind, col_ind)),
shape=(valid, ref_bmap.binarylength),
dtype="int8",
)
)
y[condition] = jnp.array(variant_targets)
ret[condition] = float(loss_fxn(self.params, (X, y)))
ret["total"] = sum(ret.values())
if not conditional:
return ret["total"]
return ret
def add_phenotypes_to_df(
self,
df,
substitutions_col="aa_substitutions",
condition_col="condition",
latent_phenotype_col="predicted_latent",
observed_phenotype_col="predicted_func_score",
converted_substitutions_col="aa_subs_wrt_ref",
overwrite_cols=False,
unknown_as_nan=False,
phenotype_as_effect=True,
):
"""Add predicted phenotypes to data frame of variants.
Parameters
----------
df : pandas.DataFrame
Data frame containing variants. Requirements are the same as
those used to initialize the `multidms.Data` object - except
the indices must be unique.
substitutions_col : str
Column in `df` giving variants as substitution strings
with respect to a given variants condition.
These will be converted to be with respect to the reference sequence
prior to prediction. Defaults to 'aa_substitutions'.
condition_col : str
Column in `df` giving the condition from which a variant was
observed. Values must exist in the self.data.conditions and
and error will be raised otherwise. Defaults to 'condition'.
latent_phenotype_col : str
Column added to `df` containing predicted latent phenotypes.
observed_phenotype_col : str
Column added to `df` containing predicted observed phenotypes.
converted_substitutions_col : str or None
Columns added to `df` containing converted substitution strings
for non-reference conditions if they do not share a wildtype seq.
overwrite_cols : bool
If the specified latent or observed phenotype column already
exist in `df`, overwrite it? If `False`, raise an error.
unknown_as_nan : bool
If some of the substitutions in a variant are not present in
the model (not in :attr:`AbstractEpistasis.binarymap`) set the
phenotypes to `nan` (not a number)? If `False`, raise an error.
phenotype_as_effect : bool
if True, phenotypes (both latent, and func_score)
are calculated as the _difference_ between predicted
phenotype of a given variant and the respective experimental
wildtype prediction. Otherwise, report the unmodified
model prediction.
Returns
-------
pandas.DataFrame
A copy of `df` with the phenotypes added. Phenotypes are predicted
based on the current state of the model.
"""
ref_bmap = self.data.binarymaps[self.data.reference]
if substitutions_col is None:
substitutions_col = ref_bmap.substitutions_col
if substitutions_col not in df.columns:
raise ValueError("`df` lacks `substitutions_col` " f"{substitutions_col}")
if condition_col not in df.columns:
raise ValueError("`df` lacks `condition_col` " f"{condition_col}")
if not df.index.is_unique:
raise ValueError("`df` must have unique indices")
# return copy
ret = df.copy()
if phenotype_as_effect:
wildtype_df = self.wildtype_df
# initialize new columns
for col in [
latent_phenotype_col,
observed_phenotype_col,
converted_substitutions_col,
]:
if col is None:
continue
if col in df.columns and not overwrite_cols:
raise ValueError(f"`df` already contains column {col}")
ret[col] = onp.nan
if converted_substitutions_col is not None:
ret[converted_substitutions_col] = ""
for condition, condition_df in df.groupby(condition_col):
variant_subs = condition_df[substitutions_col]
if condition not in self.data.reference_sequence_conditions:
variant_subs = condition_df.apply(
lambda x: self.data.convert_subs_wrt_ref_seq(
condition, x[substitutions_col]
),
axis=1,
)
if converted_substitutions_col is not None:
ret.loc[condition_df.index, converted_substitutions_col] = variant_subs
# build binary variants as csr matrix, make prediction, and append
row_ind = [] # row indices of elements that are one
col_ind = [] # column indices of elements that are one
nan_variant_indices = [] # indices of variants that are nan
for ivariant, subs in enumerate(variant_subs):
try:
for isub in ref_bmap.sub_str_to_indices(subs):
row_ind.append(ivariant)
col_ind.append(isub)
except ValueError:
if unknown_as_nan:
nan_variant_indices.append(ivariant)
else:
raise ValueError(
"Variant has substitutions not in model:"
f"\n{subs}\nMaybe use `unknown_as_nan`?"
)
X = sparse.BCOO.from_scipy_sparse(
scipy.sparse.csr_matrix(
(onp.ones(len(row_ind), dtype="int8"), (row_ind, col_ind)),
shape=(len(condition_df), ref_bmap.binarylength),
dtype="int8",
)
)
# latent predictions on binary variants, X
latent_predictions = onp.array(
self.latent_frombinary(X, condition=condition)
)
assert len(latent_predictions) == len(condition_df)
if phenotype_as_effect:
latent_predictions -= wildtype_df.loc[condition, "predicted_latent"]
latent_predictions[nan_variant_indices] = onp.nan
ret.loc[
condition_df.index.values, latent_phenotype_col
] = latent_predictions
# func_score predictions on binary variants, X
phenotype_predictions = onp.array(
self.phenotype_frombinary(X, condition=condition)
)
assert len(phenotype_predictions) == len(condition_df)
if phenotype_as_effect:
phenotype_predictions -= wildtype_df.loc[
condition, "predicted_func_score"
]
phenotype_predictions[nan_variant_indices] = onp.nan
ret.loc[
condition_df.index.values, observed_phenotype_col
] = phenotype_predictions
return ret
@property
def wildtype_df(self):
"""
Get a dataframe indexed by condition wildtype
containing the prediction features for each.
"""
wildtype_df = (
pd.DataFrame(index=self.data.conditions)
.assign(predicted_latent=onp.nan)
.assign(predicted_func_score=onp.nan)
)
for condition in self.data.conditions:
for pheno, model in zip(
["latent", "func_score"],
[self.latent_fromsubs, self.phenotype_fromsubs],
):
wildtype_df.loc[condition, f"predicted_{pheno}"] = model("", condition)
return wildtype_df
def mutation_site_summary_df(self, agg_func=onp.mean, times_seen_threshold=0):
"""
Get all single mutational attributes from self._data
updated with all model specific attributes, then aggregate
all numerical columns by "sites" using
``agg`` function. The mean values are given by default.
"""
numerics = ["int16", "int32", "int64", "float16", "float32", "float64"]
mut_df = self.mutations_df.select_dtypes(include=numerics)
times_seen_cols = [c for c in mut_df.columns if "times" in c]
for c in times_seen_cols:
mut_df = mut_df[mut_df[c] >= times_seen_threshold]
return mut_df.groupby("sites").aggregate(agg_func)
def get_condition_params(self, condition=None):
"""Get the relent parameters for a model prediction"""
condition = self.data.reference if condition is None else condition
if condition not in self.data.conditions:
raise ValueError(f"condition {condition} does not exist in model")
return {
"theta": self.params["theta"],
"beta_m": self.params["beta"],
"beta_naught": self.params["beta_naught"],
"s_md": self.params[f"shift_{condition}"],
"alpha_d": self.params[f"alpha_{condition}"],
"gamma_d": self.params[f"gamma_{condition}"],
}
def phenotype_fromsubs(self, aa_subs, condition=None):
"""
take a single string of subs which are
not already converted wrt reference, convert them and
then make a functional score prediction and return the result.
"""
converted_subs = self.data.convert_subs_wrt_ref_seq(condition, aa_subs)
X = jnp.array(
[
self.data.binarymaps[self.data.reference].sub_str_to_binary(
converted_subs
)
]
)
return self.phenotype_frombinary(X, condition)
def latent_fromsubs(self, aa_subs, condition=None):
"""
take a single string of subs which are
not already converted wrt reference, convert them and
then make a latent prediction and return the result.
"""
converted_subs = self.data.convert_subs_wrt_ref_seq(condition, aa_subs)
X = jnp.array(
[
self.data.binarymaps[self.data.reference].sub_str_to_binary(
converted_subs
)
]
)
return self.latent_frombinary(X, condition)
def phenotype_frombinary(self, X, condition=None):
"""
Condition specific functional score prediction
on X using the biophysical model
given current model parameters.
Parameters
----------
X : jnp.array
Binary encoded variants to make predictions on.
condition : str
Condition to make predictions for. If None, use the reference
"""
d_params = self.get_condition_params(condition)
return jax.jit(self.model_components["f"])(d_params, X)
def latent_frombinary(self, X, condition=None):
"""
Condition specific latent phenotype prediction
on X using the biophysical model
given current model parameters.
Parameters
----------
X : jnp.array
Binary encoded variants to make predictions on.
condition : str
Condition to make predictions for. If None, use the reference
"""
d_params = self.get_condition_params(condition)
return jax.jit(self.model_components["latent_model"])(d_params, X)
def fit_reference_beta(self, **kwargs):
"""
Fit the Model beta's to the reference data.
This is an experimental feature and is not recommended
for general use.
"""
ref_X = self.data.training_data["X"][self.data.reference]
ref_y = self.data.training_data["y"][self.data.reference]
self._params["beta"] = solve_normal_cg(
lambda beta: ref_X @ beta, ref_y, init=self._params["beta"], **kwargs
)
def fit(self, lasso_shift=1e-5, tol=1e-6, maxiter=1000, lock_params={}, **kwargs):
r"""
Use jaxopt.ProximalGradiant to optimize the model's free parameters.
Parameters
----------
lasso_shift : float
L1 penalty on the shift parameters. Defaults to 1e-5.
tol : float
Tolerance for the optimization. Defaults to 1e-6.
maxiter : int
Maximum number of iterations for the optimization. Defaults to 1000.
lock_params : dict
Dictionary of parameters, and desired value to constrain
them at during optimization. By default, none of the parameters
besides the reference shift, and reference latent offset are locked.
**kwargs : dict
Additional keyword arguments passed to the objective function.
These include hyperparameters like a ridge penalty on beta, shift, and gamma
as well as huber loss scaling.
"""
solver = ProximalGradient(
jax.jit(self._model_components["objective"]),
jax.jit(self._model_components["proximal"]),
tol=tol,
maxiter=maxiter,
)
lock_params[f"shift_{self._data.reference}"] = jnp.zeros(
len(self._params["beta"])
)
lock_params[f"gamma_{self._data.reference}"] = jnp.zeros(shape=(1,))
if not self.conditional_shifts:
for condition in self._data.conditions:
lock_params[f"shift_{condition}"] = jnp.zeros(shape=(1,))
if not self.gamma_corrected:
for condition in self._data.conditions:
lock_params[f"gamma_{condition}"] = jnp.zeros(shape=(1,))
if not self.alpha_d:
for condition in self._data.conditions:
lock_params[f"alpha_{condition}"] = jnp.zeros(shape=(1,))
else: