alphapepttools.io.AnnDataFactory#

class alphapepttools.io.AnnDataFactory(psm_df)#

Factory class to convert AlphaBase PSM DataFrames to AnnData format.

Methods table#

create_anndata([level, intensity_column, ...])

Create AnnData object from PSM DataFrame.

from_files(file_paths, reader_type, *[, ...])

Create AnnDataFactory from PSM files.

Methods#

AnnDataFactory.create_anndata(level='proteins', *, intensity_column=None, sample_id_column=None, feature_id_column=None, var_columns=None, obs_columns=None)#

Create AnnData object from PSM DataFrame.

Parameters:
  • level (Literal['proteins', 'genes', 'peptides', 'precursors'] (default: 'proteins')) – Level of quantification to read.

  • intensity_column (str | None (default: None)) – Name of the standardized column storing intensity data. Default is taken from alphapepttools.io.reader_columns.FEATURE_LEVEL_CONFIG from the respective level.

  • sample_id_column (str | None (default: None)) – Name of the standardized column storing sample ids. Default is taken from alphapepttools.io.reader_columns.FEATURE_LEVEL_CONFIG.

  • feature_id_column (str | None (default: None)) – Name of the standardized column storing feature ids. Default is taken from alphapepttools.io.reader_columns.FEATURE_LEVEL_CONFIG from the respective level.

  • var_columns (str | list[str] | None (default: None)) – Additional standardized columns to include in var of the AnnData object, by default None.

  • obs_columns (str | list[str] | None (default: None)) – Additional standardized columns to include in obs of the AnnData object, by default None.

Return type:

AnnData

Returns:

AnnData object where: - obs (rows) are samples - var (columns) are features (e.g., proteins, peptides, or genes) - X contains intensity values

Examples

import pandas as pd
from alphapepttools.io.anndata_factory import AnnDataFactory

# Create sample data with metadata
df = pd.DataFrame(
    {
        "raw_name": ["sample1"] * 3 + ["sample2"] * 3,
        "protein_group": ["PROT1", "PROT2", "PROT3"] * 2,
        "intensity": [100, 200, 150, 120, 210, 160],
        "gene_names": ["GENE1", "GENE2", "GENE3"] * 2,
        "condition": ["control"] * 3 + ["treated"] * 3,
    }
)

factory = AnnDataFactory(psm_df=df)

# Create AnnData with metadata
adata = factory.create_anndata(
    level="precursors",
    var_columns=["gene_names"],  # Add gene names to var
    obs_columns=["condition"],  # Add condition to obs
)

print(adata.shape)  # (2, 3) - 2 samples, 3 proteins
print(adata.var["gene_names"])  # Gene annotations
print(adata.obs["condition"])  # Sample conditions
classmethod AnnDataFactory.from_files(file_paths, reader_type, *, additional_columns=None, **reader_kwargs)#

Create AnnDataFactory from PSM files.

Parameters:
  • file_paths (str | list[str]) – Path(s) to PSM file(s)

  • reader_type (str) – Type of PSM reader to use.

  • additional_columns (list[str] | None (default: None)) – Names of additional columns from the PSM report to retain for experiment-specific metadata. These columns might be added to the created AnnData object as additional annotations.

  • **reader_kwargs – Additional arguments passed to PSM reader

Return type:

AnnDataFactory

Returns:

Initialized AnnDataFactory instance

Examples

from alphapepttools.io.anndata_factory import AnnDataFactory

# Load DIA-NN data at protein level
# assuming a diann report called "report.tsv" exists in the current directory

factory = AnnDataFactory.from_files("report.tsv", reader_type="diann")
adata = factory.create_anndata(level="proteins")

# Load with custom column names and additional metadata columns
factory = AnnDataFactory.from_files(
    report_path,
    reader_type="diann",
    additional_columns=[
        "Precursor.Quantity"
    ],  # columns that are not standardized by alphabase can be specified here.
)
adata = factory.create_anndata(
    # We use the non-default intensity column "Precursor.Quantity" from the PSM report
    intensity_column="Precursor.Quantity",
    # Add m/z and stripped sequence via their alphabase-standardized column names in var
    var_columns=["charge", "sequence"],
)
display(adata.var)  # Check that additional columns are included in var