Preloader
Others
  • Estimated reading time: 4 Minutes

Working With Protein Data in Python: UniProt and the Protein Data Bank

Working With Protein Data in Python: UniProt and the Protein Data Bank

Developers end up in bioinformatics more often than they expect — a research group needs a data pipeline, a health startup needs to cross-reference targets, or a side project turns into something that has to read real biological data. The good news is that the two reference databases for proteins are both free, both have plain REST APIs, and neither needs an API key.

This article walks through fetching a protein record from UniProt, downloading its 3D structure from the Protein Data Bank, and parsing both in Python. The worked example is human interferon beta throughout, so you can run every snippet as written and compare output.

What you need

Two libraries: requests for the HTTP calls and biopython for parsing structure files.

pip install requests biopython

If you are new to making HTTP calls in Python, Our Code World's step-by-step web scraping tutorial covers requests and response handling in more depth than we will here.

Fetching a protein record from UniProt

UniProt is the reference database for protein sequence and annotation. Every entry has a stable accession, which is the ID you build your code around — protein names change, accessions do not. Human interferon beta is P01574.

The REST API returns JSON by appending .json to the accession:

import requests

ACCESSION = "P01574"  # human interferon beta
url = f"https://rest.uniprot.org/uniprotkb/{ACCESSION}.json"

response = requests.get(url, timeout=30)
response.raise_for_status()
entry = response.json()

print(entry["uniProtkbId"])
print(entry["proteinDescription"]["recommendedName"]["fullName"]["value"])
print(entry["genes"][0]["geneName"]["value"])
print(len(entry["sequence"]["value"]), "residues")

That prints:

IFNB_HUMAN
Interferon beta
IFNB1
187 residues

The response has seventeen top-level keys. The ones you will reach for most are proteinDescription, genes, sequence, features (annotated regions such as signal peptides and binding sites), and uniProtKBCrossReferences, which is how you find linked structures in other databases.

If you only want the sequence, there is a FASTA endpoint that saves you parsing JSON:

fasta = requests.get(
    f"https://rest.uniprot.org/uniprotkb/{ACCESSION}.fasta", timeout=30
).text
print(fasta.splitlines()[0])

Downloading a structure from the PDB

UniProt gives you the sequence. For the three-dimensional shape you want the Protein Data Bank, where each experimentally solved structure has a four-character ID. The crystal structure of human interferon beta is 1AU1, solved by X-ray diffraction at 2.20 Å.

Structure files download over plain HTTP, no library needed:

PDB_ID = "1AU1"
url = f"https://files.rcsb.org/download/{PDB_ID}.pdb"

response = requests.get(url, timeout=60)
response.raise_for_status()

with open(f"{PDB_ID}.pdb", "wb") as handle:
    handle.write(response.content)

Parsing the structure with Biopython

A PDB file is a fixed-width text format with decades of accumulated quirks. Do not parse it by hand — Biopython's parser handles the edge cases:

from Bio.PDB import PDBParser

parser = PDBParser(QUIET=True)
structure = parser.get_structure("ifnb", "1AU1.pdb")

for chain in structure[0]:
    residues = [r for r in chain if r.id[0] == " "]
    print(chain.id, len(residues), "residues")

Two details in that loop matter. structure[0] selects the first model, which is what you want for X-ray structures; NMR structures contain many models, and iterating the whole structure would process all of them. The filter on r.id[0] == " " keeps only standard amino acid residues and drops water molecules and bound ligands, which Biopython also returns as residues.

Why the two residue counts disagree

Run both scripts and the numbers will not match. UniProt reports 187 residues; the structure contains fewer. This catches almost everyone the first time.

UniProt gives the full translated protein, including the signal peptide — the short leading sequence that directs a protein out of the cell and is cut off in the process. Interferon beta's signal peptide is 21 residues, so the mature protein is 166. A crystal structure is solved from the mature, secreted protein, and residues too disordered to resolve are missing from the file as well.

So there are three different numbers in play: the translated length, the mature length, and the number of residues actually observed. If your pipeline maps between sequence positions and structure positions, this offset is the first thing to check when the alignment looks wrong. The features array in the UniProt response tells you exactly where the signal peptide ends.

Where the code stops and the bench starts

Everything above is read-only analysis. At some point a computational shortlist has to be tested experimentally, and that needs the physical protein rather than its coordinates.

It is worth noting how the example structure was made: 1AU1 was expressed in Chinese hamster ovary cells, not in bacteria. Interferon beta is glycosylated and disulfide-bonded, so a mammalian system is needed to produce a correctly folded protein — which is also why commercial interferon beta, sold among recombinant cytokines, is produced in CHO cells. The expression system is not an incidental detail; it determines whether the protein behaves like the real thing in an assay.

Wrapping up

Two endpoints and one parsing library cover a surprising amount of protein work: rest.uniprot.org for sequence and annotation, files.rcsb.org for structures, and Biopython to read them. Build on accessions and PDB IDs rather than names, remember that sequence numbering and structure numbering are not the same thing, and cache responses locally so you are not re-fetching the same entry on every run.

From there the obvious next steps are the features array for annotated regions, cross-references for finding every structure of a given protein, and — when the analysis points at a target worth testing — a supplier such as Beta LifeScience for the protein itself.

Related articles
The Silicon Dollar Debate: What Pax Silica and the Data Show
30 Nov, 2026
  • Estimated reading time: 7 Minutes
Before You Lose Your iPhone: A 10-Minute Setup
30 Nov, 2026
  • Estimated reading time: 5 Minutes
What Pushes People to Unsubscribe from Service Email Lists?
30 Nov, 2026
  • Estimated reading time: 3 Minutes
Weekly trending
The Silicon Dollar Debate: What Pax Silica and the Data Show
30 Nov, 2026
  • Estimated reading time: 7 Minutes
Before You Lose Your iPhone: A 10-Minute Setup
30 Nov, 2026
  • Estimated reading time: 5 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.