pytest_wdl/data_types/bam.py
def assert_bam_files_equal(
    file1: Path,
    file2: Path,
    allowed_diff_lines: int = 0,
    min_mapq: int = 0,
    compare_tag_columns: bool = False
):
    """
    Compare two BAM files:
    * Convert them to SAM format
    * Optionally re-sort the files by chromosome, position, and flag
    * First compare all lines using only a subset of columns that should be
    deterministic
    * Next, filter the files by MAPQ and compare the remaining rows using all columns

    Args:
        file1: First BAM to compare
        file2: Second BAM to compare
        allowed_diff_lines: Number of lines by which the BAMs are allowed to differ
            (after being convert to SAM)
        min_mapq: Minimum mapq used to filter reads when comparing all columns
        compare_tag_columns: Whether to include tag columns (12+) when comparing
            all columns
    """
    with tempdir() as temp:
        # Compare all reads using subset of columns
        cmp_file1 = temp / "all_reads_subset_columns_file1"
        cmp_file2 = temp / "all_reads_subset_columns_file2"
        bam_to_sam(
            file1,
            cmp_file1,
            headers=None,
            sorting=Sorting.NAME
        )
        bam_to_sam(
            file2,
            cmp_file2,
            headers=None,
            sorting=Sorting.NAME
        )
        assert_text_files_equal(
            cmp_file1,
            cmp_file2,
            allowed_diff_lines,
            diff_fn=partial(diff_bam_columns, columns=INVARIATE_COLUMNS)
        )

        # Compare subset of reads using all columns
        cmp_file1 = temp / "subset_reads_all_columns_file1"
        cmp_file2 = temp / "subset_reads_all_columns_file2"
        bam_to_sam(
            file1,
            cmp_file1,
            min_mapq=min_mapq,
            sorting=Sorting.COORDINATE,
        )
        bam_to_sam(
            file2,
            cmp_file2,
            min_mapq=min_mapq,
            sorting=Sorting.COORDINATE
        )
        if compare_tag_columns:
            diff_fn = diff_default
        else:
            diff_fn = partial(diff_bam_columns, columns=ALL_COLUMNS)
        assert_text_files_equal(
            cmp_file1,
            cmp_file2,
            allowed_diff_lines,
            diff_fn=diff_fn
        )

    Compare two BAM files:
    * Convert them to SAM format
    * Optionally re-sort the files by chromosome, position, and flag
    * First compare all lines using only a subset of columns that should be
    deterministic
    * Next, filter the files by MAPQ and compare the remaining rows using all columns

    Args:
        file1: First BAM to compare
        file2: Second BAM to compare
        allowed_diff_lines: Number of lines by which the BAMs are allowed to differ
            (after being convert to SAM)
        min_mapq: Minimum mapq used to filter reads when comparing all columns
        compare_tag_columns: Whether to include tag columns (12+) when comparing
            all columns
    
Compare two BAM files:
Convert them to SAM format
Optionally re-sort the files by chromosome, position, and flag
First compare all lines using only a subset of columns that should be
deterministic
Next, filter the files by MAPQ and compare the remaining rows using all columns
Compare two BAM files : Convert them to SAM format Optionally re - sort the files by chromosome position and flag First compare all lines using only a subset of columns that should be deterministic Next filter the files by MAPQ and compare the remaining rows using all columns
def assert_bam_files_equal(
    file1: Path,
    file2: Path,
    allowed_diff_lines: int = 0,
    min_mapq: int = 0,
    compare_tag_columns: bool = False
):
    with tempdir() as temp:
        cmp_file1 = temp / "all_reads_subset_columns_file1"
        cmp_file2 = temp / "all_reads_subset_columns_file2"
        bam_to_sam(
            file1,
            cmp_file1,
            headers=None,
            sorting=Sorting.NAME
        )
        bam_to_sam(
            file2,
            cmp_file2,
            headers=None,
            sorting=Sorting.NAME
        )
        assert_text_files_equal(
            cmp_file1,
            cmp_file2,
            allowed_diff_lines,
            diff_fn=partial(diff_bam_columns, columns=INVARIATE_COLUMNS)
        )
        cmp_file1 = temp / "subset_reads_all_columns_file1"
        cmp_file2 = temp / "subset_reads_all_columns_file2"
        bam_to_sam(
            file1,
            cmp_file1,
            min_mapq=min_mapq,
            sorting=Sorting.COORDINATE,
        )
        bam_to_sam(
            file2,
            cmp_file2,
            min_mapq=min_mapq,
            sorting=Sorting.COORDINATE
        )
        if compare_tag_columns:
            diff_fn = diff_default
        else:
            diff_fn = partial(diff_bam_columns, columns=ALL_COLUMNS)
        assert_text_files_equal(
            cmp_file1,
            cmp_file2,
            allowed_diff_lines,
            diff_fn=diff_fn
        )
def assert_bam_files_equal ( file1 : Path , file2 : Path , allowed_diff_lines : int = 0 , min_mapq : int = 0 , compare_tag_columns : bool = False ) : with tempdir ( ) as temp : cmp_file1 = temp / "all_reads_subset_columns_file1" cmp_file2 = temp / "all_reads_subset_columns_file2" bam_to_sam ( file1 , cmp_file1 , headers = None , sorting = Sorting . NAME ) bam_to_sam ( file2 , cmp_file2 , headers = None , sorting = Sorting . NAME ) assert_text_files_equal ( cmp_file1 , cmp_file2 , allowed_diff_lines , diff_fn = partial ( diff_bam_columns , columns = INVARIATE_COLUMNS ) ) cmp_file1 = temp / "subset_reads_all_columns_file1" cmp_file2 = temp / "subset_reads_all_columns_file2" bam_to_sam ( file1 , cmp_file1 , min_mapq = min_mapq , sorting = Sorting . COORDINATE , ) bam_to_sam ( file2 , cmp_file2 , min_mapq = min_mapq , sorting = Sorting . COORDINATE ) if compare_tag_columns : diff_fn = diff_default else : diff_fn = partial ( diff_bam_columns , columns = ALL_COLUMNS ) assert_text_files_equal ( cmp_file1 , cmp_file2 , allowed_diff_lines , diff_fn = diff_fn )
Compare two BAM files:
 Convert them to SAM format
 Optionally re-sort the files by chromosome, position, and flag
 First compare all lines using only a subset of columns that should be
deterministic
 Next, filter the files by MAPQ and compare the remaining rows using all columns
Compare two BAM files : Convert them to SAM format Optionally re - sort the files by chromosome position and flag First compare all lines using only a subset of columns that should be deterministic Next filter the files by MAPQ and compare the remaining rows using all columns
"""
    Compare two BAM files:
    * Convert them to SAM format
    * Optionally re-sort the files by chromosome, position, and flag
    * First compare all lines using only a subset of columns that should be
    deterministic
    * Next, filter the files by MAPQ and compare the remaining rows using all columns

    Args:
        file1: First BAM to compare
        file2: Second BAM to compare
        allowed_diff_lines: Number of lines by which the BAMs are allowed to differ
            (after being convert to SAM)
        min_mapq: Minimum mapq used to filter reads when comparing all columns
        compare_tag_columns: Whether to include tag columns (12+) when comparing
            all columns
    """ # Compare all reads using subset of columns # Compare subset of reads using all columns
file1
Path
First BAM to compare
First BAM to compare
file2
Path
Second BAM to compare
Second BAM to compare
allowed_diff_lines
int
Number of lines by which the BAMs are allowed to differ
(after being convert to SAM)
Number of lines by which the BAMs are allowed to differ ( after being convert to SAM )
min_mapq
int
Minimum mapq used to filter reads when comparing all columns
Minimum mapq used to filter reads when comparing all columns
compare_tag_columns
bool
Whether to include tag columns (12+) when comparing
all columns
Whether to include tag columns ( 12 + ) when comparing all columns