pku-yuangroup/openai4s/bio-alignment-files-alignment-indexing
Claude Code and agent skill by pku-yuangroup/openai4s
- community
Checking that the SKILL.md file is still in the repository.
Create and use BAI/CSI indices for BAM/CRAM files using samtools and pysam. Use when enabling random access to alignment files or fetching specific genomic regions.
Install
npx vskill@latest install pku-yuangroup/openai4s/bio-alignment-files-alignment-indexingSKILL.md
The same SKILL.md was published earlier as pku-yuangroup/openai4s/bio-alignment-files-alignment-indexing.
Version Compatibility
Reference examples tested with: pysam 0.22+, samtools 1.19+
Before using code patterns, verify installed versions match. If versions differ:
- Python:
pip show <package>thenhelp(module.function)to check signatures - CLI:
<tool> --versionthen<tool> --helpto confirm flags
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Alignment Indexing
Create indices for random access to alignment files using samtools and pysam.
"Index a BAM file" -> Create a .bai/.csi index enabling random access to genomic regions.
- CLI:
samtools index file.bam - Python:
pysam.index('file.bam')
Index Types
| Index | Extension | Max contig | Bin shift | When required | |
|---|---|---|---|---|---|
| BAI | .bai / .bam.bai | 2^29 bp ≈ 537 Mbp | fixed (16 kb) | Default for human, mouse, fly, fish | |
| CSI | .csi / .bam.csi | 2^(min_shift + depth*3) | configurable via -m | Required for any contig >537 Mbp | |
| CRAI | .crai / .cram.crai | chunk-based | n/a | CRAM only | |
| TBI | .tbi | 2^29-1 | fixed | tabix VCF/BED -- same limit as BAI |
Which Index for Which Genome
| Genome | Largest contig | Index | |||
|---|---|---|---|---|---|
| GRCh38 / GRCh37 (human) | 248 Mbp | BAI | |||
| GRCm39 (mouse) | 195 Mbp | BAI | |||
| GRCz11 (zebrafish), TAIR10 (Arabidopsis) | 78 Mbp / 30 Mbp | BAI | |||
| Wheat IWGSC (Triticum aestivum) | ~830 Mbp (chr3B) | CSI | |||
| Pine, fir, axolotl, sugar pine | multi-Gbp | CSI with larger `-m` | |||
| Long-read assembly with very large contigs | varies | check `cut -f2 ref.fa.fai \ | sort -nr \ | head -1` |
For polyploid plants and salamander-scale genomes, increase the bin shift:
Index file precedence: htslib (and therefore the samtools CLI) tries .csi before .bai on auto-load, so when both exist the .csi is used -- a stale .bai left over after re-indexing to CSI is generally ignored. (Note: htsjdk/Java prefers .bai, the opposite order.) Removing the obsolete .bai still avoids confusion.
samtools index
Create BAI Index
Create CSI Index
Specify Output Name
Multi-threaded Indexing
Index CRAM
Index Requirements
Indexing requires coordinate-sorted files:
Using Indices for Region Access
Goal: Extract reads overlapping specific genomic coordinates from an indexed BAM.
Approach: With the index present, samtools view or pysam.fetch() can jump directly to the relevant file offset instead of scanning the entire file.
samtools view with Region
Multiple Regions
Regions from BED File
pysam Python Alternative
Create Index
Create CSI Index
Fetch with Index
Check if Indexed
Fetch Multiple Regions
Count Reads in Region
Get Reads Covering Position
Index File Locations
samtools looks for indices in two locations:
For CRAM:
idxstats - Index Statistics
Get Per-Chromosome Counts
Output format:
Columns: reference name, length, mapped reads, unmapped reads.
What "mapped" Actually Counts (Caveat)
The mapped column counts every alignment record with that RNAME, including secondary AND supplementary. For long-read minimap2 output, where a single read can produce many supplementary chimeric alignments, idxstats overcounts input reads -- typically 1.5-3x.
For unique read counts, use primary-only:
Cross-check unmapped consistency (a senior sanity check):
Sum Total Mapped Reads
pysam idxstats
FASTA Index (faidx)
Related but different - index reference FASTA for random access:
pysam FastaFile
Quick Reference
| Task | samtools | pysam | |
|---|---|---|---|
| Create BAI | samtools index file.bam | pysam.index('file.bam') | |
| Create CSI | samtools index -c file.bam | pysam.index('-c', 'file.bam') | |
| Fetch region | samtools view file.bam chr1:1-1000 | bam.fetch('chr1', 0, 1000) | |
| Count in region | samtools view -c file.bam chr1:1-1000 | bam.count('chr1', 0, 1000) | |
| Index stats | samtools idxstats file.bam | bam.get_index_statistics() | |
| Index FASTA | samtools faidx ref.fa | Automatic with FastaFile |
Index Staleness
If the BAM was modified after indexing, the index points to wrong file offsets and region queries return wrong (or zero) reads. Quick check:
Contig-Naming Sanity Check
A leading cause of "my variant calling produced empty VCFs" tickets: querying chrM against a BAM that uses MT (or chr1 vs 1). Always inspect contig conventions before region queries:
UCSC convention uses chr1/chrM; Ensembl/NCBI uses 1/MT. The two are not interchangeable; tools fail with "contig not found" or silently return zero reads.
Common Errors
| Error | Cause | Solution | |
|---|---|---|---|
random alignment retrieval only works for indexed BAM | Missing index | Run samtools index file.bam | |
file is not sorted | Unsorted BAM | Sort first with samtools sort | |
chromosome not found | Wrong chromosome name | Check names with samtools view -H | |
| Region query returns zero reads on a known-populated locus | Stale BAI / chr vs no-chr mismatch | Re-index; verify naming convention | |
| BAI silently truncates reads on contigs >537 Mbp | Plant / amphibian / amplified genome | Use CSI: samtools index -c file.bam |
Related Skills
- sam-bam-basics - View and convert alignment files
- alignment-sorting - Sort BAM files (required before indexing)
- alignment-filtering - Filter by regions using index
- bam-statistics - Use idxstats for quick counts
- sequence-io/read-sequences - Index FASTA with SeqIO.index_db()
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/bioskills/bio-alignment-files-alignment-indexing/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Grok Build (Cursor)
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/pku-yuangroup/openai4s/bio-alignment-files-alignment-indexing)