對蛇類完全陌生,所以請多多包涵。我花了相當多的時間來搜索類似的問題,但沒有任何運氣。
我想創建一個規則,將某些檔案從包含所有檔案的檔案夾復制到新目錄。
我要復制的檔案的檔案名列在一個文本檔案中(每行一個檔案名)。我撰寫了一個小 bash 腳本,使用cat和xargs將文本檔案中列出的檔案名復制到目標目錄。這個腳本作業正常!
如何告訴snakemake我的輸出應該是文本檔案中列出的目標目錄 檔案名?
好的,所以我最初的想法是在蛇檔案中創建一個串列,其中包含應該復制的檔案的所有目標路徑。
我把這個熱垃圾弄得一團糟(對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}"
uj5u.com熱心網友回復:
但是,snakemake 指出我缺少該規則的輸入檔案。
cp_from規則中的變數可能cp_files_to_target_dir不包含正確的路徑。要除錯,我建議將它移到規則之外并列印它以查看它包含的內容。例如
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}"
總的來說,我認為您的腳本可以整理一下,但如果沒有更多背景關系,我無法更具體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/514442.html
標籤:重击文件文本复制蛇制造
