Spaces:
Runtime error
Runtime error
Upload visualization.py
Browse files3 unmodified lines456789103 unmodified lines1415161617181920216 unmodified lines28293031323334353637383940414243444546474847 unmodified lines9697989910010110277 unmodified lines1801811821701831841851861871881891901911921931941951961971981992002012022032042 unmodified lines20720820921021121221321421521621721821922022122222322422522622722822923023123223323423523681 unmodified lines318319320271321322323324325326327328329330331332333334335336337338339340341342343344345255 unmodified lines601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671134 unmodified lines8068078086738093 unmodified lines"""
import numpy as npfrom pathlib import Pathfrom rmsd_utils import ( parse_residue_atoms, translate_rotate_coords,3 unmodified lines
def create_structure_visualization(ref_path, query_path, ref_window_indices, query_window_indices, rotation_matrix, ref_com, query_com, rmsd=None): rotation_matrix, ref_com, query_com, rmsd=None, ref_name=None, query_name=None, ref_sequence=None, query_sequence=None): """ Create an interactive 3D visualization of aligned structures. 6 unmodified lines ref_com: Center of mass of reference window query_com: Center of mass of query window rmsd: RMSD value (optional, for display) ref_name: Reference structure name (optional, for display) query_name: Query structure name (optional, for display) ref_sequence: Reference sequence (optional, for display) query_sequence: Query sequence (optional, for display) Returns: HTML string containing the py3Dmol visualization """ # Extract simple names if not provided if ref_name is None: ref_name = Path(ref_path).stem if query_name is None: query_name = Path(query_path).stem # Read PDB files with open(ref_path) as f: ref_pdb = f.read()47 unmodified lines <html> <head> <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script> <style> #container {{ width: 100%;77 unmodified lines bottom: 10px; left: 10px; background: rgba(255, 255, 255, 0.95); padding: 10px 15px; padding: 12px 15px; border-radius: 8px; font-family: Arial, sans-serif; font-size: 13px; z-index: 1000; box-shadow: 0 2px 8px rgba(0,0,0,0.1); max-width: 450px; }} .info-row {{ margin: 4px 0; line-height: 1.4; }} .info-label {{ font-weight: bold; color: #555; }} .info-value {{ color: #333; font-family: 'Courier New', monospace; }} .section-title {{ font-weight: bold;2 unmodified lines font-size: 12px; text-transform: uppercase; }} .download-section {{ position: absolute; bottom: 10px; right: 10px; background: rgba(255, 255, 255, 0.95); padding: 10px; border-radius: 8px; font-family: Arial, sans-serif; z-index: 1000; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }} .download-btn {{ background: #4A90E2; color: white; border: none; padding: 8px 16px; border-radius: 4px; cursor: pointer; font-size: 13px; font-weight: bold; }} .download-btn:hover {{ background: #357ABD; }} </style> </head> <body>81 unmodified lines </div> <div class="rmsd-info"> <strong>RMSD:</strong> <span style="color: #E94B3C; font-weight: bold;">{f"{rmsd:.3f}" if rmsd is not None else "N/A"} Å</span> <div class="info-row"> <span class="info-label">RMSD:</span> <span style="color: #E94B3C; font-weight: bold; font-size: 14px;">{f"{rmsd:.3f}" if rmsd is not None else "N/A"} Å</span> </div> <div style="margin-top: 8px; padding-top: 8px; border-top: 1px solid #ddd;"> <div class="info-row"> <span class="info-label">Reference:</span> <span class="info-value">{ref_name}</span> </div> {f'<div class="info-row" style="margin-left: 15px; font-size: 12px;"><span class="info-label">Seq:</span> <span class="info-value">{ref_sequence}</span></div>' if ref_sequence else ''} </div> <div style="margin-top: 6px;"> <div class="info-row"> <span class="info-label">Query:</span> <span class="info-value">{query_name}</span> </div> {f'<div class="info-row" style="margin-left: 15px; font-size: 12px;"><span class="info-label">Seq:</span> <span class="info-value">{query_sequence}</span></div>' if query_sequence else ''} </div> </div> <div class="download-section"> <button class="download-btn" onclick="downloadImage()">📷 Download PNG</button> </div> <script>255 unmodified lines }}); }} function downloadImage() {{ try {{ // Generate filename with metadata var refName = "{ref_name}".replace('.pdb', ''); var queryName = "{query_name}".replace('.pdb', ''); var rmsdValue = "{f'{rmsd:.3f}' if rmsd is not None else 'NA'}"; var filename = 'alignment_' + refName + '_' + queryName + '_RMSD_' + rmsdValue + '.png'; // Ensure viewer is rendered if (viewer) {{ viewer.render(); }} // Get the container element (includes canvas + all overlays) const container = document.getElementById('container'); if (!container) {{ alert('Container not ready. Please wait and try again.'); return; }} // Use html2canvas to capture the entire container with overlays html2canvas(container, {{ backgroundColor: '#ffffff', scale: 2, // Higher resolution logging: false, useCORS: true, allowTaint: true }}).then(function(canvas) {{ // Convert to PNG const dataURL = canvas.toDataURL('image/png'); // Create download link const link = document.createElement('a'); link.download = filename; link.href = dataURL; // Trigger download document.body.appendChild(link); link.click(); document.body.removeChild(link); }}).catch(function(error) {{ console.error('html2canvas error:', error); // Fallback: just capture the canvas without overlays const canvas = document.querySelector('#container canvas'); if (canvas) {{ const dataURL = canvas.toDataURL('image/png'); const link = document.createElement('a'); link.download = filename; link.href = dataURL; document.body.appendChild(link); link.click(); document.body.removeChild(link); }} else {{ alert('Download failed. Please try again.'); }} }}); }} catch (error) {{ console.error('PNG download error:', error); alert('Error downloading PNG: ' + error.message); }} }} // Initialize on load initViewer(); </script>134 unmodified lines else: transformed_lines.append(line) return '\n'.join(transformed_lines) return '\n'.join(transformed_lines)visualization.py+140-4