From: Michael Kreim (mkreim_at_keychem.de)
Date: Fri Apr 24 2009 - 08:43:13 CDT

Thanks a lot Axel for explaining this to me.

I rewrote my program and I am using a environment variable now.
I also rewrote my sloppy command line parser. If anybody is interested,
here is my code:

from molecule import *
from atomsel import *
import sys
import getopt
import os

def main(argv):
    atomselection = inputpdb = outputpdb = 0
    try:
        opts, args = getopt.getopt(argv, "hs:i:o:", ["help",
"selection=","input=","output="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in("-s", "--selection"):
            atomselection = arg
        elif opt in("-i", "--input"):
            inputpdb = arg
        elif opt in("-o", "--output"):
            outputpdb = arg

    atomselection = os.getenv("MYVMDASEL",atomselection)

    if atomselection == 0 or inputpdb == 0 or outputpdb == 0:
        usage()
        sys.exit(2)

    print "Using following configuration:"
    print "atomselection:",atomselection
    print "inputpdb: ",inputpdb
    print "outputpdb: ",outputpdb

if __name__ == "__main__":
    main(sys.argv[1:])
    sys.exit()

To use it you have to provide a function usage() and of course something
useful that you do with the variables atomselection, inputpdb, outputpdb.
Most of the getopt parser is adapted from:
http://www.faqs.org/docs/diveintopython/kgp_commandline.html

And if anybody has any remarks how to writter better, nicer or more
beautiful python code please let me hear.

With best regards,

Michael Kreim

Axel Kohlmeyer wrote:
> On Wed, 2009-04-22 at 10:50 +0200, Michael Kreim wrote:
>> Dear VMD users,
>>
>> I observed a strange behaviour in VMD/python using command line arguments.
>> My program expects two file names and one atomselection string as
>> parameters. Everything goes well as long as my atomselection does not
>> contain any spaces. This leads to an error message I don't understand.
>
> you cannot use arguments with quoted spaces. this is a deficiency
> of the c-shell script language that is used in the vmd launch script.
> before we start the usual flame war about why c-shell scripting is
> bad for your health (it just is!), i would like point out that you
> have two options to work around this: 1) use a bourne shell launch
> script. the latest vmd alpha/beta versions have it and it is used
> whenever there is no /bin/csh available (getting more common these
> days). 2) use environment variables. env VMDMYSELECTIONTEXT="xxx with
> blanks" vmd -dispdev text ... and so on will work.
>
> due to the fact that the quoted string is broken into words, you have
> an odd number of arguments, your python script code seems to be failing
> on one of the sys.argv[i+1]. the whole loop code looks a bit sloppy
> to me anyways... ;)
>
>> To make it short. My script:
>> # vmd -dispdev text -python -e test.vmdpy -args -i input.pdb -o
>> output.pdb -s "all"
>> import sys
>>
>> for i in range(len(sys.argv)):
>> if sys.argv[i]=="-s":
>> atomselection=sys.argv[i+1]
>> elif sys.argv[i]=="-i":
>> inputpdb=sys.argv[i+1]
>> elif sys.argv[i]=="-o":
>> outputpdb=sys.argv[i+1]
>>
>> print "atomselction:",atomselection
>> print "inputpdb: ",inputpdb
>> print "outputpdb: ",outputpdb
>> sys.exit()
>> # --- EOF ---
>>
>> This shows this behaviour:
>> $ vmd -dispdev text -python -e test.vmdpy -args -i input.pdb -o
>> output.pdb -s "all"
>> Info) VMD for LINUXAMD64, version 1.8.6 (April 6, 2007)
>> Info) http://www.ks.uiuc.edu/Research/vmd/
>> Info) Email questions and bug reports to vmd_at_ks.uiuc.edu
>> Info) Please include this reference in published work using VMD:
>> Info) Humphrey, W., Dalke, A. and Schulten, K., `VMD - Visual
>> Info) Molecular Dynamics', J. Molec. Graphics 1996, 14.1, 33-38.
>> Info) -------------------------------------------------------------
>> Info) Multithreading available, 4 CPUs detected.
>> Info) Free system memory: 1486MB (38%)
>> /home/mkreim/bin/python/python-2.2.3/
>> Info) Starting Python...
>> Info) Text interpreter now Python
>> atomselction: all
>> inputpdb: input.pdb
>> outputpdb: output.pdb
>>
>>
>> $ vmd -dispdev text -python -e test.vmdpy -args -i input.pdb -o
>> output.pdb -s "all and not water"
>> argv: Index nicht im gültigen Bereich.
>
> o, and _please_ change your locale to "C" before quoting error
> messages. i guess that more than 90% of the people subscribed
> here would have to respond to this mail with "i don't understand
> this error message either, because i don't speak any german".
>
> in general, i'd like to remind you that using native language
> support when using scientific software can be leading to all
> sorts of weird problems, starting with the difference between
> decimal point and decimal comma in german and english.
> caveat emptor.
>
>
> cheers,
> axel.
>
>
>
>> I tested this also with the python 2.5.1 installation of my linux
>> distribution and with the python2.2 binary of VMD:
>> $python2.2 test.vmdpy -o out -i in -s "all but not water"
>> atomselction: all but not water
>> inputpdb: in
>> outputpdb: out
>>
>> As you can see, if I run the program without VMD everything goes well
>> but VMD leads to this strange "argv: Index nicht im gültigen Bereich."
>> message.
>>
>> What am I doing wrong? How can I pass my atomselection strings to
>> VMD/python-scripts via command line?
>>
>> Thanking you in anticipation.
>>
>> With best regards,
>>
>> Michael Kreim
>
>