Bsoft  2.1.0
Bernard's software package
bshow_boxes.tcl
Go to the documentation of this file.
1 ##
2 # @file bshow_boxes.tcl
3 #
4 # @brief Procedures to create, move and destroy particle boxes
5 #
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20191017
9 
10 set show_boxes 1
11 set show_bad 1
12 set show_box_labels 1
13 set part_sel 1
14 set auto_renum 1
15 set box_color_mode "Uniform"
16 set fom_min 0
17 set fom_max 1
18 set sel_min 0
19 set sel_max 1
20 set box_size "100 100 100"
21 set bad_radius 20
22 set contrast 0
23 set box_selected 0
24 set scale 1
25 set thetemp "thetemp"
26 set peach 0
27 
28 ## @brief Dialog box to manipulate particle boxes on the main canvas
29 #
30 
31 proc Particles { } {
32  global project_item theimg imgtype
33  global box_size bad_radius box_selected part_sel mg_sel
34 
35  if ![Bimage exists $theimg] {
36  tk_dialog .dialog "No image in memory!" \
37  {Please read in an appropriate image first.} \
38  info 0 {OK}
39  return
40  }
41 
42  set c [getImageCanvas $theimg]
43  set filename [Bimage get $theimg filename]
44  if ![Bmg exists] {
45  createMgParam
46  setMgParam
47  }
48 # puts "Launching Particles for $c: $project_item"
49  if { [string length $imgtype] < 1 } { set imgtype "mg" }
50  if { [lindex $box_size 0] < 1 } { set box_size "100 100 100" }
51  if { $bad_radius < 1 } { set bad_radius [expr int([lindex $box_size 0] / 4)] }
52 # puts "Box size: $box_size"
53 # puts "Bad radius: $bad_radius"
54 
55  set typelabel ""
56  if { $imgtype == "mg" } {
57  set typelabel "micrograph"
58  } elseif { $imgtype == "frame" } {
59  set typelabel "frames"
60  } elseif { $imgtype == "rec" } {
61  set typelabel "reconstruction"
62  } elseif { $imgtype == "part" } {
63  set typelabel "particle"
64  if { $box_selected <= 0 } {
65  set box_selected 1
66  }
67  }
68 # puts "$imgtype: $box_selected"
69 
70  set w .wbox
71  if ![winfo exists $w] {
72  if { $imgtype != "mg" && $imgtype != "rec" && $imgtype != "part" && $imgtype != "frame" } {
73  set i [tk_dialog .dialog "No micrograph, reconstruction or particle!" \
74  {Please read in an appropriate image first.} \
75  info 0 {Cancel} {Proceed anyway}]
76  if { $i < 1 } { return }
77  }
78 
79  toplevel $w
80 
81  menu $w.menuBar -tearoff 0
82  $w.menuBar add cascade -menu $w.menuBar.box -label "Boxes" -underline 0
83  menu $w.menuBar.box -tearoff 0
84  $w configure -menu $w.menuBar
85  $w.menuBar.box add command -label "Help" -underline 0 \
86  -command { showURL "file://$Bsoft/doc/bshow/bshow_part.html" }
87 # -command { showHelp bshow_boxes.hlp }
88  $w.menuBar.box add command -label "Delete all boxes" \
89  -command { boxDeleteAll } -underline 0
90  $w.menuBar.box add command -label "Delete boxes in current image" \
91  -command { boxDeleteCurrent } -underline 0
92  $w.menuBar.box add command -label "Delete unselected boxes" \
93  -command { boxDeleteUnselected } -underline 0
94  $w.menuBar.box add command -label "Renumber particles" \
95  -command { boxRenumber } -underline 0
96  $w.menuBar.box add command -label "Center particles" \
97  -command { boxCenter } -underline 0
98  $w.menuBar.box add command -label "Template picker" \
99  -command { templatePicker } -underline 0
100  $w.menuBar.box add command -label "Variance picker" \
101  -command { variancePicker } -underline 0
102  $w.menuBar.box add command -label "Extract particles from current image" \
103  -command { extractParticles } -underline 0
104  $w.menuBar.box add command -label "Show selected particles" \
105  -command { showSelectedParticles } -underline 0
106  $w.menuBar.box add command -label "Close" \
107  -command "destroy $w" -underline 0
108 
109  $w.menuBar add cascade -menu $w.menuBar.spaflow -label "Workflow" -underline 0
110  menu $w.menuBar.spaflow -tearoff 0
111  $w configure -menu $w.menuBar
112  $w.menuBar.spaflow add command -label "Extract particles" \
113  -command { extractParticles } -underline 0
114  $w.menuBar.spaflow add command -label "Align particles" \
115  -command { alignParticles } -underline 0
116  $w.menuBar.spaflow add command -label "Refine particles" \
117  -command { refineParticles } -underline 0
118  $w.menuBar.spaflow add command -label "Align 3D particles" \
119  -command { align3DParticles } -underline 0
120  $w.menuBar.spaflow add command -label "Select particles" \
121  -command { selectParticles } -underline 0
122  $w.menuBar.spaflow add command -label "Reconstruct" \
123  -command { particleReconstruct } -underline 0
124 
125  setupItemID $w.itemid
126 
127  if [regexp "Micrograph" $project_item] {
128  setupMicrographThickness $w.thick
129  }
130 
131  setupVectorEntry $w.box_size "Box size" integer $box_size "Particle box size in pixels/voxels"
132  setupEntry $w.bad_radius "Bad radius" double $bad_radius "Bad area radius in pixels/voxels"
133 
134  frame $w.number
135  label $w.number.tag -text "Particles picked: mg" -width 20 -anchor w
136  label $w.number.num -width 6 -text "0" -relief sunken -bd 1 -anchor w
137  label $w.number.tag2 -text "project" -width 10 -anchor w
138  label $w.number.num2 -width 6 -text "0" -relief sunken -bd 1 -anchor w
139  pack $w.number.tag $w.number.num $w.number.tag2 $w.number.num2 -side left -padx 1m
140 
141  frame $w.nbad
142  label $w.nbad.tag -text "Bad regions" -width 20 -anchor w
143  label $w.nbad.num -width 10 -text "0" -relief sunken -bd 1 -anchor w
144  pack $w.nbad.tag $w.nbad.num -side left -padx 1m
145 
146  frame $w.sel
147  label $w.sel.tag -text "Current particle" -width 20 -anchor w
148  label $w.sel.id -width 6 -text "0" -relief sunken -bd 1 -anchor w
149  label $w.sel.fom -width 6 -text "0" -relief sunken -bd 1 -anchor w
150 # checkbutton $w.sel.set -text "Selection" -variable part_sel \
151 # -command { updatePartSel }
152  tk_optionMenu $w.sel.set part_sel 0 1 2 3 4 5 6 7 8 9 10 \
153  11 12 13 14 15 16 17 18 19 20
154  pack $w.sel.tag $w.sel.id $w.sel.fom $w.sel.set -side left -padx 1m
155 
156  frame $w.fom
157  label $w.fom.label -text "FOM" -anchor w
158  scale $w.fom.scale -orient horizontal -length 400 -from 0 -to 1 \
159  -command { setFOMcutoff } -resolution 0.001
160  pack $w.fom.label $w.fom.scale -side top -fill x -padx 1m
161 
162  frame $w.switches
163  checkbutton $w.switches.boxes -text "Show boxes" \
164  -variable show_boxes -command { boxesDrawAll }
165  checkbutton $w.switches.labels -text "Show labels" \
166  -variable show_box_labels -command { boxesDrawAll }
167  checkbutton $w.switches.bad -text "Show bad areas" \
168  -variable show_bad -command { boxesDrawAll }
169  pack $w.switches.boxes $w.switches.labels $w.switches.bad \
170  -side left -ipadx 5 -ipady 5 -padx 1m
171 
172  frame $w.switches2
173  checkbutton $w.switches2.autorenum -text "Automatic renumbering" -variable auto_renum \
174  -command { boxesDrawAll }
175  tk_optionMenu $w.switches2.color box_color_mode \
176  "Uniform" "FOM" "Selection"
177  pack $w.switches2.autorenum $w.switches2.color \
178  -side left -ipadx 5 -ipady 5 -padx 1m
179 
180  setupMicrographButtons $w.neighbor $typelabel
181 
182  pack $w.itemid -side top -fill x -pady 2
183  if [regexp "Micrograph" $project_item] {
184  pack $w.thick -side top -fill x -pady 2
185  }
186  pack $w.box_size $w.bad_radius $w.number $w.nbad $w.sel $w.fom \
187  $w.switches $w.switches2 $w.neighbor -side top -fill x -pady 2
188 
189  updateBoxParam
190  } else {
191  wm deiconify $w
192  raise $w
193  }
194  wm title $w [list "Particles:" $filename]
195  wm iconname $w "Particles"
196  .menuBar.window entryconfigure "Particles" -state normal
197  bind $c <1> "boxCreateSelect %x %y part"
198  bind $c <Shift-1> "boxDelete %x %y"
199  bind $c <Control-1> "boxCreateSelect %x %y bad"
200  bind $c <2> "boxCreateSelect %x %y bad"
201  bind $c <B1-Motion> "boxMove %x %y"
202  bind $c <B2-Motion> "boxMove %x %y"
203  bind $w <Return> "Update 0"
204  bind $w <Control-w> { destroy $w }
205  bind $w <Command-w> { destroy $w }
206  trace add variable part_sel write updatePartSel
207  boxesDrawAll
208 }
209 
210 ## @brief Sets the 3 dimensions for a box from the entries.
211 #
212 
213 proc setBoxSize { } {
214  set w .wbox
215  if ![winfo exists $w] { return }
216  global theimg project_item
217  global box_size bad_radius
218 # puts "Setting box size for $theimg: $project_item"
219 # set wc [getControlWindow $theimg]
220 
221  set box_size_x [$w.box_size.x get]
222  set box_size_y [$w.box_size.y get]
223  set box_size_z [$w.box_size.z get]
224  set box_size "$box_size_x $box_size_y $box_size_z"
225  if { [lindex $box_size 0] < 1 } { set box_size "50 50 50" }
226  set bad_radius [$w.bad_radius.e get]
227 # puts "setBoxSize [$wc.slice.scale cget -to]"
228 
229  if { [Bimage get $theimg nslices] < 2 } {
230  set box_size [lreplace $box_size 2 2 1]
231  $w.box_size.z delete 0 end
232  $w.box_size.z insert 0 "1"
233  }
234 
235  Bmg set $project_item box_size [lindex $box_size 0] [lindex $box_size 1] [lindex $box_size 2]
236  Bmg set $project_item bad_radius $bad_radius
237 # puts "setBoxSize: $box_size"
238 }
239 
240 ## @brief Selects a box or bad area near a clicked location
241 #
242 # @param x
243 # @param y Box position in canvas coordinates.
244 
245 proc boxSelect { x y } {
246  if ![winfo exists .wbox] { return }
247  global project_item
248  global box_selected
249  global tool
250  setBoxSize
251  set loc [imageCoordinatesFromCanvas $x $y]
252  set id [Bmg box $project_item select [lindex $loc 0] [lindex $loc 1] [lindex $loc 2]]
253  boxDrawOldNew $id
254  return $id
255 }
256 
257 ## @brief Creates or selects a box or bad area near a clicked location
258 #
259 # @param x
260 # @param y Box position in canvas coordinates.
261 # @param t Type: "part"/"bad"
262 
263 proc boxCreateSelect { x y t } {
264  if ![winfo exists .wbox] { return }
265  global project_item
266  global box_selected part_sel
267  global tool
268  setBoxSize
269  set loc [imageCoordinatesFromCanvas $x $y]
270  set id [Bmg box $project_item select [lindex $loc 0] [lindex $loc 1] [lindex $loc 2]]
271 # puts "selected ID = $id"
272  if { $id == 0 && $tool == "box" } {
273  set id [Bmg box $project_item create [lindex $loc 0] [lindex $loc 1] [lindex $loc 2] $t $part_sel]
274 # puts "created ID = $id"
275  }
276  boxDrawOldNew $id
277  return $id
278 }
279 
280 proc boxDrawOldNew { id } {
281  if ![winfo exists .wbox] { return }
282  global box_selected
283  if { $id != $box_selected } {
284  set tid $box_selected
285  set box_selected $id
286  boxDraw $tid
287  }
288  if { $id != 0 } {
289  boxDraw $id
290  updateBoxSelected
291  updateBoxCounts
292 # puts "selected ID = $id"
293  }
294 }
295 
296 ## @brief Moves the selected box or bad area to the new coordinates.
297 #
298 # @param x
299 # @param y Box position in canvas coordinates.
300 
301 proc boxMove { x y } {
302  if ![winfo exists .wbox] { return }
303  global project_item theimg
304  global box_selected
305  global px py
306  if { $box_selected == 0 } { return }
307  set c [getImageCanvas $theimg]
308  set wc [getControlWindow $theimg]
309  set scale [$wc.scale.scale get]
310  set ix [expr [$c canvasx $x] / $scale ]
311  set iy [expr [$c canvasy $y] / $scale ]
312  set dx [expr $ix - $px]
313  set dy [expr $py - $iy]
314  set px $ix
315  set py $iy
316 # puts "Moving $dx $dy"
317  Bmg box $project_item move $box_selected $dx $dy
318  boxesDrawAll
319 }
320 
321 
322 ## @brief Draws a box or bad area
323 #
324 # Draws a box and circle with the current box size and number
325 # for a selected box, or draws a red circle with the current bad size
326 # for a selected bad area.
327 #
328 # @param id Box or bad area identifier.
329 
330 proc boxDraw { id } {
331  global project_item theimg
332  global box_selected box_color_mode
333  global show_box_labels box_color box_select_color bad_color bad_select_color
334  global fom_min fom_max
335  global sel_min sel_max
336  if ![winfo exists .wbox] { return }
337  if { $id == 0 } { return }
338  set c [getImageCanvas $theimg]
339  set wc [getControlWindow $theimg]
340  set n [$wc.image.scale get]
341  set z [$wc.slice.scale get]
342  set mg_item [micrographItem $n]
343  set loc [Bmg box $mg_item location $id]
344 # puts "Location for $mg_item $id: $loc"
345  if { [lindex $loc 0] < 0 } {
346  puts "ID $id not found!"
347  return
348  }
349  set scale [$wc.scale.scale get]
350  set scale_1 [expr $scale - 1]
351  set yi [yFlip [lindex $loc 1]]
352  set xr [expr [lindex $loc 0] * $scale ]
353  set yr [expr $yi * $scale ]
354  set zr [expr abs([lindex $loc 2] - $z) ]
355  if { $id > 0 } {
356  set box_radius_x [expr [.wbox.box_size.x get] * 0.5]
357  set box_radius_y [expr [.wbox.box_size.y get] * 0.5]
358  set box_radius_z [expr [.wbox.box_size.z get] * 0.5]
359  if { $zr > $box_radius_z } { return }
360  set zr2 1
361  if { $box_radius_z > 0 } {
362  set zr [expr $zr*1.0/$box_radius_z]
363  set zr2 [expr sqrt(1 - $zr*$zr)]
364  }
365 # puts "$xr $yr"
366  set box_radius_x [expr $box_radius_x * $scale ]
367  set box_radius_y [expr $box_radius_y * $scale ]
368  set oval_radius_x [expr $box_radius_x*$zr2]
369  set oval_radius_y [expr $box_radius_y*$zr2]
370  set xmin [expr $xr - $box_radius_x ]
371  set xmax [expr $xr + $box_radius_x + $scale_1 ]
372  set ymin [expr $yr - $box_radius_y ]
373  set ymax [expr $yr + $box_radius_y + $scale_1 ]
374  set cxmin [expr $xr - $oval_radius_x ]
375  set cxmax [expr $xr + $oval_radius_x + $scale_1 ]
376  set cymin [expr $yr - $oval_radius_y ]
377  set cymax [expr $yr + $oval_radius_y + $scale_1 ]
378  set idtag [format "box%d" $id]
379  if { [string compare $box_color_mode "FOM"] == 0 } {
380  set fom [Bmg box $project_item fom $id]
381  set color [spectrum $fom $fom_min $fom_max]
382  } elseif { [string compare $box_color_mode "Selection"] == 0 } {
383  set sel [Bmg box $project_item select $id]
384  set color [spectrum $sel $sel_min $sel_max]
385  } elseif { [lsearch $box_selected $id] >= 0 } {
386  set color $box_select_color;
387  } else {
388  set color $box_color;
389  }
390  $c create rect $xmin $ymin $xmax $ymax -outline $color -width 1 -tags [list box $idtag]
391  $c create oval $cxmin $cymin $cxmax $cymax -outline $color -width 1 -tags [list box $idtag]
392  if { $show_box_labels } {
393  $c create text $xr $yr -text $id -fill $color -tags [list box $idtag]
394  }
395  } elseif { $id < 0 } {
396  set bad_radius [.wbox.bad_radius.e get]
397  if { $zr > $bad_radius } { return }
398  set bad_radius [expr $scale * sqrt($bad_radius*$bad_radius - $zr*$zr)]
399  set xmin [expr $xr - $bad_radius ]
400  set xmax [expr $xr + $bad_radius + $scale_1 ]
401  set ymin [expr $yr - $bad_radius ]
402  set ymax [expr $yr + $bad_radius + $scale_1 ]
403  if { [lsearch $box_selected $id] >= 0 } {
404  set color $bad_select_color;
405  } else {
406  set color $bad_color;
407  }
408  $c create oval $xmin $ymin $xmax $ymax -outline $color -width 1 -tags bad
409  }
410 }
411 
412 ## @brief Redraws all boxes and bad circles.
413 #
414 
415 proc boxesDrawAll { } {
416  global project_item theimg
417  global imgtype fom_cutoff
418  global show_boxes show_bad
419  set c [getImageCanvas $theimg]
420 # set wc [getControlWindow $theimg]
421 # puts "Drawing boxes for $theimg: $project_item"
422  $c delete box bad
423  if { [string compare $imgtype "mg"] && [string compare $imgtype "rec"] } { return }
424  if ![winfo exists .wbox] { return }
425  setBoxSize
426  updateBoxCounts
427  if { $show_boxes } {
428 # puts "Getting box IDs for cutoff = $fom_cutoff"
429  set ids [Bmg box $project_item ids $fom_cutoff]
430  foreach id $ids {
431  boxDraw $id
432  }
433  }
434 # puts "Boxes drawn"
435  if { $show_bad } {
436  set ids [Bmg box $project_item ids bad]
437  foreach id $ids {
438  boxDraw $id
439  }
440  }
441 # puts "Done"
442 }
443 
444 
445 ## @brief Deletes the selected box or bad area.
446 #
447 # @param x
448 # @param y Box or bad area position in canvas coordinates.
449 
450 proc boxDelete { x y } {
451  if ![winfo exists .wbox] { return }
452  global theimg project_item
453  global boxes_selected
454  global auto_renum
455  set wc [getControlWindow $theimg]
456  set n [$wc.image.scale get]
457  set id [boxSelect $x $y]
458 # puts "ID to be deleted = $id"
459  if { $id != 0 } {
460  Bmg box $project_item delete $id
461  if { $auto_renum } { Bmg box $project_item renumber }
462  }
463  boxesDrawAll
464 }
465 
466 ## @brief Deletes all boxes.
467 #
468 
469 proc boxDeleteAll { } {
470  global project_item theimg
471  Bmg box $project_item delete all
472  set c [getImageCanvas $theimg]
473  $c delete box bad
474  .wbox.number.num config -text "0"
475  .wbox.nbad.num config -text "0"
476 }
477 
478 ## @brief Deletes all boxes in current image.
479 #
480 
481 proc boxDeleteCurrent { } {
482  global project_item theimg
483  Bmg box $project_item delete current
484  boxesDrawAll
485 }
486 
487 ## @brief Deletes all unselected boxes.
488 #
489 
490 proc boxDeleteUnselected { } {
491  global auto_renum fom_cutoff project_item
492 # puts "boxDeleteUnselected: $project_item $fom_cutoff"
493  Bmg box $project_item delete fom $fom_cutoff
494  if { $auto_renum } { Bmg box $project_item renumber }
495  boxesDrawAll
496 }
497 
498 ## @brief Renumbers boxes.
499 #
500 
501 proc boxRenumber { } {
502  global project_item
503  Bmg box $project_item renumber
504  boxesDrawAll
505 }
506 
507 ## @brief Centers boxes.
508 #
509 
510 proc boxCenter { } {
511  global project_item
512  Bmg box $project_item center
513  boxesDrawAll
514 }
515 
516 ## @brief Picks particles by cross-correlation with a template.
517 #
518 
519 proc templatePicker { } {
520  if ![winfo exists .wbox] { return }
521  global theimg thetemp
522  global window_scale hires lores
523 
524  set w .wtemp
525  if ![winfo exists $w] {
526  toplevel $w
527 
528  set m $w.menu
529  menu $m -tearoff 0
530  $m add cascade -menu $m.file -label "File" -underline 0
531  menu $m.file -tearoff 0
532  $w configure -menu $m
533  $m.file add command -label "Open ..." -command { readTemplate } \
534  -underline 0 -accelerator "Ctrl-o"
535  $m.file add command -label "Save ..." -command { saveTemplate } \
536  -underline 0 -accelerator "Ctrl-S"
537 
538  frame $w.frame -relief ridge -borderwidth 2 -bg black
539 
540  wm minsize $w 200 200
541  set c $w.frame.c
542  canvas $c -width 200 -height 200 \
543  -relief sunken -borderwidth 2 \
544  -xscrollcommand "$w.frame.hscroll set" \
545  -yscrollcommand "$w.frame.vscroll set"
546  scrollbar $w.frame.vscroll -orient vertical -command "$c yview"
547  scrollbar $w.frame.hscroll -orient horizontal -command "$c xview"
548 
549  grid $c -in $w.frame \
550  -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
551  grid $w.frame.vscroll \
552  -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
553  grid $w.frame.hscroll \
554  -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
555 # grid $c -in $w.frame -sticky nw -padx 2 -pady 2
556  grid rowconfig $w.frame 0 -weight 1 -minsize 0
557  grid columnconfig $w.frame 0 -weight 1 -minsize 0
558 
559  image create photo $thetemp
560  $c create image 0 0 -image $thetemp -anchor nw -tags template
561 
562  frame $w.scale
563  label $w.scale.label -text "Scale" -width 6 -anchor w
564  scale $w.scale.scale -orient horizontal -from 0.1 -to 10 \
565  -command { setTemplateScale } -resolution 0.1
566  pack $w.scale.label -side left
567  pack $w.scale.scale -side bottom -fill x
568  $w.scale.scale set $window_scale
569 
570  setupResolutionEntry $w
571 
572  labelframe $w.fomcut -text "FOM range"
573  setupEntry $w.fomcut.min "Minimum" double 0 \
574  "The minimum FOM cutoff to select picked particles\nIf it is zero, an automatic cutoff will be calculated"
575  setupEntry $w.fomcut.max "Maximum" double 1e30 \
576  "The maximum FOM cutoff to select picked particles"
577  pack $w.fomcut.min $w.fomcut.max -side top -expand 1
578 
579  setupEntry $w.excld "Exclusion distance" double 0 "Minimum distance between picked particles"
580  setupEntry $w.bin "Binning" integer 1 "Binning the image to accelerate picking"
581 
582  frame $w.buttons
583  button $w.buttons.update -text Update -command "updateBoxTemplate $w" -relief raised
584  button $w.buttons.pick -text Pick -command "doTemplatePicker $w" -relief raised
585  button $w.buttons.cancel -text Close -command " destroy $w " -relief raised
586  pack $w.buttons.update $w.buttons.pick $w.buttons.cancel -side left -expand 1
587 
588  pack $w.frame $w.scale $w.res $w.fomcut $w.excld $w.bin $w.buttons -side top -fill both -expand yes
589 
590  wm title $w "Template Picker"
591  wm iconname $w "Template"
592  } else {
593  wm deiconify $w
594  raise $w
595  }
596 
597  setTemplateScale 1
598  updateBoxTemplate $w
599 
600  bind $w <Control-w> { destroy $w }
601  bind $w <Command-w> { destroy $w }
602 }
603 
604 proc setTemplateScale { scale } {
605  global thetemp
606 # global window_width window_height window_scale
607  set w .wtemp
608  if ![winfo exists $w] { return }
609  if ![Bimage exists thetemp] { return }
610  set c $w.frame.c
611  set width [Bimage get $thetemp width]
612  set height [Bimage get $thetemp height]
613  set size "0 0 [expr $width * $scale ] [expr $height * $scale ]"
614  $c configure -scrollregion $size
615 # $c configure -width $window_width -height $window_height -scrollregion $size
616 # puts "set scale to $scale"
617 
618  set oz [expr [Bimage get $thetemp nslices] / 2]
619 
620 # Bimage template $thetemp 0 $oz $scale 1
621  Bimage template $thetemp $oz $scale
622 }
623 
624 proc readTemplate { } {
625  global imgdir thetemp
626  global filetypes
627  set w .wtemp
628  if ![winfo exists $w] { return }
629  set currdir $imgdir
630  if { [string length $currdir] < 2 } { set currdir [pwd] }
631  set new_filename [tk_getOpenFile -title "Open image file" \
632  -filetypes $filetypes -initialdir $currdir]
633  if { [string length $new_filename] < 1 } { return }
634  if { [catch { set nimages [Bimage open $thetemp $new_filename 0] } err] } {
635  bgerror $err
636  return
637  }
638  set oz [expr [Bimage get $thetemp nslices] / 2]
639  set scale [$w.scale.scale get]
640 # Bimage template $thetemp 0 $oz 1 1
641  Bimage template $thetemp $oz $scale
642 }
643 
644 proc saveTemplate { } {
645  global imgdir thetemp
646  global filetypes filename
647  set currdir $imgdir
648  if { [string length $currdir] < 2 } { set currdir [pwd] }
649  set new_filename [tk_getSaveFile -filetypes $filetypes \
650  -initialfile [file tail $filename] -initialdir $currdir \
651  -defaultextension .mrc]
652  if { [string length $new_filename] < 1 } { return }
653  Bimage set $thetemp pixel_size [getPixelSizeEntry]
654  Bimage save $thetemp $new_filename
655  writeSettingsFile
656 }
657 
658 ## @brief Updates and shows the particle template.
659 #
660 # @param w the window.
661 
662 proc updateBoxTemplate { w } {
663  if ![winfo exists .wbox] { return }
664  global theimg thetemp
665 
666  set wc [getControlWindow $theimg]
667  set img_num [$wc.image.scale get]
668  set mg_item [micrographItem $img_num]
669 
670  Bmg box $mg_item template
671 
672  if ![winfo exists $w] {
673  templatePicker
674  }
675 
676 # set z [$wc.slice.scale get]
677  if { [Bimage exists $thetemp] } {
678  set oz [expr [Bimage get $thetemp nslices] / 2]
679  set scale [$w.scale.scale get]
680 # Bimage template $thetemp 0 $oz $scale 1
681  Bimage template $thetemp $oz $scale
682  }
683 }
684 
685 ## @brief Picks particles using cross-correlation.
686 #
687 # @param w the window.
688 
689 proc doTemplatePicker { w } {
690  global project_item theimg
691  global hires lores fom_cutoff
692 
693  set wc [getControlWindow $theimg]
694  set img_num [$wc.image.scale get]
695  set mg_item [micrographItem $img_num]
696 
697  set hires [$w.res.hi.e get]
698  set lores [$w.res.lo.e get]
699  set fom_cutoff [$w.fomcut.min.e get]
700  set fommax [$w.fomcut.max.e get]
701  set excld [$w.excld.e get]
702  set bin [$w.bin.e get]
703 
704  Bmg box $mg_item pickcc $hires $lores $fom_cutoff $fommax $excld $bin
705 
706  updateBoxParam
707  boxesDrawAll
708 }
709 
710 ## @brief Picks particles from a variance map.
711 #
712 
713 proc variancePicker { } {
714  if ![winfo exists .wbox] { return }
715  global theimg thetemp
716 
717  set w .wvar
718  if ![winfo exists $w] {
719  toplevel $w
720 
721  setupEntry $w.avg "Average kernel" integer 21 "The edge size of the averaging kernel"
722  setupEntry $w.var "Variance kernel" integer 301 "The edge size of the variance kernel"
723  setupEntry $w.excld "Exclusion distance" double 100 "Minimum distance between picked particles"
724  setupEntry $w.bin "Binning" integer 1 "Binning the image to accelerate picking"
725 # setupEntry $w.nsig "Relative to sigma" double 5 "Number of standard deviations from the average in the variance map"
726  setupEntry $w.fmin "Minimum cutoff" double 0.1 "Minimum to accept in the variance map"
727  setupEntry $w.fmax "Maximum cutoff" double 0.2 "Maximum to accept in the variance map"
728 
729  frame $w.buttons
730  button $w.buttons.pick -text Pick -command "doVariancePicker $w" -relief raised
731  button $w.buttons.cancel -text Close -command " destroy $w " -relief raised
732  pack $w.buttons.pick $w.buttons.cancel -side left -expand 1
733 
734  pack $w.avg $w.var $w.excld $w.bin $w.fmin $w.fmax $w.buttons -side top -fill both -expand yes
735 
736  wm title $w "Variance Picker"
737  wm iconname $w "Variance"
738  } else {
739  wm deiconify $w
740  raise $w
741  }
742 
743  bind $w <Control-w> { destroy $w }
744  bind $w <Command-w> { destroy $w }
745 }
746 
747 ## @brief Picks particles using variance map.
748 #
749 # @param w the window.
750 
751 proc doVariancePicker { w } {
752  global project_item theimg
753 
754  set wc [getControlWindow $theimg]
755  set img_num [$wc.image.scale get]
756  set mg_item [micrographItem $img_num]
757 
758  set avg [$w.avg.e get]
759  set var [$w.var.e get]
760  set excld [$w.excld.e get]
761  set bin [$w.bin.e get]
762 # set nsig [$w.nsig.e get]
763  set fmin [$w.fmin.e get]
764  set fmax [$w.fmax.e get]
765 
766  Bmg box $mg_item pickvar $avg $var $excld $bin $fmin $fmax
767 
768  updateBoxParam
769  boxesDrawAll
770 }
771 
772 proc updatePartSel args {
773  global project_item
774  global box_selected part_sel
775 # puts "Particle $box_selected: $part_sel"
776  Bmg box $project_item select $box_selected $part_sel
777 # boxesDrawAll
778 }
779 
780 ## @brief Updates box and bad area parameters.
781 #
782 
783 proc updateBoxParam { } {
784  global theimg imgtype
785  global project_item
786  global box_size bad_radius
787  global fom_min fom_max
788  global sel_min sel_max
789  set w .wbox
790  if ![winfo exists $w] { return }
791  if { $imgtype != "mg" && $imgtype != "rec" && $imgtype != "part" && $imgtype != "frame" } { return }
792 # puts "Updating box parameters for $theimg: $project_item"
793  if ![Bmg exists] {
794  createMgParam
795  setBoxSize
796  }
797 # puts "Project item $project_item $idx"
798  updatePixelsize
799  set temp_size [Bmg get $project_item box_size]
800  if { [lindex $temp_size 0] > 0 && [lindex $temp_size 1] > 0 } {
801  set box_size $temp_size
802  }
803  if { [Bmg get $project_item bad_radius] > 0 } {
804  set bad_radius [Bmg get $project_item bad_radius]
805  }
806  set filename [Bmg get $project_item filename $imgtype]
807  wm title $w [list "Particles:" $filename]
808  updateItemID $w.itemid
809  updateMicrographThickness $w.thick
810  $w.box_size.x delete 0 end
811  $w.box_size.x insert 0 [lindex $box_size 0]
812  $w.box_size.y delete 0 end
813  $w.box_size.y insert 0 [lindex $box_size 1]
814  $w.box_size.z delete 0 end
815  $w.box_size.z insert 0 [lindex $box_size 2]
816  $w.bad_radius.e delete 0 end
817  $w.bad_radius.e insert 0 $bad_radius
818  updateBoxCounts
819  updateBoxSelected
820  set fom_min [Bmg box all fom_min]
821  set fom_max [Bmg box all fom_max]
822  set sel_min [Bmg box all select_min]
823  set sel_max [Bmg box all select_max]
824 }
825 
826 ## @brief Updates box and bad area counts.
827 #
828 
829 proc updateBoxCounts { } {
830  global project_item
831  global fom_max
832  if ![winfo exists .wbox] { return }
833 # puts "Updating box counts for $project_item"
834  set nbox [Bmg box $project_item count]
835  set nbad [Bmg box $project_item count bad]
836  set nboxall [Bmg box all count]
837  set w .wbox
838  if [winfo exists $w] {
839  $w.number.num config -text "$nbox"
840  $w.number.num2 config -text "$nboxall"
841  $w.nbad.num config -text "$nbad"
842  set fom_max [Bmg box $project_item fom_max]
843  if { $fom_max < 0.001 } { set fom_max 0.001 }
844  set sres 0.00001
845  if { $fom_max > 0.005 } {
846  set sres 0.0001
847  } elseif { $fom_max > 0.05 } {
848  set sres 0.001
849  } elseif { $fom_max > 0.5 } {
850  set sres 0.01
851  }
852  $w.fom.scale config -to $fom_max -tickinterval [expr $fom_max / 5] -resolution $sres
853  }
854 }
855 
856 ## @brief Updates selected box and bad area counts.
857 #
858 
859 proc updateBoxSelected { } {
860  global theimg imgtype project_item
861  global box_selected part_sel
862  set w .wbox
863  if ![winfo exists $w] { return }
864  set wc [getControlWindow $theimg]
865  if { $imgtype == "part" } {
866  set box_selected [expr [$wc.image.scale get] + 1]
867 # puts $box_selected
868  }
869  if { $box_selected > 0 } {
870  $w.sel.id config -text "$box_selected"
871  set fom [Bmg box $project_item fom $box_selected]
872  $w.sel.fom config -text "$fom"
873  set sel [Bmg box $project_item select $box_selected]
874  if { $sel >= 0 } {
875  set part_sel $sel
876  }
877  }
878 # puts "$box_selected $part_sel"
879 }
880 
881 ## @brief Extracts particles from the current set of box coordinates.
882 #
883 
884 proc extractParticles { } {
885  global project_item theimg
886  global scale
887 
888  if { [Bmg box $project_item count] < 1 } {
889  tk_messageBox -icon error -type ok -title "Error" -message \
890  "No particles picked!"
891  return
892  }
893 
894  set w .wpext
895  catch {destroy $w}
896  toplevel $w
897  wm title $w "Extracting particles"
898  wm iconname $w "ExtPart"
899 
900  label $w.msg -wraplength 4i -justify left \
901  -text {Select parameters for particle extraction}
902 
903  frame $w.fill
904  label $w.fill.tag -text "Fill value" -width 12 -anchor w
905  radiobutton $w.fill.avg -text "Average" -variable filltype -value 1
906  radiobutton $w.fill.back -text "Background" -variable filltype -value 2
907  radiobutton $w.fill.val -text "Value" -variable filltype -value 0
908  entry $w.fill.entry -width 10 -relief sunken \
909  -validate key -vcmd { string is double %P }
910  $w.fill.entry insert 0 [Bimage get $theimg background]
911 # $w.fill.entry insert 0 0
912 
913  setupEntry $w.scale "Scale" double $scale "Particle extraction scale relative to the micrograph sampling"
914 
915  setupEntry $w.mask_width "Filament mask width" double 0.0 "Mask width to erase areas outside filament"
916 
917  frame $w.path
918  label $w.path.tag -text "Particle path" -width 10 -anchor w
919  entry $w.path.entry -width 50 -relief sunken
920  $w.path.entry insert 0 ""
921 
922  frame $w.check
923 # checkbutton $w.check.oddbutton -text "Odd size" -width 20 -anchor w -variable odd
924  checkbutton $w.check.backbutton -text "Correct background" -width 20 -anchor w -variable back
925  checkbutton $w.check.normbutton -text "Normalize" -width 14 -anchor w -variable norm
926  checkbutton $w.check.splitbutton -text "Individual output files" \
927  -width 20 -anchor w -variable part_split
928 
929  frame $w.buttons
930  button $w.buttons.ok -text OK -command "doExtractParticles $w"
931  button $w.buttons.cancel -text Cancel -command "destroy $w"
932 
933  pack $w.msg -side top -padx 5 -pady 5
934 
935  pack $w.fill.tag $w.fill.avg $w.fill.back $w.fill.val \
936  $w.fill.entry -side left -fill both -expand true
937  pack $w.path.tag $w.path.entry -side left -padx 5 -pady 5
938  pack $w.fill $w.scale $w.mask_width $w.path -side top
939 
940  pack $w.check.backbutton $w.check.normbutton $w.check.splitbutton -side left -pady 5 -padx 2
941  pack $w.check -side top -pady 2m
942 
943  pack $w.buttons.ok $w.buttons.cancel -side left -padx 5
944  pack $w.buttons -side bottom -pady 2m
945 
946  bind $w <Return> "doExtractParticles $w"
947 }
948 
949 ## @brief Extracts particles from the current set of box coordinates.
950 #
951 # @param w Dialog window.
952 
953 proc doExtractParticles { w } {
954  global filetypes project_item
955  global filename imgdir paramdir
956  global scale filltype
957  global back norm part_split
958  set xdir $paramdir
959  if { [string length $xdir] < 2 } { set xdir $imgdir }
960  if { [string length $xdir] < 2 } { set xdir [pwd] }
961  set xname [file root [file tail $filename]]
962  append xname "_part.mrc"
963  set xname [tk_getSaveFile -filetypes $filetypes \
964  -initialfile $xname -initialdir $xdir -defaultextension .mrc]
965  if { [string length $xname] < 1 } { return }
966 # puts "Extract from $filename to $xname"
967  set xname [relativePath [pwd] $xname]
968  set pathname [$w.path.entry get]
969  set scale [$w.scale.e get]
970  set mask_width [$w.mask_width.e get]
971  set fill [$w.fill.entry get]
972  Bmg set $project_item pixel_size [getPixelSizeEntry]
973  setMgParam
974  Bmg box $project_item extract $xname $scale $back $norm $filltype $fill $mask_width $part_split $pathname
975  destroy $w
976 }
977 
978 ## @brief Aligns particles by projection matching to a reference.
979 #
980 
981 proc alignParticles { } {
982  global paramfile
983  global hires lores
984  global angle annmin annmax shift_limit
985  global peach
986  checkParamFile
987  set w .waln
988  if ![winfo exists $w] {
989  toplevel $w
990 
991  set angle 3
992  set shift_limit 10
993  set annmin 1
994  set annmax 100
995 
996  set newfile [file rootname $paramfile]
997  append newfile "_aln.star"
998  set logfile [file rootname $paramfile]
999  append logfile "_aln.log"
1000 
1001  frame $w.head
1002  label $w.head.lab -text \
1003  "Aligning will be done on the currently saved parameter file: $paramfile"
1004  pack $w.head.lab
1005 
1006  setupNewFileEntry $w.file "Output parameter file" $newfile \
1007  "New parameter file written after alignment"
1008 
1009  setupScrollEntry $w.ref "Reference map" "" "Template or reference map to align to"
1010 
1011  setupEntry $w.sym "Symmetry" alnum "C1" "Point group symmetry"
1012 
1013  setupResolutionEntry $w
1014 
1015  setupEntry $w.ang "Angular step" double $angle \
1016  "The grid search step intervals in degrees"
1017  setupEntry $w.shift "Shift limit" double $shift_limit \
1018  "The maximum number of pixels of shift allowed"
1019 
1020  labelframe $w.ann -text "Annuli"
1021  setupEntry $w.ann.min "Minimum" double $annmin \
1022  "The innermost annulus to include"
1023  setupEntry $w.ann.max "Maximum" double $annmax \
1024  "The outermost annulus to include"
1025  pack $w.ann.min $w.ann.max -side top -expand 1
1026 
1027  checkbutton $w.peach -text "Peach" -variable peach
1028 
1029  pack $w.head $w.file $w.ref $w.sym $w.res $w.ang \
1030  $w.shift $w.ann -side top -pady 2 -padx 10
1031 
1032  frame $w.buttons
1033  button $w.buttons.do -text Do -command "doAlignParticles $w"
1034  button $w.buttons.close -text Close -command "destroy $w"
1035  pack $w.buttons -side bottom -fill x -pady 2m
1036  pack $w.buttons.do $w.buttons.close -side left -expand 1
1037 
1038  bind $w <Return> "doAlignParticles $w"
1039  } else {
1040  wm deiconify $w
1041  raise $w
1042  }
1043  wm title $w "Align"
1044  wm iconname $w "Align"
1045 }
1046 
1047 ## @brief Executes marker tracking.
1048 #
1049 # @param w dialog window.
1050 
1051 proc doAlignParticles { w } {
1052  global env paramfile
1053  global hires lores
1054  global peach
1055  set newfile [$w.file.e get]
1056  set ref [$w.ref.e get]
1057  set sym [$w.sym.e get]
1058  set hires [$w.res.hi.e get]
1059  set lores [$w.res.lo.e get]
1060  set angle [$w.ang.e get]
1061  set shift [$w.shift.e get]
1062  set annmin [$w.ann.min.e get]
1063  set annmax [$w.ann.max.e get]
1064  setMgParam
1065  if { ![file exists $paramfile] } {
1066  puts "$paramfile not found!"
1067  }
1068  set logfile "[file rootname $newfile].log"
1069 # set psfile "[file rootname $newfile].ps"
1070 # puts "logfile = $logfile"
1071  if { $peach } {
1072  set cmd "psubmit /stachraid/bin/bomrun4 -mode ccc -CTF -sym $sym -resol $hires,$lores -ang $angle -shift $shift -ann $annmin,$annmax -ref $ref $paramfile -ss"
1073  } else {
1074  set cmd "$env(BSOFT)/bin/borient -verb 1 -mode ccc -CTF -sym $sym -resol $hires,$lores -ang $angle -shift $shift -ann $annmin,$annmax -ref $ref -out $newfile $paramfile"
1075  executeCommand $cmd $logfile
1076  }
1077 }
1078 
1079 ## @brief Refines particle orientations by central section matching to a reference.
1080 #
1081 
1082 proc refineParticles { } {
1083  global paramfile
1084  global hires lores
1085  global angini angacc stpini stpacc def mag
1086  checkParamFile
1087  set w .wref
1088  if ![winfo exists $w] {
1089  toplevel $w
1090 
1091  set angini 3
1092  set angacc 0.5
1093  set stpini 4
1094  set stpacc 0.5
1095  set def 0.0
1096  set mag 0.0
1097 
1098  set newfile [file rootname $paramfile]
1099  append newfile "_aln.star"
1100  set logfile [file rootname $paramfile]
1101  append logfile "_aln.log"
1102 
1103  frame $w.head
1104  label $w.head.lab -text \
1105  "Aligning will be done on the currently saved parameter file: $paramfile"
1106  pack $w.head.lab
1107 
1108  setupNewFileEntry $w.file "Output parameter file" $newfile \
1109  "New parameter file written after alignment"
1110 
1111  setupScrollEntry $w.ref "Reference map" "" "Template or reference map to align to"
1112 
1113  labelframe $w.grd -text "Angular grid search"
1114  setupEntry $w.grd.ini "Initial angle" double $angini "Initial angluar step size in degrees"
1115  setupEntry $w.grd.acc "Angular accuracy" double $angacc "Target angular accuracy in degrees"
1116  pack $w.grd.ini $w.grd.acc -side top -expand 1
1117 
1118  labelframe $w.stp -text "Translational search"
1119  setupEntry $w.stp.ini "Initial step" double $stpini "Initial translational step size in pixels"
1120  setupEntry $w.stp.acc "Translational accuracy" double $stpacc "Target translational accuracy in pixels"
1121  pack $w.stp.ini $w.stp.acc -side top -expand 1
1122 
1123  setupResolutionEntry $w
1124 
1125  setupEntry $w.def "Defocus refinement" double $def \
1126  "Defocus step size to refine defocus in micrometer"
1127  setupEntry $w.mag "Magnification refinement" double $mag \
1128  "The maximum number of pixels of shift allowed"
1129 
1130  checkbutton $w.peach -text "Peach" -variable peach
1131 
1132  pack $w.head $w.file $w.ref $w.grd $w.stp $w.res $w.def $w.mag \
1133  -side top -pady 2 -padx 10
1134 
1135  frame $w.buttons
1136  button $w.buttons.do -text Do -command "doRefineParticles $w"
1137  button $w.buttons.close -text Close -command "destroy $w"
1138  pack $w.buttons -side bottom -fill x -pady 2m
1139  pack $w.buttons.do $w.buttons.close -side left -expand 1
1140 
1141  bind $w <Return> "doRefineParticles $w"
1142  } else {
1143  wm deiconify $w
1144  raise $w
1145  }
1146  wm title $w "Refine"
1147  wm iconname $w "Refine"
1148 }
1149 
1150 ## @brief Executes particle orientation refinement.
1151 #
1152 # @param w dialog window.
1153 
1154 proc doRefineParticles { w } {
1155  global env paramfile
1156  global hires lores
1157  global angini angacc stpini stpacc def mag
1158  set newfile [$w.file.e get]
1159  set ref [$w.ref.e get]
1160  set angini [$w.grd.ini.e get]
1161  set angacc [$w.grd.acc.e get]
1162  set stpini [$w.stp.ini.e get]
1163  set stpacc [$w.stp.acc.e get]
1164  set hires [$w.res.hi.e get]
1165  set lores [$w.res.lo.e get]
1166  set def [$w.def.e get]
1167  set mag [$w.mag.e get]
1168  setMgParam
1169  if { ![file exists $paramfile] } {
1170  puts "$paramfile not found!"
1171  }
1172  set opt "-grid $angini,$angacc "
1173  if { $stpacc } { append opt "-step $stpini,$stpacc " }
1174  append opt "-resol $hires,$lores "
1175  if { $def } { append opt "-defocus $def " }
1176  if { $mag } { append opt "-magnif $mag " }
1177 
1178  set logfile "[file rootname $newfile].log"
1179 # set psfile "[file rootname $newfile].ps"
1180 # puts "logfile = $logfile"
1181  set cmd "$env(BSOFT)/bin/brefine -verb 1 $opt -ref $ref -out $newfile $paramfile"
1182  executeCommand $cmd $logfile
1183 }
1184 
1185 ## @brief Aligns 3D particles to a reference.
1186 #
1187 
1188 proc align3DParticles { } {
1189  global paramfile
1190  global hires lores
1191  global angle angacc shift_limit
1192  global peach
1193  checkParamFile
1194  set w .waln3
1195  if ![winfo exists $w] {
1196  toplevel $w
1197 
1198  set angle 5
1199  set angacc 1
1200  set shift_limit 10
1201 
1202  set newfile [file rootname $paramfile]
1203  append newfile "_aln.star"
1204  set logfile [file rootname $paramfile]
1205  append logfile "_aln.log"
1206 
1207  frame $w.head
1208  label $w.head.lab -text \
1209  "Aligning will be done on the currently saved parameter file: $paramfile"
1210  pack $w.head.lab
1211 
1212  setupNewFileEntry $w.file "Output parameter file" $newfile \
1213  "New parameter file written after alignment."
1214 
1215  setupScrollEntry $w.ref "Reference map" "" "Template or reference map to align to."
1216 
1217  setupScrollEntry $w.mask "Mask" "" "Frequency space mask defining missing regions."
1218 
1219  setupEntry $w.sym "Symmetry" alnum "C1" "Point group symmetry"
1220 
1221  setupResolutionEntry $w
1222 
1223  labelframe $w.grd -text "Angular grid search"
1224  setupEntry $w.grd.ini "Initial angle" double $angle "Initial angluar step size in degrees"
1225  setupEntry $w.grd.acc "Angular accuracy" double $angacc "Target angular accuracy in degrees"
1226  pack $w.grd.ini $w.grd.acc -side top -expand 1
1227 
1228  setupEntry $w.shift "Shift limit" double $shift_limit \
1229  "The maximum number of pixels of shift allowed"
1230 
1231  checkbutton $w.peach -text "Peach" -variable peach
1232 
1233  pack $w.head $w.file $w.ref $w.mask $w.sym $w.res $w.grd \
1234  $w.shift -side top -pady 2 -padx 10
1235 
1236  frame $w.buttons
1237  button $w.buttons.do -text Do -command "doAlign3DParticles $w"
1238  button $w.buttons.close -text Close -command "destroy $w"
1239  pack $w.buttons -side bottom -fill x -pady 2m
1240  pack $w.buttons.do $w.buttons.close -side left -expand 1
1241 
1242  bind $w <Return> "doAlign3DParticles $w"
1243  } else {
1244  wm deiconify $w
1245  raise $w
1246  }
1247  wm title $w "Align"
1248  wm iconname $w "Align"
1249 }
1250 
1251 ## @brief Executes marker tracking.
1252 #
1253 # @param w dialog window.
1254 
1255 proc doAlign3DParticles { w } {
1256  global env paramfile
1257  global hires lores
1258  global angle angacc shift_limit
1259  global peach
1260  set newfile [$w.file.e get]
1261  set mask [$w.mask.e get]
1262  set ref [$w.ref.e get]
1263  set sym [$w.sym.e get]
1264  set hires [$w.res.hi.e get]
1265  set lores [$w.res.lo.e get]
1266  set angle [$w.grd.ini.e get]
1267  set angacc [$w.grd.acc.e get]
1268  set shift [$w.shift.e get]
1269  setMgParam
1270  if { ![file exists $paramfile] } {
1271  puts "$paramfile not found!"
1272  }
1273  set logfile "[file rootname $newfile].log"
1274 # set psfile "[file rootname $newfile].ps"
1275 # puts "logfile = $logfile"
1276  if { $peach } {
1277  } else {
1278  set cmd "$env(BSOFT)/bin/3daln.pl -verb 1 -global 2 -mode refine -sym $sym -resol $hires,$lores -ang $angle -acc $angacc -shift $shift -Temp $ref -Mask $mask -out $newfile $paramfile"
1279  executeCommand $cmd $logfile
1280  }
1281 }
1282 
1283 ## @brief Selects particles after an alignment run.
1284 #
1285 
1286 proc selectParticles { } {
1287  global paramfile theimg project_item
1288  global selall fom_check cv_check top_check ran_check defadj
1289  showSelectedParticles
1290  set w .wspasel
1291  if ![winfo exists $w] {
1292  toplevel $w
1293 
1294  set newfile [file rootname $paramfile]
1295  append newfile "_sel.star"
1296 
1297  frame $w.head
1298  label $w.head.lab -text \
1299  "Particle seletion will be done using the currently saved parameter file: $paramfile"
1300 
1301  setupNewFileEntry $w.file "Output parameter file" $newfile \
1302  "New parameter file written after selection"
1303 
1304  checkbutton $w.all -text "Select all" -variable selall
1305  setupCheckEntry $w.fom "FOM" list fom fom_check "Selection based on the figure-of-merit"
1306  setupCheckEntry $w.cv "CV" list cv cv_check "Selection based on cross-validation"
1307  setupCheckEntry $w.top "Top" list top top_check "Selection based on the top scorers"
1308  checkbutton $w.defadj -text "Adjust for defocus" -variable defadj
1309  setupCheckEntry $w.ran "Random" list ran ran_check "Random selection"
1310 
1311  pack $w.head.lab
1312  pack $w.head $w.file $w.all $w.fom $w.cv $w.top $w.defadj $w.ran -side top -pady 2 -padx 15
1313 
1314  frame $w.buttons
1315  button $w.buttons.do -text Do -command "doSelectParticles $w"
1316  button $w.buttons.reset -text Reset -command "showSelectedParticles"
1317  button $w.buttons.close -text Close -command "destroy $w"
1318  pack $w.buttons -side bottom -fill x -pady 2m
1319  pack $w.buttons.do $w.buttons.reset $w.buttons.close -side left -expand 1
1320 
1321  bind $w <Return> "doSelectParticles $w"
1322  } else {
1323  wm deiconify $w
1324  raise $w
1325  }
1326  wm title $w "Select"
1327  wm iconname $w "Select"
1328 }
1329 
1330 ## @brief Shows the current particle selection.
1331 #
1332 
1333 proc showSelectedParticles { } {
1334  global env paramfile
1335  set logfile "[file rootname $paramfile].log"
1336  set cmd "$env(BSOFT)/bin/bpartsel -verb 7 $paramfile"
1337  executeCommand $cmd $logfile
1338 }
1339 
1340 ## @brief Executes particle selection.
1341 #
1342 # @param w dialog window.
1343 
1344 proc doSelectParticles { w } {
1345  global env paramfile
1346  global selall fom_check cv_check top_check ran_check defadj
1347  set newfile [$w.file.e get]
1348  set fom [$w.fom.e get]
1349  set cv [$w.cv.e get]
1350  set top [$w.top.e get]
1351  set ran [$w.ran.e get]
1352  setMgParam
1353  if { ![file exists $paramfile] } {
1354  puts "$paramfile not found!"
1355  }
1356  set ad ""
1357  if { $defadj } { set ad ",1" }
1358  set opt ""
1359  if { $selall } { append opt "-all " }
1360  if { $fom_check } { append opt "-fom $fom$ad " }
1361  if { $cv_check } { append opt " -cv $cv$ad " }
1362  if { $top_check } { append opt "-top $top$ad " }
1363  if { $ran_check } { append opt "-random $ran " }
1364  set logfile "[file rootname $newfile].log"
1365  set cmd "$env(BSOFT)/bin/bpartsel -verb 7 $opt -out $newfile $paramfile"
1366  executeCommand $cmd $logfile
1367 }
1368 
1369 ## @brief Reconstructs a single particle.
1370 #
1371 
1372 proc particleReconstruct { } {
1373  global paramfile theimg project_item
1374  global hires lores
1375  set w .wsparec
1376  if ![winfo exists $w] {
1377  toplevel $w
1378 
1379  set sz [Bmg get $project_item box_size]
1380  set x [lindex $sz 0]
1381  set y [lindex $sz 1]
1382  set z [lindex $sz 0]
1383 
1384  set recfile [file rootname $paramfile]
1385  append recfile "_rec.mrc"
1386  set newfile [file rootname $paramfile]
1387  append newfile "_rec.star"
1388 
1389  frame $w.head
1390  label $w.head.lab -text \
1391  "Reconstruction will be done using the currently saved parameter file: $paramfile"
1392 
1393  frame $w.rec
1394  label $w.rec.l -text "Output reconstruction" -width 20 -anchor w
1395  entry $w.rec.e -width 40
1396  $w.rec.e insert 0 $recfile
1397  pack $w.rec.l $w.rec.e -side left -pady 5
1398 
1399  setupNewFileEntry $w.file "Output parameter file" $newfile \
1400  "New parameter file written after reconstruction"
1401 
1402  setupEntry $w.cls "Classes" alnum "all" "Classes of particles to select for reconstruction"
1403 
1404  setupEntry $w.interp "Type" integer 0 "Reconstruction interpolation type: 0=nearest neighbor, 1=weighted, 2=trilinear"
1405 
1406  labelframe $w.size -text "Reconstruction size"
1407  setupEntry $w.size.x "x" double $x "Reconstruction x size"
1408  setupEntry $w.size.y "y" double $y "Reconstruction y size"
1409  setupEntry $w.size.z "z" double $z "Reconstruction z size"
1410  pack $w.size.x $w.size.y $w.size.z -side top -expand 1
1411 
1412  setupEntry $w.scale "Scale" double 1 "Reconstruction scale"
1413  setupEntry $w.res "High resolution" double $hires "Spatial frequency limit for reconstruction"
1414  setupEntry $w.ctf "CTF correction" alnum "flip" "Contrast transfer function correction algorithm"
1415  setupEntry $w.sym "Symmetry" alnum "C1" "Point group symmetry"
1416 
1417  pack $w.head.lab
1418  pack $w.head $w.rec $w.file $w.cls $w.interp $w.size $w.scale $w.res $w.ctf $w.sym -side top -fill x -pady 2 -padx 15
1419 
1420  frame $w.buttons
1421  button $w.buttons.do -text Do -command "doReconstruct $w"
1422  button $w.buttons.close -text Close -command "destroy $w"
1423  pack $w.buttons -side bottom -fill x -pady 2m
1424  pack $w.buttons.do $w.buttons.close -side left -expand 1
1425 
1426  bind $w <Return> "doReconstruct $w"
1427  } else {
1428  wm deiconify $w
1429  raise $w
1430  }
1431  wm title $w "Reconstruct"
1432  wm iconname $w "Reconstruct"
1433 }
1434 
1435 ## @brief Executes tomogram reconstruction.
1436 #
1437 # @param w dialog window.
1438 
1439 proc doReconstruct { w } {
1440  global env paramfile
1441  global hires
1442  set recfile [$w.rec.e get]
1443  set newfile [$w.file.e get]
1444  set cls [$w.cls.e get]
1445  set interp [$w.interp.e get]
1446  set x [$w.size.x.e get]
1447  set y [$w.size.y.e get]
1448  set z [$w.size.z.e get]
1449  set scale [$w.scale.e get]
1450  set hires [$w.res.e get]
1451  set ctf [$w.ctf.e get]
1452  set sym [$w.sym.e get]
1453  if { $ctf == "none" } {
1454  set ctf ""
1455  } else {
1456  set ctf "-CTF $ctf"
1457  }
1458  setMgParam
1459  if { ![file exists $paramfile] } {
1460  puts "$paramfile not found!"
1461  }
1462  set logfile "[file rootname $newfile].log"
1463  set cmd "$env(BSOFT)/bin/breconstruct -verb 1,tim -classes $cls -interp $interp -size $x,$y,$z -scale $scale -resol $hires $ctf -sym $sym -rescale 0,1 -full -half -rec $recfile -out $newfile $paramfile"
1464  executeCommand $cmd $logfile
1465 }
1466