Snakemake. Problem with awk
1
1
Entering edit mode
3.4 years ago
kamanovae ▴ 100

Hi!

I have a problem with the awk that looks like this:

awk: cmd. line:1: {if(NR%4==1) {printf("">%s
awk: cmd. line:1:                         ^ syntax error
[Wed Jun 30 16:46:02 2021]
Error in rule univec:
    jobid: 0
    output: out_univec/SRR13510812_1.fasta, out_univec/SRR13510812_2.fasta

RuleException:
CalledProcessError in line 63 of /storage1/kaman/Ldec_all/snakefile:
Command 'set -euo pipefail;  cat out/SRR13510812_1_nohg38.fastq | awk '{if(NR%4==1) {printf("">%s

my code looks like this. I guess I somehow need to screen part of awk command

rule univec:
        input: data1=config["out"] + "{sample}_1_nohg38.fastq", data2=config["out"] + "{sample}_2_nohg38.fastq"
        output: output1=config["out_univec"] + "{sample}_1.fasta", output2=config["out_univec"] + "{sample}_2.fasta"
        run:
                shell("""cat {input.data1} | awk '{{if(NR%4==1) {{printf("">%s\n"",substr($0,2));}} else if(NR%4==2) print;}}' > {output.output1}""")
                shell("""cat {input.data2} | awk '{{if(NR%4==1) {{printf("">%s\n"",substr($0,2));}} else if(NR%4==2) print;}}' > {output.output2}""")

Thanks!

awk Snakemake • 3.6k views
ADD COMMENT
1
Entering edit mode

how about using a """ section ?

 shell: """
         awk '{{if(NR%4==1) {{printf(">%s\n",substr($0,2));}} else if(NR%4==2) print;}}'  {input.data1}  > {output.output1}
        """
ADD REPLY
0
Entering edit mode

it doesn't help either. This error appears:

awk: cmd. line:1: {if(NR%4==1) {printf(">%s
awk: cmd. line:1:                      ^ unterminated string
awk: cmd. line:1: {if(NR%4==1) {printf(">%s
awk: cmd. line:1:                      ^ syntax error
[Wed Jun 30 17:05:11 2021]
Error in rule univec:
    jobid: 13
    output: out_univec/SRR13510812_1.fasta, out_univec/SRR13510812_2.fasta
    shell:
         awk '{if(NR%4==1) {printf(">%s
",substr($0,2));} else if(NR%4==2) print;}'  out/SRR13510812_1_nohg38.fastq  > out_univec/SRR13510812_1.fasta
        (one of the commands exited with non-zero exit code; note that snakemake uses bash strict mode!)
ADD REPLY
3
Entering edit mode
3.4 years ago

ah ! I think you need to escape the \n to \\n

ADD COMMENT
0
Entering edit mode

Thanks! It really works!

ADD REPLY
0
Entering edit mode

Could you post the final working command so others may benefit from it?

ADD REPLY
0
Entering edit mode

It's my final code:

   rule univec:
            input: data1=config["out"] + "{sample}_1_nohg38.fastq", data2=config["out"] + "{sample}_2_nohg38.fastq"
            output: output1=config["out_univec"] + "{sample}_1.fasta", output2=config["out_univec"] + "{sample}_2.fasta"
            run:
                    shell(""" awk '{{if(NR%4==1) {{printf(">%s\\n",substr($0,2));}} else if(NR%4==2) print;}}'  {input.data1}  > {output.output1}""")
                    shell(""" awk '{{if(NR%4==1) {{printf(">%s\\n",substr($0,2));}} else if(NR%4==2) print;}}'  {input.data2}  > {output.output2}""")
ADD REPLY

Login before adding your answer.

Traffic: 2662 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6