对snakemake来说是全新的,所以请耐心等待。我花了相当多的时间来搜索类似的问题,但没有任何运气。
我想创建一个规则,将某些文件从包含所有文件的文件夹复制到新目录。
我要复制的文件的文件名列在一个文本文件中(每行一个文件名(。我已经编写了一个小型bash脚本,使用cat
和xargs
将文本文件中列出的文件名复制到目标目录。这个剧本很好用!
如何告诉snakemake我的输出应该是目标目录+文本文件中列出的文件名?
好的,所以我最初的想法是在snakefile中创建一个列表,其中包含应该复制的文件的所有目标路径。
我把这个热垃圾搞得一团糟(对python来说也是全新的(:
import glob
all_files = glob.glob("path/to/all/files", recursive = False)
file = open("path/to/file/list_of_files.txt", "r")
file_lines = file.readlines()
path_to_target_dir = "some/path/"
path_lines = [path_to_target_dir + str(x) for x in file_lines]
# for some reason path_lines end with line break after each filename. not good.
# remove line break to yield correct paths + filenames
list_of_correct_paths = []
for element in path_lines:
list_of_correct_paths.append(element.strip())
这将生成一个列表,其中包含要复制文件的所有路径。
rule cp_files_to_target_dir:
input:
cp_from = expand("path/to/all/files/{id}", id = all_files),
list = "list_of_files.txt",
script = "bash_script.sh"
output:
cp_to = expand("{path}", path = list_of_correct_paths)
shell:
"{input.script}"
不过,snakemake声明我缺少规则的输入文件。
我希望我的问题有道理。我很感激能得到的任何帮助。
编辑:这现在工作
import os
# filenames for all files
files = os.listdir("/path/to/all/files")
# create paths to files of interest from text file
text_file = open("path/to/text_file.txt", "r")
list_files = text_file.read().splitlines()
target_path = "path/to/target/dir"
target_file_paths = [target_path + str(x) for x in list_files]
这将生成一个列表,其中包含要复制文件的所有路径。
rule cp_files_to_target_dir:
input:
cp_from = expand("path/to/all/files/{id}", id = list_files),
list = "text_file.txt",
script = "bash_script.sh"
output:
cp_to = expand("{path}", path = target_file_paths)
shell:
"{input.script}"
但是,snakemake声明我缺少规则的输入文件。
规则cp_files_to_target_dir
中的变量cp_from
可能不包含正确的路径。要进行调试,我建议将它移到规则之外,然后打印它以查看它包含的内容。例如
cp_from = expand("path/to/all/files/{id}", id = all_files),
cp_to = expand("{path}", path = list_of_correct_paths)
# To debug:
print(cp_from) # Check these are what you expect
print(cp_to)
rule cp_files_to_target_dir:
input:
cp_from = cp_from,
list = "list_of_files.txt",
script = "bash_script.sh"
output:
cp_to = cp_to,
shell:
"{input.script}"
总的来说,我认为你的剧本可以整理一下,但如果没有更多的上下文,我就不能更具体了。