7: Modules and Packages
Chapter 7: Modules and Packages in Python
As computational biology pipelines expand, keeping all code in a single monolithic script leads to maintenance bottlenecks. Modules and packages allow bioinformaticians to organize algorithms, parsers, and utilities into reusable libraries across projects.
1. Modules Architecture
A module is simply a Python file (e.g. seq_tools.py) containing functions, classes, and variables. Create a file named seq_tools.py with common sequence utilities:
# seq_tools.py - Reusable Genomic Utilities
IUPAC_COMPLEMENT = str.maketrans("ACGTUacgtu", "TGCAAACCAA")
def calculate_gc(sequence):
"""Calculates GC percentage of any nucleotide sequence."""
seq = sequence.upper()
g = seq.count("G")
c = seq.count("C")
return (g + c) / len(seq) * 100 if len(seq) > 0 else 0.0
def find_orfs(sequence, min_protein_len=50):
"""Identifies candidate Open Reading Frames starting with ATG."""
orfs = []
seq = sequence.upper()
for frame in range(3):
for i in range(frame, len(seq) - 2, 3):
if seq[i:i+3] == "ATG":
for j in range(i + 3, len(seq) - 2, 3):
if seq[j:j+3] in {"TAA", "TAG", "TGA"}:
orf_seq = seq[i:j+3]
if len(orf_seq) // 3 >= min_protein_len:
orfs.append((i, j+3, orf_seq))
break
return orfs2. Importing Modules in Scripts
Python provides several clean syntaxes to import modules and specific functions into your analysis workflows:
# Approach 1: Import entire module
import seq_tools
gc = seq_tools.calculate_gc("ATGCGCGCTAGC")
# Approach 2: Import specific functions
from seq_tools import calculate_gc, find_orfs
orfs = find_orfs("ATGGGATCGTAGCTAGCTAGCTAA")
# Approach 3: Import with namespace alias
import seq_tools as st
print("GC Content:", st.calculate_gc("CCCGGG"))Python comes bundled with robust built-in modules critical for bioinformaticians:
os&pathlib: Path manipulation, directory traversal, and automated file discovery.gzip: Direct reading and writing of compressed.fastq.gzand.vcf.gzstreams.collections.Counter: Ultra-fast k-mer and base frequency counting.itertools: Generating codon combinations, sliding windows, and pairwise sequence comparisons.
3. Using Modules Across Multiple Files & Packages
A package is a directory containing multiple modules and an __init__.py file. Here is the recommended directory structure for a production bioinformatics tool:
my_ngs_tool/
├── __init__.py # Marks directory as Python package
├── config.py # Pipeline parameters, paths, thresholds
├── parsers/
│ ├── __init__.py
│ ├── fasta.py # FASTA & FASTQ reader/writer
│ └── vcf.py # Variant Call Format processor
├── algorithms/
│ ├── __init__.py
│ ├── alignment.py # Smith-Waterman / Needleman-Wunsch
│ └── filtering.py # Depth, Quality & Allele Frequency filters
└── main.py # Command-line entrypoint (argparse/click)