def test_upload_ln_s_no_access(self): txt = 'hello\n' omero_data_dir = '/tmp' f = create_path(suffix=".txt") f.write_text(txt) with pytest.raises(omero.ApiUsageException): upload_ln_s(self.client, f, omero_data_dir, 'text/plain')
def main(conn, filepath): path = Path(filepath) filename = path.name if path.suffix != '.zip': sys.exit("Not a zip file") img_name = path.stem tmp = list(conn.getObjects("Image", attributes={"name": img_name})) if len(tmp) == 0: sys.exit("No Image found") if len(tmp) > 1: sys.exit("More than one Image found") tgt = tmp[0] existingfas = set( a.getFile().name for a in tgt.listAnnotations() if isinstance(a, omero.gateway.FileAnnotationWrapper)) if filename in existingfas: sys.exit("File already attached.") print("Attaching {} to Image {} [{}]".format(path.resolve(), tgt.getName(), tgt.getId())) if not DRY_RUN: fo = upload_ln_s(conn.c, path.resolve(), OMERO_DATA_DIR, MIMETYPE) fa = omero.model.FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(NAMESPACE)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) tgt.linkAnnotation(fa)
def main(conn, args): path = Path(args.file) filename = path.name target_type = args.target.split(":")[0] target_id = args.target.split(":")[1] tmp = list(conn.getObjects(target_type, attributes={"id": target_id})) if len(tmp) == 0: sys.exit("Target found") if len(tmp) > 1: sys.exit("More than one Target found") tgt = tmp[0] existingfas = set(a.getFile().name for a in tgt.listAnnotations() if isinstance(a, omero.gateway.FileAnnotationWrapper)) if filename in existingfas: sys.exit("File already attached.") print("Attaching {} to {} {} [{}]".format(path.resolve(), target_type, tgt.getName(), tgt.getId())) fo = upload_ln_s(conn.c, path.resolve(), OMERO_DATA_DIR, args.mimetype) fa = omero.model.FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(args.namespace)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) tgt.linkAnnotation(fa)
def upload_and_link(conn, attachment, project): fo = upload_ln_s(conn.c, attachment, OMERO_DATA_DIR, MIMETYPE) fa = FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(NAMESPACE)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) project.linkAnnotation(fa)
def upload_and_link(conn, target, attachment): print("upload_and_link", attachment) attachment = os.path.join(FILESETS_DIR, attachment) fo = upload_ln_s(conn.c, attachment, OMERO_DATA_DIR, MIMETYPE) fa = FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(NAMESPACE)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) target.linkAnnotation(fa)
def upload_and_link(conn, attachment, image): fa = image.getAnnotation(ns=NAMESPACE) if fa is not None: log.warning("Found file annotation. Skipping upload") return log.info("Uploading and linking %s to %s" % (attachment, image.getName())) fo = upload_ln_s(conn.c, attachment, OMERO_DATA_DIR, MIMETYPE) fa = FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(NAMESPACE)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) image.linkAnnotation(fa)
def link(conn, target, attachment, is_image): fo = upload_ln_s(conn.c, attachment, OMERO_DATA_DIR, MIMETYPE) fa = omero.model.FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(NAMESPACE)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) if is_image: tg = conn.getObject("Image", target.getId().getValue()) tg.linkAnnotation(fa) else: for d in target: tg = conn.getObject("Dataset", d.getId().getValue()) tg.linkAnnotation(fa)
def test_upload_ln_s(self): txt = 'hello\n' omero_data_dir = self.root.sf.getConfigService().getConfigValue( 'omero.data.dir') assert omero_data_dir f = create_path(suffix=".txt") f.write_text(txt) ofile = upload_ln_s(self.client, f, omero_data_dir, 'text/plain') assert ofile.getHash() == 'f572d396fae9206628714fb2ce00f72e94f2258f' assert ofile.getSize() == 6 with ofile.asFileObj() as fo: assert fo.read().decode('utf-8') == txt omero_path = os.path.join(omero_data_dir, 'Files', long_to_path(ofile.id)) assert os.path.islink(omero_path) assert os.readlink(omero_path) == str(f)
def main(conn): for im in get_images(conn): print('Image: %d' % im.id) mask_files, filenames = get_mask_files(im) attached = set(get_label_files_in_t_order(im, filenames, check=False)) for mf in mask_files: if mf in attached: continue print('Uploading: %s' % mf) if not DRYRUN: fo = upload_ln_s(conn.c, mf, OMERO_DATA_DIR, 'image/tiff') fa = omero.model.FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(NAMESPACE)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) im.linkAnnotation(fa) for im in get_images(conn): print('Image: %d' % im.id) rois = create_rois(im) if not DRYRUN: save_rois(conn, im, rois)
def main(conn): image_attachment_map, dataset_attachment_map = get_feature_files() errors = [] for im in get_images(conn): print('Image: %d' % im.id) existing = existing_file_attachments(im) try: seg = image_attachment_map[im.name] except KeyError: errors.append('No segmentation found for {}'.format(im.name)) continue if os.path.basename(seg) in existing: print('Skipping {} ➔ {}'.format(seg, im.name)) continue print('Uploading (in-place) {} ➔ {}'.format(seg, im.name)) if not DRYRUN: fo = upload_ln_s(conn.c, seg, OMERO_DATA_DIR, 'image/tiff') fa = omero.model.FileAnnotationI() fa.setFile(fo._obj) fa.setNs(omero.rtypes.rstring(NAMESPACE)) fa = conn.getUpdateService().saveAndReturnObject(fa) fa = omero.gateway.FileAnnotationWrapper(conn, fa) im.linkAnnotation(fa) if errors: print('{} errors:'.format(len(errors))) for err in errors: print(err) for ds in get_datasets(conn): print('Dataset:{} {}'.format(ds.id, ds.name)) existing = existing_file_attachments(ds) features = dataset_attachment_map[ds.name] if features in existing: print('Skipping {} ➔ {}'.format(features, ds.name)) continue print('Uploading {} ➔ {}'.format(features, ds.name)) if not DRYRUN: fo = conn.c.upload(features, type='text/tab-separated-values') fa = omero.model.FileAnnotationI() fa.setFile(omero.model.OriginalFileI(fo.id, False)) fa.setNs(rstring(NAMESPACE)) link = omero.model.DatasetAnnotationLinkI() link.setParent(ds._obj) link.setChild(fa) link = conn.getUpdateService().saveAndReturnObject(link) roisvc = conn.getRoiService() for im in get_images(conn): if SKIP_IF_EXISTING_ROIS and roisvc.findByImage(im.id, None).rois: print('Image:{} {} has ROIs, skipping'.format(im.id, im.name)) continue print('Image:{} {}'.format(im.id, im.name)) try: labels = get_labels(im, image_attachment_map) rois = create_rois(im, labels) if not DRYRUN: save_rois(conn, im, rois) except KeyError: print('No segmentation found for {}'.format(im.name)) except ValueError as e: print('ERROR in {}: {}'.format(im.name, e))