Skip to content

Instantly share code, notes, and snippets.

@whitead
Last active May 2, 2022 08:38
Show Gist options
  • Save whitead/00416b2241942b8eff578b81c0907010 to your computer and use it in GitHub Desktop.
Save whitead/00416b2241942b8eff578b81c0907010 to your computer and use it in GitHub Desktop.
Here's a python function that goes from search string (like "human albumin") and returns a pdb file using @rcsbPDB's top result.
import requests
import tempfile
def get_pdb(query_string):
url = "https://search.rcsb.org/rcsbsearch/v1/query"
query = {
"query": {
"type": "terminal",
"service": "full_text",
"parameters": {"value": query_string},
},
"return_type": "entry",
}
r = requests.post(url, json=query)
if "result_set" in r.json() and len(r.json()["result_set"]) > 0:
pdbid = r.json()["result_set"][0]["identifier"]
print('Fetched PDB', pdbid)
url = f"https://files.rcsb.org/download/{pdbid}.pdb"
pdb = requests.get(url)
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pdb")
tmp.write(pdb.text.encode())
return tmp
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment