如何记录从snakemake中脚本到达的错误消息

发布于 2025-02-12 14:09:43 字数 509 浏览 1 评论 0原文

我正在尝试使用自定义的Python脚本构建一条Snakemake管道。 我的一些脚本陷入了错误,导致管道关闭。 但是,尽管在Shell输出中,我几乎看不到导致关闭的Python错误消息的末尾,但此错误不会在任何地方记录。它没有在自动创建的snakemake.log中记录(仅说明哪个脚本在不给出错误消息的情况下失败),并添加一个“ log:”,然后将文件夹带到失败的规则中,只能创建一个空的日志。

有没有办法访问错误消息,因此我可以解决基本问题?

编辑: 我当前的Snakemake规则看起来像这样:

rule do_X:
    input: "{Wildcard}_a"
    output: "{wildcard}_b"
    log: out = "{Wildcard}_stdout.log"
         err = "{Wildcardd}_stderr.err"
    shell: python script x.py {input}{output}

如果脚本失败,我会恢复空的日志,

I am trying to build a snakemake pipeline with custom python scripts.
Some of my scripts run into errors, leading to a shutdown of the pipeline.
However, while in the shell output I can barely see the end of the python error message that leads to the shutdown, this error is not logged anywhere. It is not logged in the snakemake.log that gets automatically created (only stating which script failed without giving the error message), and adding a "log: " with a folder to the rule that fails only creates an empty log.

Is there a way to access the error message, so I can solve the underlying issue?

Edit:
my current snakemake rule looks like this:

rule do_X:
    input: "{Wildcard}_a"
    output: "{wildcard}_b"
    log: out = "{Wildcard}_stdout.log"
         err = "{Wildcardd}_stderr.err"
    shell: python script x.py {input}{output}

If the script fails, I recive empty logs,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-02-19 14:09:43

link”由KeyboardCat的评论提供似乎效果很好。

演示从您的代码和建议的解决方案进行了调整:

将其保存为script_x.py

#script_x.py
print("test output in stdout made my script")
#import sys # WITH THIS COMMENTED OUT, YOU'LL GET TRACEBACK IN `test_stderr.err`
sys.stderr.write("This was made from std.err")
with open("test_b", "w") as out_file:
    out_file.write("result\n")

然后您的snakefile is>是:

with open("test_a", "w") as out_file:
    out_file.write("#trigger file")


rule all:
    input: "test_b"

rule do_X:
    input: "{wildcard}_a"
    output: "{wildcard}_b"
    log: out = "{wildcard}_stdout.log",
         err = "{wildcard}_stderr.err"
    shell: 'python script_x.py {input}{output} 2> {log.err} 1> {log.out}'

execute snakemake,因此它使用snakefile 。它会引起问题,因为script_x.py有错误并且失败。

test_stderr.err中,您会从script_x.py错误的中看到追溯,因为sys未导入 strong>在编写脚本时:

Traceback (most recent call last):
  File "script_x.py", line 3, in <module>
    sys.stderr.write("This was made from std.err")
NameError: name 'sys' is not defined

如果您删除文件test_a,然后从import incort ofimport中删除 script_x.py(行#3),它应该立即运行,并在文本中导致中的std.err在文件test_stderr中显示。 .err

test_stdout.log将始终以在脚本中制作的测试输出,因为它在脚本中包含错误的行之前创建。


您说您在由KeyboardCat的评论提供的链接;但是,您实际尝试了什么?始终最好包括您在评论中尝试的变化或在OP中添加更新部分。

The link provided by KeyboardCat's comment seems to work well.

Demonstration adapted from your code and the suggested solution:

Save this as script_x.py:

#script_x.py
print("test output in stdout made my script")
#import sys # WITH THIS COMMENTED OUT, YOU'LL GET TRACEBACK IN `test_stderr.err`
sys.stderr.write("This was made from std.err")
with open("test_b", "w") as out_file:
    out_file.write("result\n")

Then your Snakefile is:

with open("test_a", "w") as out_file:
    out_file.write("#trigger file")


rule all:
    input: "test_b"

rule do_X:
    input: "{wildcard}_a"
    output: "{wildcard}_b"
    log: out = "{wildcard}_stdout.log",
         err = "{wildcard}_stderr.err"
    shell: 'python script_x.py {input}{output} 2> {log.err} 1> {log.out}'

Execute snakemake so it uses the Snakefile. It will cause an issue because script_x.py has an error in it and fails.

In test_stderr.err, you'll see the traceback from when the script_x.py errored because sys wasn't imported as the script was written:

Traceback (most recent call last):
  File "script_x.py", line 3, in <module>
    sys.stderr.write("This was made from std.err")
NameError: name 'sys' is not defined

If you delete the files test_a and remove the # from in front of the import sys line inside script_x.py (line #3), it should run without error now and result in the text This was made from std.err showing up in the file test_stderr.err.

test_stdout.log will always end up with test output in stdout made by script in it because it gets created before the line containing the error in the script.


You say you tried the solution suggested at the link provided by KeyboardCat's comment; however, what did you actually try? Always best to include the variation you tried in the comment or add an update section to your OP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文