This is a test version of Biostars. For the public version, visit https://www.biostars.org.
snakemake truncating shell codes

I'm trying to change the chromosome number notation from [0-9XY] to Chr[0-9XY] using the samtools reheader in the shell command of the snakemake.

rule rename:

    input:
       os.path.join(config["input"], "{sample}.bam"),

    output:
       os.path.join(config["output"], "new_sample/{sample}_chr.bam")

    log:
       os.path.join(config["log"], "samtools/{sample}")

    shell:
         "samtools view -H {input} | sed  -e 's/SN:\([0-9XY]*\)/SN:chr\1\/' -e 's/SN:MT/SN:chrM/' |samtools  reheader - {input}  {output}"

The code ran successfully in the terminal but when I used the code in snakemake it gave the error:

shell:
        samtools view -H /Users/input/EGAF00000788153_PD11458c.bam | sed  -e 's/SN:\([0-9XY]*\)/SN:chr/' -e 's/SN:MT/SN:chrM/' |samtools reheader - /Users/input/EGAF00000788153_PD11458c.bam > /Users/output/new_sample/EGAF00000788153_PD11458c_chr.bam
        (one of the commands exited with non-zero exit code; note that snakemake uses bash strict mode!)

When I looked into the error I found that the snakemake read the sed -e 's/SN:([0-9XY]*)/SN:chr\1/ as 's/SN:([0-9XY]*)/SN:chr/'. That is, it truncated the code.

python snakemake shell

1 answer

I think it's due to python treats "\" as escape character, so adding another backslash should fix the error

"samtools view -H {input} | sed  -e 's/SN:\([0-9XY]*\)/SN:chr\\1\/' -e 's/SN:MT/SN:chrM/' |samtools  reheader - {input}  {output}"

Log in to answer this question.