I am new to the CWL world and while attempting to replicate my manual workflow in CWL I stumbled upon a problem. I’m from a materials science background and what I often need to do is process .cif files (containing crystallographic information for a certain element and structure) using the same python script over a range of different numbers of atoms. On the shell, this would look something like
some_script.py will parse some_element and some_crystal_structure, load the appropriate .cif file and process. The only to options to replicate this in CWL I found so far are
I either copy my python script to the work directory via
requirements:
- class: InlineJavascriptRequirement
- class: InitialWorkDirRequirement
listing:
- entryname: some_script.py
entry: |
# source code for the script
which is inconvenient because then I can never access the script as a regular python file or
2. I explictly state the name of my python script in the input file
which I personally find kind of redundant, since this workflow is specific to my specific python script.
Is there a third option I am missing here?
This applies to the *.cif files as well. Ideally I would have a an input file to look like this:
element: Be
structure: hcp
natoms: 256
and the workflow would stage the appropriate .cif file itself. But I have found no way to stage a file to the work directory using values derived from the input values…
I think you’re on the right track with your evaluation of solution 1). I believe this feature is meant more for Python “snippets” that can be succinctly expressed to perform simple tasks. If some_script.py is more than, say, 10 lines then this is probably not the move.
Solution 2) is the way to go if you haven’t packaged and installed your Python script. The reason for this is CWL does everything in an isolated temporary directory, so if your Python script isn’t an input for your Workflow or CommandLineTool then it isn’t copied to that temporary directory, and therefore when the Workflow executes Python with “some_script.py”, that script file isn’t found in current working directory (the temp directory).
Re redundancy: you need to list the python script as an input so that it is staged for use. With CommandLineTools, you don’t always define inputBinding, and when you don’t that input isn’t actually passed as a parameter to the underlying baseCommand. This is useful in combination with InitialWorkDirRequirement because it ensures that the file is included in the temporary directory without actually being passed to the command.
Another workaround is to make your script runnable from any directory, not just when it’s in your current working directory. The process of installing a Python package essentially places your script in a directory listed in your $PATH variable. You may also add the script’s current directory (absolute path) to the PATH variable, but this gets messy fast and I’d recommend against it.
Regarding your .cif files, if you have a consistent naming scheme for these files that can be derived from element,structure,natoms, then you can use these string type inputs to craft a File type input using an expression so that they can be staged with InitialWorkDirRequirement. Can you provide more details about the cif file(s) you would need for a given run, and whether these required files change depending on your inputs for element,structure,natoms?
Thank you for your insight!
You’re right I could package the scripts or make them globally available but since they are small processing scripts, that feels not optimal either. I will go the way with including the script in the input file for now. I still find this solution somewhat suboptimal, since the other input parameters (e.g. -e, -s, -n) are specific to the script. Therefore there will be one input that can only have one value or else the CWL script will return an error. This stage of my workflow contains three python scripts of similar name, so I could imagine that this might happen.
Is this a situation where exclusive inputs should be used (Advanced Inputs – Common Workflow Language User Guide)? As in, I could make one CWL script for all the python processing scripts for this stage of my workflow and then make it so that depending on the chosen script, only the appropriate input parameters are allowed? Would that work
“if you have a consistent naming scheme for these files that can be derived from element,structure,natoms, then you can use these string type inputs to craft a File type input using an expression so that they can be staged with InitialWorkDirRequirement” - Yes, I do have that! Given element and structure, the cif files will take the form $element_$structure.cif. I was thinking I could do this via an expression, but I never got it to work. So I guess this is more my unfamiliarity with CWL/JS expressions, I will try again and maybe post a new question to this forum here if I cannot figure it out.
Hmm, that sounds like something else might be going on. I’m going to take a step back on what I said about including the Python script as an input without inputBinding. Instead I’d recommend something like the following (which is just a snippet and hasn’t been tested):
@tetron I like that. Rather than shoehorning a super minimal File object from a string, just start with a Directory listing with complete File records and choose the one(s) you want. Filing this away for later use, thank you!
Thank you so much for your input! I like @tetron’s approach for the python scripts as well, this looks like what I am looking for. I will implement this and test the code for the cif file handling as well!