Galaxy tool, select different functions

Hi everybody.
I’m developing a tool that allows to choose between different options for a query.
Now my code is inside a python file where for each type of query I have a different function.
Following this guide I created the XML file and everything is running correctly without errors when I start Galaxy.
The problem is that when I run the tool (with every option) I get this error:

Job submission failed.
The server could not complete this request. Please verify your parameter settings, retry submission and contact the Galaxy Team if this error persists. A transcript of the submitted data is shown below.

In the terminal I get this:

galaxy.tools ERROR 2022-02-28 11:43:36,932 [pN:main.web.1,p:13472,w:1,m:0,tN:uWSGIWorker1Core0] Exception caught while attempting to execute tool with id 'impc_test':
Traceback (most recent call last):
  File "lib/galaxy/util/template.py", line 80, in fill_template
    return unicodify(t, log_exception=False)
  File "lib/galaxy/util/__init__.py", line 1060, in unicodify
    value = str(value)
  File "/Users/andrea/Desktop/galaxy/.venv/lib/python3.6/site-packages/Cheetah/Template.py", line 1053, in __unicode__
    return getattr(self, mainMethName)()
  File "cheetah_DynamicallyCompiledCheetahTemplate_1646045016_93172_79883.py", line 91, in respond
EOFError: EOF when reading a line

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "lib/galaxy/tools/__init__.py", line 1770, in handle_single_execution
    flush_job=flush_job,
  File "lib/galaxy/tools/__init__.py", line 1858, in execute
    return self.tool_action.execute(self, trans, incoming=incoming, set_output_hid=set_output_hid, history=history, **kwargs)
  File "lib/galaxy/tools/actions/__init__.py", line 529, in execute
    handle_output(name, output)
  File "lib/galaxy/tools/actions/__init__.py", line 455, in handle_output
    data.name = self.get_output_name(output, data, tool, on_text, trans, incoming, history, wrapped_params.params, job_params)
  File "lib/galaxy/tools/actions/__init__.py", line 784, in get_output_name
    return fill_template(output.label, context=params, python_template_version=tool.python_template_version)
  File "lib/galaxy/util/template.py", line 124, in fill_template
    python_template_version=python_template_version,
  File "lib/galaxy/util/template.py", line 126, in fill_template
    raise first_exception or e
  File "lib/galaxy/util/template.py", line 80, in fill_template
    return unicodify(t, log_exception=False)
  File "lib/galaxy/util/__init__.py", line 1060, in unicodify
    value = str(value)
  File "/Users/andrea/Desktop/galaxy/.venv/lib/python3.6/site-packages/Cheetah/Template.py", line 1053, in __unicode__
    return getattr(self, mainMethName)()
  File "DynamicallyCompiledCheetahTemplate.py", line 89, in respond
EOFError: EOF when reading a line
galaxy.tools.execute WARNING 2022-02-28 11:43:36,932 [pN:main.web.1,p:13472,w:1,m:0,tN:uWSGIWorker1Core0] There was a failure executing a job for tool [impc_test] - Error executing tool with id 'impc_test': EOF when reading a line

Here’s the .py code that I’m using as test and its XML file:

test.py (here I don’t have functions since I’m making tests on the input which is the part that, in my opinion, is raising the error)

import sys
import argparse
import requests
import pandas as pd
import numpy as np

def main():
    
    # define options
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("-k", "--option", type=str, help="Option selected")

    # parse
    args = parser.parse_args()
    option = str(args.option)
    
    if option == 'q1':
        print("Hello 1! Input: " + sys.argv[1])
        textfile = open(sys.argv[2], "w+")
        textfile.write( "Here's the input of first query: " + sys.argv[1] + "\n")
        textfile.close()
    elif option == 'q2':
        print("Hello 2! Input: " + sys.argv[1])
        textfile = open(sys.argv[2], "w+")
        textfile.write( "Here's the input of second query: " + sys.argv[1] + "\n")
        textfile.close()
    else:
        sys.exit("Error!")    
    
if __name__ == "__main__":
    main()        

test.xml

<tool id="impc_test" name="Test " version="0.1.0">
	<description>for impc query selection</description>
    <requirements>
      <requirement type="package">requests</requirement>
      <requirement type="package">pandas</requirement>
      <requirement type="package">numpy</requirement>
    </requirements>
    <command>
            python3 "{$__tool_directory__/test.py}" '$query_type.input' '$output' #set $option = str($selector) --option $option
	</command>
	<inputs>
			<conditional name="query_type">
				<param name="selector" type="select" labels="Select a query:">
					<option value="q1">First</option>
					<option value="q2">Second</option>
				</param>
				<when value="q1">
					<param name="input" type="text" label="Input 1:"/>
				</when>
				<when value="q2">
					<param name="input" type="text" label="Input 2:"/>
				</when>
			</conditional>
	</inputs>
    <outputs>
            <data format="tabular" name="output" label="${tool.name} on ${input}"/>
    </outputs>
	<help>
		Just a test for IMPC queries.
	</help>
</tool>

Essentially, I’m trying to set ‘option’ on different values so inside the code I can use it to call different functions.
Thank you all for the help.

I thought I understand your question but your code does not look like the guide link that you are sharing. You are pointing out slide 22. So not sure why you dont just use that example but maybe I understand your question wrong. See code below for inspiriation, not sure if it is a working example.

<tool id="impc_test" name="Test " version="0.1.0">
	<description>for impc query selection</description>
    <requirements>
      <requirement type="package">requests</requirement>
      <requirement type="package">pandas</requirement>
      <requirement type="package">numpy</requirement>
    </requirements>
    <command>
            python3 "{$__tool_directory__/test.py}" '$query_type.input' '$output'
            #if $query_type.selector == 'q1':
            --option $query_type.input_q1
            #else:
            --option $query_type.input_q2
             #end if
	</command>
	<inputs>
			<conditional name="query_type">
				<param name="selector" type="select" labels="Select a query:">
					<option value="q1">First</option>
					<option value="q2">Second</option>
				</param>
				<when value="q1">
					<param name="input_q1" type="text" label="Input 1:"/>
				</when>
				<when value="q2">
					<param name="input_q2" type="text" label="Input 2:"/>
				</when>
			</conditional>
	</inputs>
    <outputs>
            <data format="tabular" name="output" label="${tool.name} on ${input}"/>
    </outputs>
	<help>
		Just a test for IMPC queries.
	</help>
</tool>

Yes it’s a bit different from the slide 22 because I already tried to use a similar code and I had the exact same error that I have with the new code.
Essentially I would like to do something like:

if selector == 'q1'
    python3 test.py_function1 (not correct syntax, just a way to say that I would like to call that function)
else
    python3 test.py_function2

It seems that it’s not possible to call directly the function, so I’m trying to set a third variable to be used inside the python script.

To make the things more clear, I’m adding here an easier example that is giving me the exact same error:

test.xml

<tool id="test_impc" name="Test " version="0.1.0">
	<description>for impc.</description>
	<command><![CDATA[
python3 '{$__tool_directory__/test.py}' '$output' $selection
#if $operation.selection == 'op1':
	'$operation.input1'
#else:
	'$operation.input2'
]]></command>
	<inputs>
		<conditional name="operation">
			<param name="selection" type="select" labels="Select an option:">
				<option value="op1">First</option>
				<option value="op2">Second</option>
			</param>
            <when value="op1">
                    <param name="input1" type="text" label="Input first:"/>
            </when>
            <when value="op2">
                    <param name="input2" type="text" label="Input second:"/>
            </when>
		</conditional>	
	</inputs>
	<outputs>
		<data format="tabular" name="output" label="${tool.name} on ${input}"/>
	</outputs>
	<help>A simple test for impc, with options!</help>
</tool>

test.py

import sys

def main(): 
    
    opt = str(sys.argv[1])
    if opt == 'op1':
        first()
    else:
        second()
            
    sys.exit(0)    

def first():
    inpt = sys.argv[3]
    out = sys.argv[2]
    opt = str(sys.argv[1])
    
    txt = open(out, "w+")
    txt.write("You selected the first option (" + opt + ") with input string " + inpt + "\n")
    txt.close()
    
def second():
       inpt = sys.argv[3]
       out = sys.argv[2]
       opt = str(sys.argv[1])
    
       txt = open(out, "w+")
       txt.write("You selected the second option (" + opt + ") with input string " + inpt + "\n")
       txt.close()
    
if __name__ == "__main__":
    main()

The syntax of both files seems to be correct, I cannot really understand what is causing the error.

For starters you are already missing the $ character.
#if $operation.selection == 'op1':

Sorry it was a typo, in my code the $ is present

You are also missing ${input}:

<data format="tabular" name="output" label="${tool.name} on ${input}"/>

You use it in your output but to what is it pointing? Try to just use some text first and check if it works. Something like:

<data format="tabular" name="output" label="testing output"/>

And on top of my head, once you do have added $input also try this:

<data format="tabular" name="output" label="${tool.name} on ${input.display_name}"/>

Ok the problem was ${input} in the output section since it have a different name depending on which query I’m selecting. In the final code I have to remove it completely since one of the queries has no input.

You can solve this in another way. See:

<data format="tabular" name="output_op1" from_work_dir="path/output.txt" label="${tool.name} on ${input.display_name}">
<filter>operation['selection'] == 'op1'</filter>
</data>

<data format="tabular" name="output_op2" from_work_dir="path/output.txt" label="${tool.name} on No input">
<filter>operation['selection'] == 'op2'</filter>
</data>

I am not sure if this directly works but for inspiration.