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.