| Bsoft 2.0.5 Bernard's software package

bshow_param.tcl
Go to the documentation of this file.
1 ##
2 # @file bshow_param.tcl
3 #
4 # @brief Procedures to read and write parameter files
5 #
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20190911
9
10 set paramfile ""
11 set paramdir [pwd]
12 set project_item ""
13 set fom_cutoff 0
14 set tilt_axis 0
15 set tilt_angle 0
16 set tilt_offset 0
17 set show_tilt_axis 1
18
19 ## @brief Menu for opening and closing parameter files
20 #
21
22 proc setupParameterMenu {} {
23 global theimg
24
25 .menuBar add cascade -menu .menuBar.param -label "Micrograph" -underline 0
26 menu .menuBar.param -tearoff 0
27 .menuBar.param add command -label "Read parameters" \
28 -command { openParam } \
29 -underline 0 -accelerator "Ctrl-r"
30 .menuBar.param add command -label "Append parameters" \
31 -command { appendParam } \
32 -underline 0 -accelerator "Ctrl-a"
33 .menuBar.param add command -label "Write parameters" \
34 -command { writeParam } \
35 -underline 0 -accelerator "Ctrl-w"
36 .menuBar.param add command -label "Edit parameters" \
37 -command { editParam } \
38 -underline 0 -accelerator "Ctrl-e"
39 .menuBar.param add command -label "Change image" \
40 -command { selectAFile } \
41 -underline 0 -accelerator "Ctrl-a"
42 .menuBar.param add command -label "Fit CTF" \
43 -command { CTF $theimg } \
44 -underline 0 -accelerator "Ctrl-c"
45 .menuBar.param add command -label "Particles" \
46 -command { Particles } \
47 -underline 0 -accelerator "Ctrl-p"
48 .menuBar.param add command -label "Filaments" \
49 -command { Filaments } \
50 -underline 0 -accelerator "Ctrl-f"
51 .menuBar.param add command -label "Helix" \
52 -command { Helix } \
53 -underline 0 -accelerator "Ctrl-h"
54 .menuBar.param add command -label "Crystallography" \
55 -command { Crystallography } \
56 -underline 0 -accelerator "Ctrl-x"
57 .menuBar.param add command -label "Tomography" \
58 -command { Tomography } \
59 -underline 0 -accelerator "Ctrl-t"
60 }
61
62
63 ## @brief Opens a micrograph parameter file for reading.
64 #
65
66 proc openParam { } {
67 global paramfile paramdir
68 set currdir $paramdir
69 if { [string length $currdir] < 2 } { set currdir [pwd]}
70 set types {
71 {"STAR files" {.star .STAR} }
72 {"XML files" {.xml .XML} }
73 {"EMX files" {.emx .EMX} }
74 }
75 set paramfile [tk_getOpenFile -title "Open parameter file" -filetypes $types \
76 -initialdir $currdir -defaultextension .star]
77 if { [string length $paramfile] < 1 } { return}
78 loadParam $paramfile
79 Update 0
80 }
81
82 ## @brief Opens a micrograph parameter file for reading.
83 #
84 # @param file parameter file name.
85
86 proc loadParam { file } {
87 global paramfile paramdir
88 global project_item
89 set project_item "all"
90 set paramfile $file
91 Bmg read $paramfile
92 set paramdir [file dirname $paramfile]
93 set paramfile [file tail $paramfile]
94 # puts "$paramdir $paramfile"
95 selectAFile
96 }
97
98 ## @brief Appends one or more micrograph parameter files.
99 #
100
101 proc appendParam { } {
102 global paramfile paramdir
103 set currdir $paramdir
104 if { [string length $currdir] < 2 } { set currdir [pwd]}
105 set types {
106 {"STAR files" {.star .STAR} }
107 {"XML files" {.xml .XML} }
108 {"EMX files" {.emx .EMX} }
109 }
110 set addfiles [tk_getOpenFile -title "Append parameter files" -filetypes $types \
111 -multiple true -initialdir $currdir -defaultextension .star]
112 if { [string length $addfiles] < 1 } { return}
113 puts $addfiles
114 # loadParam $paramfile
115 Bmg add $addfiles
116 Update 0
117 }
118
119 proc tag_line { w } {
120 set cur [$w index current]
121 $w tag delete seline
122 $w tag add seline "$cur linestart" "$cur lineend"
123 $w tag configure seline -background yellow
124 }
125
126 ## @brief Dialog box to select an image file.
127 #
128
129 proc selectAFile { } {
130 global filename
131 global imgtype
132 global project_item
133 if { [Bmg exists] } {
134 set img_files [Bmg get all image_filenames]
135 } elseif { [Bmodel exists] } {
136 set img_files [Bmodel get all image_filenames]
137 } else {
138 return
139 }
140 # puts "$img_files"
141 set oldfilename $filename
142 if { [llength $img_files] < 1 } {
143 puts "Error: No image files found!"
144 if { [Bmodel exists] } {
145 Bmodel set all map $filename
146 }
147 } elseif { [llength $img_files] < 2 } {
148 set list [split $img_files ":"]
149 set filename [lindex $list 0]
150 set imgtype [lindex $list 1]
151 set project_item "[lindex $list 2]:[lindex $list 3]"
152 # puts "$filename ($imgtype) $project_item"
153 } else {
154 set w .wsel
155 catch {destroy $w}
156 toplevel $w
157 wm title $w "Select an image"
158 wm iconname $w "Select"
159
160 label $w.msg -wraplength 4i -justify left \
161 -text {Double click on the desired file name}
162 pack $w.msg -side top -fill x -pady 2m
163
164 frame $w.file
165 scrollbar $w.file.xscroll -command "$w.file.list xview" -orient horizontal
166 scrollbar $w.file.yscroll -command "$w.file.list yview"
167 text $w.file.list -width 80 -height 20 -relief sunken -borderwidth 3 \
168 -xscrollcommand "$w.file.xscroll set" \
169 -yscrollcommand "$w.file.yscroll set" -wrap none
170 pack $w.file.xscroll -side bottom -expand 0 -fill x
171 pack $w.file.yscroll -side right -expand 0 -fill y
172 pack $w.file.list -side left -expand 1 -fill both
173 pack $w.file -side top -fill both -pady 2m -expand 1
174
175 frame $w.buttons
176 button $w.buttons.ok -text OK -command { setSelectionAndDestroy }
177 button $w.buttons.cancel -text Cancel -command { destroy .wsel }
178 pack $w.buttons -side bottom -fill x -pady 2m
179 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
180
181 foreach name $img_files {
182 $w.file.list insert end "$name"
183 # puts "$filename $name"
184 if [string length $filename] {
185 if [regexp $filename $name] {
186 $w.file.list see end
187 tag_line $w.file.list
188 # puts $filename
189 }
190 }
191 $w.file.list insert end "\n"
192 }
193 $w.file.list delete [$w.file.list index current] end
194 if ![string length $filename] {
195 $w.file.list mark set current 1.0
196 tag_line $w.file.list
197 }
198 # puts [$w.file.list index current]
199
200 bind $w.file.list <1> { tag_line .wsel.file.list }
201 # bind $w.file.list <Double-1> { setSelectionAndDestroy }
202 # bind $w.file.list <Double-1> {
203 # tag_line .wsel.file.list
204 # setSelectionAndDestroy
205 # }
206
207 lower . .wsel
208 tkwait window $w
209 }
210 # puts "$oldfilename --- $filename"
211 # puts "$filename ($imgtype) $project_item"
212 if { [string compare $oldfilename $filename] != 0 } {
213 loadImage $filename -1
214 }
215 updateParametersAndImage
216 }
217
218 proc setSelectionAndDestroy { } {
219 global filename
220 global imgtype
221 global project_item
222 set item [.wsel.file.list get -- seline.first seline.last]
223 set list [split $item ":"]
224 set filename [lindex $list 0]
225 set imgtype [lindex $list 1]
226 set project_item "[lindex $list 2]:[lindex $list 3]"
227 # puts "$filename ($imgtype) $project_item"
228 destroy .wsel
229 }
230
231 proc updateClassParam { } {
232 global project_item theimg
233 if [regexp "Class" $project_item] {
234 set n [Bimage get $theimg nimages]
235 for { set i 0} { $i < $n } { incr i} {
236 Bimage set $theimg select $i [Bmg get $project_item select [expr $i + 1]]
237 }
238 }
239 }
240
241 proc updateParametersAndImage { } {
242 global filename paramfile modelfile
243 global theimg imgtype
244 global project_item mg_sel
245
246 # puts "Updating parameters and image: $project_item"
247
248 if { [Bmg exists] } {
249 Bmg set all active 0
250 if [regexp "Reconstruction" $project_item] { Bmg set all active 1}
251 if { $imgtype == "frame" } {
252 if { [Bimage get $theimg nslices] > 1 } { switchImage}
253 }
254 set mg_sel [Bmg get $project_item select]
255 wm title . [list $filename ":" $paramfile]
256 # puts [Bmg get all number_of_rec]
257 } elseif { [Bmodel exists] } {
258 wm title . [list $filename ":" $modelfile]
259 }
260
261 # Bimage set $theimg label $project_item
262 updatePixelsize
263 updateOrigins
264
265 if { [Bmg exists] } {
266 getCTFparam
267 updateClassParam
268 updateBoxParam
269 updateNodeParam
270 updateTomoTable
271 updateMarkerParam
272 updateSpotParam
273 updateLayerLineParam
274 # puts [Bmg get all number_of_rec]
275 } elseif { [Bmodel exists] } {
276 objectsDrawAll
277 }
278 }
279
280 proc getNeighborImageOfType { dir } {
281 global filename
282 global theimg imgtype
283 global project_item
284
285 set wc [getControlWindow $theimg]
286 set img_num [expr [$wc.image.scale get] + $dir]
287 set img_max [$wc.image.scale cget -to]
288
289 if { $img_num >= 0 && $img_num <= $img_max } {
290 $wc.image.scale set $img_num
291 updateParametersAndImage
292 return
293 }
294
295 if { [Bmg exists] } {
296 set img_files [Bmg get all image_filenames]
297 } elseif { [Bmodel exists] } {
298 set img_files [Bmodel get all image_filenames]
299 } else {
300 return
301 }
302
303 # set oldfilename $filename
304 set f ""
305 set prevfile ""
306 set prevlist ""
307 set nxt 0
308 foreach name $img_files {
309 set list [split $name ":"]
310 if [string equal $imgtype [lindex $list 1]] {
311 set f [lindex $list 0]
312 if { $nxt } {
313 break
314 }
315 if [string equal $filename $f] {
316 if { $dir > 0 } {
317 set nxt 1
318 set f ""
319 } else {
320 # if [string length $prevfile] { set f $prevfile }
321 # if [string length $prevlist] { set list $prevlist }
322 set f $prevfile
323 set list $prevlist
324 break
325 }
326 }
327 set prevfile $f
328 set prevlist $list
329 }
330 }
331
332 if [string length $f] {
333 set filename $f
334 set project_item "[lindex $list 2]:[lindex $list 3]"
335 loadImage $filename -1
336 updateParametersAndImage
337 }
338 }
339
340 proc getNeighborImageOfType2 { dir } {
341 global filename
342 global theimg imgtype
343 global project_item
344
345 set wc [getControlWindow $theimg]
346 set img_num [expr [$wc.image.scale get] + $dir]
347
348 if { [Bmg exists] } {
349 set img_files [Bmg get all image_filenames]
350 } elseif { [Bmodel exists] } {
351 set img_files [Bmodel get all image_filenames]
352 } else {
353 return
354 }
355
356 # set oldfilename $filename
357 set f ""
358 set curr_item ""
359 set prevfile ""
360 set prevlist ""
361 set nxt 0
362 # puts $project_item
363 foreach name $img_files {
364 set list [split $name ":"]
365 # puts $name
366 if [string equal $imgtype [lindex $list 1]] {
367 set f [lindex $list 0]
368 set curr_item "[lindex $list 2]:[lindex $list 3]"
369 if { $nxt } {
370 break
371 }
372 # puts $curr_item
373 if [string equal $project_item $curr_item] {
374 # puts $curr_item
375 if { $dir > 0 } {
376 set nxt 1
377 } else {
378 if [string length $prevfile] { set f $prevfile}
379 if [string length $prevlist] { set list $prevlist}
380 break
381 }
382 }
383 set prevfile $f
384 set prevlist $list
385 }
386 }
387
388 if [string length $f] {
389 set project_item "[lindex $list 2]:[lindex $list 3]"
390 if [string equal $filename $f] {
391 } else {
392 set filename $f
393 loadImage $filename -1
394 }
395 $wc.image.scale set $img_num
396 updateParametersAndImage
397 }
398 }
399
400 ## @brief Sets up micrograph id and total.
401 #
402 # @param w window.
403
404 proc setupMicrographID { w } {
405 global project_item
406
407 set idx [Bmg get $project_item index_of_mg]
408 set nmg [Bmg get all number_of_mg]
409
410 if ![winfo exists $w] {
411 frame $w
412 }
413
414 label $w.tag -text "Micrograph" -width 10 -anchor e
415 label $w.str -width 50 -text $project_item -relief sunken -anchor w
416 label $w.idx -width 5 -text $idx -anchor w
417 label $w.nmg -width 5 -text " / $nmg" -anchor w
418
419 pack $w.tag $w.str $w.idx $w.nmg -side left -padx 1m
420 }
421
422 ## @brief Sets up micrograph navigation and selection buttons.
423 #
424 # @param w window.
425 # @param typelabel type of navigation (micrograph).
426
427 proc setupMicrographButtons { w typelabel } {
428 global mg_sel
429
430 if ![winfo exists $w] {
431 frame $w
432 }
433
434 button $w.prev -text "Previous $typelabel" \
435 -relief raised -command "getNeighborImageOfType -1"
436 button $w.next -text "Next $typelabel" \
437 -relief raised -command "getNeighborImageOfType 1"
438 checkbutton $w.select -text "Select" -variable mg_sel \
439 -command { selectMicrograph }
440 pack $w.prev $w.next $w.select \
441 -side left -pady 2 -padx 2 -ipadx 2 -ipady 2
442 }
443
444 ## @brief Creates micrograph parameters from the image.
445 #
446
447 proc createMgParam { } {
448 global filename theimg imgtype
449 global paramfile project_item
450 if [Bmg exists] { return}
451 set id_name [file rootname [file tail $filename]]
452 # puts "New ID: $id_name"
453 set nz [Bimage get $theimg nslices]
454 if { $imgtype == "" } { setImageType}
455 if { $imgtype == "mg" || $imgtype == "frame" } {
456 if { $nz > 1 } { switchImage}
457 set nz [Bimage get $theimg nslices]
458 }
459 set nimg [Bimage get $theimg nimages]
460 Bmg create $filename $imgtype
461 if { $nz > 1 } {
462 Bmg set all active 1
463 if { $nimg > 1 } {
464 set imgtype "part"
465 } else {
466 set imgtype "rec"
467 }
468 set project_item "Reconstruction:$id_name"
469 Bmg set $project_item id $id_name
470 } else {
471 Bmg set all active 0
472 set project_item "Micrograph:$id_name"
473 Bmg set $project_item id $id_name
474 if { $nimg > 1 && $imgtype != "frame" } {
475 set project_item "Field:$id_name"
476 Bmg set $project_item id $id_name
477 }
478 }
479 # puts "New item: $project_item"
480 set origin [Bimage get $theimg origin]
481 set ps [Bimage get $theimg pixel_size]
482 Bmg set $project_item origin [lindex $origin 0] [lindex $origin 1] [lindex $origin 2]
483 Bmg set $project_item pixel_size $ps
484 # puts "Pixel size: $ps"
485 # Bimage set $theimg label $project_item
486 }
487
488 ## @brief Returns the current project item.
489 #
490 # @param n micrograph index (first = 0)
491
492 proc micrographItem { n } {
493 global project_item
494
495 if { ![Bmg exists] && ![Bmodel exists] } { return ""}
496
497 if [regexp "Model" $project_item] {
498 return $project_item
499 } elseif [regexp "Reconstruction" $project_item] {
500 return $project_item
501 } elseif [regexp "Class" $project_item] {
502 return $project_item
503 } elseif [regexp "Micrograph" $project_item] {
504 set id [Bmg get $project_item id $n]
505 return "Micrograph:$id"
506 # return $project_item
507 } elseif [regexp "Field" $project_item] {
508 set id [Bmg get $project_item id $n]
509 return "Micrograph:$id"
510 } else {
511 puts "Error: project_item not defined! ($project_item)"
512 }
513 }
514
515 proc selectMicrograph { } {
516 global project_item mg_sel
517 Bmg set $project_item select $mg_sel
518 }
519
520 ## @brief Updates an existing project for micrograph parameters in memory.
521 #
522
523 proc setMgParam { } {
524 global filament_width filament_node_radius
525 global marker_radius fom_cutoff
526 global theimg imgtype
527 global filename project_item
528 if ![Bmg exists] { createMgParam}
529 puts "setMgParam: $project_item"
530 # Bimage set $theimg label $project_item
531 if { [Bimage get $theimg nslices] > 1 } { # Converting from mg to rec
532 Bmg set all active 1
533 if { [string first "mg" $imgtype] > -1 } {
534 set imgtype "rec"
535 puts "Trying to convert to reconstruction"
536 }
537 # puts "Reconstruction: $imgtype $filename"
538 } else {
539 if { [string first "rec" $imgtype] > -1 } { set imgtype "mg"}
540 }
541 # puts "Bmg set $project_item filename $filename $imgtype"
542 Bmg set $project_item filename $filename $imgtype
543 Bmg set $project_item pixel_size [.pixel_size.entry get]
544 # if { [Bimage get $theimg nslices] < 2 } { # Micrograph parameters only
545 # if [winfo exists .wctf] { setCTFparam }
546 # }
547 puts "setMgParam: $filename"
548 setBoxSize
549 if [winfo exists .wfil] {
550 set filament_width [.wfil.fil_width.entry get]
551 set filament_node_radius [.wfil.node_radius.entry get]
552 Bmg set $project_item filament_width $filament_width
553 Bmg set $project_item filament_node_radius $filament_node_radius
554 }
555 if [winfo exists .wtomo] {
556 set marker_radius [.wtomo.marker_radius.e get]
557 Bmg set $project_item marker_radius $marker_radius
558 }
559 }
560
561 ## @brief Saves the micrograph data base to a parameter file.
562 #
563
564 proc writeParam { } {
565 global filename
566 global paramfile paramdir
567 set currdir $paramdir
568 if { [string length $currdir] < 2 } { set currdir [pwd]}
569 if { [string first $currdir ".."] > -1 } { set currdir [pwd]}
570 puts "Current directory: $currdir"
571 createMgParam
572 set thename [file rootname [file tail $filename]]
573 if { [string length $paramfile] < 1 } {
574 set paramfile $thename
575 if { ![string match "*.star" $paramfile] } {
576 append paramfile ".star"
577 }
578 }
579 set paramfile [file tail $paramfile]
580 set types {
581 {"STAR files" {.star .STAR} }
582 {"XML files" {.xml .XML} }
583 {"EMX files" {.emx .EMX} }
584 }
585 set paramfile [tk_getSaveFile -filetypes $types \
586 -initialfile $paramfile -initialdir $currdir -defaultextension .star]
587 if { [string length $paramfile] < 1 } { return}
588 set paramfile [relativePath $paramdir $paramfile]
589 setMgParam
590 wm title . [list $filename ":" $paramfile]
591 # puts "Writing $paramfile"
592 Bmg write $paramfile
593 # puts "Writing .bshow"
594 writeSettingsFile
595 }
596
597 ## @brief Edits a text parameter file.
598 #
599
600 proc editParam { } {
601 global paramfile paramdir
602 editTextFile $paramfile
603 }
604
605 ## @brief Checks if the parameter file exist, otherwise save it.
606 #
607
608 proc checkParamFile { } {
609 global paramfile
610 if { ![file exists $paramfile] } {
611 writeParam
612 }
613 }
614
615