deeplc
DeepLC: Retention time prediction for peptides carrying any modification.
- class deeplc.TrainingIndex(path)
Bases:
objectOpen a packed
.dlcidxfile or a training index directory.- Parameters:
path (PathLike | str)
- contains(keys)
Whether each canonical key occurs anywhere in the training corpus.
- 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.
- distance_to_training(sequences, max_distance=10)
Levenshtein distance from each stripped sequence to the closest training sequence.
Distances are exact up to
max_distanceand reported asmax_distance + 1beyond 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.
- 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_listwhen None, as indeeplc.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;
SplineTransformerCalibrationwhen None. PassMultiHeadRidgeCalibrationto 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
TrainingIndexor 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 atcoverage),observed_rt(when present),in_reference,dist_to_referenceand, with a training index,in_training,dist_to_trainingandin_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.multiheadfor 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 naiveCalibrationis accepted too and upgraded to itsMultiHead*Calibrationcounterpart; seedeeplc.calibration.upgrade_calibration().predict_kwargs (dict | None) – Additional keyword arguments to pass to the prediction function.
- Returns:
Fitted calibration instance.
- Return type:
- 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 densendarrayfromreturn_matrix=True. The default hands back aFactoredPredictionMatrix, 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 byDEFAULT_TASK_NAMEwhen the model knows its setups, and head 0 otherwise. Uncalibrated output is on that setup’s gradient; usepredict_and_calibrate()to map it onto your own.
- Returns:
Retention time predictions. Shape
(n,)unlessreturn_matrix=Trueand 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.multiheadfor lighter alternatives. An unfitted naiveCalibrationis accepted too and upgraded to itsMultiHead*Calibrationcounterpart; seedeeplc.calibration.upgrade_calibration(). A fitted one is not, since it carries no record of which head it was fit on: fit aMultiHead*Calibrationinstead 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_referenceto use for validation whenpsm_list_validationis None. Raised to at least 0.25 when the reference set is smaller thanMIN_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:
- deeplc.save_model(model, path)
Save a model’s state dict to a file.
Use
load_model()(viapredict()) to reload the saved checkpoint.- Parameters:
- 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: