MACS3.IO.BAM module

Utilities for reading BAM files and their BAI indexes in MACS3.

This code is free software; you can redistribute it and/or modify it under the terms of the BSD License (see the file LICENSE included with the distribution).

class MACS3.IO.BAM.BAIFile(filename)

Bases: object

In-memory representation of a BAM index (BAI) file.

bins: list
fhd: object
filename: str
get_chunks_by_bin(ref_n, bin_n)

Return sorted BGZF chunks for bin_n on reference ref_n.

Return type:

list

get_chunks_by_list_of_bins(ref_n, bins)

Return sorted chunks for the unique set of bins provided.

Return type:

list

get_chunks_by_list_of_regions(ref_n, regions)

Return BGZF chunks overlapping any region in regions.

Return type:

list

get_chunks_by_region(ref_n, beg, end)

Return BGZF chunks overlapping [beg, end) on reference ref_n.

Return type:

list

get_coffset_by_region(ref_n, beg, end)

Return the BGZF compressed offset for the leftmost overlapping block.

Return type:

typedef

get_coffsets_by_list_of_regions(ref_n, regions)

Return compressed offsets for the leftmost block of each region.

Return type:

typedef

get_metadata_by_refseq(ref_n)

Return pseudo-bin metadata for reference ref_n.

Return type:

dict

magic: bytes
metadata: dict
n_bins: typedef
n_chunks: typedef
n_mapped: typedef
n_ref: typedef
n_unmapped: typedef
class MACS3.IO.BAM.BAMaccessor(BAM_filename)

Bases: object

Random-access BAM reader backed by a matching BAI index.

The accessor reads headers via gzip for compatibility, but seeks directly to BGZF blocks when fetching alignments for specific regions.

bai_filename: str
baifile: BAIFile
bam_filename: str
bamfile: object
bgzf_block_cache: bytes
close()

Close the underlying BAM stream.

coffset_cache: typedef
get_chromosomes()

Return reference names in header order.

Return type:

list

get_reads_in_region(chrom, left, right, maxDuplicate=1)

Return alignments overlapping [left, right) on chrom.

Parameters:
  • chrom (bytes) – Chromosome name matching the BAM header.

  • left (typedef) – 0-based inclusive start coordinate.

  • right (typedef) – 0-based exclusive end coordinate.

  • maxDuplicate (typedef) – Maximum number of identical alignments to retain.

Returns:

MACS3.Signal.ReadAlignment.ReadAlignment objects.

Return type:

list

get_rlengths()

Return reference lengths keyed by reference name.

Return type:

dict

noffset_cache: typedef
references: list
rlengths: dict
exception MACS3.IO.BAM.MDTagMissingError(name, aux)

Bases: Exception

Raised when an alignment entry is missing the MD auxiliary tag.

exception MACS3.IO.BAM.StrandFormatError(string, strand)

Bases: Exception

Raised when a strand annotation cannot be interpreted.

MACS3.IO.BAM.bool(*args, **kwargs)
MACS3.IO.BAM.get_bins_by_region(beg, end)

Return BAI bin IDs that overlap [beg, end).

Parameters:
  • beg (typedef) – 0-based inclusive start coordinate.

  • end (typedef) – 0-based exclusive end coordinate.

Returns:

All bin indices covering the requested interval.

Return type:

list

MACS3.IO.BAM.reg2bins(rbeg, rend)

Return BAI bin IDs intersecting [rbeg, rend).

Parameters:
  • rbeg (int) – 0-based inclusive start coordinate.

  • rend (int) – 0-based exclusive end coordinate.

Returns:

Bin identifiers overlapping the interval.

Return type:

list

Raises:

AssertionError – If the interval falls outside the supported range.