The Bioinformatician's Command Cookbook & Format Reference
High-yield command-line one-liners, CLI tool syntax cards, and comprehensive field-by-field specifications for core genomic formats. Designed for daily computational biology practice, pipeline engineering, and quick lookups.
โก The Bioinformatician's One-Liner Cookbook
Awk, Sed, Grep & BashCount Total Reads in Gzipped FASTQ
FASTQDivides total line count by 4. Fast, streaming without uncompressing to disk.
zcat reads.fq.gz | awk '{s++}END{print s/4}'Convert FASTQ to FASTA
FASTQReplaces '@' with '>' on line 1 and keeps line 2 (sequence), skipping quality scores.
zcat reads.fq.gz | paste - - - - | cut -f 1,2 | sed 's/^@/>/' | tr " " "
" > out.faKeep Only Mapped High-Quality Reads
BAM
Discards unmapped reads (-F 4) and requires Mapping Quality ≥ 30 (-q 30).
samtools view -b -F 4 -q 30 in.bam > filtered.bamCount Mapped Reads per Chromosome
BAMInstant lookup from BAM index (.bai) without parsing read alignments.
samtools idxstats in.bam | cut -f 1,3 | head -n 25Filter VCF for PASS & High-Frequency SNVs
VCFKeeps only high-confidence variants passing all filters with AF > 5%.
bcftools view -f 'PASS' -i 'TYPE="snp" && INFO/AF > 0.05' in.vcf.gz -Oz -o filtered.vcf.gzCalculate Transition / Transversion (Ti/Tv) Ratio
VCFExpected Ti/Tv is ~2.0-2.1 for human WGS, and ~2.8-3.0 for whole exome (WES).
bcftools stats in.vcf.gz | grep "^TSTV" | cut -f 5๐ ๏ธ Samtools & Bcftools Essential Syntax
v1.19+ Standards| Task / Action | Command Syntax | Key Flags & Notes |
|---|---|---|
| SAM to Sorted BAM | samtools sort -@ 8 -o out.bam in.sam | -@ 8 uses 8 CPU threads for multi-threaded compression. |
| Index BAM File | samtools index -@ 4 in.bam | Generates in.bam.bai required for random coordinate queries. |
| Alignment Statistics | samtools flagstat in.bam > stats.txt | Shows total reads, primary alignments, proper pair %, and duplicates. |
| Per-Base Depth of Coverage | samtools depth -a -b targets.bed in.bam > depth.tsv | -a outputs all positions including 0-depth; -b restricts to BED. |
| Normalize & Split Multiallelics | bcftools norm -m -any -f ref.fa in.vcf.gz -Oz -o norm.vcf.gz | Left-aligns indels and splits lines with multiple ALT alleles (e.g. A,T). |
| Query Genotypes to TSV | bcftools query -f '%CHROM %POS %REF %ALT[ %GT] ' in.vcf.gz | Square brackets [...] iterate over all samples in the VCF. |
๐ Bedtools Interval Arithmetic Reference
Genomic ArithmeticFind overlaps between features in A and B (e.g. ChIP peaks in Promoters).
bedtools intersect -a peaks.bed -b promoters.bed -wa -wb > overlaps.bed
Combine overlapping or contiguous intervals into unified single intervals.
sort -k1,1 -k2,2n in.bed | bedtools merge -i - > merged.bed
Remove regions in B from features in A (e.g. Exclude repetitive elements).
bedtools subtract -a exons.bed -b repeats.bed > non_repeat_exons.bed
Extract nucleotide sequences from reference FASTA for BED coordinates.
bedtools getfasta -fi hg38.fa -bed peaks.bed -fo peaks.fa
๐ Genomic File Formats Field-by-Field Reference
Specification StandardsVCF 4.2 / 4.3 Fixed Columns & Standard Tags
Every VCF file has 8 mandatory fixed tab-separated columns, followed by an optional 9th column (FORMAT) and sample columns (10+).
| Col # | Field Name | Type / Convention | Description & Biological Context |
|---|---|---|---|
| 1 | CHROM | String | Chromosome identifier (e.g. chr17 or 17). |
| 2 | POS | Integer (1-based) | Reference coordinate of the first base in the REF allele. |
| 3 | ID | String | Identifier (e.g. dbSNP rsID rs121913529 or . if unassigned). |
| 4 | REF | String [A,C,G,T,N] | Reference allele sequence from the reference genome build. |
| 5 | ALT | Comma-separated | Alternate non-reference allele(s) detected in the sample(s). |
| 6 | QUAL | Float (Phred-scale) | -10 * log10(P(error)) that ALT is wrong. QUAL 30 = 99.9% accurate. |
| 7 | FILTER | String | PASS if passed all quality filters; or semicolon list of failed filters. |
| 8 | INFO | Key-Value (Semicolon) | Site-level annotations (e.g. DP=120;AF=0.45;AN=2;AC=1). |
GT: Genotype (0/0 ref hom, 0/1 het, 1/1 alt hom, 0|1 phased)AD: Allelic Depths (e.g. 32,18 = 32 ref reads, 18 alt reads)DP: Total Filtered Depth at this sample locus (e.g. 50)GQ: Genotype Quality Phred score (0-99, confidence in assigned GT)SAM / BAM Mandatory Fields & CIGAR Operators
The Sequence Alignment/Map (SAM) format has 11 mandatory tab-delimited columns for every aligned segment.
CIGAR String Operators (Column 6):
M: Alignment match/mismatchI: Insertion into referenceD: Deletion from referenceN: Skipped intron (RNA-seq)S: Soft clipping (bases in SEQ)H: Hard clipping (not in SEQ)→ 15 bp soft-clipped adapter/primer + 85 bp exon alignment + 1,200 bp spliced intron + 50 bp downstream exon alignment.