如何解决反复运行snakemake规则
所以我以为我终于掌握了snakemake,但是当尝试运行多个不同的数据文件时,我意识到它仍然无法正常工作。这是Snakefile:
import pandas as pd
configfile: "config.json"
experiments = pd.read_csv(config["experiments"],sep = '\t')
experiments['Name'] = [filename.split('/')[-1].split('.fa')[0] for filename in experiments['Files']]
rule all:
input:
expand("{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq",output = config["output"],fr = (['_forward_paired','_reverse_paired'] if experiments["Files"].str.contains(',').tolist() else ''),name = experiments['Name'])
rule preprocess:
input:
experiments["Files"].str.split(',')
output:
expand("{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq",name = experiments['Name'])
threads:
config["threads"]
run:
shell("python preprocess.py -i {reads} -t {threads} -o {output} -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases -d {data_type}",data_type = experiments["Data type"].tolist(),reads = ",".join(input))
这是配置文件:
{
"output": "test_snakemake","threads": 14,"experiments": "experiments.tsv"
}
这是实验文件
Files Sample Data type Condition
path/to/mg_R1.fastq,path/to/mg_R2.fastq Sample dna
path/to/a/0.01/mt_0.01a_R1.fastq,path/to/a/0.01/mt_0.01a_R2.fastq Sample rna c1
path/to/b/0.01/mt_0.01b_R1.fastq,path/to/b/0.01/mt_0.01b_R2.fastq Sample rna c1
path/to/c/0.01/mt_0.01c_R1.fastq,path/to/c/0.01/mt_0.01c_R2.fastq Sample rna c1
path/to/a/1/mt_1a_R1.fastq,path/to/a/1/mt_1a_R2.fastq Sample rna c2
path/to/b/1/mt_1b_R1.fastq,path/to/b/1/mt_1b_R2.fastq Sample rna c2
path/to/c/1/mt_1c_R1.fastq,path/to/c/1/mt_1c_R2.fastq Sample rna c2
path/to/a/100/mt_100a_R1.fastq,path/to/a/100/mt_100a_R2.fastq Sample rna c3
path/to/b/100/mt_100b_R1.fastq,path/to/b/100/mt_100b_R2.fastq Sample rna c3
path/to/c/100/mt_100c_R1.fastq,path/to/c/100/mt_100c_R2.fastq Sample rna c3
我想做的是有预处理规则分别对待每一行。我认为这就是shell解释命令的方式,它将运行命令python preprocess.py -i path/to/mg_R1.fastq,path/to/mg_R2.fastq -t 14 -o test_snakemake -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases -d dna
,而是尝试连接所有行并同时python preprocess.py -i path/to/mg_R1.fastq,path/to/mg_R2.fastq,path/to/a/0.01/mt_0.01a_R1.fastq,path/to/a/0.01/mt_0.01a_R2.fastq,path/to/b/0.01/mt_0.01b_R1.fastq,path/to/b/0.01/mt_0.01b_R2.fastq,... -t 14 -o test_snakemake -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases -d dna rna rna rna rna rna rna rna rna rna
对所有示例运行它。
我如何让snakemake分别考虑每一行?
解决方法
这是一个非常常见的错误。要记住的是,规则应适用于单个样本。 Snakemake将使用通配符(使用通配符)进行操作,并根据规则生成特定的作业。您编写的东西要包含所有输入和所有输出,然后我想,preprocess.py希望有一个输入/输出。
而是一次考虑一个文件。对于输出"{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq"
,如何生成该文件?您必须使用名称作为关键字来匹配实验数据框中的输入文件。
def preprocess_input(wildcards):
# get files with matching names
df = experiments.loc[experiments['Name'] == wildcards.name,'Files']
# get first value (in case multiple) and split on commas
return df.iloc[0].split(',')
rule preprocess:
input:
preprocess_input
output:
"{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq"
threads:
config["threads"]
shell:
'python preprocess.py -i {reads} -t {threads} -o {config[output]} ...'
使用输入功能从输出文件中查找正确的输入文件。这不是完美的方法,但应该可以指引您正确的方向。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。