Bsoft  2.1.0
Bernard's software package
bshow_tomo.tcl
Go to the documentation of this file.
1 ##
2 # @file bshow_tomo.tcl
3 #
4 # @brief Procedures to manage tomographic images
5 #
6 # @author Bernard Heymann
7 # @date Created: 20021112
8 # @date Modified: 20200325
9 
10 set show_markers 1
11 set show_marker_errors 1
12 set show_marker_labels 1
13 set marker_radius 10
14 set markers_selected []
15 set refop "m"
16 set hires 10
17 set lores 1000
18 set samrat 1
19 set norm_type "Simple"
20 set shift_limit 500
21 set cc_type 0
22 
23 ## @brief Dialog box to manage tomographic information
24 #
25 
26 proc Tomography { } {
27 # puts "In Tomography"
28  global tool project_item theimg imgtype
29  global helv12 cour12
30  global project_item
31  global thickness tilt_axis marker_radius
32 
33  if ![Bimage exists $theimg] {
34  tk_dialog .dialog "No image in memory!" \
35  {Please read in an appropriate image first.} \
36  info 0 {OK}
37  return
38  }
39 
40  set c [getImageCanvas $theimg]
41  if ![Bmg exists] {
42  set imgtype "mg"
43  createMgParam
44  setMgParam
45  Bmg set $project_item axis $tilt_axis
46  Bmg set $project_item marker_radius $marker_radius
47 # puts "Setting tilt axis in the project"
48  } else {
49  set axis [expr [Bmg get $project_item axis] ]
50  if { $axis > -1000 } {
51  set tilt_axis $axis
52 # puts "Getting tilt axis from the project"
53  }
54  set r [Bmg get $project_item marker_radius]
55  if $r {
56  set marker_radius $r
57  } else {
58 # set marker_radius 10
59  Bmg set $project_item marker_radius $marker_radius
60  }
61  }
62 
63  set field [Bmg get $project_item field]
64  set nmg [Bmg get $project_item number_of_mg]
65  set nrec [Bmg get $project_item number_of_rec]
66 
67 # puts "Number of micrographs: $nmg"
68 # puts "Number of reconstructions: $nrec"
69 # puts "Field: $field"
70 # puts "Marker radius: $marker_radius"
71 # puts "Tilt axis: $tilt_axis"
72 
73  set w .wtomo
74  if ![winfo exists $w] {
75  toplevel $w
76  menu $w.menuBar -tearoff 0
77  $w.menuBar add cascade -menu $w.menuBar.tomo -label "Tomography" -underline 0
78  menu $w.menuBar.tomo -tearoff 0
79  $w configure -menu $w.menuBar
80  $w.menuBar.tomo add command -label "Help" -underline 0 \
81  -command { showURL "file://$Bsoft/doc/bshow/bshow_tomo.html" }
82 # -command { showHelp bshow_tomo.hlp }
83  $w.menuBar.tomo add command -label "Set tilt angles" \
84  -command { Tilts } -underline 0
85  $w.menuBar.tomo add command -label "Read tilt angles file" \
86  -command { readTlt } -underline 0
87 # $w.menuBar.tomo add command -label "Find markers in all images" \
88 # -command { markerFind -1 } -underline 0
89  $w.menuBar.tomo add command -label "Renumber markers in current image" \
90  -command { markerRenumber } -underline 0
91  $w.menuBar.tomo add command -label "Center markers in current image" \
92  -command { markerCenter } -underline 0
93  $w.menuBar.tomo add command -label "Extract markers" \
94  -command { markerExtract } -underline 0
95  $w.menuBar.tomo add command -label "Show marker table" \
96  -command { markerTable } -underline 0
97  $w.menuBar.tomo add command -label "Delete selected markers" \
98  -command { markerDeleteSelected } -underline 0
99  $w.menuBar.tomo add command -label "Delete selected markers in all images" \
100  -command { markerDeleteSelectedAll } -underline 0
101  $w.menuBar.tomo add command -label "Delete all markers" \
102  -command { markerDeleteAll } -underline 0
103  $w.menuBar.tomo add command -label "Close" \
104  -command "destroy $w" -underline 0
105 
106  $w.menuBar add cascade -menu $w.menuBar.tomoflow -label "Workflow" -underline 0
107  menu $w.menuBar.tomoflow -tearoff 0
108  $w configure -menu $w.menuBar
109  $w.menuBar.tomoflow add command -label "Align and sum frames" \
110  -command { alignSumFrames } -underline 0
111  $w.menuBar.tomoflow add command -label "Normalize images" \
112  -command { normalizeImages } -underline 0
113  $w.menuBar.tomoflow add command -label "Align micrographs" \
114  -command { alignMicrographs } -underline 0
115  $w.menuBar.tomoflow add command -label "Find markers in current image" \
116  -command { markerFind [.image.scale get] } -underline 0
117  $w.menuBar.tomoflow add command -label "Generate markers from seed" \
118  -command { markerGenerate } -underline 0
119  $w.menuBar.tomoflow add command -label "Find tilt axis" \
120  -command { findTiltAxis } -underline 0
121  $w.menuBar.tomoflow add command -label "Transfer markers to second series" \
122  -command { markerTransfer } -underline 0
123  $w.menuBar.tomoflow add command -label "Track markers" \
124  -command { markerTrack } -underline 0
125  $w.menuBar.tomoflow add command -label "Refine alignment" \
126  -command { markerRefine } -underline 0
127  $w.menuBar.tomoflow add command -label "Calculate and fit power spectra" \
128  -command { tomoPowerSpectra } -underline 0
129  $w.menuBar.tomoflow add command -label "Estimate resolution" \
130  -command { tomoResolution } -underline 0
131  $w.menuBar.tomoflow add command -label "Reconstruct" \
132  -command { tomoReconstruct } -underline 0
133  $w.menuBar.tomoflow add command -label "Denoise" \
134  -command { tomoDenoise } -underline 0
135 
136  frame $w.switches
137  checkbutton $w.switches.markers -text "Show markers" -variable show_markers \
138  -command { markerDrawAll }
139  checkbutton $w.switches.errors -text "Show errors" -variable show_marker_errors \
140  -command { markerDrawAll }
141  checkbutton $w.switches.labels -text "Show labels" -variable show_marker_labels \
142  -command { markerDrawAll }
143  pack $w.switches.markers $w.switches.errors $w.switches.labels -side left -ipadx 2 -ipady 5 -pady 5 -padx 1m
144 
145  setupEntry $w.thick "Thickness " double $thickness "The tomogram thickness estimate set by the user or determined using the proportionality parameter, lambda."
146  label $w.thick.unit -text "Å" -width 3 -font $helv12 -anchor w
147 # button $w.thick.update -text "Fit intensities" \
148 # -relief raised -command "fitIntensities"
149  label $w.thick.update -text "Update" -relief raised
150  set m [menu $w.thick.pup]
151  $m add command -label "Fit intensities" -command "fitIntensities"
152  $m add command -label "Marker range" -command "markerRange"
153  pack $w.thick.unit $w.thick.update -side left -padx 1m
154  bind $w.thick.update <1> [list tk_popup "$w.thick.pup" %X %Y]
155 
156  setupEntry $w.tilt_axis "Tilt axis " double $tilt_axis "The general tilt axis angle starting from the x-axis and proceeding counter-clockwise"
157  checkbutton $w.tilt_axis.show -text "Show" -variable show_tilt_axis \
158  -command { drawTiltAxis }
159  bind $w.tilt_axis.e <Return> { setTiltAxis }
160  pack $w.tilt_axis.show -side left -padx 1m
161 
162  setupEntry $w.marker_radius "Marker radius " double $marker_radius "The typical radius of fidicial markers in pixels"
163 
164  frame $w.marker
165  label $w.marker.tag -text "Markers" -width 20 -anchor w
166  label $w.marker.num -width 10 -text "0" -relief sunken -bd 1 -anchor w
167  button $w.marker.tabbut -text "Marker table" \
168  -relief raised -command "markerTable"
169  pack $w.marker.tag $w.marker.num $w.marker.tabbut -side left -padx 1m
170 
171  frame $w.selected
172  label $w.selected.numtag -text "Selected marker" -width 20 -anchor w
173  label $w.selected.num -width 4 -text "" -relief sunken -bd 1 -anchor w
174  label $w.selected.restag -text "Residual" -width 10 -anchor e
175  label $w.selected.res -width 6 -text "" -relief sunken -bd 1 -anchor w
176  label $w.selected.fomtag -text "FOM" -width 5 -anchor e
177  label $w.selected.fom -width 6 -text "" -relief sunken -bd 1 -anchor w
178  button $w.selected.refbut -text "Refine" \
179  -relief raised -command "refineMarker"
180  pack $w.selected.numtag $w.selected.num $w.selected.restag \
181  $w.selected.res $w.selected.fomtag $w.selected.fom \
182  $w.selected.refbut -side left -padx 1m
183 
184  frame $w.resid
185  label $w.resid.tag -text "Residual" -width 20 -anchor w
186  label $w.resid.val -width 10 -text "" -relief sunken -bd 1 -anchor w
187  button $w.resid.update -text "Update" \
188  -relief raised -command "updateTomoTable"
189  pack $w.resid.tag $w.resid.val $w.resid.update -side left -padx 1m
190 
191  frame $w.fom
192  label $w.fom.label -text "FOM cutoff" -width 20 -anchor w
193  scale $w.fom.scale -orient horizontal -length 300 -from 0 -to 1 \
194  -command { setFOMcutoff } -resolution 0.001
195  pack $w.fom.label $w.fom.scale -side left -padx 1m
196 
197  frame $w.field
198  label $w.field.tag -text "Field" -width 20 -anchor w
199  label $w.field.str -width 50 -text $field -relief sunken -bd 1 -anchor w
200  pack $w.field.tag $w.field.str -side left -padx 1m
201 
202  setupMicrographButtons $w.neighbor "micrograph"
203 
204  label $w.header -font $helv12 -anchor w \
205  -text " Mg Tilt Axis Level OriginX OriginY ScaleX ScaleY Residual"
206 
207  frame $w.frame
208  text $w.table -relief sunken -bd 2 -width 70\
209  -yscrollcommand ".wtomo.yscroll set" \
210  -setgrid 1 -highlightthickness 0 -pady 2 -padx 3 -font $cour12
211  scrollbar $w.yscroll -command ".wtomo.table yview" \
212  -highlightthickness 0 -orient vertical
213  grid $w.table -in $w.frame -padx 1 -pady 1 \
214  -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
215  grid $w.yscroll -in $w.frame -padx 1 -pady 1 \
216  -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
217  grid rowconfig $w.frame 0 -weight 1 -minsize 0
218  grid columnconfig $w.frame 0 -weight 1 -minsize 0
219 
220  pack $w.switches $w.thick $w.tilt_axis $w.marker_radius $w.marker $w.selected \
221  $w.resid $w.fom $w.field $w.neighbor -side top -pady 2 -padx 5 -anchor w
222  pack $w.header -side top -fill x -padx 5
223  pack $w.frame -expand yes -fill both -padx 5 -pady 5
224 
225 # puts "Tomo window setup done"
226 
227  updateTomoTable
228  updateMarkerParam
229 # puts [getPixelSizeEntry]
230  markerDrawAll
231 
232  } else {
233  wm deiconify $w
234  raise $w
235  }
236 
237  wm title $w "Tomography"
238  wm iconname $w "Tomography"
239  if { $tool == "marker_group" } {
240  set n [.image.scale get]
241  set mg_item [micrographItem $n]
242  set markers_selected [Bmg marker $mg_item selectrect -10000 -10000 10000 10000]
243  }
244 
245  .menuBar.window entryconfigure "Tomography" -state normal
246  bind $c <1> "markerCreateSelect %x %y"
247  bind $c <2> "markerCreateSelect %x %y"
248  bind $c <Shift-1> "markerDelete %x %y"
249  bind $c <Shift-2> "markerDelete %x %y"
250  bind $c <B1-Motion> "markerMove %x %y"
251  bind $c <B2-Motion> "markerMove %x %y"
252  bind $c <ButtonRelease-1> "markerSetSelection %x %y"
253  bind $c <ButtonRelease-2> "markerSetSelection %x %y"
254  bind $w <Return> "Update 0"
255  bind $w <Control-w> { destroy .wtomo }
256  bind $w <Command-w> { destroy .wtomo }
257  bind . <Key-a> "markerAcceptModelLocation"
258 # bind . <Key-i> "invertSelection $c"
259  bind $c <Control-1> "markerSelectAppend %x %y"
260  bind $c <Command-1> "markerSelectAppend %x %y"
261 }
262 
263 ## @brief Sets the tilt axis angle
264 #
265 
266 proc setTiltAxis { } {
267  global tilt_axis project_item
268  set tilt_axis [.wtomo.tilt_axis.e get]
269  if [Bmg exists] {
270  Bmg set $project_item axis $tilt_axis
271  updateTomoTable
272  }
273 }
274 
275 ## @brief Dialog box to set the tilts for a tomographic series
276 
277 proc Tilts { } {
278  global helv12
279  global tilt_axis project_item
280  set tilt_axis [.wtomo.tilt_axis.e get]
281  set tilt_step 1
282  set tilt_start -60
283  if [Bmg exists] {
284  set mg_item [micrographItem 0]
285  set tilt_axis [expr [Bmg get $mg_item axis]]
286  set tilt_start [expr [Bmg get $mg_item tilt]]
287  set mg_item [micrographItem 1]
288  set tilt_step [expr [Bmg get $mg_item tilt] - $tilt_start]
289  }
290  set w .wtilt
291  if ![winfo exists $w] {
292  toplevel $w
293 
294  setupEntry $w.tilt_step "Tilt step " double $tilt_step \
295  "The angular increment between micrographs"
296 
297  setupEntry $w.tilt_start "Starting angle" double $tilt_start \
298  "The tilt angle of the first micrograph"
299 
300  pack $w.tilt_start $w.tilt_step -side top -fill x -pady 2 -padx 2
301 
302  frame $w.buttons
303  button $w.buttons.ok -text OK -command "setTilts .wtilt"
304  button $w.buttons.cancel -text Cancel -command "destroy .wtilt"
305  pack $w.buttons -side bottom -fill x -pady 2m
306  pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
307 
308  bind $w <Return> "setTilts .wtilt"
309  } else {
310  wm deiconify $w
311  raise $w
312  }
313  wm title $w "Tilts"
314  wm iconname $w "Tilts"
315 }
316 
317 ## @brief Generates the tilt list for a tomographic series.
318 #
319 # @param w the window to destroy.
320 
321 proc setTilts { w } {
322  global tilt_axis project_item
323  set step [expr [$w.tilt_step.e get]]
324  set start [expr [$w.tilt_start.e get]]
325  destroy $w
326  set nimg [Bmg get $project_item number_of_mg]
327 # puts "Number of micrographs = $nimg"
328  setTiltAxis
329  for {set i 0} {$i < $nimg} {incr i 1} {
330  set tilt [expr $start + $i * $step]
331  set mg_item [micrographItem $i]
332  Bmg set $mg_item tilt $tilt
333  }
334  Bmg update_matrices
335  updateTomoTable
336 }
337 
338 ## @brief Reads tilt angles from a rawtlt file
339 #
340 
341 proc readTlt { } {
342  global project_item
343  set types {
344  {"tlt files" {.tlt .TLT} }
345  {"rawtlt files" {.rawtlt .RAWTLT} }
346  }
347  set rawtltfile [tk_getOpenFile -filetypes $types]
348  if { [string length $rawtltfile] < 1 } { return }
349  set rawtltID [open $rawtltfile r]
350  set nimg [Bmg get $project_item number_of_mg]
351  set i 0
352  foreach line [split [read $rawtltID] \n] {
353  if { [scan $line "%g" tilt ] > 0 } {
354  if { $i < $nimg } {
355  Bmg set $project_item tilt $tilt $i
356  }
357  incr i 1
358  }
359  }
360  close $rawtltID
361  set tilt_axis [.wtomo.tilt_axis.e get]
362  Bmg update_matrices
363  updateTomoTable
364 }
365 
366 ## @brief Draws the tilt axis on the canvas
367 #
368 
369 proc drawTiltAxis { } {
370  global theimg PI
371  global show_tilt_axis project_item
372  if ![Bmg exists] { return }
373  set c [getImageCanvas $theimg]
374  set wc [getControlWindow $theimg]
375  $c delete tilt_axis
376  if { [$wc.slice.scale get] > 1 } { set show_tilt_axis 0 }
377  if { $show_tilt_axis < 1 } { return }
378  set img_num [$wc.image.scale get]
379  set mg_item [micrographItem $img_num]
380  set tax [expr [Bmg get $mg_item axis] * $PI/180.0]
381  set ang [expr [Bmg get $mg_item tilt] * $PI/180.0]
382 # puts "$mg_item $tax $ang"
383  set img_num [$wc.image.scale get]
384  set scale [$wc.scale.scale get]
385  set height [Bimage get $theimg height]
386  set radius [expr $height * $scale / 2]
387  set origin [Bmg get $mg_item origin]
388  set ox [expr [lindex $origin 0] * $scale]
389  set oy [expr ($height - [lindex $origin 1] - 1) * $scale]
390  set ct [expr cos($tax)]
391  set st [expr sin($tax)]
392  set dx [expr $radius*$ct]
393  set dy [expr $radius*$st]
394  set xs [expr $ox - $dx]
395  set ys [expr $oy + $dy]
396  set xe [expr $ox + $dx]
397  set ye [expr $oy - $dy]
398  $c create line $xs $ys $xe $ye -fill LightBlue -dash - -width 1.5 -tags tilt_axis
399 # puts "$tax $xs $ys $xe $ye"
400  # Note that this line points to the tomogram z-axis
401  set vlen [expr 0.1 * $height * $scale * sin($ang)]
402  set xe [expr $ox + $vlen*$st]
403  set ye [expr $oy + $vlen*$ct]
404  $c create line $ox $oy $xe $ye -fill LightBlue -width 1.5 -tags tilt_axis
405 # puts "$vlen $ox $oy $xe $ye"
406 }
407 
408 ## @brief Selects a marker
409 #
410 # @param x
411 # @param y Marker position in canvas coordinates.
412 
413 proc markerSelect { x y } {
414  if ![winfo exists .wtomo] { return }
415  global theimg project_item
416  global marker_radius markers_selected
417  global px py
418  global mx my
419  set c [getImageCanvas $theimg]
420  set wc [getControlWindow $theimg]
421  set marker_radius [.wtomo.marker_radius.e get]
422  Bmg set $project_item marker_radius $marker_radius
423 # puts $marker_radius
424  set n [$wc.image.scale get]
425  set z [$wc.slice.scale get]
426  set scale [$wc.scale.scale get]
427  set px [expr floor( [$c canvasx $x] / $scale ) ]
428  set py [expr floor( [$c canvasy $y] / $scale ) ]
429  set mx [expr int($px)]
430  set my [expr int($py)]
431  set myi [yFlip $my]
432  set mg_item [micrographItem $n]
433  set id [Bmg marker $mg_item select $mx $myi $z]
434  set markers_selected $id
435  set res [Bmg marker $mg_item residual $id]
436  set fom [Bmg marker $mg_item fom $id]
437  .wtomo.selected.num config -text "$id"
438  .wtomo.selected.res config -text "$res"
439  .wtomo.selected.fom config -text "$fom"
440  return $id
441 }
442 
443 ## @brief Appends marker at a location to list of selected markers.
444 #
445 # @param x
446 # @param y Cursor position in window coordinates.
447 
448 proc markerSelectAppend { x y } {
449  global markers_selected
450  set id [markerSelect $x $y]
451  if { $id < 1 } { return }
452  if { [lsearch $markers_selected $id] < 0 } {
453  lappend markers_selected $id
454  }
455 }
456 
457 ## @brief The marker selection is reset and set to within a rectangle.
458 #
459 # @param x
460 # @param y Marker position in canvas coordinates.
461 
462 proc markerSetSelection { x y } {
463  global theimg project_item
464  global markers_selected
465  global tool
466  global px py
467 # set markers_selected []
468  if { $tool != "marker_group" } { return }
469  set c [getImageCanvas $theimg]
470  set wc [getControlWindow $theimg]
471  set scale [$wc.scale.scale get]
472  set n [$wc.image.scale get]
473  set x1 $px
474  set y1 [yFlip $py]
475  set x2 [expr [$c canvasx $x] / $scale ]
476  set y2 [yFlip [expr [$c canvasx $y] / $scale ]]
477 # puts "$x1 $y1 $x2 $y2"
478  set mg_item [micrographItem $n]
479  set markers_selected [Bmg marker $mg_item selectrect $x1 $y1 $x2 $y2]
480 # puts "selected IDS = $markers_selected"
481  markerDrawAll
482 }
483 
484 ## @brief Selects or creates a marker
485 #
486 # @param x
487 # @param y Marker position in canvas coordinates.
488 
489 proc markerCreateSelect { x y } {
490  if ![winfo exists .wtomo] { return }
491  global theimg project_item
492  global marker_radius markers_selected
493  global tool
494  global px py
495  global mx my
496  set c [getImageCanvas $theimg]
497  set wc [getControlWindow $theimg]
498  set marker_radius [.wtomo.marker_radius.e get]
499  Bmg set $project_item marker_radius $marker_radius
500 # puts $marker_radius
501  set n [$wc.image.scale get]
502  set z [$wc.slice.scale get]
503  set scale [$wc.scale.scale get]
504  set px [expr floor( [$c canvasx $x] / $scale ) ]
505  set py [expr floor( [$c canvasy $y] / $scale ) ]
506  set mx [expr int($px)]
507  set my [expr int($py)]
508  set myi [yFlip $my]
509  set mg_item [micrographItem $n]
510  set id [Bmg marker $mg_item select $mx $myi $z]
511 # puts "selected ID = $id"
512  if { $id < 1 } {
513  $c delete selection
514  if { $tool == "marker" } {
515  set id [Bmg marker $mg_item create $mx $myi $z]
516 # puts "created ID = $id"
517  }
518  if { $tool == "marker_group" } {
519  set markers_selected []
520  }
521  }
522  if { $tool == "marker_group" } {
523  if { $id > 0 && [llength $markers_selected] == 0 } {
524  set markers_selected [Bmg marker $mg_item ids 0]
525  }
526  return
527  }
528  if { $id > 0 } {
529  set markers_selected $id
530 # Bmg marker $mg_item snap $id
531  set res [Bmg marker $mg_item residual $id]
532  set fom [Bmg marker $mg_item fom $id]
533  .wtomo.selected.num config -text "$id"
534  .wtomo.selected.res config -text "$res"
535  .wtomo.selected.fom config -text "$fom"
536  }
537 # puts "Selected IDs = $markers_selected"
538  markerDrawAll
539 # Update 0
540 }
541 
542 ## @brief Generates markers from a seed.
543 #
544 
545 proc markerGenerate { } {
546  global theimg
547  global show_tilt_axis project_item
548  set show_tilt_axis 1
549  Bmg marker $project_item generate
550  updateOrigins
551  Update 0
552 }
553 
554 ## @brief Extracts markers from a micrograph.
555 #
556 
557 proc markerExtract { } {
558  global filetypes
559  global filename project_item theimg
560  if ![winfo exists .wtomo] { return }
561  set wc [getControlWindow $theimg]
562  set xname [file root [file tail $filename]]
563  set xdir [file dirname $filename]
564  append xname "_marker.mrc"
565  set xname [tk_getSaveFile -filetypes $filetypes \
566  -initialfile $xname -initialdir $xdir -defaultextension .mrc]
567  if { [string length $xname] < 1 } { return }
568  setMgParam
569  set n [$wc.image.scale get]
570  set mg_item [micrographItem $n]
571  Bmg marker $mg_item extract $xname
572 }
573 
574 ## @brief Moves the selected markers
575 #
576 # @param x
577 # @param y Mouse position in canvas coordinates.
578 
579 proc markerMove { x y } {
580  if ![winfo exists .wtomo] { return }
581  global theimg
582  global markers_selected project_item
583  global tool
584  global px py
585  set c [getImageCanvas $theimg]
586  set wc [getControlWindow $theimg]
587  set scale [$wc.scale.scale get]
588  set cx [$c canvasx $x]
589  set cy [$c canvasy $y]
590  if { $tool == "marker_group" && [llength $markers_selected] == 0 } {
591  $c delete selection
592  set cpx [expr int($px * $scale)]
593  set cpy [expr int($py * $scale) ]
594  $c create rectangle $cpx $cpy $cx $cy -tags selection -outline red
595  return
596  }
597  set n [$wc.image.scale get]
598  set ix [expr $cx / $scale ]
599  set iy [expr $cy / $scale ]
600  set dx [expr $ix - $px]
601  set dy [expr $py - $iy]
602  set px $ix
603  set py $iy
604  set mg_item [micrographItem $n]
605  foreach id $markers_selected {
606  Bmg marker $mg_item move $id $dx $dy
607  }
608  markerDrawAll
609 }
610 
611 ## @brief Draws a marker with an error line and label.
612 #
613 # @param id Marker identifier.
614 
615 proc markerDraw { id } {
616  global theimg project_item
617  global show_marker_errors show_marker_labels
618  global marker_color marker2_color marker_select_color error_color error_select_color
619  global marker_radius markers_selected
620  if ![winfo exists .wtomo] { return }
621  set c [getImageCanvas $theimg]
622  set wc [getControlWindow $theimg]
623  set z [$wc.slice.scale get]
624  set n [$wc.image.scale get]
625  set mg_item [micrographItem $n]
626  set locerr [Bmg marker $mg_item location $id]
627 # puts $locerr
628  set selflag [Bmg marker $mg_item getflag $id]
629 # puts $selflag
630  set zr [expr abs([lindex $locerr 2] - $z)]
631  set marker_radius [.wtomo.marker_radius.e get]
632  if { $zr > $marker_radius } { return }
633  set zr2 1
634  if { $zr > 0.1 && $marker_radius > 0 } {
635  set zr2 [expr sqrt(1 - $zr*$zr*1.0/($marker_radius*$marker_radius))]
636  }
637  set scale [$wc.scale.scale get]
638  set x [expr [lindex $locerr 0] * $scale ]
639  set y [expr [yFlip [lindex $locerr 1]] * $scale ]
640  set radius [expr $marker_radius * $scale * $zr2]
641  set ex [expr $x + [lindex $locerr 3] * $scale ]
642  set ey [expr $y - [lindex $locerr 4] * $scale ]
643 # puts "$id $locerr"
644  set xmin [expr $x - $radius ]
645  set xmax [expr $x + $radius ]
646  set ymin [expr $y - $radius ]
647  set ymax [expr $y + $radius ]
648  if { [lsearch $markers_selected $id] >= 0 } {
649  set markcolorshow $marker_select_color;
650  set errcolorshow $error_select_color;
651  } elseif { $selflag == 2 } {
652  set markcolorshow $marker2_color;
653  set errcolorshow $error_color;
654  } else {
655  set markcolorshow $marker_color;
656  set errcolorshow $error_color;
657  }
658  set idtag [format "m%d" $id]
659  $c create oval $xmin $ymin $xmax $ymax -outline $markcolorshow -width 1 -tags [list marker $idtag]
660  if { $show_marker_errors } {
661  $c create line $x $y $ex $ey -fill $errcolorshow -width 2 -tags [list marker $idtag]
662  }
663  if { $show_marker_labels } {
664  $c create text $xmax $y -text $id -fill $markcolorshow -anchor w -tags [list marker $idtag]
665  }
666 }
667 
668 ## @brief Redraws all markers on the canvas
669 #
670 
671 proc markerDrawAll {} {
672  global theimg project_item
673  global fom_cutoff
674  global show_markers
675  if ![winfo exists .wtomo] { return }
676  set c [getImageCanvas $theimg]
677  set wc [getControlWindow $theimg]
678  $c delete marker
679  if { $show_markers < 1 } { return }
680  set n [$wc.image.scale get]
681  set mg_item [micrographItem $n]
682 # puts "markerDrawAll $mg_item"
683  set ids [Bmg marker $mg_item ids $fom_cutoff]
684 # puts "fom_cutoff = $fom_cutoff"
685 # puts "IDs = $ids"
686  foreach id $ids {
687 # puts "drawing $id"
688  markerDraw $id
689  }
690  set nsel [Bmg marker $project_item count]
691  .wtomo.marker.num config -text "$nsel"
692 }
693 
694 ## @brief Deletes the selected marker.
695 #
696 # @param x
697 # @param y Marker position in canvas coordinates.
698 
699 proc markerDelete { x y } {
700  global project_item theimg
701  set wc [getControlWindow $theimg]
702  set n [$wc.image.scale get]
703  set id [markerSelect $x $y]
704  set mg_item [micrographItem $n]
705  if { $id > 0 } {
706  Bmg marker $mg_item delete $id
707  }
708  markerDrawAll
709 }
710 
711 ## @brief Deletes the selected marker.
712 #
713 # @param id Marker identifier.
714 
715 proc markerDeleteID { id } {
716  global project_item theimg
717  set wc [getControlWindow $theimg]
718  set n [$wc.image.scale get]
719  set mg_item [micrographItem $n]
720  if { $id > 0 } {
721  Bmg marker $mg_item delete $id
722  }
723  markerDrawAll
724 }
725 
726 ## @brief Deletes all the selected markers.
727 #
728 
729 proc markerDeleteSelected { } {
730  global markers_selected project_item theimg
731  set wc [getControlWindow $theimg]
732  set n [$wc.image.scale get]
733 # puts "IDs = $markers_selected"
734  set mg_item [micrographItem $n]
735  foreach id $markers_selected {
736  Bmg marker $mg_item delete $id
737  }
738  markerDrawAll
739 }
740 
741 ## @brief Deletes all the selected markers in all images.
742 #
743 
744 proc markerDeleteSelectedAll { } {
745  global markers_selected project_item
746 # puts "IDs = $markers_selected"
747  foreach id $markers_selected {
748  Bmg marker all delete $id
749  }
750  markerDrawAll
751 }
752 
753 ## @brief Deletes all markers.
754 #
755 
756 proc markerDeleteAll { } {
757  global theimg project_item
758  set c [getImageCanvas $theimg]
759  Bmg marker all delete all
760  $c delete marker
761  .wtomo.marker.num config -text "0"
762 }
763 
764 ## @brief Renumbers markers in the current micrograph.
765 #
766 
767 proc markerRenumber { } {
768  global project_item theimg
769  set wc [getControlWindow $theimg]
770  set n [$wc.image.scale get]
771  set mg_item [micrographItem $n]
772  Bmg marker $mg_item renumber
773  markerDrawAll
774 }
775 
776 ## @brief Centers markers in the current micrograph.
777 #
778 
779 proc markerCenter { } {
780  global project_item theimg
781  set wc [getControlWindow $theimg]
782  set n [$wc.image.scale get]
783  set mg_item [micrographItem $n]
784  Bmg marker $mg_item center
785  markerDrawAll
786 }
787 
788 
789 ## @brief Finds markers in a file.
790 #
791 # @param n Image number.
792 
793 proc markerFind { n } {
794  global theimg
795  global marker_radius
796  global fom_cutoff project_item
797  if ![winfo exists .wtomo] { return }
798  set marker_radius [.wtomo.marker_radius.e get]
799  set mg_item [micrographItem $n]
800  set nmark [Bmg marker $mg_item find $marker_radius]
801  if { $nmark < 1 } {
802  tk_messageBox -icon error -type ok -title "Error" -message \
803  "No markers found!"
804  return
805  }
806 
807  set max_fom [Bmg marker $mg_item fom_maximum]
808  set fom_cutoff [expr $max_fom / 2]
809  set fom_inc [expr $max_fom/100]
810  set h [Bimage get $theimg height]
811  set maxedge [expr int($h / 4)]
812  markerDrawAll
813 
814  if { $nmark } {
815  set w .wtomfom
816  catch {destroy $w}
817  toplevel $w
818  wm title $w "Set the FOM cutoff"
819  wm iconname $w "FOMcut"
820  tkwait visibility $w
821  grab $w
822  frame $w.fom
823  label $w.fom.label -text "Choose a FOM cutoff and close this window before doing anything else" -anchor w
824  scale $w.fom.scale -orient horizontal -length 500 -from 0 -to $max_fom \
825  -command { setFOMcutoff } -resolution $fom_inc
826  label $w.fom.edgelabel -text "Choose an edge width in pixels to exclude markers" -anchor w
827  scale $w.fom.edge -orient horizontal -length 500 -from 0 -to $maxedge \
828  -command { setEdge } -resolution 1
829  button $w.fom.done -text "Done" -relief raised -command { destroy .wtomfom }
830  pack $w.fom.label -side top -fill x
831  pack $w.fom.scale -side top -fill x
832  pack $w.fom.edgelabel -side top -fill x
833  pack $w.fom.edge -side top -fill x
834  pack $w.fom.done -side right
835  $w.fom.scale set $fom_cutoff
836  pack $w.fom -side top -fill x -pady 2
837  tkwait window $w
838  }
839 
840  Bmg marker $mg_item delete fom $fom_cutoff
841  Bmg marker $mg_item renumber
842  markerDrawAll
843  set fom_cutoff 0.0
844 }
845 
846 proc setEdge { edge } {
847  global project_item theimg
848  set wc [getControlWindow $theimg]
849  set n [$wc.image.scale get]
850  set mg_item [micrographItem $n]
851  Bmg marker $mg_item edge $edge
852  Update 0
853 }
854 
855 ## @brief Centers the selected marker.
856 #
857 # @param x
858 # @param y Marker position in canvas coordinates.
859 
860 proc markerSnap { x y } {
861  global project_item theimg
862  set wc [getControlWindow $theimg]
863  set n [$wc.image.scale get]
864  set id [markerSelect $x $y]
865  set mg_item [micrographItem $n]
866  if { $id > 0 } {
867  Bmg marker $mg_item snap $id
868  }
869  markerDrawAll
870 }
871 
872 ## @brief Updates the residual calculation which also sets all marker residuals.
873 #
874 
875 proc updateResidual { } {
876  global project_item
877  if ![winfo exists .wtomo] { return }
878 # puts "Updating residuals"
879  set resid [Bmg marker $project_item residual]
880  .wtomo.resid.val config -text $resid
881 }
882 
883 ## @brief Updates the tomographic table from the micrograph parameters in memory.
884 #
885 
886 proc updateTomoTable { } {
887  global tilt_axis project_item
888  if ![winfo exists .wtomo] { return }
889  if ![Bmg exists] { return }
890  set act [Bmg get all active]
891 # puts "Updating tomo table with active = $act"
892  set nmg [Bmg get $project_item number_of_mg]
893  if { $nmg < 1 } { return }
894 # puts "Number of micrographs: $nmg"
895  Bmg set all active 0
896  updateResidual
897  set wt 0.0
898  set tax 0.0
899  set table ""
900  for { set i 0 } { $i < $nmg } { incr i 1 } {
901  set mg_item [micrographItem $i]
902 # puts "Project: $project_item mg: $mg_item"
903  set axis [expr [Bmg get $mg_item axis]]
904  set tilt [expr [Bmg get $mg_item tilt]]
905  set level [expr [Bmg get $mg_item level]]
906  set origin [Bmg get $mg_item origin]
907  set scale [Bmg get $mg_item scale]
908  set ox [lindex $origin 0]
909  set oy [lindex $origin 1]
910  set sx [lindex $scale 0]
911  set sy [lindex $scale 1]
912  set fom [Bmg get $mg_item fom]
913 # puts "$i $tilt $axis $level $ox $oy $sx $sy $fom"
914  append table [format " %3d %6.2f %6.2f %6.2f %6.1f %6.1f %5.3f %5.3f %6.3f\n" $i $tilt $axis $level $ox $oy $sx $sy $fom]
915  if { [expr abs($tilt)] > 2 } {
916  set w1 [expr 1 - exp(-$tilt*$tilt/0.5)]
917  set tax [expr $tax + $axis]
918  set wt [expr $wt + $w1]
919  }
920  }
921  Bmg set all active $act
922  if { $wt } { set tilt_axis [expr $tax / $wt] }
923  .wtomo.tilt_axis.e delete 0 end
924  .wtomo.tilt_axis.e insert 0 $tilt_axis
925  .wtomo.field.str config -text [Bmg get $project_item field]
926  .wtomo.table delete 1.0 end
927  .wtomo.table insert 1.0 $table
928  drawTiltAxis
929 }
930 
931 ## @brief Updates marker parameters from the micrograph parameters in memory.
932 #
933 
934 proc updateMarkerParam { } {
935  global marker_radius project_item
936  if ![Bmg exists] { return }
937 # puts "Updating marker parameters"
938  updatePixelsize
939  set r [Bmg get $project_item marker_radius]
940  if { $r } {
941  set marker_radius $r
942  }
943  Bmg set $project_item marker_radius $r
944  set w .wtomo
945  if [winfo exists $w] {
946 # updateItemID $w.itemid
947  $w.marker_radius.e delete 0 end
948  $w.marker_radius.e insert 0 $marker_radius
949  }
950 }
951 
952 ## @brief Sets the selection flag for the selected markers.
953 #
954 
955 proc markerSetSelectFlag { } {
956  global markers_selected project_item theimg
957  set wc [getControlWindow $theimg]
958  set n [$wc.image.scale get]
959  set mg_item [micrographItem $n]
960  foreach id $markers_selected {
961  Bmg marker $mg_item setflag $id
962  }
963  updateMarkerTable
964 }
965 
966 ## @brief Clears the selection flag for the selected markers.
967 #
968 
969 proc markerClearSelectFlag { } {
970  global markers_selected project_item theimg
971  set wc [getControlWindow $theimg]
972  set n [$wc.image.scale get]
973  set mg_item [micrographItem $n]
974  foreach id $markers_selected {
975  Bmg marker $mg_item clearflag $id
976  }
977  updateMarkerTable
978 }
979 
980 ## @brief Sets up the window for the marker table and updates it.
981 #
982 
983 proc markerTable { } {
984  if ![winfo exists .wtomo] { return }
985  if ![Bmg exists] { return }
986  global cour12 helv12
987  global refop hires lores
988  set w .wmark
989  if ![winfo exists $w] {
990  toplevel $w
991 
992  frame $w.frame
993  text $w.table -relief sunken -bd 2 -width 40\
994  -yscrollcommand "$w.yscroll set" \
995  -setgrid 1 -highlightthickness 0 -pady 2 -padx 3 -font $cour12
996  scrollbar $w.yscroll -command "$w.table yview" \
997  -highlightthickness 0 -orient vertical
998  grid $w.table -in $w.frame -padx 1 -pady 1 \
999  -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
1000  grid $w.yscroll -in $w.frame -padx 1 -pady 1 \
1001  -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
1002  grid rowconfig $w.frame 0 -weight 1 -minsize 0
1003  grid columnconfig $w.frame 0 -weight 1 -minsize 0
1004 
1005  label $w.header -font $helv12 -anchor w \
1006  -text " Mg Marker Residual FOM Select"
1007 
1008  pack $w.header -side top -fill x -padx 5
1009  pack $w.frame -expand yes -fill both -padx 5 -pady 5
1010 
1011  frame $w.butres
1012  label $w.butres.label -font $helv12 -anchor w -text Residual -width 8
1013  button $w.butres.min -text "|<" -command "markerNextResidual $w min"
1014  button $w.butres.lower -text "<" -command "markerNextResidual $w lower"
1015  button $w.butres.higher -text ">" -command "markerNextResidual $w higher"
1016  button $w.butres.max -text ">|" -command "markerNextResidual $w max"
1017  entry $w.butres.entry -width 10 -validate key -vcmd [list checkType double %P]
1018 
1019  frame $w.butfom
1020  label $w.butfom.label -font $helv12 -anchor w -text FOM -width 8
1021  button $w.butfom.min -text "|<" -command "markerNextFOM $w min"
1022  button $w.butfom.lower -text "<" -command "markerNextFOM $w lower"
1023  button $w.butfom.higher -text ">" -command "markerNextFOM $w higher"
1024  button $w.butfom.max -text ">|" -command "markerNextFOM $w max"
1025  entry $w.butfom.entry -width 10 -validate key -vcmd [list checkType double %P]
1026 
1027  frame $w.flag
1028  button $w.flag.clear -text "Clear selection" -command "markerClearSelectFlag"
1029  button $w.flag.set -text "Set selection" -command "markerSetSelectFlag"
1030 
1031  frame $w.buttons
1032  button $w.buttons.update -text Update -command "updateMarkerTable"
1033  button $w.buttons.close -text Close -command "destroy $w"
1034 
1035  pack $w.butres.label $w.butres.min $w.butres.lower \
1036  $w.butres.entry $w.butres.higher $w.butres.max -side left -expand 1
1037  pack $w.butfom.label $w.butfom.min $w.butfom.lower \
1038  $w.butfom.entry $w.butfom.higher $w.butfom.max -side left -expand 1
1039 # pack $w.flag.clear $w.flag.set -side left -expand 1
1040  pack $w.buttons.update $w.buttons.close -side left -expand 1
1041 
1042  pack $w.buttons -side bottom -fill x -pady 2m
1043  pack $w.flag -side bottom -fill x -pady 2m
1044  pack $w.butfom -side bottom -fill x -pady 2m
1045  pack $w.butres -side bottom -fill x -pady 2m
1046 
1047  $w.table tag configure selmark -background #a0b7ce
1048  } else {
1049  wm deiconify $w
1050  raise $w
1051  }
1052  wm title $w "Marker list"
1053  wm iconname $w "Markers"
1054  updateMarkerTable
1055 }
1056 
1057 ## @brief Updates the marker table.
1058 #
1059 
1060 proc updateMarkerTable {} {
1061  global markers_selected project_item
1062  if ![winfo exists .wmark] { return }
1063 # puts "Updating tomography table parameters"
1064  updateResidual
1065  set m [Bmg marker $project_item list]
1066  set table ""
1067  foreach {img_num id res fom sel} $m {
1068  append table [format " %4d %4d %7.3f %7.3f %4d\n" $img_num $id $res $fom $sel]
1069  }
1070  .wmark.table delete 1.0 end
1071  .wmark.table insert 1.0 $table
1072 }
1073 
1074 ## @brief Centers the viewable canvas on the location in the image.
1075 #
1076 # @param img_num image number.
1077 # @param loc location list.
1078 
1079 proc moveCanvasTo { img_num loc } {
1080  global theimg
1081  set c [getImageCanvas $theimg]
1082  set wc [getControlWindow $theimg]
1083  $wc.image.scale set $img_num
1084  set scale [$wc.scale.scale get]
1085  set wd [Bimage get $theimg width]
1086  set ht [Bimage get $theimg height]
1087  set ws [expr [winfo width $c] / (2.0 * $scale)]
1088  set hs [expr [winfo height $c] / (2.0 * $scale)]
1089  set fx [expr ([lindex $loc 0] - $ws) / $wd ]
1090  set fy [expr ($ht - [lindex $loc 1] - 1 - $hs) / $ht ]
1091  $c xview moveto $fx
1092  $c yview moveto $fy
1093 }
1094 
1095 ## @brief Goes to a marker.
1096 #
1097 # @param w dialog window.
1098 # @param id marker identifier
1099 # @param isel offset in table ?
1100 # @param img micrograph index
1101 
1102 proc goToMarker { w id isel img } {
1103  global markers_selected project_item
1104  if { $id <= 0 } { return }
1105  set markers_selected $id
1106  set mg_item [micrographItem $img]
1107  set locerr [Bmg marker $mg_item location $id]
1108  moveCanvasTo $img $locerr
1109  $w.butres.entry delete 0 end
1110  $w.butres.entry insert 0 [Bmg marker $mg_item residual $id]
1111  $w.butfom.entry delete 0 end
1112  $w.butfom.entry insert 0 [Bmg marker $mg_item fom $id]
1113  $w.table tag add selmark $isel.0 $isel.end
1114  set isel [expr $isel - 3]
1115  if { $isel < 0 } { set isel 0 }
1116  $w.table yview $isel
1117  markerDrawAll
1118 }
1119 
1120 ## @brief Goes to the next marker based on its residual.
1121 #
1122 # @param w dialog window.
1123 # @param dir direction: min, lower, higher, max
1124 
1125 proc markerNextResidual { w dir } {
1126  global markers_selected
1127  global theimg project_item
1128  set wc [getControlWindow $theimg]
1129  $w.table tag remove selmark 1.0 end
1130  set sgn 1
1131  set n -1
1132  set previd -1
1133  set prevres -1
1134  set ressel -1
1135  if { $dir == "min" || $dir == "higher" } {
1136  set sgn -1
1137  set ressel 1e10
1138  }
1139  if { $dir == "max" } { set prevres 1e10 }
1140  if { $dir == "min" || $dir == "max" } {
1141  set markers_selected []
1142  }
1143  if { [llength $markers_selected] > 0 } {
1144  set n [$wc.image.scale get]
1145  set mg_item [micrographItem $n]
1146  set previd [lindex $markers_selected 0]
1147  set prevres [Bmg marker $mg_item residual $previd]
1148 # puts "1. $n $previd $prevres"
1149  }
1150  set m [Bmg marker $project_item list]
1151  set i 0
1152  set isel 0
1153  set img 0
1154  set idsel 0
1155  foreach {img_num id res fom sel} $m {
1156  incr i
1157  if { $sel } {
1158  set dr [expr $sgn * ($ressel - $res)]
1159  set pdr [expr $sgn * ($res - $prevres)]
1160  if { !($img_num == $n && $id == $previd) && $dr < 0 && $pdr < 0 } {
1161  set img $img_num
1162  set idsel $id
1163  set ressel $res
1164  set isel $i
1165  }
1166  }
1167  }
1168  if { $idsel > 0 } {
1169  goToMarker $w $idsel $isel $img
1170  }
1171 # puts "2. $img $idsel $ressel"
1172 }
1173 
1174 ## @brief Goes to next marker based on its FOM.
1175 #
1176 # @param w dialog window.
1177 # @param dir direction: min, lower, higher, max
1178 
1179 proc markerNextFOM { w dir } {
1180  global markers_selected
1181  global theimg project_item
1182  set wc [getControlWindow $theimg]
1183  $w.table tag remove selmark 1.0 end
1184  set sgn 1
1185  set n -1
1186  set previd -1
1187  set prevfom -1
1188  set fomsel -1
1189  if { $dir == "min" || $dir == "higher" } {
1190  set sgn -1
1191  set fomsel 1e10
1192  }
1193  if { $dir == "max" } { set prevfom 1e10 }
1194  if { $dir == "min" || $dir == "max" } {
1195  set markers_selected []
1196  }
1197  if { [llength $markers_selected] > 0 } {
1198  set n [$wc.image.scale get]
1199  set mg_item [micrographItem $n]
1200  set previd [lindex $markers_selected 0]
1201  set prevfom [Bmg marker $mg_item fom $previd]
1202 # puts "1. $n $previd $prevfom"
1203  }
1204  set m [Bmg marker $project_item list]
1205  set i 0
1206  set isel 0
1207  set img 0
1208  set idsel 0
1209  foreach {img_num id res fom sel} $m {
1210  incr i
1211  if { $sel } {
1212  set df [expr $sgn * ($fomsel - $fom)]
1213  set pdf [expr $sgn * ($fom - $prevfom)]
1214  if { !($img_num == $n && $id == $previd) && $df < 0 && $pdf < 0 } {
1215  set img $img_num
1216  set idsel $id
1217  set fomsel $fom
1218  set isel $i
1219  }
1220  }
1221  }
1222  if { $idsel > 0 } {
1223  goToMarker $w $idsel $isel $img
1224  }
1225 # puts "2. $img $idsel $fomsel"
1226 }
1227 
1228 ## @brief Accepts the model locations of selected markers.
1229 #
1230 
1231 proc markerAcceptModelLocation { } {
1232  global markers_selected
1233  global theimg project_item
1234  if { [llength $markers_selected] < 1 } { return }
1235  set wc [getControlWindow $theimg]
1236  set n [$wc.image.scale get]
1237  set mg_item [micrographItem $n]
1238  foreach id $markers_selected {
1239  Bmg marker $mg_item accept $id
1240  }
1241  markerDrawAll
1242  updateMarkerTable
1243 }
1244 
1245 ##### Workflow #####
1246 
1247 ## @brief Aligns a series of frames from dose fractionation.
1248 #
1249 
1250 proc alignSumFrames {} {
1251  global paramfile counts
1252  checkParamFile
1253  set w .wframes
1254  if ![winfo exists $w] {
1255  toplevel $w
1256 
1257  set newfile [newParamFile "_framesum.star"]
1258  if ![string length $newfile] { destroy $w; return }
1259 
1260  frame $w.head
1261  label $w.head.lab -text \
1262  "Frame alignment and summation will be done using the currently saved parameter file:\n$paramfile"
1263 
1264  setupNewFileEntry $w.file "Output parameter file" $newfile \
1265  "New parameter file written after summing the frames"
1266 
1267  radiobutton $w.count -text "Convert to counts" -variable counts -value 1
1268 
1269  setupEntry $w.refnum "Reference frame" integer 3 "Number of reference frame to use for the initial progressive alignment"
1270 
1271  labelframe $w.res -text "Resolution limits"
1272  setupEntry $w.res.hi "High" double 20 "High resolution limit in Å"
1273  setupEntry $w.res.lo "Low" double 1000 "Low resolution limit in Å"
1274  pack $w.res.hi $w.res.lo -side top -fill x
1275 
1276  setupEntry $w.bin "Binning " integer 2 "Binning to speed up alignment"
1277 
1278  setupEntry $w.shift "Shift limit " double 20 "Limit to cross-correlation shift allowed"
1279 
1280  setupEntry $w.dose "Dose/frame " double 1 "Dose rate in electrons per frame per Ångstrom squared"
1281 
1282  pack $w.head.lab
1283  pack $w.head $w.file -side top -pady 2 -padx 2 -fill x
1284  pack $w.head $w.count -side top -pady 2 -padx 2 -anchor w
1285  pack $w.head $w.refnum $w.res $w.bin $w.shift $w.dose \
1286  -side top -pady 2 -padx 2 -fill x
1287 
1288  frame $w.buttons
1289  button $w.buttons.do -text Do -command "doAlignSumFrames $w"
1290  button $w.buttons.close -text Close -command "destroy $w"
1291  pack $w.buttons -side bottom -fill x -pady 2m
1292  pack $w.buttons.do $w.buttons.close -side left -expand 1
1293 
1294  bind $w <Return> "doAlignSumFrames $w"
1295  } else {
1296  wm deiconify $w
1297  raise $w
1298  }
1299  wm title $w "AlignFrames"
1300  wm iconname $w "AlignFrames"
1301 }
1302 
1303 ## @brief Aligns and averages frames.
1304 #
1305 # @param w dialog window.
1306 
1307 proc doAlignSumFrames { w } {
1308  global env paramfile counts
1309  set newfile [$w.file.e get]
1310  set refnum [$w.refnum.e get]
1311  set hires [$w.res.hi.e get]
1312  set lores [$w.res.lo.e get]
1313  set bin [$w.bin.e get]
1314  set shift [$w.shift.e get]
1315  set dose [$w.dose.e get]
1316  setMgParam
1317  if { ![file exists $paramfile] } {
1318 # puts "$paramfile not found!"
1319  tk_dialog .dialog "$paramfile not found!" \
1320  {Please save an appropriate parameter file first.} \
1321  info 0 {OK}
1322  return
1323  }
1324  set c ""
1325  if { $counts } {
1326  set c "-counts"
1327  }
1328  set logfile "[file rootname $newfile].log"
1329  set cmd "$env(BSOFT)/bin/bseries -verb 1 -frames $c -rate $dose -align $refnum -resol $hires,$lores -shift $shift -bin $bin -write sum -out $newfile $paramfile"
1330  executeCommand $cmd $logfile
1331 }
1332 
1333 ## @brief Fit the intensities of a tilt series.
1334 #
1335 
1336 proc fitIntensities {} {
1337  global paramfile project_root
1338  global thickness lambda adjust_tilt
1339  set w .wthick
1340  if ![winfo exists $w] {
1341  toplevel $w
1342 
1343  panelLambda $w
1344 
1345  checkbutton $w.flag -text "Apply tilt adjustment" \
1346  -variable adjust_tilt -relief raised
1347 
1348  set psfile [newParamFile "_int.ps"]
1349  setupPostscriptEntry $w.file "Postscript file" $psfile \
1350  "Postscript file with the fitted intensity-tilt plot"
1351 
1352  pack $w.flag $w.file -side top -pady 2 -padx 10
1353 
1354  frame $w.buttons
1355  button $w.buttons.do -text Fit -command "doFitIntensities $w"
1356  button $w.buttons.close -text Close -command "destroy $w"
1357  pack $w.buttons -side bottom -fill x -pady 2m
1358  pack $w.buttons.do $w.buttons.close -side left -expand 1
1359 
1360  bind $w.lambda.e <Return> "doFitIntensities $w"
1361  } else {
1362  wm deiconify $w
1363  raise $w
1364  }
1365  wm title $w "Fit thickness"
1366  wm iconname $w "Fit"
1367  return $thickness
1368 }
1369 
1370 
1371 ## @brief Fits the intensities of a tilt series.
1372 #
1373 
1374 proc doFitIntensities { w } {
1375  global paramfile thickness lambda adjust_tilt
1376  set lambda [$w.lambda.e get]
1377  set psfile [$w.file.e get]
1378  set thickness [Bmg thickness $lambda $adjust_tilt $psfile]
1379  updateThickness .wtomo
1380  updateTomoTable
1381 }
1382 
1383 ## @brief Calcul;ates the thickness from the marker range.
1384 #
1385 
1386 proc markerRange { } {
1387  global project_item thickness
1388  set thickness [Bmg marker $project_item z_range]
1389  updateThickness .wtomo
1390  updateTomoTable
1391 }
1392 
1393 ## @brief Normalizes a series of images based on simple statistics.
1394 #
1395 
1396 proc normalizeImages {} {
1397  global paramfile
1398  global norm_type
1399  checkParamFile
1400  set w .wnorm
1401  if ![winfo exists $w] {
1402  toplevel $w
1403 
1404  set newfile [newParamFile "_norm.star"]
1405  if ![string length $newfile] { destroy $w; return }
1406 
1407  frame $w.head
1408  label $w.head.lab -text \
1409  "The normalization will be done using the currently saved parameter file:\n$paramfile"
1410 
1411  setupNewFileEntry $w.file "Output parameter file" $newfile \
1412  "New parameter file written after normalizing the micrographs"
1413 
1414  frame $w.type
1415  label $w.type.l -text "Normalization type"
1416  tk_optionMenu $w.type.v norm_type "Simple" "Gaussian" "Poisson"
1417  pack $w.type.l $w.type.v -side left -ipadx 2 -fill x
1418 
1419  pack $w.head.lab
1420  pack $w.head $w.file $w.type -side top -pady 2 -padx 2 -anchor w
1421 
1422  frame $w.buttons
1423  button $w.buttons.do -text Do -command "doNormalizeImages $w"
1424  button $w.buttons.close -text Close -command "destroy $w"
1425  pack $w.buttons -side bottom -fill x -pady 2m
1426  pack $w.buttons.do $w.buttons.close -side left -expand 1
1427 
1428  bind $w <Return> "doNormalizeImages $w"
1429  } else {
1430  wm deiconify $w
1431  raise $w
1432  }
1433  wm title $w "Normalize"
1434  wm iconname $w "Normalize"
1435 }
1436 
1437 ## @brief Normalizes images.
1438 #
1439 # @param w dialog window.
1440 
1441 proc doNormalizeImages { w } {
1442  global env paramfile
1443  global norm_type
1444  set newfile [$w.file.e get]
1445  setMgParam
1446  if { ![file exists $paramfile] } {
1447 # puts "$paramfile not found!"
1448  tk_dialog .dialog "$paramfile not found!" \
1449  {Please save an appropriate parameter file first.} \
1450  info 0 {OK}
1451  return
1452  }
1453  set logfile "[file rootname $newfile].log"
1454  set cmd "$env(BSOFT)/bin/bnorm -v 1 -data float -type $norm_type -out $newfile $paramfile"
1455  executeCommand $cmd $logfile
1456 }
1457 
1458 ## @brief Aligns a tilt series by cross correlation.
1459 #
1460 
1461 proc alignMicrographs {} {
1462  global paramfile
1463  checkParamFile
1464  set w .wframes
1465  if ![winfo exists $w] {
1466  toplevel $w
1467 
1468  set newfile [newParamFile "_aln.star"]
1469  if ![string length $newfile] { destroy $w; return }
1470 
1471  frame $w.head
1472  label $w.head.lab -text \
1473  "Micrograph alignment will be done using the currently saved parameter file:\n$paramfile"
1474 
1475  setupNewFileEntry $w.file "Output parameter file" $newfile \
1476  "New parameter file written after aligning micrographs"
1477 
1478  setupEntry $w.iter "Iterations " integer 3 "Maximum number of iterations for alignment refinement"
1479 
1480  setupEntry $w.stop "Stopping condition " double 1.0 "Stop iterations when the average change in shift drops below this number of pixels"
1481 
1482  setupEntry $w.adj "Adjcent micrographs" integer 3 "Number of adjacent micrographs to include in each subtomogram reconstruction"
1483 
1484  setupResolutionEntry $w
1485 
1486  pack $w.head.lab
1487  pack $w.head $w.file $w.iter $w.stop $w.adj $w.res \
1488  -side top -pady 2 -padx 2 -fill x
1489 
1490  frame $w.buttons
1491  button $w.buttons.do -text Do -command "doAlignMicrographs $w"
1492  button $w.buttons.close -text Close -command "destroy $w"
1493  pack $w.buttons -side bottom -fill x -pady 2m
1494  pack $w.buttons.do $w.buttons.close -side left -expand 1
1495 
1496  bind $w <Return> "doAlignMicrographs $w"
1497  } else {
1498  wm deiconify $w
1499  raise $w
1500  }
1501  wm title $w "AlignMicrographs"
1502  wm iconname $w "AlignMicrographs"
1503 }
1504 
1505 ## @brief Aligns Micrographs.
1506 #
1507 # @param w dialog window.
1508 
1509 proc doAlignMicrographs { w } {
1510  global env paramfile lambda
1511  set newfile [$w.file.e get]
1512  set iter [$w.iter.e get]
1513  set stop [$w.stop.e get]
1514  set adj [$w.adj.e get]
1515  set hires [$w.res.hi.e get]
1516  set lores [$w.res.lo.e get]
1517  setMgParam
1518  if { ![file exists $paramfile] } {
1519 # puts "$paramfile not found!"
1520  tk_dialog .dialog "$paramfile not found!" \
1521  {Please save an appropriate parameter file first.} \
1522  info 0 {OK}
1523  return
1524  }
1525  set logfile "[file rootname $newfile].log"
1526  set cmd "$env(BSOFT)/bin/btomaln -verb 1 -align $iter,$stop,$adj -resol $hires,$lores -edge 20,3 -lambda $lambda -out $newfile $paramfile"
1527  executeCommand $cmd $logfile
1528 }
1529 
1530 # findTiltAxis
1531 # Locates the tilt axis.
1532 #
1533 
1534 proc findTiltAxis { } {
1535  global paramfile
1536  global hires lores
1537  checkParamFile
1538  set w .wtax
1539  if ![winfo exists $w] {
1540  toplevel $w
1541 
1542  set newfile [newParamFile "_findaxis.star"]
1543  if ![string length $newfile] { destroy $w; return }
1544 
1545  frame $w.head
1546  label $w.head.lab -text \
1547  "The search for the tilt axis angle will use the currently saved parameter file:\n$paramfile"
1548 
1549  setupNewFileEntry $w.file "Output parameter file" $newfile \
1550  "New parameter file written after searching for the tilt axis"
1551 
1552  setupEntry $w.tlt "Tilt angle" double 15 "Tilt angle to find tilt axis"
1553 
1554  labelframe $w.tax -text "Tilt axis search range"
1555  setupEntry $w.tax.start "Start" double -180 "Starting angle to test for the tilt axis"
1556  setupEntry $w.tax.end "End" double 180 "Ending angle to test for the tilt axis"
1557  setupEntry $w.tax.inc "Increment" double 1 "Angle increment to test for the tilt axis"
1558  pack $w.tax.start $w.tax.end $w.tax.inc -side top -fill x
1559 
1560  setupResolutionEntry $w
1561 
1562  pack $w.head.lab
1563  pack $w.head $w.file $w.tlt $w.tax $w.res -side top -fill x -pady 2 -padx 2
1564 
1565  frame $w.buttons
1566  button $w.buttons.do -text Do -command "doFindTiltAxis $w"
1567  button $w.buttons.close -text Close -command "destroy $w"
1568  pack $w.buttons -side bottom -fill x -pady 2m
1569  pack $w.buttons.do $w.buttons.close -side left -expand 1
1570 
1571  bind $w <Return> "doFindTiltAxis $w"
1572  } else {
1573  wm deiconify $w
1574  raise $w
1575  }
1576  wm title $w "FindAxis"
1577  wm iconname $w "FindAxis"
1578 }
1579 
1580 ## @brief Executes the tilt axis search.
1581 #
1582 # @param w dialog window.
1583 
1584 proc doFindTiltAxis { w } {
1585  global env paramfile
1586  global tilt_axis hires lores
1587  set newfile [$w.file.e get]
1588  set tlt [$w.tlt.e get]
1589  set start [$w.tax.start.e get]
1590  set end [$w.tax.end.e get]
1591  set step [$w.tax.inc.e get]
1592  set hires [$w.res.hi.e get]
1593  set lores [$w.res.lo.e get]
1594  setMgParam
1595  if { ![file exists $paramfile] } {
1596 # puts "$paramfile not found!"
1597  tk_dialog .dialog "$paramfile not found!" \
1598  {Please save an appropriate parameter file first.} \
1599  info 0 {OK}
1600  return
1601  }
1602  set logfile "[file rootname $newfile].log"
1603 # Bmg findaxis $axis $start $end $step $hires $lores
1604  set cmd "$env(BSOFT)/bin/btrack -verb 1 -findaxis $tlt,$step,$start,$end -resol $hires,$lores -out $newfile $paramfile"
1605  executeCommand $cmd $logfile
1606 }
1607 
1608 # markerTransfer
1609 # Transfers markers from one tilt series to a second.
1610 #
1611 
1612 proc markerTransfer { } {
1613  global paramfile
1614  global hires lores refine
1615  checkParamFile
1616  set w .wtrans
1617  if ![winfo exists $w] {
1618  toplevel $w
1619 
1620  labelframe $w.ang -text "Rotation angle search range"
1621  setupEntry $w.ang.start "Start" double -180 "Starting angle to find transfer rotation"
1622  setupEntry $w.ang.end "End" double 180 "Ending angle to find transfer rotation"
1623  setupEntry $w.ang.inc "Increment" double 1 "Angle increment to find transfer rotation"
1624  pack $w.ang.start $w.ang.end $w.ang.inc -side top -fill x
1625 
1626  setupResolutionEntry $w
1627 
1628  setupEntry $w.shift "Shift limit" double 100 "Limit on how much the second image may shift to fit the first"
1629 
1630  checkbutton $w.refine -text "Refine markers" \
1631  -variable refine -anchor w
1632 
1633  pack $w.ang $w.res $w.shift -side top -fill x -pady 2 -padx 2
1634  pack $w.ang $w.refine -side top -pady 2 -padx 2
1635 
1636  frame $w.buttons
1637  button $w.buttons.do -text Do -command "doMarkerTransfer $w"
1638  button $w.buttons.close -text Close -command "destroy $w"
1639  pack $w.buttons -side bottom -fill x -pady 2m
1640  pack $w.buttons.do $w.buttons.close -side left -expand 1
1641 
1642  bind $w <Return> "doMarkerTransfer $w"
1643  } else {
1644  wm deiconify $w
1645  raise $w
1646  }
1647  wm title $w "Transfer"
1648  wm iconname $w "Transfer"
1649 }
1650 
1651 ## @brief Executes the marker transfer.
1652 #
1653 # @param w dialog window.
1654 
1655 proc doMarkerTransfer { w } {
1656  global env paramfile
1657  global tilt_axis hires lores refine
1658  set start [$w.ang.start.e get]
1659  set end [$w.ang.end.e get]
1660  set step [$w.ang.inc.e get]
1661  set hires [$w.res.hi.e get]
1662  set lores [$w.res.lo.e get]
1663  set shift [$w.shift.e get]
1664  setMgParam
1665  if { ![file exists $paramfile] } {
1666 # puts "$paramfile not found!"
1667  tk_dialog .dialog "$paramfile not found!" \
1668  {Please save an appropriate parameter file first.} \
1669  info 0 {OK}
1670  return
1671  }
1672  Bmg transfer $start $end $step $hires $lores $shift $refine
1673  updateOrigins
1674  updateTomoTable
1675  markerDrawAll
1676 }
1677 
1678 ## @brief Tracks the markers through the tilt series.
1679 #
1680 
1681 proc markerTrack { } {
1682  global paramfile
1683  global hires lores recenter cc_type refine
1684  global shift_limit thickness
1685  checkParamFile
1686  set w .wtrk
1687  if ![winfo exists $w] {
1688  toplevel $w
1689 
1690  set newfile [newParamFile "_trk.star"]
1691  if ![string length $newfile] { destroy $w; return }
1692 
1693  set logfile [file rootname $paramfile]
1694  append logfile "_trk.log"
1695 
1696  frame $w.head
1697  label $w.head.lab -text \
1698  "Tracking will be done on the currently saved parameter file:\n $paramfile"
1699  pack $w.head.lab
1700 
1701  setupNewFileEntry $w.file "Output parameter file" $newfile \
1702  "New parameter file written after tracking"
1703 
1704  setupEntry $w.track "Iterations " integer 5 "Number of iterative tracking cycles"
1705  setupEntry $w.term "Target residual " double 1 \
1706  "The desired residual to terminate before the set number of cycles"
1707 
1708  setupResolutionEntry $w
1709 
1710  setupEntry $w.shift "Shift limit " double $shift_limit \
1711  "The maximum number of pixels of shift allowed"
1712  setupEntry $w.thick "Thickness " double $thickness \
1713  "The estimated maximum thickness of the tomogram"
1714 
1715  frame $w.cb
1716  checkbutton $w.cb.cc_type -text "Cross-correlate markers" -anchor e -variable cc_type
1717  checkbutton $w.cb.recenter -text "Recenter" -anchor e -variable recenter
1718  checkbutton $w.cb.refine -text "Refine markers" -variable refine
1719  pack $w.cb.cc_type $w.cb.recenter $w.cb.refine -side left -pady 2 -padx 10
1720 
1721  pack $w.head $w.file $w.track $w.term $w.res $w.shift \
1722  $w.thick $w.cb -side top -pady 2 -padx 10 -fill x
1723 # pack $w.cc_type $w.recenter -side top -pady 2 -padx 10
1724 
1725  frame $w.buttons
1726  button $w.buttons.do -text Do -command "doMarkerTrack $w"
1727  button $w.buttons.close -text Close -command "destroy $w"
1728  pack $w.buttons -side bottom -fill x -pady 2m
1729  pack $w.buttons.do $w.buttons.close -side left -expand 1
1730 
1731  bind $w <Return> "doMarkerTrack $w"
1732  } else {
1733  wm deiconify $w
1734  raise $w
1735  }
1736  wm title $w "Track"
1737  wm iconname $w "Track"
1738 }
1739 
1740 ## @brief Executes marker tracking.
1741 #
1742 # @param w dialog window.
1743 
1744 proc doMarkerTrack { w } {
1745  global env paramfile
1746  global hires lores recenter cc_type refine
1747  global shift_limit thickness
1748  set newfile [$w.file.e get]
1749  set iter [$w.track.e get]
1750  set term [$w.term.e get]
1751  set hires [$w.res.hi.e get]
1752  set lores [$w.res.lo.e get]
1753  set shift_limit [$w.shift.e get]
1754  set thickness [$w.thick.e get]
1755  setMgParam
1756  if { ![file exists $paramfile] } {
1757 # puts "$paramfile not found!"
1758  tk_dialog .dialog "$paramfile not found!" \
1759  {Please save an appropriate parameter file first.} \
1760  info 0 {OK}
1761  return
1762  }
1763  set cct ""
1764  set rec ""
1765  set ref ""
1766  if { $cc_type } { set cct "-cross" }
1767  if { $recenter } { set rec "-recenter" }
1768  if { $refine } { set ref "-refine m" }
1769  if { $term < 0.1 } { set term 1 }
1770  set logfile "[file rootname $newfile].log"
1771  set psfile "[file rootname $newfile].ps"
1772 # puts "logfile = $logfile"
1773  set cmd "$env(BSOFT)/bin/btrack -verb 1 -reset -update -track $iter,$term -resol $hires,$lores -shift $shift_limit -thick $thickness $cct $rec $ref -Post $psfile -out $newfile $paramfile"
1774  executeCommand $cmd $logfile
1775 }
1776 
1777 ## @brief Refines marker positions and micrograph geometry.
1778 #
1779 
1780 proc markerRefine { } {
1781  global helv12
1782  global hires lores ref_iter
1783  set w .wref
1784  if ![winfo exists $w] {
1785  toplevel $w
1786 
1787  frame $w.mark
1788  button $w.mark.b -text "Refine marker locations" -command "doMarkerRefine $w m"
1789  pack $w.mark.b -side left -pady 5
1790 
1791  setupResolutionEntry $w
1792 
1793  frame $w.aln
1794  button $w.aln.b -text "Refine alignment" -command "doMarkerRefine $w a"
1795  label $w.aln.tag -text Iterations
1796  tk_optionMenu $w.aln.iter ref_iter 1 2 3 4 5 6 7 8 9 10
1797  pack $w.aln.b $w.aln.tag $w.aln.iter -side left -padx 1m
1798 
1799  frame $w.z
1800  checkbutton $w.z.z -text "Marker z-coordinates" -variable ref_z
1801 
1802  frame $w.mg
1803  label $w.mg.g -text "Micrographs"
1804  checkbutton $w.mg.v -text "Views" -variable ref_view
1805  checkbutton $w.mg.o -text "Origins" -variable ref_ori
1806  checkbutton $w.mg.s -text "Scales" -variable ref_scale
1807 
1808  frame $w.iter
1809 
1810  pack $w.z.z -side left -pady 5
1811  pack $w.mg.g $w.mg.v $w.mg.o $w.mg.s -side left -pady 5
1812  pack $w.mark $w.res $w.aln $w.z $w.mg $w.iter \
1813  -side top -fill x -pady 2 -padx 2
1814 
1815  frame $w.buttons
1816 # button $w.buttons.do -text Refine -command "doMarkerRefine $w"
1817  button $w.buttons.close -text Close -command "destroy $w"
1818  pack $w.buttons -side bottom -fill x -pady 2m
1819  pack $w.buttons.close -side left -expand 1
1820 
1821  bind $w <Return> "doMarkerRefine $w a"
1822  } else {
1823  wm deiconify $w
1824  raise $w
1825  }
1826  wm title $w "Refine"
1827  wm iconname $w "Refine"
1828 }
1829 
1830 ## @brief Does a refinement of markers or views.
1831 #
1832 # @param w dialog window.
1833 
1834 proc doMarkerRefine { w refop } {
1835  global ref_z ref_view ref_ori ref_scale
1836  global hires lores ref_iter
1837  setMgParam
1838  if { $refop == "m" } {
1839  set hires [$w.res.hi.e get]
1840  set lores [$w.res.lo.e get]
1841  Bmg refine $refop $hires $lores
1842  } else {
1843  set refop ""
1844  if $ref_z { append refop "z" }
1845  if $ref_view { append refop "v" }
1846  if $ref_ori { append refop "o" }
1847  if $ref_scale { append refop "s" }
1848  puts "doMarkerRefine: $refop $ref_iter"
1849 # Bmg refine $refop $ref_view $ref_ori $ref_scale
1850  Bmg refine $refop $ref_iter
1851  }
1852  updateOrigins
1853  updateTomoTable
1854  markerDrawAll
1855 }
1856 
1857 ## @brief Refines one marker position.
1858 #
1859 
1860 proc refineMarker { } {
1861  global markers_selected
1862  global hires lores
1863  set hires 20
1864  set lores 1000
1865  set w .wref
1866  if [winfo exists $w] {
1867  set hires [$w.res.hi get]
1868  set lores [$w.res.lo get]
1869  }
1870  Bmg refine_one $markers_selected $hires $lores
1871  updateTomoTable
1872  markerDrawAll
1873 }
1874 
1875 ## @brief Dialog box for calculating tilt-adjusted power spectra
1876 #
1877 
1878 proc tomoPowerSpectra { } {
1879  global paramfile theimg
1880  global volt Cs amp_fac defocus
1881  global tilex tiley tilez
1882  global hires lores
1883  checkParamFile
1884  set w .wps
1885  if ![winfo exists $w] {
1886  toplevel $w
1887 
1888  set newfile [newParamFile "_ps.star"]
1889  if ![string length $newfile] { destroy $w; return }
1890 
1891  frame $w.head
1892  label $w.head.lab -text \
1893  "The power spectra will be calculated using the currently saved parameter file:\n$paramfile"
1894  pack $w.head.lab
1895 
1896  setupNewFileEntry $w.file "Output parameter file" $newfile \
1897  "New parameter file written after calculating the power spectra"
1898 
1899  setupEntry $w.volt "Acceleration voltage" double $volt "Acceleration voltage in kV"
1900  setupEntry $w.cs "Spherical aberration" double $Cs "Spherical aberration in mm"
1901  setupEntry $w.amp "Amplitude fraction " double $amp_fac "Amplitude contrast as a fraction"
1902  setupEntry $w.def "Defocus " double $defocus "Average defocus in um"
1903 
1904  labelframe $w.tiles -text "Tile size:"
1905  setupEntry $w.tiles.x "x" integer $tilex "X-size for tiles to calculate power spectra"
1906  setupEntry $w.tiles.y "y" integer $tiley "Y-size for tiles to calculate power spectra"
1907  pack $w.tiles.x $w.tiles.y -side top -fill x
1908 
1909  labelframe $w.res -text "Resolution limits"
1910  setupEntry $w.res.hi "High" double 8 "High resolution limit in Å"
1911  setupEntry $w.res.lo "Low" double 30 "Low resolution limit in Å"
1912  pack $w.res.hi $w.res.lo -side top -fill x
1913 
1914  labelframe $w.deflim -text "Defocus limits"
1915  setupEntry $w.deflim.lo "Low" double 1.0 "Low defocus limit in µm"
1916  setupEntry $w.deflim.hi "High" double 5.0 "High defocus limit in µm"
1917  pack $w.deflim.lo $w.deflim.hi -side top -fill x
1918 
1919  pack $w.head $w.file $w.volt $w.cs $w.amp $w.def $w.tiles $w.res \
1920  $w.deflim -side top -fill x -pady 2 -padx 2
1921 
1922  frame $w.buttons
1923  button $w.buttons.do -text Do -command "doTomoPowerSpectra $w"
1924  button $w.buttons.close -text Close -command "destroy $w"
1925  pack $w.buttons -side bottom -fill x -pady 2m
1926  pack $w.buttons.do $w.buttons.close -side left -expand 1
1927 
1928  bind $w <Return> "doTomoResolution $w"
1929  } else {
1930  wm deiconify $w
1931  raise $w
1932  }
1933  wm title $w "Power spectrum"
1934  wm iconname $w "Power spectrum"
1935 }
1936 
1937 ## @brief Calculating tilt-adjusted power spectra
1938 #
1939 
1940 proc doTomoPowerSpectra { w } {
1941  global env paramfile
1942  global volt Cs amp_fac defocus
1943  global tilex tiley tilez
1944  global hires lores recenter
1945  set newfile [$w.file.e get]
1946  set volt [$w.volt.e get]
1947  set Cs [$w.cs.e get]
1948  set amp_fac [$w.amp.e get]
1949  set defocus [$w.def.e get]
1950  set tilex [$w.tiles.x.e get]
1951  set tiley [$w.tiles.y.e get]
1952  set hires [$w.res.hi.e get]
1953  set lores [$w.res.lo.e get]
1954  set lodef [$w.deflim.lo.e get]
1955  set hidef [$w.deflim.hi.e get]
1956  setMgParam
1957  if { ![file exists $paramfile] } {
1958 # puts "$paramfile not found!"
1959  tk_dialog .dialog "$paramfile not found!" \
1960  {Please save an appropriate parameter file first.} \
1961  info 0 {OK}
1962  return
1963  }
1964  set logfile "[file rootname $newfile].log"
1965  set cmd "$env(BSOFT)/bin/bctf -verb 1 -act prepfit -tile $tilex,$tiley,1 -Volt $volt -Cs $Cs -Amp $amp_fac -Defocus $defocus -Range $lodef,$hidef -resol $hires,$lores -fitastig 2 -out $newfile $paramfile"
1966  executeCommand $cmd $logfile
1967 }
1968 
1969 ## @brief Estimates the resolution of images in a tilt series.
1970 #
1971 
1972 proc tomoResolution { } {
1973  global paramfile theimg
1974  global hires samrat
1975  checkParamFile
1976  set w .wres
1977  if ![winfo exists $w] {
1978  toplevel $w
1979 
1980  frame $w.head
1981  label $w.head.lab -text \
1982  "Resolution will be estimated using the currently saved parameter file: $paramfile"
1983  pack $w.head.lab
1984 
1985  set psfile [newParamFile "_res.ps"]
1986  if ![string length $psfile] { destroy $w; return }
1987  setupPostscriptEntry $w.ps "Postscript file" $psfile \
1988  "Postscript file with the tilt-resolution plot"
1989 
1990  setupEntry $w.fast "Fast angle " double 10 "Limit images added to each reconstruction"
1991  setupEntry $w.res "High resolution" double $hires "Spatial frequency limit for reconstructions"
1992  setupEntry $w.rat "Sampling ratio " double $samrat "Ratio to average adjacent frequency shells (≥1)"
1993  setupEntry $w.cut "FRC cutoff " double 0.3 "Fourier ring correlation threshold to report resolution"
1994  setupEntry $w.ctf "CTF correction " alnum "none" "Contrast transfer function correction algorithm"
1995 
1996  pack $w.head $w.ps $w.fast $w.res $w.rat $w.cut $w.ctf -side top -fill x -pady 2 -padx 2
1997 
1998  frame $w.buttons
1999  button $w.buttons.do -text Do -command "doTomoResolution $w"
2000  button $w.buttons.close -text Close -command "destroy $w"
2001  pack $w.buttons -side bottom -fill x -pady 2m
2002  pack $w.buttons.do $w.buttons.close -side left -expand 1
2003 
2004  bind $w <Return> "doTomoResolution $w"
2005  } else {
2006  wm deiconify $w
2007  raise $w
2008  }
2009  wm title $w "Resolution"
2010  wm iconname $w "Resolution"
2011 }
2012 
2013 ## @brief Executes estimation of the resolution of the tilt series.
2014 #
2015 # @param w dialog window.
2016 
2017 proc doTomoResolution { w } {
2018  global env paramfile
2019  global hires samrat
2020  set psfile [$w.ps.e get]
2021  set fast [$w.fast.e get]
2022  set hires [$w.res.e get]
2023  set samrat [$w.rat.e get]
2024  set cut [$w.cut.e get]
2025  set ctf [$w.ctf.e get]
2026  if { $ctf == "none" } {
2027  set ctf ""
2028  } else {
2029  set ctf "-CTF $ctf"
2030  }
2031  setMgParam
2032  if { ![file exists $paramfile] } {
2033 # puts "$paramfile not found!"
2034  tk_dialog .dialog "$paramfile not found!" \
2035  {Please save an appropriate parameter file first.} \
2036  info 0 {OK}
2037  return
2038  }
2039  set logfile "[file rootname $psfile].log"
2040  set cmd "$env(BSOFT)/bin/btomres -verb 1 -fast $fast -resol $hires -ratio $samrat $ctf -cut $cut -Post $psfile $paramfile"
2041  executeCommand $cmd $logfile
2042 }
2043 
2044 ## @brief Reconstructs a tomogram.
2045 #
2046 
2047 proc tomoReconstruct { } {
2048  global paramfile theimg project_item
2049  global hires lores
2050  checkParamFile
2051  set w .wrec
2052  if ![winfo exists $w] {
2053  toplevel $w
2054 
2055  set sz [Bmg get $project_item size]
2056  set x [lindex $sz 0]
2057  set y [lindex $sz 1]
2058  set z [expr $x/10]
2059 
2060  set recfile [newParamFile "_rec.mrc"]
2061  if ![string length $recfile] { destroy $w; return }
2062  set newfile [newParamFile "_rec.star"]
2063  if ![string length $newfile] { destroy $w; return }
2064 
2065  frame $w.head
2066  label $w.head.lab -text \
2067  "Reconstruction will be done using the currently saved parameter file:\n$paramfile"
2068 
2069  setupFileEntry $w.rec "Output reconstruction " $recfile \
2070  "New tomographic reconstruction file name"
2071 
2072  setupNewFileEntry $w.file "Output parameter file " $newfile \
2073  "New parameter file written after reconstruction"
2074 
2075  setupEntry $w.interp "Type " integer 0 "Reconstruction interpolation type: 0=nearest neighbor, 1=weighted, 2=trilinear"
2076 
2077  labelframe $w.size -text "Reconstruction size"
2078  setupEntry $w.size.x "x" double $x "Reconstruction x size"
2079  setupEntry $w.size.y "y" double $y "Reconstruction y size"
2080  setupEntry $w.size.z "z" double $z "Reconstruction z size"
2081  pack $w.size.x $w.size.y $w.size.z -side top -fill x
2082 
2083  setupEntry $w.scale "Scale " double 1 "Reconstruction scale"
2084  setupEntry $w.res "High resolution " double $hires "Spatial frequency limit for reconstruction"
2085  setupEntry $w.remark "Marker deletion radius" double 0 "If greater than 0, discs of this radius will be deleted from micrographs"
2086  setupEntry $w.edge "Edge smoothing width " double 3 "If greater than 0, the micrograph edges and extraneous areas will be deleted"
2087  setupEntry $w.ctf "CTF correction " alnum "none" "Contrast transfer function correction algorithm"
2088 
2089  frame $w.mem
2090  button $w.mem.b -text "Memory required" -command "tomoRecMemory $w"
2091  label $w.mem.v -text "" -relief sunken -bd 1 -width 16 -anchor w
2092  pack $w.mem.b $w.mem.v -side left
2093 
2094  pack $w.head.lab
2095 # pack $w.file.lab $w.file.entry $w.file.load -side left -pady 5
2096  pack $w.head $w.rec $w.file $w.interp $w.size $w.scale $w.res $w.remark $w.edge $w.ctf $w.mem -side top -fill x -pady 2 -padx 2
2097 
2098  frame $w.buttons
2099  button $w.buttons.do -text Do -command "doTomoReconstruct $w"
2100  button $w.buttons.close -text Close -command "destroy $w"
2101  pack $w.buttons -side bottom -fill x -pady 2m
2102  pack $w.buttons.do $w.buttons.close -side left -expand 1
2103 
2104  bind $w <Return> "doTomoReconstruct $w"
2105  } else {
2106  wm deiconify $w
2107  raise $w
2108  }
2109  wm title $w "Reconstruct"
2110  wm iconname $w "Reconstruct"
2111 }
2112 
2113 ## @brief Executes tomogram reconstruction.
2114 #
2115 # @param w dialog window.
2116 
2117 proc tomoRecMemory { w } {
2118  set x [$w.size.x.e get]
2119  set y [$w.size.y.e get]
2120  set z [$w.size.z.e get]
2121  set mem [expr 20 * $x * $y * $z]
2122  set p [expr round ($mem * 100.0 / [systemMemory] ) ]
2123  set mem [ expr round($mem * 1.0e-9) ]
2124  $w.mem.v config -text "$mem Gb ($p %)"
2125 }
2126 
2127 ## @brief Executes tomogram reconstruction.
2128 #
2129 # @param w dialog window.
2130 
2131 proc doTomoReconstruct { w } {
2132  global env paramfile
2133  global hires
2134  set recfile [$w.rec.e get]
2135  set newfile [$w.file.e get]
2136  set interp [$w.interp.e get]
2137  set x [$w.size.x.e get]
2138  set y [$w.size.y.e get]
2139  set z [$w.size.z.e get]
2140  set scale [$w.scale.e get]
2141  set hires [$w.res.e get]
2142  set remark [$w.remark.e get]
2143  set edge [$w.edge.e get]
2144  set ctf [$w.ctf.e get]
2145  set remove ""
2146  if { $remark > 0 } { set remove "-remove $remark" }
2147  set smooth ""
2148  if { $edge > 0 } { set smooth "-edge $edge" }
2149  if { $ctf == "none" } {
2150  set ctf ""
2151  } else {
2152  set ctf "-CTF $ctf"
2153  }
2154  setMgParam
2155  if { ![file exists $paramfile] } {
2156 # puts "$paramfile not found!"
2157  tk_dialog .dialog "$paramfile not found!" \
2158  {Please save an appropriate parameter file first.} \
2159  info 0 {OK}
2160  return
2161  }
2162  set logfile "[file rootname $newfile].log"
2163  set cmd "$env(BSOFT)/bin/btomrec -verb 1,tim -interp $interp -size $x,$y,$z -scale $scale -resol $hires $ctf -rescale 0,1 -trans full $remove $smooth -rec $recfile -out $newfile $paramfile"
2164  executeCommand $cmd $logfile
2165 }
2166 
2167 ## @brief Denoises a tomogram.
2168 #
2169 
2170 proc tomoDenoise { } {
2171  global paramfile theimg project_item
2172  global hires lores
2173  checkParamFile
2174  set w .wrec
2175  if ![winfo exists $w] {
2176  toplevel $w
2177 
2178  set denfile [file rootname [Bmg get $project_item filename rec]]
2179 # set denfile [file rootname $paramfile]
2180  append denfile "_den.mrc"
2181  set newfile [newParamFile "_den.star"]
2182  if ![string length $newfile] { destroy $w; return }
2183 
2184  frame $w.head
2185  label $w.head.lab -text \
2186  "Denoising will be done using the currently saved parameter file: \n$paramfile"
2187 
2188  frame $w.den
2189  label $w.den.l -text "Output denoised map " -anchor w
2190  entry $w.den.e -width 40
2191  $w.den.e insert 0 $denfile
2192  pack $w.den.l $w.den.e -side left -pady 5
2193 
2194  setupNewFileEntry $w.file "Output parameter file" $newfile \
2195  "New parameter file written after denoising"
2196 
2197  setupEntry $w.iter "Iterations " integer 100 "Number of iterative denoising cycles"
2198  setupEntry $w.out "Output frequency" integer 10 "Number of iterations between writing intermediate denoised images"
2199  setupEntry $w.slab "Slab size " integer 100 "Denoising slab size for parallel processing"
2200 
2201  pack $w.head.lab
2202 # pack $w.log.lab $w.log.entry -side left -pady 5
2203  pack $w.head $w.den $w.file $w.iter $w.out $w.slab -side top -fill x -pady 2 -padx 2
2204 
2205  frame $w.buttons
2206  button $w.buttons.do -text Do -command "doTomoDenoise $w"
2207  button $w.buttons.close -text Close -command "destroy $w"
2208  pack $w.buttons -side bottom -fill x -pady 2m
2209  pack $w.buttons.do $w.buttons.close -side left -expand 1
2210 
2211  bind $w <Return> "doTomoDenoise $w"
2212  } else {
2213  wm deiconify $w
2214  raise $w
2215  }
2216  wm title $w "Denoise"
2217  wm iconname $w "Denoise"
2218 }
2219 
2220 ## @brief Executes tomogram denoising.
2221 #
2222 # @param w dialog window.
2223 
2224 proc doTomoDenoise { w } {
2225  global env paramfile project_item
2226  global hires
2227  set recfile [Bmg get $project_item filename rec]
2228  set denfile [$w.den.e get]
2229  set newfile [$w.file.e get]
2230  set iter [$w.iter.e get]
2231  set out [$w.out.e get]
2232  set slab [$w.slab.e get]
2233  setMgParam
2234  if { ![file exists $paramfile] } {
2235 # puts "$paramfile not found!"
2236  tk_dialog .dialog "$paramfile not found!" \
2237  {Please save an appropriate parameter file first.} \
2238  info 0 {OK}
2239  return
2240  }
2241  set logfile "[file rootname $newfile].log"
2242 # set cmd "$env(BSOFT)/bin/bnad -verb 1 -iter $iter -out $out -slab $slab -map $denfile -out $newfile $paramfile"
2243  set cmd "$env(BSOFT)/bin/bnad -verb 1 -iter $iter -out $out -slab $slab $recfile $denfile"
2244  executeCommand $cmd $logfile
2245 }
2246 
2247