|
| 1 | +"""Tests verifying that known problematic sequences are down-ranked. |
| 2 | +
|
| 3 | +Per AGENTS.md Phase 2: 'Toxicity penalty — Predicted hemolytic/toxic candidates |
| 4 | +are down-ranked.' These tests check that the safety and activity scores penalise |
| 5 | +sequences with properties associated with mammalian toxicity risk: |
| 6 | +- Extreme hydrophobicity (hemolysis risk) |
| 7 | +- All-cysteine (aggregation, disulfide chaos) |
| 8 | +- Purely negative charge (anti-AMP, repelled by bacterial membranes) |
| 9 | +- Very long repeat runs (low complexity, synthesis issues) |
| 10 | +
|
| 11 | +No claims of actual biological toxicity are made. These are heuristic proxy checks. |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | + |
| 16 | +from openamp_foundry.features.physchem import compute_features |
| 17 | +from openamp_foundry.scoring.activity import activity_likeness_score |
| 18 | +from openamp_foundry.scoring.safety import safety_score |
| 19 | +from openamp_foundry.scoring.synthesis import synthesis_feasibility_score |
| 20 | + |
| 21 | + |
| 22 | +def score_seq(seq: str) -> dict: |
| 23 | + f = compute_features(seq) |
| 24 | + return { |
| 25 | + "features": f, |
| 26 | + "activity": activity_likeness_score(f), |
| 27 | + "safety": safety_score(f), |
| 28 | + "synthesis": synthesis_feasibility_score(f, valid_sequence=True), |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | +REFERENCE_AMP = "KWKLFKKIGAVLKVL" # Magainin analogue, classic AMP |
| 33 | + |
| 34 | + |
| 35 | +class TestActivityPenalization: |
| 36 | + def test_negative_charge_sequence_has_low_activity(self): |
| 37 | + # Purely negative charge — repelled by bacterial membranes |
| 38 | + result = score_seq("DEDEDEDEDEDE") |
| 39 | + assert result["activity"] < score_seq(REFERENCE_AMP)["activity"] |
| 40 | + |
| 41 | + def test_low_charge_sequence_has_low_activity(self): |
| 42 | + # No charge → low charge_density → low charge_score |
| 43 | + result = score_seq("AAAAAAAAGGGGGGGG") |
| 44 | + ref = score_seq(REFERENCE_AMP) |
| 45 | + assert result["activity"] < ref["activity"] |
| 46 | + |
| 47 | + def test_reference_amp_scores_above_low_charge(self): |
| 48 | + ref = score_seq(REFERENCE_AMP) |
| 49 | + bad = score_seq("GGGGGGGGGGGG") |
| 50 | + assert ref["activity"] > bad["activity"] |
| 51 | + |
| 52 | + |
| 53 | +class TestSafetyPenalization: |
| 54 | + def test_extreme_hydrophobicity_penalizes_safety(self): |
| 55 | + # LLLLLLLLLLLL — very hydrophobic → hemolysis risk proxy |
| 56 | + result = score_seq("LLLLLLLLLLLL") |
| 57 | + assert result["safety"] < 1.0 |
| 58 | + |
| 59 | + def test_high_cysteine_penalizes_safety(self): |
| 60 | + # All cys → risk of aggregation/disulfide chaos |
| 61 | + result = score_seq("CCCCCCCCCCCC") |
| 62 | + assert result["safety"] < 0.9 |
| 63 | + |
| 64 | + def test_very_long_repeat_run_penalizes_safety(self): |
| 65 | + # 8+ same residue in a row triggers safety penalty |
| 66 | + result = score_seq("KKKKKKKKLLLL") |
| 67 | + assert result["safety"] < 1.0 |
| 68 | + |
| 69 | + def test_reference_amp_has_high_safety(self): |
| 70 | + # Known AMP with moderate hydrophobicity should score well |
| 71 | + result = score_seq(REFERENCE_AMP) |
| 72 | + assert result["safety"] >= 0.8 |
| 73 | + |
| 74 | + def test_pure_negative_charge_has_lower_safety_than_amp(self): |
| 75 | + neg = score_seq("DEDEDEDEDEDE") |
| 76 | + ref = score_seq(REFERENCE_AMP) |
| 77 | + # Safety differs because charge_density penalizes extremes |
| 78 | + assert ref["safety"] >= neg["safety"] |
| 79 | + |
| 80 | + |
| 81 | +class TestSynthesisPenalization: |
| 82 | + def test_very_long_sequence_penalizes_synthesis(self): |
| 83 | + long_seq = "KWKLFKKIGAVLKVL" * 3 # 45 residues |
| 84 | + result = score_seq(long_seq) |
| 85 | + assert result["synthesis"] < 1.0 |
| 86 | + |
| 87 | + def test_high_cysteine_penalizes_synthesis(self): |
| 88 | + result = score_seq("CCCCCCCCCCCC") |
| 89 | + assert result["synthesis"] < 0.9 |
| 90 | + |
| 91 | + def test_short_repeat_penalizes_synthesis(self): |
| 92 | + result = score_seq("KKKKKKKKK") # long repeat run |
| 93 | + assert result["synthesis"] < 1.0 |
| 94 | + |
| 95 | + def test_reference_amp_fully_feasible(self): |
| 96 | + # 15-residue AMP with no special challenges should be fully feasible |
| 97 | + result = score_seq(REFERENCE_AMP) |
| 98 | + assert result["synthesis"] == 1.0 |
| 99 | + |
| 100 | + |
| 101 | +class TestNegativesVsPositivesSummary: |
| 102 | + """Aggregate check: negatives from demo should average below known AMPs.""" |
| 103 | + |
| 104 | + KNOWN_AMPS = [ |
| 105 | + "KWKLFKKIGAVLKVL", |
| 106 | + "GIGKFLHSAKKFGKAFVGEIMNS", |
| 107 | + "GLFDIVKKVVGALGSL", |
| 108 | + "RRWQWRMKKLG", |
| 109 | + ] |
| 110 | + KNOWN_NEGATIVES = [ |
| 111 | + "AAAAAAAAAAAA", |
| 112 | + "DEDEDEDEDEDE", |
| 113 | + "GGGGGGGGGGGG", |
| 114 | + ] |
| 115 | + |
| 116 | + def _avg_activity(self, seqs: list[str]) -> float: |
| 117 | + scores = [score_seq(s)["activity"] for s in seqs] |
| 118 | + return sum(scores) / len(scores) |
| 119 | + |
| 120 | + def test_amp_average_activity_exceeds_negative_average(self): |
| 121 | + amp_avg = self._avg_activity(self.KNOWN_AMPS) |
| 122 | + neg_avg = self._avg_activity(self.KNOWN_NEGATIVES) |
| 123 | + assert amp_avg > neg_avg, ( |
| 124 | + f"AMP avg activity {amp_avg:.4f} should exceed negative avg {neg_avg:.4f}" |
| 125 | + ) |
| 126 | + |
| 127 | + def test_amp_activity_margin_over_negatives(self): |
| 128 | + amp_avg = self._avg_activity(self.KNOWN_AMPS) |
| 129 | + neg_avg = self._avg_activity(self.KNOWN_NEGATIVES) |
| 130 | + margin = amp_avg - neg_avg |
| 131 | + assert margin >= 0.15, f"Activity margin {margin:.4f} below minimum 0.15" |
0 commit comments