annotate pyramid_upgrade.py @ 0:b3054f3d42b2 draft

"planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
author perssond
date Fri, 12 Mar 2021 00:14:49 +0000
parents
children f183d9de4622
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
1 import sys
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
2 import os
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
3 import argparse
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
4 import struct
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
5 import re
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
6 import fractions
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
7 import io
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
8 import xml.etree.ElementTree
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
9 import collections
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
10 import reprlib
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
11 import dataclasses
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
12 from typing import List, Any
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
13
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
14
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
15 datatype_formats = {
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
16 1: "B", # BYTE
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
17 2: "s", # ASCII
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
18 3: "H", # SHORT
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
19 4: "I", # LONG
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
20 5: "I", # RATIONAL (pairs)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
21 6: "b", # SBYTE
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
22 7: "B", # UNDEFINED
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
23 8: "h", # SSHORT
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
24 9: "i", # SLONG
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
25 10: "i", # SRATIONAL (pairs)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
26 11: "f", # FLOAT
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
27 12: "d", # DOUBLE
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
28 13: "I", # IFD
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
29 16: "Q", # LONG8
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
30 17: "q", # SLONG8
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
31 18: "Q", # IFD8
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
32 }
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
33 rational_datatypes = {5, 10}
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
34
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
35
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
36 class TiffSurgeon:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
37 """Read, manipulate and write IFDs in BigTIFF files."""
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
38
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
39 def __init__(self, path, *, writeable=False, encoding=None):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
40 self.path = path
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
41 self.writeable = writeable
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
42 self.encoding = encoding
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
43 self.endian = ""
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
44 self.ifds = None
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
45 self.file = open(self.path, "r+b" if self.writeable else "rb")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
46 self._validate()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
47
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
48 def _validate(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
49 signature = self.read("2s")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
50 signature = signature.decode("ascii", errors="ignore")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
51 if signature == "II":
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
52 self.endian = "<"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
53 elif signature == "MM":
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
54 self.endian = ">"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
55 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
56 raise FormatError(f"Not a TIFF file (signature is '{signature}').")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
57 version = self.read("H")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
58 if version == 42:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
59 raise FormatError("Cannot process classic TIFF, only BigTIFF.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
60 offset_size, reserved, first_ifd_offset = self.read("H H Q")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
61 if version != 43 or offset_size != 8 or reserved != 0:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
62 raise FormatError("Malformed TIFF, giving up!")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
63 self.first_ifd_offset = first_ifd_offset
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
64
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
65 def read(self, fmt, *, file=None):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
66 if file is None:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
67 file = self.file
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
68 endian = self.endian or "="
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
69 size = struct.calcsize(endian + fmt)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
70 raw = file.read(size)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
71 value = self.unpack(fmt, raw)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
72 return value
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
73
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
74 def write(self, fmt, *values):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
75 if not self.writeable:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
76 raise ValueError("File is opened as read-only.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
77 raw = self.pack(fmt, *values)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
78 self.file.write(raw)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
79
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
80 def unpack(self, fmt, raw):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
81 assert self.endian or re.match(r"\d+s", fmt), \
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
82 "can't unpack non-string before endianness is detected"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
83 fmt = self.endian + fmt
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
84 size = struct.calcsize(fmt)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
85 values = struct.unpack(fmt, raw[:size])
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
86 if len(values) == 1:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
87 return values[0]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
88 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
89 return values
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
90
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
91 def pack(self, fmt, *values):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
92 assert self.endian, "can't pack without endian set"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
93 fmt = self.endian + fmt
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
94 raw = struct.pack(fmt, *values)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
95 return raw
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
96
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
97 def read_ifds(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
98 ifds = [self.read_ifd(self.first_ifd_offset)]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
99 while ifds[-1].offset_next:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
100 ifds.append(self.read_ifd(ifds[-1].offset_next))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
101 self.ifds = ifds
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
102
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
103 def read_ifd(self, offset):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
104 self.file.seek(offset)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
105 num_tags = self.read("Q")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
106 buf = io.BytesIO(self.file.read(num_tags * 20))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
107 offset_next = self.read("Q")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
108 try:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
109 tags = TagSet([self.read_tag(buf) for i in range(num_tags)])
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
110 except FormatError as e:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
111 raise FormatError(f"IFD at offset {offset}, {e}") from None
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
112 ifd = Ifd(tags, offset, offset_next)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
113 return ifd
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
114
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
115 def read_tag(self, buf):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
116 tag = Tag(*self.read("H H Q 8s", file=buf))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
117 value, offset_range = self.tag_value(tag)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
118 tag = dataclasses.replace(tag, value=value, offset_range=offset_range)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
119 return tag
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
120
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
121 def append_ifd_sequence(self, ifds):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
122 """Write list of IFDs as a chained sequence at the end of the file.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
123
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
124 Returns a list of new Ifd objects with updated offsets.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
125
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
126 """
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
127 self.file.seek(0, os.SEEK_END)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
128 new_ifds = []
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
129 for ifd in ifds:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
130 offset = self.file.tell()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
131 self.write("Q", len(ifd.tags))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
132 for tag in ifd.tags:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
133 self.write_tag(tag)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
134 offset_next = self.file.tell() + 8 if ifd is not ifds[-1] else 0
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
135 self.write("Q", offset_next)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
136 new_ifd = dataclasses.replace(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
137 ifd, offset=offset, offset_next=offset_next
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
138 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
139 new_ifds.append(new_ifd)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
140 return new_ifds
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
141
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
142 def append_tag_data(self, code, datatype, value):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
143 """Build new tag and write data to the end of the file if necessary.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
144
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
145 Returns a Tag object corresponding to the passed parameters. This
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
146 function only writes any "overflow" data and not the IFD entry itself,
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
147 so the returned Tag must still be written to an IFD.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
148
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
149 If the value is small enough to fit in the data field within an IFD, no
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
150 data will actually be written to the file and the returned Tag object
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
151 will have the value encoded in its data attribute. Otherwise the data
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
152 will be appended to the file and the returned Tag's data attribute will
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
153 encode the corresponding offset.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
154
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
155 """
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
156 fmt = datatype_formats[datatype]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
157 # FIXME Should we perform our own check that values match datatype?
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
158 # struct.pack will do it but the exception won't be as understandable.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
159 original_value = value
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
160 if isinstance(value, str):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
161 if not self.encoding:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
162 raise ValueError(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
163 "ASCII tag values must be bytes if encoding is not set"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
164 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
165 value = [value.encode(self.encoding) + b"\x00"]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
166 count = len(value[0])
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
167 elif isinstance(value, bytes):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
168 value = [value + b"\x00"]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
169 count = len(value[0])
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
170 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
171 try:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
172 len(value)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
173 except TypeError:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
174 value = [value]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
175 count = len(value)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
176 struct_count = count
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
177 if datatype in rational_datatypes:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
178 value = [i for v in value for i in v.as_integer_ratio()]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
179 count //= 2
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
180 byte_count = struct_count * struct.calcsize(fmt)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
181 if byte_count <= 8:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
182 data = self.pack(str(struct_count) + fmt, *value)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
183 data += bytes(8 - byte_count)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
184 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
185 self.file.seek(0, os.SEEK_END)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
186 data = self.pack("Q", self.file.tell())
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
187 self.write(str(count) + fmt, *value)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
188 # TODO Compute and set offset_range.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
189 tag = Tag(code, datatype, count, data, original_value)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
190 return tag
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
191
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
192 def write_first_ifd_offset(self, offset):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
193 self.file.seek(8)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
194 self.write("Q", offset)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
195
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
196 def write_tag(self, tag):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
197 self.write("H H Q 8s", tag.code, tag.datatype, tag.count, tag.data)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
198
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
199 def tag_value(self, tag):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
200 """Return decoded tag data and the file offset range."""
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
201 fmt = datatype_formats[tag.datatype]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
202 count = tag.count
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
203 if tag.datatype in rational_datatypes:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
204 count *= 2
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
205 byte_count = count * struct.calcsize(fmt)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
206 if byte_count <= 8:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
207 value = self.unpack(str(count) + fmt, tag.data)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
208 offset_range = range(0, 0)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
209 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
210 offset = self.unpack("Q", tag.data)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
211 self.file.seek(offset)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
212 value = self.read(str(count) + fmt)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
213 offset_range = range(offset, offset + byte_count)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
214 if tag.datatype == 2:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
215 value = value.rstrip(b"\x00")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
216 if self.encoding:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
217 try:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
218 value = value.decode(self.encoding)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
219 except UnicodeDecodeError as e:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
220 raise FormatError(f"tag {tag.code}: {e}") from None
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
221 elif tag.datatype in rational_datatypes:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
222 value = [
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
223 fractions.Fraction(*v) for v in zip(value[::2], value[1::2])
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
224 ]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
225 if len(value) == 1:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
226 value = value[0]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
227 return value, offset_range
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
228
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
229 def close(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
230 self.file.close()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
231
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
232
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
233 @dataclasses.dataclass(frozen=True)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
234 class Tag:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
235 code: int
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
236 datatype: int
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
237 count: int
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
238 data: bytes
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
239 value: Any = None
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
240 offset_range: range = None
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
241
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
242 _vrepr = reprlib.Repr()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
243 _vrepr.maxstring = 60
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
244 _vrepr.maxother = 60
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
245 vrepr = _vrepr.repr
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
246
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
247 def __repr__(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
248 return (
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
249 self.__class__.__qualname__ + "("
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
250 + f"code={self.code!r}, datatype={self.datatype!r}, "
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
251 + f"count={self.count!r}, data={self.data!r}, "
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
252 + f"value={self.vrepr(self.value)}"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
253 + ")"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
254 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
255
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
256 @dataclasses.dataclass(frozen=True)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
257 class TagSet:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
258 """Container for Tag objects as stored in a TIFF IFD.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
259
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
260 Tag objects are maintained in a list that's always sorted in ascending order
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
261 by the tag code. Only one tag for a given code may be present, which is where
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
262 the "set" name comes from.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
263
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
264 """
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
265
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
266 tags: List[Tag] = dataclasses.field(default_factory=list)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
267
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
268 def __post_init__(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
269 if len(self.codes) != len(set(self.codes)):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
270 raise ValueError("Duplicate tag codes are not allowed.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
271
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
272 def __repr__(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
273 ret = type(self).__name__ + "(["
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
274 if self.tags:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
275 ret += "\n"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
276 ret += "".join([f" {t},\n" for t in self.tags])
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
277 ret += "])"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
278 return ret
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
279
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
280 @property
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
281 def codes(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
282 return [t.code for t in self.tags]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
283
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
284 def __getitem__(self, code):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
285 for t in self.tags:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
286 if code == t.code:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
287 return t
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
288 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
289 raise KeyError(code)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
290
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
291 def __delitem__(self, code):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
292 try:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
293 i = self.codes.index(code)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
294 except ValueError:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
295 raise KeyError(code) from None
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
296 self.tags[:] = self.tags[:i] + self.tags[i+1:]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
297
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
298 def __contains__(self, code):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
299 return code in self.codes
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
300
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
301 def __len__(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
302 return len(self.tags)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
303
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
304 def __iter__(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
305 return iter(self.tags)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
306
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
307 def get(self, code, default=None):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
308 try:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
309 return self[code]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
310 except KeyError:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
311 return default
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
312
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
313 def get_value(self, code, default=None):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
314 tag = self.get(code)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
315 if tag:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
316 return tag.value
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
317 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
318 return default
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
319
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
320 def insert(self, tag):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
321 """Add a new tag or replace an existing one."""
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
322 for i, t in enumerate(self.tags):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
323 if tag.code == t.code:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
324 self.tags[i] = tag
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
325 return
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
326 elif tag.code < t.code:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
327 break
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
328 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
329 i = len(self.tags)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
330 n = len(self.tags)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
331 self.tags[i:n+1] = [tag] + self.tags[i:n]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
332
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
333
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
334 @dataclasses.dataclass(frozen=True)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
335 class Ifd:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
336 tags: TagSet
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
337 offset: int
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
338 offset_next: int
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
339
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
340 @property
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
341 def nbytes(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
342 return len(self.tags) * 20 + 16
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
343
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
344 @property
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
345 def offset_range(self):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
346 return range(self.offset, self.offset + self.nbytes)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
347
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
348
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
349 class FormatError(Exception):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
350 pass
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
351
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
352
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
353 def fix_attrib_namespace(elt):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
354 """Prefix un-namespaced XML attributes with the tag's namespace."""
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
355 # This fixes ElementTree's inability to round-trip XML with a default
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
356 # namespace ("cannot use non-qualified names with default_namespace option"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
357 # error). 7-year-old BPO issue here: https://bugs.python.org/issue17088
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
358 # Code inspired by https://gist.github.com/provegard/1381912 .
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
359 if elt.tag[0] == "{":
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
360 uri, _ = elt.tag[1:].rsplit("}", 1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
361 new_attrib = {}
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
362 for name, value in elt.attrib.items():
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
363 if name[0] != "{":
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
364 # For un-namespaced attributes, copy namespace from element.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
365 name = f"{{{uri}}}{name}"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
366 new_attrib[name] = value
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
367 elt.attrib = new_attrib
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
368 for child in elt:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
369 fix_attrib_namespace(child)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
370
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
371
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
372 def parse_args():
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
373 parser = argparse.ArgumentParser(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
374 description="Convert an OME-TIFF legacy pyramid to the BioFormats 6"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
375 " OME-TIFF pyramid format in-place.",
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
376 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
377 parser.add_argument("image", help="OME-TIFF file to convert")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
378 parser.add_argument(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
379 "-n",
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
380 dest="channel_names",
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
381 nargs="+",
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
382 default=[],
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
383 metavar="NAME",
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
384 help="Channel names to be inserted into OME metadata. Number of names"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
385 " must match number of channels in image. Be sure to put quotes"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
386 " around names containing spaces or other special shell characters."
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
387 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
388 args = parser.parse_args()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
389 return args
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
390
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
391
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
392 def main():
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
393
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
394 args = parse_args()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
395
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
396 image_path = sys.argv[1]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
397 try:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
398 tiff = TiffSurgeon(image_path, encoding="utf-8", writeable=True)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
399 except FormatError as e:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
400 print(f"TIFF format error: {e}")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
401 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
402
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
403 tiff.read_ifds()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
404
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
405 # ElementTree doesn't parse xml declarations so we'll just run some sanity
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
406 # checks that we do have UTF-8 and give it a decoded string instead of raw
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
407 # bytes. We need to both ensure that the raw tag bytes decode properly and
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
408 # that the declaration encoding is UTF-8 if present.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
409 try:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
410 omexml = tiff.ifds[0].tags.get_value(270, "")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
411 except FormatError:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
412 print("ImageDescription tag is not a valid UTF-8 string (not an OME-TIFF?)")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
413 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
414 if re.match(r'<\?xml [^>]*encoding="(?!UTF-8)[^"]*"', omexml):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
415 print("OME-XML is encoded with something other than UTF-8.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
416 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
417
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
418 xml_ns = {"ome": "http://www.openmicroscopy.org/Schemas/OME/2016-06"}
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
419
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
420 if xml_ns["ome"] not in omexml:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
421 print("Not an OME-TIFF.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
422 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
423 if (
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
424 "Faas" not in tiff.ifds[0].tags.get_value(305, "")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
425 or 330 in tiff.ifds[0].tags
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
426 ):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
427 print("Not a legacy OME-TIFF pyramid.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
428 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
429
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
430 # All XML manipulation assumes the document is valid OME-XML!
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
431 root = xml.etree.ElementTree.fromstring(omexml)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
432 image = root.find("ome:Image", xml_ns)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
433 pixels = image.find("ome:Pixels", xml_ns)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
434 size_x = int(pixels.get("SizeX"))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
435 size_y = int(pixels.get("SizeY"))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
436 size_c = int(pixels.get("SizeC"))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
437 size_z = int(pixels.get("SizeZ"))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
438 size_t = int(pixels.get("SizeT"))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
439 num_levels = len(root.findall("ome:Image", xml_ns))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
440 page_dims = [(ifd.tags[256].value, ifd.tags[257].value) for ifd in tiff.ifds]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
441
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
442 if len(root) != num_levels:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
443 print("Top-level OME-XML elements other than Image are not supported.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
444 if size_z != 1 or size_t != 1:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
445 print("Z-stacks and multiple timepoints are not supported.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
446 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
447 if size_c * num_levels != len(tiff.ifds):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
448 print("TIFF page count does not match OME-XML Image elements.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
449 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
450 if any(dims != (size_x, size_y) for dims in page_dims[:size_c]):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
451 print(f"TIFF does not begin with SizeC={size_c} full-size pages.")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
452 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
453 for level in range(1, num_levels):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
454 level_dims = page_dims[level * size_c : (level + 1) * size_c]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
455 if len(set(level_dims)) != 1:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
456 print(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
457 f"Pyramid level {level + 1} out of {num_levels} has inconsistent"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
458 f" sizes:\n{level_dims}"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
459 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
460 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
461 if args.channel_names and len(args.channel_names) != size_c:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
462 print(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
463 f"Wrong number of channel names -- image has {size_c} channels but"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
464 f" {len(args.channel_names)} names were specified:"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
465 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
466 for i, n in enumerate(args.channel_names, 1):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
467 print(f"{i:4}: {n}")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
468 sys.exit(1)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
469
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
470 print("Input image summary")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
471 print("===================")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
472 print(f"Dimensions: {size_x} x {size_y}")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
473 print(f"Number of channels: {size_c}")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
474 print(f"Pyramid sub-resolutions ({num_levels - 1} total):")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
475 for dim_x, dim_y in page_dims[size_c::size_c]:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
476 print(f" {dim_x} x {dim_y}")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
477 software = tiff.ifds[0].tags.get_value(305, "<not set>")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
478 print(f"Software: {software}")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
479 print()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
480
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
481 print("Updating OME-XML metadata...")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
482 # We already verified there is nothing but Image elements under the root.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
483 for other_image in root[1:]:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
484 root.remove(other_image)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
485 for tiffdata in pixels.findall("ome:TiffData", xml_ns):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
486 pixels.remove(tiffdata)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
487 new_tiffdata = xml.etree.ElementTree.Element(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
488 f"{{{xml_ns['ome']}}}TiffData",
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
489 attrib={"IFD": "0", "PlaneCount": str(size_c)},
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
490 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
491 # A valid OME-XML Pixels begins with size_c Channels; then comes TiffData.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
492 pixels.insert(size_c, new_tiffdata)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
493
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
494 if args.channel_names:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
495 print("Renaming channels...")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
496 channels = pixels.findall("ome:Channel", xml_ns)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
497 for channel, name in zip(channels, args.channel_names):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
498 channel.attrib["Name"] = name
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
499
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
500 fix_attrib_namespace(root)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
501 # ElementTree.tostring would have been simpler but it only supports
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
502 # xml_declaration and default_namespace starting with Python 3.8.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
503 xml_file = io.BytesIO()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
504 tree = xml.etree.ElementTree.ElementTree(root)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
505 tree.write(
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
506 xml_file,
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
507 encoding="utf-8",
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
508 xml_declaration=True,
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
509 default_namespace=xml_ns["ome"],
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
510 )
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
511 new_omexml = xml_file.getvalue()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
512
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
513 print("Writing new TIFF headers...")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
514 stale_ranges = [ifd.offset_range for ifd in tiff.ifds]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
515 main_ifds = tiff.ifds[:size_c]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
516 channel_sub_ifds = [tiff.ifds[c + size_c : : size_c] for c in range(size_c)]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
517 for i, (main_ifd, sub_ifds) in enumerate(zip(main_ifds, channel_sub_ifds)):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
518 for ifd in sub_ifds:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
519 if 305 in ifd.tags:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
520 stale_ranges.append(ifd.tags[305].offset_range)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
521 del ifd.tags[305]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
522 ifd.tags.insert(tiff.append_tag_data(254, 3, 1))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
523 if i == 0:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
524 stale_ranges.append(main_ifd.tags[305].offset_range)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
525 stale_ranges.append(main_ifd.tags[270].offset_range)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
526 old_software = main_ifd.tags[305].value.replace("Faas", "F*a*a*s")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
527 new_software = f"pyramid_upgrade.py (was {old_software})"
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
528 main_ifd.tags.insert(tiff.append_tag_data(305, 2, new_software))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
529 main_ifd.tags.insert(tiff.append_tag_data(270, 2, new_omexml))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
530 else:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
531 if 305 in main_ifd.tags:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
532 stale_ranges.append(main_ifd.tags[305].offset_range)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
533 del main_ifd.tags[305]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
534 sub_ifds[:] = tiff.append_ifd_sequence(sub_ifds)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
535 offsets = [ifd.offset for ifd in sub_ifds]
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
536 main_ifd.tags.insert(tiff.append_tag_data(330, 16, offsets))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
537 main_ifds = tiff.append_ifd_sequence(main_ifds)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
538 tiff.write_first_ifd_offset(main_ifds[0].offset)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
539
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
540 print("Clearing old headers and tag values...")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
541 # We overwrite all the old IFDs and referenced data values with obvious
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
542 # "filler" as a courtesy to anyone who might need to poke around in the TIFF
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
543 # structure down the road. A real TIFF parser wouldn't see the stale data,
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
544 # but a human might just scan for the first thing that looks like a run of
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
545 # OME-XML and not realize it's been replaced with something else. The filler
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
546 # content is the repeated string "unused " with square brackets at the
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
547 # beginning and end of each filled IFD or data value.
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
548 filler = b"unused "
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
549 f_len = len(filler)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
550 for r in stale_ranges:
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
551 tiff.file.seek(r.start)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
552 tiff.file.write(b"[")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
553 f_total = len(r) - 2
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
554 for i in range(f_total // f_len):
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
555 tiff.file.write(filler)
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
556 tiff.file.write(b" " * (f_total % f_len))
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
557 tiff.file.write(b"]")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
558
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
559 tiff.close()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
560
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
561 print()
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
562 print("Success!")
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
563
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
564
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
565 if __name__ == "__main__":
b3054f3d42b2 "planemo upload for repository https://github.com/ohsu-comp-bio/ashlar commit 27f0c9be58e9e5aecc69067d0e60b5cb945de4b2-dirty"
perssond
parents:
diff changeset
566 main()