Spaces:
Running
Running
| import os | |
| import json | |
| import gradio as gr | |
| from gradio_moleculeview import moleculeview | |
| import cellscape | |
| def predict(input_mol, style, view_str, chains): | |
| # write view to file | |
| with open("view_matrix", "w") as f: | |
| f.write(json.loads(view_str)) | |
| chain_str = "" | |
| chain_dict = json.loads(chains) | |
| # sort keys in dict and add colors to chain_str | |
| for chain in sorted(chain_dict.keys()): | |
| chain_str += f" '{chain_dict[chain]}'" | |
| if style == "Goodsell3D": | |
| os.system(f"cellscape cartoon --pdb {input_mol.name} --outline residue --depth_shading --depth_lines --colors {chain_str} --depth flat --back_outline --view view_matrix --save outline_all.svg") | |
| elif style == "Contour": | |
| os.system(f"cellscape cartoon --pdb {input_mol.name} --outline residue --depth_contour_interval 10 --colors {chain_str} --depth contours --back_outline --view view_matrix --save outline_all.svg") | |
| else: | |
| os.system(f"cellscape cartoon --pdb {input_mol.name} --outline chain --colors {chain_str} --depth flat --back_outline --view view_matrix --save outline_all.svg") | |
| #read content of file | |
| with open("outline_all.svg", "r") as f: | |
| return f.read(), "outline_all.svg" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# PDB2Vector") | |
| style = gr.Radio(value="Flat", choices=["Flat", "Contour", "Goodsell3D"], label="Style") | |
| inp = moleculeview(label="Molecule3D") | |
| view_str = gr.Textbox("viewMatrixResult", label="View Matrix", visible=False) | |
| chains = gr.Textbox("chainsResult", label="Chains", visible=False) | |
| hidden_style = gr.Textbox(visible=False) | |
| btn = gr.Button("Vectorize") | |
| html = gr.HTML("") | |
| out_file = gr.File(label="Download SVG") | |
| btn.click(None, style, [view_str, chains, hidden_style], js="(style) => [document.getElementById('viewMatrixResult').value, document.getElementById('chains').value, style]") # | |
| # on change of chains trigger, rendering | |
| view_str.change(predict, [inp, style, view_str, chains], [html, out_file]) | |
| chains.change(predict, [inp, style, view_str, chains], [html, out_file]) | |
| hidden_style.change(predict, [inp, style, view_str, chains], [html, out_file]) | |
| if __name__ == "__main__": | |
| demo.launch() | |