view rename_tiff_channels.xml @ 6:4763362914f4 draft default tip

planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit 5c324d06ad54dca76aa2b4c8118df5654f49da66
author goeckslab
date Mon, 15 Jul 2024 21:37:39 +0000
parents 61fd94c7ce42
children
line wrap: on
line source

<tool id="rename_tiff_channels" name="Rename OME-TIFF channels" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
    <description>with ome-types</description>
    <macros>
        <import>macros.xml</import>
    </macros>

    <edam_operations>
        <edam_operation>operation_3443</edam_operation>
    </edam_operations>

    <expand macro="requirements"/>

    <stdio>
        <regex match=".*XMLSyntaxError.*"
               source="stderr"
               level="fatal"
               description="XML metadata does not adhere to OME format. Considering converting image to OME-TIFF" />
    </stdio>

    <expand macro="version_cmd"/>

    <command detect_errors="aggressive"><![CDATA[

        cp '$image' ./renamed_image.ome.tiff && 

        ln -s '$channel_csv' ./channels.csv &&

        python '$script'

    ]]></command>
    <configfiles>
        <configfile name = "script">
import os
import sys 
import argparse
from itertools import cycle
import pandas as pd
import ome_types
from ome_types.model import channel, Color
from tifffile import tiffcomment

# setting default colors to match avivator
colors = [
    (0,0,255,1.0),     # blue
    (0,255,0,1.0),     # green
    (255,0,255,1.0),   # magenta
    (255,255,0,1.0),   # yellow
    (255,127,0,1.0),   # orange
    (0,255,255,1.0),   # cyan
    (255,255,255,1.0), # white
    (255,0,0,1.0)      # red
]

# convert to repeatable object of primitive int colors 
color_cycle = cycle([Color(c).as_int32() for c in colors])

cwd = os.getcwd()

channels_df = pd.read_csv(os.path.join(cwd, 'channels.csv'))

original_ome_xml = tiffcomment(os.path.join(cwd, 'renamed_image.ome.tiff'))
working_ome = ome_types.from_xml(original_ome_xml)

for l_idx, level in enumerate(working_ome.images):

    for c_idx, channel in enumerate(level.pixels.channels):

        new_name = channels_df.loc[c_idx, 'marker_name']
        channel.name = new_name
        channel.color = next(color_cycle)

        if l_idx == 0:

            print(f"Channel {c_idx} renamed to {new_name}")

updated_ome_xml = working_ome.to_xml().encode()
tiffcomment(os.path.join(cwd, 'renamed_image.ome.tiff'), updated_ome_xml)

print("Updated OME-TIFF metadata:")
print(tiffcomment(os.path.join(cwd, 'renamed_image.ome.tiff')))
        </configfile>
    </configfiles>
    <inputs>
        <param name="image" type="data" format="ome.tiff" label="Input image in OME-tiff format"/>
        <param name="channel_csv" type="data" format="csv" label="Channel Metadata CSV"/>
    </inputs>
    <outputs>
        <data name="renamed_image" format="ome.tiff" from_work_dir="renamed_image.ome.tiff" label="${tool.name} on ${on_string}"/>
    </outputs>
    <tests>
        <test>
            <param name="image" value="rename_test.ome.tiff" />
            <param name="channel_csv" value="rename_channels.csv" />
            <output name="renamed_image" ftype="ome.tiff">
                <assert_contents>
                    <has_size value="900000" delta="50000" />
                </assert_contents>
            </output>
            <assert_stdout>
                <has_text text="Channel 0 renamed to Hematoxylin" />
                <has_text text="Updated OME-TIFF metadata" />
                <has_text text="µm" />
            </assert_stdout>
        </test>
    </tests>
    <help><![CDATA[
    Given an OME.TIFF image and channel metadata CSV file, rename the image channels in the 
    OME XML metadata. CSV file must contain column 'marker_name' which will populate the XML
    metadata with channel names in the order provided in the CSV file. 
    ]]></help>
    <expand macro="citations" />
</tool>