High-level description
This file contains functions for preprocessing sequencing data into a character matrix, which is suitable for phylogenetic inference. It provides a series of functions that perform various steps of the preprocessing pipeline, including converting FASTQ files to BAM, filtering reads, collapsing UMIs, aligning sequences, calling alleles, error correcting barcodes and UMIs, filtering molecule tables, and calling lineage groups.Code Structure
The code defines several functions that are used sequentially in a preprocessing pipeline. The functions are designed to be modular and can be invoked individually or as part of a larger pipeline. The main functions includeconvert_fastqs_to_unmapped_bam, filter_bam, error_correct_cellbcs_to_whitelist, collapse_umis, resolve_umi_sequence, align_sequences, call_alleles, error_correct_intbcs_to_whitelist, error_correct_umis, filter_molecule_table, and call_lineage_groups. Each function performs a specific step in the preprocessing pipeline and takes a DataFrame or a file path as input and returns a processed DataFrame or a file path.
References
This file references constants defined incassiopeia.preprocess.constants, utility functions from cassiopeia.preprocess.utilities, and alignment utilities from cassiopeia.preprocess.alignment_utilities. It also uses functions from external libraries such as ngs_tools, pysam, and pandas.
Symbols
convert_fastqs_to_unmapped_bam
Description
Converts FASTQ files to an unmapped BAM file based on the specified chemistry. It adds appropriate BAM tags based on the chemistry used.Inputs
Outputs
Internal Logic
The function first checks if the provided chemistry is supported. It then retrieves the appropriate BAM tags for the chemistry and converts the FASTQ files to an unmapped BAM file using thengs.fastq.fastqs_to_bam_with_chemistry function from the ngs_tools library.
filter_bam
Description
Filters reads in a BAM file that have low quality barcode or UMIs.Inputs
Outputs
Internal Logic
The function defines a filter function that checks the quality scores of the barcode and UMI sequences. It then uses thengs.bam.filter_bam function from the ngs_tools library to filter the input BAM file based on the filter function.
error_correct_cellbcs_to_whitelist
Description
Error-corrects cell barcodes in the input BAM file to a provided whitelist.Inputs
Outputs
Internal Logic
The function first reads the whitelist from the provided file or list. It then extracts the raw barcodes and their qualities from the input BAM file. It uses thengs.sequence.correct_sequences_to_whitelist function from the ngs_tools library to correct the barcodes to the whitelist. Finally, it writes the corrected barcodes to a new BAM file.
collapse_umis
Description
Collapses close UMIs together from a BAM file.Inputs
Outputs
Internal Logic
The function first sorts the input BAM file by cell barcode and UMI using thesort_bam function. It then forms collapsed clusters of UMIs using the form_collapsed_clusters function. Finally, it converts the collapsed BAM file to a DataFrame using the utilities.convert_bam_to_df function.
resolve_umi_sequence
Description
Resolves a consensus sequence for each UMI.Inputs
Outputs
Internal Logic
The function first groups the molecule table by cell barcode and UMI. For each group, it selects the most abundant sequence as the consensus sequence and marks the remaining sequences for filtering. It then filters out the marked sequences and applies cell filtering based on the minimum number of UMIs per cell and the minimum average reads per UMI.align_sequences
Description
Aligns reads to the TargetSite reference.Inputs
Outputs
Internal Logic
The function first reads the reference sequence from the provided file or string. It then aligns all unique sequences to the reference using either local or global alignment, based on themethod argument. The alignments are performed using the alignment_utilities.align_local or alignment_utilities.align_global functions. Finally, the alignments are merged into the input DataFrame.
call_alleles
Description
Calls indels from CIGAR strings.Inputs
Outputs
Internal Logic
The function first reads the reference sequence from the provided file or string. It then iterates through the alignments and parses the CIGAR strings using thealignment_utilities.parse_cigar function to extract the indels at each cutsite. The extracted indels are then added to the input DataFrame.
error_correct_intbcs_to_whitelist
Description
Corrects all intBCs to the provided whitelist.Inputs
Outputs
Internal Logic
The function first reads the whitelist from the provided file or list. It then iterates through the unique intBCs in the input DataFrame and calculates the Levenshtein distance to each whitelist intBC. If the distance is less than or equal to the threshold and there is only one matching whitelist intBC, the intBC is corrected.error_correct_umis
Description
Within cellBC-intBC pairs, collapses UMIs that have close sequences.Inputs
Outputs
Internal Logic
The function first groups the input DataFrame by cell barcode, intBC, and optionally allele. For each group, it corrects UMIs that have a Hamming distance less than or equal to the threshold using theUMI_utils.correct_umis_in_group function. The corrected UMIs are then merged back into the input DataFrame.
