app.py 2.9 KB
Newer Older
1 2 3
import gradio as gr
import os

4

5 6 7 8
def molecule(input_pdb):

    mol = read_mol(input_pdb)

9
    x = ("""<!DOCTYPE html>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        <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://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
27
    <script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
28 29 30 31 32
    </head>
    <body>  
    <div id="container" class="mol-container"></div>
  
            <script>
33
               let pdb = `""" + mol + """`  
34 35 36 37 38 39 40 41 42 43 44 45
      
             $(document).ready(function () {
                let element = $("#container");
                let config = { backgroundColor: "white" };
                let viewer = $3Dmol.createViewer(element, config);
                viewer.addModel(pdb, "pdb");
                viewer.getModel(0).setStyle({}, { cartoon: { color:"spectrum" } });
                viewer.zoomTo();
                viewer.render();
                viewer.zoom(1, 1000); /* slight zoom */
              })
        </script>
46
        </body></html>""")
47 48 49 50 51 52 53

    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>"""

54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
def get_pdb(pdb_code="", filepath=""):
    if pdb_code is None or pdb_code == "":
        try:
            return filepath.name
        except AttributeError as e:
            return None
    else:
        os.system(f"wget -qnc https://files.rcsb.org/view/{pdb_code}.pdb")
        return f"{pdb_code}.pdb"


def read_mol(molpath):
    with open(molpath, "r") as fp:
        lines = fp.readlines()
    mol = ""
    for l in lines:
        mol += l
    return mol


75 76 77
def update(fastaName='', fastaContent=''):
    if (fastaName == ''):
        return None
78
    else:
79
        return molecule(fastaName + "_pred.pdb")
80 81 82 83


demo = gr.Blocks()
with demo:
84
    gr.Markdown("# PP-HelixFold Demo")
85 86
    with gr.Row():
        with gr.Box():
87 88 89 90 91
            fastaName = gr.Textbox(interactive=False, label='Fasta label')
            fastaContent = gr.Textbox(interactive=False, label='Fasta content')
            gr.Examples([["T1026", read_mol("T1026.fasta")],
                         ["T1037", read_mol("T1037.fasta")]],
                        [fastaName, fastaContent])
92
            btn = gr.Button("Predict")
93
    mol = gr.HTML()
94
    btn.click(fn=update, inputs=[fastaName, fastaContent], outputs=mol)
D
DanielYang 已提交
95
demo.launch()