Snakemake:本地机器上的SFTP

Snakemake : SFTP on local machine

我从我的本地计算机连接到远程服务器上的 ssh。
我 运行 我的 Snakemake 在远程服务器上。
我想将本地计算机上的文件用作规则的输入。
当然,由于我 运行 我的 Snakemake 在服务器上,服务器成为本地机器,本地机器成为远程机器(对于 Snakemake)。

from snakemake.remote.SFTP import RemoteProvider

# I am not sure about the private key, is it the one I have on the server ?
# I have the same result with or without private_key anyway

# SFTP = RemoteProvider(port=22, username="myusername", private_key="/path/to/.ssh/id_rsa")
SFTP = RemoteProvider(port=22, username="myusername")

configfile : "config.json"

localrules: copyBclLocalToCluster

rule all:
    input:
        "copycluster.txt"

rule copyBclLocalToCluster:
    input:
        SFTP.remote("adress:path/to/filelocal.txt")
    output:
        "copycluster.txt"
    shell:
        "scp {input} {output}"

-----------------------------------------
Building DAG of jobs...
MissingInputException in line 26 of /path/to/Snakefile:
Missing input files for rule copyBclLocalToCluster:
adress:path/to/filelocal.txt

https://snakemake.readthedocs.io/en/stable/snakefiles/remote_files.html
The remote file addresses used must be specified with the host (domain or IP address) and the absolute path to the file on the remote server. A port may be specified if the SSH daemon on the server is listening on a port other than 22, in either the RemoteProvider or in each instance of remote():

文档说端口不应该是端口 22,但为什么呢?我真的很想使用它,因为我不知道如何配置另一个端口,我什至不确定是否有权这样做。

真的是端口问题吗?或者我只是不明白如何将 SFTP 与 Snakemake 一起使用。

使用本地机器上的文件作为我的 snakemake 输入的最佳方法是什么?


编辑

这不是端口问题,我什至不需要指定它,因为它是端口 22。
我试图指定好的 ssh 私钥:

SFTP = RemoteProvider(port=22, username="myusername", private_key="/path/to/.ssh/id_rsa")
-----------------------------
Building DAG of jobs...
MissingInputException in line 26 of /path/to/Snakefile:
Missing input files for rule copyBclLocalToCluster:
adress:path/to/filelocal.txt

如果我在服务器上的控制台上尝试 sftp myusername@adress:path/to/filelocal.txt .,它工作正常。

为什么它在 snakemake 中不起作用?


编辑

当我尝试在 remoteProvider 中使用我的密码而不是 ssh-key 时,我遇到了同样的错误。

SFTP = RemoteProvider(port=22, username="myusername", password="mypassword")
--------------------------------
Building DAG of jobs...
MissingInputException in line 26 of /path/to/Snakefile:
Missing input files for rule copyBclLocalToCluster:
adress:path/to/filelocal.txt

我确定地址、用户名、密码、ssh-key 是正确的并且文件存在,我可以在 snakemake 之外完成它工作正常。


编辑

由于 RemoteProvider 使用 pysftp,我尝试在 python 脚本中使用 pysftp 复制相同的文件。

import pysftp
with pysftp.Connection(adress, 
                       username="myusername",
                       private_key_pass="/path/to/.ssh/id_rsa") as sftp:
    sftp.get(path/to/filelocal.txt, /path/on/cluster/fileCOPY.txt)

它工作正常,所以问题肯定来自我的 Snakefile。


编辑

RemoteProvider 还需要 ftputil,我在 python 脚本中尝试了 ftputil。

import ftputil
with ftputil.FTPHost("adress", "myusername", "mypassword") as ftp_host:
    print(getcwd())
    ftp_host.download(remote_path, local_path)
----------------------------------------------
Traceback (most recent call last):
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/site-packages/ftputil/host.py", line 129, in _make_session
    return factory(*args, **kwargs)
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/ftplib.py", line 117, in __init__
    self.connect(host)
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/ftplib.py", line 152, in connect
    source_address=self.source_address)
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "sftptest.py", line 16, in <module>
    with ftputil.FTPHost("adress", "myusername", "mypassword") as ftp_host:
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/site-packages/ftputil/host.py", line 69, in __init__
    self._session = self._make_session()
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/site-packages/ftputil/host.py", line 129, in _make_session
    return factory(*args, **kwargs)
  File "/work/username/miniconda3/envs/RNAseq_snakemake/lib/python3.6/site-packages/ftputil/error.py", line 146, in __exit__
    raise FTPOSError(*exc_value.args, original_exception=exc_value)
ftputil.error.FTPOSError: [Errno 111] Connection refused
Debugging info: ftputil 3.2, Python 3.6.7 (linux)

会不会是问题?但是我在snakemake中没有出现这种错误,只是缺少文件的错误。我不明白为什么 ftputil 不工作。

所以我在 Snakemake 的源代码中进行了快速搜索,发现 Snakemake 使用 ftputil 需要用户名和密码。当您不提供 Snakemake 的 ssh-key 路径时,此密码将默认为 None,然后传递给 ftputil.

Snakemake source

我同意默认行为应该默认为更明智的行为,例如 ~/.ssh/id_rsa,但不幸的是它没有。

当您在控制台上使用 SFTP 时,您必须编写

sftp myusername@adress:/path/to/file .

但是在Snakemake的远程功能中你应该删除主机和文件路径之间的“:”。
我被 SFTP 语法误导了,但它在 snakemake 文档中写得很好

# example from snakemake doc
SFTP.remote("example.com/path/to/file.bam")

# what I was doing badly
SFTP.remote("adress:path/to/filelocal.txt")

正确的命令是:

from snakemake.remote.SFTP import RemoteProvider
SFTP = RemoteProvider(port=22, username="myusername", password="mypassword")
rule all:
    input:
        # "/" instead of ":" between host and the path of the file
        SFTP.remote("adress/path/to/filelocal.txt")