Homologous gene comparisons are a core task in comparative genomics. A practical workflow usually starts with downloading related sequences, checking quality, and aligning pairs before deeper analyses like tree building. In this tutorial, you will download homologous [nucleotide sequences](/tutorials/biopython-nucleotide-sequences) and align them with Biopython. ## Downloading Homologous Gene Sequences
import requests
from Bio import SeqIO
# Download three homologous nucleotide sequences from related plant examples
urls = {
"lupine.nu": "https://raw.githubusercontent.com/biopython/biopython/master/Tests/Fasta/lupine.nu",
"lavender.nu": "https://raw.githubusercontent.com/biopython/biopython/master/Tests/Fasta/lavender.nu",
"phlox.nu": "https://raw.githubusercontent.com/biopython/biopython/master/Tests/Fasta/phlox.nu",
}
records = []
for filename, url in urls.items():
response = requests.get(url, timeout=30)
response.raise_for_status()
with open(filename, "w", encoding="utf-8") as f:
f.write(response.text)
records.append(SeqIO.read(filename, "fasta"))
print("Downloaded records:", [r.id for r in records])
print("Sequence lengths:", [len(r.seq) for r in records])This block downloads and stores homologous sequences locally so later alignment steps can be rerun without additional network calls. ## Pairwise Global Alignment of Homologs
from Bio import SeqIO
from Bio.Align import PairwiseAligner
# Read local FASTA records
lupine = SeqIO.read("lupine.nu", "fasta")
lavender = SeqIO.read("lavender.nu", "fasta")
# Configure a global aligner for homolog comparison
aligner = PairwiseAligner()
aligner.mode = "global"
aligner.match_score = 2
aligner.mismatch_score = -1
aligner.open_gap_score = -3
aligner.extend_gap_score = -1
score = aligner.score(str(lupine.seq), str(lavender.seq))
alignment = aligner.align(str(lupine.seq), str(lavender.seq))[0]
print("Alignment score:", score)
print(alignment)Pairwise global alignment is a practical first comparison when you want to quantify overall similarity between two homologs. ## Ranking Homolog Similarity Across Multiple Pairs
from Bio import SeqIO
from Bio.Align import PairwiseAligner
from itertools import combinations
records = [
SeqIO.read("lupine.nu", "fasta"),
SeqIO.read("lavender.nu", "fasta"),
SeqIO.read("phlox.nu", "fasta"),
]
aligner = PairwiseAligner()
aligner.mode = "global"
aligner.match_score = 2
aligner.mismatch_score = -1
aligner.open_gap_score = -3
aligner.extend_gap_score = -1
pair_scores = []
for r1, r2 in combinations(records, 2):
score = aligner.score(str(r1.seq), str(r2.seq))
pair_scores.append((r1.id, r2.id, score))
pair_scores.sort(key=lambda x: x[2], reverse=True)
print("Pairwise homolog ranking:")
for item in pair_scores:
print(item)Ranking pairwise scores gives a practical similarity overview before selecting candidates for [phylogenetic tree construction](/tutorials/biopython-phylogenetic-trees). ## Preparing a Combined [FASTA](/tutorials/biopython-fasta-files) for Downstream MSA Tools
from Bio import SeqIO
records = [
SeqIO.read("lupine.nu", "fasta"),
SeqIO.read("lavender.nu", "fasta"),
SeqIO.read("phlox.nu", "fasta"),
]
SeqIO.write(records, "homologs_combined.fasta", "fasta")
print("Wrote homologs_combined.fasta")Exporting a combined FASTA is a practical handoff step to external MSA tools or collaborative workflows where one shared input file is needed.