pysam

Read and query SAM/BAM/CRAM, VCF/BCF, and FASTA/FASTQ genomic files via Python APIs for NGS analysis.

783|65|Updated Feb 27, 2026
One-click install
npx skills add https://github.com/LeonChaoX/qinyan-academic-skills --skill pysam-leonchaox
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pysam
Source: https://github.com/LeonChaoX/qinyan-academic-skills/tree/main/skills/05-%E7%94%9F%E7%89%A9%E4%BF%A1%E6%81%AF%E4%B8%8E%E5%9F%BA%E5%9B%A0%E7%BB%84%E5%AD%A6/pysam
Command: npx skills add https://github.com/LeonChaoX/qinyan-academic-skills --skill pysam-leonchaox

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill solves the problem of manually struggling to read, query, and analyze large sequencing alignment and variant files by giving you a fast Python toolkit for core NGS file operations.

Core Features & Use Cases

  • Alignment file operations (SAM/BAM/CRAM): Fetch reads from genomic regions, filter by mapping qualities/flags, write filtered outputs, and compute coverage via pileup.
  • Variant file operations (VCF/BCF): Iterate and query variants in regions, inspect INFO/FORMAT/genotypes, and write updated VCF/BCF records.
  • Sequence extraction (FASTA/FASTQ): Random-access reference sequences and iterate FASTQ reads for sequence+quality based workflows.
  • Integrated NGS workflows: Combine BAM+VCF+FASTA to validate variants against read support, annotate with depth, and extract variant contexts for downstream analysis.

Quick Start

Use pysam to read a BAM/CRAM file and fetch reads overlapping a target region by running Python code that opens the alignment file and loops over reads returned by fetch() for the specified coordinates.

Frequently Asked Questions about pysam

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

FAQPage Schema
How do I read and extract reads from a BAM file in a specific genomic region using Python?

You can compute coverage from BAM files in Python by opening the alignment file and using the pileup-based method to iterate over columns and calculate per-base read depth.

Can I query variants and extract genotype information from a VCF file with Python?

You can extract reference sequences from FASTA files in Python by opening the file and using random-access queries to fetch specific genomic intervals efficiently.

What is the correct coordinate convention for fetching reads and processing genomic intervals?

You must use 0-based half-open coordinate conventions for Python objects and 1-based region strings for samtools-style fetch calls to query genomic intervals correctly.

How do I combine BAM, VCF, and FASTA files to validate variants against read support?

You can integrate BAM, VCF, and FASTA files in Python to validate variants against read support, annotate them with depth, and extract variant contexts for downstream analysis.