Search code examples
pythonargumentssnakemake

split args of my script into multiple lines in snakemake


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.


Solution

  • 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.