From: Justin Gullingsrud (jgulling_at_mccammon.ucsd.edu)
Date: Thu Apr 01 2004 - 11:17:30 CST

Hi,

Use
   set coord1 [lindex [$sel1 get {x y z}] 0]

and similarly for the others. This is a very common point of confusion
which arises from the fact that $sel1 is returning the coordinates
within a nested list, even if there is only one element in that list.
For example:

   set oneAtom [atomselect top "index 1"]
   set twoAtoms [atomselect top "index 1 2"]
   set oneCoord [$oneAtom get {x y z}] ;# returns something like
{{1 2 3}}
   set twoCoords [$twoAtoms get {x y z}] ;# returns something like {{1
2 3} {4 5 6}}
   set p1 [lindex $oneCoord 0]
   set q1 [lindex $twoCoords 0
   set q2 [lindex $twoCoords 1]

So the rule is: when you get multiple atom attributes (in this case, x,
y, and z), the result is always returned as a nested list, even if that
list contains only one element.

On the other hand, if you request only a single atom attribute, such as
x, then things work as you might expect:

   set oneX [$oneAtom get x] ;# returns something like 1
   set twoX [$twoAtoms get x] ;# returns something like {1 4}

See the difference? Seems broken, right? Well, it can actually be
helpful to have things work this way in situations where you don't know
how many atoms are in your atom selection. Say you want to draw a
sphere at every point in a selection. The following code works when
the selection contains 0, 1, or more atoms:

   set sel [atomselect top "within 3 of protein"]
   foreach pos [$sel get {x y z}] {
     draw sphere $pos
   }

If $sel returned a regular, non-nested vector when there was only one
atom in the selection, the draw command would fail because foreach
would break up the position vector into x, y, and z, and pass them one
at a time to the draw command.

So that's the (perhaps ugly) truth of it all.

Cheers,
Justin

On Apr 1, 2004, at 7:30 AM, Vlad Cojocaru wrote:

> Dear vmd users
> I am trying to draw a triangle between 3 atoms with the script
> shown below. I get the following message all the time:
> "need three numbers for a vector"
> I am wondering what is this because all the $coord1 2 and 3 are
> defined properly. If I use the numbers instead of the variables
> $coord1 2 and 3 everything works fine.
> Thanks a lot in advance
> Best wishes
> vlad
>
> set sel1 [atomselect 0 "name P and resid 16" frame 0]
> set sel2 [atomselect 0 "name P and resid 5" frame 0]
> set sel3 [atomselect 0 "name P and resid 9" frame 0]
>
> set coord1 [$sel1 get {x y z}]
> set coord2 [$sel2 get {x y z}]
> set coord3 [$sel3 get {x y z}]
>
> draw triangle $coord1 $coord2 $coord3
>
>
>
>
> --
> Vlad Cojocaru Max Planck Institute for Biophysical Chemistry
> Department: 060 Am Fassberg 11, 37077 Goettingen, Germany tel:
> ++49-551-201.1327 e-mail: Vlad.Cojocaru_at_mpi-bpc.mpg.de
> home tel: ++49-551-9963204
>
>

--
It is easier to make a theory of everything, than a theory of 
something. -- Aharon Katchalsky