Snakemake:使用run指令时如何实现log指令?

2023-11-24

Snakemake 允许为每个规则创建日志log参数指定日志文件的名称。通过管道传输结果相对简单shell输出到此日志,但我无法找出记录输出的方法run输出(即 python 脚本)。

一种解决方法是将 python 代码保存在脚本中,然后从 shell 运行它,但我想知道是否还有另一种方法?


我有一些规则同时使用log and run指令。在里面run指令,我“手动”打开并写入日志文件。

例如:

rule compute_RPM:
    input:
        counts_table = source_small_RNA_counts,
        summary_table = rules.gather_read_counts_summaries.output.summary_table,
        tags_table = rules.associate_small_type.output.tags_table,
    output:
        RPM_table = OPJ(
            annot_counts_dir,
            "all_{mapped_type}_on_%s" % genome, "{small_type}_RPM.txt"),
    log:
        log = OPJ(log_dir, "compute_RPM_{mapped_type}", "{small_type}.log"),
    benchmark:
        OPJ(log_dir, "compute_RPM_{mapped_type}", "{small_type}_benchmark.txt"),
    run:
        with open(log.log, "w") as logfile:
            logfile.write(f"Reading column counts from {input.counts_table}\n")
            counts_data = pd.read_table(
                input.counts_table,
                index_col="gene")
            logfile.write(f"Reading number of non-structural mappers from {input.summary_table}\n")
            norm = pd.read_table(input.summary_table, index_col=0).loc["non_structural"]
            logfile.write(str(norm))
            logfile.write("Computing counts per million non-structural mappers\n")
            RPM = 1000000 * counts_data / norm
            add_tags_column(RPM, input.tags_table, "small_type").to_csv(output.RPM_table, sep="\t")

对于写入标准输出的第三方代码,也许redirect_stdout上下文管理器可能会有所帮助(可以在https://stackoverflow.com/a/40417352/1878788,记录于https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout).

测试蛇文件,test_run_log.snakefile:

from contextlib import redirect_stdout

rule all:
    input:
        "test_run_log.txt"

rule test_run_log:
    output:
        "test_run_log.txt"
    log:
        "test_run_log.log"
    run:
        with open(log[0], "w") as log_file:
            with redirect_stdout(log_file):
                print(f"Writing result to {output[0]}")
                with open(output[0], "w") as out_file:
                    out_file.write("result\n")

运行它:

$ snakemake -s test_run_log.snakefile

Results:

$ cat test_run_log.log 
Writing result to test_run_log.txt
$ cat test_run_log.txt 
result
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Snakemake:使用run指令时如何实现log指令? 的相关文章

随机推荐