def test_rgb_channel(self, tmpdir): f = str(tmpdir.join('rgb.companion.ome')) i = Image("test", 256, 512, 1, 3, 1) i.add_channel(samplesPerPixel=3) create_companion(images=[i], out=f) root = ElementTree.parse(f).getroot() images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test' pixels = images[0].findall('OME:Pixels', namespaces=NS) assert len(pixels) == 1 assert pixels[0].attrib['SizeX'] == '256' assert pixels[0].attrib['SizeY'] == '512' assert pixels[0].attrib['SizeZ'] == '1' assert pixels[0].attrib['SizeC'] == '3' assert pixels[0].attrib['SizeT'] == '1' assert pixels[0].attrib['DimensionOrder'] == 'XYZTC' assert pixels[0].attrib['Type'] == 'uint16' channels = pixels[0].findall('OME:Channel', namespaces=NS) assert len(channels) == 1 assert 'Name' not in channels[0].attrib assert 'Color' not in channels[0].attrib assert channels[0].attrib['SamplesPerPixel'] == '3'
def test_invalid_plane_option(self, tmpdir): f = str(tmpdir.join('invalid_plane_options.companion.ome')) i = Image("test", 512, 512, 1, 1, 1) options = { 'EmissionWavelength': '512.0', 'EmissionWavelengthUnit': 'nm', } i.add_plane(z=0, c=0, t=0, options=options) create_companion(images=[i], out=f) root = ElementTree.parse(f).getroot() images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test' pixels = images[0].findall('OME:Pixels', namespaces=NS) assert len(pixels) == 1 assert pixels[0].attrib['SizeX'] == '512' assert pixels[0].attrib['SizeY'] == '512' assert pixels[0].attrib['SizeZ'] == '1' assert pixels[0].attrib['SizeC'] == '1' assert pixels[0].attrib['SizeT'] == '1' assert pixels[0].attrib['DimensionOrder'] == 'XYZTC' assert pixels[0].attrib['Type'] == 'uint16' planes = pixels[0].findall('OME:Plane', namespaces=NS) assert len(planes) == 1 assert planes[0].attrib['TheZ'] == '0' assert planes[0].attrib['TheC'] == '0' assert planes[0].attrib['TheT'] == '0' assert 'EmissionWavelength' not in planes[0].attrib assert 'EmissionWavelengthUnit' not in planes[0].attrib
def test_too_many_channel_samples(self, tmpdir): f = str(tmpdir.join('invalid.companion.ome')) i = Image("test", 512, 512, 1, 2, 1) i.add_channel(samplesPerPixel=3) with pytest.raises(AssertionError): create_companion(images=[i], out=f)
def test_pixelzies(self, tmpdir, pixel_size_x, pixel_size_y, pixel_size_z, pixel_size_x_unit, pixel_size_y_unit, pixel_size_z_unit): f = str(tmpdir.join('image.companion.ome')) i = Image("test", 256, 512, 3, 4, 5, physSizeX=pixel_size_x, physSizeY=pixel_size_y, physSizeZ=pixel_size_z, physSizeXUnit=pixel_size_x_unit, physSizeYUnit=pixel_size_y_unit, physSizeZUnit=pixel_size_z_unit, type="unit8") create_companion(images=[i], out=f) root = ElementTree.parse(f).getroot() images = root.findall('OME:Image', namespaces=NS) pixels = images[0].findall('OME:Pixels', namespaces=NS)[0] if pixel_size_x: assert pixels.attrib['PhysicalSizeX'] == pixel_size_x if pixel_size_x_unit: assert pixels.attrib['PhysicalSizeXUnit'] == pixel_size_x_unit if pixel_size_y: assert pixels.attrib['PhysicalSizeY'] == pixel_size_y if pixel_size_y_unit: assert pixels.attrib['PhysicalSizeYUnit'] == pixel_size_y_unit if pixel_size_z: assert pixels.attrib['PhysicalSizeZ'] == pixel_size_z if pixel_size_z_unit: assert pixels.attrib['PhysicalSizeZUnit'] == pixel_size_z_unit
def test_multiple_rows_columns_wellsamples(self, tmpdir): f = str(tmpdir.join('plate.companion.ome')) p = Plate("test", 4, 5) for row in range(4): for column in range(5): well = p.add_well(row, column) for field in range(6): i = Image("test", 256, 512, 3, 4, 5) well.add_wellsample(field, i) create_companion(plates=[p], out=f) root = ElementTree.parse(f).getroot() plates = root.findall('OME:Plate', namespaces=NS) assert len(plates) == 1 assert plates[0].attrib['Name'] == 'test' wells = plates[0].findall('OME:Well', namespaces=NS) assert len(wells) == 20 imageids = [] for i in range(20): wellsamples = wells[i].findall('OME:WellSample', namespaces=NS) assert len(wellsamples) == 6 for ws in wellsamples: imagerefs = ws.findall('OME:ImageRef', namespaces=NS) assert len(imagerefs) == 1 imageids.append(imagerefs[0].attrib['ID']) assert len(imageids) == 120 images = root.findall('OME:Image', namespaces=NS) assert len(images) == 120 assert [x.attrib['ID'] for x in images] == imageids
def test_invalid_plane_index(self, tmpdir, invalidindex): i = Image("test", 512, 512, 1, 1, 1) with pytest.raises(AssertionError): i.add_plane(z=invalidindex, c=0, t=0) with pytest.raises(AssertionError): i.add_plane(z=0, c=invalidindex, t=0) with pytest.raises(AssertionError): i.add_plane(z=0, c=0, t=invalidindex)
def test_plane_options(self, tmpdir): f = str(tmpdir.join('plane_options.companion.ome')) i = Image("test", 512, 512, 1, 1, 1) options = { 'DeltaT': '0.0', 'DeltaTUnit': 's', 'ExposureTime': '100', 'ExposureTimeUnit': 'ms', 'PositionX': '0.0', 'PositionXUnit': 'reference frame', 'PositionY': '0.0', 'PositionYUnit': 'reference frame', 'PositionZ': '0.0', 'PositionZUnit': 'reference frame', } i.add_plane(z=0, c=0, t=0, options=options) create_companion(images=[i], out=f) root = ElementTree.parse(f).getroot() images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test' pixels = images[0].findall('OME:Pixels', namespaces=NS) assert len(pixels) == 1 assert pixels[0].attrib['SizeX'] == '512' assert pixels[0].attrib['SizeY'] == '512' assert pixels[0].attrib['SizeZ'] == '1' assert pixels[0].attrib['SizeC'] == '1' assert pixels[0].attrib['SizeT'] == '1' assert pixels[0].attrib['DimensionOrder'] == 'XYZTC' assert pixels[0].attrib['Type'] == 'uint16' planes = pixels[0].findall('OME:Plane', namespaces=NS) assert len(planes) == 1 assert planes[0].attrib['TheZ'] == '0' assert planes[0].attrib['TheC'] == '0' assert planes[0].attrib['TheT'] == '0' assert planes[0].attrib['DeltaT'] == '0.0' assert planes[0].attrib['DeltaTUnit'] == 's' assert planes[0].attrib['ExposureTime'] == '100' assert planes[0].attrib['ExposureTimeUnit'] == 'ms' assert planes[0].attrib['PositionX'] == '0.0' assert planes[0].attrib['PositionXUnit'] == 'reference frame' assert planes[0].attrib['PositionY'] == '0.0' assert planes[0].attrib['PositionYUnit'] == 'reference frame' assert planes[0].attrib['PositionZ'] == '0.0' assert planes[0].attrib['PositionZUnit'] == 'reference frame'
def test_minimal_plate(self, tmpdir, rc): f = str(tmpdir.join('plate.companion.ome')) if rc: p = Plate("test", 1, 2) else: p = Plate("test") well = p.add_well(0, 0) i = Image("test", 256, 512, 3, 4, 5) well.add_wellsample(0, i) create_companion(plates=[p], out=f) root = ElementTree.parse(f).getroot() plates = root.findall('OME:Plate', namespaces=NS) assert len(plates) == 1 assert plates[0].attrib['Name'] == 'test' if rc: assert plates[0].attrib['Rows'] == '1' assert plates[0].attrib['Columns'] == '2' wells = plates[0].findall('OME:Well', namespaces=NS) assert len(wells) == 1 assert wells[0].attrib['Row'] == '0' assert wells[0].attrib['Column'] == '0' wellsamples = wells[0].findall('OME:WellSample', namespaces=NS) assert len(wellsamples) == 1 imagerefs = wellsamples[0].findall('OME:ImageRef', namespaces=NS) assert len(imagerefs) == 1 imageid = imagerefs[0].attrib['ID'] images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test' assert images[0].attrib['ID'] == imageid pixels = images[0].findall('OME:Pixels', namespaces=NS) assert len(pixels) == 1 assert pixels[0].attrib['SizeX'] == '256' assert pixels[0].attrib['SizeY'] == '512' assert pixels[0].attrib['SizeZ'] == '3' assert pixels[0].attrib['SizeC'] == '4' assert pixels[0].attrib['SizeT'] == '5' assert pixels[0].attrib['DimensionOrder'] == 'XYZTC' assert pixels[0].attrib['Type'] == 'uint16' channels = pixels[0].findall('OME:Channel', namespaces=NS) assert len(channels) == 0
def test_channel(self, tmpdir, name, color): f = str(tmpdir.join('3channels.companion.ome')) i = Image("test", 256, 512, 3, 4, 5) i.add_channel() i.add_channel(name="Blue", color="65535", samplesPerPixel=1) i.add_channel(name=name, color=color) create_companion(images=[i], out=f) root = ElementTree.parse(f).getroot() images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test' pixels = images[0].findall('OME:Pixels', namespaces=NS) assert len(pixels) == 1 assert pixels[0].attrib['SizeX'] == '256' assert pixels[0].attrib['SizeY'] == '512' assert pixels[0].attrib['SizeZ'] == '3' assert pixels[0].attrib['SizeC'] == '4' assert pixels[0].attrib['SizeT'] == '5' assert pixels[0].attrib['DimensionOrder'] == 'XYZTC' assert pixels[0].attrib['Type'] == 'uint16' channels = pixels[0].findall('OME:Channel', namespaces=NS) assert len(channels) == 3 assert 'Name' not in channels[0].attrib assert 'Color' not in channels[0].attrib assert channels[0].attrib['SamplesPerPixel'] == '1' assert channels[1].attrib['Name'] == 'Blue' assert channels[1].attrib['Color'] == '65535' assert channels[1].attrib['SamplesPerPixel'] == '1' if name: assert channels[2].attrib['Name'] == name else: assert 'Name' not in channels[2].attrib if color: assert channels[2].attrib['Color'] == color else: assert 'Color' not in channels[2].attrib assert channels[2].attrib['SamplesPerPixel'] == '1'
def test_pixeltypes(self, tmpdir, pixel_type): f = str(tmpdir.join('image.companion.ome')) i = Image("test", 256, 512, 3, 4, 5, type=pixel_type) create_companion(images=[i], out=f) root = ElementTree.parse(f).getroot() images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test' pixels = images[0].findall('OME:Pixels', namespaces=NS) assert len(pixels) == 1 assert pixels[0].attrib['SizeX'] == '256' assert pixels[0].attrib['SizeY'] == '512' assert pixels[0].attrib['SizeZ'] == '3' assert pixels[0].attrib['SizeC'] == '4' assert pixels[0].attrib['SizeT'] == '5' assert pixels[0].attrib['DimensionOrder'] == 'XYZTC' assert pixels[0].attrib['Type'] == pixel_type channels = pixels[0].findall('OME:Channel', namespaces=NS) assert len(channels) == 0
def test_multiple_plates_one_per_file(self, tmpdir): files = [str(tmpdir.join('%s.companion.ome' % i)) for i in range(4)] for i in range(4): p = Plate("test %s" % i, 1, 1) well = p.add_well(0, 0) img = Image("test %s" % i, 256, 512, 3, 4, 5) well.add_wellsample(0, img) create_companion(plates=[p], out=files[i]) for i in range(4): root = ElementTree.parse(files[i]).getroot() plates = root.findall('OME:Plate', namespaces=NS) assert len(plates) == 1 assert plates[0].attrib['Name'] == 'test %s' % i wells = plates[0].findall('OME:Well', namespaces=NS) assert len(wells) == 1 wellsamples = wells[0].findall('OME:WellSample', namespaces=NS) assert len(wellsamples) == 1 imagerefs = wellsamples[0].findall('OME:ImageRef', namespaces=NS) assert len(imagerefs) == 1 images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test %s' % i
def test_planes(self, tmpdir): f = str(tmpdir.join('planes.companion.ome')) i = Image("test", 256, 512, 1, 2, 2) i.add_plane(c=0, z=0, t=0) i.add_plane(c=0, z=0, t=1) i.add_plane(c=1, z=0, t=0) i.add_plane(c=1, z=0, t=1) create_companion(images=[i], out=f) root = ElementTree.parse(f).getroot() images = root.findall('OME:Image', namespaces=NS) assert len(images) == 1 assert images[0].attrib['Name'] == 'test' pixels = images[0].findall('OME:Pixels', namespaces=NS) assert len(pixels) == 1 assert pixels[0].attrib['SizeX'] == '256' assert pixels[0].attrib['SizeY'] == '512' assert pixels[0].attrib['SizeZ'] == '1' assert pixels[0].attrib['SizeC'] == '2' assert pixels[0].attrib['SizeT'] == '2' assert pixels[0].attrib['DimensionOrder'] == 'XYZTC' assert pixels[0].attrib['Type'] == 'uint16' planes = pixels[0].findall('OME:Plane', namespaces=NS) assert len(planes) == 4 assert planes[0].attrib['TheZ'] == '0' assert planes[0].attrib['TheC'] == '0' assert planes[0].attrib['TheT'] == '0' assert planes[1].attrib['TheZ'] == '0' assert planes[1].attrib['TheC'] == '0' assert planes[1].attrib['TheT'] == '1' assert planes[2].attrib['TheZ'] == '0' assert planes[2].attrib['TheC'] == '1' assert planes[2].attrib['TheT'] == '0' assert planes[3].attrib['TheZ'] == '0' assert planes[3].attrib['TheC'] == '1' assert planes[3].attrib['TheT'] == '1'
def create_genotypic_companions(slides): GENO_CYCLES = 11 logging.info("Generating genotypic metadata files") filePaths = [] for slide in slides: for i in range(GENO_CYCLES): images = [] index = i + 1 logging.debug("Processing %s" % slide) # Genotypic metadata generation for position in sorted(slides[slide]): logging.debug(" Creating image for %s" % position) image = Image(position, 2048, 879, 1, 5, 1, order="XYZCT", type="uint16") image.add_channel("Cy5", -16776961) image.add_channel("Cy3", 16711935) image.add_channel("TxR", 65535) image.add_channel("Fam", -65281) image.add_channel("Phase", -1) image.add_tiff(get_genotypic_filename(position, "1_cy5_fluor", index), c=0, z=0, t=0) image.add_tiff(get_genotypic_filename(position, "2_cy3_fluor", index), c=1, z=0, t=0) image.add_tiff(get_genotypic_filename(position, "3_TxR_fluor", index), c=2, z=0, t=0) image.add_tiff(get_genotypic_filename(position, "4_fam_flour", index), c=3, z=0, t=0) image.add_tiff(get_genotypic_filename(position, "phase", index), c=4, z=0, t=0) images.append(image) position_dir = join(EXPERIMENTB_DIRECTORY, 'companions', slide, 'geno', position) if not os.path.lexists(position_dir): os.symlink(join(DATA_DIRECTORY, slide, "geno", position), position_dir) companion_file = join( EXPERIMENTB_DIRECTORY, 'companions', slide, "geno", '%s_round-%02g.companion.ome' % (slide, index)) write_companion(images, companion_file) filePaths.append( "Project:name:idr0065-camsund-crispri/experimentB/" "Dataset:name:%s_round-%02g\t" "%s\t%s\n" % (slide, index, companion_file, slide)) tsv = join(EXPERIMENTB_DIRECTORY, 'idr0065-experimentB-filePaths.tsv') with open(tsv, 'w') as f: for p in filePaths: f.write(p)
def create_phenotypic_companions(): """Phenotypic metadata files""" logging.info("Generating phenotypic metadata files") time_indexes = read_phenotypic_time_indexes() timestamps = read_phenotypic_timestamps() slides = parse_phenotypic_filenames(timestamps) filePaths = [] for slide in slides: images = [] logging.debug("Processing phenotypic %s" % slide) # Genotypic metadata generation for position in sorted(slides[slide]): logging.debug(" Creating image for %s" % position) image = Image(position, 2048, 879, 1, 2, 481, order="XYZCT", type="uint16") image.add_channel("Phase", -1) image.add_channel("Fluorescence", 16711935) # Phase images for t in range(481): relative_path = get_phenotypic_filename(position, "phase", t) full_path = get_phenotypic_filename(position, "phase", t, slide=slide) image.add_tiff(relative_path, c=0, z=0, t=t, planeCount=1) plane_options = { "DeltaT": timestamps[full_path], "DeltaTUnit": "s", "ExposureTime": "20", "ExposureTimeUnit": "ms", } image.add_plane(c=0, z=0, t=t, options=plane_options) # Fluorescence images for i in range(241): relative_path = get_phenotypic_filename(position, "fluor", i) full_path = get_phenotypic_filename(position, "fluor", i, slide=slide) t = time_indexes[full_path] plane_options = { "DeltaT": timestamps[full_path], "DeltaTUnit": "s", "ExposureTime": "300", "ExposureTimeUnit": "ms", } if t == 2 * i: image.add_tiff(relative_path, c=1, z=0, t=t, planeCount=1) if i != 240: image.add_tiff(None, c=1, z=0, t=(2 * i) + 1) elif t == (2 * i) + 1: image.add_tiff(None, c=1, z=0, t=2 * i) if t != 481: image.add_tiff(relative_path, c=1, z=0, t=t, planeCount=1) else: raise Exception("Invalid mapping") image.add_plane(c=1, z=0, t=t, options=plane_options) images.append(image) position_dir = join(EXPERIMENTA_DIRECTORY, 'companions', slide, "pheno", position) if not os.path.lexists(position_dir): os.symlink(join(DATA_DIRECTORY, slide, "pheno", position), position_dir) companion_file = join(EXPERIMENTA_DIRECTORY, 'companions', slide, "pheno", slide + '.companion.ome') write_companion(images, companion_file) filePaths.append("Project:name:idr0065-camsund-crispri/experimentA/" "Dataset:name:%s\t" "%s\t%s\n" % (slide, companion_file, slide)) tsv = join(EXPERIMENTA_DIRECTORY, 'idr0065-experimentA-filePaths.tsv') with open(tsv, 'w') as f: for p in filePaths: f.write(p) return slides
# Image Dimensions img = PIL_Image.open(join(cell, rawtiffs[1])) (size_x, size_y) = img.size size_z = 0 while True: try: img.seek(size_z) except EOFError: break size_z += 1 # Create 2-channel image image = Image(os.path.basename(cell), size_x, size_y, size_z, 2, 1, order="XYZCT", type="uint16") image.data['Pixels']['PhysicalSizeX'] = '20' image.data['Pixels']['PhysicalSizeXUnit'] = 'nm' image.data['Pixels']['PhysicalSizeY'] = '20' image.data['Pixels']['PhysicalSizeYUnit'] = 'nm' (n1, c1, n2, c2) = CHANNEL_MAPPING[os.path.basename(folder)] image.add_channel(n1, c1) image.add_channel(n2, c2) for i in range(len(rawtiffs)): image.add_tiff("%s/%s" % (os.path.basename(cell), rawtiffs[i]), c=i, z=0,
columns = ['01', '02', '03', '04', '05', '06'] 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)) 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) 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) 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', } for z in range(25): image.add_plane(c=0, z=z, t=i, options=options) well.add_wellsample(well_index, image)