forked from jhenin/parseFEP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseFEP.tcl
More file actions
executable file
·3284 lines (2881 loc) · 144 KB
/
parseFEP.tcl
File metadata and controls
executable file
·3284 lines (2881 loc) · 144 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#######################################################################
# ParseFEP
#######################################################################
# ParseFEP is a tool for analyzing the results of a NAMD FEP run.
# ParseFEP computes: - free-energy differences
# - first-order error estimates
# - Gram-Charlier interpolations
# - simple overlap sampling free-energy differences
# - enthalpy and entropy differences
# ParseFEP displays: - free-energy time series
# - probability distributions
# ParseFEP assumes that Grace and ImageMagick are installed.
# All computations in kcal/mol.
#######################################################################
# Chris Chipot, Liu Peng, 2007-2012
# INCLUSION OF PARSER. MAY 28, 2009, C.C.
# 1.8 2013-12-20
#----------------------------------------------------------------------
# $Id: parseFEP.tcl,v 1.16 2018/03/06 20:06:08 johns Exp $
#----------------------------------------------------------------------
# YingChih Chiang (Letitia), 2017/01/18, 2017/11/20
# Re-structure the source code for performance:
# (1) Reading fepout files are done via calling readfepout.
# (2) Only read the fepout files once and save the data.
# (3) Bugs of reading fepout files in BAR_estimator and
# inaccuracy_estimation have been removed.
# YingChih Chiang (Letitia), 2018/02/01, 2018/02/10
# (1) Introuduce parsing doublewide sampling output file;
# Display (Plot figures) not supported.
# (2) Introduce initvar function to initialize arrays and variables
# for continuous multiple analysis
# (3) Enable .fep as output file in GUI
#######################################################################
package provide parsefep 2.2
namespace eval ::ParseFEP:: {
if { [catch {package require exectool 1.2}] } {
set has_exectool 0
} else {
set has_exectool 1
}
namespace export parsefepgui
namespace export parsefep
variable w
variable version 2.2
variable temperature 300.0
variable gcindex 0
variable max_order ""
variable gaussian_guess 0
variable combine_method 0
variable dispindex 0
variable entropyindex 0
variable interfilesindex 0
variable fepofile ""
variable fepbofile ""
variable fepdwfile ""
variable nb_windows
variable nb_sample
variable nb_equil
variable FEPfreq
variable k 0.001987200
variable delta_a 0.0
# CORRECTION ADDED FEBRUARY 04, 2010, C.C.
variable variance_gauss 0.0
variable error_gauss 0.0
variable variance 0.0
variable error 0.0
variable square_error 0.0
variable square_error_gauss 0.0
# END CORRECTION
variable fep_delta_a_forward 0.0
variable fep_delta_a_backward 0.0
variable fep_delta_u_forward 0.0
variable fep_delta_u_backward 0.0
variable fep_delta_s_forward 0.0
variable fep_delta_s_backward 0.0
variable fwdlog ""
variable bwdlog ""
variable overlap_array ""
variable file_lambda
# BKR - temporary flag to shutoff code when this is not true.
variable uniformSampleSize
# Data (Letitia)
variable jstart
variable fwd_nb_windows
variable fwd_nb_sample
variable fwd_nb_equil
variable fwd_FEPfreq
variable bwd_nb_windows
variable bwd_nb_sample
variable bwd_nb_equil
variable bwd_FEPfreq
# List Data (Letitia)
variable fwd_Lsample
variable fwd_Lsample_energy1
variable fwd_Lsample_energy2
variable fwd_Lnb_sample
variable fwd_Lnb_equil
variable fwd_lambda
variable fwd_lambda2
variable bwd_Lsample
variable bwd_Lsample_energy1
variable bwd_Lsample_energy2
variable bwd_Lnb_sample
variable bwd_Lnb_equil
variable bwd_lambda
variable bwd_lambda2
}
proc ::ParseFEP::parsefep_usage {} {
puts "Usage: parsefep <option1> <option2> ..."
puts "NOTE: parsefep is a tootl for analyzing the results of FEP simulations"
puts " -forward <file name> the name of the fep output file for the forward simulation"
puts " -entropy calculate the enthaply and entropy differences"
puts " -gc <max order> Gram-Charlier interpolations, the max_order should be defined"
puts " -gauss applying Gaussian distribution to model the underlying probability distribution"
puts " -backward <file name> the name of the fep output files for the backward simulation"
puts " -doublewide <file name> the name of the fep output files for the doublewide simulation"
puts " -<sos|bar> applying simple over sampling method to combine the forward and backward simulations"
puts " or applying bennett acceptance ratio method to combine the forward and backward simulations"
}
#################################################
# Intermediate layer for parsefepcmd entrance
#################################################
proc ::ParseFEP::parsefepcmd { args } {
puts "\n--------------------------"
puts "ParseFEP: Version $::ParseFEP::version"
puts "--------------------------"
set narges [llength $args]
if { $narges == 0 } {
parsefep_usage
error ""
}
# Initializing filenames
set ::ParseFEP::fepofile ""
set ::ParseFEP::fepbofile ""
set ::ParseFEP::fepdwfile ""
for {set argnum 0 } { $argnum < [llength $args] } {incr argnum} {
set arg [lindex $args $argnum]
set val [lindex $args [expr {$argnum + 1} ] ]
switch -- $arg {
"-forward" { set ::ParseFEP::fepofile $val ; incr argnum }
"-backward" { set ::ParseFEP::fepbofile $val ; incr argnum }
"-doublewide" { set ::ParseFEP::fepdwfile $val ; incr argnum }
"-entropy" { set ::ParseFEP::entropyindex 1 }
"-gauss" { set ::ParseFEP::gaussian_guess 1 }
"-gc" { set ::ParseFEP::gcindex 1 ; set ::ParseFEP::max_order $val; incr argnum }
"-sos" { set ::ParseFEP::combine_method 1 }
"-bar" { set ::ParseFEP::combine_method 2 }
default { error "unknown arguments: $arg $val" ; parsefep_usage }
}
}
namdparse
}
################################################
# Intermediate layer for parsefepgui entrance
################################################
proc ::ParseFEP::parsefepgui {} {
puts "\n--------------------------"
puts "ParseFEP: Version $::ParseFEP::version"
puts "--------------------------"
# add traces to the checkboxes, so various widgets can be disabled appropriately
if {[llength [trace info variable [namespace current]::fepbofile ]] == 0 || [llength [trace info variable [namespace current]::fepdwfile ]] == 0 } {
trace add variable [namespace current]::fepdwfile write ::ParseFEP::reading_para_sosbar
trace add variable [namespace current]::fepbofile write ::ParseFEP::reading_para_sosbar
}
if {[llength [trace info variable [namespace current]::dispindex ]] == 0 } {
trace add variable [namespace current]::dispindex write ::ParseFEP::reading_para_interfiles
}
#-------------------------
# read parameter section
#-------------------------
variable w
#De-minimize if the window is already running
if { [winfo exists .parseFEP] } {
wm deiconify $w
raise .parseFEP
return
}
set w [toplevel ".parseFEP"]
wm title $w "ParseFEP"
wm resizable $w yes yes
set row 0
#Add a menubar
frame $w.menubar -relief raised -bd 2
grid $w.menubar -padx 1 -column 0 -columnspan 5 -row $row -sticky ew
menubutton $w.menubar.help -text "Help" -underline 0 \
-menu $w.menubar.help.menu
$w.menubar.help config -width 5
pack $w.menubar.help -side right
# help menu
menu $w.menubar.help.menu -tearoff no
$w.menubar.help.menu add command -label "About" \
-command {tk_messageBox -type ok -title "About ParseFEP" \
-message "A tool for parsing the result of FEP output"}
$w.menubar.help.menu add command -label "Usage" \
-command {tk_messageBox -type ok -title "Usage:" \
-message "Useage: \n \
ParseFEP is a tool for analyzing the results of a NAMD FEP run. \n\
ParseFEP computes: \n \
- free-energy differences \n\
- first-order error estimates \n\
- Gram-Charlier interpolations \n \
- simple overlap sampling free-energy differences \n \
- enthalpy and entropy differences \n \
ParseFEP displays: \n\
- free-energy time series \n \
- probability distributions "}
$w.menubar.help.menu add command -label "help..." \
-command "vmd_open_url [string trimright [vmdinfo www] /]/plugins/parsefep"
incr row
##Selection of parameter (Disabled by Letitia)
#grid [label $w.enerlabel -text "Parameters "] -row $row -column 0 -sticky w
#incr row
# Inputs for temperature, Gram-Charlier order, etc.
grid [label $w.templable -text "Temperature: " -anchor w ] \
-row $row -column 0 -sticky w
grid [entry $w.temp -width 20 \
-textvariable [namespace current]::temperature] \
-row $row -column 1 -sticky w
incr row
grid [label $w.gcindex -text "Gram-Charlier order : " -anchor w ] \
-row $row -column 0 -sticky w
grid [entry $w.temp2 -width 5 \
-textvariable [namespace current]::max_order ] \
-row $row -column 1 -sticky w
incr row
grid [checkbutton $w.disp -text "disp (this option is restricted to Unix-like systems)" \
-variable [namespace current]::dispindex ] \
-row $row -column 0 -sticky e
grid [radiobutton $w.dispwithinterfiles -text "Do or" -state disabled \
-variable [namespace current]::interfilesindex -value "1" ] \
-row $row -column 1 -sticky ew
grid [radiobutton $w.dispwithoutinterfiles -text "Don't keep intermediate files (for plotting purposes)" -state disabled \
-variable [namespace current]::interfilesindex -value "0" ] \
-row $row -column 2 -sticky ew
incr row
grid [checkbutton $w.entropy -text "entropy" \
-variable [namespace current]::entropyindex ] \
-row $row -column 0 -sticky w
grid [checkbutton $w.gg -text "Gaussian approximation" \
-variable [namespace current]::gaussian_guess ] \
-row $row -column 1 -sticky w
incr row
#selection for fep output file (forward)
grid [label $w.fepolabel -text "FEP output file "] -row $row -column 0 -sticky w
grid [entry $w.fepofile -width 20 -textvar [namespace current]::fepofile] -row $row -column 1 -columnspan 2 -sticky ew
frame $w.fepobuttons
button $w.fepobuttons.fepobrowse -text "Browse" -command [ namespace code {
set filetypes {
{{NAMD output file} {.fepout .fep}}
{{All Files} {*}}
}
set ::ParseFEP::fepofile [tk_getOpenFile -filetypes $filetypes]
} ]
pack $w.fepobuttons.fepobrowse -side left -fill x
grid $w.fepobuttons -row $row -column 2 -columnspan 1 -sticky nsew
incr row
# selection for fep backward output file
grid [label $w.fepbolabel -text "FEP(backward) output file"] -row $row -column 0 -sticky w
grid [entry $w.fepbofile -width 20 -textvar [namespace current]::fepbofile] -row $row -column 1 -columnspan 2 -sticky ew
frame $w.fepbobuttons
button $w.fepbobuttons.fepbobrowse -text "Browse" -command [ namespace code {
set filetypes {
{{NAMD output file} {.fepout .fep}}
{{All Files} {*}}
}
set ::ParseFEP::fepbofile [tk_getOpenFile -filetypes $filetypes]
} ]
pack $w.fepbobuttons.fepbobrowse -side left -fill x
grid $w.fepbobuttons -row $row -column 2 -columnspan 1 -sticky nsew
incr row
# selection for doublewide fep output file
grid [label $w.fepdwlabel -text "FEP(doublewide) output file"] -row $row -column 0 -sticky w
grid [entry $w.fepdwfile -width 20 -textvar [namespace current]::fepdwfile] -row $row -column 1 -columnspan 2 -sticky ew
frame $w.fepdwbuttons
button $w.fepdwbuttons.fepdwbrowse -text "Browse" -command [ namespace code {
set filetypes {
{{NAMD output file} {.fepout .fep}}
{{All Files} {*}}
}
set ::ParseFEP::fepdwfile [tk_getOpenFile -filetypes $filetypes]
} ]
pack $w.fepdwbuttons.fepdwbrowse -side left -fill x
grid $w.fepdwbuttons -row $row -column 2 -columnspan 1 -sticky nsew
incr row
# selection for SOS or BAR
grid [label $w.textfor_overlap -text "Combine forward and backward sampling: " ] -row $row -column 0 -sticky w
incr row
grid [radiobutton $w.sosindex -text "SOS-estimator" -state disabled -variable ::ParseFEP::combine_method -value "1" ] -row $row -column 0 -sticky w
grid [radiobutton $w.barindex -text "BAR-estimator" -state disabled -variable ::ParseFEP::combine_method -value "2" ] -row $row -column 1 -sticky w
incr row
#-------------------
# run job section
#-------------------
grid [button $w.runbutton -text "Run FEP parsing" \
-command [ namespace code {
if { [string length $::ParseFEP::fepofile] < 1 && [string length $::ParseFEP::fepdwfile] < 1} {
tk_dialog .errmsg {NamdPlot Error} "No FEP (inward or doublewide) logfile selected." error 0 Dismiss
return
}
if { $::ParseFEP::max_order != "" } {
set ::ParseFEP::gcindex 1 ;# run the gram-chalier expansion
puts "ParseFEP: Gram-Charlier expansion up to order $::ParseFEP::max_order "
} else {
set ::ParseFEP::gcindex 0
puts "ParseFEP: No valid expansion order provided. Therefore, Gram-Charlier analysis of convergence will not be performed. "
}
namdparse
} ]
] -row $row -column 0 -columnspan 5 -sticky nsew
incr row
}
# small routine for parsefepgui
proc ::ParseFEP::reading_para_sosbar {args} {
variable w
if { $::ParseFEP::fepbofile == "" && $::ParseFEP::fepdwfile == "" } {
$w.barindex configure -state disabled
$w.sosindex configure -state disabled
} else {
$w.barindex configure -state normal
$w.sosindex configure -state normal
}
}
# small routine for parsefepgui
proc ::ParseFEP::reading_para_interfiles {args} {
variable w
if { $::ParseFEP::dispindex == 0 } {
$w.dispwithinterfiles configure -state disabled
$w.dispwithoutinterfiles configure -state disabled
} else {
$w.dispwithinterfiles configure -state normal
$w.dispwithoutinterfiles configure -state normal
}
}
##################################################################
# Routine for reading traditional NAMD FEP output file (Letitia)
##################################################################
proc readfepout {fepout Label} {
set isForward [string match -nocase $Label "Forward"]
set isBackward [string match -nocase $Label "Backward"]
# Initialization
set lambda [list] ;# list of lambda values in fepout
set lambda2 [list] ;# list of comparison values in fepout
set Lsample [list] ;# list of energy differences in fepout
set Lsample_tmp [list]
set Lsample_energy1 [list] ;# list of ELECT1+VDW1 energies
set Lsample_energy1_tmp [list]
set Lsample_energy2 [list] ;# list of ELECT2+VDW2 energies
set Lsample_energy2_tmp [list]
set Lnb_equil [list] ;# list of no. of equilibration steps
set Lnb_sample [list] ;# list of no. of samples
# Read NAMD FEP output file
set fid [open $fepout "r"]
set windex -1
set flag_rdequil 1
while {![eof $fid]} {
gets $fid data
if {[regexp "^#NEW FEP WINDOW" $data]} { ;# This is a new FEP window.
incr windex
# TODO: IDWS entries will have more than 9 tokens.
if {[llength $data] < 9} {
error "The file $fepout has incorrect format in NEW FEP WINDOW line."
}
lappend lambda [expr {1.*[lindex $data 6]}]
lappend lambda2 [expr {1.*[lindex $data 8]}]
if {$windex > 0} {
# Save the sample/equilibration info from the previous window.
if {$flag_rdequil} { ;# This happens if alchEquilSteps = 0.
set nb_equil 0
}
lappend Lnb_equil $nb_equil
lappend Lnb_sample $nb_sample
set Lsample [concat $Lsample $Lsample_tmp]
set Lsample_energy1 [concat $Lsample_energy1 $Lsample_energy1_tmp]
set Lsample_energy2 [concat $Lsample_energy2 $Lsample_energy2_tmp]
}
# Reset the flags and counters.
set flag_rdequil 1
set nb_equil 0
set nb_sample 0
set Lsample_tmp [list]
set Lsample_energy1_tmp [list]
set Lsample_energy2_tmp [list]
continue
}
# Older versions of the code assumed equilibration occurs, which is NOT
# required by NAMD. We now just start reading data and start over if we
# find the flag for the end of equilibration. The tmp buffers are then
# concatenated onto the aggregate sample similar to the sample sizes.
#
# TODO: IDWS also includes the tag "FepE_back"
if {[regexp "^FepEnergy" $data]} {
# There's no reason to read TS or alchEnsembleAvg fields.
lassign [lrange $data 2 end] ELEC1 ELEC2 VDW1 VDW2 dE
lappend Lsample_tmp $dE
lappend Lsample_energy1_tmp [expr {$ELEC1 + $VDW1}]
lappend Lsample_energy2_tmp [expr {$ELEC2 + $VDW2}]
incr nb_equil $flag_rdequil
incr nb_sample
}
if {[regexp "^#STARTING COLLECTION" $data]} { ;# end of equilibration
# Reset sample collection and stop recording equilibration.
set flag_rdequil 0 ;# stop counting equilibratin
set nb_sample 0 ;# reset the counter to ignore equilibration
set Lsample_tmp [list]
set Lsample_energy1_tmp [list]
set Lsample_energy2_tmp [list]
}
}
close $fid
# Save the sample/equilibration info from the last window.
if {$flag_rdequil} { ;# This happens if alchEquilSteps = 0.
set nb_equil 0
}
lappend Lnb_equil $nb_equil
lappend Lnb_sample $nb_sample
set Lsample [concat $Lsample $Lsample_tmp]
set Lsample_energy1 [concat $Lsample_energy1 $Lsample_energy1_tmp]
set Lsample_energy2 [concat $Lsample_energy2 $Lsample_energy2_tmp]
# Sanity check that data was actually read correctly.
set nb_windows [expr {$windex + 1}]
if {$nb_windows != [llength $Lnb_equil] ||
$nb_windows != [llength $Lnb_sample]} {
puts "$nb_windows [llength $Lnb_equil] [llength $Lnb_sample]"
error "Something went wrong counting the data!"
}
# BKR - nobody really knows why this is checked and stored...
# My guess is that it used to be a hack correction to inclusive
# indexing when using lrange when exclusive indexing was expected.
#
set ::ParseFEP::jstart [expr {$nb_equil == 0 ? 0 : 1}]
foreach nb_sample $Lnb_sample {
if {$nb_sample < 1} {
error "No sample data in fepout! Check your input files again!"
}
}
# Sanity check that lambda values are in proper direction.
set firstLambda [lindex $lambda 0]
set lastLambda [lindex $lambda end]
if {$isForward && $firstLambda > $lastLambda} {
error "Error when reading forward fep/fepout: $fepout is NOT a Forward file. Program stops!"
} elseif {$isBackward && $firstLambda < $lastLambda} {
error "Error when reading backward fep/fepout: $fepout is NOT a Backward file. Program stops!"
}
# Evaluate FEPfreq
# BKR - there has to be a better way to do this...
set FEPfreq 0
set fid [open $fepout "r"]
while {![eof $fid]} {
gets $fid data
if {[regexp "^FepEnergy" $data]} {
set t1 [lindex $data 1]
while {![eof $fid]} {
gets $fid data
if {[regexp "^FepEnergy" $data]} {
set t2 [lindex $data 1]
break
}
}
set FEPfreq [expr {$t2-$t1}]
break
}
}
close $fid
if {$isForward} {
set ::ParseFEP::fwd_nb_equil [lindex $Lnb_equil end]
set ::ParseFEP::fwd_nb_sample [lindex $Lnb_sample end]
set ::ParseFEP::fwd_nb_windows $nb_windows
set ::ParseFEP::fwd_FEPfreq $FEPfreq
set ::ParseFEP::fwd_lambda $lambda
set ::ParseFEP::fwd_lambda2 $lambda2
set ::ParseFEP::fwd_Lsample $Lsample
set ::ParseFEP::fwd_Lsample_energy1 $Lsample_energy1
set ::ParseFEP::fwd_Lsample_energy2 $Lsample_energy2
set ::ParseFEP::fwd_Lnb_sample $Lnb_sample
set ::ParseFEP::fwd_Lnb_equil $Lnb_equil
} elseif {$isBackward} {
# set ::ParseFEP::bwd_nb_equil $nb_equil ;# This is never used anywhere
# set ::ParseFEP::bwd_nb_sample $nb_sample ;# This is never used anywhere
set ::ParseFEP::bwd_nb_windows $nb_windows
set ::ParseFEP::bwd_FEPfreq $FEPfreq
set ::ParseFEP::bwd_lambda $lambda
set ::ParseFEP::bwd_lambda2 $lambda2
set ::ParseFEP::bwd_Lsample $Lsample
set ::ParseFEP::bwd_Lsample_energy1 $Lsample_energy1
set ::ParseFEP::bwd_Lsample_energy2 $Lsample_energy2
set ::ParseFEP::bwd_Lnb_sample $Lnb_sample
set ::ParseFEP::bwd_Lnb_equil $Lnb_equil
} else {
puts " "
puts "Error when parsing" $Label "file. Program stops!"
puts " "
exit
}
unset lambda
unset lambda2
unset Lnb_equil
unset Lnb_sample
unset Lsample
unset Lsample_tmp
unset Lsample_energy1
unset Lsample_energy1_tmp
unset Lsample_energy2
unset Lsample_energy2_tmp
}
# Getter function to cleanup extraneous variables (BKR)
#
# This also avoids previous issues where temperature was not updated.
#
proc ::ParseFEP::kT {} {
return [expr {$::ParseFEP::k*$::ParseFEP::temperature}]
}
#########################################################
# Getter functions to avoid redundant indexing code (BKR)
#########################################################
# Retrieve the energy differences for the given window (retrieved by index).
# Different conventions are used for two-sided versus one-sided methods and
# this is toggled by a flag.
#
proc ::ParseFEP::getFwdData {windex {useOneSidedConvention 0}} {
variable ::ParseFEP::fwd_Lsample
variable ::ParseFEP::fwd_Lsample_energy1
variable ::ParseFEP::fwd_Lsample_energy2
variable ::ParseFEP::fwd_Lnb_sample
variable ::ParseFEP::fwd_Lnb_equil
variable ::ParseFEP::jstart
set mystart 0
foreach nb_sample [lrange $fwd_Lnb_sample 0 [expr {$windex - 1}]] {
incr mystart $nb_sample
}
set nb_sample [lindex $fwd_Lnb_sample $windex]
set myend [expr {$mystart + $nb_sample - 1}]
if {$useOneSidedConvention} {
# The purpose of jstart is unclear, but retained for legacy reasons. It was
# apparently only ever used in the one-sided FEP pathway.
#
incr mystart $jstart
}
set data_forward [lrange $fwd_Lsample $mystart $myend]
set data_forward_energy1 [lrange $fwd_Lsample_energy1 $mystart $myend]
set data_forward_energy2 [lrange $fwd_Lsample_energy2 $mystart $myend]
return [list $data_forward $data_forward_energy1 $data_forward_energy2]
}
proc ::ParseFEP::getBwdData {windex {useOneSidedConvention 0}} {
variable ::ParseFEP::bwd_Lsample
variable ::ParseFEP::bwd_Lsample_energy1
variable ::ParseFEP::bwd_Lsample_energy2
variable ::ParseFEP::bwd_Lnb_sample
variable ::ParseFEP::bwd_Lnb_equil
variable ::ParseFEP::jstart
variable ::ParseFEP::fwd_lambda
variable ::ParseFEP::bwd_lambda2
if {$useOneSidedConvention} {
set bwd_windex $windex
} else {
# Convert windex to the backward index with the matching lambda.
set bwd_windex 0
set lambda [lindex $fwd_lambda $windex]
foreach lambda2 $bwd_lambda2 {
if {$lambda2 == $lambda} {
break
}
incr bwd_windex
}
}
set mystart 0
foreach nb_sample [lrange $bwd_Lnb_sample 0 [expr {$bwd_windex - 1}]] {
incr mystart $nb_sample
}
set nb_sample [lindex $bwd_Lnb_sample $bwd_windex]
set myend [expr {$mystart + $nb_sample - 1}]
if {$useOneSidedConvention} {
# The purpose of jstart is unclear, but retained for legacy reasons. It was
# apparently only ever used in the one-sided FEP pathway.
#
incr mystart $jstart
}
if {!$useOneSidedConvention} {
# dE is always computed going towards alchLambda2. By convention, this is
# the wrong sign for two-sided methods like BAR, so these are flipped here.
#
# TODO? It's not clear that this is checked in the input...
#
set data_backward [list]
set data_backward_energy1 [list]
set data_backward_energy2 [list]
foreach item [lrange $bwd_Lsample $mystart $myend]\
item1 [lrange $bwd_Lsample_energy1 $mystart $myend]\
item2 [lrange $bwd_Lsample_energy2 $mystart $myend] {
lappend data_backward [expr {-1.0*$item}]
lappend data_backward_energy1 [expr {-1.0*$item1}]
lappend data_backward_energy2 [expr {-1.0*$item2}]
}
} else {
set data_backward [lrange $bwd_Lsample $mystart $myend]
set data_backward_energy1 [lrange $bwd_Lsample_energy1 $mystart $myend]
set data_backward_energy2 [lrange $bwd_Lsample_energy2 $mystart $myend]
}
return [list $data_backward $data_backward_energy1 $data_backward_energy2]
}
##################################################################
# Routine for reading doublewide NAMD FEP output file (Letitia)
##################################################################
proc ::ParseFEP::readdoublewide {fepout} {
# Initialization
set previous_window 0
set windex -1
set flag_rdequil 0
set flag_rdsample 0
set nb_equil 0
set nb_sample 0
set nb_windows 0
set FEPfreq 0
set lambda ""
set lambda2 ""
set lambda3 ""
set fwd_Lsample {}
set fwd_Lsample_energy1 {}
set fwd_Lsample_energy2 {}
set bwd_Lsample {}
set bwd_Lsample_energy1 {}
set bwd_Lsample_energy2 {}
# First read all doublewide fepout file
set fid [open $fepout "r"]
while {![eof $fid]} {
gets $fid data
# prepare to go through equilibration data
if {[regexp "^#NEW FEP WINDOW" $data]} {
if {[llength $data] != 11} { error "The file $fepout is not in the double-wide format. Program stops!" }
if { $previous_window == 1 } {
set flag_rdsample 0
set nb_sample [expr {$samindex+$nb_equil-1}]; # ParseFEP counts nb_equil in nb_sample but do not use the equilibration data for analysis.
} else {
set previous_window 1
}
incr windex 1
lappend lambda [lindex $data 6]
lappend lambda2 [lindex $data 8]
lappend lambda3 [lindex $data 10]
set flag_rdequil 1
set equindex 0
continue
}
# go through equilibration data
if {[expr $flag_rdequil]} {
if {[regexp "^FepEnergy" $data]} {
incr equindex 1
} else {
set flag_rdequil 0
set nb_equil [expr {$equindex+1}]; # ParseFEP counts the last frame in nb_equil.
}
}
# prepare to read sampling data
if {[regexp "^#STARTING COLLECTION" $data]} {
set flag_rdsample 1
set samindex 0
continue
}
# read sampling data
if {[expr $flag_rdsample]} {
if {[regexp "^FepEnergy" $data]} {
foreach {a b c d e f g h i j k l m n o} $data {break}
lappend fwd_Lsample $i
lappend fwd_Lsample_energy1 [expr {$c+$f}]
lappend fwd_Lsample_energy2 [expr {$d+$g}]
lappend bwd_Lsample $j
lappend bwd_Lsample_energy1 [expr {$c+$f}]
lappend bwd_Lsample_energy2 [expr {$e+$h}]
incr samindex 1
}
}
}
close $fid
# If sample array size = 0, return with error
if {[llength $fwd_Lsample]==0} {error "No sample data in fepout! Check your input files again!"}
# If number of windows does not much, return with error
set dlambda [expr {abs([lindex $lambda2 0]-[lindex $lambda 0])}]
if {[expr {$windex+1}] > [expr {int(1.0/$dlambda)+1}]} {
puts "We expect to have [expr {int(1.0/$dlambda)+1}] windows."
puts "We got [expr {$windex+1}] windows."
error "We have duplicate windows in doublewide fepout file! Program stops!"
} elseif {[expr {$windex+1}] < [expr {int(1.0/$dlambda)+1}]} {
puts "We expect to have [expr {int(1.0/$dlambda)+1}] windows."
puts "We got [expr {$windex+1}] windows."
error "You have windows missing in doublewide fepout file! Program stops!"
}
# Then evaluate FEPfreq
set fid [open $fepout "r"]
while {![eof $fid]} {
gets $fid data
if {[regexp "^FepEnergy" $data]} {
set t1 [lindex $data 1]
while {![eof $fid]} {
gets $fid data
if {[regexp "^FepEnergy" $data]} {
set t2 [lindex $data 1]
break
}
}
set FEPfreq [expr {$t2-$t1}]
break
}
}
close $fid
# Now parse the data to the traditional forward/backward FEP format
set nwindows [expr {$windex + 1 - 1}]
set first_window [lsearch -all $lambda 0]
set last_window [lsearch -all $lambda 1]
set dlambda [expr {1.0/$nwindows}]
if {[llength $first_window]!=1 || [llength $last_window]!=1} {
puts " "
puts "Cannot find lambda 0 or 1: Your doublewide fepout file is not complete."
puts "Program stops."
puts " "
exit
}
if {$last_window > $first_window} {
# ascending order: e.g. 0, 0.1, ..., 1
set ::ParseFEP::fwd_lambda [lrange $lambda 0 end-1]
set ::ParseFEP::fwd_lambda2 [lrange $lambda2 0 end-1]
set totalsize [llength $fwd_Lsample]
set blocksize [expr {$totalsize/($nwindows+1)}]
set endblock [expr {$blocksize*$nwindows-1}]
set ::ParseFEP::fwd_Lsample [lrange $fwd_Lsample 0 $endblock]
set ::ParseFEP::fwd_Lsample_energy1 [lrange $fwd_Lsample_energy1 0 $endblock]
set ::ParseFEP::fwd_Lsample_energy2 [lrange $fwd_Lsample_energy2 0 $endblock]
# reverse backward data
set ::ParseFEP::bwd_lambda [lreverse [lrange $lambda 1 end]]
set ::ParseFEP::bwd_lambda2 [lreverse [lrange $lambda3 1 end]]
for {set i $nwindows} {$i > 0} {incr i -1} {
set initblock [expr {$i*$blocksize}]
set endblock [expr {$initblock+$blocksize-1}]
foreach item [lrange $bwd_Lsample $initblock $endblock] {
lappend ::ParseFEP::bwd_Lsample $item
}
foreach item [lrange $bwd_Lsample_energy1 $initblock $endblock] {
lappend ::ParseFEP::bwd_Lsample_energy1 $item
}
foreach item [lrange $bwd_Lsample_energy2 $initblock $endblock] {
lappend ::ParseFEP::bwd_Lsample_energy2 $item
}
}
} else {
# descending order: e.g. 1, 0.9, ..., 0
set ::ParseFEP::bwd_lambda [lrange $lambda 0 end-1]
set ::ParseFEP::bwd_lambda2 [lrange $lambda2 0 end-1]
set totalsize [llength $bwd_Lsample]
set blocksize [expr {$totalsize/($nwindows+1)}]
set endblock [expr {$blocksize*$nwindows-1}]
set ::ParseFEP::bwd_Lsample [lrange $fwd_Lsample 0 $endblock]
set ::ParseFEP::bwd_Lsample_energy1 [lrange $fwd_Lsample_energy1 0 $endblock]
set ::ParseFEP::bwd_Lsample_energy2 [lrange $fwd_Lsample_energy2 0 $endblock]
# reverse forward data
set ::ParseFEP::fwd_lambda [lreverse [lrange $lambda 1 end]]
set ::ParseFEP::fwd_lambda2 [lreverse [lrange $lambda3 1 end]]
for {set i $nwindows} {$i > 0} {incr i -1} {
set initblock [expr {$i*$blocksize}]
set endblock [expr {$initblock+$blocksize-1}]
foreach item [lrange $bwd_Lsample $initblock $endblock] {
lappend ::ParseFEP::fwd_Lsample $item
}
foreach item [lrange $bwd_Lsample_energy1 $initblock $endblock] {
lappend ::ParseFEP::fwd_Lsample_energy1 $item
}
foreach item [lrange $bwd_Lsample_energy2 $initblock $endblock] {
lappend ::ParseFEP::fwd_Lsample_energy2 $item
}
}
}
## debug
#set fd [open "debug.dat" w]
#for {set i 0} {$i < $nwindows} {incr i} {
# set initblock [expr {$i*$blocksize}]
# puts $fd "[lindex $::ParseFEP::fwd_lambda $i] [lindex $::ParseFEP::fwd_lambda2 $i] [lindex $::ParseFEP::fwd_Lsample $initblock]"
#}
#for {set i 0} {$i < $nwindows} {incr i} {
# set initblock [expr {$i*$blocksize}]
# puts $fd "[lindex $::ParseFEP::bwd_lambda $i] [lindex $::ParseFEP::bwd_lambda2 $i] [lindex $::ParseFEP::bwd_Lsample $initblock]"
#}
#close $fd
# Assign the rest of the data
if {[expr $nb_equil]==1} {set ::ParseFEP::jstart 0} else {set ::ParseFEP::jstart 1}
set ::ParseFEP::fwd_nb_equil $nb_equil
set ::ParseFEP::fwd_nb_sample $nb_sample
set ::ParseFEP::fwd_nb_windows $nwindows
set ::ParseFEP::fwd_FEPfreq $FEPfreq
set ::ParseFEP::bwd_nb_equil $nb_equil
set ::ParseFEP::bwd_nb_sample $nb_sample
set ::ParseFEP::bwd_nb_windows $nwindows
set ::ParseFEP::bwd_FEPfreq $FEPfreq
unset lambda
unset lambda2
unset lambda3
unset fwd_Lsample
unset fwd_Lsample_energy1
unset fwd_Lsample_energy2
unset bwd_Lsample
unset bwd_Lsample_energy1
unset bwd_Lsample_energy2
}
# combine the file of entropy.log, gc.log and ParseFEP.log ( if fileutil is accessible, this part can be replaced by it. )
proc ::ParseFEP::combine_file { } {
set target_file [open ParseFEP.log "a+"]
if { $::ParseFEP::entropyindex == 1} {
set source_file [open entropy.log "r"]
while { [gets $source_file line] >= 0 } { puts $target_file "$line" }
close $source_file
file delete entropy.log
puts $target_file " "
}
if {$::ParseFEP::gcindex == 1 } {
set source_file [open gc.log "r"]
while { [gets $source_file line] >= 0 } { puts $target_file "$line" }
close $source_file
file delete gc.log
puts $target_file " "
}
close $target_file
}
###################################################################
# Initializing variables and arrays
# called by namdparse: must be done within namdparse to allow
# continuous multiple executions
###################################################################
proc ::ParseFEP::initvar {} {
set ::ParseFEP::variance_gauss 0.0
set ::ParseFEP::error_gauss 0.0
set ::ParseFEP::variance 0.0
set ::ParseFEP::error 0.0
set ::ParseFEP::square_error 0.0
set ::ParseFEP::square_error_gauss 0.0
set ::ParseFEP::fep_delta_a_forward 0.0
set ::ParseFEP::fep_delta_a_backward 0.0
set ::ParseFEP::fep_delta_u_forward 0.0
set ::ParseFEP::fep_delta_u_backward 0.0
set ::ParseFEP::fep_delta_s_forward 0.0
set ::ParseFEP::fep_delta_s_backward 0.0
set ::ParseFEP::fwdlog {}
set ::ParseFEP::bwdlog {}
set ::ParseFEP::overlap_array {}
set ::ParseFEP::file_lambda {}
set ::ParseFEP::fwd_Lsample {}
set ::ParseFEP::fwd_Lsample_energy1 {}
set ::ParseFEP::fwd_Lsample_energy2 {}
set ::ParseFEP::fwd_lambda {}
set ::ParseFEP::fwd_lambda2 {}
set ::ParseFEP::bwd_Lsample {}
set ::ParseFEP::bwd_Lsample_energy1 {}
set ::ParseFEP::bwd_Lsample_energy2 {}
set ::ParseFEP::bwd_lambda {}
set ::ParseFEP::bwd_lambda2 {}
}
###################################################################
# Core of ParseFEP
# called by both parsefepcmd and parsefepgui
# call other important routines
# read files only once and put data into arrays (Letitia)
###################################################################
proc ::ParseFEP::namdparse { } {
# Initialize arrys [for continuous multiple executions]
::ParseFEP::initvar
# Read NAMD FEP output file
if { $::ParseFEP::fepdwfile != "" } {
readdoublewide $::ParseFEP::fepdwfile
} else {
readfepout $::ParseFEP::fepofile "Forward"
if { $::ParseFEP::fepbofile != "" } {readfepout $::ParseFEP::fepbofile "Backward"}
}
# BKR - If needed, permit output for variable sample size. For sanity, still
# use the old sample size convention, which adds 1 to the equilibration
# size. Note also that nb_sample only includes nb_equil when it is reported.
# Elsewhere, it specifically means the length of the appropriate segment in
# Lsample.
#
variable ::ParseFEP::uniformSampleSize
variable ::ParseFEP::fwd_Lnb_equil
variable ::ParseFEP::fwd_Lnb_sample
variable ::ParseFEP::bwd_Lnb_equil
variable ::ParseFEP::bwd_Lnb_sample
set nb_equil [lindex $fwd_Lnb_equil end]
set nb_sample [lindex $fwd_Lnb_sample end]
set uniformSampleSize 1
foreach fwd_nb_equil [lrange $fwd_Lnb_equil 0 end-1]\
fwd_nb_sample [lrange $fwd_Lnb_sample 0 end-1]\
bwd_nb_equil [lrange $bwd_Lnb_equil 0 end-1]\
bwd_nb_sample [lrange $bwd_Lnb_sample 0 end-1] {
if {$fwd_nb_equil != $nb_equil || $bwd_nb_equil != $nb_equil ||
$fwd_nb_sample != $nb_sample || $bwd_nb_sample != $nb_sample} {
set uniformSampleSize 0
break
}
}
set nb_equil [expr {[lindex $fwd_Lnb_equil end] + 1}]
set nb_sample [expr {[lindex $fwd_Lnb_sample end] + $nb_equil - 1}]
# TODO: Remove this and its dependencies in the GUI.
set ::ParseFEP::nb_windows $::ParseFEP::fwd_nb_windows
set ::ParseFEP::FEPfreq $::ParseFEP::fwd_FEPfreq
set ::ParseFEP::nb_sample $nb_sample
set ::ParseFEP::nb_equil $nb_equil
puts "ParseFEP: Get the number of samples per window"
puts "ParseFEP: Split time-series in $::ParseFEP::fwd_nb_windows files"
puts "ParseFEP: $::ParseFEP::fwd_FEPfreq steps between stored samples"
if {$uniformSampleSize} {
puts "ParseFEP: $nb_sample effective samples per windows"
puts "ParseFEP: $nb_equil effective equilibration steps per window"
} else {
puts "ParseFEP: effective samples/equilibration steps per window:"
puts "ParseFEP: [format "%17s %17s" "forward" "backward"]"
puts "ParseFEP: [format "%8s %8s %8s %8s" "equil." "sample" "equil." "sample"]"
foreach fwd_nb_equil $fwd_Lnb_equil fwd_nb_sample $fwd_Lnb_sample \
bwd_nb_equil $bwd_Lnb_equil bwd_nb_sample $bwd_Lnb_sample {
incr fwd_nb_sample $fwd_nb_equil
incr bwd_nb_sample $bwd_nb_equil
incr fwd_nb_equil
incr bwd_nb_equil
puts "ParseFEP: [format "%8d %8d %8d %8d" $fwd_nb_equil $fwd_nb_sample $bwd_nb_equil $bwd_nb_sample]"
}
}
puts "ParseFEP: Parse time-series"
# Normal analysis
if { $::ParseFEP::fepdwfile != "" } {
::ParseFEP::normal_parse_log $::ParseFEP::fepdwfile forward
::ParseFEP::normal_parse_log $::ParseFEP::fepdwfile backward
} else {
::ParseFEP::normal_parse_log $::ParseFEP::fepofile forward
if { $::ParseFEP::fepbofile != "" } { ::ParseFEP::normal_parse_log $::ParseFEP::fepbofile backward }
}