omics.co.in logo omics.co.in

5: Pattern Matching & Regular Expressions with grep

Tested Stacks: GATK 4.6.1 Samtools 1.21 Nextflow 24.10 Seurat v5.1 Python 3.12
Prerequisites: Basic Linux CLI & Sequencing Formats
📚 Chapter 5 of 13 0% Course Completed

Chapter 5: Pattern Matching & Regular Expressions with grep

Searching for specific sequence motifs, identifying chromosome scaffolds, filtering out quality metadata, and subsetting biological records are everyday tasks in bioinformatics. The Unix utility grep (Global Regular Expression Print) searches text streams for matching character sequences and prints matching lines with blistering speed.

Grep vs. Python for Simple Filtering

While Python scripts require importing libraries, opening file handles, and running loops, a compiled C utility like grep can filter a 10 GB VCF or FASTA file in under 15 seconds. For simple record filtering and sequence counting, grep is substantially faster and requires zero boilerplate code.

1. Core grep Syntax & Common Flags

# Basic syntax: grep [flags] "pattern" [file]
grep "chr17" variants.vcf

# Useful grep flags:
grep -i "tp53" genes.tsv      # Case-insensitive match (TP53, tp53, Tp53)
grep -c "chr1" variants.vcf   # Count matching lines only
grep -n "BRCA1" genes.tsv     # Print line numbers alongside matches
grep -v "^#" variants.vcf     # Invert match (print lines that DO NOT start with #)
grep -w "EGFR" genes.tsv      # Exact whole word match (avoids matching EGFR-AS1)
grep -F "AGATCGGAAG" reads.fq # Fast literal fixed-string search (disables regex engine)

2. Regular Expression Fundamentals for Genomics

Regular expressions (regex) define flexible search patterns:

  • ^ : Matches the beginning of a line (e.g. ^> matches FASTA headers).
  • $ : Matches the end of a line (e.g. STOP$).
  • . : Matches any single character.
  • [ATCG] : Character class: matches any one nucleotide within brackets.
  • [^N] : Negated character class: matches any base except N.
  • * : Matches zero or more repetitions of the preceding character.

3. Extracting Headers & Scaffolds from FASTA

# List all chromosome / contig identifiers in a reference genome
grep "^>" GRCh38.fa

# Count total sequences / contigs in an assembly
grep -c "^>" GRCh38.fa

# Extract FASTA headers and display the first 10
grep "^>" hg38.fa | head -n 10

4. Filtering VCF Annotations and Quality Records

# Strip comment metadata lines (#) from a VCF to view data rows only
grep -v "^#" sample.vcf | head -n 20

# Filter variants on chromosome 1 with PASS filter status using extended regex (-E)
grep -v "^#" cohort.vcf | grep -E "^chr1[[:space:]].*PASS"

# Count total high-confidence variants passing filter
grep -v "^#" cohort.vcf | grep -c "PASS"

5. Contextual Extraction with -B, -A, and -C

In FASTQ files, each sequencing read occupies four consecutive lines: (1) Header, (2) Nucleotide Sequence, (3) Plus separator, (4) Phred Quality String. If you search for an Illumina adapter sequence in the sequence line, you need the surrounding lines to preserve the valid FASTQ format:

# Search for an adapter sequence and print 1 line before (-B 1) and 2 lines after (-A 2)
# This captures the entire 4-line FASTQ record!
zcat sample_R1.fq.gz | grep -B 1 -A 2 "AGATCGGAAGAGC" | head -n 8
⚠️

Common Pitfalls & Troubleshooting Checklist

  • Java Heap Exhaustion (GATK / Picard): If processes terminate with java.lang.OutOfMemoryError, set your environment variable explicitly: export _JAVA_OPTIONS="-Xmx16g -Djava.io.tmpdir=/tmp".
  • Index Timestamp Mismatch: If tools complain that a BAM index is missing or out of date, regenerate with samtools index -@ 4 sample.bam.
  • Conda Channel Conflicts: Avoid ABI incompatibilities between bioconda and conda-forge by setting strict priority: conda config --set channel_priority strict.
  • Chromosome Naming Contamination: Always verify whether reference FASTA headers use chr1 or 1; divergent naming causes silent alignment drops.
Was this guide helpful to your research?
Help us maintain 100% accurate, peer-reviewed bioinformatics standards.
📝 Propose Edit on GitHub ↗