From: fan li (fanliqmul_at_gmail.com)
Date: Thu Aug 16 2018 - 12:18:18 CDT

Sorry for that

I have two molecules sub1 and sub2 surrounding by SOL and the position of
sub2 changes over time. SO I want to select the SOL in the region defined
by following position conditions and rename it.

The $x0 $y0 and $z0 are the center of the sub2, and the $xmin, $xmax ,
$ymin, $ymax and $zmax are the position conditions defined based on center
of sub2 to specify the region ($zmin is defined outside the loop), then I
put the selected SOL in the specified region in $sel_water and change the
its resname. Finally I select all atoms and write it to .gro file.

#########################################
set zmin 30.3

set nf [molinfo top get numframes]
for {set i 0} {$i < $nf} {incr i} {

        #select sub2 and get the center
        set sub2($i) [atomselect top "resname sub2"]
        $sub2($i) frame $i
        $sub2($i) update

        #puts "$sub2 num\t"

        set cen_sub2 [measure center $sub2($i)]

        set x0 [lindex $cen_sub2 0]

        set y0 [lindex $cen_sub2 1]

        set z0 [lindex $cen_sub2 2]

        #get the position conditions based on the center of sub2
        set xmin [expr $x0-10]

        set xmax [expr $x0+10]

        set ymin [expr $y0-10]

        set ymax [expr $y0+10]

        set zmax [expr $z0-10]

        puts "i=$i\t"
        puts "xmin=$xmin xmax=$xmax ymin=$ymin ymax=$ymax zmin=$zmin
zmax=$zmax\t"

        #select SOL atoms within the region defined by the position
conditions
        set sel_water($i) [atomselect top "resname SOL and (x>$xmin and
x<$xmax) and (y>$ymin and y<$ymax) and (z>$zmin and z<$zmax)"]

        $sel_water($i) frame $i
        $sel_water($i) update

        #reset the resname of the selected SOL
        $sel_water($i) set resname den

        puts "num of interested water=$sel_water($i)\t"

       #select all atoms and write it to .gro
        set system($i) [atomselect top "all"]
        $system($i) frame $i
        $system($i) update
        $system($i) writegro "water_interest$i.gro"

        puts "num of all atoms=$system($i)\t"

        $sub2($i) delete
        $sel_water($i) delete
        $system($i) delete

}
####################################################

The above script only works for first few frames, but it gives me wrong
results for other frames (I have checked the position conditions, they are
right for all frames). If there are other questions about what I want to
do and need me to explain, let me know.

Thanks a lot.

Fan

Ashar Malik <asharjm_at_gmail.com> 于2018年8月16日周四 下午5:22写道:

> With the logic you are proceeding in the script, is this correct???
>
> set zmax [expr $z0-10]
>
>
> Still eyeballing your code. Perhaps you can tell us a bit more as in what
> is happening and what do you expect to happen and someone can comment...
>
> On Aug 16, 2018 23:56, "fan li" <fanliqmul_at_gmail.com> wrote:
>
>> Hi everyone
>>
>> The suggestions from Ashar and Alex work fine for the part of code I
>> posed in last email. But I can only get the right results for the first few
>> frames and the results for other frames are not correct when I used their
>> suggestions for my real script. The following is the real script I used.
>>
>> #########################################
>> set zmin 30.3
>>
>> set nf [molinfo top get numframes]
>> for {set i 0} {$i < $nf} {incr i} {
>>
>> #select sub2 and get the center
>> set sub2($i) [atomselect top "resname sub2"]
>> $sub2($i) frame $i
>> $sub2($i) update
>>
>> #puts "$sub2 num\t"
>>
>> set cen_sub2 [measure center $sub2($i)]
>>
>> set x0 [lindex $cen_sub2 0]
>>
>> set y0 [lindex $cen_sub2 1]
>>
>> set z0 [lindex $cen_sub2 2]
>>
>> set xmin [expr $x0-10]
>>
>> set xmax [expr $x0+10]
>>
>> set ymin [expr $y0-10]
>>
>> set ymax [expr $y0+10]
>>
>> set zmax [expr $z0-10]
>>
>>
>> puts "i=$i\t"
>> puts "xmin=$xmin xmax=$xmax ymin=$ymin ymax=$ymax zmin=$zmin
>> zmax=$zmax\t"
>>
>>
>> #select water atoms within the region defined by the position
>> condition
>> set sel_water($i) [atomselect top "resname SOL and (x>$xmin and
>> x<$xmax) and (y>$ymin and y<$ymax) and (z>$zmin and z<$zmax)"]
>>
>> $sel_water($i) frame $i
>> $sel_water($i) update
>>
>> #reset the resname of the selected water
>> $sel_water($i) set resname den
>>
>> puts "num of interested water=$sel_water($i)\t"
>>
>> set system($i) [atomselect top "all"]
>> $system($i) frame $i
>> $system($i) update
>> $system($i) writegro "water_interest$i.gro"
>>
>> puts "num of all atoms=$system($i)\t"
>>
>> $sub2($i) delete
>> $sel_water($i) delete
>> $system($i) delete
>>
>>
>> }
>>
>>
>> #############################################################
>>
>> I have no idea what is going wrong? Could some one point the bug in the
>> script?
>>
>> Fan
>>
>> Giacomo Fiorin <giacomo.fiorin_at_gmail.com> 于2018年8月16日周四 下午3:10写道:
>>
>>> Each VMD atom selection object contains internally vectors that are as
>>> long as the total number of atoms in the molecule, and the memory usage of
>>> these vectors will not change with the selection keyword. I think it'd be
>>> safer to always delete an atom selection as soon as it's not needed.
>>>
>>> Giacomo
>>>
>>>
>>> On Thu, Aug 16, 2018 at 10:00 AM João Ribeiro <jribeiro_at_ks.uiuc.edu>
>>> wrote:
>>>
>>>> Also, if you need to create atom selections inside the loop (e.g. if
>>>> zmin value changes), you should definitely delete the selection before the
>>>> next iteration ("$sel_water($i) delete"), unless you need the atom
>>>> selection for another operation outside the loop, or the selections don't
>>>> take that much memory.
>>>>
>>>> Best
>>>>
>>>> João
>>>>
>>>> On Thu, Aug 16, 2018 at 8:14 AM Axel Kohlmeyer <akohlmey_at_gmail.com>
>>>> wrote:
>>>>
>>>>>
>>>>>
>>>>> On Thu, Aug 16, 2018 at 7:17 AM fan li <fanliqmul_at_gmail.com> wrote:
>>>>>
>>>>>> Hi everyone
>>>>>> I am trying to select atoms with specific region of SOL of each frame
>>>>>> and change the selected atom's resname. I list part of my code below--000000000000c29081057390a130--