gwseq_io
Read and write the files genome-scale sequencing produces — bigWig, bigBed, BAM, CRAM and HiC — from Python or the command line. The parsing, indexing and binning are done in Rust, and results are handed to numpy without a copy.
- Read bigWig and bigBed values binned over any regions, bigBed entries,
BAM and CRAM alignments (with or without an index), HiC contact matrices,
and GTF and GFF3 records and gene models, from a local path or an
http(s)://URL, as numpy arrays and tables of columns. - Write bigWig, bigBed and BAM files, and convert bedGraph, WIG, BED, SAM, BAM and CRAM into them.
- Pile up a BAM or CRAM into a coverage bigWig (ChIP, ATAC, RNA-seq).
gwseq-io, a command line over all of it.
Every example in this file is run by the test suite
(crates/gwseq-io-py/tests/test_readme.py), and every signature is checked
against the package's source.
Install
pip install gwseq-ioinstalls the package and thegwseq-iocommand. numpy comes with it.- Only a source distribution is published, so pip compiles the extension on the installing machine. That needs a Rust toolchain, 1.85 or newer, from rustup, and Python 3.11 or newer. No C source is compiled and no Python headers are needed: the extension is pure Rust, built against the stable ABI, and needs only the linker Rust itself uses.
cargo install --path crates/gwseq-io-cli, from a checkout, builds the samegwseq-iocommand without Python.
Quick start
>>> import gwseq_io
>>> with gwseq_io.open("signal.bw", "w", chr_sizes={"chr1": 10_000}) as writer:
... writer.write_values("chr1", 0, 100, [0.5, 1.0, 1.5, 2.0])
>>> reader = gwseq_io.open("signal.bw")
>>> reader
<gwseq_io.BigWigReader chromosomes=1>
>>> reader.read_values("chr1:1-400", bin_size=100)
array([[0.5, 1. , 1.5, 2. ]], dtype=float32)
>>> reader.quantify(["chr1:1-200", "chr1:201-400"])
array([0.75, 1.75], dtype=float32)
The same from the command line:
$ gwseq-io read signal.bw -r chr1:1-400 --bin-size 100
chr1 0 400 0.5 1 1.5 2
$ gwseq-io quantify signal.bw -r chr1:1-200 -r chr1:201-400
chr1 0 200 0.75
chr1 200 400 1.75
Concepts
Paths and URLs
Every path argument is a str or an os.PathLike. A reader also takes an
http:// or https:// URL, which it reads with range requests: only the
blocks a call needs are fetched, through a cache of blocks each reader keeps
(block_size, cache_bytes). A BAM's or CRAM's index is looked for at the
URL with .bai, .csi or .crai appended.
Reads in flight are bounded by io_parallel. For a local file it is the most
reads at once, 8 by default. For a URL it is where a limit shared by every
reader of that host starts, 16 by default: the limit halves when the server
answers 429 or 503 or resets a connection, and grows back while it does not,
up to 128 (or up to the io_parallel given). A server that throttles needs no
setting.
Regions and coordinates
Every read by region takes a RegionsLike:
- a string, 1-based and inclusive as samtools, UCSC and IGV write one:
"chr1:1,001-2,000"is the thousand bases from 1000 to 1999 counted from 0."chr1"is the whole chromosome,"chr1:1,001"runs to its end, and,and_group digits; - a
(chr, start, end)or(chr, start, end, strand)tuple, 0-based and half-open, as every number in this package is; - a sequence of strings and tuples, in any mix;
- a mapping or a DataFrame of columns:
chr,startandend, or one ofstart,endandcenterwithspan, and optionallystrand. Numbers are 0-based and half-open. Aliases like chromosome, chrom, chr_id, chromStart and plural forms are accepted. Asummitorabs_summitcolumn (MACS2's) is a center: a position on the chromosome. narrowPeak'speak, an offset from the start, is not one.
span re-cuts each region to that width, from the point anchor names:
"start", "end", "center" or "summit" (genomic, not strand-aware).
"center" is the center column, or the middle of start and end without one;
"summit" is the center or summit column only, and is refused without one.
anchor may be left out when the regions carry only one of start, end and
center.
>>> reader = gwseq_io.open("signal.bw")
>>> reader.quantify("chr1:101-200")
array([1.], dtype=float32)
>>> reader.quantify(("chr1", 100, 200))
array([1.], dtype=float32)
>>> reader.quantify({"chr": ["chr1"], "start": [100], "end": [200]})
array([1.], dtype=float32)
>>> reader.read_values({"chr": ["chr1", "chr1"], "center": [100, 300]}, span=100, bin_size=50)
array([[0.5, 1. ],
[1.5, 2. ]], dtype=float32)
A walk over a file reports its regions as Region, a NamedTuple of chr,
start, end and strand ("+", "-", or "." for none).
The types the signatures below use:
import os
from collections.abc import Callable, Mapping, Sequence
from typing import Any, Literal
import numpy as np
import numpy.typing as npt
StrPath = str | os.PathLike[str]
Progress = bool | Callable[[int, int], object] | None
Flags = int | str
Zoom = Literal["full", "auto"] | int
Anchor = Literal["start", "end", "center", "summit"]
Reduction = Literal["mean", "sd", "sem", "sum", "count", "min", "max", "l1norm", "l2norm"]
Snap = bool | Literal["auto", "expand", "force_expand"]
RegionTuple = tuple[str, int, int] | tuple[str, int, int, str]
RegionsLike = str | RegionTuple | Sequence[str | RegionTuple] | Mapping[str, Any] | Any # Any: a DataFrame or a Table
Region2D = str | tuple[str, int, int] | tuple[str, int, int, str]
Threads
Two process-wide pools do the work, shared by every reader and writer:
- the CPU pool decodes, bins and compresses. It has one thread per core, up
to 64, unless
GWSEQ_IO_THREADSorset_threads(n)says otherwise; either must come before the pool is first used. - the IO pool waits on files and servers. It starts threads on demand, up to
128 (
GWSEQ_IO_IO_THREADSorset_io_threads(n), changeable at any time), and they exit after 30 s idle.
parallel caps how much of the CPU pool one call uses: a reader takes it once,
at open, and a writer or a conversion as an argument. None is the whole pool.
A child made by os.fork starts with fresh pools.
set_max_output_bytes(n) bounds what one call may return, 4 GiB by default: a
read whose result would be larger raises InvalidRequest before it reads
anything.
Ctrl-C and progress
Every read, conversion and pileup, and the close() of a bigWig or bigBed
writer, runs on the pool while the calling thread waits, so Ctrl-C stops
it: the work is cancelled, the call raises KeyboardInterrupt, and a file
being written is discarded, leaving what was at its path before. The reader
stays usable.
progress is None (nothing), True (a bar on standard error, redrawn in
place on a terminal and a line at a time otherwise), or a callable taking
(done, total). The callable runs on the calling thread, at most every
~100 ms, and once more with done == total when the call finishes; an
exception it raises cancels the call and is re-raised. The units are the
call's own, most often bytes of the input; done / total is the share done.
>>> seen = []
>>> summary = gwseq_io.pileup_reads("reads.bam", "coverage.bw", progress=lambda done, total: seen.append((done, total)))
>>> seen[-1][0] == seen[-1][1]
True
Errors
Everything the package raises is a gwseq_io.Error, and each class also
derives from the built-in exception that means the same thing, so
except ValueError and except OSError keep working:
Error
├── InvalidRequest (ValueError) the call asked for something impossible
│ ├── UnknownChromosome .chromosome .available
│ ├── Closed a reader or writer used after close()
│ └── WriterFailed a writer used after a write failed
├── InvalidFile not a file this reads .filename
│ └── CorruptFile its bytes contradict each other .offset
├── SourceError (OSError) the bytes did not arrive, or did not land
│ ├── FileNotFound (FileNotFoundError)
│ ├── PermissionDenied (PermissionError)
│ └── HttpError .url .status
└── Unsupported (NotImplementedError) a feature of the format this does not implement
A CRAM slice that does not match the reference it is decoded against raises
CorruptFile. A conversion that finds its input malformed raises
InvalidFile naming the line.
>>> reader = gwseq_io.open("signal.bw")
>>> reader.quantify("chrZ:1-100")
Traceback (most recent call last):
...
gwseq_io.UnknownChromosome: Chromosome chrZ not found (available: chr1)
>>> try:
... gwseq_io.open("missing.bw")
... except FileNotFoundError as error:
... print(error)
[Errno 2] No such file or directory: 'missing.bw'
Reference
The examples run in a directory holding signal.bw (the quick start's),
reads.bam with its .bai, reads.cram with ref.fa, the bigBeds
features.bb and genes.bb, contacts.hic, and annotation.gtf.gz with its
.tbi: small files of the test suite, listed under
Development.
Module functions
@overload
def open(
path: StrPath,
mode: Literal["r"] = "r",
*,
parallel: int | None = ...,
io_parallel: int | None = ...,
zoom_correction: float = ...,
block_size: int | None = ...,
cache_bytes: int | None = ...,
index: StrPath | None = ...,
reference: StrPath | None = ...,
) -> BigWigReader | BigBedReader | BamReader | CramReader | HiCReader | GtfReader: ...
@overload
def open(
path: StrPath,
mode: Literal["w"],
*,
type: Literal["bigwig", "bigbed", "bam"] | None = None,
**options: Any,
) -> BigWigWriter | BigBedWriter | BamWriter: ...
def get_chr_sizes(genome: str, *, full: bool = False) -> dict[str, int]: ...
def resolve_regions(
regions: RegionsLike,
*,
chr_sizes: Mapping[str, int] | str | None = None,
span: int | None = None,
anchor: Anchor | None = None,
bin_size: int | None = None,
bin_count: int | None = None,
snap_to_grid: Snap = "auto",
full_bin: bool = False,
return_full: bool = False,
return_binned: bool = True,
bins: Binning | None = None,
) -> Table: ...
def set_threads(n: int) -> None: ...
def set_io_threads(n: int) -> None: ...
def set_max_output_bytes(n: int) -> None: ...
open(path)reads the format from the file's first bytes, not its name, and returns the reader of that format (a GTF or GFF3 by its text, inflated when it is compressed);optionsare that class's keywords (the...above), and one it does not take is refused.open(path, "w")returns the writertypenames, or the one the extension names:.bw/.bigwig,.bb/.bigbed,.bam.resolve_regionssays what a binned read would bin, without a file: see Resolving regions.chr_sizesis a{name: size}mapping or a bundled assembly's name (no network); withNone, names are not checked, andbin_basesis not limited by chromosome ends.get_chr_sizesgives an assembly's chromosome sizes: bundled for mm10, mm39, hg19, hg38, dm6, ce11 and sacCer3, asked of the UCSC API for any other and forfull=True, which keeps the unplaced and alternate contigs.set_threads,set_io_threadsandset_max_output_bytes: see Threads.
>>> type(gwseq_io.open("reads.bam")).__name__
'BamReader'
>>> gwseq_io.open("reads.cram", reference="ref.fa").type
'cram'
>>> gwseq_io.open("reads.bam", zoom_correction=0.5)
Traceback (most recent call last):
...
gwseq_io.InvalidRequest: zoom_correction is not an option of a bam reader, which is what reads.bam needs (its options: index, parallel, io_parallel, block_size, cache_bytes)
>>> list(gwseq_io.get_chr_sizes("sacCer3").items())[:2]
[('chrI', 230218), ('chrII', 813184)]
Tables
Every read of entries, records, alignments or gene models returns a Table:
a dict of columns of one length. Numbers are numpy arrays (int64,
float64), text is a list of str (None where a cell holds none), and a
column of mixed cells is a list of objects, so pandas.DataFrame(table) takes
a table as it is. len(table) is the number of columns, as for any dict;
row_count is the number of rows.
class Table:
row_count: int
groups: npt.NDArray[np.int64] | None
nested: dict[str, Table]
meta: dict[str, str]
def take(self, rows: Any) -> Table: ...
def group(self, i: int) -> Table: ...
def split(self) -> list[Table]: ...
def rows(self) -> Iterator[Any]: ...
def to_dict(self) -> dict[str, Any]: ...
- One table per call. A read over several regions puts every region's rows
in one table, region after region, and
groupssays which are whose: regioni's rows aregroups[i]:groups[i + 1], and an entry overlapping two regions is in both.group(i)is regioni's rows as a table, andsplit()every region's. A read of the whole file hasgroups=None. - Nested parts. Parts with a number of rows of their own are tables in
nested, grouped by the rows of their parent the same way: a transcript's exons aretable.nested["exons"], transcripti's at rowsgroups[i]:groups[i + 1]of it, and a gene's transcripts aretable.nested["transcripts"], with their exons nested in turn. - Rows.
rows()gives one object per row, built as it is iterated: aBedEntryfor a bigBed, a BED or peaks table, aGtfEntryfor GTF or GFF3 records, aTranscriptor aGenefor gene models, aSamEntryfor alignments.take(rows)keeps the rows named, in that order, with their nested parts, and no groups. metasays what the table is: itskind("bed","gtf","transcript","gene","alignment","regions") and, for annotation, itsdialect.- A table pickles, and so does each row (as a table of itself alone).
Table(columns)makes one of any columns of one length.
>>> reader = gwseq_io.open("features.bb")
>>> table = reader.read_entries(["chr1:1-3,000", "chr1:4,001-4,500"])
>>> table
<gwseq_io.Table 6 rows, 8 columns, 2 groups>
>>> list(table)
['chr', 'start', 'end', 'name', 'score', 'strand', 'signal', 'note']
>>> table["start"], table.groups
(array([ 846, 1336, 2330, 846, 4292, 4476]), array([0, 3, 6]))
>>> table.group(1)["name"]
['transcript_955804_0', 'LOC389182_3', 'gene504425_4']
>>> entry = next(table.rows())
>>> entry, entry["signal"]
(BedEntry(chr1:846-4649, 5 columns), 18.92)
>>> import pandas
>>> pandas.DataFrame(table).shape
(6, 8)
Argument objects
Two groups of keywords many calls share can be given as one object instead:
class ReadFilter:
exclude_flags: Flags = "UNMAP,SECONDARY,SUPPLEMENTARY"
require_flags: Flags = 0
min_mapq: int = 0
drop_improper_pairs: bool = True
class Binning:
bin_size: int | None = None
bin_count: int | None = None
snap_to_grid: Snap = "auto"
full_bin: bool = False
filter=ReadFilter(min_mapq=30) is min_mapq=30 wherever the read
filter applies, and bins=Binning(bin_size=100) is
bin_size=100 wherever a read is binned (Binning). Both are
frozen dataclasses, made once and passed to every call. Giving an object and
one of its own keywords together is refused, naming the two.
>>> keep = gwseq_io.ReadFilter(exclude_flags="UNMAP,DUP", min_mapq=40)
>>> gwseq_io.open("reads.bam").read_entries("chr1:1,001-2,300", filter=keep)["read_name"]
['plain_fwd', 'pair_fr', 'pair_fr']
>>> gwseq_io.open("signal.bw").read_values("chr1:1-400", bins=gwseq_io.Binning(bin_size=200))
array([[0.75, 1.75]], dtype=float32)
>>> gwseq_io.open("reads.bam").read_entries("chr1", filter=keep, min_mapq=10)
Traceback (most recent call last):
...
gwseq_io.InvalidRequest: filter and min_mapq cannot be given together: min_mapq is one of filter's
Readers
Every reader is one class per format, deriving from Reader, and from
BbiReader (bigWig, bigBed, and BedReader, a table read into memory) or
AlignmentReader (BAM, CRAM), so isinstance(reader, gwseq_io.BbiReader)
works. A reader is frozen, may be used from several threads at once, and
pickles (it is opened again on its path, with its options). It holds a file
handle and a block cache until close() or the end of a with block; after
that a read raises Closed, and what was read at open stays readable.
class Reader:
path: str
type: str # "bigwig", "bigbed", "bam", "cram", "hic", "gtf", "gff3", "bed" or "peaks"
closed: bool
chr_sizes: dict[str, int] # in the file's order
def close(self) -> None: ...
def resolve_regions(
self,
regions: RegionsLike,
*,
span: int | None = None,
anchor: Anchor | None = None,
bin_size: int | None = None,
bin_count: int | None = None,
snap_to_grid: Snap = "auto",
full_bin: bool = False,
return_full: bool = False,
return_binned: bool = True,
bins: Binning | None = None,
) -> Table: ...
resolve_regions lays a binned read's bins over the reader's chromosomes:
see Resolving regions. On a BAM or CRAM, those are the
bins read_values would read from a pileup of the file.
Every reader's constructor takes:
parallel: the most of the CPU pool one call uses.Noneis all of it.io_parallel: reads in flight (see Paths and URLs).block_size,cache_bytes: the cache in front of the file. Blocks are 32 KiB for a local file and 1 MiB for a URL, 128 of them by default.
Every read of entries takes its regions the same way: regions (a
RegionsLike, with span and anchor) gives one group of rows per region;
regions=None with chromosomes (all of them when None) gives every entry
of those chromosomes once, in file order. iter_entries walks whole
chromosomes a window at a time instead, each entry in the window it starts
in, one table per step.
bigWig and bigBed
class BigWigReader:
main_header: BbiHeader
def __init__(
self,
path: StrPath,
*,
parallel: int | None = None,
io_parallel: int | None = None,
zoom_correction: float = 1/3,
block_size: int | None = None,
cache_bytes: int | None = None,
) -> None: ...
class BbiReader:
zoom_headers: list[ZoomHeader] # finest first
total_summary: TotalSummary
def read_values(
self,
regions: RegionsLike,
*,
span: int | None = None,
anchor: Anchor | None = None,
bin_size: int | None = None,
bin_count: int | None = None,
snap_to_grid: Snap = "auto",
full_bin: bool = False,
bin_mode: Reduction = "mean",
def_value: float = 0.0,
zoom: Zoom = "full",
bins: Binning | None = None,
progress: Progress = None,
) -> npt.NDArray[np.float32]: ...
def quantify(
self,
regions: RegionsLike,
*,
span: int | None = None,
anchor: Anchor | None = None,
reduce: Reduction = "mean",
def_value: float = 0.0,
zoom: Zoom = "full",
progress: Progress = None,
) -> npt.NDArray[np.float32]: ...
def profile(
self,
regions: RegionsLike,
*,
span: int | None = None,
anchor: Anchor | None = None,
bin_size: int | None = None,
bin_count: int | None = None,
snap_to_grid: Snap = "auto",
full_bin: bool = False,
bin_mode: Reduction = "mean",
def_value: float = 0.0,
zoom: Zoom = "full",
reduce: Reduction = "mean",
bins: Binning | None = None,
progress: Progress = None,
) -> npt.NDArray[np.float32]: ...
def iter_values(
self,
*,
chromosomes: Sequence[str] | None = None,
window: int = 1000000,
bin_size: int | None = None,
bin_mode: Reduction = "mean",
full_bin: bool = False,
def_value: float = 0.0,
zoom: Zoom = "full",
progress: Progress = None,
) -> RegionIterator[npt.NDArray[np.float32]]: ...
def to_bedgraph(
self,
output: StrPath,
*,
chromosomes: Sequence[str] | None = None,
bin_size: int | None = None,
bin_mode: Reduction = "mean",
full_bin: bool = False,
def_value: float = 0.0,
zoom: Zoom = "full",
merge_bins: bool = True,
progress: Progress = None,
) -> None: ...
def to_wig(
self,
output: StrPath,
*,
chromosomes: Sequence[str] | None = None,
bin_size: int | None = None,
bin_mode: Reduction = "mean",
full_bin: bool = False,
def_value: float = 0.0,
zoom: Zoom = "full",
progress: Progress = None,
) -> None: ...
read_valuesgives an array of shape(regions, bins);quantifyone value per region, reduced over exactly the bases it names;profileone value per bin, the regions binned asread_valuesbins them and each column reduced byreduceover the regions, NaN left out.iter_valueswalks whole chromosomes awindowat a time; each step is a 1-D array of the window's bins. The windows start at multiples ofbin_size, so laid end to end they are bit-identical to one read of the whole chromosome.to_bedgraphandto_wigwrite whole chromosomes as text, the valuesread_valuesgives at the same settings. A bin holding NaN is left out of the file, sodef_value=float("nan")writes the covered part alone.merge_binswrites a run of equal bins as one bedGraph line.- On a bigBed, the values are the depth of coverage its entries make.
zoom_correctionscales the choicezoom="auto"makes (below).
>>> reader = gwseq_io.open("signal.bw")
>>> reader.read_values(["chr1:1-400", ("chr1", 100, 500, "-")], bin_size=200)
array([[0.75, 1.75],
[1. , 1.25]], dtype=float32)
>>> reader.profile([("chr1", 0, 200), ("chr1", 200, 400)], bin_size=100)
array([1. , 1.5], dtype=float32)
>>> walk = reader.iter_values(bin_size=1000, window=5000)
>>> len(walk), walk.regions[0]
(2, Region(chr='chr1', start=0, end=5000, strand='.'))
>>> for region, values in walk.items():
... print(region.start, values)
0 [0.5 0. 0. 0. 0. ]
5000 [0. 0. 0. 0. 0.]
>>> reader.to_bedgraph("signal.bedgraph")
>>> print(open("signal.bedgraph").read(), end="")
chr1 0 100 0.5
chr1 100 200 1
chr1 200 300 1.5
chr1 300 400 2
chr1 400 10000 0
Binning
bin_size, bin_count, snap_to_grid and full_bin can be given together
as bins=Binning(...) (Argument objects).
A gap base is a base in [0, chr_size) that no record covers. Bases
outside the chromosome are neither data nor gap: a bin crossing the chromosome
end is simply shorter.
| Parameter | Applies to | Default | Meaning |
|---|---|---|---|
bin_size |
read_values, profile, walks, exports | 1 when bin_count is unset |
Bin width, laid from each region's own start (its 5′ end for "−"). A row has floor(span/bin_size) bins, or ceil with full_bin. Every region must give the same count, or the call raises naming two spans and suggesting bin_count. A size of 0 is refused. Not a quantify parameter. |
bin_count |
read_values, profile | unset | Each region is cut into exactly n bins of span/n, which may be fractional; weights are exact. The way to read regions of unequal span into one matrix. Refused with bin_size, full_bin or snap_to_grid=True. |
snap_to_grid |
read_values, profile | "auto" | Moves each row onto the genome-wide grid of bin_size (its multiples from base 0): the region's 5′ end goes to the nearest multiple, the lower one on a tie, and the row of n bins moves with it, so n still comes from the span. True always, False never, "auto" when a region has at least 10 bins, where the move (at most half a bin) is at most 5% of it (snapping will occur when span ≥ 100 bp with a bin size of 10 bp, or when span ≥ 10,000 bp with a bin size of 1,000 bp). Every region of a call has the same n, so its rows all snap or none does. "expand" and "force_expand" grow every region outward to the grid instead, so each of its bases is in a bin: ceil(span/bin_size) bins or one more, depending on where it sits. "expand" makes n the most any region needs; "force_expand" makes it ceil(span/bin_size) + 1 from the span alone, so a region already on the grid gets one too. A region one bin short gets it on the side it grew less on, the 3′ side on a tie. Regions of a call must have the same ceil(span/bin_size). Ignored with bin_count, refused there as True, "expand" or "force_expand". |
full_bin |
read_values, profile, walks, exports | False | Keeps a last partial bin at full width, clipped at the chromosome end. In walks and exports it keeps a chromosome's short last bin. |
zoom |
every value read, bigBed included | "full" | "auto" picks the coarsest level with reduction ≤ w × zoom_correction, where w is the narrowest output bin (for quantify, the region's span). An int picks a level. |
def_value |
every value read | 0 | The value of every gap base. Finite: gap bases join every reduction (UCSC's "mean0"). NaN: gap bases are left out, and a bin or region with no data is NaN. |
bin_mode |
read_values, profile, walks, exports | "mean" | Any reduction (mean, sd, sem, sum, count, min, max, l1norm, l2norm), over the bases of one bin: the same function quantify applies over a region. |
count |
everywhere | — | Covered bases, the sum of record weights. It never includes def-filled bases. |
reduce (quantify) |
quantify | "mean" | Over the region's bases, read exactly [start, end) and never snapped. |
reduce (profile) |
profile | "mean" | Folds each region's finished bin value per column, skipping NaN and the bins with no base inside the chromosome, whatever def_value. A column left with none is NaN (0 for count). |
| l1norm with zoom | quantify, bin_mode |
— | An explicit level is refused; "auto" resolves to full data. |
| strand "−" | read_values, profile, HiC reads | — | Bins are laid from the end, so bin 0 is the 5′ end, and any remainder falls at the 3′ side. A snapped "−" row moves its end, the 5′ end. Ignored by quantify and entries. |
| NaN in a file | everywhere | — | A gap in every reduction. sd and sem use the population (n) variance. |
>>> reader = gwseq_io.open("signal.bw")
>>> reader.read_values([("chr1", 50, 400)], bin_size=100)
array([[0.75, 1.25, 1.75]], dtype=float32)
>>> reader.read_values([("chr1", 50, 400)], bin_size=100, full_bin=True)
array([[0.75, 1.25, 1.75, 1. ]], dtype=float32)
>>> reader.read_values([("chr1", 50, 400)], bin_size=100, snap_to_grid=True)
array([[0.5, 1. , 1.5]], dtype=float32)
>>> reader.read_values([("chr1", 30, 1030)], bin_size=100)
array([[0.5, 1. , 1.5, 2. , 0. , 0. , 0. , 0. , 0. , 0. ]], dtype=float32)
>>> reader.read_values([("chr1", 30, 1030)], bin_size=100, snap_to_grid=False)
array([[0.65, 1.15, 1.65, 1.4 , 0. , 0. , 0. , 0. , 0. , 0. ]],
dtype=float32)
>>> reader.read_values([("chr1", 50, 400)], bin_size=100, snap_to_grid="expand")
array([[0.5, 1. , 1.5, 2. ]], dtype=float32)
>>> reader.read_values([("chr1", 50, 400)], bin_size=100, snap_to_grid="force_expand")
array([[0.5, 1. , 1.5, 2. , 0. ]], dtype=float32)
>>> reader.read_values(["chr1:1-400", "chr1:1-200"], bin_count=2)
array([[0.75, 1.75],
[0.5 , 1. ]], dtype=float32)
>>> reader.read_values("chr1:1-500", bin_size=250, def_value=float("nan"))
array([[0.9 , 1.8333334]], dtype=float32)
>>> reader.quantify("chr1:1-500", reduce="count")
array([400.], dtype=float32)
Resolving regions
resolve_regions says what a binned read would bin, without reading
anything. It takes the arguments of read_values and refuses what it
refuses. Every reader has it, reading against its own chromosomes, and so
does the module, given chr_sizes.
The result is a table, one row per region in the order given:
| Key | What |
|---|---|
chr, strand |
the chromosome as the file names it, and the strand given ("." for none) |
start, end |
the row the read bins: its first bin's edge to its last, after snap_to_grid, not clipped to the chromosome. With return_binned=False, the region after span and anchor, as quantify reads it. |
snapped, shift |
with return_full: whether the row moved onto the grid, and how far its 5′ end moved |
bin_bases |
with return_full: the bases of start–end inside the chromosome (only base 0 bounds them without chr_sizes) |
A row spans n · bin_size, without the remainder full_bin=False drops; with
bin_count it is the region itself. Read again with snap_to_grid=False, a
row's bounds give the read's bins. A row on the grid of bin_size is on the
grid of every divisor of it, so regions resolved once at a HiC resolution
give a bigWig read at a finer bin size and a HiC read the same bounds, bin
for bin.
>>> reader = gwseq_io.open("signal.bw")
>>> resolved = reader.resolve_regions([("chr1", 30, 1030)], bin_size=100, return_full=True)
>>> resolved["start"], resolved["end"], resolved["shift"]
(array([0]), array([1000]), array([-30]))
>>> reader.resolve_regions([("chr1", 30, 1030)], bin_size=100, return_binned=False)["start"]
array([30])
>>> gwseq_io.resolve_regions(("chr1", 9950, 10_150), chr_sizes={"chr1": 10_000}, bin_size=100, return_full=True)["bin_bases"]
array([50])
>>> hic = gwseq_io.open("contacts.hic")
>>> peaks = {"chr": ["chr1"], "summit": [25_000], "strand": ["-"]}
>>> rows = gwseq_io.resolve_regions(peaks, chr_sizes=hic.chr_sizes, span=15_000, bin_size=10_000, snap_to_grid="expand")
>>> rows["start"], rows["end"]
(array([10000]), array([40000]))
>>> gwseq_io.open("signal.bw").read_values(rows, bin_size=1_000).shape
(1, 30)
>>> hic.read_values(("chr1", 10_000, 40_000, "-"), bin_size=10_000).shape
(3, 3)
bigBed entries
class BigBedReader:
main_header: BbiHeader
def __init__(
self,
path: StrPath,
*,
parallel: int | None = None,
io_parallel: int | None = None,
zoom_correction: float = 1/3,
block_size: int | None = None,
cache_bytes: int | None = None,
) -> None: ...
auto_sql: dict[str, str]
extra_indexes: list[str]
def read_entries(
self,
regions: RegionsLike | None = None,
*,
chromosomes: Sequence[str] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
columns: int | Sequence[str] | None = None,
where: dict[str, str | Sequence[str]] | None = None,
progress: Progress = None,
) -> Table: ...
def iter_entries(
self,
*,
chromosomes: Sequence[str] | None = None,
columns: int | Sequence[str] | None = None,
window: int = 1000000,
progress: Progress = None,
) -> RegionIterator[Table]: ...
def to_bed(
self,
output: StrPath,
*,
chromosomes: Sequence[str] | None = None,
columns: int | None = None,
progress: Progress = None,
) -> None: ...
def to_bigbed(
self,
output: StrPath,
*,
compression_level: int = 6,
items_per_slot: int | None = None,
zoom_share: float = 0.2,
extra_index: Sequence[str] | None = None,
progress: Progress = None,
) -> None: ...
auto_sqlis each column and the type the file declares for it, in the file's order.extra_indexesare the columns the file carries an index on.read_entriesgives a table ofchr,start,endand the columns past them, named by the autoSql and typed by it: an integer columnint64, afloatordoubleonefloat64, the rest text. A column whose cells do not all read as the number it declares (UCSC'sitemRgb, auintholding255,0,0) is text, whole.columnsis how many columns to read, the three coordinates included, or the names of the columns past them to read, in that order;Nonereads them all, and a column left out is never parsed.wherekeeps the entries whose columns hold its values:{"name": "x"}, or{"name": ["x", "y"]}for any of several. Several fields keep what holds all of them. On a bigBed, the first field is one ofextra_indexes, looked up through its index; the others are read through. With regions, it keeps the entries of each region that hold its values; without, every such entry of the file, in file order.to_bedwrites every entry as a BED line, its firstcolumnscolumns.to_bigbedwrites every entry, every column, to a new bigBed, as the text the file holds them as: a copy, recompressed atcompression_level, of a local file or a URL.extra_indexnames the columns to index, by defaultextra_indexes; the other options are those ofBigBedWriter.
>>> reader = gwseq_io.open("features.bb")
>>> reader.extra_indexes
['name']
>>> entries = reader.read_entries("chr1:1-3000")
>>> entries["start"], entries["signal"]
(array([ 846, 1336, 2330]), array([ 18.92, 205.43, 458.74]))
>>> reader.read_entries(where={"name": "LOC472203_1"})["start"]
array([1336])
>>> reader.read_entries(where={"name": ["LOC472203_1", "LOC548305_2"], "strand": "+"})["name"]
['LOC548305_2']
>>> reader.read_entries("chr1:1-3000", columns=["strand"])
<gwseq_io.Table 3 rows, 4 columns, 1 group>
>>> reader.quantify("chr1:1-3000", reduce="max")
array([3.], dtype=float32)
A BedEntry is one row of such a table: the coordinates as attributes and
every column by key, a number as int or float. keys(), len(), in and
iteration work as on a dict, so dict(entry) builds one.
class BedEntry:
chr: str
start: int
end: int
def keys(self) -> list[str]: ...
def to_dict(self) -> dict[str, Any]: ...
>>> next(gwseq_io.open("genes.bb").read_entries("chr1:1-400", columns=6).rows()).to_dict()
{'chr': 'chr1', 'start': 323, 'end': 3048, 'name': 'tx0', 'score': 633, 'strand': '.'}
In-memory BED and peaks
def open_bed(
path: StrPath,
*,
fields: Mapping[str, str] | None = None,
chr_sizes: Mapping[str, int] | str | None = None,
extra_index: Sequence[str] | None = None,
parallel: int | None = None,
zoom_correction: float = 1/3,
progress: Progress = None,
) -> BedReader: ...
def open_peaks(
source: StrPath | Mapping[str, Any] | Any, # Any: a DataFrame or a Table
*,
sep: str | None = None,
header: Literal["auto"] | int | None = "auto",
fields: Mapping[str, str] | None = None,
chr_sizes: Mapping[str, int] | str | None = None,
extra_index: Sequence[str] | None = None,
parallel: int | None = None,
zoom_correction: float = 1/3,
progress: Progress = None,
) -> BedReader: ...
class BedReader:
table: Table
auto_sql: dict[str, str]
extra_indexes: list[str]
def read_entries(
self,
regions: RegionsLike | None = None,
*,
chromosomes: Sequence[str] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
columns: int | Sequence[str] | None = None,
where: dict[str, str | Sequence[str]] | None = None,
row_index: bool = False,
progress: Progress = None,
) -> Table: ...
def iter_entries(
self,
*,
chromosomes: Sequence[str] | None = None,
columns: int | Sequence[str] | None = None,
window: int = 1000000,
row_index: bool = False,
progress: Progress = None,
) -> RegionIterator[Table]: ...
def to_bed(
self,
output: StrPath,
*,
chromosomes: Sequence[str] | None = None,
columns: int | None = None,
progress: Progress = None,
) -> None: ...
def to_bigbed(
self,
output: StrPath,
*,
compression_level: int = 6,
items_per_slot: int | None = None,
zoom_share: float = 0.2,
extra_index: Sequence[str] | None = None,
progress: Progress = None,
) -> None: ...
Both read a whole table into memory and give a BedReader: every method of
BigBedReader, answered as the bigBed to_bigbed writes from it answers,
values bit for bit. The rows may come in any order; they are held sorted by
chromosome, start, end and then their order in the input, as typed columns,
parsed on the CPU pool. table is those rows. The input is a local path or a
URL, gzipped or not.
open_bedreads a BED asconvert_to_bigbedreads one,fieldsincluded, but unsorted. A BED of 10 or 9 columns whose 7th to 9th are numbers that are not a BED9's thick part and colour is a narrowPeak or a broadPeak, typed as UCSC'sbigNarrowPeak.astypes them;convert_to_bigbedreads it the same way.open_peaksreads a table with a header: a TSV or CSV, or a DataFrame, a mapping of columns or aTable(a read's, or a model table: a transcript table becomes a table of transcripts). Its coordinates are the columns a table of regions takes as them (Regions and coordinates), 0-based and half-open: a MACS2_peaks.xlscounts its starts from 1. Every other column is kept, in order.sepis one character;Noneis a tab when the header line holds one, a comma otherwise. With a comma, a field in double quotes is one cell.header="auto"is the first line that is not blank, a comment,trackorbrowser, unless its 2nd and 3rd fields are whole numbers. Then it is the#line right before the data when that has as many fields, as bedtools and MACS2 write it, and otherwise there is none. Anintis the line's index from 0;Noneis no header. A table with none gets the standard BED names.- Each column of text is typed by every cell in it:
intwhen all are whole numbers (bigintpast 32 bits),doublewhen all are numbers or missing (empty,NA,NaN,nanor., held asnan),stringotherwise. A DataFrame's columns keep their dtypes: integers and booleansint, floatsdouble, a nullable integer column with a gapdouble, the reststring, a missing value"".fieldssets the type of a column by name.
chr_sizesis a dict or an assembly's name. It bounds every coordinate, resolves the names as a read does, and orders the chromosomes. Without it, the chromosomes come in the order they first appear, each as long as its furthest entry.typeis"bed"or"peaks", andpaththe path or URL ("<table>"for columns). There is nomain_header, and no zoom level: every read is answered from the full data, whatever itszoom, andzoom_headersis empty.total_summaryis computed the first time it is asked for.wheretakes any column past the coordinates, and builds an index on it the first time it is asked.extra_indexesis whatto_bigbedindexes:extra_index, ornamewhen there is such a column.row_index=Trueadds a last column,row(_rowwhen the table has arow), the row each entry was in the input, counted from 0, data lines only.to_bigbedwrites the table to a bigBed. A column name autoSql cannot declare is written with_for every character but a letter, a digit or_(-log10(pvalue)is_log10_pvalue_).close()frees the table. ABedReaderpickles its content.
>>> with open("unsorted.tsv", "w") as text:
... _ = text.write("name\tchrom\tstart\tend\tq\np2\tchr1\t500\t900\t3.5\np1\tchr1\t100\t300\tNA\n")
>>> peaks = gwseq_io.open_peaks("unsorted.tsv")
>>> peaks.type, peaks.auto_sql
('peaks', {'chrom': 'string', 'chromStart': 'uint', 'chromEnd': 'uint', 'name': 'string', 'q': 'double'})
>>> peaks.read_entries(row_index=True).to_dict()
{'chr': ['chr1', 'chr1'], 'start': array([100, 500]), 'end': array([300, 900]), 'name': ['p1', 'p2'], 'q': array([nan, 3.5]), 'row': array([1, 0])}
>>> peaks.read_entries(where={"name": "p2"})["start"]
array([500])
>>> peaks.quantify("chr1:1-1,000", reduce="sum")
array([600.], dtype=float32)
>>> peaks.to_bigbed("unsorted.bb")
>>> gwseq_io.open("unsorted.bb").read_entries("chr1:401-501")["name"]
['p2']
>>> table = gwseq_io.open_peaks({"chr": ["chr2", "chr1"], "start": [10, 5], "end": [20, 9], "score": [0.5, 2.0]})
>>> table.chr_sizes
{'chr2': 20, 'chr1': 9}
>>> import pickle
>>> list(pickle.loads(pickle.dumps(table)).read_entries().rows())
[BedEntry(chr2:10-20, 1 column), BedEntry(chr1:5-9, 1 column)]
GTF and GFF3
def open_gtf(
path: StrPath,
*,
index: StrPath | None = None,
format: Literal["gtf", "gff3"] | None = None,
chr_sizes: Mapping[str, int] | str | None = None,
feature: str | Sequence[str] | None = None,
attributes: Sequence[str] | None = None,
strict: bool = True,
model_tables: bool | None = None,
parallel: int | None = None,
io_parallel: int | None = None,
block_size: int | None = None,
cache_bytes: int | None = None,
) -> GtfReader: ...
def index_gtf(
input: StrPath,
output: StrPath,
*,
format: Literal["gtf", "gff3"] | None = None,
feature: str | Sequence[str] | None = None,
csi: bool = False,
compression_level: int = 6,
parallel: int | None = None,
progress: Progress = None,
) -> dict[str, Any]: ...
class GtfReader:
def __init__(
self,
path: StrPath,
*,
index: StrPath | None = None,
format: Literal["gtf", "gff3"] | None = None,
chr_sizes: dict[str, int] | str | None = None,
feature: str | Sequence[str] | None = None,
attributes: Sequence[str] | None = None,
strict: bool = True,
model_tables: bool | None = None,
parallel: int | None = None,
io_parallel: int | None = None,
block_size: int | None = None,
cache_bytes: int | None = None,
) -> None: ...
indexed: bool
index_path: str
header_lines: list[str]
skipped_models: int
def read_entries(
self,
regions: RegionsLike | None = None,
*,
chromosomes: Sequence[str] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
model: Literal["transcript", "gene"] | None = None,
feature: str | Sequence[str] | None = None,
attributes: Sequence[str] | None = None,
where: dict[str, str | Sequence[str]] | None = None,
gene_type: str | Sequence[str] | None = None,
transcript_type: str | Sequence[str] | None = None,
progress: Progress = None,
) -> Table: ...
def iter_entries(
self,
*,
chromosomes: Sequence[str] | None = None,
model: Literal["transcript", "gene"] | None = None,
feature: str | Sequence[str] | None = None,
attributes: Sequence[str] | None = None,
gene_type: str | Sequence[str] | None = None,
transcript_type: str | Sequence[str] | None = None,
window: int = 1000000,
progress: Progress = None,
) -> RegionIterator[Table]: ...
def to_gtf(
self,
output: StrPath,
*,
feature: str | Sequence[str] | None = None,
index: bool = True,
compression_level: int = 6,
progress: Progress = None,
) -> None: ...
class GtfEntry:
chr: str
start: int
end: int
source: str
feature: str
score: float | None
strand: str
frame: int | None
attributes: str
def keys(self) -> list[str]: ...
def get(self, key: str, default: Any = None) -> Any: ...
def to_dict(self) -> dict[str, Any]: ...
open_gtf reads a GTF or a GFF3 compressed with BGZF and indexed by tabix
(a .tbi or .csi beside it, or index=), local or over HTTP, as a BAM is
read through its index: a region read inflates only the blocks that hold it.
A file without an index is refused, saying what it is and how to make
one: index_gtf(input, output) reads any GTF or GFF3 (local or a URL; plain,
gzip or BGZF; in any order), checks every record, sorts them by sequence (in
the order they first appear) and then start, keeping the order of records
with the same start, and writes output with <output>.tbi, or .csi with
csi=True or when a sequence is longer than a .tbi reaches. The header lines
are kept; a GFF3's ### lines and ##FASTA section are left out. It holds the
records in memory while it sorts, about the size of the text. gwseq-io index-gtf is the same.
- Coordinates are 0-based and half-open, as everywhere here:
startis the file's 1-based start less one.formatis told from the content when not given:##gff-version 3, or the first record's attributes (key "value";is GTF,key=valueGFF3). chr_sizesis a dict or an assembly's name. Without it a sequence is as long as a GFF3's##sequence-regionline says, or as far as its records reach, which is read the first time the sizes are needed (one small read per sequence).read_entrieswithoutmodelgives the records:chr,start,end,source,feature,score(NaN for.),strand,frame(−1 for.), then one column per attribute: every key found withattributes=None(None where a record has none), those keys with a list, and the raw column 9 asattributeswith(). A value given twice (GTF'stag) or a GFF3 list is joined with,.featurekeeps those feature types, tested before anything else is parsed.feature=andattributes=given toopen_gtfare the defaults of every read.wherekeeps the records whose attributes hold its values, as on a bigBed. The first read with a field reads the whole file once and keeps where each value's records are, so later ones take milliseconds.- A
GtfEntry, a row of a table of records, has the eight fixed columns as attributes (scoreandframeNone for.) and the attributes by key, parsed when first asked for: GTF quoted values arestr, bare numbersintorfloat; GFF3 values are percent-decoded; a key given twice or a GFF3 list (Parent=a,b) is alist[str].attributesis column 9 as the file holds it. to_gtfwrites the records, or afeaturesubset, as BGZF with a.tbi: the lines as the file holds them, its header first.
>>> gtf = gwseq_io.open("annotation.gtf.gz")
>>> gtf.type, gtf.index_path
('gtf', 'annotation.gtf.gz.tbi')
>>> records = gtf.read_entries("chr1:1,001-1,200", feature="transcript")
>>> records["start"], records["transcript_name"]
(array([1000, 1000]), ['Alpha-201', 'Alpha-202'])
>>> entry = next(records.rows())
>>> entry, entry["tag"], entry["level"]
(GtfEntry(transcript chr1:1000-2400), ['basic', 'CCDS'], 2)
>>> exons = gtf.read_entries("chr1:1-3,000", feature="exon", attributes=["transcript_id", "exon_number"])
>>> exons["start"], exons["exon_number"][:3]
(array([1000, 1000, 1500, 2000, 2000]), [1, 1, 2])
>>> gtf.read_entries(feature="gene", where={"gene_name": ["Beta", "Zeta"]})["gene_id"]
['G2', 'G6']
>>> gwseq_io.index_gtf("unsorted.gtf", "sorted.gtf.gz")["index"]
'sorted.gtf.gz.tbi'
Gene models
class GtfReader:
def tss(
self,
regions: RegionsLike | None = None,
*,
level: Literal["transcript", "gene"] = "transcript",
gene_type: str | Sequence[str] | None = None,
transcript_type: str | Sequence[str] | None = None,
) -> Table: ...
def to_bed12(
self,
output: StrPath,
*,
chromosomes: Sequence[str] | None = None,
name: str = "transcript_id",
gene_type: str | Sequence[str] | None = None,
transcript_type: str | Sequence[str] | None = None,
progress: Progress = None,
) -> None: ...
class Gene:
chr: str
start: int
end: int
strand: str
id: str
name: str | None
type: str | None
tss: int
exonic_length: int
transcripts: list[Transcript]
def keys(self) -> list[str]: ...
def to_dict(self) -> dict[str, Any]: ...
class Transcript:
chr: str
start: int
end: int
strand: str
id: str
name: str | None
type: str | None
gene_id: str
gene_name: str | None
gene_type: str | None
exons: npt.NDArray[np.int64] # (n, 2), in genome order
cds: npt.NDArray[np.int64] # (m, 2), the stop codon included
cds_start: int | None
cds_end: int | None
tss: int
tes: int
exonic_length: int
introns: npt.NDArray[np.int64]
utr5: npt.NDArray[np.int64]
utr3: npt.NDArray[np.int64]
def keys(self) -> list[str]: ...
def to_dict(self) -> dict[str, Any]: ...
read_entries(model="transcript") and model="gene" give the records grouped
into gene models: genes, which hold transcripts, which hold exons and a coding
sequence.
- Grouping. GTF: a record belongs to the transcript its
transcript_idnames and the gene itsgene_idnames;geneandtranscriptrecords give spans and attributes. GFF3:IDandParent; a record whose children include exons, CDS, UTRs or codons is a transcript, whatever its type is called (transcript,mRNA,lnc_RNA, …), its parent is its gene, and an exon listing several parents belongs to each. A file with no gene or transcript lines (a UCSCrefGeneGTF) takes the spans from the parts. - Exons and CDS are merged and in genome order; a transcript with no exon
record takes its exons from its CDS, UTRs and codons. The CDS includes the
stop codon: a GTF puts it outside the CDS and a GFF3 inside, so both give
the same
cds_end. UTRs are the exon parts outside the CDS, by strand, so a GTF'sUTRand a GFF3'sfive_prime_UTR/three_prime_UTRagree.tssandtesare the 5′ and 3′ positions.nameandtypearegene_name/gene_typeandtranscript_name/transcript_type, orNameandbiotype.gene["key"]andtranscript["key"]read the attributes of their own record. - The tables. A transcript table has
chr,start,end,strand, the transcript's and its gene's ids, names and types,cds_startandcds_end(−1 when non-coding),tss,tes,exon_countandexonic_length, and nestsexonsandcds(start,end). A gene table has the gene's columns,transcript_count,exonic_length(of its exons' union) andtss, and nests itstranscripts, as a transcript table.attributes=[...]adds those attributes of each model's own record. Their rows areTranscripts andGenes. - By region, a gene belongs to a region its span overlaps and comes whole,
with the exons outside the region. Walks give each gene in the window it
starts in.
wherekeeps the genes (or the transcripts) holding its values in any attribute of their own records, or the synthesizedgene_id,gene_name,gene_typeand their transcript counterparts. gene_typeandtranscript_typekeep those biotypes; a gene left with no transcript is left out.- The model tables. The first read of models starts building every gene
model of the file in the background, once, on the CPU pool; reads go through
tabix until it is done, then through the tables, which hold the genes sorted
by sequence with an interval index and the attributes looked up by value.
Whichever answers, the answer is the same. A read of the whole file, a walk
and
wherewait for the build.model_tables=Falsenever builds them, andTruebuilds them for a URL too, which by default builds them only on a read of the whole file.close()cancels a build in progress, and so doesos.fork(in the parent and the child, the next read of models starts it again). tss()gives the regions a read takes, one row per transcript (or gene, withlevel="gene"):chr,center(the TSS),strand,idandname, over the genes overlappingregionsor the whole file. Withspan=, a read lays bins from the 5′ end of each, so profiles come out strand-aware.to_bed12writes one BED12 line per transcript: thick part from the CDS (end, endwhen non-coding, as UCSC writes it), blocks from the exons, the line named byname. It is whatgtfToGenePred -genePredExtandgenePredToBedwrite, line for line.- A transcript whose records disagree on chromosome or strand, or with an exon
outside its own record's span, or a GFF3 part naming a
Parentno record has, raisesCorruptFile;strict=Falseleaves it out and counts it inskipped_models.
>>> gtf = gwseq_io.open_gtf("annotation.gtf.gz")
>>> genes = gtf.read_entries("chr1:60,001-60,010", model="gene")
>>> genes, list(genes.rows())
(<gwseq_io.Table 1 row, 10 columns, 1 group>, [Gene(Gamma chr1:5000-125100+, 1 transcript)])
>>> alpha = next(gtf.read_entries(model="gene", where={"gene_name": "Alpha"}).rows())
>>> transcript = alpha.transcripts[0]
>>> transcript, transcript.cds_start, transcript.cds_end
(Transcript(G1.T1 chr1:1000-2400+, 3 exons), 1100, 2200)
>>> transcript.exons
array([[1000, 1200],
[1500, 1700],
[2000, 2400]])
>>> transcript.utr3
array([[2200, 2400]])
>>> transcripts = gtf.read_entries(model="transcript", transcript_type="protein_coding")
>>> transcripts["transcript_id"], transcripts["exon_count"]
(['G1.T1', 'G1.T2', 'G2.T1', 'G5.T1', 'G5.T2'], array([3, 2, 2, 2, 2]))
>>> exons = transcripts.nested["exons"]
>>> exons.groups, exons["start"][:3]
(array([ 0, 3, 5, 7, 9, 11]), array([1000, 1500, 2000]))
>>> sites = gtf.tss(transcript_type="protein_coding")
>>> sites["center"], sites["strand"]
(array([1000, 1000, 3899, 599, 599]), ['+', '+', '-', '-', '-'])
BAM and CRAM
class BamReader:
def __init__(
self,
path: StrPath,
*,
index: StrPath | None = None,
parallel: int | None = None,
io_parallel: int | None = None,
block_size: int | None = None,
cache_bytes: int | None = None,
) -> None: ...
def read_entries(
self,
regions: RegionsLike | None = None,
*,
chromosomes: Sequence[str] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
unplaced: bool = False,
exclude_flags: Flags = "UNMAP,SECONDARY,SUPPLEMENTARY",
require_flags: Flags = 0,
min_mapq: int = 0,
drop_improper_pairs: bool = True,
filter: ReadFilter | None = None,
tags: bool | Sequence[str] = True,
columns: Sequence[str] | None = None,
progress: Progress = None,
) -> Table: ...
def iter_entries(
self,
regions: RegionsLike | None = None,
*,
chromosomes: Sequence[str] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
sort_regions: bool = False,
unplaced: bool = False,
window: int = 1000000,
exclude_flags: Flags = "UNMAP,SECONDARY,SUPPLEMENTARY",
require_flags: Flags = 0,
min_mapq: int = 0,
drop_improper_pairs: bool = True,
filter: ReadFilter | None = None,
tags: bool | Sequence[str] = True,
columns: Sequence[str] | None = None,
progress: Progress = None,
) -> RegionIterator[Table]: ...
class AlignmentReader:
header: list[dict[str, Any]]
is_indexed: bool
index_path: str | None
index_kind: Literal["bai", "csi", "crai"] | None
index_error: str | None
warnings: list[str]
indexis the index's path when it is not beside the file as<path>.bai,<path>.csior<path>.crai.index_pathandindex_kindsay which one is in use;index_errorsays why an index that was there could not be read, and isNonewhen one loaded or there is none. A local CRAM with no.craiis indexed from its container headers.- A read by region needs an index. A read with no regions, and a walk, go
over whole references and need none (a walk needs an index or a file sorted
by coordinate).
unplaced=Trueadds the alignments with no reference after the others, which the default filter drops as unmapped.iter_entrieswith regions gives a table per region;sort_regionsreads the regions in the file's order, and the iterator'sordersays which region of the request each step is. - The table's columns are
read_name,chr,start,end,flag,mapping_quality,cigar,sequence,qualities,next_chr,next_start,template_length, and the rest of what aSamEntryhas buttags.columnsnames the ones to decode, in that order; the others are never decoded.tags=Falseleaves the optional fields unread; a list of names gives those fields a column each,Nonewhere an alignment has none. Its rows are theSamEntrys themselves, which aBamWriterwrites back. headeris the header's lines, each{"type": "SQ", "fields": {...}}.warningsis what was found wrong at open without stopping it: no end-of-file marker, an index that could not be read where another could.
The read filter. Every read of alignments, the pileup and the conversion
to BAM take the same four keywords (or one ReadFilter holding them), and by
default drop unmapped, secondary and supplementary alignments and improper
pairs, and keep duplicates and QC failures:
exclude_flags: drop an alignment carrying any of these flags;require_flags: drop one not carrying all of these;min_mapq: drop one whose MAPQ is lower;drop_improper_pairs: drop a paired read (0x1) that is not a proper pair (0x2 unset), whose template length is 0, or whose mate is on another reference.
Flags are an int or samtools flag names joined by commas ("UNMAP,DUP").
>>> reader = gwseq_io.open("reads.bam")
>>> reader.index_kind, reader.chr_sizes
('bai', {'chr1': 200000, 'chr2': 100000, 'chr3': 5000})
>>> alignments = reader.read_entries("chr1:1,001-2,300")
>>> alignments["read_name"], alignments["start"]
(['plain_fwd', 'plain_rev', 'pair_fr', 'pair_fr'], array([1000, 1100, 2000, 2200]))
>>> list(alignments.rows())[0]
<gwseq_io.SamEntry chr1:1000-1050 plain_fwd>
>>> reader.read_entries("chr1:1,001-2,300", min_mapq=40)["read_name"]
['plain_fwd', 'pair_fr', 'pair_fr']
>>> reader.read_entries("chr1:6,001-9,100").row_count, reader.read_entries("chr1:6,001-9,100", drop_improper_pairs=False).row_count
(0, 8)
>>> reader.read_entries("chr1:1,001-2,300", columns=["start", "flag"], tags=["NM"])
<gwseq_io.Table 4 rows, 3 columns, 1 group>
>>> _["NM"]
[0, 1, None, None]
>>> walk = reader.iter_entries(["chr2:1-1000", "chr1:1-3000"], sort_regions=True)
>>> walk.order, [step.row_count for step in walk]
(array([1, 0]), [5, 0])
>>> reader.read_entries(exclude_flags=0, unplaced=True).row_count
46
class CramReader:
def __init__(
self,
path: StrPath,
*,
index: StrPath | None = None,
reference: StrPath | None = None,
parallel: int | None = None,
io_parallel: int | None = None,
block_size: int | None = None,
cache_bytes: int | None = None,
) -> None: ...
version: str
references: dict[str, dict[str, str | None]]
def read_entries(
self,
regions: RegionsLike | None = None,
*,
chromosomes: Sequence[str] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
unplaced: bool = False,
exclude_flags: Flags = "UNMAP,SECONDARY,SUPPLEMENTARY",
require_flags: Flags = 0,
min_mapq: int = 0,
drop_improper_pairs: bool = True,
filter: ReadFilter | None = None,
tags: bool | Sequence[str] = True,
sequence: bool = True,
columns: Sequence[str] | None = None,
progress: Progress = None,
) -> Table: ...
def iter_entries(
self,
regions: RegionsLike | None = None,
*,
chromosomes: Sequence[str] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
sort_regions: bool = False,
unplaced: bool = False,
window: int = 1000000,
exclude_flags: Flags = "UNMAP,SECONDARY,SUPPLEMENTARY",
require_flags: Flags = 0,
min_mapq: int = 0,
drop_improper_pairs: bool = True,
filter: ReadFilter | None = None,
tags: bool | Sequence[str] = True,
sequence: bool = True,
columns: Sequence[str] | None = None,
progress: Progress = None,
) -> RegionIterator[Table]: ...
A CramReader reads CRAM 2.1, 3.0 and 3.1 and has BamReader's methods, each
with sequence. A CRAM stores a mapped read as its differences from the
reference, so the bases come from a FASTA (with its .fai, or bgzipped with
its .gzi too), looked for per sequence in this order: the bases the file
embeds; reference; the FASTA the @SQ line's UR names; REF_CACHE and
REF_PATH by the M5 checksum (a URL in REF_PATH only when set
explicitly). references says, per sequence, which one was found:
{"source", "path", "error"}, where source is "embedded", "given",
"header", "cache", "not_required" or "missing". A sequence with no
reference, or a read with sequence=False, reads every field but the bases the
reference supplies, which read N (and MD/NM unless the file stored them).
The slice's MD5 is checked against the reference, and a mismatch raises
CorruptFile. version is "major.minor".
>>> reader = gwseq_io.open("reads.cram", reference="ref.fa")
>>> reader.version, reader.references["chr1"]["source"]
('3.1', 'given')
>>> entry = list(reader.read_entries("chr1:1-120").rows())[1]
>>> entry.cigar, entry.sequence[:12], entry.tag("MD")
('250M', 'ATACGATGATAC', '78A125C45')
>>> gwseq_io.open("reads.cram").references["chr1"]["source"]
'missing'
>>> gwseq_io.open("reads.cram").read_entries("chr1:1-120")["sequence"][1][:12]
'NNNNNNNNNNNN'
A SamEntry is one alignment, frozen; it pickles its record's bytes. A CRAM
record is rebuilt into what a BAM holds.
class SamEntry:
chr: str
start: int # 0-based
end: int # from the CIGAR; equal to start for an alignment covering no reference
read_name: str
flag: int
mapping_quality: int
cigar: str
sequence: str # "*" when the record holds none
qualities: str # phred+33, "*" when the record holds none
next_chr: str # "*" when the mate has no reference
next_start: int
template_length: int
bai_bin: int
reference_length: int
query_length: int
tags: dict[str, Any]
is_paired: bool
is_proper_pair: bool
is_mapped: bool
is_next_mapped: bool
is_reverse: bool
is_next_reverse: bool
is_first_in_pair: bool
is_last_in_pair: bool
is_secondary_or_supplementary: bool
is_failed_qc_or_duplicate: bool
def tag(self, name: str) -> Any: ...
def to_dict(self) -> dict[str, Any]: ...
tags is the optional fields by two-letter name, in the record's order, typed
as the file types them; it is the one attribute that can raise, on a record
whose optional fields are malformed. tag(name) reads one field, or None.
to_dict() has the same keys for every entry, "tags" included.
>>> entry = next(gwseq_io.open("reads.bam").read_entries("chr1:2,001-2,050").rows())
>>> entry.read_name, entry.flag, entry.is_proper_pair, entry.next_start, entry.template_length
('pair_fr', 99, True, 2200, 250)
HiC
class HiCReader:
def __init__(
self,
path: StrPath,
*,
parallel: int | None = None,
io_parallel: int | None = None,
block_size: int | None = None,
cache_bytes: int | None = None,
) -> None: ...
header: HiCHeader
footer: HiCFooter
normalizations: list[str]
units: list[str]
bin_sizes: list[int]
def resolve_regions(
self,
region1: Region2D,
region2: Region2D | None = None,
*,
bin_size: int | None = None,
bin_count: int | tuple[int, int] | None = None,
snap_to_grid: Snap = "auto",
full_bin: bool = False,
return_full: bool = False,
return_binned: bool = True,
) -> dict[str, Any]: ...
def read_values(
self,
region1: Region2D,
region2: Region2D | None = None,
*,
bin_size: int | None = None,
bin_count: int | tuple[int, int] | None = None,
snap_to_grid: Snap = "auto",
full_bin: bool = False,
def_value: float = 0.0,
triangle: bool = False,
min_distance: int | None = None,
max_distance: int | None = None,
normalization: str = "none",
matrix_type: Literal["observed", "oe", "expected"] = "observed",
save_to: StrPath | None = None,
progress: Progress = None,
) -> npt.NDArray[np.float32] | None: ...
def read_sparse_values(
self,
region1: Region2D,
region2: Region2D | None = None,
*,
bin_size: int | None = None,
bin_count: int | tuple[int, int] | None = None,
snap_to_grid: Snap = "auto",
full_bin: bool = False,
triangle: bool = False,
min_distance: int | None = None,
max_distance: int | None = None,
normalization: str = "none",
matrix_type: Literal["observed", "oe", "expected"] = "observed",
save_to: StrPath | None = None,
progress: Progress = None,
) -> dict[str, Any] | None: ...
A Juicer .hic file, versions 6 to 9, at its base-pair resolutions. A
Region2D is one region: a string, or a (chr, start, end) or
(chr, start, end, strand) tuple.
read_valuesgives the contact matrix ofregion1(the rows) againstregion2(the columns;region1when left out) as a float32 array.read_sparse_valuesgives it as a dict of"data"(float32),"row"and"col"(uint32) and"shape": whatscipy.sparse.coo_array((m["data"], (m["row"], m["col"])), shape=m["shape"])takes.- Each region is binned as a bigWig region is (see Binning), by
the same code:
bin_sizeis one ofbin_sizes, the finest when left out. A region hasfloor(span / bin_size)bins from its 5′ end, andfull_binkeeps a last partial one, valued over its part inside the chromosome.- A "−" region, given as
(chr, start, end, "-"), is laid from its end and reverses its axis: index 0 is its 5′ end, as in a bigWig read.quantifyignores the strand. snap_to_gridmoves a region's 5′ end to the nearest multiple ofbin_size:Truealways,Falsenever,"auto"(the default) when the region has at least 10 bins."expand"and"force_expand"grow it to the grid instead, as in a bigWig read; one region has nothing to share a count with, so"expand"never adds a bin. Rows and columns decide on their own.bin_countcuts the regions into exactly that many bins instead, along the longer region for one number (the other gets its share), along each for a(rows, cols)pair, read from the coarsest resolution giving at least that many. It is refused besidebin_size,full_binandsnap_to_grid=True,"expand"or"force_expand".- A bin that is one of the file's is its cell as stored. Any other (a region off the grid, or a bin count's bins) is the mean of the file's cells it covers, weighted by how much of each, NaN left out.
resolve_regionsgives the bins of a read without reading:"rows"and"cols", each a one-row table as a bigWig reader'sresolve_regionsgives for one region, with its options, and"bin_size", the resolution the cells come from.def_valueis the value of a cell no contact reached; a cell the chosen normalization cannot value is NaN.trianglereads the one side of the diagonal the file stores, without mirroring it;min_distanceandmax_distancekeep the contacts that far from the diagonal, in bp.normalizationis"none"or one ofnormalizations;matrix_typeis"observed","oe"(observed over expected) or"expected". The fragment resolutions and restriction sites a file may carry are inheader, and read nothing.- With
save_to, the matrix is written there as a.npzandNoneis returned: under the key"values", or for a sparse read asscipy.sparse.save_npzwrites one.
>>> reader = gwseq_io.open("contacts.hic")
>>> reader.bin_sizes, reader.normalizations
([100000, 25000, 10000, 1000], ['none', 'scale', 'vc', 'vc_sqrt'])
>>> reader.read_values("chr1:1-300,000", bin_size=100_000)
array([[367., 96., 50.],
[ 96., 414., 113.],
[ 50., 113., 428.]], dtype=float32)
>>> reader.read_values("chr1:1-500,000", "chr2:1-300,000", bin_size=100_000, normalization="vc").shape
(5, 3)
>>> reader.read_values("chr1", bin_count=4).shape
(4, 4)
>>> reader.read_values("chr1:50,001-250,000", bin_size=100_000)
array([[243.25, 168.25],
[168.25, 267. ]], dtype=float32)
>>> sparse = reader.read_sparse_values("chr1:1-200,000", bin_size=100_000)
>>> sparse["data"], sparse["row"], sparse["col"], sparse["shape"]
(array([367., 96., 96., 414.], dtype=float32), array([0, 0, 1, 1], dtype=uint32), array([0, 1, 0, 1], dtype=uint32), (2, 2))
Quantifying contacts
class HiCReader:
def quantify(
self,
regions1: RegionsLike,
regions2: RegionsLike | None = None,
*,
partners: Sequence[RegionsLike] | None = None,
span: int | None = None,
anchor: Anchor | None = None,
paired: bool = False,
bin_size: int | None = None,
reduce: Reduction = "mean",
def_value: float = 0.0,
min_distance: int | None = None,
max_distance: int | None = None,
normalization: str = "none",
matrix_type: Literal["observed", "oe", "expected"] = "observed",
progress: Progress = None,
) -> Any: ...
One value per pair of regions: reduce over the file's cells under the
rectangle region1 × region2, at bin_size (the finest when left out).
-
The regions are any
RegionsLike,spanandanchorapplied to every list; the strand is ignored. -
Every region of
regions1against every region ofregions2gives an array of shape(len(regions1), len(regions2)); withregions2left out, the regions against each other, a symmetric matrix. Withpaired=Truethe two lists are zipped, which need the same length, for one value per pair, as for the rows of a BEDPE. -
partnersgives each region ofregions1against its own partners:partners[i]is aRegionsLikeof any length, and the result is a list of one float32 array per region,[i][k]beingregions1[i]againstpartners[i][k]. Each value is what the other forms give that pair. It is given instead ofregions2andpaired. -
A rectangle is read exactly, never snapped. A file bin a region covers in part weighs the share of it the region covers, and a cell the product of its two sides' shares, so
meanis the valueread_valuesgives the same rectangle as one bin. -
The reductions are those of a bigWig
quantify, over the cells:reduceValue meanThe weighted mean of the cells. sumΣ share × value: on the grid, the plain sum of the cells, so observed counts sum to the contacts between the two regions. countThe cells a contact reached, weighted alike. It never counts a cell filled with def_value.sd,semThe weighted population standard deviation, and it over √ count,def_valuecells included.min,maxOver the cells the rectangle reaches. l1norm,l2normΣ share × |value|, and √(Σ share × value²). -
def_valueis the value of a cell no contact reached, and joins every reduction butcount; NaN leaves such cells out. A cell the normalization cannot value is left out of every reduction, and a pair with nothing left, or with a region that has no base on its chromosome, is NaN. -
On one chromosome the stored triangle is mirrored, the diagonal counted once.
min_distanceandmax_distancekeep the cells of such a pair that far from the diagonal, in bp; a pair on two chromosomes is quantified whole. -
normalizationandmatrix_typeare those ofread_values. -
Each block of the file is read once per call, whatever the number of pairs it feeds, and the result does not depend on the number of threads.
>>> reader = gwseq_io.open("contacts.hic")
>>> reader.quantify(["chr1:1-100,000", "chr1:200,001-300,000"], bin_size=100_000, reduce="sum")
array([[367., 50.],
[ 50., 428.]], dtype=float32)
>>> reader.quantify(["chr1:1-100,000"], ["chr1:200,001-300,000"], paired=True, bin_size=100_000)
array([50.], dtype=float32)
>>> reader.quantify("chr1:50,001-250,000", bin_size=100_000)
array([[211.6875]], dtype=float32)
>>> reader.quantify(
... ["chr1:1-100,000", "chr1:100,001-200,000"],
... partners=[["chr1:100,001-200,000", "chr1:200,001-300,000"], ["chr1:200,001-300,000"]],
... bin_size=100_000,
... reduce="sum",
... )
[array([96., 50.], dtype=float32), array([113.], dtype=float32)]
class HiCHeader:
version: int
genome_id: str
footer_position: int
nvi_position: int
nvi_length: int
bp_resolutions: list[int]
frag_resolutions: list[int]
sites: dict[str, list[int]]
attributes: dict[str, str]
class HiCFooter:
byte_count_v5: int
normalizations: list[str]
units: list[str]
master_index: dict[str, HiCIndexItem]
class HiCIndexItem:
position: int
size: int
Writers
Every writer is frozen, does not pickle, and derives from Writer. Nothing at
its path is the new file until close() returns: the file is written beside it
and moved into place, so a writer that fails, is abandoned, or is interrupted
leaves what the path held before. A with block closes the writer when it ends
normally and abandons it when it ends with an exception; to keep the file
anyway, call close() in your own except. A writer that is never closed is
abandoned when it is collected.
class Writer:
path: str
type: str # "bigwig", "bigbed" or "bam"
closed: bool # finished, or abandoned
state: Literal["open", "failed", "closed"]
chr_sizes: dict[str, int]
def abandon(self) -> None: ...
state is "failed" once a write or the close has failed and the file has
been discarded; a failed writer raises WriterFailed, a closed one Closed.
BigWigWriter
class BigWigWriter:
def __init__(
self,
path: StrPath,
*,
chr_sizes: Mapping[str, int] | None = None,
genome: str | None = None,
items_per_slot: int | None = None,
compression_level: int = 6,
layout: Literal["optimal", "as_given"] = "optimal",
zoom_share: float = 0.2,
parallel: int | None = None,
) -> None: ...
def write_value(self, chr: str, start: int, end: int, value: float) -> None: ...
def write_values(self, chr: str, start: int, span: int, values: npt.ArrayLike) -> None: ...
def close(self, *, progress: Any = None) -> None: ...
section_count: int
section_counts: dict[str, int]
skipped_count: int
clipped_count: int
item_count: int
chr_sizes, or an assembly's throughgenome, bound every coordinate, and a name written is matched against them as a read one is. Without them a chromosome ends where its last value does. Only chromosomes written go into the file.- Values must be pooled by chromosome and written in order without overlap.
write_valueswrites a run of valuesspanbases each, the first atstart, which is stored in a third of what the same values cost one at a time. A NaN or infinite value is not written (skipped_count): it leaves a gap. A value hanging over the end of a declared chromosome is cut back to it (clipped_count); one starting past it raises. items_per_slotis the values a block holds (1024, as UCSC's tools);compression_level0 to 9;layout="optimal"re-cuts the values into the sections that store them in the fewest bytes,"as_given"writes one item per value handed over.zoom_shareis the most of the data section the finest zoom level may take: smaller is a smaller file whose finest summary is coarser.close(progress=)builds the zoom levels and the index; its progress counts the bytes the zoom levels read back.
>>> writer = gwseq_io.BigWigWriter("draft.bw", chr_sizes={"chr1": 1000})
>>> writer.write_value("chr1", 0, 10, 1.0)
>>> writer.write_values("chr1", 10, 5, [1.0, 2.0, float("nan")])
>>> writer.item_count, writer.skipped_count, writer.state
(3, 1, 'open')
>>> writer.abandon()
>>> writer.state, writer.closed
('closed', True)
BigBedWriter
class BigBedWriter:
def __init__(
self,
path: StrPath,
*,
fields: Mapping[str, str] | None = None,
chr_sizes: Mapping[str, int] | None = None,
genome: str | None = None,
items_per_slot: int | None = None,
compression_level: int = 6,
zoom_share: float = 0.2,
extra_index: Sequence[str] = (),
parallel: int | None = None,
) -> None: ...
def write_entry(self, chr: str, start: int, end: int, fields: Mapping[str, Any] | None = None) -> None: ...
def close(self, *, progress: Any = None) -> None: ...
entry_count: int
fields: dict[str, str]
fieldsnames the columns and their autoSql types, the three coordinates first; by defaultchr,start,endandname."string","int","uint"and"float"are the usual ones; the rest of autoSql (lstring,char[n],double, arrays,enum,set) is accepted so a file read here can be written back as it was.write_entry'sfieldsare the columns past the coordinates; one left out is written empty, or as zero when numeric, and each value is checked against its column's type. Entries must be pooled by chromosome and in order of their start, and may overlap and nest.extra_indexnames columns to index by their text, forwhere.- The summary and zoom levels describe the depth of coverage the entries make.
>>> fields = {"chr": "string", "start": "uint", "end": "uint", "name": "string", "score": "float"}
>>> with gwseq_io.open("scored.bb", "w", fields=fields, chr_sizes={"chr1": 1000}) as writer:
... writer.write_entry("chr1", 10, 20, {"name": "a", "score": 1.5})
... writer.write_entry("chr1", 15, 30, {"name": "b"})
>>> gwseq_io.open("scored.bb").read_entries().to_dict()
{'chr': ['chr1', 'chr1'], 'start': array([10, 15]), 'end': array([20, 30]), 'name': ['a', 'b'], 'score': array([1.5, 0. ])}
BamWriter
class BamWriter:
def __init__(
self,
path: StrPath,
*,
header: Any,
index: bool = True,
compression_level: int = 6,
parallel: int | None = None,
) -> None: ...
def write(self, entry: Any) -> None: ...
def write_table(self, table: Table) -> None: ...
def close(self) -> None: ...
record_count: int
index_path: str | None
headeris SAM header text, a reader'sheader(the list of dicts), or aBamReaderorCramReaderitself, whose header is copied verbatim.writetakes aSamEntryas a reader gave it, with every field its record holds: an entry read withtags=Falseis written without its optional fields, and one read from a CRAM withsequence=Falsewithout its bases. An entry of a file whose references are numbered otherwise is placed on the header's reference of the same name; a name the header lacks raisesInvalidRequest, with nothing written.write_tablewrites every alignment of a table a reader gave, in its order.- With
index=True(the default) the entries must come sorted by coordinate: one out of order raisesInvalidRequest, naming both reads, and the writer stays open.close()writes<path>.bai, or<path>.csiwhen a reference is longer than 512 Mb (index_path). - Existing indexes are removed.
close()deletes an index already beside the path (<path>.bai,<path>.csi, and<stem>.baifor a path ending.bam), since it indexes the file being replaced, whether or not a new one is written. A failed or abandoned writer deletes nothing.
>>> reader = gwseq_io.open("reads.bam")
>>> with gwseq_io.open("subset.bam", "w", header=reader) as writer:
... writer.write_table(reader.read_entries("chr1:1,001-2,300"))
>>> writer.record_count, writer.index_path
(4, 'subset.bam.bai')
>>> gwseq_io.open("subset.bam").read_entries()["read_name"]
['plain_fwd', 'plain_rev', 'pair_fr', 'pair_fr']
Converters
def convert_to_bigwig(
input: StrPath,
output: StrPath,
*,
bin_size: int | None = None,
chr_sizes: Mapping[str, int] | None = None,
genome: str | None = None,
items_per_slot: int | None = None,
compression_level: int = 6,
zoom_share: float = 0.2,
sort_bytes: int | None = None,
parallel: int | None = None,
progress: Progress = None,
) -> dict[str, Any]: ...
def convert_to_bigbed(
input: StrPath,
output: StrPath,
*,
fields: Mapping[str, str] | None = None,
chr_sizes: Mapping[str, int] | None = None,
genome: str | None = None,
items_per_slot: int | None = None,
compression_level: int = 6,
zoom_share: float = 0.2,
extra_index: Sequence[str] = (),
sort_bytes: int | None = None,
parallel: int | None = None,
progress: Progress = None,
) -> dict[str, Any]: ...
def convert_to_bam(
input: StrPath,
output: StrPath,
*,
reference: StrPath | None = None,
regions: RegionsLike | None = None,
exclude_flags: Flags = "UNMAP,SECONDARY,SUPPLEMENTARY",
require_flags: Flags = 0,
min_mapq: int = 0,
drop_improper_pairs: bool = True,
filter: ReadFilter | None = None,
index: bool = True,
compression_level: int = 6,
parallel: int | None = None,
progress: Progress = None,
) -> dict[str, Any]: ...
Each converter streams its input, which may be gzipped where it is text, and tells its format from its content, not its name. The output holds the finished file or what it held before, never part of one. Each returns a dict of what was read and written. The options they share with a writer are that writer's.
convert_to_bigwig: bedGraph or WIG (the first line that is not blank, a comment,trackorbrowserdecides).bin_sizeaverages the values into bins of that width, each weighted by the bases it covers.convert_to_bigbed: BED, split on tabs, every record with the first's number of columns.fieldsnames the columns; by default the standard BED names and types, and naming only some standard columns changes their types and keeps the rest (fields={"score": "float"}). Standard columns are checked as UCSC checks them.extra_indexisbedToBigBed -extraIndex.- Input out of order. A bedGraph or BED sorted as
sort -k1,1 -k2,2nsorts it (pooled by chromosome, by start within one) streams. One that is not is sorted in memory, by chromosome name and start, records with equal keys in their input order, and converts to the file its sorted copy would:"sorted"in the result says which happened. The sort holds the text and 24 bytes per line; pastsort_bytes(1 GiB whenNone) the input is refused, naming it. A WIG is never sorted: one out of order is refused. convert_to_bam: SAM (gzipped or not), BAM or CRAM, withreferencefor a CRAM asCramReadertakes it; a CRAM holding a sequence no reference is found for is refused before anything is written.regionskeeps the records overlapping any of them, each once, in the input's order; a BAM or CRAM input needs its index for it. The read filter applies, with the defaults above: passexclude_flags=0, drop_improper_pairs=Falseto keep every record.indexwrites<output>.bai(or.csi) asBamWriterdoes, and the input must then be sorted; as withBamWriter, an index already besideoutputis removed. Progress is counted in bases of the input's references, or bytes of a SAM.
>>> with open("track.bedgraph", "w") as text:
... _ = text.write("chr2\t0\t50\t2\nchr1\t100\t300\t1.5\nchr1\t0\t100\t0.5\n")
>>> gwseq_io.convert_to_bigwig("track.bedgraph", "track.bw", chr_sizes={"chr1": 1000, "chr2": 500})
{'format': 'bedgraph', 'line_count': 3, 'item_count': 3, 'skipped_count': 0, 'clipped_count': 0, 'chr_sizes': {'chr1': 1000, 'chr2': 500}, 'sorted': True}
>>> with open("peaks.bed", "w") as text:
... _ = text.write("chr1\t100\t200\tpeak1\t500\t+\nchr1\t150\t400\tpeak2\t900\t-\n")
>>> gwseq_io.convert_to_bigbed("peaks.bed", "peaks.bb", genome="mm10", extra_index=["name"])["item_count"]
2
>>> gwseq_io.open("peaks.bb").read_entries(where={"name": "peak2"})["start"]
array([150])
>>> gwseq_io.convert_to_bam("reads.cram", "from_cram.bam", reference="ref.fa", regions="chr1:1-2,000")
{'format': 'cram', 'record_count': 90, 'index': 'from_cram.bam.bai', 'index_kind': 'bai', 'chr_sizes': {'chr1': 34000, 'chr2': 8000, 'chr3': 3000}}
>>> gwseq_io.convert_to_bam("reads.bam", "every_record.bam", exclude_flags=0, drop_improper_pairs=False)["record_count"]
60
Pileup
def pileup_reads(
input: StrPath,
output: StrPath,
*,
bin_size: int = 10,
exclude_flags: Flags = "UNMAP,SECONDARY,SUPPLEMENTARY",
require_flags: Flags = 0,
min_mapq: int = 0,
drop_improper_pairs: bool = True,
filter: ReadFilter | None = None,
strand: Literal["+", "-"] | None = None,
max_fragment: int = 2000,
shift_reads: int = 0,
transform_reads: Literal["extend", "force_extend", "splice"] = "extend",
extend_size: int = 150,
count_fragments_once: bool = False,
def_value: float | None = 0.0,
normalize: Literal["count", "cpm", "bpm", "enrichment"] = "count",
scale: float = 1.0,
index: StrPath | None = None,
reference: StrPath | None = None,
hold_bytes: int | None = None,
items_per_slot: int | None = None,
compression_level: int = 6,
zoom_share: float = 0.2,
parallel: int | None = None,
progress: Progress = None,
) -> dict[str, Any]: ...
Piles a BAM or CRAM up into a bigWig of bin_size bins, each the number of
intervals that touch it. The input must be sorted by coordinate (an unsorted
one is refused, naming samtools sort); an index is used when there is one and
is not needed. index names it when it is not beside the input, and
reference is a CRAM's FASTA.
A read becomes an interval in three steps:
- The filter (the read filter, same defaults): unmapped, secondary and supplementary alignments and improper pairs are dropped; duplicates and QC failures are kept.
- The strand, with
strand="+"or"-": a read is kept when its fragment's strand is that one. A fragment's strand is the read's own, flipped for the second read of a pair (0x80 set, 0x40 not), so a pair counts on the strand of its first read and a single read on its own. dUTP (fr-firststrand) libraries sequence the complement, so there the labels are swapped:"+"is the track of the genes on the minus strand. - The transform,
transform_reads:"extend": a read of a proper pair with its mate on the same reference, a template length and exactly one of 0x40/0x80 is piled up as its fragment, from the leftmost mate's start over|TLEN|bases; a fragment longer thanmax_fragment, or starting more thanmax_fragmentbefore its read, is dropped (its read still counted). Every other read is extended from its 5′ end toextend_sizebases, or its own aligned length at 0. Withcount_fragments_once, a fragment is piled up once, by the mate with the lower position (READ1 on a tie), rather than once per mate."force_extend": every read is movedshift_readsbases along its own direction and extended from its 5′ end toextend_size, pairs or not.shift_readsis refused with"extend"."splice": the segments of each read between theNoperations of its CIGAR (its introns), each movedshift_readsbases along the read.
normalize is "count" (none), "cpm" (per million intervals), "bpm" (each
bin as its share of the total, per million) or "enrichment" (over the
geometric mean of the covered bins); scale multiplies after it. Every
normalization but "count" is a property of the whole file: the binned result
is held in memory, or the input read twice when it would take more than
hold_bytes (1 GiB when None; passes in the result says which).
def_value is written for a bin with no coverage, whatever the normalization;
None leaves those bins out of the file. Progress is counted in bytes of the
input. The result's read_count is what the filter and the strand kept, and
interval_count what was piled up.
>>> summary = gwseq_io.pileup_reads("reads.bam", "coverage.bw")
>>> summary["read_count"], summary["interval_count"], summary["passes"]
(43, 40, 1)
>>> gwseq_io.open("coverage.bw").read_values("chr1:2,001-2,300", bin_size=50)
array([[2., 2., 2., 2., 2., 0.]], dtype=float32)
>>> gwseq_io.pileup_reads("reads.bam", "coverage.cpm.bw", normalize="cpm")["scale"]
25000.0
>>> gwseq_io.pileup_reads("reads.bam", "coverage.once.bw", count_fragments_once=True)["interval_count"]
33
>>> gwseq_io.pileup_reads("reads.cram", "cram.bw", reference="ref.fa", def_value=None)["read_count"]
682
Recipes: ChIP normalize="cpm"; ATAC shift_reads=-36, transform_reads="force_extend", extend_size=80, normalize="cpm"; RNA-seq
bin_size=1, transform_reads="splice", normalize="cpm".
Walks, regions and headers
RegionIterator is what every iter_* method returns: one step per region or
window. It is re-iterable (each for loop reads again), and an interrupted pass
does not spoil the next. len() is the number of steps, regions[n] the region
of step n, and items() pairs each step with its region. order is, for a
walk over regions, the index in the request of each step's region, and None
for a walk over whole chromosomes. A walk with unplaced=True ends in batches
with no region: len() raises TypeError and items() pairs them with
None. Regions is a sequence of Region with to_dict(), the columns
pandas.DataFrame takes.
class Region:
chr: str
start: int
end: int
strand: str
class RegionIterator(Generic[T]):
regions: Regions
order: npt.NDArray[np.int64] | None
def items(self) -> Iterator[tuple[Region | None, T]]: ...
class Regions:
def index(self, value: object) -> int: ...
def count(self, value: object) -> int: ...
def to_dict(self) -> dict[str, Any]: ...
class BbiHeader:
magic: int
version: int
zoom_levels: int
chr_tree_offset: int
full_data_offset: int
full_index_offset: int
field_count: int
defined_field_count: int
auto_sql_offset: int
total_summary_offset: int
uncompress_buffer_size: int
class ZoomHeader:
reduction_level: int
data_offset: int
index_offset: int
class TotalSummary:
bases_covered: int
min_value: float
max_value: float
sum_data: float
sum_squared: float
>>> walk = gwseq_io.open("features.bb").iter_entries(chromosomes=["chrX"], window=30_000)
>>> len(walk), [step.row_count for step in walk]
(3, [16, 15, 9])
>>> walk.regions.to_dict()["start"]
array([ 0, 30000, 60000])
>>> summary = gwseq_io.open("signal.bw").total_summary
>>> summary.bases_covered, summary.min_value, summary.max_value, summary.sum_data
(400, 0.5, 2.0, 500.0)
The command line
gwseq-io is installed with the package, and is the same program as
python -m gwseq_io. Data goes to standard output as tab-separated text;
summaries, progress and errors go to standard error. An error is one line,
gwseq-io: <message>.
| Command | What |
|---|---|
info |
A file's format, headers, chromosomes and summary; for a GTF or GFF3 its index and sequences |
read |
bigWig or bigBed values over regions: one row per region, one column per bin; every chromosome, one at a time, with no region |
quantify |
One value per region |
profile |
One value per bin, reduced across regions |
entries |
bigBed entries (as BED), BAM/CRAM alignments (name chr start end flag mapq cigar), or GTF/GFF3 records (the nine columns, start 0-based, or 1-based with --gtf; --feature keeps some types) or gene models (--model transcript or gene, a table with a header line); the whole file when no region is given. --where FIELD=V1,V2, repeatable, keeps what holds the values, as where= does |
genes |
The genes of a GTF or GFF3 over regions or the whole file: a gene table, a transcript table with --transcripts, or BED12 with --bed12 |
index-gtf |
A GTF or GFF3 sorted, compressed with BGZF and indexed by tabix (index_gtf); the output is <stem>.sorted.gtf.gz beside the input by default |
hic |
A HiC contact matrix over one or two regions |
export |
A bigWig or bigBed to bedGraph, WIG or BED |
convert |
bedGraph, WIG or BED to bigWig or bigBed; SAM, BAM or CRAM to BAM, by the output's extension. An unsorted bedGraph or BED is sorted in memory, within --sort-bytes |
pileup |
A BAM or CRAM to a bigWig (pileup-reads and pileup_reads are the same command) |
chr-sizes |
An assembly's chromosome sizes |
- Global options:
-p/--parallel N, the most threads one command uses;-q/--quiet, nothing but errors;--progress, a progress bar on standard error, drawn by default when it is a terminal.gwseq-io <command> --helplists a command's options. - Regions are
-r chr:start-end, 1-based and inclusive, or a chromosome's name, repeatable.--zoomisfull,autoor a level's index. Flags are samtools names or numbers. - The flags of
gwseq-io pileuparepileup_reads' keywords, hyphenated (--exclude-flags,--count-fragments-once,-d nonefordef_value=None), plus--range-bytes, the compressed bytes read as one range.-b,-s,-t,-e,-d,-nand-Tare the short forms of--bin-size,--shift-reads,--transform-reads,--extend-size,--def-value,--normalizeand--reference. - The filter of
entries,convertandpileupis the read filter, with its defaults;--keep-improper-pairsis--drop-improper-pairs false, andconvert --no-filterkeeps every record. - Exit codes: 0 success, 1 an error, 2 a usage error, 130 interrupted (Ctrl-C, which leaves no partial output). A closed pipe on standard output is a success.
$ gwseq-io info signal.bw
path signal.bw
type bigwig
version 4
chromosomes 1
genome size 10000 bp
data 45 bytes
zoom levels
summary 400 bases covered, min 0.5, max 2, mean 1.25
chr1 10000
$ gwseq-io entries reads.bam -r chr1:1,001-2,300
plain_fwd chr1 1000 1050 0 60 50M
plain_rev chr1 1100 1150 16 30 50M
pair_fr chr1 2000 2050 99 60 50M
pair_fr chr1 2200 2250 147 60 50M
$ gwseq-io genes annotation.gtf.gz -r chr2:1-1,000 --transcripts
chr start end strand transcript_id transcript_name transcript_type gene_id gene_name cds_start cds_end exons
chr2 99 600 - G5.T1 Eps-201 protein_coding G5 Eps 149 550 2
chr2 99 600 - G5.T2 Eps-202 protein_coding G5 Eps -1 -1 2
$ gwseq-io entries features.bb --where name=LOC472203_1
chr1 1336 3623 LOC472203_1 47 - 205.43 n/a
$ gwseq-io hic contacts.hic chr1:1-300,000 --bin-size 100000
367 96 50
96 414 113
50 113 428
$ gwseq-io pileup reads.bam coverage.cpm.bw -n cpm
bin size 10
reads 43
intervals 40
normalization cpm x25000 (1 pass)
covered bins 367
items written 59
sections 9
chromosomes 3
$ gwseq-io convert reads.cram from_cram.bam -T ref.fa -r chr1:1-2,000
format cram
records written 90
references 3
index from_cram.bam.bai
$ gwseq-io export signal.bw signal.bedgraph --bin-size 200
$ gwseq-io chr-sizes sacCer3
chrI 230218
chrII 813184
...
$ gwseq-io read missing.bw
gwseq-io: error reading missing.bw: No such file or directory (os error 2)
Recipes:
$ gwseq-io pileup reads.bam chip.cpm.bw -n cpm -q
$ gwseq-io pileup reads.bam atac.cpm.bw -s -36 -t force_extend -e 80 -n cpm -q
$ gwseq-io pileup reads.bam rna.cpm.bw -b 1 -t splice -n cpm -q
cargo install --path crates/gwseq-io-cli builds gwseq-io alone, as a Rust
binary.
Development
The workspace holds three crates: gwseq-io, the library, with no Python in
it (its own README covers the Rust API); gwseq-io-cli, the command line, a
library and a binary; and gwseq-io-py, the extension, which links both and
holds the Python package (crates/gwseq-io-py/python/gwseq_io): the public
classes and functions in its .py modules, over the extension's primitives,
which take their keywords as one dict and are typed by the private stub
_gwseq_io.pyi. docs/architecture.md is the
design. docs/ holds the format specifications implemented.
Building. pip install -e ".[dev]" in a venv builds and installs the
package with what building, checking and publishing need; maturin develop --release rebuilds only what changed. cargo build --workspace does not link
the extension (maturin does): use cargo check -p gwseq-io-py for its type
errors. The Rust floor is rust-version in Cargo.toml, 1.85.
The check list. No CI runs anywhere; tests run on this machine through one script, standard library only:
python3 tools/check.py --quick:cargo fmt --check, clippy (all features, and the core with none), the debug tests,cargo check -p gwseq-io-py.python3 tools/check.py: all of that, then the optimised tests, the ignored tests, the codec oracle,cargo docwith warnings as errors, the 1.85 toolchain and the Windows and macOS targets when installed, and the Python step: an optimised wheel built withmaturin buildinto the cargo target directory, installed into a throwaway venv there with thetestextra (pytest, mypy, pandas, scipy), and the pytest suite run against it.--python PATHalso runs the suite under another interpreter; the floor check is--python python3.11, which gets the oldest numpy with wheels for it (1.23.2). The optimised steps build with therelease-checkprofile: release optimisation at sixteen codegen units, incremental, so an edit rebuilds them in seconds; what ships is built withrelease.python3 tools/dist.py --check:tools/check.py, then the sdist built, compiled in a clean venv and tested there with the pytest suite.--check-versionschecks that the workspace version, thegwseq-iopin andpyproject.tomlagree.tools/dist.pyalso uploads (--upload,--test-upload,--crate-upload).
Tests.
cargo test --workspace: unit tests in every module, the integration suite (crates/gwseq-io/tests/suite/: round trips, requests against independent oracles, a fuzz pass over every parser, properties), and the command line's tests. No network, samtools, Java or C compiler is needed.crates/gwseq-io-py/tests/: the pytest suite, run against an installed build, never the source tree.conftest.pycopies the committed fixtures into each test's own directory.test_readme.pyruns this file: each```pyconblock through doctest, each```consolecommand through the installedgwseq-io, each```pyiblock against the.pysources and the stub; any other fence but```textfails. The stub is held to the extension bymypy.stubtestandtests/typing/calls.pybymypy --strict.- Tests that need the large files of
local/test_data/(not committed) skip without them; so do the benchmarks (cargo bench -p gwseq-io). The few that take half a minute unoptimised run in optimised builds only.
Fixtures are committed under crates/gwseq-io/tests/data/, excluded from
both packages, and written by deterministic generators run by hand, each with a
--check that regenerates and compares:
tools/fixtures/make_alignment_fixtures.sh: the BAMs, indexes and samtools views, from hand-written SAM files (samtools 1.23);tools/fixtures/cram_fixtures.py: CRAM 2.1, 3.0 and 3.1 files in every samtools profile, and what samtools reads back from them;tools/fixtures/bbi/make_extra_index.sh: bigBeds written by UCSC'sbedToBigBed(pinned, sha256-checked download);tools/fixtures/hic/make.sh:.hicfiles written by juicer_tools 1.22 and 2.20, and what straw reads out of them.tools/fixtures/gff/make.sh: GTF and GFF3 files compressed and indexed by htslib 1.21'sbgzipandtabix(built from the pinned release), whattabixanswers for a list of regions, and the BED12 UCSC'sgtfToGenePredandgenePredToBedmake of the GTF; it also hastabixread whatgwseq-io index-gtfwrites.
The examples of this README use geometry.bam, v31_normal_default.cram with
ref.fa, extra_index.bb, bed12_ucsc.bb, v9.hic, and small.gtf.gz with
its .tbi and unsorted.gtf from there, copied under the names above
(annotation.gtf.gz), and write signal.bw as the quick start does.
Oracles. oracle/ is a crate outside the workspace, the one place that
compiles C: it vendors htscodecs and checks the CRAM codecs against it
(cd oracle && cargo test, which tools/check.py runs when a C compiler is
installed). tools/oracle/pileup_oracle.py computes a pileup from
samtools view text, sharing no code with the library. tools/oracle/gene_models.py
assembles gene models from a GTF or GFF3, standard library only, for the
pytest suite to hold GtfReader's to.
Metadata
Release files for gwseq-io 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gwseq_io-0.7.0.tar.gz | 1.4 MB | Without provenance | Details for gwseq_io-0.7.0.tar.gz |
Release files / gwseq_io-0.7.0.tar.gz
| Download URL | gwseq_io-0.7.0.tar.gz |
|---|---|
| Size | 1.4 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d8f01975168f4f760cb97cca88fe80977e0ad230a3364b904f266b33bbb8ef8
|
|
BLAKE2b-256 checksum How to use checksums |
bd35e7cb65d9b1625027520e77f39d6cc516b06ed2216d63b245e6d9ec617ef4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Security for gwseq-io 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.