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_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_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 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
# 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, t=0, ifd=0, planeCount=size_z) create_companion(images=[image], out=cell + '.companion.ome') # Generate indented XML for readability proc = subprocess.Popen([ 'xmllint', '--format', '-o', cell + '.companion.ome', cell + '.companion.ome'
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) well_index += 1 companion_file = "../companions/{}.companion.ome".format(plate_name) create_companion(plates=[plate], out=companion_file)