微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当在akemake中的ues awk时发生NameError

如何解决当在akemake中的ues awk时发生NameError

首先,这是一个测试示例(Snakefile):

rule test:
    input:"path/file1","path/file2"
    output:"path/file3"
    shell:
        """
        awk 'NR==FNR{score[$3]=$5;next}{{sum=0}for(i=$2;i<=$3;i++){sum+=score[i]}printf "%-10s\\t%-10s\\n",sum,$4}' {input[0]} {input[1]} >> {output}
        """

当我运行此脚本时,它返回NameError: The name 'score' is unkNown in this context. Please make sure that you defined that variable. Also note that braces not used for variable access have to be escaped by repeating them,i.e. {{print $1}},我曾尝试{score}或复制{},但所有方法都不起作用。所以,我想知道如何解决这个问题。谢谢。

解决方法

那是因为snakemake试图格式化文本并将变量放入字符串中。由于例如分数是脚本的一部分,snakemake无法推断出它属于哪个变量,并且崩溃。要避免这种行为,请使用双大括号:{{score[$3]}}。带有多个大括号的情况看起来很丑陋,就像您的规则

rule test:
    input:"path/file1","path/file2"
    output:"path/file3"
    shell:
        """
        awk 'NR==FNR{{score[$3]=$5;next}}{{{{sum=0}}for(i=$2;i<=$3;i++){{sum+=score[i]}}printf "%-10s\\t%-10s\\n",sum,$4}}' {input[0]} {input[1]} >> {output}
        """

(我希望我不会错过任何机会,但我想你明白了)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。