【问题标题】:Mix shell command in environment conda with python scripts in snakemake tool将环境 conda 中的 shell 命令与蛇形工具中的 python 脚本混合
【发布时间】:2020-09-16 09:52:14
【问题描述】:

我想将环境 conda 中的 shell 命令与任何 Python 脚本混合使用,因此无法使用“运行”部分...

我试过了:

shell:"""
   gunzip -c {input.ech} | NanoFilt -l {params.min_length} --maxlength {params.max_length} -s {input.summary} -q {params.q} --readtype {params.rd} | gzip > Working_space_ont/01_nanofilt/{wildcards.sample}_filt_{params.q}.fastq.gz   
   python3 reinit.py
   while [[ -f Working_space_ont/01_nanofilt/{wildcards.sample}_filt_{params.q}.fastq.gz ]] && [[ grep -c '>' Working_space_ont/01_nanofilt/{wildcards.sample}_filt_{params.q}.fastq.gz < {params.cov} ]]; do
       python3 quality_minor.py
       gunzip -c {input.ech} | NanoFilt -l {params.min_length} --maxlength {params.max_length} -s {input.summary} -q {params.q} --readtype {params.rd} | gzip > Working_space_ont/01_nanofilt/{wildcards.sample}_filt_{params.q}.fastq.gz
   done
   mv Working_space_ont/01_nanofilt/{wildcards.sample}_filt_{params.q}.fastq.gz {output} 
"""

我有非零退出状态 1 错误

重新初始化.py:

import yaml

with open("config_wf.yaml") as f:
    old_yaml = yaml.load(f)

old_yaml["params"]["filtration"]["quality"] = old_yaml["params"]["filtration"]["quality_fix"]  

with open("config_wf.yaml", 'w') as f:
    yaml.dump(old_yaml, f, default_flow_style=False)

quality_minor.py:

import yaml

with open("config_wf.yaml") as f:
    old_yaml = yaml.load(f)

old_yaml["params"]["filtration"]["quality"] -= 1

with open("config_wf.yaml", 'w') as f:
    yaml.safe(old_yaml, f, default_flow_style=False)

你对我有什么想法吗?
最好的问候,

伊娃

【问题讨论】:

  • 您是否有意重写文件"Working_space_ont/01_nanofilt/{wildcards.sample}_filt_{params.q}.fastq.gz"
  • 是的,因为,我想创建这个文件并在之后控制它:如果它的内容太差,我想用较低的阈值重新运行命令..
  • 当它的内容可以接受时,我想把它移到一个目录 Working_space_ont/01_nanofilt/**PASS**/{wildcards.sample}_filt_{params.q}.fastq.gz

标签: python-3.x shell while-loop yaml snakemake


【解决方案1】:

看起来您的 python 脚本正在更改工作流的配置 yaml,而不是将其直接传递给 nanofilt 命令。那你是不是死循环了?配置 yaml 在启动 snakemake 过程时被读取,因此在规则中更改它应该没有效果。

您可以通过snakemake 实现此目的,在输入函数中尝试对参数进行尝试,其中每次失败都会减少参数,并且当过滤不好时作业会失败。

更简洁的解决方案是编写一个 python 脚本,将 nanofilt 用作库并包装其执行。 main method 是直截了当的。建立你的 args 并根据需要进行调整,然后所有的评估代码和循环都将在 python 中。

【讨论】:

  • 修改配置文件是一个非常糟糕的主意。感谢您对我的帖子感兴趣。在我的水平上,我只是设法将每个样本的更改参数导出到一个临时文件中。但也许在未来我能够做到这一点以获得更清洁的解决方案。
【解决方案2】:

Snakemake shell 不支持循环的 bash 语法。我看到了两种可能的解决方案。

首先,您可以将此代码包装到 .sh 文件中并从 shell 部分运行:

shell: "run.sh"

接下来,您可以使用 run 部分中的 shell 函数运行每个命令:

run:
    shell("""gunzip -c {input.ech} | NanoFilt -l {params.min_length} --maxlength {params.max_length} -s {input.summary} -q {params.q} --readtype {params.rd} | gzip > Working_space_ont/01_nanofilt/{wildcards.sample}_filt_{params.q}.fastq.gz""")
    shell("python3 reinit.py")
    # Implement your logic in Python
    ...

【讨论】:

  • 谢谢!它还没有解决,但它更好:) 第一个解决方案对我来说没问题,通过脚本外壳,我可以做循环。第二种解决方案,我试过了,但运行部分不允许 --use-conda 标志运行 NanoFilt .. 祝你有美好的一天,
猜你喜欢
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多