pysam

Parse and query SAM/BAM/CRAM, VCF/BCF, and FASTA/FASTQ files via htslib-backed Python interfaces.

Updated May 24, 2026
One-click install
npx skills add https://github.com/Estrella-231/Mathematical_modeling_tongmeng --skill pysam-estrella-231
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pysam
Source: https://github.com/Estrella-231/Mathematical_modeling_tongmeng/tree/main/.agents/skills/pysam
Command: npx skills add https://github.com/Estrella-231/Mathematical_modeling_tongmeng --skill pysam-estrella-231

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Pysam removes the friction of manually parsing and querying large NGS files by giving a unified Python interface for alignments, variants, and sequences.

Core Features & Use Cases

  • Read/write SAM/BAM/CRAM alignments: Fetch reads by genomic region, filter by mapping attributes, compute coverage, and run pileup-style analyses for read depth and per-base statistics.
  • Read/write VCF/BCF variants: Iterate variants, query by region, inspect INFO/FORMAT fields, and extract genotypes for downstream analysis.
  • Random-access FASTA/FASTQ sequence data: Extract reference sequences by coordinate (FASTA) and stream FASTQ records for QC and read filtering.
  • Integrated pipelines: Combine BAM + VCF + FASTA workflows to validate variants, annotate with coverage (DP), extract sequence contexts, and support common QC/reporting steps.

Quick Start

Use the pysam skill to compute per-base coverage from an indexed BAM file over a target interval like chromosome 1 from 1,000 to 2,000.

Frequently Asked Questions about pysam

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I compute per-base coverage from a BAM file over a specific genomic region?

To compute per-base coverage from a BAM file, fetch reads by genomic region and run pileup-style analyses for read depth. This requires an indexed BAM file to enable random-access queries over target intervals.

Can I query VCF variants by genomic coordinate and extract genotype information?

Yes, you can query VCF variants by genomic coordinate to iterate variants, inspect INFO/FORMAT fields, and extract genotypes. The VariantFile interface supports subsetting VCF/BCF files for downstream analysis.

How do I extract reference sequences by coordinate from a FASTA file?

Extract reference sequences by coordinate from a FASTA file using random-access support. The FastaFile interface enables targeted sequence extraction, while FASTQ records can be streamed for quality control and read filtering.

Why do my region queries return incorrect coordinates when parsing SAM/BAM files?

Incorrect region query coordinates often result from mismatched coordinate handling between 0-based half-open intervals and 1-based samtools region strings. Correct coordinate handling is required for accurate SAM/BAM/CRAM alignment parsing.

What is the best way to validate variants and annotate coverage in bioinformatics pipelines?

The best way to validate variants and annotate coverage in bioinformatics pipelines is to combine BAM, VCF, and FASTA workflows. This integrated approach validates variants, annotates with depth (DP), and extracts sequence contexts.

Does pysam require indexed files for random-access queries on CRAM alignments?

Yes, pysam requires indexed files to support random-access queries on CRAM alignments and other NGS formats. Indexed random-access support is necessary for fetching reads by genomic region and running pileup analyses.