Skip to main content

High-level description

This file provides utility functions for the Cassiopeia-Preprocess pipeline, including filtering cells and UMIs, error correcting integration barcodes, converting allele tables to character matrices and lineage profiles, and computing empirical indel priors. These functions are used to process and prepare sequencing data for phylogenetic inference.

Code Structure

The file defines several functions that operate on Pandas DataFrames representing molecule tables and allele tables. These functions perform filtering, error correction, and conversion operations to prepare the data for downstream analysis. Some functions are decorated with log_molecule_table and log_runtime decorators to provide logging and runtime information.

References

This file references symbols from the ngs_tools library for sequence manipulation and analysis, as well as the cassiopeia.mixins module for logging and warnings.

Symbols

filter_cells

Description

Filters out cell barcodes that have too few UMIs or too few reads per UMI, based on user-defined thresholds.

Inputs

Outputs

Internal Logic

  1. Groups the molecule table by cell barcode.
  2. Calculates the number of UMIs and average reads per UMI for each cell.
  3. Filters out cells that do not meet the minimum thresholds for both UMIs per cell and average reads per UMI.

filter_umis

Description

Filters out UMIs with too few reads, based on a user-defined threshold.

Inputs

Outputs

Internal Logic

  1. Filters the molecule table to keep only UMIs with a read count greater than or equal to the minimum threshold.

error_correct_intbc

Description

Error corrects close integration barcodes (intBCs) with small enough unique UMI counts.

Inputs

Outputs

Internal Logic

  1. Groups the molecule table by cellBC, intBC, and allele.
  2. Iterates through pairs of intBCs within each cellBC-allele group.
  3. Corrects an intBC to another if:
    • They have the same allele.
    • The Levenshtein distance between their sequences is less than or equal to dist_thresh.
    • The number of UMIs of the intBC to be changed is less than or equal to umi_count_thresh.
    • The proportion of the UMI count in the intBC to be changed out of the total UMI count in both intBCs is less than prop.

record_stats

Description

Records the number of UMIs per intBC and per cellBC, as well as the read counts for each alignment.

Inputs

Outputs

Internal Logic

  1. Groups the molecule table by cellBC and intBC to count UMIs per intBC.
  2. Groups the molecule table by cellBC to count UMIs per cellBC.
  3. Extracts the read counts from the ‘readCount’ column.

convert_bam_to_df

Description

Converts a BAM file to a Pandas DataFrame.

Inputs

Outputs

Internal Logic

  1. Opens the BAM file using pysam.AlignmentFile.
  2. Iterates through each alignment in the BAM file.
  3. Extracts relevant information from the alignment, including cellBC, UMI, readCount, grpFlag, sequence, quality scores, and read name.
  4. Creates a Pandas DataFrame from the extracted information.

convert_alleletable_to_character_matrix

Description

Converts an AlleleTable into a character matrix, suitable for input into a CassiopeiaSolver object.

Inputs

Outputs

Internal Logic

  1. Filters out samples with intBCs in ignore_intbcs.
  2. Iterates through each cellBC and intBC-cut_site combination.
  3. Assigns a unique integer state to each unique indel observed at a cut site.
  4. Constructs the character matrix by mapping cellBCs to their corresponding character states.
  5. Optionally collapses duplicate character states for a single cellBC-intBC pair.
  6. Optionally calculates prior probabilities for each character state based on the provided mutation_priors.

convert_alleletable_to_lineage_profile

Description

Converts an AlleleTable to a lineage profile, which is a pivot table over the cellBC / intBCs.

Inputs

Outputs

Internal Logic

  1. Groups the allele table by cellBC and intBC.
  2. Creates a multi-index DataFrame with cellBCs as rows and intBC-cut_site combinations as columns.
  3. Fills the DataFrame with the observed indels for each cellBC-intBC-cut_site combination.
  4. Optionally collapses duplicate character states for a single cellBC-intBC pair.
  5. Reorders the columns based on the frequency of intBCs.

convert_lineage_profile_to_character_matrix

Description

Converts a lineage profile to a character matrix, where indels are abstracted into integers.

Inputs

Outputs

Internal Logic

  1. Replaces missing values with “Missing” and optionally replaces a specific missing allele indicator with “Missing”.
  2. Assigns a unique integer state to each unique indel observed at a cut site.
  3. Constructs the character matrix by mapping cellBCs to their corresponding character states.
  4. Optionally calculates prior probabilities for each character state based on the provided indel_priors.

compute_empirical_indel_priors

Description

Computes indel prior probabilities from the input allele table.

Inputs

Outputs

Internal Logic

  1. Groups the allele table by the specified grouping_variables.
  2. Counts the number of times each unique indel occurs in each group.
  3. Calculates the frequency of each indel by dividing its count by the total number of groups.

get_default_cut_site_columns

Description

Retrieves the default cut-sites columns of an AlleleTable, assuming the AlleleTable was created using the Cassiopeia pipeline.

Inputs

Outputs

Internal Logic

  1. Selects columns from the allele table that match the pattern “r” (e.g., “r1”, “r2”).

Dependencies

Error Handling

The