cg2all / app.py
huhlim's picture
Update app.py
31efa71
import gradio as gr
import cg2all
import os
def read_mol(molpath):
with open(molpath, "r") as fp:
lines = fp.readlines()
mol = ""
for l in lines:
mol += l
#
mol = mol.replace("OT1", "O ")
mol = mol.replace("OT2", "OXT")
return mol
def molecule(input_pdb):
mol = read_mol(input_pdb)
x = (
"""<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style>
body{
font-family:sans-serif
}
.mol-container {
width: 100%;
height: 600px;
position: relative;
}
.mol-container select{
background-image:None;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
</head>
<body>
<div id="container" class="mol-container"></div>
<script>
let pdb = `"""
+ mol
+ """`
$(document).ready(function () {
let element = $("#container");
let config = { backgroundColor: "white" };
let viewer = $3Dmol.createViewer(element, config);
viewer.addModel(pdb, "pdb");
viewer.setStyle({}, {cartoon: { color:"spectrum"}});
viewer.addStyle({"and":[{resn:["GLY","PRO"], invert:true}, {atom:["N","C","O"], invert:true}]}, {stick: {radius:0.2, colorscheme:"WhiteCarbon"}});
viewer.addStyle({"and":[{resn:"GLY"}, {atom:"CA"}]}, {stick: {radius:0.2, colorscheme:"WhiteCarbon"}});
viewer.addStyle({"and":[{resn:"PRO"}, {atom:["C","O"], invert:true}]}, {stick: {radius:0.2, colorscheme:"WhiteCarbon"}});
viewer.zoomTo();
viewer.render();
viewer.zoom(0.8, 2000);
})
</script>
</body></html>"""
)
return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
allow-scripts allow-same-origin allow-popups
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
def runner(in_pdb, model_type):
out_fn = in_pdb.name[:-4] + "-all.pdb"
ckpt_fn = f"model/{model_type}.ckpt"
cg2all.convert_cg2all(in_pdb.name, out_fn, model_type=model_type, ckpt_fn=ckpt_fn)
view = molecule(out_fn)
return out_fn, view
with gr.Blocks() as app:
gr.Markdown(
"# cg2all: conversion of coarse-grained protein structure model to all-atom structure"
)
with gr.Row():
with gr.Column():
input_pdb = gr.File(
file_count="single",
label="Input CG structure",
file_types=[".pdb", ".PDB", ".txt", ".TXT"],
)
model_type = gr.Radio(
[
"CalphaBasedModel",
"ResidueBasedModel",
"SidechainModel",
"CalphaCMModel",
"CalphaSCModel",
"BackboneModel",
"MainchainModel",
"Martini",
"Martini3",
"PRIMO",
],
label="Input CG model type",
)
#
button = gr.Button("Run")
#
gr.Examples(
[
["inputs/1ab1_A.calpha.pdb", "CalphaBasedModel"],
["inputs/1ab1_A.residue.pdb", "ResidueBasedModel"],
["inputs/1ab1_A.sc.pdb", "SidechainModel"],
["inputs/1ab1_A.cacm.pdb", "CalphaCMModel"],
["inputs/1ab1_A.casc.pdb", "CalphaSCModel"],
["inputs/1ab1_A.bb.pdb", "BackboneModel"],
["inputs/1ab1_A.mc.pdb", "MainchainModel"],
["inputs/1ab1_A.martini.pdb", "Martini"],
["inputs/1ab1_A.martini3.pdb", "Martini3"],
["inputs/1ab1_A.primo.pdb", "PRIMO"],
],
[input_pdb, model_type],
label="Monomeric coarse-grained structure",
)
gr.Examples(
[
["inputs/Q9EP54.sample.pdb", "CalphaBasedModel"],
],
[input_pdb, model_type],
label="ML(idpGAN)-generated IDP structure",
)
gr.Examples(
[
["inputs/3iyg.pdb", "CalphaBasedModel"],
],
[input_pdb, model_type],
label="Multimeric medium-resolution cryo-EM structure",
)
gr.Examples(
[
["inputs/LAF1rgg.sample.pdb", "CalphaBasedModel"],
],
[input_pdb, model_type],
label="Snapshot of COCOMO simulation of LLPS",
)
with gr.Column():
output_pdb = gr.File(file_count="single", label="Output structure")
viewer = gr.HTML()
button.click(fn=runner, inputs=[input_pdb, model_type], outputs=[output_pdb, viewer])
#
gr.Markdown("---")
gr.Markdown(
"### GitHub repository: [https://github.com/huhlim/cg2all](https://github.com/huhlim/cg2all)"
)
gr.Markdown("### Local installation: `pip install git+http://github.com/huhlim/cg2all`")
gr.Markdown("### Supported coarse-grained models")
gr.Markdown("- CalphaBasedModel: CA-trace")
gr.Markdown("- ResidueBasedModel: Residue center-of-mass")
gr.Markdown("- SidechainModel: Sidechain center-of-mass")
gr.Markdown("- CalphaCMModel: CA-trace + Residue center-of-mass")
gr.Markdown("- CalphaSCModel: CA-trace + Sidechain center-of-mass")
gr.Markdown("- BackboneModel: Backbone N, CA, and C atoms")
gr.Markdown("- MainchainModel: Backbone N, CA, C, and O atoms")
gr.Markdown("- Martini: [Martini model](http://cgmartini.nl)")
gr.Markdown("- Martini3: [Martini3 model](http://www.cgmartini.nl/index.php/martini-3-0)")
gr.Markdown("- PRIMO: [PRIMO model](https://dx.doi.org/10.1002/prot.22645)")
gr.Markdown("### Cite: TODO")
app.launch()