omics.co.in logo omics.co.in

Control structures, Loops, and Conditional Statements

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 4 of 13 0% Course Completed
🐍 Downloadable Lesson Python Script 3.4 KB

ch04_fastq_stream_parser.py

Stream-parses raw 4-line FASTQ records, converts ASCII Phred+33 scores, computes mean read quality, and filters reads exceeding Q30 thresholds.

$ python ch04_fastq_stream_parser.py -i reads.fastq.gz -q 30 -l 50

Control Structures: Loops and Conditional Statements in Python

Control structures allow programmers to dictate the flow of execution in biological data processing scripts. When working with thousands of sequences or genomic variants, mastering conditionals and loops is indispensable.

1. Conditional Statements (if, elif, else)

Evaluating biological filters such as sequencing quality thresholds, GC thresholds, and length boundaries:

# Quality Score filtering
read_phred_score = 34

if read_phred_score >= 30:
    quality_status = "Q30+ (High Confidence, Error Rate <= 0.1%)"
elif read_phred_score >= 20:
    quality_status = "Q20+ (Moderate, Error Rate <= 1%)"
else:
    quality_status = "Low Quality (Needs Trimming or Filtering)"

print(f"Read Status: {quality_status}")

2. Iterating with For Loops

Iterate over codon triplets or genomic coordinates with ease:

dna_sequence = "ATGCGATCGATCGTAGCTAGCTA"

# Extracting all 3-base codons
codons = [dna_sequence[i:i+3] for i in range(0, len(dna_sequence)-2, 3)]
print("Codon Stream:", codons)
⚠️

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 ↗