Spaces:
Running
Running
File size: 14,767 Bytes
8adb95d 10b1be2 55fdc9a 10b1be2 8adb95d 10b1be2 8adb95d 10b1be2 30f2e61 77daddd 1013f57 77daddd 1013f57 77daddd 30f2e61 77daddd 853bf92 77daddd 1013f57 77daddd 3c00078 853bf92 855e839 1013f57 853bf92 855e839 853bf92 855e839 853bf92 855e839 853bf92 855e839 853bf92 1013f57 853bf92 855e839 853bf92 30f2e61 91f2076 30f2e61 21cbe60 30f2e61 55fdc9a 30f2e61 55fdc9a 30f2e61 55fdc9a 30f2e61 55fdc9a 30f2e61 853bf92 55fdc9a 8adb95d 59e713c 660f406 59e713c 660f406 8adb95d 083f3dd 1365361 59e713c 8adb95d fef897d c5e4478 8adb95d c5e4478 601a92d 8adb95d |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
import os
import json
import uuid
import gradio as gr
from gradio_moleculeview import moleculeview
import cellscape
def html_output(input_file):
with open(input_file, "r") as f:
svg = f.read().replace("<svg", "<svg id='svgElement'")
x = (
"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
#snackbar {
visibility: hidden;
color: #fff;
background-color: #333;
min-width: 250px;
margin-left: -125px;
border-radius: 2px;
padding: 16px;
text-align: center;
left: 50%;
bottom: 30px;
z-index: 1;
position: fixed;
}
/* This will be activated when the snackbar's class is 'show' which will be added through JS */
#snackbar.show {
visibility: visible;
-webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
animation: fadein 0.5s, fadeout 0.5s 2.5s;
}
/* Animations for fading in and out */
@-webkit-keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@-webkit-keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
@keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
</style>
</head>
<body>
<div class="flex container mx-auto max-w-3xl">
<div class="w-1/2">
"""
+ svg
+ """
</div>
<div class="w-1/2 space-y-4 justify-center items-center flex flex-col">
<div class="w-3/4">
<div
class="w-full flex space-x-2 items-center text-xl justify-center font-light mb-3"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-4 h-4"
viewBox="0 0 16 16"
>
<path
fill="currentColor"
fill-rule="evenodd"
d="M6.091 5.194q.2-.322.305-.694h3.208c.259.916.98 1.637 1.896 1.896v3.208A2.76 2.76 0 0 0 9.604 11.5H6.396A2.76 2.76 0 0 0 4.5 9.604V6.396a2.76 2.76 0 0 0 1.591-1.202M13 6.396v3.208q.372.106.694.305a2.75 2.75 0 0 1 .897 3.785A2.751 2.751 0 0 1 9.603 13H6.397q-.106.372-.305.694A2.75 2.75 0 1 1 3 9.604V6.396A2.751 2.751 0 1 1 6.396 3h3.208A2.751 2.751 0 1 1 13 6.396M3.75 2.5a1.25 1.25 0 1 1 0 2.5a1.25 1.25 0 0 1 0-2.5M5 12.25a1.25 1.25 0 1 0-2.5 0a1.25 1.25 0 0 0 2.5 0M12.25 11a1.25 1.25 0 1 1 0 2.5a1.25 1.25 0 0 1 0-2.5m1.25-7.25a1.25 1.25 0 1 0-2.5 0a1.25 1.25 0 0 0 2.5 0"
clip-rule="evenodd"
/>
</svg>
<div>SVG</div>
</div>
<div class="w-full flex space-x-5">
<button
id="copySvgBtn"
class="flex items-center justify-center space-x-2 w-full p-3 border border-gray-300 rounded bg-gray-100"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-4 h-4"
viewBox="0 0 16 16"
>
<path
fill="currentColor"
fill-rule="evenodd"
d="M12 2.5H8A1.5 1.5 0 0 0 6.5 4v1H8a3 3 0 0 1 3 3v1.5h1A1.5 1.5 0 0 0 13.5 8V4A1.5 1.5 0 0 0 12 2.5M11 11h1a3 3 0 0 0 3-3V4a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v1H4a3 3 0 0 0-3 3v4a3 3 0 0 0 3 3h4a3 3 0 0 0 3-3zM4 6.5h4A1.5 1.5 0 0 1 9.5 8v4A1.5 1.5 0 0 1 8 13.5H4A1.5 1.5 0 0 1 2.5 12V8A1.5 1.5 0 0 1 4 6.5"
clip-rule="evenodd"
/>
</svg>
<div>Copy*</div>
</button>
<button
id="downloadSvgBtn"
class="w-full flex items-center justify-center space-x-2 p-3 border border-gray-300 rounded bg-gray-100"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
viewBox="0 0 16 16"
>
<path
fill="currentColor"
fill-rule="evenodd"
d="M6.5 6H4.029l3.499 4.276a.61.61 0 0 0 .944 0L11.971 6H9.5V2.75a.25.25 0 0 0-.25-.25h-2.5a.25.25 0 0 0-.25.25zm1.498 7.5H1.75a.75.75 0 0 0 0 1.5h12.5a.75.75 0 0 0 0-1.5zm0-1.5a2.11 2.11 0 0 1-1.631-.774L2.833 6.907A1.474 1.474 0 0 1 3.973 4.5H5V2.75C5 1.784 5.784 1 6.75 1h2.5c.966 0 1.75.784 1.75 1.75V4.5h1.026a1.474 1.474 0 0 1 1.14 2.407l-3.533 4.319c-.4.49-1 .774-1.632.774H8"
clip-rule="evenodd"
/>
</svg>
<div>Download</div>
</button>
</div>
<div class="text-xs mt-3 text-center w-full">
*Only works in in Adobe Illustrator.
</div>
<div
class="flex space-x-2 items-center text-xl justify-center font-light mb-3 mt-10"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-4 h-4"
viewBox="0 0 16 16"
>
<path
fill="currentColor"
fill-rule="evenodd"
d="M11.5 3h-7A1.5 1.5 0 0 0 3 4.5v5.027l.962-.7a1.75 1.75 0 0 1 2.079.016l.928.696l2.368-2.03a1.75 1.75 0 0 1 2.325.043L13 8.787V4.5A1.5 1.5 0 0 0 11.5 3m3 7.498V4.5a3 3 0 0 0-3-3h-7a3 3 0 0 0-3 3v7a3 3 0 0 0 3 3h7a3 3 0 0 0 3-3zm-1.5.33l-2.355-2.174a.25.25 0 0 0-.332-.006L7.488 11.07l-.457.392l-.481-.361l-1.41-1.057a.25.25 0 0 0-.296-.002L3 11.381v.119A1.5 1.5 0 0 0 4.5 13h7a1.5 1.5 0 0 0 1.5-1.5zM7.5 6a1.5 1.5 0 1 1-3 0a1.5 1.5 0 0 1 3 0"
clip-rule="evenodd"
/>
</svg>
<div>PNG</div>
</div>
<div class="flex space-x-5">
<!-- Buttons for Download -->
<button
id="copyPngBtn"
class="w-full flex items-center justify-center space-x-2 p-3 border border-gray-300 rounded bg-gray-100"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-4 h-4"
viewBox="0 0 16 16"
>
<path
fill="currentColor"
fill-rule="evenodd"
d="M12 2.5H8A1.5 1.5 0 0 0 6.5 4v1H8a3 3 0 0 1 3 3v1.5h1A1.5 1.5 0 0 0 13.5 8V4A1.5 1.5 0 0 0 12 2.5M11 11h1a3 3 0 0 0 3-3V4a3 3 0 0 0-3-3H8a3 3 0 0 0-3 3v1H4a3 3 0 0 0-3 3v4a3 3 0 0 0 3 3h4a3 3 0 0 0 3-3zM4 6.5h4A1.5 1.5 0 0 1 9.5 8v4A1.5 1.5 0 0 1 8 13.5H4A1.5 1.5 0 0 1 2.5 12V8A1.5 1.5 0 0 1 4 6.5"
clip-rule="evenodd"
/>
</svg>
<div>Copy</div>
</button>
<button
id="downloadPngBtn"
class="w-full flex items-center justify-center space-x-2 p-3 border border-gray-300 rounded bg-gray-100"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
viewBox="0 0 16 16"
>
<path
fill="currentColor"
fill-rule="evenodd"
d="M6.5 6H4.029l3.499 4.276a.61.61 0 0 0 .944 0L11.971 6H9.5V2.75a.25.25 0 0 0-.25-.25h-2.5a.25.25 0 0 0-.25.25zm1.498 7.5H1.75a.75.75 0 0 0 0 1.5h12.5a.75.75 0 0 0 0-1.5zm0-1.5a2.11 2.11 0 0 1-1.631-.774L2.833 6.907A1.474 1.474 0 0 1 3.973 4.5H5V2.75C5 1.784 5.784 1 6.75 1h2.5c.966 0 1.75.784 1.75 1.75V4.5h1.026a1.474 1.474 0 0 1 1.14 2.407l-3.533 4.319c-.4.49-1 .774-1.632.774H8"
clip-rule="evenodd"
/>
</svg>
<div>Download</div>
</button>
</div>
</div>
</div>
</div>
<span id="snackbar">Copied to clipboard</span>
<script type="text/javascript">
function copySvgToClipboard() {
const svgElement = document.getElementById('svgElement');
const svgData = new XMLSerializer().serializeToString(svgElement);
// Copy the serialized SVG text to the clipboard
navigator.clipboard.writeText(svgData).then(() => {
var sb = document.getElementById("snackbar");
sb.className = "show";
setTimeout(()=>{ sb.className = sb.className.replace("show", ""); }, 3000);
}).catch(err => {
console.error("Could not copy SVG text to clipboard: ", err);
});
}
// Function to convert SVG to PNG and copy to clipboard
function copyPngToClipboard() {
const svgElement = document.getElementById('svgElement');
const svgData = new XMLSerializer().serializeToString(svgElement);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = function () {
// Ensure canvas size matches the SVG
canvas.width = svgElement.clientWidth;
canvas.height = svgElement.clientHeight;
// Clear the canvas (transparent background)
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the SVG image on the canvas
ctx.drawImage(img, 0, 0);
// Convert the canvas content to a PNG blob
canvas.toBlob(blob => {
const clipboardItem = [new ClipboardItem({ 'image/png': blob })];
navigator.clipboard.write(clipboardItem).then(() => {
var sb = document.getElementById("snackbar");
sb.className = "show";
setTimeout(()=>{ sb.className = sb.className.replace("show", ""); }, 3000);
}).catch(err => {
console.error("Could not copy PNG to clipboard: ", err);
});
}, 'image/png');
};
// Set the image source to the serialized SVG data
img.src = 'data:image/svg+xml;base64,' + btoa(svgData);
}
// Function to download SVG
function downloadSvg() {
const svgElement = document.getElementById('svgElement');
const svgData = new XMLSerializer().serializeToString(svgElement);
const blob = new Blob([svgData], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'image.svg';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Function to download PNG
function downloadPng() {
const svgElement = document.getElementById('svgElement');
const svgData = new XMLSerializer().serializeToString(svgElement);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = function () {
canvas.width = svgElement.clientWidth;
canvas.height = svgElement.clientHeight;
ctx.drawImage(img, 0, 0);
canvas.toBlob(blob => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'image.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 'image/png');
};
img.src = 'data:image/svg+xml;base64,' + btoa(svgData);
}
// Button event listeners
document.getElementById('copySvgBtn').addEventListener('click', copySvgToClipboard);
document.getElementById('copyPngBtn').addEventListener('click', copyPngToClipboard);
document.getElementById('downloadSvgBtn').addEventListener('click', downloadSvg);
document.getElementById('downloadPngBtn').addEventListener('click', downloadPng);
</script>
</body></html>"""
)
x = x.replace('"', """)
x = x.replace("'", """)
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 predict(input_mol, style, contour_level, 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)
outputfile = os.path.basename(input_mol).replace(".pdb",".svg")
# 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 --color_by chain --depth_shading --depth_lines --colors {chain_str} --depth flat --back_outline --view view_matrix --save {outputfile}")
elif style == "Contour":
os.system(f"cellscape cartoon --pdb {input_mol.name} --outline chain --color_by chain --depth_contour_interval {contour_level} --colors {chain_str} --depth contours --back_outline --view view_matrix --save {outputfile}")
else:
os.system(f"cellscape cartoon --pdb {input_mol.name} --outline chain --colors {chain_str} --depth flat --back_outline --view view_matrix --save {outputfile}")
#read content of file
os.system(f"inkscape {outputfile} --actions='select-all;path-simplify;export-plain-svg' --export-filename {outputfile.replace('.svg', '_opt.svg')}")
return html_output(outputfile.replace('.svg', '_opt.svg'))
def show_contour_level(style):
if style=="Contour":
return gr.Slider(minimum=1,maximum=50,step=1, value=10, label="Contour level", visible=True)
else:
return gr.Slider(minimum=1,maximum=50,step=1, value=10, label="Contour level", visible=False)
with gr.Blocks() as demo:
style = gr.Radio(value="Flat", choices=["Flat", "Contour", "Goodsell3D"], label="Style")
contour_level = gr.Slider(minimum=1,maximum=50,step=1, value=10, label="Contour level", visible=False)
style.change(show_contour_level, style, contour_level)
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)
timestamp = gr.Textbox(visible=False)
btn = gr.Button("Vectorize")
html = gr.HTML("")
btn.click(None, style, [view_str, chains, hidden_style, timestamp], js="(style) => [document.getElementById('viewMatrixResult').value, document.getElementById('chains').value, style, Date.now()]") #
timestamp.change(predict, [inp, style, contour_level, view_str, chains], [html])
# on change of chains trigger, rendering
if __name__ == "__main__":
demo.launch()
|