views:

111

answers:

2

I'm writing a script to extract some useful data about a series of chemical simulations I've been running.

To get this data I need (1) a C-program that calculates the density from a file type called *.pdb. I already have (1). And (2) I need to use a program called vmd to get that pdb. In order to accomplish (2) from the command line, I can submit a tcl script, as vmd has a build in tcl interpreter.

These functions -- calling the vmd to run the tcl script, then running the compiled c-program -- will be the key activities of my wrapper data extraction script.

I would like to eliminate the superfluous TCL script, reducing my count from 2 scripts (wrapper script + tcl script for vmd) down to 1. But I'm not sure quite how to do this. One potentially solution seems to be to embed my TCL script within my wrapper script, if there's a way to make such an embedded script callable from external programs.

Most of my data collection scripts so far have been in BASH, so ideally I would like to stick to a BASH script as I'm very familiar with bash scripting versus having only beginning knowledge of Python/Perl.

Here are my questions:
1. Can you embed a TCL script inside a Bash script?
2. Can you make this script callable by an external program?
e.g. in pseudocode:

#!/bin/bash
....
tclembed extract {
   #tcl script
   ...
}
...
vmd -dispdev text -e extract.tcl >& extract_results.log #where vmd is 
                                                        #an external program

3. If the answer to #2 is no, can you do this in Python, perhaps with the Minotaur library? I would consider the switch to python, if so...
http://markmail.org/message/6kogjphzqtn4ilch
4. If not, how would you suggest trying to merge these two scripts (a tcl routine and a bash script that calls it) into a single file?
5. If anybody HAS gotten external calls of this nature to work using Minotaur, can you post some explanatory code?

I've thought of one non-embedding solution which to #4, which would be to write a function in my Bash script that writes a file with the entire tcl script. That way I would have a single script, but could dump the subscript for use with external programs, later deleting it. I have a feeling this solution is kinda kludgy though I know for sure that it works, vs. embedded solutions.

+1  A: 

1. Can you embed a TCL script inside a Bash script?

Not easily. The best way is to write the script to a temporary file and pass the name of that file to tclsh (or wish if it is a Tcl/Tk program). That should be a "simple matter of programming", i.e., some awkward coding but not fundamentally hard.

2. Can you make this script callable by an external program?

I don't quite understand what you want to do here. You can put a #! line at the start of a Tcl script and mark the file executable. That works well. The best way of all to do that is this:

#!/usr/bin/env tclsh8.5
your tcl script here...

3. If the answer to #2 is no, can you do this in Python?

This wiki page mentions something called Typcl, which is reported to allow doing Tcl from inside Python. I have never tried it.

(I think questions 4 and 5 are largely irrelevant based on my answers above.)

Donal Fellows
Your answer to 1) echoes my suggestions in my closing paragraph, so I already had that one figured out. :o) Let me see if I can clarify to #2. I of course understand how to make a tcl executable. My question is whether you can EMBED a TCL subscript inside a BASH (or PYTHON file) and make that embedded script callable by external programs. For example, in my case, my hypothetical bash script would contain the TCL subscript. I would then call an external program (vmd) from WITHIN the BASH script, passing it this subscript as an argument. (cont'd)
Jason R. Mick
Using a separate TCL script defeats my whole premise -- to reduce my # of scripts from two to a single all-purpose script. As to your answer to #3, based on my previous reading, if I understood correctly, the Typcl interface in Python was only useful for interpreting single commands, whereas Minotaur offers an entire script can be embedded. Hence, if I understood properly it would NOT be irrelevant... And your response still doesn't clarify whether you can embed the script AND make a call to an external program within your python script, passing that external program your embedded script.
Jason R. Mick
@Jason: You can wrap things so that it builds the separate script from the contents of it (stored in the shell script) and writes it to a temporary file. The thing that you're missing though is a way for tclsh to take a script from the command line via some kind of `-e` option (by analogy with other programs); you're missing it because tclsh just doesn't have one.
Donal Fellows
I'm sure lost, Donal. If lack of an -e were the only difficulty, we could offer "expect -c ..." OR propose an invocation such as "tclsh this_script.tcl << HERE ..." I don't think that's it, though. I'm now thoroughly confused about whether "this script" is supposed to go to vmd or "an external program that I call". My best guess: vmd *is* the "external program", and, yes, it's eminently feasible to embed a Tcl script within a bash script, extract the embedded Tcl script, and do whatever-is-intended-although-I-don't-understand-it.
Cameron Laird
Cameron is (somewhat) correct. If the tcl script is a separate file, vmd can understand it and execute it using its interpreter regardless of whether your make the tcl script executable. i.e. vmd -e my.tcl works whether my.tcl starts with the tclsh location or not. My question is whether there's a more elegant way to embed the script (say, my.tcl) in bash and then hand it to vmd rather than just a text dump to my.tcl. You and I both proposed a text-dump type approach, if I understood you correctly.
Jason R. Mick
YES. It is possible: vmd reads commands from stdin. While I don't think it's such a terrible thing to "text-dump" to $TEMPNAME.tcl, you can, as an alternative, invoke "vmd -dispdev text -eofexit << HERE > output.log", as <URL: http://www.ks.uiuc.edu/Research/vmd/current/ug/node204.html > mentions. I think we've converged on something that meets your requirements, Jason R. Mick.
Cameron Laird
Awesome, I think that's what I'm looking for. Thanks! I thought there might be a more streamlined way of inputting the script text to the interpreter and you appear to have found it. Can you edit your answer to include this solution, so I can give you credit for it?
Jason R. Mick
I will indeed write it up. I'm in meetings for a couple of hours. 'Catch you later.
Cameron Laird
+2  A: 

There have been several Tcl-Python alloys. As Rafe Kettler's comment above sketches, the place to start is with a standard Python installation. This includes Tkinter, which builds in a full Tcl interpreter, accessible as described in the Wiki page mentioned. So, yes, it is feasible to "do this in Python".

I really don't get what this has to do with vmd, though. vmd builds in a Tcl interpreter already. While I entirely support the aim of "reduction of moving parts", so that you have, for example, one script, rather than two, coding something in Python, when vmd already exposes Tcl, doesn't seem like a step in the direction Jason R. Mick wants to go.

SOMEWHAT LATER: after an exchange of comments with Jason R. Mick, it occurred to me he might find

#!/bin/bash


echo "Here's a bit of bash-iness."

MYSCRIPT='
puts "Here I am, inside Tcl."
puts "See?  I can do calculations:  [expr 3 + 5]."
exit 0
'

tclsh << HERE
$MYSCRIPT
HERE

suggestive. Its output, of course, is

Here's a bit of bash-iness.
Here I am, inside Tcl.
See?  I can do calculations:  8.

I wrote this in terms of tclsh, but, if I'm keeping up, Jason R. Mick will actually want to use vmd. The appropriate homologue for *vmd is something like

    ...
vmd -dispdev text -eofexit << HERE > output.log
$MYSCRIPT
HERE

While I can think of several other ways to meld bash and Tcl, I believe this one is most in the spirit of the original question.

I want to note, too, that, from the little I know of vmd, it would be entirely appropriate to do the same with Python in place of Tcl: vmd is equally adept with either.

Finally, both Python and Tcl are general-purpose languages, with approximately the same power as bash, so yet another direction to take this project would be to write it entirely in Tcl (or Python), rather than bash. Embedding scripts in the way illustrated above is at least as easy in Tcl (or Python) as in bash.

Cameron Laird
Reread my problem description. I want to embed a tcl script that I can submit to an external program I call. Regardless of whether the script is embedded or in a separate file, I need vmd to run this script in order to generate my data. Ideally I would like to embed the script in my wrapper (BASH script), call vmd from my wrapper, handing it the embedded script, and then conclude by processing the results within my wrapper script (calling my c program, etc.) That would have only (1) working part. I don't think you understood my problem description.
Jason R. Mick
You forgot on the end `vmd -dispdev text -eofexit << HERE > output.log`. Post an edit, please when you can! Nicely done!
Jason R. Mick