UniProt Protein Database Query
Query the UniProt REST API for protein information.
When to Use
- User asks about a protein's function, sequence, or annotation
- User provides a gene name and wants protein info
- User needs protein accession IDs
- User asks "what does gene X do" (protein level)
How to Execute
import requests
import json
BASE_URL = "https://rest.uniprot.org"
# 1. Search by gene name (default: human, reviewed/Swiss-Prot)
def search_uniprot(gene_name, organism_id=9606, max_results=5):
url = f"{BASE_URL}/uniprotkb/search"
params = {
"query": f"gene_exact:{gene_name} AND organism_id:{organism_id} AND reviewed:true",
"format": "json",
"size": max_results,
"fields": "accession,id,gene_names,protein_name,organism_name,length,cc_function,ft_domain,sequence"
}
r = requests.get(url, params=params)
r.raise_for_status()
return r.json()
# 2. Get by accession ID
def get_uniprot_entry(accession):
url = f"{BASE_URL}/uniprotkb/{accession}.json"
r = requests.get(url)
r.raise_for_status()
return r.json()
# 3. Get FASTA sequence
def get_fasta(accession):
url = f"{BASE_URL}/uniprotkb/{accession}.fasta"
r = requests.get(url)
r.raise_for_status()
return r.text
# Example usage
data = search_uniprot("TP53")
for entry in data.get("results", []):
acc = entry["primaryAccession"]
name = entry.get("proteinDescription", {}).get("recommendedName", {}).get("fullName", {}).get("value", "N/A")
gene = entry.get("genes", [{}])[0].get("geneName", {}).get("value", "N/A")
length = entry.get("sequence", {}).get("length", "N/A")
# Extract function
functions = [c["texts"][0]["value"] for c in entry.get("comments", []) if c["commentType"] == "FUNCTION"]
func_text = functions[0][:200] if functions else "N/A"
print(f"Accession: {acc}")
print(f"Protein: {name}")
print(f"Gene: {gene}")
print(f"Length: {length} aa")
print(f"Function: {func_text}")
Common Search Patterns
- By gene:
gene_exact:BRCA1 AND organism_id:9606
- By keyword:
keyword:kinase AND organism_id:9606
- By disease:
cc_disease:cancer AND organism_id:9606
- By GO term:
go:apoptosis AND organism_id:9606
- Species IDs: Human=9606, Mouse=10090, Rat=10116, Zebrafish=7955, Fly=7227, Yeast=559292
Output Format
Present: Accession, protein name, gene, organism, length, function summary, and UniProt link.
Follow-up Suggestions
- "Want me to get the AlphaFold structure for this protein?"
- "Should I check protein-protein interactions on STRING?"
- "Want me to BLAST this protein sequence?"
1---2name: query-uniprot3description: Query UniProt protein database. Use when user asks about protein sequences, functions, annotations, domains, or protein identifiers. Triggers on "uniprot", "protein function", "protein sequence", "gene product", "protein info".4---5
6# UniProt Protein Database Query
7
8Query the UniProt REST API for protein information.
9
10## When to Use
11
12- User asks about a protein's function, sequence, or annotation
13- User provides a gene name and wants protein info
14- User needs protein accession IDs
15- User asks "what does gene X do" (protein level)
16
17## How to Execute
18
19```python
20import requests
21import json
22
23BASE_URL = "https://rest.uniprot.org"
24
25# 1. Search by gene name (default: human, reviewed/Swiss-Prot)
26def search_uniprot(gene_name, organism_id=9606, max_results=5):
27 url = f"{BASE_URL}/uniprotkb/search"
28 params = {
29 "query": f"gene_exact:{gene_name} AND organism_id:{organism_id} AND reviewed:true",
30 "format": "json",
31 "size": max_results,
32 "fields": "accession,id,gene_names,protein_name,organism_name,length,cc_function,ft_domain,sequence"
33 }
34 r = requests.get(url, params=params)
35 r.raise_for_status()
36 return r.json()
37
38# 2. Get by accession ID
39def get_uniprot_entry(accession):
40 url = f"{BASE_URL}/uniprotkb/{accession}.json"
41 r = requests.get(url)
42 r.raise_for_status()
43 return r.json()
44
45# 3. Get FASTA sequence
46def get_fasta(accession):
47 url = f"{BASE_URL}/uniprotkb/{accession}.fasta"
48 r = requests.get(url)
49 r.raise_for_status()
50 return r.text
51
52# Example usage
53data = search_uniprot("TP53")
54for entry in data.get("results", []):
55 acc = entry["primaryAccession"]
56 name = entry.get("proteinDescription", {}).get("recommendedName", {}).get("fullName", {}).get("value", "N/A")
57 gene = entry.get("genes", [{}])[0].get("geneName", {}).get("value", "N/A")
58 length = entry.get("sequence", {}).get("length", "N/A")
59
60 # Extract function
61 functions = [c["texts"][0]["value"] for c in entry.get("comments", []) if c["commentType"] == "FUNCTION"]
62 func_text = functions[0][:200] if functions else "N/A"
63
64 print(f"Accession: {acc}")
65 print(f"Protein: {name}")
66 print(f"Gene: {gene}")
67 print(f"Length: {length} aa")
68 print(f"Function: {func_text}")
69```
70
71## Common Search Patterns
72
73- By gene: `gene_exact:BRCA1 AND organism_id:9606`
74- By keyword: `keyword:kinase AND organism_id:9606`
75- By disease: `cc_disease:cancer AND organism_id:9606`
76- By GO term: `go:apoptosis AND organism_id:9606`
77- Species IDs: Human=9606, Mouse=10090, Rat=10116, Zebrafish=7955, Fly=7227, Yeast=559292
78
79## Output Format
80
81Present: Accession, protein name, gene, organism, length, function summary, and UniProt link.
82
83## Follow-up Suggestions
84
85- "Want me to get the AlphaFold structure for this protein?"
86- "Should I check protein-protein interactions on STRING?"
87- "Want me to BLAST this protein sequence?"