python - 如何在没有任何路径或环境相关问题的情况下通过 nextflow 无缝地在 docker 容器中运行 python 脚本?

标签 python bash docker aws-batch nextflow

我正在尝试使用 nextflow 和 docker 运行 python 脚本。我正在使用 dockerfile(如下所示)来创建 docker 镜像。 Nextflow 脚本有一个简单的 python 脚本启动。问题是当我从 docker 容器中(在交互模式下)运行相同的 python 命令时,它工作正常。但是当我使用带有 docker 容器的 nextflow 启动它时,它会抛出错误。

docker 文件:

#!/usr/local/bin/docker
# -*- version: 20.10.2 -*-

############################################
## MULTI-STAGE CONTAINER CONFIGURATION ##
FROM python:3.6.2
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    software-properties-common \
    unzip \
    curl
RUN wget -O- https://apt.corretto.aws/corretto.key | apt-key add - && \
    add-apt-repository 'deb https://apt.corretto.aws stable main' && \
    apt-get update && \
    apt-get install -y java-1.8.0-amazon-corretto-jdk


############################################
## PHEKNOWLATOR (PKT_KG) PROJECT SETTINGS ##
# create needed project directories
WORKDIR /PKT
RUN mkdir -p /PKT
RUN mkdir -p /PKT/resources
RUN mkdir -p /PKT/resources/construction_approach
RUN mkdir -p /PKT/resources/edge_data
RUN mkdir -p /PKT/resources/knowledge_graphs
RUN mkdir -p /PKT/resources/node_data
RUN mkdir -p /PKT/resources/ontologies
RUN mkdir -p /PKT/resources/processed_data
RUN mkdir -p /PKT/resources/relations_data

# copy scripts/files needed to run pkt_kg
COPY pkt_kg /PKT/pkt_kg
COPY Main.py /PKT
COPY setup.py /PKT
COPY README.rst /PKT
COPY resources /PKT/resources

# download and copy needed data
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/edge_source_list.txt && mv edge_source_list.txt resources/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/ontology_source_list.txt && mv ontology_source_list.txt resources/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/resource_info.txt && mv resource_info.txt resources/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/subclass_construction_map.pkl && mv subclass_construction_map.pkl resources/construction_approach/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/PheKnowLator_MergedOntologies.owl && mv PheKnowLator_MergedOntologies.owl resources/knowledge_graphs/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/node_metadata_dict.pkl && mv node_metadata_dict.pkl resources/node_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/DISEASE_MONDO_MAP.txt && mv DISEASE_MONDO_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/ENSEMBL_GENE_ENTREZ_GENE_MAP.txt && mv ENSEMBL_GENE_ENTREZ_GENE_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/ENTREZ_GENE_PRO_ONTOLOGY_MAP.txt && mv ENTREZ_GENE_PRO_ONTOLOGY_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/GENE_SYMBOL_ENSEMBL_TRANSCRIPT_MAP.txt && mv GENE_SYMBOL_ENSEMBL_TRANSCRIPT_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/HPA_GTEx_TISSUE_CELL_MAP.txt && mv HPA_GTEx_TISSUE_CELL_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/MESH_CHEBI_MAP.txt && mv MESH_CHEBI_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/PHENOTYPE_HPO_MAP.txt && mv PHENOTYPE_HPO_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/STRING_PRO_ONTOLOGY_MAP.txt && mv STRING_PRO_ONTOLOGY_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/UNIPROT_ACCESSION_PRO_ONTOLOGY_MAP.txt && mv UNIPROT_ACCESSION_PRO_ONTOLOGY_MAP.txt resources/processed_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/INVERSE_RELATIONS.txt && mv INVERSE_RELATIONS.txt resources/relations_data/
RUN curl -O https://storage.googleapis.com/pheknowlator/current_build/data/processed_data/RELATIONS_LABELS.txt && mv RELATIONS_LABELS.txt resources/relations_data/

# install needed python libraries
RUN pip install --upgrade pip setuptools
WORKDIR /PKT
RUN pip install .


############################################
## GLOBAL ENVRIONMENT SETTINGS ##
# copy files needed to run docker container
COPY entrypoint.sh /PKT

# update permissions for all files
RUN chmod -R 755 /PKT

# set OWlTools memory (set to a high value, system will only use available memory)
ENV OWLTOOLS_MEMORY=500g
RUN echo $OWLTOOLS_MEMORY

# set python envrionment encoding
RUN export PYTHONIOENCODING=utf-8

docker 镜像的名称 -- pkt:2.0.0

Nextflow 脚本:

process run_PKTBaseRun{

echo True

container 'pkt:2.0.0'
publishDir "${params.outDir}", mode: 'copy'

output:
file '*' into output_ch

script:
"""
which python
$PWD
pwd
python /PKT/Main.py --onts /PKT/resources/ontology_source_list.txt \
            --edg /PKT/resources/edge_source_list.txt \
            --res /PKT/resources/resource_info.txt \
            --out /PKT/resources/knowledge_graphs --app subclass --kg full --nde yes --rel yes --owl no
"""


}

现在当我执行时:

nextflow run main.nf

然后这给出了与 glob.glob 模块相关的错误,因为它没有列出文件,因为它必须在 docker 容器中。

但是,当我在 docker 容器中简单地运行上面的 python 代码时,它运行起来很顺利。

> docker run -it pkt:2.0.0 /bin/bash

/PKT> python Main.py --onts resources/ontology_source_list.txt \
            --edg resources/edge_source_list.txt \
            --res resources/resource_info.txt \
            --out resources/knowledge_graphs --app subclass --kg full --nde yes --rel yes --owl no

只有当我将 nextflow 与 docker 结合使用时,这段代码才会抛出错误。 我已确保使用的 python 是容器内的 python。

问题:

  1. 有什么想法/想法可以让它发挥作用吗?

有趣的是,
which python 的输出 --> 容器内的 python
但是,
$PWD 的输出 --> 启动 nextflow 的目录
pwd 的输出 --> nextflow 的工作目录

  1. 当我们在nextflow进程中添加容器时,nextflow进程(run_PKTBaseRun)中的命令不是从容器workdir运行的吗?因此pwd的值不应该是容器workdir的值而不是nextflow workdir吗?<

所有必需的文件都已添加到 docker 镜像中。

  1. 有没有办法确保 nextflow 进程中脚本部分中的命令从 docker root/workdir 运行?

这个 nextflow 和 docker 背后的想法是最终使用 awscli 在 aws batch 上运行它。但是在aws batch上运行之前,要确保它在本地服务器上运行良好。

期待您的建议和想法。谢谢。

最佳答案

尝试转义 \$PWD,这将为您提供安装在 docker 中的 nextflow 进程工作目录。我很好奇你是否通过其他方式解决了它?

尝试在 nextflow 流程脚本中运行它。

export pdir=\$PWD
echo \$pdir

关于python - 如何在没有任何路径或环境相关问题的情况下通过 nextflow 无缝地在 docker 容器中运行 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66425132/

相关文章:

python - 代码 8 : Loading a plug-in failed

python - 从正则表达式中提取文本

linux - 自动将输入提供给 Linux 命令行

python - 无法打开需求文件 : [Errno 2] No such file or directory: 'requirements.txt'

Docker Swarm 与 etcd

docker - docker-compose建立和建立

python - 从 Windows 主机在远程 Linux 机器上运行部分 Python 单元测试

python - Tensorflow 中的默认变量初始值设定项是什么?

windows - 在 Cygwin 中获取 Windows 版本

regex - 如何在 Perl 中用不同的随机值多次替换同一个字符串?