我有一个简单的python函数,它接受输入并创建输出
def enlarge_overlapping_region(input,output):
fi=open(input,"r")
fo=open(output,"w")
df = pd.read_table(fi, delimiter='t',header=None,names=["chr","start","end","point","score","strand","cdna_count","lib_count","region_type","region_id"])
df1 = (df.groupby('region_id', as_index=False)
.agg({'chr':'first', 'start':'min', 'end':'max','region_type':'first'})
[['chr','start','end','region_type','region_id']])
df1 = df1[df1.region_id != "."]
df1.to_csv(fo,index=False, sep='t')
return(df1)
我在一个规则中调用这个函数。但我无法访问该文件,我不知道为什么。
我试过这样的东西:
rule get_enlarged_dhs:
input:
"data/annotated_clones/{cdna}_paste_{lib}.annotated.bed"
output:
"data/enlarged_coordinates/{cdna}/{cdna}_paste_{lib}.enlarged_dhs.bed"
run:
lambda wildcards: enlarge_overlapping_region(f"{wildcards.input}",f"{wildcards.output}")
我得到了这个错误:
Missing files after 5 seconds:
data/enlarged_coordinates/pPGK_rep1/pPGK_rep1_paste_pPGK_input.enlarged_dhs.bed
This might be due to filesystem latency. If that is the case, consider to increase the wait time with --latency-wa
it.
如果我直接把python代码放进规则中,比如h:
rule get_enlarged_dhs:
input:
"data/annotated_clones/{cdna}_paste_{lib}.annotated.bed"
output:
"data/enlarged_coordinates/{cdna}/{cdna}_paste_{lib}.enlarged_dhs.bed"
run:
fi=open(input,"r")
fo=open(output,"w")
df = pd.read_table(fi, delimiter='t',header=None,names=["chr","start","end","point","score","strand","cdna_count","lib_count","region_type","region_id"])
df1 = (df.groupby('region_id', as_index=False)
.agg({'chr':'first', 'start':'min', 'end':'max','region_type':'first'})
[['chr','start','end','region_type','region_id']])
df1 = df1[df1.region_id != "."]
df1.to_csv(fo,index=False, sep='t')
我得到了这个错误:
expected str, bytes or os.PathLike object, not InputFiles
它可能比你想象的更简单:
lambda wildcards: enlarge_overlapping_region(f"{wildcards.input}",f"{wildcards.output}")
应为:
enlarge_overlapping_region(input[0], output[0])
同样,为了修复您尝试更改的第二个解决方案:
fi=open(input,"r")
fo=open(output,"w")
至
fi=open(input[0],"r")
fo=open(output[0],"w")
在我看来,为输入和输出文件分配名称并在run
或shell
指令中使用该名称不太容易出错。例如
rule get_enlarged_dhs:
input:
bed= "...",
output:
bed= "...",
run:
enlarge_overlapping_region(input.bed, output.bed)