1

as simple as it sounds it is not working for me!

I wrote a workflow with snakemake, in which I'm trying to call my own script with the neccessary options. I would like to split the command into multiple lines as follows (simplified version):

rule stats:
"""
create summary of fasta file
"""
input:
   config["In_dir"] + "Fasta/{sample}.fasta"
params:
   i = config["i"],
   nseq = 100,
   l = config["l"], 
   l_mismatch = 3,
   v_dist = 5, 
output:
   config["Out_dir"] + "/{sample}_stats.csv",
shell:
   """  
   scripts/test_args.py -f {input} \ 
   -l {params.l} -i {params.i} \  
   -ld {params.l_mismatch} \ 
   -vd {params.v_dist} --nseq {params.nseq} \ 
   -o {output}  &> {log}
   """  

However, the script is not recognizing the args, instead it prints the usage command with the required options. Since I'm importing argparse to provide the options, the default behaviour is to print the usage of the script if the required options were not given (see example bellow): enter image description here I tried other ways as for example in here but still not working. The only format it would work, is to have them all in one line. What am I missing? I already checked my input and the validity of the options, it works only if it is one line?! Thanks for any tip.

H.Hasani
  • 79
  • 1
  • 8
  • Can you explain what you mean by "it prints the usage command"? Also, are you running snakemake by any chance on Windows? What is the output of calling snakemake with the `-p` (print shell commands) option? Finally it is better if you provide is with the full `rule` definition, not only with the `shell` part. – euronion Apr 25 '23 at 12:37
  • thanks, I just updated the question, and no, I'm working on ubuntu. I copied the command from snakemake and run directly, that is how I tested all input is valid. The problem occurs purely when splitting the args on multiple lines. – H.Hasani Apr 25 '23 at 12:55

1 Answers1

1

Check if you have whitespace at the end of your lines. The \ is escaping the newline to continue the command but if you have \ it escapes the space instead.

If you run snakemake -p it prints all the commands as it will run and can help you in quickly finding formatting errors.

As another aside, I'm in the camp of using string concatentation instead of multiline strings as I don't like the escapes:

shell:
    'my_command '  # <-- note the space at the end, it is required!
        '--option1 val1 '
        '--option2 val2\n'  # <-- this newline indicates a new command
    'another_command '

Compared to

shell:
'''
    my_command \
        --option1 val1 \
        --option2 val2
    another_command
'''

Kind of pick your poison, but at least with single quotes any weird whitespace stuff is easy to find and with snakemake -p the commands print nicely.

Troy Comi
  • 1,579
  • 3
  • 12
  • Oh my! that did it, I simply replaced the escapes with single space. Thanx a zillion :) – H.Hasani Apr 25 '23 at 14:28
  • (+1) My preference is for raw strings with triple quotes, like `r""" bla bla bla """`. In this way you can use quotes inside the shell string and `-p` will show special characters like `\` and newlines exactly as they appear in the snakefile. String concatenation seems a bit noisy to me, but yes, pick your poison. – dariober Apr 26 '23 at 11:31