deeplc

DeepLC: Retention time prediction for peptides carrying any modification.

class deeplc.TrainingIndex(path)

Bases: object

Open a packed .dlcidx file or a training index directory.

Parameters:

path (PathLike | str)

contains(keys)

Whether each canonical key occurs anywhere in the training corpus.

Parameters:

keys (list[str])

Return type:

ndarray

contains_in_tasks(keys, task_idx)

Whether each key was observed in at least one of the given setups.

Setup ids the index does not know (a model with more heads than the corpus the index was built from) are ignored: they cannot contribute a membership either way.

Parameters:
Return type:

ndarray

distance_to_training(sequences, max_distance=10)

Levenshtein distance from each stripped sequence to the closest training sequence.

Distances are exact up to max_distance and reported as max_distance + 1 beyond it. The cap is what keeps this fast: exact matches are a set lookup, near matches a length-banded cutoff search, and the expensive unbounded scan over millions of sequences never runs. Beyond ten edits the distance carries no usable signal anyway; on held-out setups the prediction error is flat in this distance.

Parameters:
Return type:

ndarray

deeplc.prediction_report(psm_list, psm_list_reference=None, model=None, calibration=None, coverage=0.9, training_index=None, per_peptide_width=True, predict_kwargs=None)

Predict with calibration and report provenance and uncertainty per PSM.

Parameters:
  • psm_list (PSMList | list[PSM | Peptidoform | str]) – PSMs to predict retention times for.

  • psm_list_reference (PSMList | list[PSM | Peptidoform | str] | None) – Reference for calibration; auto-selected from psm_list when None, as in deeplc.predict_and_calibrate().

  • model (Module | PathLike | str | None) – Trained model or path; the bundled multitask model when None.

  • calibration (Calibration | MultiHeadCalibration | None) – Unfitted calibration to use; SplineTransformerCalibration when None. Pass MultiHeadRidgeCalibration to combine setups, in which case the membership column covers every selected head.

  • coverage (float) – Nominal coverage of the conformal interval (marginal, on peptides exchangeable with the reference). 0.90 by default.

  • training_index (TrainingIndex | PathLike | str | None) – A TrainingIndex or a path to one. Without it, the columns about the training corpus are omitted and the report is limited to the reference.

  • per_peptide_width (bool) – Scale each interval by how far the combined setup heads lie apart for that peptide, so two peptides predicted at the same retention time can get different intervals. Ignored with a calibration that reports no such disagreement, such as SplineTransformerCalibration, where the width depends on the predicted retention time alone.

  • predict_kwargs (dict | None) – Passed to the prediction function ({"device": "cpu"} and the like).

Returns:

One row per input PSM, in order: peptidoform, predicted_rt, ci_lower, ci_upper (conformal at coverage), observed_rt (when present), in_reference, dist_to_reference and, with a training index, in_training, dist_to_training and in_selected_heads_training.

Return type:

pd.DataFrame

deeplc.calibrate(psm_list_reference, model=None, calibration=None, predict_kwargs=None)

Return a MultiHeadCalibration instance fitted to the reference dataset.

Parameters:
  • psm_list_reference (PSMList) – List of PSMs to use as reference for calibration.

  • model (Module | PathLike | str | None) – Trained model or path to model file.

  • calibration (Calibration | MultiHeadCalibration | None) – Calibration instance to use. If None, MultiHeadRidgeCalibration is used. A model with a single LC setup still predicts a one-column matrix, so this default also covers single-task models. See deeplc.calibration.multihead for lighter alternatives (e.g. MultiHeadSplineCalibration) when the ridge over many heads isn’t the right fit, for example for a custom, non-multitask model. An unfitted naive Calibration is accepted too and upgraded to its MultiHead*Calibration counterpart; see deeplc.calibration.upgrade_calibration().

  • predict_kwargs (dict | None) – Additional keyword arguments to pass to the prediction function.

Returns:

Fitted calibration instance.

Return type:

MultiHeadCalibration

deeplc.predict(psm_list, model=None, predict_kwargs=None, return_matrix=False)

Predict retention times for a list of PSMs using a trained model.

Parameters:
  • psm_list (PSMList | list[PSM | Peptidoform | str]) – List of PSMs to predict retention times for.

  • model (Module | PathLike | str | None) – Trained model or path to model file. If None, the default DeepLC model is used.

  • predict_kwargs (dict | None) – Additional keyword arguments to pass to the prediction function. Pass {"factored": False} to force a dense ndarray from return_matrix=True. The default hands back a FactoredPredictionMatrix, which holds the head’s low-rank factors and indexes identically at a fraction of the memory.

  • return_matrix (bool) – If True, return the full prediction matrix of shape (n, n_heads) when using a multitask model. If False (default), return a 1D array of shape (n,) for the setup named by DEFAULT_TASK_NAME when the model knows its setups, and head 0 otherwise. Uncalibrated output is on that setup’s gradient; use predict_and_calibrate() to map it onto your own.

Returns:

Retention time predictions. Shape (n,) unless return_matrix=True and model produces multitask output, in which case shape is (n, n_heads).

Return type:

np.ndarray

deeplc.predict_and_calibrate(psm_list, psm_list_reference=None, model=None, calibration=None, predict_kwargs=None)

Predict retention times and calibrate to a reference.

Parameters:
  • psm_list (PSMList | list[PSM | Peptidoform | str]) – List of PSMs to predict retention times for.

  • psm_list_reference (PSMList | list[PSM | Peptidoform | str] | None) – List of PSMs to use as reference for calibration. If None, the best PSMs are automatically selected from psm_list (auto-calibration). This requires that the input PSM list contains observed retention times, score and decoy status to select the best PSMs for auto-calibration.

  • model (Module | PathLike | str | None) – Trained model or path to model file.

  • calibration (Calibration | MultiHeadCalibration | None) – Calibration instance to use. If None, MultiHeadRidgeCalibration is used. See deeplc.calibration.multihead for lighter alternatives. An unfitted naive Calibration is accepted too and upgraded to its MultiHead*Calibration counterpart; see deeplc.calibration.upgrade_calibration(). A fitted one is not, since it carries no record of which head it was fit on: fit a MultiHead*Calibration instead to pass in an already fitted calibration.

  • predict_kwargs (dict | None) – Additional keyword arguments to pass to the prediction function.

Returns:

Calibrated retention time predictions.

Return type:

np.ndarray

deeplc.finetune_and_predict(psm_list, psm_list_reference=None, model=None, train_kwargs=None, predict_kwargs=None)

Fine-tune the model to a reference and predict new retention times.

Parameters:
  • psm_list (PSMList | list[PSM | Peptidoform | str]) – List of PSMs to predict retention times for.

  • psm_list_reference (PSMList | list[PSM | Peptidoform | str] | None) – List of PSMs to use as reference for calibration. If None, the best PSMs are automatically selected from psm_list (auto-calibration). This requires that the input PSM list contains observed retention times, score and decoy status to select the best PSMs for auto-calibration.

  • model (Module | PathLike | str | None) – Trained model or path to model file.

  • train_kwargs (dict | None) – Additional keyword arguments to pass to the training function.

  • predict_kwargs (dict | None) – Additional keyword arguments to pass to the prediction function.

Returns:

Calibrated retention time predictions after fine-tuning.

Return type:

np.ndarray

deeplc.finetune(psm_list_reference, psm_list_validation=None, validation_split=0.1, model=None, train_kwargs=None)

Fine-tune an existing model.

Parameters:
  • psm_list_reference (PSMList) – List of PSMs to use as reference for fine-tuning.

  • psm_list_validation (PSMList | None) – List of PSMs to use for validation during fine-tuning. If None, a split from psm_list is used.

  • validation_split (float) – Fraction of psm_list_reference to use for validation when psm_list_validation is None. Raised to at least 0.25 when the reference set is smaller than MIN_FINETUNE_REFERENCE, because early stopping cannot work on a handful of PSMs and an unchecked fit can end up far worse than calibration.

  • model (Module | PathLike | str | None) – Trained model or path to model file.

  • train_kwargs (dict | None) – Additional keyword arguments to pass to the training function.

Returns:

Fine-tuned model.

Return type:

torch.nn.Module

deeplc.save_model(model, path)

Save a model’s state dict to a file.

Use load_model() (via predict()) to reload the saved checkpoint.

Parameters:
  • model (Module) – Trained model instance to save.

  • path (PathLike | str) – Destination file path.

  • architecture (Models that can describe themselves are saved with their)

Return type:

None

:param : :param constructor arguments and feature specification alongside the weights: :param so: :param that load_model() can rebuild them. Saving a bare state dict for such a: :param model produced a file that could not be reloaded: :param because the loader would: :param fall back to inferring the architecture from tensor names.:

deeplc.train(psm_list_reference, psm_list_validation=None, validation_split=0.1, train_kwargs=None)

Train a new model from scratch.

Parameters:
  • psm_list_reference (PSMList) – List of PSMs to use as reference for fine-tuning.

  • psm_list_validation (PSMList | None) – List of PSMs to use for validation. If None, a split from psm_list is used.

  • validation_split (float) – If psm_list_validation is None, this fraction of psm_list will be used for validation.

  • train_kwargs (dict | None) – Additional keyword arguments to pass to the training function.

Returns:

Trained model.

Return type:

torch.nn.Module