Install any skill in seconds. Free to start, no credit card required.
Get Started Free →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.
.claude/skills/bio-alignment-sorting/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-05 | ✗→✓ | ▲ Improved | 216% | 0% |
| case-01 | ✓→✓ | = Same ✓ | 115% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 196% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 240% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 71% | 0% |
<!--
#
#
-->
Sort alignment files by coordinate or read name using samtools and pysam.
| 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 |
bashsamtools sort -o sorted.bam input.bam
bashsamtools sort -n -o namesorted.bam input.bam
bashsamtools sort -@ 8 -o sorted.bam input.bam
bashsamtools sort -m 4G -@ 4 -o sorted.bam input.bam
bashsamtools sort -T /tmp/sort_tmp -o sorted.bam input.bam
bash# Output as BAM (default) samtools sort -O bam -o sorted.bam input.bam # Output as CRAM samtools sort -O cram --reference ref.fa -o sorted.cram input.bam
bash# Sort by cell barcode (10x Genomics) samtools sort -t CB -o sorted_by_barcode.bam input.bam
bashbwa mem ref.fa reads.fq | samtools sort -o aligned.bam
Group paired reads together without full sorting (faster than name sort for some workflows):
bash# Collate paired reads samtools collate -o collated.bam input.bam # With output prefix for temp files samtools collate -O input.bam /tmp/collate > collated.bam # Fast mode (output to stdout) samtools collate -u -O input.bam /tmp/collate | samtools fastq -1 R1.fq -2 R2.fq -
bashsamtools view -H input.bam | grep "^@HD" # SO:coordinate = coordinate sorted # SO:queryname = name sorted # SO:unsorted = not sorted
bash# Check if coordinate sorted (returns 0 if sorted) samtools view input.bam | awk '$4 < prev {exit 1} {prev=$4}'
pythonimport pysam pysam.sort('-o', 'sorted.bam', 'input.bam')
pythonpysam.sort('-n', '-o', 'namesorted.bam', 'input.bam')
pythonpysam.sort('-@', '4', '-m', '2G', '-o', 'sorted.bam', 'input.bam')
pythonimport pysam with pysam.AlignmentFile('input.bam', 'rb') as infile: header = infile.header reads = list(infile) reads.sort(key=lambda r: (r.reference_id, r.reference_start)) with pysam.AlignmentFile('sorted.bam', 'wb', header=header) as outfile: for read in reads: outfile.write(read)
pythonimport pysam with pysam.AlignmentFile('input.bam', 'rb') as bam: hd = bam.header.get('HD', {}) sort_order = hd.get('SO', 'unknown') print(f'Sort order: {sort_order}')
For streaming from aligners, use shell pipes (simpler and more reliable):
pythonimport subprocess subprocess.run( 'bwa mem ref.fa reads.fq | samtools sort -o aligned.bam', shell=True, check=True )
Or use pysam with a named pipe:
pythonimport os import pysam import subprocess os.mkfifo('aligner.pipe') try: aligner = subprocess.Popen(['bwa', 'mem', 'ref.fa', 'reads.fq'], stdout=open('aligner.pipe', 'w')) pysam.sort('-o', 'aligned.bam', 'aligner.pipe') aligner.wait() finally: os.unlink('aligner.pipe')
Combine multiple BAM files into one.
bashsamtools merge merged.bam sample1.bam sample2.bam sample3.bam
bashsamtools merge -@ 4 merged.bam sample1.bam sample2.bam sample3.bam
bash# files.txt contains one BAM path per line samtools merge -b files.txt merged.bam
bashsamtools merge -f merged.bam sample1.bam sample2.bam
bashsamtools merge -R chr1:1000000-2000000 merged_region.bam sample1.bam sample2.bam
pythonimport pysam pysam.merge('-f', 'merged.bam', 'sample1.bam', 'sample2.bam', 'sample3.bam')
bashbwa mem -t 8 ref.fa R1.fq R2.fq | samtools sort -@ 4 -o aligned.bam samtools index aligned.bam
bash# Full workflow: sort by name, fixmate, sort by coord, markdup samtools sort -n -o namesorted.bam input.bam samtools fixmate -m namesorted.bam fixmate.bam samtools sort -o sorted.bam fixmate.bam samtools markdup sorted.bam marked.bam
bashsamtools sort -o coord_sorted.bam name_sorted.bam samtools index coord_sorted.bam
bash# Collate first to group pairs samtools collate -u -O input.bam /tmp/collate | \ samtools fastq -1 R1.fq -2 R2.fq -0 /dev/null -s /dev/null -
| 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) |
bash# Use 8 threads, 4GB per thread, low compression for speed samtools sort -@ 8 -m 4G -l 1 -o sorted.bam input.bam
| 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 |
| 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 |
<!-- AUTHOR_SIGNATURE: 9a7f3c2e-MD-BABU-MIA-2026-MSSM-SECURE -->
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | pass→pass | 6,385 | 2,456 | -62% | 1 | 1 | 0% | 1,195 | 2,569 | +115% | 0 | 0 | — |
case-02 | pass→pass | 5,208 | 3,314 | -36% | 1 | 1 | 0% | 931 | 2,757 | +196% | 0 | 0 | — |
case-03 | pass→pass | 4,631 | 2,181 | -53% | 1 | 1 | 0% | 739 | 2,511 | +240% | 0 | 0 | — |
case-04 | pass→pass | 10,495 | 5,444 | -48% | 1 | 1 | 0% | 1,888 | 3,224 | +71% | 0 | 0 | — |
case-05 | fail→pass | 6,963 | 2,638 | -62% | 1 | 1 | 0% | 830 | 2,623 | +216% | 0 | 0 | — |
case-06 | pass→pass | 9,850 | 3,721 | -62% | 1 | 1 | 0% | 1,811 | 2,777 | +53% | 0 | 0 | — |
case-07 | pass→pass | 3,899 | 2,715 | -30% | 1 | 1 | 0% | 641 | 2,616 | +308% | 0 | 0 | — |
case-08 | pass→pass | 7,997 | 3,418 | -57% | 1 | 1 | 0% | 1,450 | 2,723 | +88% | 0 | 0 | — |
case-09 | pass→pass | 3,799 | 3,785 | -0% | 1 | 1 | 0% | 682 | 2,652 | +289% | 0 | 0 | — |
case-10 | pass→pass | 8,321 | 2,895 | -65% | 1 | 1 | 0% | 1,697 | 2,706 | +59% | 0 | 0 | — |
case-11 | pass→pass | 3,479 | 1,661 | -52% | 1 | 1 | 0% | 589 | 2,375 | +303% | 0 | 0 | — |
case-12 | pass→pass | 6,350 | 2,501 | -61% | 1 | 1 | 0% | 1,161 | 2,602 | +124% | 0 | 0 | — |
case-13 | pass→pass | 5,851 | 3,219 | -45% | 1 | 1 | 0% | 1,145 | 2,734 | +139% | 0 | 0 | — |
case-14 | pass→pass | 9,934 | 8,084 | -19% | 1 | 1 | 0% | 1,984 | 3,604 | +82% | 0 | 0 | — |
case-15 | pass→pass | 5,929 | 2,577 | -57% | 1 | 1 | 0% | 1,201 | 2,580 | +115% | 0 | 0 | — |
case-16 | pass→pass | 7,767 | 9,077 | +17% | 1 | 1 | 0% | 1,381 | 3,856 | +179% | 0 | 0 | — |
case-17 | pass→pass | 4,780 | 3,026 | -37% | 1 | 1 | 0% | 933 | 2,686 | +188% | 0 | 0 | — |
case-18 | pass→pass | 3,115 | 2,978 | -4% | 1 | 1 | 0% | 567 | 2,501 | +341% | 0 | 0 | — |
case-19 | pass→pass | 5,264 | 3,211 | -39% | 1 | 1 | 0% | 934 | 2,729 | +192% | 0 | 0 | — |
case-20 | pass→pass | 7,306 | 4,762 | -35% | 1 | 1 | 0% | 1,418 | 3,078 | +117% | 0 | 0 | — |
case-21 | pass→pass | 3,807 | 2,309 | -39% | 1 | 1 | 0% | 621 | 2,514 | +305% | 0 | 0 | — |
case-22 | pass→pass | 18,938 | 9,390 | -50% | 1 | 1 | 0% | 3,555 | 3,999 | +12% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
| Model | Method | Date | Lift |
|---|---|---|---|
| gemini-3.6-flash | verified | 7/26/2026 | +23% |
Other measured skills in the registry, with their headline benchmark lift.