From: Thomas Evangelidis (te8624_at_mbg.duth.gr)
Date: Fri Feb 27 2009 - 16:19:03 CST

Hi everyone,

I tried to integrate bigdcd into a namespace and seems that I made it
but for some strange reason the procedure assigned to $bigdcd_proc is
executed whenever I load a different pdb file. In this case
$bigdcd_proc is a simple proc that write the current frame to a pdb
file. Moreover none of the molecules is visible. The only variable
I've set to be global is vmd_frame and is being increased whenever a
new file is loaded. I quote the code below and would really appreciate
any advice!

PS: there are a lot of "puts" for debugging...

package provide trunctraj 1.6

namespace eval ::TruncTraj {
     namespace export trunctraj
     variable trajectory_psf
"/home/thomas/Documents/Molecular_Dynamics/truncate_trajectory1.5/DCD_with_cell.psf"
     variable trajectory_dcd
"/home/thomas/Documents/Molecular_Dynamics/truncate_trajectory1.5/sample_DCD_with_cell.dcd"
     variable bigdcd_frame
     variable bigdcd_proc
     variable bigdcd_firstframe
     variable bigdcd_is_done
     variable ch1 [open "bigdcd.log" RDWR]
     puts $::TruncTraj::ch1 "point 0"
}

proc ::TruncTraj::adaptedBigdcd { script args } {
   variable bigdcd_frame
   variable bigdcd_proc
   variable bigdcd_firstframe 0
   global vmd_frame
   variable bigdcd_is_done 0
puts $::TruncTraj::ch1 "point 1"

   set bigdcd_firstframe [molinfo top get numframes]
   set bigdcd_proc $script
   puts $::TruncTraj::ch1 "point 2"
   uplevel #0 trace variable vmd_frame w ::TruncTraj::bigdcd_callback
   puts $::TruncTraj::ch1 "point 3"
   foreach dcd $args {
     animate read dcd $dcd waitfor 0
   }
}

proc ::TruncTraj::bigdcd_callback { name1 name2 op } {
   variable bigdcd_frame
   variable bigdcd_proc
   variable bigdcd_firstframe
   global vmd_frame
  puts $::TruncTraj::ch1 "point 4"
   # If we're out of frames, we're also done
   set thisframe $vmd_frame($name2)
   if { $thisframe < $bigdcd_firstframe } {
     ::TruncTraj::bigdcd_done
     return
   }
  puts $::TruncTraj::ch1 "point 5"
   incr bigdcd_frame
   if { [catch {uplevel #0 $bigdcd_proc $bigdcd_frame} msg] } {
     puts $::TruncTraj::ch1 "point 6"
     puts stderr "bigdcd aborting at frame $bigdcd_frame\n$msg"
     ::TruncTraj::bigdcd_done
     return
   }
   puts $::TruncTraj::ch1 "point 7"
   animate delete beg $thisframe end $thisframe
   return $msg
}

proc ::TruncTraj::bigdcd_done { } {
     variable bigdcd_is_done
     global vmd_frame
     set bigdcd_is_done 1
   puts "bigdcd_done"
   uplevel #0 trace vdelete vmd_frame w ::TruncTraj::bigdcd_callback
   puts $::TruncTraj::ch1 "point 8"
}

proc ::TruncTraj::adaptedBigdcd_wait_till_done {} {
     variable bigdcd_is_done
     global vmd_frame
     puts $::TruncTraj::ch1 "point 9"
   while {! $bigdcd_is_done} {
     display update
     puts $::TruncTraj::ch1 "point 10 bigdcd_is_done = $bigdcd_is_done
bigdcd_frame = $::TruncTraj::bigdcd_frame"
   }
}

proc ::TruncTraj::SaveTruncTraj { frm } {
     puts "frame = $frm"
     animate write pdb
"/home/thomas/Documents/Molecular_Dynamics/truncate_trajectory1.5/wholeframe_$frm.pdb" beg 0 end 0 waitfor all
top
}

proc ::TruncTraj::RunTruncTraj {} {
     variable trajectory_psf
     variable trajectory_dcd
     mol delete all
     mol load psf "$trajectory_psf"
     ::TruncTraj::adaptedBigdcd ::TruncTraj::SaveTruncTraj "$trajectory_dcd"
     ::TruncTraj::adaptedBigdcd_wait_till_done
}

::TruncTraj::RunTruncTraj