Skip to content

Add residual electron density (Fo−Fc) wireframe map to MoleculeWidget#9

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/draw-residual-electron-density-again
Draft

Add residual electron density (Fo−Fc) wireframe map to MoleculeWidget#9
Copilot wants to merge 3 commits intomainfrom
copilot/draw-residual-electron-density-again

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 13, 2026

Compute and display a difference Fourier map from a CIF + SHELX HKL file, rendered as a green (positive) / red (negative) wireframe overlay in the existing 2D projection widget. Crystal symmetry is fully accounted for via gemmi's StructureFactorCalculatorX, which applies all space-group operators when computing Fc.

New: diff_map.py

  • compute_diff_map(cif_path, hkl_path=None) — reads structure + ADPs from CIF, parses and merges SHELX HKL reflections (external file or _shelx_hkl_file embedded in CIF), computes Fc, estimates scale factor k = √(ΣFo²/ΣFc²), fills a ReciprocalComplexGrid with (Fo/k − |Fc|)·exp(iφ_c) coefficients, and inverse-FFTs to a real-space grid.
  • get_mesh_segments(result, level_sigma=3.0) — marching cubes at ±N·σ → unique wireframe edges in Cartesian Å.

molecule2D.MoleculeWidget

  • set_density_mesh(pos_segs, neg_segs) / clear_density_mesh() / show_density(bool) — manage the (N, 2, 3) segment arrays.
  • Segments are rotated in-place alongside atoms during interactive rotation; drawn before atoms so the structure overlays the mesh.

loader.MoleculeLoader

  • load_diff_map(hkl_path=None, level_sigma=3.0) — auto-detects <stem>.hkl / <stem>-finalcif.hkl, falls back to embedded CIF data.

viewer_widget.MoleculeViewerWidget

  • Added Load Map button, Map σ spinbox (1–20, default 3.0), and Show Map checkbox.

Usage

loader = MoleculeLoader(widget)
loader.load_file("p31c.cif")
loader.load_diff_map(level_sigma=3.0)   # auto-detects p31c.hkl or embedded HKL

Dependencies

scikit-image added as an optional extra (pip install fastmolwidget[diffmap]). Raises ImportError with a clear message if absent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants