annotate rename_channels.py @ 4:0b8423c7ee3e draft default tip

planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
author goeckslab
date Wed, 26 Feb 2025 18:09:34 +0000 (6 weeks ago)
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
1 # ------------------------------------------------------------------------------------
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
2 # Stripped down and modified from:
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
3 # https://github.com/ohsu-comp-bio/ashlar/blob/master/pyramid_upgrade.py
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
4 # ------------------------------------------------------------------------------------
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
5
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
6 import argparse
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
7 import csv
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
8 import xml.etree.ElementTree
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
9
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
10 from tifffile import tiffcomment
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
11
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
12
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
13 def fix_attrib_namespace(elt):
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
14 """Prefix un-namespaced XML attributes with the tag's namespace."""
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
15 # This fixes ElementTree's inability to round-trip XML with a default
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
16 # namespace ("cannot use non-qualified names with default_namespace option"
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
17 # error). 7-year-old BPO issue here: https://bugs.python.org/issue17088
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
18 # Code inspired by https://gist.github.com/provegard/1381912 .
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
19 if elt.tag[0] == "{":
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
20 uri, _ = elt.tag[1:].rsplit("}", 1)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
21 new_attrib = {}
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
22 for name, value in elt.attrib.items():
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
23 if name[0] != "{":
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
24 # For un-namespaced attributes, copy namespace from element.
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
25 name = f"{{{uri}}}{name}"
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
26 new_attrib[name] = value
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
27 elt.attrib = new_attrib
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
28 for child in elt:
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
29 fix_attrib_namespace(child)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
30
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
31
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
32 def main(image_fh, marker_file):
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
33 """
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
34 Parameters
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
35 ---------
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
36 image_fh : str
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
37 File path to the OME Tiff image.
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
38 marker_file : str
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
39 File path to CSV containing marker name information.
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
40 """
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
41
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
42 # parse marker file, create list of new marker names
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
43 new_channel_names = []
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
44 with open(marker_file, newline='') as csvfile:
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
45 reader = csv.DictReader(csvfile)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
46 for row in reader:
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
47 new_channel_names.append(row['marker_name'])
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
48
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
49 # read OME-XML metadata and parse down to channels
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
50 xml_ns = {"ome": "http://www.openmicroscopy.org/Schemas/OME/2016-06"}
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
51 root = xml.etree.ElementTree.fromstring(tiffcomment(image_fh))
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
52 image = root.find("ome:Image", xml_ns)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
53 pixels = image.find("ome:Pixels", xml_ns)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
54 channels = pixels.findall("ome:Channel", xml_ns)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
55
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
56 # name channels
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
57 for channel, name in zip(channels, new_channel_names):
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
58 channel.attrib["Name"] = name
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
59
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
60 # encode new xml and set image metadata
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
61 fix_attrib_namespace(root)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
62 new_ome_xml = xml.etree.ElementTree.tostring(
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
63 root,
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
64 encoding='utf-8',
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
65 xml_declaration=True,
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
66 default_namespace=xml_ns["ome"])
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
67
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
68 tiffcomment(image_fh, comment=new_ome_xml)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
69
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
70 print("Updated OME-TIFF metadata:")
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
71 print(tiffcomment(image_fh))
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
72
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
73
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
74 if __name__ == '__main__':
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
75 aparser = argparse.ArgumentParser()
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
76 aparser.add_argument("-i", "--image", dest="image", required=True)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
77 aparser.add_argument("-m", "--markers", dest="markers", required=True)
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
78
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
79 args = aparser.parse_args()
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
80
0b8423c7ee3e planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit d1f9d43d20432cc958e340271ba63c85a17ff338
goeckslab
parents:
diff changeset
81 main(args.image, args.markers)