omics.co.in

Control structures, Loops, and Conditional Statements

🐍 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)
← Return to Home Hub Scroll to Top ↑