Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of migration to ome-types for generating companion OME-XML #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 62 additions & 22 deletions scripts/create_companions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
#!/usr/bin/env python
import sys
from ome_model.experimental import Plate, Image, create_companion
import subprocess

from ome_types.model import OME
from ome_types.model import Channel
from ome_types.model import Image
from ome_types.model import ImageRef
from ome_types.model import Pixels
from ome_types.model import Plane
from ome_types.model import Plate
from ome_types.model import TiffData
from ome_types.model import Well
from ome_types.model import WellSample
import uuid

filenames = []
with open("idr0092.files") as fp:
Expand All @@ -15,36 +25,66 @@
rows = ['A','B','C', 'D']
timepoints = ['0h', '24h', '48h', '72h', '96h']
print("Creating {}.companion.ome ...".format(plate_name))
plate = Plate(plate_name, len(rows), len(columns))

ome = OME()
plate = Plate(name=plate_name, rows=len(rows), columns=len(columns))
ome.plates.append(plate)

well_index = 0
for row_index, row in enumerate(rows):
for column_index, column in enumerate(columns):
well = plate.add_well(row_index, column_index)
well = Well(row=row_index, column=column_index)
plate.wells.append(well)

test = "{}_{}{}_0h.tiff".format(plate_name, row, column)
if test in filenames:
basename = "{}{}".format(row, column)
image = Image(basename, 2080, 1552, 25, 3, len(timepoints),
order="XYZTC", type="uint8")
image.add_channel(samplesPerPixel=3)
pixels = Pixels(
type='uint8',
size_x=2080,
size_y=1552,
size_z=25,
size_c=3,
size_t=len(timepoints),
dimension_order='XYZCT'
)
pixels.channels.append(Channel(samples_per_pixel=3))
image = Image(
name=basename,
pixels=pixels
)
ome.images.append(image)

for i, timepoint in enumerate(timepoints):
filename = "{}_{}{}_{}.tiff".format(plate_name, row, column, timepoint)
image.add_tiff(filename, c=0, z=0, t=i, planeCount=25)
options = {
'DeltaT': timepoint[:-1],
'DeltaTUnit': 'h',
}
tiff_uuid = TiffData.UUID(
value=f"urn:uuid:{uuid.uuid4()}",
file_name=filename
)
tiff = TiffData(
first_c=0,
first_t=i,
first_z=0,
plane_count=25,
uuid=tiff_uuid)
pixels.tiff_data_blocks.append(tiff)
for z in range(25):
image.add_plane(c=0, z=z, t=i, options=options)
well.add_wellsample(well_index, image)
pixels.planes.append(Plane(
delta_t=timepoint[:-1],
delta_t_unit="h",
the_c=0,
the_t=i,
the_z=z
)
)

well.well_samples.append(
WellSample(index=well_index, image_ref=ImageRef(id=image.id))
)
well_index += 1
companion_file = "../companions/{}.companion.ome".format(plate_name)
create_companion(plates=[plate], out=companion_file)

# Indent XML for readability
proc = subprocess.Popen(
['xmllint', '--format', '-o', companion_file, companion_file],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
(output, error_output) = proc.communicate()
with open(companion_file, 'w') as f:
f.write(ome.to_xml())


print("Done.")