Skip to content

About

Web-based viewer for the grid map data from SerialEM.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

60 Commits

Folders and files

Repository files navigation

SerialEM Grid Viewer DOI

A browser viewer for SerialEM sessions. It places the atlas, square maps, exposures, tilt series and PACEtomo targets from their .mdoc files on one stage map, lets you align them to each other, and makes an acquisition movie from atlas to tilt series.

It is one HTML page (index.html) plus a small Python server (mdoc_server.py) that serves your data folders to the browser.

Overview: atlas, square maps and tilt series on one stage map

Contents

Quick start

On the remote computer that has the data. Organize the data this way

SerialEMData/
├── atlas.mrc.mdoc                # Single grid atlas metadata
├── atlas.mrc                     # Single grid atlas
├── MMM1.mrc.mdoc
├── MMM1.mrc
├── ts/                           # Better to soft link all the .mrc files
│   ├── T_1_ts_001.mrc.mdoc
│   ├── T_1_ts_001.mrc            # Much better for viewing & movie when this is a binned stack (.st is also ok)
│   ├── T_1_ts_002.mrc.mdoc
│   ├── T_1_ts_002.mrc
│   ├── T_1_ts_003.mrc.mdoc
│   └── T_1_ts_003.mrc
└── targets/
    ├── T_1_tgt_001_view.mrc.mdoc
    ├── T_1_tgt_001_view.mrc
    ├── T_1_tgt_001.mrc.mdoc
    ├── T_1_tgt_001.mrc
    ├── T_1_tgt_002.mrc.mdoc
    ├── T_1_tgt_002.mrc
    ├── T_1_tgt_003.mrc.mdoc
    ├── T_1_tgt_003.mrc
    └── T_1_tgts.txt

On the remote server where the data is, install serialEM_grid_viewer

git clone https://github.com/builab/serialEM_grid_viewer.git
cd serialEM_grid_viewer

Start the server on the remote workstation, listening only locally, and tunnel the port:

# on the workstation
python3 mdoc_server.py /path/to/your/serialEM/data --port 8765

# on your local computer do port forwarding like Cryosparc
ssh -N -f -L localhost:8765:localhost:8765 user@remote_server

Then open http://localhost:8765 in Chrome, Edge or Firefox on your local machine. When it starts, the server prints the exact ssh command for your workstation; see Running on a remote workstation for the details and for troubleshooting.

Alternatively, if the data is on your local computer, then start on the local computer. Then open http://localhost:8765 in browser.

Opening data

Open dialog browsing the server

  • Open… opens .mdoc files of any type, and PACEtomo _tgts.txt files. The type (atlas, square map, exposure, tilt series) is detected from the mdoc. You can change it under a layer's More.
  • When the page is served by mdoc_server.py, Open, Save and Load browse the server, not your own computer. A "Pick from this computer instead" link remains in each dialog.
  • The Data box shows the server, its shared folders and the folders the open files came from.
  • Each layer card shows its name, visibility, colour and Show images. Type, box rotation, piece alignment and sizes are under More.
  • An atlas file with several grid maps (one montage per section) shows one of them. Pick which with Grid map on the layer card; the choice is kept in saved sessions.

Aligning

All alignment is done from the right-click menu on the map. It lists actions for everything under the cursor.

Right-click menu

Right-click on Actions
Square map Zoom to this square · Align to atlas by dragging · Align to atlas by correlation · Reset atlas offset
Exposure or tilt series Zoom to this image · Align by dragging · Align by correlation (shift only, or shift + rotation ±5°) · Refine the whole layer by correlation · Reset position · Make acquisition movie (tilt series)
PACEtomo View Zoom to this View · Align by dragging · Align by correlation (shift only, or shift + rotation ±5°) · Find related tilt series · Make PACEtomo movie · Reset alignment
PACEtomo target Zoom to this target · Make acquisition movie

The square-map actions appear only when you right-click the square map itself, not a View, target or image on it.

Align by dragging opens an align bar. Drag anywhere on the map to move the item. What moves together:

  • Square map: every square map, exposure, tilt series and PACEtomo area. This is one atlas offset.
  • PACEtomo View: the View, all its targets and their tilt series.
  • Exposure or tilt series: the image, plus any images that share its stage position.

While aligning, the moving images are drawn at 50 % opacity. Press D for a difference view, where features that line up go dark. Arrow keys nudge (Shift ×10), [ and ] rotate, Enter keeps the change and Esc cancels it. Auto-refine finishes with a correlation from where you dropped it.

Aligning by dragging, with the align bar

Align by correlation matches the image against the square-map pieces read from the .mrc. A second pass runs at the pieces' own pixel size. The status line reports the score and the pixel size it matched at.

PACEtomo

A PACEtomo group aligned on the square map

  • Opening one _tgts.txt also opens its View image, the Preview image at each target, and every tilt series acquired so far. They form one group in the Layers list, with the tilt series nested under it.
  • Each group has its own colour (bright yellow-green by default). Each target is drawn as a circle around its image.
  • View size: if the View's pixel size in its mdoc disagrees with the square map, aligning by correlation also fits the View's size. The factor is remembered per magnification and reused for the next group. You can also set it by hand under the group's More ("View size ×").
  • Find related tilt series (right-click the View, or the group's More) searches the whole shared folder and its subfolders for the tilt series named in the target file, and opens the ones not open yet. A copy in the target file's own folder is preferred, then the nearest one. It needs the server or a chosen data folder.
  • Tilt series opened on their own are recognized by name (the tsfile entries in the _tgts.txt), whenever and from whichever folder they are opened. They join the group and sit at their target.
  • A tilt series can be aligned on its own (right-click it). It keeps its own position and stays part of the group; its target circle follows it.

Details panel

Click any item to see its details. The short list comes first; the raw mdoc keys and file header are under More.

  • Square map and atlas: file, centre X/Y, box size, image size, pixel size used, box rotation, atlas offset, fitted px → stage.
  • Exposure, tilt series and View: the same, plus image shift and specimen shift. Tilt series also show angle from, to and increment.
  • PACEtomo target (click inside its circle): target number, position, stage position in the file, specimen shift, Preview, and the tilt series with its angle range.

Details of a PACEtomo target

Acquisition movie

Right-click a tilt series (or its PACEtomo target) and choose Make acquisition movie…. The movie goes atlas → square map → acquisition position → tilt series, using the positions as aligned in the viewer. No manual coordinates or blendmont-stitched montages are needed.

Acquisition movie dialog

The dialog has a live preview (scrub or play) and these settings:

  • Labels: a title and subtitle for each of the four stages. An empty title hides the label.
  • Camera: final zoom, extra turn at the start, start with the atlas level, turn so the square map ends upright.
  • Timing: seconds for the atlas, the zoom to the square, the square map, the zoom to the target, and the hold on the target.
  • Tilt playback: start/end angle, lowest and highest angle, order (start → lowest → highest → start, or the reverse), images per second, and the stage-tilt schematic on or off. The start angle defaults to the first image acquired (from DateTime), so a lamella series starts at its pre-tilt.
  • Output: 1280×720, 1920×1080 or 3840×2160, at 24, 30 or 60 frames per second.

For the movie, the square map is rebuilt as one image: every piece is re-read at higher resolution, with one contrast for the whole map and blended overlaps. The area around the target is added at full resolution. The atlas cross-fades into the square map during the zoom, and everything outside the square map is black from then on.

Movie frames

Encoding. With ffmpeg on the server machine, the server encodes an H.264 .mp4. You can download it or save it into a shared folder. Without ffmpeg, the browser encodes a .webm frame by frame. The dialog says which one it will use. Both give exactly the duration shown in the preview.

PACEtomo movie

PACEtomo movie

Right-click the View of a PACEtomo area and choose Make PACEtomo movie…. The movie goes:

  1. Atlas → square map → zoom to the View.
  2. Picking the targets one by one: a box at each tilt series, a circle drawn around it, and its number. After target 1 is picked, the tilt axis is drawn through it as a dashed line (the stage X axis), and the movie pauses on it for one more target's time before target 2; the axis can be turned off.
  3. All tilt series in a grid, stepping through the tilt angles together.

The tilt series stand in for their targets, at their aligned positions. Targets without a tilt series, or whose tilt-series stack cannot be read (often target 1, the tracking target), are picked but left out of the grid. The playback uses the same bidirectional order as the acquisition movie (start → lowest → highest → start), not the dose-symmetric acquisition order.

The camera shows the View and all targets; View zoom (1 = they just fit) zooms in or out from there. The dialog has the same labels, camera, playback and output settings as the acquisition movie. The timing settings are the atlas, the zoom to the square, the square map, the zoom to the View, and the time per target.

Missing files:

  • Atlas or square map without its .mrc: no movie. Hide the atlas layer to start the movie at the square map instead.
  • View without its .mrc: the movie is still made from the square map; the View box comes from its mdoc and the image is not shown. Aligning the View by correlation needs the image, but aligning by dragging works.
  • Target Previews missing: no effect on targets with a tilt series.

PACEtomo movie frames

Sessions

Save session… writes a .json file with the open files, layer settings, view, atlas offset, aligned positions and PACEtomo alignments. With the server, it is saved in a shared folder. Load session… reopens everything from it. Positions are also kept in the browser between visits, and Restore last session brings back the previous one.

Settings

The collapsed Settings section in the sidebar holds:

  • Atlas offset: X/Y in µm, on or off, reset.
  • Alignment: whether Auto-refine also fits rotation; whether images that share a stage position move together; use or clear aligned positions.
  • Image shift → stage: µm per IS unit, IS rotation and invert IS Y. The box centre is StagePosition + scale · R(θ) · (ISx, −ISy).
  • Display: view rotation (180° matches the SerialEM Navigator), grid outline, labels, piece numbers, mirror stage Y.

Keyboard and mouse

Input Action
Drag Pan (while aligning: move the item)
Wheel Zoom
Click Select and show details; click a square map or a PACEtomo View to zoom into it (click again to just select)
Right-click Menu for the items under the cursor
F or Esc Full view
D Toggle difference view for moving images
While aligning: arrows / Shift+arrows Nudge by 1 / 10 screen pixels
While aligning: [ ] (Shift: ×10) Rotate by 0.5° (5°)
While aligning: Space-drag or middle-drag Pan
While aligning: Enter / Esc Keep / cancel

Server options

python3 mdoc_server.py ROOT [ROOT ...] [options]

  --port N             port to listen on (default 8765, or the next free one if it is taken)
  --host 127.0.0.1     127.0.0.1 = this computer only (default); 0.0.0.0 = whole network
  --html PATH          the viewer page (default: index.html next to the script)
  --read-only          never write anything (no session files, no movies)
  --ffmpeg PATH        ffmpeg for movies: a path, 'auto' (default: look on PATH) or 'none'
  -v, --verbose        log every request

Only the folders you list are reachable. Everything is read-only, except session .json files and movie .mp4 files saved inside those folders. Requests that write need a JSON content type, so a page from another website cannot make your browser write files there. At start-up the server prints the shared folders and whether it found ffmpeg.

Running on a remote workstation (port forwarding)

This works like a Jupyter notebook (or CryoSPARC) on a remote machine: the server runs on the workstation that has the data, listening only on that machine, and an SSH tunnel brings its port to your own computer. Nothing needs to be opened in a firewall, and only people who can log in to the workstation can reach it.

1. Start the server on the workstation

ssh you@workstation
cd serialEM_grid_viewer
python3 mdoc_server.py /path/to/your/serialEM/data

It prints the command for the next step, already filled in, for example:

  You are logged in over SSH. To open the viewer from your own computer, forward the port
  (the same as for a Jupyter notebook). On YOUR computer, in a new terminal, run:

      ssh -N -L 8765:localhost:8765 you@workstation.lab.mcgill.ca
      (or: ssh -N -L 8765:localhost:8765 you@10.0.4.17)

  leave it running, then open  http://localhost:8765  in your browser there.

If port 8765 is already taken on the workstation (another user, or another copy of the server), it uses the next free port (8766, 8767 …) and prints the command with that port. To keep the server running after you log out, start it inside tmux or screen, or with nohup python3 mdoc_server.py … &.

2. Open the tunnel from your own computer

In a terminal on your own computer (macOS and Linux Terminal, or Windows PowerShell, which includes ssh):

ssh -N -L 8765:localhost:8765 you@workstation
  • -L 8765:localhost:8765 forwards port 8765 on your computer to port 8765 on the workstation. If the server printed another port, use it on the right-hand side.
  • -N opens the tunnel without a shell. The command seems to hang: that is the tunnel. Leave the terminal open; Ctrl+C closes it.
  • Add -f (ssh -N -f -L …) to run the tunnel in the background instead. Close it later with pkill -f "ssh -N -f -L 8765".

3. Open the viewer: http://localhost:8765 in Chrome, Edge or Firefox on your own computer.

Variations

Situation What to do
Port 8765 is in use on your own computer (e.g. a local Jupyter) Use another local port on the left: ssh -N -L 9000:localhost:8765 you@workstation, then open http://localhost:9000
The workstation is reached through a gateway ssh -N -J you@gateway -L 8765:localhost:8765 you@workstation
You use it often Add to ~/.ssh/config on your computer, then ssh -N grid-viewer is enough:
Host grid-viewer
HostName workstation.lab.mcgill.ca
User you
LocalForward 8765 localhost:8765
Windows with PuTTY or MobaXterm Connection → SSH → Tunnels: source port 8765, destination localhost:8765, Add, then connect
VS Code Remote-SSH Start the server in VS Code's terminal on the workstation; VS Code forwards the port by itself (Ports tab)
Everyone on the lab network should reach it without SSH python3 mdoc_server.py … --host 0.0.0.0, then http://workstation:8765. Anyone who can reach the port can read the shared folders; add --read-only if they should not save files

Troubleshooting

Message or symptom Cause and fix
bind [127.0.0.1]:8765: Address already in use on your computer Something already uses 8765 locally (often an earlier tunnel still running). Close it (pkill -f "ssh -N") or use another local port, as above
channel 2: open failed: connect failed: Connection refused The tunnel works but nothing listens on that port on the workstation: the server is not running, or it printed a different port. Check its output and match the right-hand port
The page does not load at all The tunnel is not open (check the ssh terminal), or you opened a different local port than the left-hand one in -L
Port 8765 is already in use on this computer when starting the server You asked for that port with --port and it is taken. Leave --port out to use the next free one
  • Browser: a current Chrome, Edge or Firefox. Encoding movies in the browser needs WebCodecs (Chrome or Edge).
  • Server: Python 3.7 or newer.
    • Optional: numpy. The server then shrinks each image piece before sending it, which is much faster over a network.
    • Optional: ffmpeg on the server machine, for .mp4 movies. --ffmpeg points at a specific binary; the copy from the imageio-ffmpeg Python package is also found.
  • Data: SerialEM .mdoc files with their .mrc images. A tilt series may also be kept as .st (for example TS_001.mrc.mdoc with TS_001.st). Symbolic links work through mdoc_server.py; browsers leave links out when a folder is chosen without the server. Little-endian MRC, modes 0, 1, 2, 6 and 12.

License

GNU General Public License v3.0. See LICENSE.


Vibecoded by BuiLab@McGill.

About

Web-based viewer for the grid map data from SerialEM.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages