openmolecules.org

 
Home » DataWarrior » Functionality » Variables in Macros (Variables in Macros)
Variables in Macros [message #1154] Thu, 03 December 2020 11:42 Go to next message
zsolt
Messages: 4
Registered: December 2020
Junior Member
Hello DW gurus!

Thanks for the awesome product and making it open source - it is a great piece of software indeed.

I would like to use user-defined strings in various fields of the macros - e.g. when saving a file using the saveTextFile task, I would like to be able to compose the fileName property of the macro task from a constant directory prefix and a filename that is defined by the user on the UI - e.g. by using a defineVariable task. I haven't been able to make it work so far. (#ask# works here, but I would not like to give the user complete freedom to select the destination file, because I would like to parse the generated files later using other software so I need a predictable place. I tried variable substitutions like #variablename#, $variablename, ${variablename} and various combinations of these..) If you have a link to the relevant part of the documentation, that would be great. I am not sure if I can do loops and conditions in macro code or mimic it somehow making use of the expression parser and perhaps goto. (I found the generic macro page on the website, and the expression parser page but not much about the macro syntax. What I know is mostly from exporting various macros and reading the exported file. Also, I have seen screenshots on the Internet about the Macro Editor view displaying some help about the tasks but this does not appear for me.)

My scenario in more detail: for now I would only like to use the selection functionality ("lasso tool") of the 2D or 3D view. A script in a Jupyter notebook would do some calculation on data, then eventually start up DataWarrior with this data loaded, then have it generate a plot via macros. Then users would apply the lasso tool to create and save multiple selections, and close Datawarrior afterwards. The Jupyter notebook would resume running, it would parse these saved selections so they are available for further processing under the names specified by the user. I would like to provide this functionality with the least amount of extra clicking and typing. So optimally all the user would need to do is select the compounds and give the selection a name - repeated multiple times - then close the main window when s/he is finished.

One workaround I have now is not to let the user specify the file names, instead have macros they can click to save to selection-0, selection-1 ... etc. and these would generate predictable file names. However then they have to keep in mind what was in selection-n: it would be better if they had to just come up with a name once they create the selection and that could be the name of the file..

If this is not possible currently using macros I am thinking about writing a new macro myself and add it to Datawarrior in a jar. If you have some documentation pointers for this that would also be great.



Versions:
Datawarrior
5.2.1

Java (datawarrior bundled)
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-BellSoft-b10)
OpenJDK 64-Bit Server VM (build 25.232-b10, mixed mode)

OS
Linux 5.4.0-56-generic #62~18.04.1-Ubuntu SMP Tue Nov 24 10:07:50 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux - "Ubuntu 18.04.4 LTS

Many thanks for your help in advance.
Zsolt

Re: Variables in Macros [message #1159 is a reply to message #1154] Fri, 04 December 2020 13:40 Go to previous messageGo to next message
thomas is currently offline  thomas
Messages: 715
Registered: June 2014
Senior Member
You could put a macro into your data file or the users' macro folder, which asks for a selection name, then makes a list from the selection and saves the list into a predefined folder using the given name as part of the file name. The user could then select rows, start the macro, select other rows, start the same macro, select more... and finally close DataWarrior for the Jupyter script to continue.

The macro could look like the following.
(I am not sure, whether it runs with the old 5.2.1; potentially you have to upgrade to the current dev version)
This macro example assumes that the data file contains a column 'Structure-ID'. The saved list is a text file containing values from this column. Note: with the current dev version of DataWarrior you can just copy the macro and use 'Paste Macro' from the DataWarrior Macro-menu. (after adapting the path and 'Structure-ID' to your needs).

<macro name="Save Selection">
<task name="defineVariable">
name=filename
message=Please give a name to your selection
</task>
<task name="newRowList">
listName=selected rows
mode=selected
</task>
<task name="exportRowList">
keyColumn=Structure-ID
listName=selected rows
fileName=/home/thomas/$filename.txt
</task>
<task name="showMessage">
message=Your list has been saved!
</task>
</macro>
Re: Variables in Macros [message #1167 is a reply to message #1159] Mon, 07 December 2020 11:50 Go to previous message
zsolt
Messages: 4
Registered: December 2020
Junior Member
Hello Thomas, thank you very much for the quick reply.

The solution - as you suggested - was to upgrade from 5.2.1 to the latest development version. (I am not sure why you were referring to it as the "old" version: on the website this is shown as the latest stable version at the time of writing. Is there a repository where there are more fresh versions available?)

For those reading this post later it may be helpful to know that you can find the description on how to do the upgrade in the download page but the paragraph only appears after you tick the "I have read and understood the disclaimer" checkbox - it took me a while to find this.

Many thanks again for open-sourcing this great product.

Zsolt

[Updated on: Mon, 07 December 2020 11:50]

Report message to a moderator

Previous Topic: Loop over columns for calculations
Next Topic: Option to Display Table Only
Goto Forum:
  


Current Time: Fri Nov 22 22:44:05 CET 2024

Total time taken to generate the page: 0.04045 seconds