|
| 1 | +import argparse |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import urllib.request |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +try: |
| 9 | + from mosaicq import calculate_q, calculate_q_alt |
| 10 | + MOSAICQ_AVAILABLE = True |
| 11 | +except ImportError: |
| 12 | + MOSAICQ_AVAILABLE = False |
| 13 | + |
| 14 | +# No .format() used — placeholders are replaced with .replace() |
| 15 | +# This avoids conflicts between Python's {key} syntax and JavaScript's {key: value} |
| 16 | +HTML_TEMPLATE = """<!DOCTYPE html> |
| 17 | +<html> |
| 18 | +<head> |
| 19 | +<meta charset="utf-8"> |
| 20 | +<title>Proteins Mosaic Q. __PDB_ID__</title> |
| 21 | +<script src="https://3dmol.csb.pitt.edu/build/3Dmol-min.js"></script> |
| 22 | +<style> |
| 23 | + body { font-family: 'Segoe UI', sans-serif; margin: 20px; background: #fafafa; } |
| 24 | + h2 { color: #E05A00; } |
| 25 | + .metrics { background: white; border-radius: 8px; padding: 16px; |
| 26 | + display: inline-block; margin: 12px 0; |
| 27 | + box-shadow: 0 1px 4px rgba(0,0,0,0.1); } |
| 28 | + table { border-collapse: collapse; } |
| 29 | + td, th { padding: 8px 16px; border: 1px solid #ddd; } |
| 30 | + th { background: #f5f5f5; } |
| 31 | + .legend { display: flex; flex-wrap: wrap; gap: 10px; |
| 32 | + font-size: 12px; margin: 10px 0; } |
| 33 | + .dot { width: 12px; height: 12px; border-radius: 50%; |
| 34 | + display: inline-block; border: 1px solid #ccc; } |
| 35 | + #viewer { width: 600px; height: 500px; position: relative; |
| 36 | + border-radius: 8px; border: 1px solid #eee; } |
| 37 | +</style> |
| 38 | +</head> |
| 39 | +<body> |
| 40 | +<h2>🤠 Proteins Mosaic Q __PDB_ID__</h2> |
| 41 | +
|
| 42 | +<div class="metrics"> |
| 43 | + <table> |
| 44 | + <tr><th>Descriptor</th><th>Value</th></tr> |
| 45 | + <tr><td><b>Q</b></td><td>__Q__</td></tr> |
| 46 | + <tr><td><b>Q_alt</b></td><td>__Q_ALT__</td></tr> |
| 47 | + </table> |
| 48 | +</div> |
| 49 | +
|
| 50 | +<div class="legend"> |
| 51 | + <span><span class="dot" style="background:#fff;"></span> Hydrophobic</span> |
| 52 | + <span><span class="dot" style="background:#4CAF7D;"></span> Polar</span> |
| 53 | + <span><span class="dot" style="background:#E8863A;"></span> Acidic</span> |
| 54 | + <span><span class="dot" style="background:#5B8DD9;"></span> Basic</span> |
| 55 | + <span><span class="dot" style="background:#4DD9D9;"></span> Special</span> |
| 56 | +</div> |
| 57 | +
|
| 58 | +<div id="viewer"></div> |
| 59 | +
|
| 60 | +<script> |
| 61 | +var pdbData = `__PDB_CONTENT__`; |
| 62 | +
|
| 63 | +var viewer = $3Dmol.createViewer(document.getElementById('viewer'), { |
| 64 | + backgroundColor: 'white' |
| 65 | +}); |
| 66 | +
|
| 67 | +viewer.addModel(pdbData, 'pdb'); |
| 68 | +
|
| 69 | +var colorGroups = [ |
| 70 | + {residues: ['ALA','VAL','ILE','LEU','MET','PHE','TYR','TRP'], color: 'white'}, |
| 71 | + {residues: ['SER','THR','ASN','GLN'], color: 'green'}, |
| 72 | + {residues: ['ASP','GLU'], color: 'orange'}, |
| 73 | + {residues: ['ARG','HIS','LYS'], color: 'blue'}, |
| 74 | + {residues: ['CYS','SEC','GLY','PRO'], color: 'cyan'}, |
| 75 | +]; |
| 76 | +
|
| 77 | +colorGroups.forEach(function(group) { |
| 78 | + group.residues.forEach(function(res) { |
| 79 | + viewer.setStyle({resn: res, hetflag: false}, {sphere: {color: group.color}}); |
| 80 | + }); |
| 81 | +}); |
| 82 | +
|
| 83 | +viewer.zoomTo({hetflag: false}); |
| 84 | +viewer.render(); |
| 85 | +</script> |
| 86 | +
|
| 87 | +<p style="font-size:0.85rem; color:#888; margin-top:20px;"> |
| 88 | + <a href="https://proteins-mosaic-q.org" target="_blank">proteins-mosaic-q.org</a> |
| 89 | +</p> |
| 90 | +</body> |
| 91 | +</html>""" |
| 92 | + |
| 93 | + |
| 94 | +def main(): |
| 95 | + parser = argparse.ArgumentParser() |
| 96 | + parser.add_argument("--pdb_id", required=False, default=None) |
| 97 | + parser.add_argument("--pdb_file", required=False, default=None) |
| 98 | + parser.add_argument("--output", required=True) |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + # Determine label and source |
| 102 | + if args.pdb_file: |
| 103 | + pdb_label = args.pdb_id.strip().upper() if args.pdb_id else "Uploaded structure" |
| 104 | + elif args.pdb_id: |
| 105 | + pdb_label = args.pdb_id.strip().upper() |
| 106 | + else: |
| 107 | + pdb_label = "UNKNOWN" |
| 108 | + |
| 109 | + q, q_alt = "N/A", "N/A" |
| 110 | + pdb_content = "" |
| 111 | + try: |
| 112 | + if args.pdb_file: |
| 113 | + # Galaxy passes files with a UUID filename — copy to .pdb so mosaicq recognises it |
| 114 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 115 | + tmp_pdb = Path(tmpdir) / "structure.pdb" |
| 116 | + shutil.copy(args.pdb_file, str(tmp_pdb)) |
| 117 | + pdb_content = tmp_pdb.read_text() |
| 118 | + if MOSAICQ_AVAILABLE: |
| 119 | + q = f"{calculate_q(str(tmp_pdb)):.4f}" |
| 120 | + q_alt = f"{calculate_q_alt(str(tmp_pdb)):.4f}" |
| 121 | + elif args.pdb_id: |
| 122 | + # Download from RCSB |
| 123 | + pdb_id = args.pdb_id.strip().upper() |
| 124 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 125 | + url = f"https://files.rcsb.org/download/{pdb_id}.pdb" |
| 126 | + filepath = Path(tmpdir) / f"{pdb_id}.pdb" |
| 127 | + try: |
| 128 | + urllib.request.urlretrieve(url, str(filepath)) |
| 129 | + except Exception as download_err: |
| 130 | + raise RuntimeError(f"Could not download {pdb_id}: {download_err}") |
| 131 | + if filepath.exists(): |
| 132 | + pdb_content = filepath.read_text() |
| 133 | + if MOSAICQ_AVAILABLE: |
| 134 | + q = f"{calculate_q(str(filepath)):.4f}" |
| 135 | + q_alt = f"{calculate_q_alt(str(filepath)):.4f}" |
| 136 | + else: |
| 137 | + raise RuntimeError("Please provide either a PDB ID or a PDB file.") |
| 138 | + except Exception as e: |
| 139 | + q, q_alt = f"Error: {e}", "N/A" |
| 140 | + |
| 141 | + # Escape backticks and ${ for JS template literal |
| 142 | + pdb_content_js = pdb_content.replace('`', '\\`').replace('${', '\\${') |
| 143 | + |
| 144 | + # Use .replace() for all substitutions — no .format() needed, |
| 145 | + # so JavaScript { } syntax requires no escaping whatsoever |
| 146 | + html = HTML_TEMPLATE |
| 147 | + html = html.replace("__PDB_ID__", pdb_label) |
| 148 | + html = html.replace("__Q__", str(q)) |
| 149 | + html = html.replace("__Q_ALT__", str(q_alt)) |
| 150 | + html = html.replace("__PDB_CONTENT__", pdb_content_js) |
| 151 | + |
| 152 | + with open(args.output, "w", encoding="utf-8") as f: |
| 153 | + f.write(html) |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main() |
0 commit comments