tywei08/bioskills-plugin/alignment-sorting
Claude Code and agent skill by tywei08/bioskills-plugin
- community
Checking that the SKILL.md file is still in the repository.
Sort alignment files by coordinate or read name using samtools and pysam. Use when preparing BAM files for indexing, variant calling, or paired-end analysis.
Install
npx vskill@latest install tywei08/bioskills-plugin/alignment-sortingSKILL.md
The same SKILL.md was published earlier as tywei08/bioskills-plugin/alignment-sorting.
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 Sorting
Sort alignment files by coordinate or read name using samtools and pysam.
"Sort a BAM file" → Reorder reads by genomic coordinate (for indexing/variant calling) or by name (for paired-end processing).
- CLI:
samtools sort -o sorted.bam input.bam - Python:
pysam.sort('-o', 'sorted.bam', 'input.bam')
Sort Orders
| Order | Flag | Use Case | |
|---|---|---|---|
| Coordinate | default | Indexing, visualization, variant calling | |
| Name | -n | Paired-end processing, fixmate, markdup | |
| Tag | -t TAG | Sort by specific tag value |
samtools sort
Sort by Coordinate (Default)
Sort by Read Name
Multi-threaded Sorting
Control Memory Usage
Set Temporary Directory
Specify Output Format
Sort by Tag
Pipe from Aligner
samtools collate
Group paired reads together without full sorting (faster than name sort for some workflows):
Check Sort Order
From Header
Verify Sorted
pysam Python Alternative
Sort with pysam
Sort by Name
Sort with Options
Manual Sorting in Python
Check Sort Order in pysam
Stream Sort from Aligner
For streaming from aligners, use shell pipes (simpler and more reliable):
Or use pysam with a named pipe:
samtools merge
Combine multiple BAM files into one.
Basic Merge
Merge with Threads
Merge from File List
Force Overwrite
Merge Specific Region
pysam Merge
Common Workflows
Goal: Combine sorting with other alignment processing steps into efficient pipelines.
Approach: Pipe aligner output directly into samtools sort to avoid writing unsorted intermediates, then index for downstream access.
Align and Sort
Re-sort by Name for Duplicate Marking
Convert Name-sorted to Coordinate-sorted
Extract FASTQ from Sorted BAM
Performance Tips
| Parameter | Effect | |
|---|---|---|
-@ N | Use N additional threads | |
-m SIZE | Memory per thread (e.g., 4G) | |
-T PREFIX | Temp file location (use fast disk) | |
-l LEVEL | Compression level (1-9, default 6) |
Optimal Settings for Large Files
Quick Reference
| Task | Command | ||
|---|---|---|---|
| Sort by coordinate | samtools sort -o out.bam in.bam | ||
| Sort by name | samtools sort -n -o out.bam in.bam | ||
| Sort with threads | samtools sort -@ 8 -o out.bam in.bam | ||
| Collate pairs | samtools collate -o out.bam in.bam | ||
| Merge BAMs | samtools merge out.bam in1.bam in2.bam | ||
| Check sort order | `samtools view -H in.bam \ | grep "^@HD"` | |
| Sort + index | samtools sort -o out.bam in.bam && samtools index out.bam |
Common Errors
| Error | Cause | Solution | |
|---|---|---|---|
out of memory | Insufficient RAM | Use -m to limit per-thread memory | |
disk full | Temp files filling disk | Use -T to specify different location | |
truncated file | Interrupted sort | Re-run sort from original |
Related Skills
- sam-bam-basics - View and convert alignment files
- alignment-indexing - Index after coordinate sorting
- duplicate-handling - Requires name-sorted input for fixmate
- alignment-filtering - Filter before or after sorting
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingalignment-files/alignment-sorting/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- 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/tywei08/bioskills-plugin/alignment-sorting)