5 Paths · 122 Lessons

Learn Python for Bioinformatics.

Work through NumPy, Pandas, Biopython, and HGVS in a structured course. Every lesson is an interactive notebook, and the Python beside this sentence is running in your browser right now — nothing to install.

Python 3 · Ready1 ms
records = {}
name = None

with open("sample.fasta") as handle:
    for line in handle:
        line = line.strip()
        if line.startswith(">"):
            name = line[1:].split()[0]
            records[name] = ""
        elif name:
            records[name] += line

for name, seq in records.items():
    gc = (seq.count("G") + seq.count("C")) / len(seq)
    print(f"{name:<18} {len(seq):>4} bp   GC {gc:.1%}")
stdout
NM_000546.6        120 bp   GC 52.5%
NM_007294.4        120 bp   GC 40.8%
ENST00000646891.2  120 bp   GC 79.2%