pysam-genomic-files
Read/write SAM/BAM/CRAM, VCF/BCF, FASTA/FASTQ. Region queries, pileup, variant filtering, read groups. Python htslib wrapper exposing samtools/bcftools CLI. Use STAR/BWA for alignment; GATK/DeepVariant for variant calling.
npx skills add BioTender-max/awesome-bio-agent-skills --skill pysam-genomic-files --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
What it does
Provides a Pythonic interface to htslib for reading, manipulating, and writing genomic data files. It handles SAM/BAM/CRAM alignments, VCF/BCF variants, and FASTA/FASTQ sequences with efficient region-based random access. Also exposes samtools and bcftools as callable Python functions.
How it works
- Supports reading/writing alignment files (BAM/SAM/CRAM) and performing region queries (bam.fetch) and counts (bam.count).
- Enables per-base coverage and pileup analyses (bam.pileup, bam.count_coverage).
- Handles VCF/BCF variant files (VariantFile), iterating records, accessing record fields, and filtering, plus writing filtered VCFs and indexing with tabix_index.
- Accesses sequence data in FASTA/FASTQ via FastaFile and FastxFile, with examples for random access and iteration.
- Manages read groups and sample information from BAM headers (RG entries) and provides functions to extract reads by read group and count reads per sample.
- Bridges to samtools/bcftools CLI via Python calls (pysam.sort, pysam.index, pysam.view, pysam.bcftools.view) and includes error handling for Samtools errors.
- Provides coordinate system guidance and index prerequisites, including how to create and use indices (.bai, .tbi, .fai).
When to use it
- For reading and querying BAM/CRAM files with region extraction and read filtering.
- For analyzing VCF/BCF variant files, including access to genotype data and INFO fields.
- For extracting reference sequences from indexed FASTA files and for per-base or region-based coverage analyses.
- For building pipelines that combine alignment, variant, and sequence data, or for QC tasks on NGS data (mapping quality, flag filtering, coverage).
- When alignment from FASTQ is needed, use STAR, BWA, or minimap2; for variant calling from BAM, use GATK or DeepVariant.
What it can touch
- Files: BAM/SAM/CRAM, VCF/BCF, FASTA/FASTQ via pysam APIs.
- Indexes: .bai, .tbi, .fai generated via pysam.index, pysam.tabix_index, or pysam.faidx.
- External tools: CLI wrappers for samtools and bcftools through Python API (pysam.sort, pysam.index, pysam.view, pysam.bcftools.view).
Caveats
- Requires the htslib library (bundled with pysam on many platforms); some Linux environments may need libhts-dev.
- Coordinate conventions differ (0-based 0-based vs 1-based POS in VCF/BED); users must ensure proper alignment with the Key Concepts guidance.
- Some operations on multi-sample BAMs can be slow if filtering without indexing; using region-based fetch or appropriate indexing is recommended.
- The license is MIT; ensure compatibility with downstream dependencies in your project.
# Pysam — Genomic File Toolkit ## Overview Pysam provides a Pythonic interface to htslib for reading, manipulating, and writing genomic data files. It handles SAM/BAM/CRAM alignments, VCF/BCF variants, and FASTA/FASTQ sequences with efficient region-based random access. Also exposes samtools and bcftools as callable Python functions. ## When to Use - Reading and querying BAM/CRAM alignment files (region extraction, read filtering) - Analyzing VCF/BCF variant files (genotype access, variant filtering, annotation) - Extracting reference sequences from indexed FASTA files - Calculating per-base coverage and pileup statistics - Building custom bioinformatics pipelines that combine alignment + variant + sequence data - Quality control of NGS data (mapping quality, flag filtering, coverage) - For **alignment from FASTQ** (read mapping), use STAR, BWA, or minimap2 instead - For **variant calling from BAM**, use GATK or DeepVariant instead ## Prerequisites ```bash pip install pysam ``` **Note**: Requires htslib C library (bundled with pip install on most platforms). On some Linux systems, may need `libhts-dev` or equivalent. Index files (`.bai`, `.tbi`, `.fai`) required for random access —
- Overview
- When to Use
- Prerequisites
- Quick Start
- Core API
- 1. Alignment Files (SAM/BAM/CRAM)
- 2. Coverage and Pileup Analysis
- 3. Variant Files (VCF/BCF)
- 4. Sequence Files (FASTA/FASTQ)
- 5. Read Groups and Sample Information
- 6. Samtools/Bcftools CLI Access
- Key Concepts
- Coordinate Systems
- Index File Requirements
pip install pysam These are equivalent to the Python calls above: samtools sort -o sorted.bam input.bam samtools index sorted.bam samtools view -b -o region.bam sorted.bam chr1:1000-2000 bcftools view -O z -o output.vcf.gz input.vcf
What does the pysam-genomic-files skill do?
Read/write SAM/BAM/CRAM, VCF/BCF, FASTA/FASTQ. Region queries, pileup, variant filtering, read groups. Python htslib wrapper exposing samtools/bcftools CLI. Use STAR/BWA for alignment; GATK/DeepVariant for variant calling.
How do I install it?
Run `npx skills add BioTender-max/awesome-bio-agent-skills --skill pysam-genomic-files --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From BioTender-max/awesome-bio-agent-skills, a repository with 144 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
