def create_figure_file(conn, figure_json, figure_name):
    """Create Figure FileAnnotation from json data."""
    if len(figure_json['panels']) == 0:
        raise Exception('No Panels')
    first_img_id = figure_json['panels'][0]['imageId']

    # we store json in description field...
    description = {}
    description['name'] = figure_name
    description['imageId'] = first_img_id

    # Try to set Group context to the same as first image
    conn.SERVICE_OPTS.setOmeroGroup('-1')
    i = conn.getObject("Image", first_img_id)
    gid = i.getDetails().getGroup().getId()
    conn.SERVICE_OPTS.setOmeroGroup(gid)

    json_bytes = json.dumps(figure_json).encode('utf-8')
    file_size = len(json_bytes)
    f = BytesIO()
    try:
        f.write(json_bytes)

        update = conn.getUpdateService()
        orig_file = conn.createOriginalFileFromFileObj(
            f, '', figure_name, file_size, mimetype="application/json")
    finally:
        f.close()
    fa = FileAnnotationI()
    fa.setFile(OriginalFileI(orig_file.getId(), False))
    fa.setNs(rstring(JSON_FILEANN_NS))
    desc = json.dumps(description)
    fa.setDescription(rstring(desc))
    fa = update.saveAndReturnObject(fa, conn.SERVICE_OPTS)
    return fa.getId().getValue()
Пример #2
0
    def upload(self, args):
        client = self.ctx.conn(args)
        obj_ids = []
        for local_file in args.file:
            if not path(local_file).exists():
                self.ctx.die(500, "File: %s does not exist" % local_file)
        for local_file in args.file:
            omero_format = UNKNOWN
            if args.mimetype:
                omero_format = args.mimetype
            elif (mimetypes.guess_type(local_file) != (None, None)):
                omero_format = mimetypes.guess_type(local_file)[0]
            if args.data_dir:
                obj = upload_ln_s(client, local_file, args.data_dir,
                                  omero_format)
                obj_id = obj.id
            else:
                obj = client.upload(local_file, type=omero_format)
                obj_id = obj.id.val
            if args.wrap:
                fa = FileAnnotationI()
                fa.setFile(OriginalFileI(obj_id, False))
                if args.namespace:
                    fa.setNs(rstring(args.namespace))
                fa = client.sf.getUpdateService().saveAndReturnObject(fa)
                obj_ids.append(fa.id.val)
            else:
                obj_ids.append(obj_id)
            self.ctx.set("last.upload.id", obj_id)

        obj_ids = self._order_and_range_ids(obj_ids)
        if args.wrap:
            self.ctx.out("FileAnnotation:%s" % obj_ids)
        else:
            self.ctx.out("OriginalFile:%s" % obj_ids)
Пример #3
0
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)
Пример #4
0
def upload_csv_to_omero(ctx,
                        file,
                        tablename,
                        target_id,
                        target_type="Project"):
    """Upload the CSV file and attach it to the specified object"""
    print file
    print file.name
    svc = gateway.getFacility(DataManagerFacility)
    file_size = os.path.getsize(file.name)
    original_file = OriginalFileI()
    original_file.setName(rstring(tablename))
    original_file.setPath(rstring(file.name))
    original_file.setSize(rlong(file_size))

    checksum_algorithm = ChecksumAlgorithmI()
    checksum_algorithm.setValue(rstring(ChecksumAlgorithmSHA1160.value))
    original_file.setHasher(checksum_algorithm)
    original_file.setMimetype(rstring("text/csv"))
    original_file = svc.saveAndReturnObject(ctx, original_file)
    store = gateway.getRawFileService(ctx)

    # Open file and read stream
    store.setFileId(original_file.getId().getValue())
    print original_file.getId().getValue()
    try:
        store.setFileId(original_file.getId().getValue())
        with open(file.name, 'rb') as stream:
            buf = 10000
            for pos in range(0, long(file_size), buf):
                block = None
                if file_size - pos < buf:
                    block_size = file_size - pos
                else:
                    block_size = buf
                stream.seek(pos)
                block = stream.read(block_size)
                store.write(block, pos, block_size)

        original_file = store.save()
    finally:
        store.close()

    # create the file annotation
    namespace = "training.demo"
    fa = FileAnnotationI()
    fa.setFile(original_file)
    fa.setNs(rstring(namespace))

    if target_type == "Project":
        target_obj = ProjectData(ProjectI(target_id, False))
    elif target_type == "Dataset":
        target_obj = DatasetData(DatasetI(target_id, False))
    elif target_type == "Image":
        target_obj = ImageData(ImageI(target_id, False))

    svc.attachAnnotation(ctx, FileAnnotationData(fa), target_obj)
Пример #5
0
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)
Пример #6
0
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)
Пример #7
0
    def make_file_annotation(self,
                             name=None,
                             binary=None,
                             format=None,
                             client=None,
                             ns=None):
        """
        Creates a new DatasetI instance and returns the persisted object.
        If no name has been provided, a UUID string shall be used.

        :param name: the name of the project
        :param client: The client to use to create the object
        :param ns: The namespace for the annotation
        """

        if client is None:
            client = self.client
        update = client.sf.getUpdateService()

        # file
        if format is None:
            format = "application/octet-stream"
        if binary is None:
            binary = "12345678910"
        if name is None:
            name = str(self.uuid())

        oFile = OriginalFileI()
        oFile.setName(rstring(name))
        oFile.setPath(rstring(str(self.uuid())))
        oFile.setSize(rlong(len(binary)))
        oFile.hasher = ChecksumAlgorithmI()
        oFile.hasher.value = rstring("SHA1-160")
        oFile.setMimetype(rstring(str(format)))
        oFile = update.saveAndReturnObject(oFile)

        # save binary
        store = client.sf.createRawFileStore()
        store.setFileId(oFile.id.val)
        store.write(binary, 0, 0)
        oFile = store.save()  # See ticket:1501
        store.close()

        fa = FileAnnotationI()
        fa.setFile(oFile)
        if ns is not None:
            fa.setNs(rstring(ns))
        return update.saveAndReturnObject(fa)
Пример #8
0
    def make_file_annotation(self, name=None, binary=None, format=None, client=None, ns=None):
        """
        Creates a new DatasetI instance and returns the persisted object.
        If no name has been provided, a UUID string shall be used.

        :param name: the name of the project
        :param client: The client to use to create the object
        :param ns: The namespace for the annotation
        """

        if client is None:
            client = self.client
        update = client.sf.getUpdateService()

        # file
        if format is None:
            format = "application/octet-stream"
        if binary is None:
            binary = "12345678910"
        if name is None:
            name = str(self.uuid())

        oFile = OriginalFileI()
        oFile.setName(rstring(name))
        oFile.setPath(rstring(str(self.uuid())))
        oFile.setSize(rlong(len(binary)))
        oFile.hasher = ChecksumAlgorithmI()
        oFile.hasher.value = rstring("SHA1-160")
        oFile.setMimetype(rstring(str(format)))
        oFile = update.saveAndReturnObject(oFile)

        # save binary
        store = client.sf.createRawFileStore()
        store.setFileId(oFile.id.val)
        store.write(binary, 0, 0)
        oFile = store.save()  # See ticket:1501
        store.close()

        fa = FileAnnotationI()
        fa.setFile(oFile)
        if ns is not None:
            fa.setNs(rstring(ns))
        return update.saveAndReturnObject(fa)
Пример #9
0
def save_microscope(request, conn=None, **kwargs):

    body_json = json.loads(request.body)
    microscope_json = body_json['microscope']

    file_name = microscope_json["Name"]

    project = conn.getObject("Project", attributes={'name': PROJECT_NAME})

    curr_gid = conn.SERVICE_OPTS.getOmeroGroup()

    if project is not None:
        gid = project.getDetails().getGroup().getId()
        conn.SERVICE_OPTS.setOmeroGroup(gid)
    else:
        # TODO: create Project
        conn.SERVICE_OPTS.setOmeroGroup(curr_gid)

    update = conn.getUpdateService()

    file_data = json.dumps(microscope_json)
    file_size = len(file_data)
    f = BytesIO()
    f.write(file_data.encode("utf-8"))
    orig_file = conn.createOriginalFileFromFileObj(f,
                                                   '',
                                                   file_name,
                                                   file_size,
                                                   mimetype="application/json")
    fa = FileAnnotationI()
    fa.setFile(OriginalFileI(orig_file.getId(), False))
    fa.setNs(wrap(JSON_FILEANN_NS))
    # fa.setDescription(wrap(desc))
    fa = update.saveAndReturnObject(fa, conn.SERVICE_OPTS)
    file_id = fa.getId().getValue()

    if project is not None:
        fa_wrapper = FileAnnotationWrapper(conn, fa)
        project.linkAnnotation(fa_wrapper)

    return JsonResponse({'file_id': file_id})
Пример #10
0
def save_web_figure(conn, json_data):
    """
    Saves 'figureJSON' in POST as an original file. If 'fileId' is specified
    in POST, then we update that file. Otherwise create a new one with
    name 'figureName' from POST.
    """
    image_ids = []
    first_img_id = None
    try:
        for panel in json_data['panels']:
            image_ids.append(panel['imageId'])
        if len(image_ids) > 0:
            first_img_id = int(image_ids[0])
        # remove duplicates
        image_ids = list(set(image_ids))
        # pretty-print json
        figure_json = json.dumps(json_data,
                                 sort_keys=True,
                                 indent=2,
                                 separators=(',', ': '))
    except Exception:
        pass

    # See https://github.com/will-moore/figure/issues/16
    figure_json = figure_json.encode('utf8')

    if 'figureName' in json_data and len(json_data['figureName']) > 0:
        figure_name = json_data['figureName']
    else:
        print("No figure name found")
        return

    # we store json in description field...
    description = {}
    if first_img_id is not None:
        # We duplicate the figure name here for quicker access when
        # listing files
        # (use this instead of file name because it supports unicode)
        description['name'] = figure_name
        description['imageId'] = first_img_id
        if 'baseUrl' in panel:
            description['baseUrl'] = panel['baseUrl']
    desc = json.dumps(description)

    # Create new file
    # Try to set Group context to the same as first image
    curr_gid = conn.SERVICE_OPTS.getOmeroGroup()
    i = None
    if first_img_id:
        i = conn.getObject("Image", first_img_id)
    if i is not None:
        gid = i.getDetails().getGroup().getId()
        conn.SERVICE_OPTS.setOmeroGroup(gid)
    else:
        # Don't leave as -1
        conn.SERVICE_OPTS.setOmeroGroup(curr_gid)
    file_size = len(figure_json)
    f = BytesIO()
    f.write(figure_json)
    orig_file = conn.createOriginalFileFromFileObj(f,
                                                   '',
                                                   figure_name,
                                                   file_size,
                                                   mimetype="application/json")
    fa = FileAnnotationI()
    fa.setFile(OriginalFileI(orig_file.getId(), False))
    fa.setNs(wrap(JSON_FILEANN_NS))
    fa.setDescription(wrap(desc))

    update = conn.getUpdateService()
    fa = update.saveAndReturnObject(fa, conn.SERVICE_OPTS)
    ann_id = fa.getId().getValue()
    return ann_id