Skip to content

Barcode Count

Self-configuring barcode counter. Discovers read structure from a sample of reads, then counts exact barcode matches across the full dataset. Supports single-end and paired-end reads.

barcode_count

Forwarding stub — implementation moved to _draft/.

BarcodeCount dataclass

BarcodeCount(library_path: str, discover_fn: Callable[..., ReadStructure] | None = None)

Count barcodes in sequencing reads.

Two-phase approach from heuristicount:

  1. Discover: sample reads to learn structure (offset, orientation, flanks)
  2. Count: exact-match counting using learned structure

The discover function is injected via __init__ for testability. Defaults to heuristic_discover.

Parameters:

Name Type Description Default
library_path str

Path to barcode FASTA file.

required
discover_fn Callable[..., ReadStructure] | None

Discovery callable. Must accept (reads1, library, reads2=...) and return a ReadStructure. Defaults to heuristic_discover.

None

Examples:

>>> recipe = BarcodeCount(library_path="barcodes.fasta")
>>> track = recipe.run("reads_R1.fastq")

run

run(reads1: str, reads2: str | None = None) -> TableTrack

Run full discover -> count pipeline.

Parameters:

Name Type Description Default
reads1 str

Path to primary reads file.

required
reads2 str | None

Optional path to paired-end reads file.

None

Returns:

Type Description
TableTrack

TableTrack mapping barcodes to counts.

Examples:

>>> track = BarcodeCount("barcodes.fasta").run("reads.fq")
Source code in src/seqchain/recipes/_draft/barcode_count.py
def run(self, reads1: str, reads2: str | None = None) -> TableTrack:
    """Run full discover -> count pipeline.

    Args:
        reads1: Path to primary reads file.
        reads2: Optional path to paired-end reads file.

    Returns:
        `TableTrack` mapping barcodes to counts.

    Examples:
        >>> track = BarcodeCount("barcodes.fasta").run("reads.fq")
    """
    library = read_barcode_fasta(self.library_path)
    bc_len = validate_barcodes(library)
    log.info("Library: %d barcodes, %dbp", len(library), bc_len)

    discover = self.discover_fn or heuristic_discover
    structure = discover(reads1, library, reads2=reads2)

    return count_barcodes(reads1, library, structure=structure, reads2=reads2)