2 # @file bshow_boxes.tcl
4 # @brief Procedures to create, move and destroy particle boxes
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20191017
15 set box_color_mode "Uniform"
20 set box_size "100 100 100"
28 ## @brief Dialog box to manipulate particle boxes on the main canvas
32 global project_item theimg imgtype
33 global box_size bad_radius box_selected part_sel mg_sel
35 if ![Bimage exists $theimg] {
36 tk_dialog .dialog "No image in memory!" \
37 {Please read in an appropriate image first.} \
42 set c [getImageCanvas $theimg]
43 set filename [Bimage get $theimg filename]
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"
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 } {
68 # puts "$imgtype: $box_selected"
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 }
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
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
125 setupItemID $w.itemid
127 if [regexp "Micrograph" $project_item] {
128 setupMicrographThickness $w.thick
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"
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
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
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
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
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
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
180 setupMicrographButtons $w.neighbor $typelabel
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
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
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
210 ## @brief Sets the 3 dimensions for a box from the entries.
213 proc setBoxSize { } {
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]
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]"
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"
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"
240 ## @brief Selects a box or bad area near a clicked location
243 # @param y Box position in canvas coordinates.
245 proc boxSelect { x y } {
246 if ![winfo exists .wbox] { return }
251 set loc [imageCoordinatesFromCanvas $x $y]
252 set id [Bmg box $project_item select [lindex $loc 0] [lindex $loc 1] [lindex $loc 2]]
257 ## @brief Creates or selects a box or bad area near a clicked location
260 # @param y Box position in canvas coordinates.
261 # @param t Type: "part"/"bad"
263 proc boxCreateSelect { x y t } {
264 if ![winfo exists .wbox] { return }
266 global box_selected part_sel
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"
280 proc boxDrawOldNew { id } {
281 if ![winfo exists .wbox] { return }
283 if { $id != $box_selected } {
284 set tid $box_selected
292 # puts "selected ID = $id"
296 ## @brief Moves the selected box or bad area to the new coordinates.
299 # @param y Box position in canvas coordinates.
301 proc boxMove { x y } {
302 if ![winfo exists .wbox] { return }
303 global project_item theimg
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]
316 # puts "Moving $dx $dy"
317 Bmg box $project_item move $box_selected $dx $dy
322 ## @brief Draws a box or bad area
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.
328 # @param id Box or bad area identifier.
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!"
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) ]
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 }
361 if { $box_radius_z > 0 } {
362 set zr [expr $zr*1.0/$box_radius_z]
363 set zr2 [expr sqrt(1 - $zr*$zr)]
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;
388 set color $box_color;
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]
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;
406 set color $bad_color;
408 $c create oval $xmin $ymin $xmax $ymax -outline $color -width 1 -tags bad
412 ## @brief Redraws all boxes and bad circles.
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"
423 if { [string compare $imgtype "mg"] && [string compare $imgtype "rec"] } { return }
424 if ![winfo exists .wbox] { return }
428 # puts "Getting box IDs for cutoff = $fom_cutoff"
429 set ids [Bmg box $project_item ids $fom_cutoff]
436 set ids [Bmg box $project_item ids bad]
445 ## @brief Deletes the selected box or bad area.
448 # @param y Box or bad area position in canvas coordinates.
450 proc boxDelete { x y } {
451 if ![winfo exists .wbox] { return }
452 global theimg project_item
453 global boxes_selected
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"
460 Bmg box $project_item delete $id
461 if { $auto_renum } { Bmg box $project_item renumber }
466 ## @brief Deletes all boxes.
469 proc boxDeleteAll { } {
470 global project_item theimg
471 Bmg box $project_item delete all
472 set c [getImageCanvas $theimg]
474 .wbox.number.num config -text "0"
475 .wbox.nbad.num config -text "0"
478 ## @brief Deletes all boxes in current image.
481 proc boxDeleteCurrent { } {
482 global project_item theimg
483 Bmg box $project_item delete current
487 ## @brief Deletes all unselected boxes.
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 }
498 ## @brief Renumbers boxes.
501 proc boxRenumber { } {
503 Bmg box $project_item renumber
507 ## @brief Centers boxes.
512 Bmg box $project_item center
516 ## @brief Picks particles by cross-correlation with a template.
519 proc templatePicker { } {
520 if ![winfo exists .wbox] { return }
521 global theimg thetemp
522 global window_scale hires lores
525 if ![winfo exists $w] {
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"
538 frame $w.frame -relief ridge -borderwidth 2 -bg black
540 wm minsize $w 200 200
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"
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
559 image create photo $thetemp
560 $c create image 0 0 -image $thetemp -anchor nw -tags template
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
570 setupResolutionEntry $w
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
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"
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
588 pack $w.frame $w.scale $w.res $w.fomcut $w.excld $w.bin $w.buttons -side top -fill both -expand yes
590 wm title $w "Template Picker"
591 wm iconname $w "Template"
600 bind $w <Control-w> { destroy $w }
601 bind $w <Command-w> { destroy $w }
604 proc setTemplateScale { scale } {
606 # global window_width window_height window_scale
608 if ![winfo exists $w] { return }
609 if ![Bimage exists thetemp] { return }
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"
618 set oz [expr [Bimage get $thetemp nslices] / 2]
620 # Bimage template $thetemp 0 $oz $scale 1
621 Bimage template $thetemp $oz $scale
624 proc readTemplate { } {
625 global imgdir thetemp
628 if ![winfo exists $w] { return }
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] } {
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
644 proc saveTemplate { } {
645 global imgdir thetemp
646 global filetypes filename
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
658 ## @brief Updates and shows the particle template.
660 # @param w the window.
662 proc updateBoxTemplate { w } {
663 if ![winfo exists .wbox] { return }
664 global theimg thetemp
666 set wc [getControlWindow $theimg]
667 set img_num [$wc.image.scale get]
668 set mg_item [micrographItem $img_num]
670 Bmg box $mg_item template
672 if ![winfo exists $w] {
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
685 ## @brief Picks particles using cross-correlation.
687 # @param w the window.
689 proc doTemplatePicker { w } {
690 global project_item theimg
691 global hires lores fom_cutoff
693 set wc [getControlWindow $theimg]
694 set img_num [$wc.image.scale get]
695 set mg_item [micrographItem $img_num]
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]
704 Bmg box $mg_item pickcc $hires $lores $fom_cutoff $fommax $excld $bin
710 ## @brief Picks particles from a variance map.
713 proc variancePicker { } {
714 if ![winfo exists .wbox] { return }
715 global theimg thetemp
718 if ![winfo exists $w] {
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"
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
734 pack $w.avg $w.var $w.excld $w.bin $w.fmin $w.fmax $w.buttons -side top -fill both -expand yes
736 wm title $w "Variance Picker"
737 wm iconname $w "Variance"
743 bind $w <Control-w> { destroy $w }
744 bind $w <Command-w> { destroy $w }
747 ## @brief Picks particles using variance map.
749 # @param w the window.
751 proc doVariancePicker { w } {
752 global project_item theimg
754 set wc [getControlWindow $theimg]
755 set img_num [$wc.image.scale get]
756 set mg_item [micrographItem $img_num]
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]
766 Bmg box $mg_item pickvar $avg $var $excld $bin $fmin $fmax
772 proc updatePartSel args {
774 global box_selected part_sel
775 # puts "Particle $box_selected: $part_sel"
776 Bmg box $project_item select $box_selected $part_sel
780 ## @brief Updates box and bad area parameters.
783 proc updateBoxParam { } {
784 global theimg imgtype
786 global box_size bad_radius
787 global fom_min fom_max
788 global sel_min sel_max
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"
797 # puts "Project item $project_item $idx"
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
803 if { [Bmg get $project_item bad_radius] > 0 } {
804 set bad_radius [Bmg get $project_item bad_radius]
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
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]
826 ## @brief Updates box and bad area counts.
829 proc updateBoxCounts { } {
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]
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 }
845 if { $fom_max > 0.005 } {
847 } elseif { $fom_max > 0.05 } {
849 } elseif { $fom_max > 0.5 } {
852 $w.fom.scale config -to $fom_max -tickinterval [expr $fom_max / 5] -resolution $sres
856 ## @brief Updates selected box and bad area counts.
859 proc updateBoxSelected { } {
860 global theimg imgtype project_item
861 global box_selected part_sel
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]
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]
878 # puts "$box_selected $part_sel"
881 ## @brief Extracts particles from the current set of box coordinates.
884 proc extractParticles { } {
885 global project_item theimg
888 if { [Bmg box $project_item count] < 1 } {
889 tk_messageBox -icon error -type ok -title "Error" -message \
890 "No particles picked!"
897 wm title $w "Extracting particles"
898 wm iconname $w "ExtPart"
900 label $w.msg -wraplength 4i -justify left \
901 -text {Select parameters for particle extraction}
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
913 setupEntry $w.scale "Scale" double $scale "Particle extraction scale relative to the micrograph sampling"
915 setupEntry $w.mask_width "Filament mask width" double 0.0 "Mask width to erase areas outside filament"
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 ""
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
930 button $w.buttons.ok -text OK -command "doExtractParticles $w"
931 button $w.buttons.cancel -text Cancel -command "destroy $w"
933 pack $w.msg -side top -padx 5 -pady 5
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
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
943 pack $w.buttons.ok $w.buttons.cancel -side left -padx 5
944 pack $w.buttons -side bottom -pady 2m
946 bind $w <Return> "doExtractParticles $w"
949 ## @brief Extracts particles from the current set of box coordinates.
951 # @param w Dialog window.
953 proc doExtractParticles { w } {
954 global filetypes project_item
955 global filename imgdir paramdir
956 global scale filltype
957 global back norm part_split
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]
974 Bmg box $project_item extract $xname $scale $back $norm $filltype $fill $mask_width $part_split $pathname
978 ## @brief Aligns particles by projection matching to a reference.
981 proc alignParticles { } {
984 global angle annmin annmax shift_limit
988 if ![winfo exists $w] {
996 set newfile [file rootname $paramfile]
997 append newfile "_aln.star"
998 set logfile [file rootname $paramfile]
999 append logfile "_aln.log"
1002 label $w.head.lab -text \
1003 "Aligning will be done on the currently saved parameter file: $paramfile"
1006 setupNewFileEntry $w.file "Output parameter file" $newfile \
1007 "New parameter file written after alignment"
1009 setupScrollEntry $w.ref "Reference map" "" "Template or reference map to align to"
1011 setupEntry $w.sym "Symmetry" alnum "C1" "Point group symmetry"
1013 setupResolutionEntry $w
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"
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
1027 checkbutton $w.peach -text "Peach" -variable peach
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
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
1038 bind $w <Return> "doAlignParticles $w"
1044 wm iconname $w "Align"
1047 ## @brief Executes marker tracking.
1049 # @param w dialog window.
1051 proc doAlignParticles { w } {
1052 global env paramfile
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]
1065 if { ![file exists $paramfile] } {
1066 puts "$paramfile not found!"
1068 set logfile "[file rootname $newfile].log"
1069 # set psfile "[file rootname $newfile].ps"
1070 # puts "logfile = $logfile"
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"
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
1079 ## @brief Refines particle orientations by central section matching to a reference.
1082 proc refineParticles { } {
1085 global angini angacc stpini stpacc def mag
1088 if ![winfo exists $w] {
1098 set newfile [file rootname $paramfile]
1099 append newfile "_aln.star"
1100 set logfile [file rootname $paramfile]
1101 append logfile "_aln.log"
1104 label $w.head.lab -text \
1105 "Aligning will be done on the currently saved parameter file: $paramfile"
1108 setupNewFileEntry $w.file "Output parameter file" $newfile \
1109 "New parameter file written after alignment"
1111 setupScrollEntry $w.ref "Reference map" "" "Template or reference map to align to"
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
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
1123 setupResolutionEntry $w
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"
1130 checkbutton $w.peach -text "Peach" -variable peach
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
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
1141 bind $w <Return> "doRefineParticles $w"
1146 wm title $w "Refine"
1147 wm iconname $w "Refine"
1150 ## @brief Executes particle orientation refinement.
1152 # @param w dialog window.
1154 proc doRefineParticles { w } {
1155 global env paramfile
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]
1169 if { ![file exists $paramfile] } {
1170 puts "$paramfile not found!"
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 " }
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
1185 ## @brief Aligns 3D particles to a reference.
1188 proc align3DParticles { } {
1191 global angle angacc shift_limit
1195 if ![winfo exists $w] {
1202 set newfile [file rootname $paramfile]
1203 append newfile "_aln.star"
1204 set logfile [file rootname $paramfile]
1205 append logfile "_aln.log"
1208 label $w.head.lab -text \
1209 "Aligning will be done on the currently saved parameter file: $paramfile"
1212 setupNewFileEntry $w.file "Output parameter file" $newfile \
1213 "New parameter file written after alignment."
1215 setupScrollEntry $w.ref "Reference map" "" "Template or reference map to align to."
1217 setupScrollEntry $w.mask "Mask" "" "Frequency space mask defining missing regions."
1219 setupEntry $w.sym "Symmetry" alnum "C1" "Point group symmetry"
1221 setupResolutionEntry $w
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
1228 setupEntry $w.shift "Shift limit" double $shift_limit \
1229 "The maximum number of pixels of shift allowed"
1231 checkbutton $w.peach -text "Peach" -variable peach
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
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
1242 bind $w <Return> "doAlign3DParticles $w"
1248 wm iconname $w "Align"
1251 ## @brief Executes marker tracking.
1253 # @param w dialog window.
1255 proc doAlign3DParticles { w } {
1256 global env paramfile
1258 global angle angacc shift_limit
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]
1270 if { ![file exists $paramfile] } {
1271 puts "$paramfile not found!"
1273 set logfile "[file rootname $newfile].log"
1274 # set psfile "[file rootname $newfile].ps"
1275 # puts "logfile = $logfile"
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
1283 ## @brief Selects particles after an alignment run.
1286 proc selectParticles { } {
1287 global paramfile theimg project_item
1288 global selall fom_check cv_check top_check ran_check defadj
1289 showSelectedParticles
1291 if ![winfo exists $w] {
1294 set newfile [file rootname $paramfile]
1295 append newfile "_sel.star"
1298 label $w.head.lab -text \
1299 "Particle seletion will be done using the currently saved parameter file: $paramfile"
1301 setupNewFileEntry $w.file "Output parameter file" $newfile \
1302 "New parameter file written after selection"
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"
1312 pack $w.head $w.file $w.all $w.fom $w.cv $w.top $w.defadj $w.ran -side top -pady 2 -padx 15
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
1321 bind $w <Return> "doSelectParticles $w"
1326 wm title $w "Select"
1327 wm iconname $w "Select"
1330 ## @brief Shows the current particle selection.
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
1340 ## @brief Executes particle selection.
1342 # @param w dialog window.
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]
1353 if { ![file exists $paramfile] } {
1354 puts "$paramfile not found!"
1357 if { $defadj } { set ad ",1" }
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
1369 ## @brief Reconstructs a single particle.
1372 proc particleReconstruct { } {
1373 global paramfile theimg project_item
1376 if ![winfo exists $w] {
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]
1384 set recfile [file rootname $paramfile]
1385 append recfile "_rec.mrc"
1386 set newfile [file rootname $paramfile]
1387 append newfile "_rec.star"
1390 label $w.head.lab -text \
1391 "Reconstruction will be done using the currently saved parameter file: $paramfile"
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
1399 setupNewFileEntry $w.file "Output parameter file" $newfile \
1400 "New parameter file written after reconstruction"
1402 setupEntry $w.cls "Classes" alnum "all" "Classes of particles to select for reconstruction"
1404 setupEntry $w.interp "Type" integer 0 "Reconstruction interpolation type: 0=nearest neighbor, 1=weighted, 2=trilinear"
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
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"
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
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
1426 bind $w <Return> "doReconstruct $w"
1431 wm title $w "Reconstruct"
1432 wm iconname $w "Reconstruct"
1435 ## @brief Executes tomogram reconstruction.
1437 # @param w dialog window.
1439 proc doReconstruct { w } {
1440 global env paramfile
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" } {
1459 if { ![file exists $paramfile] } {
1460 puts "$paramfile not found!"
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