Skip to content

Isobar

Provides a transformer class for processing isobarically labeled proteomics data.

This module defines the IsotopeImpurityCorrecter class for processing of isobaric (e.g., TMT, iTRAQ) reporter intensities. This transformer must be fitted with an isotope impurity matrix to correct interference in reporter intensities. Once fitted, the transformer can then be applied to a table containing reporter ion intensities to adjust its intensity values. The transformation returns a new copy of the table with the processed values, leaving the original table unchanged.

Classes:

Name Description
IsotopeImpurityCorrecter

Corrects isotope impurity interference in isobaric reporter expression values.

Functions:

Name Description
correct_isobaric_reporter_impurities

Performs isotope impurity correction on isobaric reporter expression values.

IsotopeImpurityCorrecter

IsotopeImpurityCorrecter()

Corrects isotope impurity interference in isobaric reporter expression values.

Methods:

Name Description
fit

Fits the isotope impurity correcter to a given impurity matrix.

is_fitted

Returns True if the IsotopeImpurityCorrecter has been fitted.

get_fits

Returns a copy of the fitted impurity matrix.

transform

Applies isotope impurity correction to the values of the table.

Source code in msreport\isobar.py
25
26
def __init__(self):
    self._impurity_matrix = None

fit

fit(impurity_matrix: ndarray) -> Self

Fits the isotope impurity correcter to a given impurity matrix.

Parameters:

Name Type Description Default
impurity_matrix ndarray

A reporter isotope impurity matrix in a diagonal format, where columns describe the isotope impurity of a specific channel, and the values in each row indicate the percentage of signal from the reporter that is present in each channel. Both dimensions of the impurity matrix must have the same length.

required

Returns:

Type Description
Self

Returns the fitted class IsotopeImpurityCorrecter instance.

Source code in msreport\isobar.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def fit(self, impurity_matrix: np.ndarray) -> Self:
    """Fits the isotope impurity correcter to a given impurity matrix.

    Args:
        impurity_matrix: A reporter isotope impurity matrix in a diagonal format,
            where columns describe the isotope impurity of a specific channel, and
            the values in each row indicate the percentage of signal from the
            reporter that is present in each channel. Both dimensions of the
            impurity matrix must have the same length.

    Returns:
        Returns the fitted class IsotopeImpurityCorrecter instance.
    """
    if impurity_matrix.shape[0] != impurity_matrix.shape[1]:
        raise ValueError("The impurity matrix must be square.")
    if np.isnan(impurity_matrix).any():
        raise ValueError("The impurity matrix contains NaN values.")
    self._impurity_matrix = impurity_matrix
    return self

is_fitted

is_fitted() -> bool

Returns True if the IsotopeImpurityCorrecter has been fitted.

Source code in msreport\isobar.py
48
49
50
def is_fitted(self) -> bool:
    """Returns True if the IsotopeImpurityCorrecter has been fitted."""
    return self._impurity_matrix is not None

get_fits

get_fits() -> ndarray

Returns a copy of the fitted impurity matrix.

Returns:

Type Description
ndarray

A numpy array representing a diagonal impurity matrix.

Source code in msreport\isobar.py
52
53
54
55
56
57
58
59
60
def get_fits(self) -> np.ndarray:
    """Returns a copy of the fitted impurity matrix.

    returns:
        A numpy array representing a diagonal impurity matrix.
    """
    if not self.is_fitted():
        raise NotFittedError("The IsotopeImpurityCorrecter has not been fitted.")
    return self._impurity_matrix.copy()

transform

transform(table: DataFrame) -> DataFrame

Applies isotope impurity correction to the values of the table.

Parameters:

Name Type Description Default
table DataFrame

The data to normalize. The columns of the table must correspond to the channels of the impurity matrix used for fitting.

required

Returns:

Type Description
DataFrame

A copy of the table with isotope impurity corrected values.

Source code in msreport\isobar.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def transform(self, table: pd.DataFrame) -> pd.DataFrame:
    """Applies isotope impurity correction to the values of the table.

    Args:
        table: The data to normalize. The columns of the table must correspond to
            the channels of the impurity matrix used for fitting.

    Returns:
        A copy of the table with isotope impurity corrected values.
    """
    if not self.is_fitted():
        raise NotFittedError("The IsotopeImpurityCorrecter has not been fitted.")
    if table.shape[1] != self.get_fits().shape[1]:
        raise ValueError(
            "The number of columns in the table does not match the number "
            "of channels in the impurity matrix."
        )

    corrected_values = correct_isobaric_reporter_impurities(
        intensity_table=table.to_numpy(),
        diagonal_impurity_matrix=self._impurity_matrix,
    )
    corrected_table = table.copy()
    corrected_table[:] = corrected_values
    return corrected_table

correct_isobaric_reporter_impurities

correct_isobaric_reporter_impurities(
    intensity_table: ndarray,
    diagonal_impurity_matrix: ndarray,
) -> ndarray

Performs isotope impurity correction on isobaric reporter expression values.

Parameters:

Name Type Description Default
intensity_table ndarray

A two-dimenstional array with columns corresponding to isobaric reporter channels and rows to measured units such as PSMs, peptides or proteins.

required
diagonal_impurity_matrix ndarray

A reporter isotope impurity matrix in a diagonal format, where columns describe the isotope impurity of a specific channel, and the values in each row indicate the percentage of signal from the reporter that is present in each channel.

required
Source code in msreport\isobar.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def correct_isobaric_reporter_impurities(
    intensity_table: np.ndarray,
    diagonal_impurity_matrix: np.ndarray,
) -> np.ndarray:
    """Performs isotope impurity correction on isobaric reporter expression values.

    Args:
        intensity_table: A two-dimenstional array with columns corresponding to isobaric
            reporter channels and rows to measured units such as PSMs, peptides or
            proteins.
        diagonal_impurity_matrix: A reporter isotope impurity matrix in a diagonal
            format, where columns describe the isotope impurity of a specific channel,
            and the values in each row indicate the percentage of signal from the
            reporter that is present in each channel.
    """
    apply_impurity_correction = functools.partial(
        _correct_impurity_contamination,
        impurity_matrix=diagonal_impurity_matrix,
    )

    data_was_in_logpsace = msreport.helper.intensities_in_logspace(intensity_table)

    if data_was_in_logpsace:
        intensity_table = np.power(2, intensity_table)
    intensity_table[np.isnan(intensity_table)] = 0
    corrected_table = np.apply_along_axis(apply_impurity_correction, 1, intensity_table)
    corrected_table[corrected_table <= 0] = 0
    if data_was_in_logpsace:
        corrected_table = np.log2(corrected_table)

    return corrected_table