$npx -y skills add LeonChaoX/qinyan-academic-skills --skill pysamGenomic file toolkit. Read/write SAM/BAM/CRAM alignments, VCF/BCF variants, FASTA/FASTQ sequences, extract regions, calculate coverage, for NGS data processing pipelines.
| 1 | # Pysam |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Pysam is a Python module for reading, manipulating, and writing genomic datasets. Read/write SAM/BAM/CRAM alignment files, VCF/BCF variant files, and FASTA/FASTQ sequences with a Pythonic interface to htslib. Query tabix-indexed files, perform pileup analysis for coverage, and execute samtools/bcftools commands. |
| 6 | |
| 7 | ## When to Use This Skill |
| 8 | |
| 9 | This skill should be used when: |
| 10 | - Working with sequencing alignment files (BAM/CRAM) |
| 11 | - Analyzing genetic variants (VCF/BCF) |
| 12 | - Extracting reference sequences or gene regions |
| 13 | - Processing raw sequencing data (FASTQ) |
| 14 | - Calculating coverage or read depth |
| 15 | - Implementing bioinformatics analysis pipelines |
| 16 | - Quality control of sequencing data |
| 17 | - Variant calling and annotation workflows |
| 18 | |
| 19 | ## Quick Start |
| 20 | |
| 21 | ### Installation |
| 22 | ```bash |
| 23 | uv pip install pysam |
| 24 | ``` |
| 25 | |
| 26 | ### Basic Examples |
| 27 | |
| 28 | **Read alignment file:** |
| 29 | ```python |
| 30 | import pysam |
| 31 | |
| 32 | # Open BAM file and fetch reads in region |
| 33 | samfile = pysam.AlignmentFile("example.bam", "rb") |
| 34 | for read in samfile.fetch("chr1", 1000, 2000): |
| 35 | print(f"{read.query_name}: {read.reference_start}") |
| 36 | samfile.close() |
| 37 | ``` |
| 38 | |
| 39 | **Read variant file:** |
| 40 | ```python |
| 41 | # Open VCF file and iterate variants |
| 42 | vcf = pysam.VariantFile("variants.vcf") |
| 43 | for variant in vcf: |
| 44 | print(f"{variant.chrom}:{variant.pos} {variant.ref}>{variant.alts}") |
| 45 | vcf.close() |
| 46 | ``` |
| 47 | |
| 48 | **Query reference sequence:** |
| 49 | ```python |
| 50 | # Open FASTA and extract sequence |
| 51 | fasta = pysam.FastaFile("reference.fasta") |
| 52 | sequence = fasta.fetch("chr1", 1000, 2000) |
| 53 | print(sequence) |
| 54 | fasta.close() |
| 55 | ``` |
| 56 | |
| 57 | ## Core Capabilities |
| 58 | |
| 59 | ### 1. Alignment File Operations (SAM/BAM/CRAM) |
| 60 | |
| 61 | Use the `AlignmentFile` class to work with aligned sequencing reads. This is appropriate for analyzing mapping results, calculating coverage, extracting reads, or quality control. |
| 62 | |
| 63 | **Common operations:** |
| 64 | - Open and read BAM/SAM/CRAM files |
| 65 | - Fetch reads from specific genomic regions |
| 66 | - Filter reads by mapping quality, flags, or other criteria |
| 67 | - Write filtered or modified alignments |
| 68 | - Calculate coverage statistics |
| 69 | - Perform pileup analysis (base-by-base coverage) |
| 70 | - Access read sequences, quality scores, and alignment information |
| 71 | |
| 72 | **Reference:** See `references/alignment_files.md` for detailed documentation on: |
| 73 | - Opening and reading alignment files |
| 74 | - AlignedSegment attributes and methods |
| 75 | - Region-based fetching with `fetch()` |
| 76 | - Pileup analysis for coverage |
| 77 | - Writing and creating BAM files |
| 78 | - Coordinate systems and indexing |
| 79 | - Performance optimization tips |
| 80 | |
| 81 | ### 2. Variant File Operations (VCF/BCF) |
| 82 | |
| 83 | Use the `VariantFile` class to work with genetic variants from variant calling pipelines. This is appropriate for variant analysis, filtering, annotation, or population genetics. |
| 84 | |
| 85 | **Common operations:** |
| 86 | - Read and write VCF/BCF files |
| 87 | - Query variants in specific regions |
| 88 | - Access variant information (position, alleles, quality) |
| 89 | - Extract genotype data for samples |
| 90 | - Filter variants by quality, allele frequency, or other criteria |
| 91 | - Annotate variants with additional information |
| 92 | - Subset samples or regions |
| 93 | |
| 94 | **Reference:** See `references/variant_files.md` for detailed documentation on: |
| 95 | - Opening and reading variant files |
| 96 | - VariantRecord attributes and methods |
| 97 | - Accessing INFO and FORMAT fields |
| 98 | - Working with genotypes and samples |
| 99 | - Creating and writing VCF files |
| 100 | - Filtering and subsetting variants |
| 101 | - Multi-sample VCF operations |
| 102 | |
| 103 | ### 3. Sequence File Operations (FASTA/FASTQ) |
| 104 | |
| 105 | Use `FastaFile` for random access to reference sequences and `FastxFile` for reading raw sequencing data. This is appropriate for extracting gene sequences, validating variants against reference, or processing raw reads. |
| 106 | |
| 107 | **Common operations:** |
| 108 | - Query reference sequences by genomic coordinates |
| 109 | - Extract sequences for genes or regions of interest |
| 110 | - Read FASTQ files with quality scores |
| 111 | - Validate variant reference alleles |
| 112 | - Calculate sequence statistics |
| 113 | - Filter reads by quality or length |
| 114 | - Convert between FASTA and FASTQ formats |
| 115 | |
| 116 | **Reference:** See `references/sequence_files.md` for detailed documentation on: |
| 117 | - FASTA file access and indexing |
| 118 | - Extracting sequences by region |
| 119 | - Handling reverse complement for genes |
| 120 | - Reading FASTQ files sequentially |
| 121 | - Quality score conversion and filtering |
| 122 | - Working with tabix-indexed files (BED, GTF, GFF) |
| 123 | - Common sequence processing patterns |
| 124 | |
| 125 | ### 4. Integrated Bioinformatics Workflows |
| 126 | |
| 127 | Pysam excels at integrating multiple file types for comprehensive genomic analyses. Common workflows combine alignment files, variant files, and reference sequences. |
| 128 | |
| 129 | **Common workflows:** |
| 130 | - Calculate coverage statistics for specific regions |
| 131 | - Validate variants against aligned reads |
| 132 | - Annotate variants with coverage information |
| 133 | - Extract sequences around variant positions |
| 134 | - Filter alignments or variants based on multiple crit |