Skip to content

Commit

Permalink
add rna_tools/rna_pdb_fetch_header.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagnus committed Dec 4, 2024
1 parent 0b1002e commit 3924a56
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
91 changes: 91 additions & 0 deletions rna_tools/rna_pdb_fetch_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
"""
from __future__ import print_function
import argparse
from icecream import ic
import sys
ic.configureOutput(outputFunction=lambda *a: print(*a, file=sys.stderr))
ic.configureOutput(prefix='> ')


def get_parser():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
#parser.add_argument('-', "--", help="", default="")
parser.add_argument("-v", "--verbose",
action="store_true", help="be verbose")
parser.add_argument("file", help="", default="", nargs='+')
return parser

import requests

def fetch_pdb_header(pdb_id):
"""
Fetches the header information for a given PDB ID using the RCSB PDB GraphQL API.
:param pdb_id: The PDB ID to query.
:return: A dictionary containing the header information or an error message.
"""
# Define the GraphQL query
query = """
query getPDBHeader($id: String!) {
entry(entry_id: $id) {
rcsb_id
struct {
title
}
exptl {
method
}
rcsb_accession_info {
deposit_date
initial_release_date
}
citation {
title
rcsb_authors
year
pdbx_database_id_DOI
}
}
}
"""

# Define the variables
variables = {"id": pdb_id}

# API endpoint
url = "https://data.rcsb.org/graphql"
headers = {"Content-Type": "application/json"}

try:
# Send the POST request
response = requests.post(url, json={"query": query, "variables": variables}, headers=headers)
response.raise_for_status() # Raise HTTPError for bad responses (4xx and 5xx)
data = response.json()

# Return the data or error
if "data" in data:
return data["data"]
else:
return {"error": "No data returned. Check the PDB ID or API response."}

except requests.exceptions.RequestException as e:
return {"error": str(e)}


if __name__ == '__main__':
parser = get_parser()
args = parser.parse_args()

if list != type(args.file):
args.file = args.file

for pdb_id in [f.replace('.pdb', '').replace('.cif', '') for f in args.file]:
header_info = fetch_pdb_header(pdb_id)
title = header_info.get('entry', {}).get('struct', {}).get('title', 'Title not found')
# Print the extracted title
print(pdb_id, title)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'rna_tools/rna_standardize.py',
'rna_tools/rna_pdb_inspect.py',
'rna_tools/rna_cif2pdb.py',
'rna_tools/rna_pdb_fetch_header.py',

'rna_tools/tools/misc/rna_tools_which.py',
'rna_tools/tools/misc/rna_tools_demo.py',
Expand Down

0 comments on commit 3924a56

Please sign in to comment.