# HG changeset patch # User goeckslab # Date 1740593374 0 # Node ID 0b8423c7ee3e37a732a0cf50cfff9684f3c3f7bf # Parent ef68bc2a4dbcabb96467b2993fcaad2b4f59fa12 planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338 diff -r ef68bc2a4dbc -r 0b8423c7ee3e ashlar.xml --- a/ashlar.xml Fri Feb 09 22:48:46 2024 +0000 +++ b/ashlar.xml Wed Feb 26 18:09:34 2025 +0000 @@ -1,4 +1,4 @@ - + Alignment by Simultaneous Harmonization of Layer/Adjacency Registration macros.xml @@ -81,6 +81,12 @@ $adv.flip_mosaic_y -o registered.ome.tif; + + #if $rename.decide == "do_rename" + python3 '${__tool_directory__}/rename_channels.py' + --image registered.ome.tif + --markers '$rename.markers_file'; + #end if ]]> @@ -90,6 +96,17 @@ + + + + + + + + + + +
@@ -117,6 +134,28 @@ + + + + + + + + + + + + + + + + + + + + + + 1.18.0 - 0 + 1 ashlar diff -r ef68bc2a4dbc -r 0b8423c7ee3e rename_channels.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rename_channels.py Wed Feb 26 18:09:34 2025 +0000 @@ -0,0 +1,81 @@ +# ------------------------------------------------------------------------------------ +# Stripped down and modified from: +# https://github.com/ohsu-comp-bio/ashlar/blob/master/pyramid_upgrade.py +# ------------------------------------------------------------------------------------ + +import argparse +import csv +import xml.etree.ElementTree + +from tifffile import tiffcomment + + +def fix_attrib_namespace(elt): + """Prefix un-namespaced XML attributes with the tag's namespace.""" + # This fixes ElementTree's inability to round-trip XML with a default + # namespace ("cannot use non-qualified names with default_namespace option" + # error). 7-year-old BPO issue here: https://bugs.python.org/issue17088 + # Code inspired by https://gist.github.com/provegard/1381912 . + if elt.tag[0] == "{": + uri, _ = elt.tag[1:].rsplit("}", 1) + new_attrib = {} + for name, value in elt.attrib.items(): + if name[0] != "{": + # For un-namespaced attributes, copy namespace from element. + name = f"{{{uri}}}{name}" + new_attrib[name] = value + elt.attrib = new_attrib + for child in elt: + fix_attrib_namespace(child) + + +def main(image_fh, marker_file): + """ + Parameters + --------- + image_fh : str + File path to the OME Tiff image. + marker_file : str + File path to CSV containing marker name information. + """ + + # parse marker file, create list of new marker names + new_channel_names = [] + with open(marker_file, newline='') as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + new_channel_names.append(row['marker_name']) + + # read OME-XML metadata and parse down to channels + xml_ns = {"ome": "http://www.openmicroscopy.org/Schemas/OME/2016-06"} + root = xml.etree.ElementTree.fromstring(tiffcomment(image_fh)) + image = root.find("ome:Image", xml_ns) + pixels = image.find("ome:Pixels", xml_ns) + channels = pixels.findall("ome:Channel", xml_ns) + + # name channels + for channel, name in zip(channels, new_channel_names): + channel.attrib["Name"] = name + + # encode new xml and set image metadata + fix_attrib_namespace(root) + new_ome_xml = xml.etree.ElementTree.tostring( + root, + encoding='utf-8', + xml_declaration=True, + default_namespace=xml_ns["ome"]) + + tiffcomment(image_fh, comment=new_ome_xml) + + print("Updated OME-TIFF metadata:") + print(tiffcomment(image_fh)) + + +if __name__ == '__main__': + aparser = argparse.ArgumentParser() + aparser.add_argument("-i", "--image", dest="image", required=True) + aparser.add_argument("-m", "--markers", dest="markers", required=True) + + args = aparser.parse_args() + + main(args.image, args.markers) diff -r ef68bc2a4dbc -r 0b8423c7ee3e test-data/ashlar_test_markers.csv --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/ashlar_test_markers.csv Wed Feb 26 18:09:34 2025 +0000 @@ -0,0 +1,3 @@ +round,channel,marker_name +0,0,DAPI +0,1,CD3 \ No newline at end of file