|
| 1 | +"""Tests for helix_propensity_score() (Chou-Fasman Pα) and its integration.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from openamp_foundry.features.physchem import compute_features, helix_propensity_score |
| 5 | +from openamp_foundry.scoring.activity import activity_likeness_score |
| 6 | + |
| 7 | + |
| 8 | +class TestHelixPropensityScore: |
| 9 | + def test_empty_sequence_returns_zero(self): |
| 10 | + assert helix_propensity_score("") == 0.0 |
| 11 | + |
| 12 | + def test_single_ala_returns_142(self): |
| 13 | + # Ala is the strongest helix-former: Pα = 1.42 |
| 14 | + result = helix_propensity_score("A") |
| 15 | + assert result == 1.42 |
| 16 | + |
| 17 | + def test_single_gly_low_propensity(self): |
| 18 | + # Gly is helix-breaking: Pα = 0.57 |
| 19 | + assert helix_propensity_score("G") == 0.57 |
| 20 | + |
| 21 | + def test_single_pro_low_propensity(self): |
| 22 | + # Pro is helix-breaking: Pα = 0.57 |
| 23 | + assert helix_propensity_score("P") == 0.57 |
| 24 | + |
| 25 | + def test_poly_ala_high_propensity(self): |
| 26 | + result = helix_propensity_score("AAAAAAAAAA") |
| 27 | + assert result == 1.42 |
| 28 | + |
| 29 | + def test_poly_glu_highest_propensity(self): |
| 30 | + # Glu is the strongest helix-former in the table: Pα = 1.51 |
| 31 | + result = helix_propensity_score("EEEEEEEEEE") |
| 32 | + assert result == 1.51 |
| 33 | + |
| 34 | + def test_poly_gly_helix_breaking(self): |
| 35 | + result = helix_propensity_score("GGGGGGGGGG") |
| 36 | + assert result == 0.57 |
| 37 | + |
| 38 | + def test_helix_forming_gt_breaking(self): |
| 39 | + # Ala-rich > Gly-rich |
| 40 | + assert helix_propensity_score("AAAAAAA") > helix_propensity_score("GGGGGGG") |
| 41 | + |
| 42 | + def test_indifferent_near_1p0(self): |
| 43 | + # His has Pα = 1.0 (exactly indifferent) |
| 44 | + result = helix_propensity_score("H") |
| 45 | + assert result == 1.0 |
| 46 | + |
| 47 | + def test_mean_computed_correctly(self): |
| 48 | + # A (1.42) + G (0.57) = mean 0.995 |
| 49 | + result = helix_propensity_score("AG") |
| 50 | + assert abs(result - round((1.42 + 0.57) / 2, 4)) < 1e-6 |
| 51 | + |
| 52 | + def test_unknown_residue_uses_indifferent_1p0(self): |
| 53 | + # X not in table → default 1.0 |
| 54 | + # A (1.42) + X (1.0) = mean 1.21 |
| 55 | + result = helix_propensity_score("AX") |
| 56 | + assert abs(result - round((1.42 + 1.0) / 2, 4)) < 1e-6 |
| 57 | + |
| 58 | + def test_returns_float_rounded_to_4dp(self): |
| 59 | + result = helix_propensity_score("KWKLFKK") |
| 60 | + assert isinstance(result, float) |
| 61 | + assert result == round(result, 4) |
| 62 | + |
| 63 | + def test_classic_amp_above_1p0(self): |
| 64 | + # KWKLFKKIGAVLKVL is a well-characterized helical AMP; should have Pα > 1.0 |
| 65 | + result = helix_propensity_score("KWKLFKKIGAVLKVL") |
| 66 | + assert result > 1.0 |
| 67 | + |
| 68 | + def test_proline_rich_below_1p0(self): |
| 69 | + # GNRPVYIPQPRPPHPR (apidaecin) — many P residues → helix-breaking |
| 70 | + result = helix_propensity_score("GNRPVYIPQPRPPHPR") |
| 71 | + assert result < 1.0 |
| 72 | + |
| 73 | + |
| 74 | +class TestComputeFeaturesHelixPropensity: |
| 75 | + def test_helix_propensity_key_present(self): |
| 76 | + features = compute_features("KWKLFKKIGAVLKVL") |
| 77 | + assert "helix_propensity" in features |
| 78 | + |
| 79 | + def test_helix_propensity_is_float(self): |
| 80 | + features = compute_features("KWKLFKKIGAVLKVL") |
| 81 | + assert isinstance(features["helix_propensity"], float) |
| 82 | + |
| 83 | + def test_helix_propensity_positive(self): |
| 84 | + features = compute_features("KWKLFKKIGAVLKVL") |
| 85 | + assert features["helix_propensity"] > 0.0 |
| 86 | + |
| 87 | + def test_helix_propensity_reasonable_range(self): |
| 88 | + # Realistic sequences: Pα range 0.57–1.51, typical AMPs ~0.95–1.15 |
| 89 | + features = compute_features("KWKLFKKIGAVLKVL") |
| 90 | + assert 0.5 <= features["helix_propensity"] <= 1.6 |
| 91 | + |
| 92 | + def test_empty_sequence_no_crash(self): |
| 93 | + features = compute_features("") |
| 94 | + assert features["helix_propensity"] == 0.0 |
| 95 | + |
| 96 | + def test_helical_amp_vs_proline_rich(self): |
| 97 | + # Helical AMP (magainin-like) should have higher Pα than proline-rich |
| 98 | + helical_features = compute_features("GIGKFLHSAKKFGKAFVGEIMNS") |
| 99 | + proline_features = compute_features("GNRPVYIPQPRPPHPR") |
| 100 | + assert helical_features["helix_propensity"] > proline_features["helix_propensity"] |
| 101 | + |
| 102 | + |
| 103 | +class TestActivityScoreWithHelixPropensity: |
| 104 | + def test_activity_score_still_in_0_1(self): |
| 105 | + features = compute_features("KWKLFKKIGAVLKVL") |
| 106 | + score = activity_likeness_score(features) |
| 107 | + assert 0.0 <= score <= 1.0 |
| 108 | + |
| 109 | + def test_helix_forming_amp_gets_bonus(self): |
| 110 | + # KWKLFKKIGAVLKVL (Leu-rich helical AMP) vs GGGGGGGGGG (helix-breaker) |
| 111 | + # The helix-forming sequence should score higher |
| 112 | + helical_features = compute_features("KWKLFKKIGAVLKVL") |
| 113 | + gly_features = compute_features("GGGGGGGGGG") |
| 114 | + assert activity_likeness_score(helical_features) > activity_likeness_score(gly_features) |
| 115 | + |
| 116 | + def test_fallback_when_helix_propensity_missing(self): |
| 117 | + # If helix_propensity is absent (old feature dict), should not crash |
| 118 | + minimal_features = { |
| 119 | + "length": 15, "charge_density": 0.27, "hydrophobic_fraction": 0.47, |
| 120 | + "aromatic_fraction": 0.07, "hydrophobic_moment": 0.55, |
| 121 | + } |
| 122 | + score = activity_likeness_score(minimal_features) |
| 123 | + assert 0.0 <= score <= 1.0 |
0 commit comments