test_server.py•4.03 kB
#!/usr/bin/env python3
import requests
import json
import sys
def test_uniprot_api():
"""Test the UniProt API endpoints directly to ensure they work as expected"""
print("Testing UniProt API...")
# Test search
print("\n1. Testing search endpoint")
query = "insulin"
url = "https://rest.uniprot.org/uniprotkb/search"
params = {
"query": query,
"format": "json",
"size": 2
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
print(f"✅ Search successful - found {len(data.get('results', []))} results")
# Get first result's ID for subsequent tests
if data.get('results'):
uniprot_id = data['results'][0]['primaryAccession']
print(f"Using UniProt ID for further tests: {uniprot_id}")
else:
print("❌ No results found, using default ID")
uniprot_id = "P01308" # Human insulin as fallback
except Exception as e:
print(f"❌ Search failed: {str(e)}")
uniprot_id = "P01308" # Human insulin as fallback
# Test get by ID
print("\n2. Testing get by ID endpoint")
url = f"https://rest.uniprot.org/uniprotkb/{uniprot_id}"
params = {
"format": "json"
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
print(f"✅ Get by ID successful - retrieved {data.get('proteinDescription', {}).get('recommendedName', {}).get('fullName', {}).get('value', '(Unknown)')}")
except Exception as e:
print(f"❌ Get by ID failed: {str(e)}")
# Test sequences
print("\n3. Testing sequence retrieval")
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
sequence = data.get("sequence", {}).get("value", "")
if sequence:
print(f"✅ Sequence retrieval successful - length: {len(sequence)}")
print(f"Sequence preview: {sequence[:50]}...")
else:
print("❌ Sequence retrieval failed: No sequence found")
except Exception as e:
print(f"❌ Sequence retrieval failed: {str(e)}")
# Test functions
print("\n4. Testing function retrieval")
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
function_count = 0
for annotation in data.get("uniProtKBCrossReferences", []):
if annotation.get("database") == "GO":
function_count += 1
comment_count = 0
for comment in data.get("comments", []):
if comment.get("commentType") == "FUNCTION":
comment_count += 1
if function_count > 0 or comment_count > 0:
print(f"✅ Function retrieval successful - GO terms: {function_count}, Function comments: {comment_count}")
else:
print("❌ Function retrieval failed: No functions found")
except Exception as e:
print(f"❌ Function retrieval failed: {str(e)}")
# Test structures
print("\n5. Testing structure retrieval")
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
structure_count = 0
for ref in data.get("uniProtKBCrossReferences", []):
if ref.get("database") == "PDB":
structure_count += 1
if structure_count > 0:
print(f"✅ Structure retrieval successful - PDB entries: {structure_count}")
else:
print("⚠️ No PDB structures found (this might be normal for some proteins)")
except Exception as e:
print(f"❌ Structure retrieval failed: {str(e)}")
print("\nAPI test completed.")
if __name__ == "__main__":
test_uniprot_api()