Calling a Python script and passing arguments to it

2,388 views
Skip to first unread message

Pietro Marchesi

unread,
Feb 6, 2018, 12:36:35 PM2/6/18
to Nextflow
Hi, 

I just started using NextFlow, and came across a very simple problem. I would like to be able to run Python scripts and pass arguments to them, and I would like to do it in a Nextflow-like way.

Consider the following example, where I have a Python script that can be called with one argument --text, and prints the content of this argument. 

import argparse

argparser = argparse.ArgumentParser()
argparser.add_argument('--text', type=str)
ARGS = argparser.parse_args()

print(ARGS.text)

My attempt at creating a workflow that executes this script would look like something like this. 

process RunPythonScript {

  echo
true

  input
:
  val v
from 'some_text'


 
"""
  #!/usr/bin/env python ../../../python_script.py --text $v
  """


}

1) Finding the file with the relative path looks terrible, but I'm not sure what the 'proper' way to make sure that the main workflow directory is on the path (without hardcoding it).

2) This process fails,with 
python_script.py: error: unrecognized arguments: .command.sh

And while the command as shown in .command.sh looks fine, namely 
#!/usr/bin/env python ../../../python_script.py --text some_text

In .command.run I see this line:
/usr/bin/env python ../../../python_script.py --text some_text /home/pietro/code/nextflow_practice/my_simplest_flow/work/31/888424bbdffc5668b41b607d867913/.command.sh

I have looked around but could not find any examples on this. Any idea what I'm doing wrong / what would be the best way to implement this?

Best, 

Pietro









Paolo Di Tommaso

unread,
Feb 6, 2018, 12:41:45 PM2/6/18
to nextflow
I agree, it looks horrible. Don't use a path location to reference your script, instead give it execute permission and put in a folder named `bin` in the project root 


it will added to the task PATH so you will be able to invoke just like any other command.


p

--
You received this message because you are subscribed to the Google Groups "Nextflow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nextflow+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/nextflow.
For more options, visit https://groups.google.com/d/optout.

Emilio

unread,
Feb 7, 2018, 4:15:35 AM2/7/18
to next...@googlegroups.com

Hi Pietro,

about your second issue, it’s a scripting problem. The #!/usr/bin/env python cannot be used like this. Either you

  1. put #!/usr/bin/env python at the beginning of the python script and make it executable
  2. use the script interpreter (python) explicitly in the process

Here is an example for 2.:

process RunPythonScript {

  echo true

  input:
  val v from 'some_text'

  """
  python ../../../python_script.py --text $v
  """

}

Anyway, the best would be to use 1. and put the script in the bin folder as Paolo suggested.

Best,
Emilio

Pietro Marchesi

unread,
Feb 8, 2018, 8:08:22 AM2/8/18
to Nextflow
Ciao Paolo e Emilio, 

grazie dei suggerimenti and thanks for the awesome tool, looking forward to writing some pipelines with it. 

Best, 

Pietro 
To unsubscribe from this group and stop receiving emails from it, send an email to nextflow+u...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Nextflow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nextflow+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages