SeqMut.combine

SeqMut.combine(df_seq, variants, df_feat, jmd_n_len=10, jmd_c_len=10, constraints=None, lineage=False)[source]

Score combined (multi-mutation) variants by applying their mutations together.

Each variant groups several point mutations that are applied to the same sequence, yielding one combined sequence whose ΔCPP (and, with a bound model, prediction shift delta_pred) is measured once. This is how 2-3 mutations are combined, in contrast to SeqMut.mutate(), which scores every point mutation independently against the wild-type.

Parameters:
  • df_seq (pd.DataFrame, shape (n_samples, n_seq_info)) – DataFrame containing an entry column with unique protein identifiers, in the position-based format (sequence, tmd_start, tmd_stop). See SequenceFeature.get_df_parts() for the full df_seq format specification.

  • variants (pd.DataFrame, shape (n_mutations, >=4)) – Tidy table with columns entry, variant (a grouping id), pos (1-based) and to_aa. Rows sharing the same entry and variant are applied together as one combined variant; from_aa is derived and checked, and a variant must mutate distinct positions.

  • df_feat (pd.DataFrame) – CPP feature set (output of CPP.run()) defining the features measured over.

  • jmd_n_len (int, default=10) – Length of JMD-N in number of amino acids.

  • jmd_c_len (int, default=10) – Length of JMD-C in number of amino acids.

  • constraints (DesignConstraints, optional) –

    Shared design limits (see DesignConstraints). Each combined variant is a full candidate sequence, so every limit applies, including n_mut_max, the identity bounds and the motifs. Nothing is dropped: two columns are appended instead, is_feasible and reasons (the '; '-joined rejection reasons, empty for a feasible variant), so a discarded candidate carries its explanation. None (default) applies no limit and appends no column.

    Added in version 1.2.0.

  • lineage (bool or dict, default=False) –

    Opt in to the candidate-lineage record: the thin, JSON-serializable record of how each candidate was made (its content-hash candidate_id, its parent, the ordered parent-relative mutations, the generating method, the objective values, the effective seed, and a digest of the applied constraints). False (default) builds nothing and leaves the returned table exactly as documented above. True builds one record per scored variant, treating the df_seq sequence as the root of the chain. Passing a lineage record (one element of a previous SeqMut.lineage_ or SeqOpt.lineage_) opts in and declares that parent: its candidate_id becomes the parent_id of every record built here, which is how the rounds of a multi-generation design chain up. When opted in, the records are stored in SeqMut.lineage_, row-aligned with the returned table, and the candidate_id column is appended.

    Added in version 1.2.0.

Returns:

df_variant – One row per combined variant with entry, variant (the '+'-joined single mutations, e.g. "R20K+K27P"), n_mut, sequence_mut, delta_cpp and shift_score — plus delta_pred when a model is bound — sorted by descending delta_pred (model) or shift_score (model-free). When constraints is given, the is_feasible and reasons columns are appended, and when lineage is opted in, the candidate_id column is appended.

Return type:

pd.DataFrame, shape (n_variants, 6)

Raises:

ValueError – If df_seq is not in the position-based format, if variants does not match it (unknown entry, out-of-range position, non-canonical to_aa, or two mutations of one variant at the same position), if df_feat does not match the bound model, if constraints is not a DesignConstraints object, or if lineage is neither a bool nor a lineage record (or names a parent while variants of several sequences are scored).

See also

Examples

Binding a fitted model makes :class:SeqMut model-aware: each scored mutation carries delta_pred — the change of the model prediction score (percentage points) it induces. :meth:SeqMut.combine applies a named set of point mutations to the same sequence and scores each combined variant once, so 2-3 mutations are evaluated together (unlike :meth:SeqMut.mutate, which scores each independently).

import itertools
import pandas as pd
import matplotlib.pyplot as plt
import aaanalysis as aa
aa.options["verbose"] = False

# Data, CPP features, and a fitted TreeModel that scores each sequence
df_seq = aa.load_dataset(name="DOM_GSEC", n=10)
labels = df_seq["label"].to_list()
sf = aa.SequenceFeature()
df_parts = sf.get_df_parts(df_seq=df_seq)
split_kws = sf.get_split_kws()
df_scales = aa.load_scales()
cpp = aa.CPP(df_parts=df_parts, split_kws=split_kws, df_scales=df_scales, verbose=False)
df_feat = cpp.run(labels=labels, n_filter=25)
X = sf.feature_matrix(features=list(df_feat["feature"]), df_parts=df_parts, df_scales=df_scales)
tm = aa.TreeModel().fit(X, labels=labels)
entry = df_seq["entry"].iloc[0]
ts = int(df_seq.set_index("entry").loc[entry, "tmd_start"])

seqm = aa.SeqMut(model=tm)
variants = pd.DataFrame({
    "entry": [entry] * 5,
    "variant": ["double", "double", "triple", "triple", "triple"],
    "pos": [ts, ts + 1, ts, ts + 2, ts + 4],
    "to_aa": ["A", "P", "A", "K", "L"],
})
df_variant = seqm.combine(df_seq=df_seq, variants=variants, df_feat=df_feat)
aa.display_df(df_variant, n_rows=10, show_shape=True)
DataFrame shape: (2, 7)
  entry variant n_mut sequence_mut delta_cpp shift_score delta_pred
1 Q14802 L37A+V39K+G41L 3 MQKVTLGLLVFLAGF...PGETPPLITPGSAQS 0.326320 -0.055680 2.500000
2 Q14802 L37A+Q38P 2 MQKVTLGLLVFLAGF...PGETPPLITPGSAQS 0.066000 0.066000 2.000000

The junction-membrane-domain lengths jmd_n_len / jmd_c_len set how many flanking residues are used when rebuilding the sequence parts.

df_variant2 = seqm.combine(df_seq=df_seq, variants=variants, df_feat=df_feat,
                            jmd_n_len=10, jmd_c_len=10)
aa.display_df(df_variant2, n_rows=10, show_shape=True)
DataFrame shape: (2, 7)
  entry variant n_mut sequence_mut delta_cpp shift_score delta_pred
1 Q14802 L37A+V39K+G41L 3 MQKVTLGLLVFLAGF...PGETPPLITPGSAQS 0.326320 -0.055680 2.500000
2 Q14802 L37A+Q38P 2 MQKVTLGLLVFLAGF...PGETPPLITPGSAQS 0.066000 0.066000 2.000000

Design constraints. A combined variant is a full candidate sequence, so every limit of a shared DesignConstraints applies. Nothing is dropped: constraints appends an is_feasible column and a reasons column naming each violated limit, so a rejected candidate says why:

dc = aa.DesignConstraints(n_mut_max=2, min_identity=0.9, forbidden_substitutions=["P"])
df_variant_dc = seqm.combine(df_seq=df_seq, variants=variants, df_feat=df_feat,
                             constraints=dc)
aa.display_df(df_variant_dc[["variant", "n_mut", "is_feasible", "reasons"]],
              n_rows=10, show_shape=True)
DataFrame shape: (2, 4)
  variant n_mut is_feasible reasons
1 L37A+V39K+G41L 3 False n_mut_max: 3 mu...he maximum of 2
2 L37A+Q38P 2 False forbidden_subst...] are forbidden

Candidate lineage. lineage attaches a thin, JSON-serializable record to every candidate: a content-hash candidate_id over the source sequence and the ordered mutations, the parent_id it descends from, the parent-relative mutations, the method, the objective_values, the effective seed and a digest of the applied constraints. It is opt-in and purely additive, so the table above is unchanged; with lineage=True the candidate_id column is appended and the records land in lineage_. Passing a record instead of True declares the parent this round descends from, and SeqMut.trace_lineage walks such a chain back to its root:

import json

df_variant_lineage = seqm.combine(df_seq=df_seq, variants=variants, df_feat=df_feat,
                                  lineage=True)
aa.display_df(df_variant_lineage[["variant", "n_mut", "candidate_id"]],
              n_rows=10, show_shape=True)
print(json.dumps(seqm.lineage_[0], indent=1))
DataFrame shape: (2, 3)
  variant n_mut candidate_id
1 L37A+V39K+G41L 3 sha256:f3b4eb00...f05e143215077b1
2 L37A+Q38P 2 sha256:8c2e5458...b8d1704f17c2e0d
{
 "candidate_id": "sha256:f3b4eb00bc7d17796b45a7b749021737a21199475581e8e68f05e143215077b1",
 "parent_id": "sha256:6292830e38478ba10f73868d050a2d353cb2011b5e40b9022a80db325eb43f23",
 "source_seq_id": "sha256:6292830e38478ba10f73868d050a2d353cb2011b5e40b9022a80db325eb43f23",
 "mutations": [
  {
   "pos": 37,
   "from_aa": "L",
   "to_aa": "A"
  },
  {
   "pos": 39,
   "from_aa": "V",
   "to_aa": "K"
  },
  {
   "pos": 41,
   "from_aa": "G",
   "to_aa": "L"
  }
 ],
 "method": "SeqMut.combine",
 "objective_values": {
  "delta_cpp": 0.3263199999999999,
  "shift_score": -0.05568000000000012,
  "delta_pred": 2.500000000000001
 },
 "seed": null,
 "constraints_digest": null
}