samtools-bam-processing
Skill8 starsChanged 5 months agoMITView on GitHub
Process BAM files with samtools — sort, index, merge, flagstat, mark duplicates, fixmate, depth, filter, and stream — the standard CLI toolkit for 2026.
In this repository125 other files it gives its agents.
- AGENTS.mdAGENTS.md
- skills/AGENTS.mdAGENTS.md
- batch-processingSkill
- bowtie2-alignmentSkill
- bwa-alignmentSkill
- bwa-mem2-alignmentSkill
- codon-usageSkill
- compressed-sequence-filesSkill
- fastq-quality-scoresSkill
- filter-sequencesSkill
- format-conversionSkill
- hisat2-alignmentSkill
- motif-searchSkill
- paired-end-fastqSkill
- pysam-genomicsSkill
- read-write-sequencesSkill
- reverse-complementSkill
- sam-bam-basicsSkill
- seq-objectsSkill
- sequence-propertiesSkill
- sequence-slicingSkill
- sequence-statisticsSkill
- star-alignmentSkill
- transcription-translationSkill
- academic-cvSkill
- faculty-interviewSkill
- fellowship-applicationSkill
- industry-transitionSkill
- negotiationSkill
- tenure-dossierSkill
- admet-predictionSkill
- conformer-generationSkill
- covalent-designSkill
- docking-rescoringSkill
- free-energy-calculationsSkill
- generative-designSkill
- molecular-descriptorsSkill
- molecular-ioSkill
- molecular-standardizationSkill
- pharmacophore-modelingSkill
- pose-validationSkill
- protac-degradersSkill
- qsar-modelingSkill
- retrosynthesisSkill
- scaffold-analysisSkill
- shape-similaritySkill
- similarity-searchingSkill
- substructure-searchSkill
- virtual-screeningSkill
- snakemake-workflow-engineSkill
- ai-disclosureSkill
- data-privacySkill
- image-integritySkill
- irb-protocolSkill
- ai-detection-awarenessSkill
- ai-disclosure-statementSkill
- scientific-voice-styleSkill
- text-humanizing-editorialSkill
- cellprofiler-pipelinesSkill
- image-analysis-best-practicesSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 6 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 20
No fabricated citations: Every cited work must resolve to a verifiable
- Distinctive: line 21
No claim without provenance: Every quantitative or factual claim
- Distinctive: line 22
No silent failure: Every script invocation, API call, or tool use must declare its exit status and what to do on non-zero.
- Very specific: line 218
Check at each step with samtools flagstat.
- Distinctive: line 223
Sorting out of order: Always name-sort before fixmate, then coordinate-sort after.
- Distinctive: line 231
After fixmate, FLAG 2 should be set on properly-paired reads that previously lacked it.
- Distinctive: line 232
After dedup, duplicate count in flagstat should be non-zero only if duplicates were present.
- Very specific: line 233
After index, samtools view -b sample.bam chr1:0-100 should return reads.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
<!-- metadata:
category: bioinformatics-sequence
oversight: low
version: 1.0.0
author: Pradyumna Jayaram
tags: [bioinformatics-sequence, research]
difficulty: intermediate
-->samtools BAM Processing
Hard rules
- No fabricated citations. Every cited work must resolve to a verifiable
- No claim without provenance. Every quantitative or factual claim
- No silent failure. Every script invocation, API call, or tool use must declare its exit status and what to do on non-zero. A skill that silently swallows errors is a violation.
When to use
- Sorting and indexing BAMs after alignment.
- Getting alignment statistics.
- Marking duplicates.
- Fixing mate information for properly-paired requirements.
- Extracting subsets by FLAG, MAPQ, region.
When NOT to use
- You need alignment-level filtering → see
pysamfor Python. - You need variant calling → see GATK / bcftools.
- Visual → use IGV.
Prerequisites
samtools≥ 1.19bcftools≥ 1.19 (always installed together)- Reference FASTA +
.fai
Core workflow
- Align → SAM.
- Sort by name (
samtools sort -n) → coordinate sort needs name-sorted first. - Fixmate (
samtools fixmate) → sets the mate information. - Sort by coordinate (
samtools sort) → final coordinate-sorted BAM. - Mark duplicates (
samtools markdup -r) → remove optical duplicates. - Index (
samtools index) → enable random access.
The correct order: align → name-sort → fixmate → coordinate-sort → markdup → index.
Code patterns
Coordinate sort
samtools sort -@ 8 -m 4G -o output.sorted.bam input.bam
samtools index output.sorted.bam-@ 8threads.-m 4Gmemory per thread.-owrites to specified output, not just stdout.
Name sort (for fixmate input)
samtools sort -n -@ 8 -o output.name.bam input.samFixmate (set mate information for properly-paired requirement)
samtools fixmate -m -O bam input.name.bam fixed.bam
samtools sort -@ 8 -o fixed.sorted.bam fixed.bamfixmate -m enables "mark shorter by more different" (for GATK compatibility), and -O bam produces BAM output directly.
Mark duplicates (modern samtools 1.16+)
samtools markdup -r -s -t -v 1 \
input.sorted.bam output.dedup.bam
samtools index output.dedup.bam-rREMOVE duplicates (not just mark).-sprint statistics to stderr.-tmark duplicate tags (MC, ms).-vverbose.
Fixmate → coordinate sort → markdup pipeline
# Full pipeline (all from alignment output)
bwa mem -t 16 -M ref.fa R1.fq R2.fq |
samtools sort -n -@ 8 |
samtools fixmate -m -O bam - |
samtools sort -@ 8 -O bam |
samtools markdup -r -@ 8 - output.bam
samtools index output.bamsamtools flagstat
samtools flagstat input.bam | tail -10
# Shows mapped, paired, properly-paired, duplicate countssamtools stats
samtools stats input.bam
# Full alignment statisticsExtract coverage depth per base
samtools depth input.bam > depth.tsvPer-base coverage, only positions >= 10x
samtools depth -aa -d 1000 input.bam | awk '$3>=10' > cov10.tsvFilter by MAPQ
samtools view -q 30 -b input.bam > q30.bamExtract properly paired reads
samtools view -F 4 -f 2 -b input.bam > proper.bamExtract primary alignments only
samtools view -F 256 -b input.bam > primary.bamMerge BAMs
samtools merge -@ 8 -o merged.bam input1.bam input2.bam
samtools index merged.bamSplit BAM by read group
samtools split -f '%*_%!.bam' -d input.bamExtract alignments to chromosome
samtools view -b chr1 input.bam > chr1.bam
samtools index chr1.bamTabix region query
Requires a BED file and tabix index:
tabix -p bed input.vcf.gz
tabix input.vcf.gz chr1:1000-2000Concatenate BAMs (when same sample, different lane)
samtools cat -o merged.bam lane1.bam lane2.bam
samtools index merged.bamValidate an index
# Index is built alongside BAM
ls -la input.bam.bai # or input.bam.csiThe standard pipeline (full example)
# 1. Align
bwa mem -t 16 -M -R "@RG\tID:sample1\tSM:sample1" \
ref.fa R1.fq.gz R2.fq.gz |
# 2. Name sort (needed for fixmate)
samtools sort -n -@ 8 |
# 3. Fixmate (set mate positions, mark duplicate tags)
samtools fixmate -m -O bam - |
# 4. Coordinate sort
samtools sort -@ 8 -O bam |
# 5. Mark duplicates
samtools markdup -r -@ 8 - output.bam
# 6. Index
samtools index output.bamCheck at each step with samtools flagstat.
Common pitfalls
- Forgetting to fixmate. Without fixmate, paired reads may not have FLAG 2 (proper pair) set correctly.
- Sorting out of order. Always name-sort before fixmate, then coordinate-sort after.
- Duplicate marking too early. Mark before coordinate sort leads to wrong duplicate detection.
- Index required but missing.
samtools view -b sample.bam chr1:10-10000fails without an index. - Memory for sorting.
-m 4Gis reasonable; smaller values may cause sort failure on large files.
Validation
samtools flagstat sample.bamshows read counts and properly-paired.- After fixmate, FLAG 2 should be set on properly-paired reads that previously lacked it.
- After dedup, duplicate count in flagstat should be non-zero only if duplicates were present.
- After index,
samtools view -b sample.bam chr1:0-100should return reads.
Open alternatives
| Need | Tool |
| GPU deduplication | nvidia/parabricks (closed) |
| Multi-threaded sort | sambamba (legacy alternative) |
| In-Python filtering | pysam |
| Full pipeline | nf-core/sarek, GATK best practices |
References
- samtools manual: http://www.htslib.org/doc/samtools.html
- SAM format spec: https://samtools.github.io/hts-specs/SAMv1.pdf
- GATK Best Practices: https://gatk.broadinstitute.org/hc/en-us/articles/360035894711-About-GATK3
- Companion:
ors-bioinformatics-sequence-bwa-alignment,ors-bioinformatics-sequence-pysam-genomics.
Changelog
- 1.0.0 (2026-06-10): Initial adaptation by Pradyumna Jayaram from
samtools-bam-processing(SciAgent-Skills).
Cross-references
Other skills in this category:
- batch-processing
- bowtie2-alignment
- bwa-alignment
- bwa-mem2-alignment
- codon-usage
- compressed-sequence-files
- fastq-quality-scores
- filter-sequences
- format-conversion
- hisat2-alignment
- motif-search
- paired-end-fastq
- pysam-genomics
- read-write-sequences
- reverse-complement
- sam-bam-basics
- seq-objects
- sequence-properties
- sequence-slicing
- sequence-statistics
- star-alignment
- transcription-translation
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

