简体   繁体   中英

Snakemake use wildcards in path of python function

I have a simple function that read a file (one line) and get the first element after split.

def get_wc(wc):
    file = open(wc,"r")
    normalization_value = file.readline().split(' ')[0]
    return(normalization_value)

I use this function in a rule snakemake.

rule compute_fc:
input:
    "data/annotated_clones/{cdna}_paste_{lib}.annotated.bed"
output:
    "data/fold_change/{cdna}_paste_{lib}.fc.bed"
params:
    size_cdna=get_wc("data/wc_bed/cdna/{cdna}.wc.txt"),
    size_lib=get_wc("data/wc_bed/library/{lib}.wc.txt")
shell:'''
    awk -v cdna1={params.size_cdna} -v inp={params.size_lib} -v addon=1 -v FS='\t' -v OFS='\t' '/^chr/{{ratio=(($7+addon)/($8+addon))*(inp/cdna1);print $0,ratio}}' {input} > {output}
'''

I'm trying to get the value that get_wc function return and use it as params in snakemake rule.

But Snakemake don't get the path with the wildcards so it try to get the path with the wildcards and obviously it don't works.

[Errno 2] No such file or directory: 'data/wc_bed/cdna/{cdna}.wc.txt'

I think using a lambda function, like this, should work:

params:
    size_cdna = lambda wildcards: get_wc(f"data/wc_bed/cdna/{wildcards.cdna}.wc.txt")
    size_lib = lambda wildcards: get_wc(f"data/wc_bed/library/{wildcards.lib}.wc.txt")

Take a look here for the docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM