2 # @file bshow_filament.tcl
4 # @brief Procedures to create, move and destroy filament selections
6 # @author Bernard Heymann
7 # @date Created: 20020808
8 # @date Modified: 20180410
11 set show_node_labels 1
14 set filament_node_radius 10
15 set boxing_interval 20
19 set fil_plot_width 350
20 set fil_plot_height 200
22 ## @brief Dialog box to manipulate filaments on the main canvas
26 global project_item theimg imgtype
28 global filament_width filament_node_radius boxing_interval
29 global helix_rise helix_angle
30 global fil_plot_width fil_plot_height
32 if ![Bimage exists $theimg] {
33 tk_dialog .dialog "No image in memory!" \
34 {Please read in an appropriate image first.} \
39 set c [getImageCanvas $theimg]
40 if { $filament_width < 1 } { set filament_width 40 }
41 if { $filament_node_radius < 1 } { set filament_node_radius [expr int($filament_width / 4)] }
42 if { $filament_node_radius < 1 } { set filament_node_radius 3 }
43 if { $boxing_interval < 1 } { set boxing_interval [expr int($filament_width / 2)] }
48 puts "Launching Particles for $c: $project_item"
51 if ![winfo exists $w] {
53 menu $w.menuBar -tearoff 0
54 $w.menuBar add cascade -menu $w.menuBar.fil -label "Filament" -underline 0
55 menu $w.menuBar.fil -tearoff 0
56 $w configure -menu $w.menuBar
57 $w.menuBar.fil add command -label "Help" -underline 0 \
58 -command { showURL "file://$Bsoft/doc/bshow/bshow_fil.html" }
59 # -command { showHelp bshow_filament.hlp }
60 $w.menuBar.fil add command -label "Write filament profile" \
61 -command { filamentProfileWrite } -underline 0
62 $w.menuBar.fil add command -label "Delete selected filament" \
63 -command { filamentDelete } -underline 0
64 $w.menuBar.fil add command -label "Delete all filaments" \
65 -command { filamentDeleteAll } -underline 0
66 $w.menuBar.fil add command -label "Center nodes" \
67 -command { filamentCenterNodes } -underline 0
68 $w.menuBar.fil add command -label "Extract filaments" \
69 -command { filamentExtract } -underline 0
70 $w.menuBar.fil add command -label "Generate particles" \
71 -command { filamentsToParticles } -underline 0
72 $w.menuBar.fil add command -label "Close" \
73 -command "destroy $w" -underline 0
76 label $w.fil_width.tag -font $helv12 -text "Box width" -width 15 -anchor e
77 entry $w.fil_width.entry -width 10 -validate key -vcmd { string is double %P }
78 $w.fil_width.entry insert 0 $filament_width
81 label $w.node_radius.tag -font $helv12 -text "Node radius" -width 15 -anchor e
82 entry $w.node_radius.entry -width 10 -validate key -vcmd { string is double %P }
83 $w.node_radius.entry insert 0 $filament_node_radius
85 frame $w.boxing_interval
86 label $w.boxing_interval.tag -font $helv12 -text "Boxing interval" -width 15 -anchor e
87 entry $w.boxing_interval.entry -width 10 -validate key -vcmd { string is double %P }
88 $w.boxing_interval.entry insert 0 $boxing_interval
91 label $w.helix_rise.tag -font $helv12 -text "Helix rise per unit" -width 15 -anchor e
92 entry $w.helix_rise.entry -width 10 -validate key -vcmd { string is double %P }
93 $w.helix_rise.entry insert 0 $helix_rise
96 label $w.helix_angle.tag -font $helv12 -text "Helix angle per unit" -width 15 -anchor e
97 entry $w.helix_angle.entry -width 10 -validate key \
98 -vcmd { expr {[string match {[-+]} %P] || [string is double %P]} }
99 $w.helix_angle.entry insert 0 $helix_angle
102 label $w.fil.tag -font $helv12 -text "Filaments" -width 15 -anchor e
103 label $w.fil.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w -text "0"
106 label $w.node.tag -font $helv12 -text "Nodes" -width 15 -anchor e
107 label $w.node.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w -text "0"
109 frame $w.fil_selected
110 label $w.fil_selected.tag -font $helv12 -text "Selected filament" -width 15 -anchor e
111 label $w.fil_selected.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w
113 frame $w.node_selected
114 label $w.node_selected.tag -font $helv12 -text "Selected node" -width 15 -anchor e
115 label $w.node_selected.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w
117 setupMicrographButtons $w.neighbor "micrograph"
120 canvas $g -width [expr $fil_plot_width + 10] -height [expr $fil_plot_height + 10] \
121 -background white -relief sunken -borderwidth 2
124 checkbutton $w.switches.filaments -text "Show filaments" -variable show_filaments \
125 -command { filamentsDrawAll }
126 checkbutton $w.switches.labels -text "Show labels" -variable show_node_labels \
127 -command { filamentsDrawAll }
128 checkbutton $w.switches.profiles -text "Show profiles" -variable show_profiles
130 pack $w.node_radius.tag $w.node_radius.entry -side left -pady 5 -padx 10
131 pack $w.fil_width.tag $w.fil_width.entry -side left -pady 5 -padx 10
132 pack $w.boxing_interval.tag $w.boxing_interval.entry -side left -pady 5 -padx 10
133 pack $w.helix_rise.tag $w.helix_rise.entry -side left -pady 5 -padx 10
134 pack $w.helix_angle.tag $w.helix_angle.entry -side left -pady 5 -padx 10
135 pack $w.fil.tag $w.fil.num -side left -pady 5 -padx 10
136 pack $w.node.tag $w.node.num -side left -pady 5 -padx 10
137 pack $w.fil_selected.tag $w.fil_selected.num -side left -pady 5 -padx 10
138 pack $w.node_selected.tag $w.node_selected.num -side left -pady 5 -padx 10
139 # pack $w.neighbor.prev $w.neighbor.next \
140 # -side left -pady 5 -padx 5 -ipadx 5 -ipady 5
141 pack $w.switches.filaments $w.switches.labels $w.switches.profiles \
142 -side left -pady 5 -padx 5
143 pack $w.fil_width $w.node_radius $w.boxing_interval $w.helix_rise \
144 $w.helix_angle $w.fil $w.node \
145 $w.fil_selected $w.node_selected $w.neighbor $w.plot $w.switches -side top -fill x -pady 2
151 wm title $w "Filaments"
152 wm iconname $w "Filaments"
153 .menuBar.window entryconfigure "Filaments" -state normal
154 bind $c <1> "nodeCreateSelect %x %y"
155 bind $c <2> "filamentCreate %x %y"
156 bind $c <Command-1> "filamentCreate %x %y"
157 bind $c <Control-1> "filamentCreate %x %y"
158 bind $c <Shift-1> "nodeDelete %x %y"
159 bind $c <Shift-2> "nodeDelete %x %y"
160 bind $c <B1-Motion> "nodeMove %x %y"
161 bind $c <B2-Motion> "nodeMove %x %y"
162 # bind $c <ButtonRelease> "filamentsDrawAll"
163 # bind $c <ButtonRelease> "drawSpline %W"
164 bind $w <Return> "Update 0"
165 bind $w <Control-w> { destroy .wfil }
166 bind $w <Command-w> { destroy .wfil }
171 ## @brief Sets the node radius from the dialog box.
173 proc setNodeRadius { } {
174 if ![winfo exists .wfil] { return }
175 global filament_node_radius project_item
176 set filament_node_radius [.wfil.node_radius.entry get]
177 Bmg set $project_item filament_node_radius $filament_node_radius
180 ## @brief Starts a new filament
183 # @param y First node position in canvas coordinates.
185 proc filamentCreate { x y } {
188 nodeCreateSelect $x $y
191 ## @brief Creates or selects a node.
194 # @param y Node position in canvas coordinates.
196 proc nodeCreateSelect { x y } {
197 if ![winfo exists .wfil] { return }
201 set snid $node_selected
202 set node_selected [nodeSelect $x $y]
203 if { [llength $node_selected] > 0 } { return }
204 if { $tool != "filament" } { return }
205 set wc [getControlWindow $theimg]
207 set n [$wc.image.scale get]
208 set mg_item [micrographItem $n]
209 set loc [imageCoordinatesFromCanvas $x $y]
210 set id [Bmg node $mg_item create [lindex $loc 0] [lindex $loc 1] [lindex $loc 2] [lindex $snid 0] [lindex $snid 1]]
211 if { [llength $id] > 0 } {
212 nodeDraw [lindex $id 0] [lindex $id 1]
213 # puts "created ID = $id"
215 set node_selected $id
216 filamentUpdateSelected
218 # filamentDraw [lindex $node_selected 0]
221 ## @brief Selects a node on the canvas.
224 # @param y Node position in window coordinates.
226 # @returns node_selected Index of the selected node, 0 if none selected.
228 proc nodeSelect { x y } {
229 if ![winfo exists .wfil] { return }
232 set wc [getControlWindow $theimg]
234 set n [$wc.image.scale get]
235 set mg_item [micrographItem $n]
236 set loc [imageCoordinatesFromCanvas $x $y]
238 set id [Bmg node $mg_item select [lindex $loc 0] [lindex $loc 1] [lindex $loc 2]]
239 if { [llength $id] > 0 } {
240 nodeDraw [lindex $id 0] [lindex $id 1]
241 # puts "selected ID = $id"
243 set node_selected $id
244 filamentUpdateSelected
245 # drawSelectedNode $c
246 return $node_selected
249 ## @brief Moves the selected node
252 # @param y Node position in canvas coordinates.
254 proc nodeMove { x y } {
258 if { [llength $node_selected] < 1 } { return }
259 set c [getImageCanvas $theimg]
260 set wc [getControlWindow $theimg]
261 set scale [$wc.scale.scale get]
262 set n [$wc.image.scale get]
263 set ix [expr [$c canvasx $x] / $scale ]
264 set iy [expr [$c canvasy $y] / $scale ]
265 set dx [expr $ix - $px]
266 set dy [expr $py - $iy]
269 # puts "Moving $dx $dy"
270 set mg_item [micrographItem $n]
271 Bmg node $mg_item move [lindex $node_selected 0] [lindex $node_selected 1] $dx $dy
275 ## @brief Draws a circle for a node on the canvas.
277 # @param fid Filament identifier.
278 # @param nid Node identifier.
280 proc nodeDraw { fid nid } {
282 global node_selected show_node_labels
283 global filament_color filament_first_color filament_select_color
284 set c [getImageCanvas $theimg]
285 set wc [getControlWindow $theimg]
286 set n [$wc.image.scale get]
287 set z [$wc.slice.scale get]
288 set mg_item [micrographItem $n]
289 set loc [Bmg node $mg_item location $fid $nid]
290 # puts "drawing $fid $nid: $loc"
291 set scale [$wc.scale.scale get]
292 set x [expr [lindex $loc 0] * $scale ]
293 set y [expr [yFlip [lindex $loc 1]] * $scale ]
294 set zr [expr abs([lindex $loc 2] - $z) ]
295 set radius [expr [.wfil.node_radius.entry get] * $scale]
296 set xmin [expr $x - $radius ]
297 set xmax [expr $x + $radius + $scale - 1 ]
298 set ymin [expr $y - $radius ]
299 set ymax [expr $y + $radius + $scale - 1 ]
300 set color $filament_color
302 set color $filament_first_color
303 $c create text $xmin $y -text $fid -fill blue -anchor e -tags node
305 if { $fid == [lindex $node_selected 0] && $nid == [lindex $node_selected 1] } {
306 set color $filament_select_color
308 $c create oval $xmin $ymin $xmax $ymax -outline $color -width 1 -tags node
309 if { $show_node_labels } {
310 $c create text $xmax $y -text $nid -fill $color -anchor w -tags node
314 ## @brief Draws a line between two nodes on the canvas.
316 # @param fid Filament identifier.
317 # @param nid1 Node 1 identifier.
318 # @param nid2 Node 2 identifier.
320 proc nodeLinkDraw { fid nid1 nid2 } {
322 global filament_color
323 set c [getImageCanvas $theimg]
324 set wc [getControlWindow $theimg]
325 set n [$wc.image.scale get]
326 set z [$wc.slice.scale get]
327 set mg_item [micrographItem $n]
328 set loc1 [Bmg node $mg_item location $fid $nid1]
329 set loc2 [Bmg node $mg_item location $fid $nid2]
330 set scale [$wc.scale.scale get]
331 set x1 [expr [lindex $loc1 0] * $scale ]
332 set y1 [expr [yFlip [lindex $loc1 1]] * $scale ]
333 set x2 [expr [lindex $loc2 0] * $scale ]
334 set y2 [expr [yFlip [lindex $loc2 1]] * $scale ]
336 set dz [expr [lindex $loc2 2] - [lindex $loc1 2]]
338 set fz [expr [lindex $loc2 2] - $z + 0.5]
340 set fz [expr ([lindex $loc2 2] - $z)/($dz*1.0)]
342 if { $fz >= 0.0 && $fz <= 1.0 } {
343 $c create line $x1 $y1 $x2 $y2 -fill $filament_color -width 1 -tags fil
347 ## @brief Draws filament nodes and lines connecting nodes on the canvas
350 proc filamentsDrawAll { } {
352 global show_filaments
353 set c [getImageCanvas $theimg]
354 set wc [getControlWindow $theimg]
355 $c delete fil node selnode
356 if { $show_filaments < 1 } { return }
357 set n [$wc.image.scale get]
358 set mg_item [micrographItem $n]
359 set ids [Bmg node $mg_item ids]
360 # puts "Node IDs: $ids"
364 foreach {fid nid} $ids {
366 if { $pfid == $fid } {
367 nodeLinkDraw $fid $pnid $nid
375 ## @brief Updates the selected filament and node in the dialog window
378 proc filamentUpdateSelected { } {
379 if ![winfo exists .wfil] { return }
380 global node_selected project_item
381 # puts "Project item: $project_item"
382 set nfil [Bmg filament $project_item count]
383 set nnode [Bmg node $project_item count]
384 .wfil.fil.num config -text "$nfil"
385 .wfil.node.num config -text "$nnode"
386 if { [llength $node_selected] < 0 } {
387 .wfil.fil_selected.num config -text ""
388 .wfil.node_selected.num config -text ""
390 .wfil.fil_selected.num config -text [lindex $node_selected 0]
391 .wfil.node_selected.num config -text [lindex $node_selected 1]
392 filamentProfilePlot [lindex $node_selected 0] [lindex $node_selected 1]
396 ## @brief Deletes the selected node.
399 # @param y Node position in window coordinates.
401 proc nodeDelete { x y } {
402 if ![winfo exists .wfil] { return }
403 global theimg node_selected
404 set wc [getControlWindow $theimg]
405 set n [$wc.image.scale get]
406 set mg_item [micrographItem $n]
407 set id [nodeSelect $x $y]
408 # puts "ID to be deleted = $id"
409 if { [llength $id] > 0 } {
410 Bmg node $mg_item delete [lindex $id 0] [lindex $id 1]
416 ## @brief Deletes the selected filament.
419 proc filamentDelete { } {
420 if ![winfo exists .wfil] { return }
421 global theimg node_selected project_item
422 set wc [getControlWindow $theimg]
423 set n [$wc.image.scale get]
424 set mg_item [micrographItem $n]
425 if { [llength $node_selected] > 0 } {
426 Bmg filament $mg_item delete [lindex $node_selected 0]
432 ## @brief Deletes all filaments.
435 proc filamentDeleteAll { } {
436 global theimg project_item
437 set c [getImageCanvas $theimg]
438 Bmg filament $project_item delete all
439 $c delete node selnode
442 filamentUpdateSelected
445 ## @brief Centers filament nodes.
448 proc filamentCenterNodes { } {
449 global filament_width project_item
450 set filament_width [.wfil.fil_width.entry get]
452 Bmg filament $project_item center $filament_width
456 ## @brief Extracts filaments defined in the micrograph parameters.
459 proc filamentExtract { } {
461 global filament_width project_item
463 if { [Bmg node $project_item count] < 1 } {
464 tk_messageBox -icon error -type ok -title "Error" -message \
465 "No filaments picked!"
472 wm title $w "Extracting filaments"
473 wm iconname $w "ExtFil"
475 label $w.msg -font $helv12 -wraplength 4i -justify left \
476 -text {Select parameters for filament extraction}
480 frame $w.panel.fil_width
481 label $w.panel.fil_width.tag -font $helv12 -text "Filament width" -width 30 -anchor w
482 entry $w.panel.fil_width.entry -width 10 -relief sunken \
483 -validate key -vcmd { string is double %P }
484 $w.panel.fil_width.entry insert 0 $filament_width
486 frame $w.panel.fil_axis
487 label $w.panel.fil_axis.tag -font $helv12 -text "Filament axis" -width 30 -anchor w
488 radiobutton $w.panel.fil_axis.x -text "x" -variable fil_axis -value 1
489 radiobutton $w.panel.fil_axis.y -text "y" -variable fil_axis -value 2
490 radiobutton $w.panel.fil_axis.z -text "z" -variable fil_axis -value 3
493 label $w.panel.path.tag -font $helv12 -text "Filament path" -width 10 -anchor w
494 entry $w.panel.path.entry -width 50 -relief sunken
495 $w.panel.path.entry insert 0 ""
498 checkbutton $w.check.splitbutton -text "Individual output files" \
499 -width 20 -anchor w -variable fil_split
502 button $w.buttons.ok -text OK -command "filamentExtract_and_DestroyWindow $w"
503 button $w.buttons.cancel -text Cancel -command "destroy $w"
505 pack $w.msg -side top -padx 5 -pady 5
507 pack $w.panel.fil_width.tag $w.panel.fil_width.entry -side left -padx 5 -pady 5
508 pack $w.panel.fil_axis.tag $w.panel.fil_axis.x $w.panel.fil_axis.y \
509 $w.panel.fil_axis.z -side left -padx 5 -pady 5
510 pack $w.panel.path.tag $w.panel.path.entry -side left -padx 5 -pady 5
511 pack $w.panel.fil_width $w.panel.fil_axis $w.panel.path -side top
512 pack $w.panel -side top -fill both
514 pack $w.check.splitbutton -side left -pady 5 -padx 5
515 pack $w.check -side top -pady 2m
517 pack $w.buttons.ok $w.buttons.cancel -side left -padx 5
518 pack $w.buttons -side bottom -pady 2m
520 bind $w <Return> "filamentExtract_and_DestroyWindow $w"
523 ## @brief Extracts filaments from the current set of node coordinates.
525 # @param w Dialog window.
527 proc filamentExtract_and_DestroyWindow { w } {
529 global filename project_item
530 global filament_width
531 global fil_axis fil_split
533 set xname [file root [file tail $filename]]
534 append xname "_fil.mrc"
535 set xname [tk_getSaveFile -filetypes $filetypes \
536 -initialfile $xname -defaultextension .mrc]
537 if { [string length $xname] < 1 } { return }
538 # puts "Extract from $filename to $xname"
539 set pathname [$w.panel.path.entry get]
540 set filament_width [$w.panel.fil_width.entry get]
542 # puts "Filament axis = $fil_axis split = $fil_split"
543 .wfil.fil_width.entry delete 0 end
544 .wfil.fil_width.entry insert 0 $filament_width
545 Bmg set $project_item pixel_size [getPixelSizeEntry]
547 Bmg filament $project_item extract $xname $filament_width $fil_axis $fil_split $pathname
551 ## @brief Creates particles on filaments.
554 proc filamentsToParticles { } {
555 global filament_width filament_node_radius boxing_interval
556 global helix_rise helix_angle project_item
558 set filament_width [.wfil.fil_width.entry get]
559 set filament_node_radius [.wfil.node_radius.entry get]
560 set boxing_interval [.wfil.boxing_interval.entry get]
561 set helix_rise [.wfil.helix_rise.entry get]
562 set helix_angle [.wfil.helix_angle.entry get]
564 Bmg set $project_item filament_width $filament_width
565 Bmg set $project_item filament_node_radius $filament_node_radius
566 # puts "$filament_width $boxing_interval $helix_rise $helix_angle"
567 set np [Bmg filament $project_item toparticles $filament_width $boxing_interval $helix_rise $helix_angle]
568 # puts "$np particles generated"
572 ## @brief Updates node parameters from the micrograph parameters in memory.
575 proc updateNodeParam { } {
576 global project_item imgtype
577 global filament_width filament_node_radius
580 if ![winfo exists $w] { return }
581 # puts "Updating filament parameters"
582 if ![Bmg exists] { createMgParam }
583 set filename [Bmg get $project_item filename $imgtype]
584 # puts "File: $filename"
585 wm title $w [list "Filaments:" $filename]
587 set filament_width [$w.fil_width.entry get]
588 set filament_node_radius [$w.node_radius.entry get]
589 Bmg set $project_item filament_width $filament_width
590 Bmg set $project_item filament_node_radius $filament_node_radius
591 # puts "$filament_width $filament_node_radius"
592 $w.fil_width.entry delete 0 end
593 $w.fil_width.entry insert 0 $filament_width
594 $w.node_radius.entry delete 0 end
595 $w.node_radius.entry insert 0 $filament_node_radius
596 filamentUpdateSelected
599 ## @brief Plots the selected filament profile in the plot canvas
601 # @param fid filament identifier.
602 # @param nid node identifier.
604 proc filamentProfilePlot { fid nid } {
605 global theimg project_item
606 global filament_width
607 global fil_plot_width fil_plot_height
610 if ![winfo exists .wfil] { return }
611 if { $fid < 1 } { return }
612 if { $nid < 1 } { return }
617 if !$show_profiles { return }
619 set prof [Bmg filament $project_item profile $fid $nid $filament_width]
621 set len [lindex $prof 0]
622 if { $len < 1 } { return }
624 set min [lindex $prof 1]
625 set max [lindex $prof 1]
626 for {set i 1} {$i <= $len} {incr i 1} {
627 set v [lindex $prof $i]
628 if { $min > $v } { set min $v }
629 if { $max < $v } { set max $v }
631 if { $max <= $min } { set max [expr $min + 1] }
632 # puts "profile: $min $max"
634 set yoff [expr $fil_plot_height * ( (3*$max - $min) * 0.25 / ($max - $min)) + 5]
635 set xscale [expr $fil_plot_width*1.0/$len]
636 set yscale [expr $fil_plot_height*0.5/($max - $min)]
638 # puts "$yoff $yscale"
642 for {set i 1} {$i <= $len} {incr i 1} {
643 set j [expr $i + $len]
644 set x [expr ($i-1)*$xscale + $xoff]
645 set y [expr $yoff - [lindex $prof $i] * $yscale]
646 set ys [expr $yoff - [lindex $prof $j] * $yscale]
647 append fp " " $x " " $y
648 append fps " " $x " " $ys
650 $g create line $fp -fill blue -smooth yes -width 1 -tags filplot
651 $g create line $fps -fill red -smooth yes -width 1 -tags filplot
652 set orix [expr $fil_plot_width/2 + $xoff]
654 $g create line $orix 0 $orix [expr 2*$yoff + $fil_plot_height] \
655 -fill black -smooth yes -width 1 -tags filplot
656 $g create line 0 $oriy [expr 2*$xoff + $fil_plot_width] $oriy \
657 -fill black -smooth yes -width 1 -tags filplot
660 ## @brief Writes the selected filament profile to a file
663 proc filamentProfileWrite { } {
664 global theimg project_item
666 global filament_width
667 if { [llength $node_selected] < 0 } {
668 tk_dialog .dialog "No filament selected!" \
669 {Please select a filament first.} \
673 set filename [Bimage get $theimg filename]
674 set fprfile "[file rootname [file tail $filename]].txt"
675 set fprfile [tk_getSaveFile -initialfile $fprfile -defaultextension .txt]
676 if { [string length $fprfile] < 1 } { return }
677 set wc [getControlWindow $theimg]
678 set pixel_size [$wc.pixel_size.x get]
679 set fid [lindex $node_selected 0]
680 set nid [lindex $node_selected 1]
681 set prof [Bmg filament $project_item profile $fid $nid $filament_width]
682 set len [lindex $prof 0]
683 if { $len < 1 } { return }
684 set ffpr [open $fprfile w]
685 puts $ffpr "File name: $filename"
686 puts $ffpr "Filament: $fid"
687 puts $ffpr "Pixel size: $pixel_size angstrom/pixel"
689 puts $ffpr "Pixel Density"
690 for {set i 1} {$i <= $len} {incr i 1} {
691 set v [lindex $prof $i]