Spaces:
Runtime error
Runtime error
Upload visualization.py
Browse files3 unmodified lines4567893 unmodified lines131415161718196 unmodified lines262728293031323334353647 unmodified lines84858687888977 unmodified lines1671681691701711721731741751761771782 unmodified lines18118218318418518681 unmodified lines268269270271272273274255 unmodified lines530531532533534535134 unmodified lines6706716726733 unmodified lines"""import numpy as npfrom rmsd_utils import (parse_residue_atoms,translate_rotate_coords,3 unmodified linesdef create_structure_visualization(ref_path, query_path, ref_window_indices, query_window_indices,rotation_matrix, ref_com, query_com, rmsd=None):"""Create an interactive 3D visualization of aligned structures.6 unmodified linesref_com: Center of mass of reference windowquery_com: Center of mass of query windowrmsd: RMSD value (optional, for display)Returns:HTML string containing the py3Dmol visualization"""# Read PDB fileswith 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><style>#container {{width: 100%;77 unmodified linesbottom: 10px;left: 10px;background: rgba(255, 255, 255, 0.95);padding: 10px 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);}}.section-title {{font-weight: bold;2 unmodified linesfont-size: 12px;text-transform: uppercase;}}</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><script>255 unmodified lines}});}}// Initialize on loadinitViewer();</script>134 unmodified lineselse:transformed_lines.append(line)return '\n'.join(transformed_lines)3 unmodified lines456789103 unmodified lines14151617181920216 unmodified lines28293031323334353637383940414243444546474847 unmodified lines9697989910010110277 unmodified lines1801811821831841851861871881891901911921931941951961971981992002012022032042 unmodified lines20720820921021121221321421521621721821922022122222322422522622722822923023123223323423523681 unmodified lines318319320321322323324325326327328329330331332333334335336337338339340341342343344345255 unmodified lines601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671134 unmodified lines8068078088093 unmodified lines"""import numpy as npfrom pathlib import Pathfrom rmsd_utils import (parse_residue_atoms,translate_rotate_coords,3 unmodified linesdef create_structure_visualization(ref_path, query_path, ref_window_indices, query_window_indices,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 linesref_com: Center of mass of reference windowquery_com: Center of mass of query windowrmsd: 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 providedif ref_name is None:ref_name = Path(ref_path).stemif query_name is None:query_name = Path(query_path).stem# Read PDB fileswith 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 linesbottom: 10px;left: 10px;background: rgba(255, 255, 255, 0.95);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 linesfont-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"><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 metadatavar 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 renderedif (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 overlayshtml2canvas(container, {{backgroundColor: '#ffffff',scale: 2, // Higher resolutionlogging: false,useCORS: true,allowTaint: true}}).then(function(canvas) {{// Convert to PNGconst dataURL = canvas.toDataURL('image/png');// Create download linkconst link = document.createElement('a');link.download = filename;link.href = dataURL;// Trigger downloaddocument.body.appendChild(link);link.click();document.body.removeChild(link);}}).catch(function(error) {{console.error('html2canvas error:', error);// Fallback: just capture the canvas without overlaysconst 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 loadinitViewer();</script>134 unmodified lineselse:transformed_lines.append(line)return '\n'.join(transformed_lines)
visualization.py+140-4