> ## Documentation Index
> Fetch the complete documentation index at: https://demo.agenticlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# collapse_umi_test.py

Here's a detailed documentation of the target file `test/preprocess_tests/collapse_umi_test.py`:

## High-level description

This file contains unit tests for the UMI Collapsing module in the Cassiopeia preprocessing pipeline. It tests various aspects of UMI collapsing, including sorting BAM files, forming collapsed clusters, and converting BAM files to DataFrames.

## Code Structure

The main class `TestCollapseUMIs` inherits from `unittest.TestCase` and contains several test methods. The `setUp` method prepares the test environment by creating test files and running UMI collapsing operations. The test methods then verify the correctness of these operations.

## Symbols

### TestCollapseUMIs

#### Description

A test class that contains unit tests for the UMI Collapsing functionality.

#### Internal Logic

1. Sets up test files and performs UMI collapsing operations in `setUp`.
2. Tests various aspects of UMI collapsing in individual test methods.

### setUp

#### Description

Prepares the test environment by creating test files and running UMI collapsing operations.

#### Internal Logic

1. Creates test directories and files.
2. Sorts BAM files.
3. Performs UMI collapsing using different methods and parameters.

### test\_sort\_bam

#### Description

Tests the sorting of BAM files.

#### Internal Logic

1. Opens the sorted BAM file.
2. Extracts cell barcodes and UMIs.
3. Verifies the correctness of specific entries.

### test\_sort\_bam\_uncorrected

#### Description

Tests the sorting of uncorrected BAM files.

#### Internal Logic

Similar to `test_sort_bam`, but uses uncorrected test files.

### test\_collapse\_bam

#### Description

Tests the collapsing of BAM files.

#### Internal Logic

1. Opens the collapsed BAM file.
2. Extracts various fields (cell barcodes, UMIs, read counts, cluster IDs, qualities).
3. Verifies the correctness of specific entries and counts.

### test\_collapse\_bam\_bayesian

#### Description

Tests the Bayesian collapsing of BAM files.

#### Internal Logic

Similar to `test_collapse_bam`, but uses Bayesian collapsed files.

### test\_collapse\_bam\_uncorrected

#### Description

Tests the collapsing of uncorrected BAM files.

#### Internal Logic

Similar to `test_collapse_bam`, but uses uncorrected collapsed files.

### test\_bam2DF

#### Description

Tests the conversion of BAM files to DataFrames.

#### Internal Logic

1. Converts BAM to DataFrame.
2. Verifies specific entries in the resulting DataFrame.
3. Compares with a separately loaded DataFrame from a text file.

### test\_collapsing\_passes\_header

#### Description

Tests that the collapsing process correctly passes header information.

#### Internal Logic

1. Opens the collapsed BAM file.
2. Verifies that the alignment has non-None query sequence and qualities.

## Dependencies

| Dependency | Purpose |
| :- | :- |
| os | File and directory operations |
| unittest | Unit testing framework |
| pandas | Data manipulation and analysis |
| pathlib | Object-oriented filesystem paths |
| pysam | Reading/writing SAM/BAM/VCF/BCF files |
| cassiopeia.preprocess | UMI collapsing functionality |

## Error Handling

The tests use assertions to verify the correctness of operations. If any assertion fails, an `AssertionError` will be raised, indicating a test failure.

This file is crucial for ensuring the correctness of the UMI collapsing functionality in the Cassiopeia preprocessing pipeline. It covers various scenarios and edge cases, helping to maintain the reliability of the UMI collapsing process.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.