Snakemake 规则在 slurm 模式下不产生输出

问题描述 投票:0回答:1

我最近才开始使用 Snakemake,但到目前为止,它一直进展顺利,并且减轻了我肩上的大量手动工作。但现在我遇到了一个我无法弄清楚的问题。我想在一堆之前生成的输入文件上运行一个名为 spladder 的 python 程序。当我直接调用snakemake

snakemake -c1
时,效果很好,但是当我告诉它使用slurm
snakemake -j1 --use-conda --slurm
时,脚本本身没有输出(包括来自
print()
)。只有在脚本完成或由于某种原因失败后,我才能从脚本中获取输出。 这是让我头疼的规则:

rule spladder:
    input:
        genomes = [f"../ressources/genomes/{genome}/genomic.gtf" for genome in accessions['genome_id'].unique()],
        bams = [f"data/alignments/{rsa}/{rsa}_Aligned.sortedByCoord.out.bam" for rsa in accessions.index]
    output:
        [f"data/spladder/{genome}/merge_graphs_mutex_exons_C3.pickle" for genome in accessions['genome_id'].unique()] 
    threads: 20
    resources:
        mem_mb=1024*20, 
        runtime=60*8 
    run:
        print("========RUNNING JOB SPLADDER=========")
        print("\n\n\n")
        print(input.genomes)
        for genome in input.genomes:
            genome_id = re.search(r'genomes/(.+?)/', genome).group(1)
            filtered_accessions = accessions[accessions['genome_id'] == genome_id]
            rsa_ids = filtered_accessions.index.values
            if len(rsa_ids) > 0:
                bam_files = [f"data/alignments/{rsa}/{rsa}_Aligned.sortedByCoord.out.bam" for rsa in rsa_ids]
                bam_files = ",".join(bam_files)
                print(f"running spladder for {genome} with {bam_files}")
                cmd = fr"""
                mkdir -p data/spladder/{genome_id}

                spladder build --set-mm-tag nM --bams {bam_files} --annotation {genome} \
                --outdir data/spladder/{genome_id} --parallel {threads}
                """
                shell(cmd)

当我调用其他(非 python)程序(例如 STAR)时,输出会实时写入 slurm 文件,但我也注意到 print() 函数的输出仅在程序完成后添加。 如何在我的snakemake规则中实时查看python代码的输出?

提前致谢

尼克拉斯

python snakemake
1个回答
0
投票

我向你保证,我在谷歌上进行了广泛的搜索,并且之前也提示过chatGPT,但没有产生任何结果。但后来我粘贴了我的整个 stackoverflow 问题,我想是因为我得到的上下文比我之前的提示更多,在我看来,正确的答案。请持保留态度,如果有人有更好的解决方案,我仍然很高兴听到。但人工智能是这样说的:

您似乎遇到了标准的缓冲问题 使用 Slurm 运行 Snakefile 时的输出。这种行为可以是 受 Python 缓冲其输出的方式影响。就 Slurm 而言, 标准输出通常在写入文件时进行缓冲,这可以 导致消息打印延迟。

要解决此问题,您可以尝试以下方法:

Flush the Output Buffer:
Add explicit calls to sys.stdout.flush() after your print statements to force the buffer to be flushed immediately. Modify your

像这样运行部分:

    print("========RUNNING JOB SPLADDER=========")
    print("\n\n\n")
    print(input.genomes)
    for genome in input.genomes:
        # ... your existing code ...
        print(f"running spladder for {genome} with {bam_files}")
        cmd = fr"""
        mkdir -p data/spladder/{genome_id}

        spladder build --set-mm-tag nM --bams {bam_files} --annotation {genome} \
        --outdir data/spladder/{genome_id} --parallel {threads}
        """
        shell(cmd)
        sys.stdout.flush()  # Add this line ```

Unbuffered Mode: Another approach is to run Python in unbuffered mode
by setting the environment variable PYTHONUNBUFFERED to a non-empty
string. You can add this to the beginning of your Snakefile:

```python

    import os

    os.environ["PYTHONUNBUFFERED"] = "1" ```
    This will disable Python's output buffering and might provide real-time output in the Slurm logs.

Try these modifications and see if they resolve the issue with delayed
output in your Slurm jobs. If the problem persists, you may need to
explore Slurm-specific configurations or consult the Snakemake
documentation for any Slurm-specific recommendations.
© www.soinside.com 2019 - 2024. All rights reserved.