import csv csv.field_size_limit(131072 * 10) import gradio as gr from igfold import IgFoldRunner import os import random import base64 import socket import re from pathlib import Path def read_mol(molpath): try: with open(molpath, "r") as fp: return fp.read() except Exception as e: print(f"Error reading PDB file: {e}") return "" def molecule(input_pdb, h_seq, l_seq): try: mol = read_mol(input_pdb) if not mol: return "
Error: Failed to read PDB file
" byte_content = mol.encode('utf-8') base64_content = base64.b64encode(byte_content).decode('utf-8') x = f""" """ return f""" """ except Exception as e: print(f"Error in molecule visualization: {e}") return f"Error generating visualization: {str(e)}
" def validate(seq): alphabet = set('ACDEFGHIKLMNPQRSTVWY') return not (set(seq.upper()) - alphabet) def clean_sequence(seq): return re.sub(r'\s+', '', seq) def pred_seq(h_seq, l_seq): try: h_seq = clean_sequence(h_seq).upper() l_seq = clean_sequence(l_seq).upper() if not (validate(h_seq) and validate(l_seq)): return "Error: Invalid amino acid characters detected
" sequences = {"H": h_seq, "L": l_seq} f_name = f"temp_{random.randint(0, 100000)}.pdb" try: igfold = IgFoldRunner(num_models=1) igfold.fold(f_name, sequences=sequences, do_refine=False, do_renum=False) if not os.path.exists(f_name): return "Error: Failed to generate PDB file
" html = molecule(f_name, h_seq, l_seq) finally: if os.path.exists(f_name): os.remove(f_name) return html except Exception as e: print(f"Error in prediction: {e}") return f"Error: {str(e)}
" examples = [ ["QVQLKESGPGLVAPSQSLSITCTVSGFSLSSYGVSWVRQPPGKGLEWLGVIWGDGSTNYHPNLMSRLSISKDISKSQVLFKLNSLQTDDTATYYCVTLDYWGQGTSVTVSS", "DVVMTQTPLSLPVSLGDQASISCRSSQSLVHRNGNTYLHWYLQKPGQSPKLLIYKVSNRFSGVPDRFSGSGSGTDFTLKISRVEAEDLGLYFCFQTTYVPNTFGGGTKLEIK"], ["EVQLLESGGGLVQPGGSLRLSCAASGFTFSLYWMGWVRQAPGKGLEWVSSISSSGGVTPYADSVKGRFTISRDNSKNTLYLQMNSLRAEDTAVYYCAKLGELGWFDPWGQGTLVTVSS", "DIQMTQSPSSLSASVGDRVTITCRASQGISSYLNWYQQKPGKAPKLLIYYASNLQNGVPSRFSGSGSGTDFTLTISSLQPEDFATYYCQQSYSTPLTFGGGTKVEIK"] ] with gr.Blocks() as demo: gr.Markdown('# Antibody Structure Prediction') with gr.Row(): h_text = gr.Textbox(lines=5, label="Heavy chain", placeholder="Enter heavy chain sequence...") l_text = gr.Textbox(lines=5, label="Light chain", placeholder="Enter light chain sequence...") gr.Examples(examples=examples, inputs=[h_text, l_text], label="Example sequences") btn = gr.Button("Predict Structure") progress_bar = gr.HTML(""" """) output_html = gr.HTML() js_animation = """ """ gr.HTML(js_animation) def wrapper_pred_seq(h_seq, l_seq): import time progress_js = """ """ result = pred_seq(h_seq, l_seq) complete_js = """ """ return progress_js + result + complete_js btn.click(wrapper_pred_seq, inputs=[h_text, l_text], outputs=output_html) if __name__ == "__main__": demo.launch(show_error=True, server_name="0.0.0.0")