pku-yuangroup/openai4s/bio-alignment-files-bam-statistics
Claude Code and agent skill by pku-yuangroup/openai4s
- community
Checking that the SKILL.md file is still in the repository.
Generate alignment statistics using samtools flagstat, stats, depth, coverage, and mosdepth. Use when assessing alignment quality, calculating coverage, or generating QC reports.
Install
npx vskill@latest install pku-yuangroup/openai4s/bio-alignment-files-bam-statisticsSKILL.md
The same SKILL.md was published earlier as pku-yuangroup/openai4s/bio-alignment-files-bam-statistics.
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.
BAM Statistics
"Get alignment statistics and coverage from my BAM file" -> Generate read counts, mapping rates, per-chromosome statistics, depth profiles, and coverage summaries.
- CLI:
samtools flagstat,samtools stats,samtools depth,samtools coverage(samtools) - Python:
pysam.AlignmentFilewithpileup()andget_index_statistics()(pysam)
Generate alignment statistics using samtools and pysam.
Quick Summary Commands
| Question | Best tool | Why | |
|---|---|---|---|
| Quick read counts by FLAG category | samtools flagstat | Fast; counts secondary+supp in totals | |
| Per-chromosome counts | samtools idxstats | Fast (needs index); counts secondary+supp | |
| Insert size, MAPQ, error, GC | samtools stats -r ref.fa | Comprehensive; feeds MultiQC | |
| Per-position depth (small region) | samtools depth or pysam pileup | Slow on full genome | |
| Per-position depth (genome-wide) | `mosdepth` | 3-10x faster than samtools depth | |
| Per-region coverage (BED) | mosdepth --by regions.bed | Production default | |
| Coverage histogram / cumulative | mosdepth -t 4 --no-per-base | Single-pass histogram | |
| Breadth at depth thresholds | mosdepth --thresholds 1,10,30,100 | Standard exome QC | |
| Targeted enrichment QC | picard CollectHsMetrics | PCT_OFF_BAIT, FOLD_80_BASE_PENALTY, AT/GC dropout | |
| Cross-sample contamination | verifybamid2, somalier | FREEMIX < 0.01 expected |
What Each Tool Counts (and Doesn't)
| Counting category | flagstat | stats | idxstats | |
|---|---|---|---|---|
| Primary alignments | in total minus supp | raw total sequences | mapped column | |
| Secondary | secondary line | filtered out | counted in mapped | |
| Supplementary | supplementary line | filtered out | counted in mapped | |
| Mapping rate denominator | total including supp | primary only | mapped+unmapped |
For long-read data where one read produces many supplementary alignments, the senior cross-check:
Reports of "the file has 1.2M reads" where the input was actually 800k with 400k supplementary chimeric splits trace to flagstat misinterpretation.
samtools flagstat
Fast summary of alignment flags.
Output:
(samtools 1.13+ adds the primary, primary duplicates, and primary mapped lines shown above.)
Multi-threaded
Output to File
samtools idxstats
Per-chromosome read counts (requires index).
Output format: chrom length mapped unmapped
Parse idxstats
samtools stats
Comprehensive statistics including insert size, base quality, and more.
View Summary Numbers
Key summary fields:
raw total sequences- Total readsreads mapped- Mapped readsreads mapped and paired- Properly pairedinsert size average- Mean insert sizeinsert size standard deviation- Insert size spreadaverage length- Mean read lengtherror rate- Mismatch rate
Generate Plots (with plot-bamstats)
Stats for Specific Region
samtools depth
Per-position read depth.
Basic Depth
Output: chrom position depth
Depth at Specific Positions
Include Zero-Depth Positions
Maximum Depth Cap (Critical Trap)
Pipelines that historically break the 8000 mpileup cap: targeted oncology hotspots (5000-50000x), mitochondrial DNA (small genome, large read share), amplicon viral (ARTIC: 1000-100000x per amplicon), UMI-deduped capture (14000-17000x post-collapse), highly expressed transcripts (rRNA, mt-RNA).
Overlapping Pair Correction
Without -s, doubled support inflates somatic VAFs at sites covered by overlapping pairs (especially in fragmented samples: FFPE, cfDNA). mosdepth does not double-count overlap. samtools mpileup and bcftools mpileup both enable overlap detection by default; pass -x to disable (long form --disable-overlap-removal in samtools, --ignore-overlaps in bcftools).
mosdepth (Modern Default)
mosdepth excludes unmapped, secondary, QC-fail, and duplicate reads by default (--flag 1796); supplementary reads are NOT excluded (use --flag 3844 to drop them too). Configurable via --flag. Memory ~ 4 bytes x longest chrom (1 GB for human chr1, 12+ GB for axolotl). Does not honor base quality; use samtools depth -q INT if needed.
Depth from BED Regions
Calculate Mean Depth
samtools coverage
Per-chromosome or per-region coverage statistics (faster than depth).
Output columns:
#rname- Reference namestartpos- Start positionendpos- End positionnumreads- Number of readscovbases- Bases with coveragecoverage- Percentage of bases coveredmeandepth- Mean depthmeanbaseq- Mean base qualitymeanmapq- Mean mapping quality
Coverage for Specific Region
Coverage from BED
Histogram Output
pysam Python Alternative
Count Reads
Per-Chromosome Counts
Calculate Depth at Position
Mean Depth in Region
Coverage Statistics
Goal: Compute coverage breadth and depth for a genomic region from a BAM file.
Approach: Iterate pileup columns in the region, count covered positions and accumulate depth, then derive percentages and means.
Reference (pysam 0.22+):
Insert Size Distribution
Goal: Compute the insert size distribution to assess library preparation quality.
Approach: Iterate properly paired read1 records, accumulate template lengths into a Counter, then compute summary statistics.
Reference (pysam 0.22+):
Quick Reference
| Task | Command | ||
|---|---|---|---|
| Quick counts | samtools flagstat input.bam | ||
| Per-chrom counts | samtools idxstats input.bam | ||
| Full stats | samtools stats input.bam | ||
| Coverage summary | samtools coverage input.bam | ||
| Per-position depth | samtools depth input.bam | ||
| Mean depth | `samtools depth input.bam \ | awk '{sum+=$3;n++}END{print sum/n}'` |
QC Thresholds Are Assay-Specific
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 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-bam-statistics/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-bam-statistics)