根据输入文件的名称使用不同的 shell 命令
Use different shell commands depending on the name of an input file
我正在尝试使用 Salmon 比对 RNA-seq 数据,但是由于我使用的是多种小鼠品系,因此所需的基因组索引将取决于小鼠品系。我将这些信息保存在文件名中,所以我的想法是,如果输入文件有一个特定的名称,那么应该使用某个索引,否则应该使用另一个索引。
这个脚本运行完美,returns 我需要的所有文件,但是,它总是在 else 语句中使用 shell 命令,并且完全忽略 if 语句,尽管文件名在技术上是相同的。
我尝试过各种方法但都没有成功,所以我也想知道最好的方法(不一定是最通用的方法,因为这个管道只供我使用)来完成我想要的。
代码:
rule salmon_mapping:
input:
fastq="data/raw_data/{sample}.fastq.gz",
c57_index="/gpfs/data01/glasslab/home/cag104/refs/Mus_musculus/Ensemble/C57/Salmon_Index/",
aj_index="/gpfs/data01/glasslab/home/cag104/refs/Mus_musculus/Ensemble/AJ/Salmon_Index/"
log:
"logs/salmon/{sample}_quant.log"
params:
prefix="{sample}"
output:
"data/processed_data/{sample}/quant.sf",
"data/processed_data/{sample}/cmd_info.json"
run:
if {input.fastq} == "data/raw_data/mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_1_VML_s20180131_GTCCGC.fastq.gz" or {input.fastq} == "data/raw_data/mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_2_VML_s20180131_GTGAAA.fastq.gz":
shell("salmon quant -p 16 -i {input.aj_index} -l A -r <(gunzip -c {input.fastq}) -o data/processed_data/{wildcards.sample} &> {log}")
else:
shell("salmon quant -p 16 -i {input.c57_index} -l A -r <(gunzip -c {input.fastq}) -o data/processed_data/{wildcards.sample} &> {log}"
将 if
语句中的 {input.fastq}
替换为 input.fastq
,但不替换 shell
语句中的 {input.fastq}
。在你的例子中, input.fastq
是一个字符串,这是你想要的,而 {input.fastq}
使它成为一个 python set
对象。
使用根据通配符定义规则输入的函数。
def select_fastq_and_index(wildcards):
fq = "data/raw_data/{sample}.fastq.gz".format(sample = wildcards["sample"])
idx = ""
if wildcards["sample"] in [
"mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_1_VML_s20180131_GTCCGC",
"mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_2_VML_s20180131_GTGAAA"
]:
idx = foo
else:
idx = bar
return fq, idx
然后重写你的规则:
rule salmon_mapping:
input:
select_fastq_and_index
log:
"logs/salmon/{sample}_quant.log"
params:
prefix="{sample}"
output:
"data/processed_data/{sample}/quant.sf",
"data/processed_data/{sample}/cmd_info.json"
shell: """
salmon quant -p 16 -i {input[1]} -l A \
-r <(gunzip -c {input[0]}) \
-o data/processed_data/{wildcards.sample} \
&> {log}
"""
我正在尝试使用 Salmon 比对 RNA-seq 数据,但是由于我使用的是多种小鼠品系,因此所需的基因组索引将取决于小鼠品系。我将这些信息保存在文件名中,所以我的想法是,如果输入文件有一个特定的名称,那么应该使用某个索引,否则应该使用另一个索引。
这个脚本运行完美,returns 我需要的所有文件,但是,它总是在 else 语句中使用 shell 命令,并且完全忽略 if 语句,尽管文件名在技术上是相同的。
我尝试过各种方法但都没有成功,所以我也想知道最好的方法(不一定是最通用的方法,因为这个管道只供我使用)来完成我想要的。
代码:
rule salmon_mapping:
input:
fastq="data/raw_data/{sample}.fastq.gz",
c57_index="/gpfs/data01/glasslab/home/cag104/refs/Mus_musculus/Ensemble/C57/Salmon_Index/",
aj_index="/gpfs/data01/glasslab/home/cag104/refs/Mus_musculus/Ensemble/AJ/Salmon_Index/"
log:
"logs/salmon/{sample}_quant.log"
params:
prefix="{sample}"
output:
"data/processed_data/{sample}/quant.sf",
"data/processed_data/{sample}/cmd_info.json"
run:
if {input.fastq} == "data/raw_data/mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_1_VML_s20180131_GTCCGC.fastq.gz" or {input.fastq} == "data/raw_data/mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_2_VML_s20180131_GTGAAA.fastq.gz":
shell("salmon quant -p 16 -i {input.aj_index} -l A -r <(gunzip -c {input.fastq}) -o data/processed_data/{wildcards.sample} &> {log}")
else:
shell("salmon quant -p 16 -i {input.c57_index} -l A -r <(gunzip -c {input.fastq}) -o data/processed_data/{wildcards.sample} &> {log}"
将 if
语句中的 {input.fastq}
替换为 input.fastq
,但不替换 shell
语句中的 {input.fastq}
。在你的例子中, input.fastq
是一个字符串,这是你想要的,而 {input.fastq}
使它成为一个 python set
对象。
使用根据通配符定义规则输入的函数。
def select_fastq_and_index(wildcards):
fq = "data/raw_data/{sample}.fastq.gz".format(sample = wildcards["sample"])
idx = ""
if wildcards["sample"] in [
"mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_1_VML_s20180131_GTCCGC",
"mouse_AJ_M_Liver_Hepatocyte_RNA_notx_CG_2_VML_s20180131_GTGAAA"
]:
idx = foo
else:
idx = bar
return fq, idx
然后重写你的规则:
rule salmon_mapping:
input:
select_fastq_and_index
log:
"logs/salmon/{sample}_quant.log"
params:
prefix="{sample}"
output:
"data/processed_data/{sample}/quant.sf",
"data/processed_data/{sample}/cmd_info.json"
shell: """
salmon quant -p 16 -i {input[1]} -l A \
-r <(gunzip -c {input[0]}) \
-o data/processed_data/{wildcards.sample} \
&> {log}
"""