import gradio as gr from Bio import PDB import requests import os import pandas as pd # Function to download and parse the PDB file def fetch_and_parse_pdb(url): # Download the PDB file response = requests.get(url) if response.status_code != 200: return "Failed to download the PDB file. Please check the URL." # Save the file locally pdb_filename = url.split("/")[-1] with open(pdb_filename, 'wb') as f: f.write(response.content) # Parse the PDB file parser = PDB.PDBParser() structure = parser.get_structure(pdb_filename, pdb_filename) # Prepare the header information for output headers = structure.header header_items = [] for key, value in headers.items(): if isinstance(value, list): for item in value: header_items.append((key, item)) else: header_items.append((key, value)) # Convert to DataFrame for better formatting df = pd.DataFrame(header_items, columns=['Header', 'Content']) # Clean up the downloaded file os.remove(pdb_filename) return df # Create Gradio interface iface = gr.Interface( fn=fetch_and_parse_pdb, inputs="text", outputs=gr.Dataframe(interactive=True), title="PDB Metadata Explorer", description="Input the URL of a Protein Data Bank (PDB) file and retrieve its metadata" ) # Launch the interface iface.launch()