snakemake在python函数中使用通配符输入/输出

问题描述 投票:0回答:1

我有一个简单的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代码放入诸如thath之类的规则中:

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
python path snakemake
1个回答
0
投票

可能比您想象的要简单:

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")

我认为,为输入和输出文件分配名称并在runshell指令中使用该名称不太容易出错。例如:

rule get_enlarged_dhs:
    input:
        bed= "...",
    output:
        bed= "...",
    run:
        enlarge_overlapping_region(input.bed, output.bed)
© www.soinside.com 2019 - 2024. All rights reserved.