File size: 7,146 Bytes
a05c989
52bf9df
 
 
b3bd0e4
6d9179c
52bf9df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92263a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52bf9df
d600a06
52bf9df
 
 
 
 
 
 
 
a05c989
 
 
 
d145f05
6d9179c
a05c989
 
 
 
 
 
 
 
 
 
 
 
 
7d61a8d
aebc0d2
 
a05c989
7d61a8d
 
 
 
a05c989
 
 
fffb501
a05c989
 
 
d600a06
a05c989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52bf9df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92263a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52bf9df
 
 
 
 
92263a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
INITIAL_RENDERING_TEMPLATE = """<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <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.org/build/3Dmol.js"></script>
        <style>
            .mol-container {{
                width: 600px;
                height: 600px;
                position: relative;
            }}
            .mol-container select{{
                background-image:None;
            }}
        </style>
    </head>

    <body>
    <div id="container" class="mol-container"></div>
    <script>
        $(document).ready(function() {{
            let element = $("#container");
            let config = {{ backgroundColor: "white" }};
            let viewer = $3Dmol.createViewer(element, config);
            let defaultStyle = {{ stick: {{ colorscheme: "greenCarbon" }} }};
            viewer.addModel(`{molecule}`, "{fmt}");
            viewer.getModel(0).setStyle(defaultStyle);
            // document.cookie = document.cookie + "|selected_atoms:";
            
            viewer.getModel(0).setClickable(
                {{}}, 
                true,
                function (_atom, _viewer, _event, _container) {{
                    if (!_atom.isClicked) {{
                        _atom.isClicked = true;
                        _viewer.addStyle(
                            {{"serial": _atom.serial, "model": 0}}, 
                            {{"sphere": {{"color": "magenta", "radius": 0.4}} }}
                        );
                        // document.cookie = document.cookie + "atom_" + String(_atom.serial) + "-";
                        window.parent.postMessage({{
                            name: "atom_selection",
                            data: {{"atom": _atom.serial, "add": true}} 
                            // data: JSON.stringify({{"add": _atom.serial}})
                        }}, "*");
                    }} else {{
                        delete _atom.isClicked;
                        _viewer.setStyle({{"serial": _atom.serial, "model": 0}}, defaultStyle);
                        // document.cookie = document.cookie.replace("atom_" + String(_atom.serial) + "-", "");
                        window.parent.postMessage({{
                            name: "atom_selection",
                            data: {{"atom": _atom.serial, "add": false}} 
                            // data: JSON.stringify({{"remove": _atom.serial}})
                        }}, "*");
                    }}
                    _viewer.render();
                }}
            );
            
            viewer.zoomTo();
            viewer.zoom(0.7);
            viewer.render();
        }});
    </script>
    </body>
</html>
"""


SAMPLES_RENDERING_TEMPLATE = """<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <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.org/build/3Dmol.js"></script>
        <style>
            .mol-container {{
                width: 600px;
                height: 600px;
                position: relative;
            }}
            .mol-container select{{
                background-image:None;
            }}
        </style>
    </head>

    <body>
    <div id="container" class="mol-container"></div>
    <button id="fragments">Input Fragments</button>
    <button id="molecule">Output Molecule</button> 
    <script>
        let element = $("#container");
        let config = {{ backgroundColor: "white" }};
        let viewer = $3Dmol.createViewer( element, config );
        
        $(document).ready(function() {{
            viewer.addModel(`{fragments}`, "{fragments_fmt}")
            viewer.getModel().setStyle({{ stick: {{ colorscheme:"greenCarbon" }} }})
            viewer.getModel().hide();
            viewer.addModel(`{molecule}`, "{molecule_fmt}")
            viewer.getModel().setStyle({{ stick: {{ colorscheme:"greenCarbon" }} }})
            viewer.zoomTo();
            viewer.zoom(0.7);
            viewer.render();
        }});
        $("#fragments").click(function() {{
            viewer.getModel(0).show();
            viewer.getModel(1).hide();
            viewer.render();
        }});
        $("#molecule").click(function() {{
            viewer.getModel(1).show();
            viewer.getModel(0).hide();
            viewer.render();
        }});
    </script>
    </body>
</html>
"""


INVALID_FORMAT_MSG = """
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <style>
            body{{
                font-family:sans-serif
            }}
        </style>
    </head>

    <body>
        <h3>Invalid file format: {extension}</h3>
        Please upload the file in one of the following formats:
        <ul>
          <li>.pdb</li>
          <li>.sdf</li>
          <li>.mol</li>
          <li>.mol2</li>
        </ul>
    </body>
</html>
"""

ERROR_FORMAT_MSG = """
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <style>
            body{{
                font-family:sans-serif
            }}
        </style>
    </head>

    <body>
        <h3>Error:</h3>
        {message}
    </body>
</html>
"""


IFRAME_TEMPLATE = """<iframe style="width: 100%; height: 700px" 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='{html}'></iframe>"""


STARTUP_JS = """
() => {
    window.selected_elements = {}
    
    function handleMessage(event) {
        // console.log("New message: ", event.data)
        let atom = event.data.data["atom"];
        let add = event.data.data["add"];
        console.log("add: ", add, " atom: ", atom);
        window.selected_elements[atom] = add;
    }
    
    window.addEventListener("message", handleMessage);
    console.log("Listener Added"); 
}
"""

RETURN_SELECTION_JS = """
(input_file, n_steps, n_atoms, samples, hidden) => {
    let selected = []
    for (const [atom, add] of Object.entries(window.selected_elements)) {
      if (add) {
        console.log("Adding atom ", atom);
        selected.push(String(atom));
        window.parent.postMessage({
          name: "atom_selection",
          data: {"atom": parseInt(atom), "add": false} 
        }, "*");
      }
    }
    console.log("Finished parsing");
    return [input_file, n_steps, n_atoms, samples, selected.join(",")];
}
"""