python Snakemake从两个通道中减去一个遮罩

s4n0splo  于 2023-03-16  发布在  Python
关注(0)|答案(1)|浏览(111)

我有一个项目需要处理几个显微镜记录。记录有两个通道(红色和绿色),所以结构是这样的:

rec1/Ch0/video.tif
rec1/Ch1/video.tif
rec2/Ch0/video.tif
rec2/Ch1/video.tif
etc..

我需要做几个操作,其中大部分都是在两个通道上进行的。例如,裁剪、模糊等。但是我需要只在红色通道(Ch 0)上创建一个蒙版,并在两个通道上应用该蒙版来测量蒙版内部的强度。我该如何做呢?
结果应如下所示:

rec1/Ch0/video.tif
rec1/Ch0/video_cropped_blurred.tif
rec1/Ch0/video_cropped_blurred_mask.tif
rec1/Ch0/video_cropped_blurred_mask_measurements.csv

rec1/Ch1/video.tif
rec1/Ch1/video_cropped_blurred.tif
rec1/Ch1/video_cropped_blurred_mask_measurements.csv

rec2/Ch0/video.tif
rec2/Ch0/video_cropped_blurred.tif
rec2/Ch0/video_cropped_blurred_mask.tif
rec2/Ch0/video_cropped_blurred_mask_measurements.csv

rec2/Ch1/video.tif
rec2/Ch1/video_cropped_blurred.tif
rec2/Ch1/video_cropped_blurred_mask_measurements.csv
etc..

到目前为止我有:

import glob

RED_CHANNEL = glob.glob("rec*/Ch0/")
GREEN_CHANNEL = glob.glob("rec*/Ch1/")

rule targets:
    input:
        red_blur = expand("{red_datasets}video_blurred.tif", red_datasets=RED_CHANNEL),
        green_blur = expand("{green_datasets}video_blurred.tif", green_datasets=GREEN_CHANNEL),
        red_mask  = expand("{red_datasets}video_blurred_mask.tif", red_datasets=RED_CHANNEL),

rule red_blur:
    input:
        raw_img ="{red_datasets}video.tif"
    output:
        blurred_img = "{red_datasets}video_blurred.tif"
    shell:
        "python blur.py -i {input.raw_img} -o {output.blurred_img}"

rule green_blur:
    input:
        raw_img ="{green_datasets}video.tif"
    output:
        blurred_img = "{green_datasets}video_blurred.tif"
    shell:
        "python blur.py -i {input.raw_img} -o {output.blurred_img}"

但我得到了以下错误:

Rules green_blur and red_blur are ambiguous for the file ../Ch0/video.tif
Consider starting rule output with a unique prefix, constrain your wildcards, or use the ruleorder directive.
Wildcards:
        green_blur: green_datasets=../Ch0/
        red_subtract_background: red_datasets=../Ch0/

为什么绿色数据集通配符也会得到Ch 0?我可能只有一条模糊规则,但如何创建一个规则掩码,用Ch 0掩码屏蔽Ch 1?如何访问文件rec*/Ch 0/video_cripped_blurred_mask.tif以获取适用于Ch 1的规则?
换句话说,我如何制定这条规则?

rule mask:
    input: 
        img = "{green_datasets}_cropped_blurred.tif",
        mask = "{red_datasets}_cropped_blurred_mask.tif"
    output:
        measurements = "{green_datasets}_mask_measurements.csv"
    shell:
       "python measure.py -i {input.img} -mask {input.mask} -o {output.measurements}"

非常感谢!

yx2lnoni

yx2lnoni1#

与@euronion类似,红色和绿色的通配符就是通配符。它们的名字并不重要,实际上被.+正则表达式所取代。但这也赋予了一些你没有利用的功能,即使用一个通用通配符的规则。下面是对你的工作流的重写:

red_channel, green_channel = 0, 1  # ideally set in config

# get recordings for ch0, will validate ch1 exists when requesting all outputs
recordings = glob_wildcards('rec{recording}/Ch0/video.tif').recording

rule targets:  # need only request final output
    input:
        expand('rec{recording}/Ch{channel}/video_mask_measurements.csv',
               recording=recordings,
               channels=[red_channel, green_channel],
              )

rule blur:  # this can be generic for any channel
    input:
        raw_img ="{base}video.tif"  # wildcard can be anything
    output:
        blurred_img = "{base}video_blurred.tif"
    shell:
        "python blur.py -i {input.raw_img} -o {output.blurred_img}"

rule create_mask:
    input: 
        img = "rec{recording}/Ch{channel}/video_blurred.tif",
    output:
        mask = "rec{recording}/Ch{channel}/video_mask.tif",
    shell:
       "python make_mask.py -i {input.img} -o {output}"

rule use_mask:  # this is also generic
    input: 
        img = "rec{recording}/Ch{channel}/video_blurred.tif",
        mask = expand("rec{recording}/Ch{channel}/video_mask.tif",
                     channel=red_channel,  # but the mask can only be red
                     allow_missing=True),
    output:
        measurements = "rec{recording}/Ch{channel}/video__mask_measurements.csv"
    shell:
       "python measure.py -i {input.img} -mask {input.mask} -o {output.measurements}"

blurcreate_maskuse_mask对于任一通道都是通用的。但是,use_mask仅 * 请求 * red_channel的掩码,因此这是唯一运行的掩码。
根据你的文件名,我错过了一个裁剪步骤,但这应该解决通道问题。

相关问题