Skip to main content

High-level description

This file provides functions for collapsing and preprocessing Unique Molecular Identifiers (UMIs) in sequencing data. It handles tasks like grouping reads by cell barcodes and UMIs, forming consensus sequences for each UMI cluster, and error-correcting UMI sequences.

Code Structure

The code defines several functions that work together to perform UMI collapsing and error correction. The main functions are sort_bam, form_collapsed_clusters, form_clusters, form_clusters_likelihood, and correct_umis_in_group. sort_bam sorts the input BAM file by cell barcode and UMI. form_collapsed_clusters groups reads with the same cell barcode and UMI, and then calls either form_clusters or form_clusters_likelihood to form consensus sequences for each UMI cluster. correct_umis_in_group further groups reads by cell barcode, UMI, and optionally allele, and then performs error correction on the UMI sequences within each group.

References

This file references the following symbols:
  • cassiopeia.preprocess.constants.BAM_CONSTANTS
  • cassiopeia.preprocess.utilities.convert_bam_to_df
  • cassiopeia.mixins.logger
  • cassiopeia.mixins.PreprocessError
  • cassiopeia.mixins.PreprocessWarning

Symbols

detect_cell_bc_tag

Description

This function determines the appropriate BAM tag for cell barcodes by checking if the corrected cell barcode tag exists in the BAM file. If it does, it returns the corrected tag; otherwise, it returns the raw cell barcode tag.

Inputs

Outputs

group_bam_by_key

Description

This generator function iterates through a sorted BAM file and yields groups of alignments that share the same key, defined by the sort_key function.

Inputs

Outputs

sort_bam

Description

This function sorts a BAM file by a specified key and filters alignments based on a filter function. It splits the BAM file into chunks, sorts and filters each chunk, and then merges the sorted chunks into a final sorted BAM file.

Inputs

Outputs

form_collapsed_clusters

Description

This function groups reads with the same cell barcode and UMI, and then forms consensus sequences for each UMI cluster. It uses either a hard cutoff method or a likelihood-based method to form the initial sequence clusters, and then attempts to merge clusters with similar sequences.

Inputs

Outputs

form_clusters

Description

This function implements the hard cutoff method for forming consensus sequences for UMI clusters. It iteratively finds the most frequent sequence in the remaining alignments and groups all sequences within a certain Hamming distance from it.

Inputs

Outputs

form_clusters_likelihood

Description

This function implements the likelihood-based method for forming consensus sequences for UMI clusters. It uses the quality scores of the bases to calculate the probability of each base being correct and forms consensus sequences based on these probabilities.

Inputs

Outputs

align_clusters

Description

This function calculates the number of indels and high-quality mismatches between two aligned segments using the Smith-Waterman algorithm.

Inputs

Outputs

within_radius_of_seed

Description

This function takes a seed sequence and a list of alignments and returns two lists: one containing alignments whose sequences are within a certain Hamming distance from the seed sequence, and another containing the remaining alignments.

Inputs

Outputs

propose_seed

Description

This function proposes a seed sequence for clustering by finding the most frequent sequence in a list of alignments. If there is no most frequent sequence, it calls call_consensus to generate a consensus sequence.

Inputs

Outputs

make_singleton_cluster

Description

This function creates a singleton cluster from a single alignment.

Inputs

Outputs

call_consensus

Description

This function generates a consensus sequence from a list of alignments by taking the most frequent high-quality base at each position.

Inputs

Outputs

merge_annotated_clusters

Description

This function merges two annotated clusters by adding the read count of the smaller cluster to the larger cluster and updating the cluster ID.

Inputs

Outputs

correct_umis_in_group

Description

This function performs error correction on UMI sequences within a group of alignments that share the same cell barcode and integration barcode. It uses a Hamming distance threshold to determine which UMIs should be merged.

Inputs

Outputs

Internal Logic

The function first calculates the Hamming distance between all pairs of UMIs in the group. It then identifies UMIs that are within the specified distance threshold and merges them into the more abundant UMI.

Side Effects

This function modifies the input DataFrame in place.

Performance Considerations

The function uses a Cython implementation of the Hamming distance calculation to improve performance.