1

I am running a code with tk console in VMD (Visual Molecular Dynamics). The first problem I encountered was that the code took up too much of my computer's processing power and caused VMD to shut down. I think this is because I was having it print every result to the file. I really only need to look at the results where the distance is less than 20. Below is the original code that was causing VMD to close:

set seg1 [atomselect top "segname LA0 and name CA"]
set seg2 [atomselect top "segname RA0 and name CA"]

set file [open "Contact_map27.dat" w]

set list1 [$seg1 get index]
set list2 [$seg2 get index]

foreach atom1 $list1 {
    foreach atom2 $list2 {
            set index1 [atomselect top "index $atom1"]
            set index2 [atomselect top "index $atom2"]
            set resid1 [[atomselect top "index $atom1"] get resid]
            set resid2 [[atomselect top "index $atom2"] get resid]
            set resnm1 [[atomselect top "index $atom1"] get resname]
            set resnm2 [[atomselect top "index $atom2"] get resname]
 puts $file "$resnm1 $resid1 $resnm2 $resid2 [veclength [vecsub [measure center $index1] [measure center $index2]]]"
            $index1 delete
            $index2 delete
    }
}

close $file

Below is the modification I made that should only print the values if the distance is less than 20:

set seg1 [atomselect top "segname LA0 and name CA"]
set seg2 [atomselect top "segname RA0 and name CA"]

set file [open "Contact_map27.dat" w]

set list1 [$seg1 get index]
set list2 [$seg2 get index]

foreach atom1 $list1 {
    foreach atom2 $list2 {
            set index1 [atomselect top "index $atom1"]
            set index2 [atomselect top "index $atom2"]
            set resid1 [[atomselect top "index $atom1"] get resid]
            set resid2 [[atomselect top "index $atom2"] get resid]
            set resnm1 [[atomselect top "index $atom1"] get resname]
            set resnm2 [[atomselect top "index $atom2"] get resname]
    set dist [[veclength [vecsub [measure center $index1] [measure center $index2]]]]
 if {$dist < 20} {
puts $file "$resnm1 $resid1 $resnm2 $resid2 $dist"}
else {puts $file " "}
            $index1 delete
            $index2 delete
    }
}

close $file

The error message I get when I run the second code is "invalid command name "26.774817104116487""

If anyone can just give me a second pair of eyes and let me know what is going on I would greatly appreciate it! Thanks in advance!

1 Answers1

1

You had an extra set of brackets around the call to veclength.

You can reduce the amount of CPU used by moving some code out of the inner loop and into the outer loop. Calls that only depend on atom1 don't need to be in the inner loop.

You can also reduce the amount of CPU by using your index variables that you set. The variables aren't used and the 'atomselect' call is repeated. I'm making the assumption that it returns the same index each time.

set seg1 [atomselect top "segname LA0 and name CA"]
set seg2 [atomselect top "segname RA0 and name CA"]

set file [open "Contact_map27.dat" w]

set list1 [$seg1 get index]
set list2 [$seg2 get index]

foreach atom1 $list1 {
    set index1 [atomselect top "index $atom1"]
    set resid1 [$index1 get resid]
    set resnm1 [$index1 get resname]
    foreach atom2 $list2 {
            set index2 [atomselect top "index $atom2"]
            set resid2 [$index2 get resid]
            set resnm2 [$index2 get resname]
    set dist [veclength [vecsub [measure center $index1] [measure center $index2]]]
 if {$dist < 20} {
puts $file "$resnm1 $resid1 $resnm2 $resid2 $dist"}
else {puts $file " "}
            $index2 delete
    }
    $index1 delete
}

close $file
Brad Lanam
  • 5,192
  • 2
  • 19
  • 29