Beispiel #1
0
def index_projects(es, index, client, project_ids):
    session = client.getSession()
    query_service = session.getQueryService()

    for project_id in project_ids:
        log.info('Processing Project:%d' % project_id)

        params = ParametersI()
        params.addId(project_id)
        t0 = time.time()
        project = query_service.findByQuery(
            QUERY_PROJECT, params, {'omero.group': '-1'}
        )
        log.info(
            'Loaded Project:%d (%dms)' % (
                project_id, (time.time() - t0) * 1000
            )
        )

        if project is None:
            log.warn('Project:%d has no Datasets or Images!' % project_id)
            continue

        t0 = time.time()
        document = ProjectDocument(client, project)
        log.info(
            'Created document from Project:%d (%dms)' % (
                project_id, (time.time() - t0) * 1000
            )
        )
        if es is None:
            print document
            for image_document in document.image_documents:
                print image_document
            continue

        logging.info('Using Elasticsearch index: %s' % index)

        t0 = time.time()
        result = es.index(
            index=index,
            doc_type='project',
            id=project_id,
            body=json.dumps(document.document)
        )
        log.info(
            'Index complete: %r (%dms)' % (
                result, (time.time() - t0) * 1000
            )
        )

        t0 = time.time()
        result = helpers.bulk(
            es, image_document_index_actions(document, index)
        )
        log.info(
            'Index complete: %r (%dms)' % (
                result, (time.time() - t0) * 1000
            )
        )
Beispiel #2
0
def get_perms(conn, object_type, object_id, object_owner_id, object_group_id, cache):

    # Attempt to get permissions which have previously been recorded for this
    # group depending on if the object is owned or not
    perms = cache.get((object_group_id.val, object_owner_id.val))

    # If no cache, query an object to get the permissions for this group and
    # object ownership
    if perms is None:
        params = ParametersI()
        params.addId(object_id)
        q = (
            """
            select obj from %s obj where obj.id = :id
            """
            % object_type
        )
        qs = conn.getQueryService()
        obj = qs.projection(q, params, conn.SERVICE_OPTS)[0][0].val

        perms_obj = obj.details.permissions

        # To be compatible with parse_permissions_css, convert the required
        # fields to a dictionary
        restrictions = ("canEdit", "canAnnotate", "canLink", "canDelete")
        perms = {}
        for r in restrictions:
            if getattr(perms_obj, r)():
                perms[r] = True

        # Cache the result
        cache[(object_group_id.val, object_owner_id.val)] = perms

    return perms
Beispiel #3
0
def well_annotations(request, screen, conn=None, **kwargs):
    """
    Return Annotations on child Wells.
    JSON format same as for webclient/api/annotations/?type=map
    """

    ann_type = request.GET.get('type', None)

    # get wells in Screen
    queryService = conn.getQueryService()
    params = ParametersI()
    params.addId(screen)
    query = """select well.id from Well as well
               join well.plate plate
               join plate.screenLinks screenLinks
               where screenLinks.parent.id=:id
            """
    result = queryService.projection(query, params, conn.SERVICE_OPTS)
    iids = [r[0].val for r in result]
    anns, exps = marshal_annotations(conn,
                                     well_ids=iids,
                                     ann_type=ann_type,
                                     limit=100000)

    return JsonResponse({'annotations': anns, 'experimenters': exps})
Beispiel #4
0
def datasets(request, project, conn=None, **kwargs):
    """Return {dataset: {name: Dataset, id:1}, image: {id: 2}}."""

    queryService = conn.getQueryService()

    params = ParametersI()
    params.addId(project)
    query = """select d from Dataset as d
               join fetch d.imageLinks imageLinks
               join fetch imageLinks.child
               join fetch d.projectLinks projectLinks
               where projectLinks.parent.id=:id 
            """
    result = queryService.findAllByQuery(query, params, conn.SERVICE_OPTS)
    data = []
    for d in result:
        for link in d.copyImageLinks():
            data.append({
                'dataset': {
                    'id': d.id.val,
                    'name': d.name.val
                },
                'image': {
                    'id': link.child.id.val
                }
            })
    return JsonResponse({'data': data})
Beispiel #5
0
def image_annotations(request, project, conn=None, **kwargs):
    """
    Return Annotations on child Images.
    JSON format same as for webclient/api/annotations/?type=map
    """

    ann_type = request.GET.get('type', None)

    # get images in Project
    queryService = conn.getQueryService()
    params = ParametersI()
    params.addId(project)
    query = """select image.id from Image as image
               join image.datasetLinks datasetLinks
               join datasetLinks.parent as dataset
               join dataset.projectLinks projectLinks
               where projectLinks.parent.id=:id
            """
    result = queryService.projection(query, params, conn.SERVICE_OPTS)
    iids = [r[0].val for r in result]
    anns, exps = marshal_annotations(conn,
                                     image_ids=iids,
                                     ann_type=ann_type,
                                     limit=100000)

    return JsonResponse({'annotations': anns, 'experimenters': exps})
Beispiel #6
0
def get_perms(conn, object_type, object_id, object_owner_id, object_group_id,
              cache):

    # Attempt to get permissions which have previously been recorded for this
    # group depending on if the object is owned or not
    perms = cache.get((object_group_id.val, object_owner_id.val))

    # If no cache, query an object to get the permissions for this group and
    # object ownership
    if perms is None:
        params = ParametersI()
        params.addId(object_id)
        q = '''
            select obj from %s obj where obj.id = :id
            ''' % object_type
        qs = conn.getQueryService()
        obj = qs.projection(q, params, conn.SERVICE_OPTS)[0][0].val

        perms_obj = obj.details.permissions

        # To be compatible with parse_permissions_css, convert the required
        # fields to a dictionary
        restrictions = ('canEdit', 'canAnnotate', 'canLink', 'canDelete')
        perms = {}
        for r in restrictions:
            if getattr(perms_obj, r)():
                perms[r] = True

        # Cache the result
        cache[(object_group_id.val, object_owner_id.val)] = perms

    return perms
Beispiel #7
0
def index_screens(es, index, client, screen_ids):
    session = client.getSession()
    query_service = session.getQueryService()

    for screen_id in screen_ids:
        log.info('Processing Screen:%d' % screen_id)

        params = ParametersI()
        params.addId(screen_id)
        t0 = time.time()
        plates = query_service.findAllByQuery(
            QUERY_PLATES, params, {'omero.group': '-1'}
        )
        log.info(
            'Loaded %d Plates from Screen:%d (%dms)' % (
                len(plates), screen_id, (time.time() - t0) * 1000
            )
        )
        for plate in plates:
            plate_id = plate.id.val
            t0 = time.time()
            document = PlateDocument(client, plate)
            log.info(
                'Created document from Plate:%d (%dms)' % (
                    plate_id, (time.time() - t0) * 1000
                )
            )
            if es is None:
                print document
                for well_document in document.well_documents:
                    print well_document
                continue

            logging.info('Using Elasticsearch index: %s' % index)

            t0 = time.time()
            result = es.index(
                index=index,
                doc_type='plate',
                id=plate_id,
                body=json.dumps(document.document)
            )
            log.info(
                'Index complete: %r (%dms)' % (
                    result, (time.time() - t0) * 1000
                )
            )

            t0 = time.time()
            result = helpers.bulk(
                es, well_document_index_actions(document, index)
            )
            log.info(
                'Index complete: %r (%dms)' % (
                    result, (time.time() - t0) * 1000
                )
            )
Beispiel #8
0
 def get_image_counts_per_dataset(self, query_service):
     params = ParametersI()
     params.addId(self.project.id.val)
     t0 = time.time()
     image_counts = dict([
         (r[0].val, r[1].val) for r in query_service.projection(
             QUERY_IMAGE_COUNTS, params, {'omero.group': '-1'})
     ])
     log.info('Image counts: %s (%dms)' % (pformat(image_counts, indent=2),
                                           (time.time() - t0) * 1000))
     return image_counts
Beispiel #9
0
def get_well_ids(conn, plate_id):
    """Get well IDs for Plate"""
    conn.SERVICE_OPTS.setOmeroGroup('-1')
    query_service = conn.getQueryService()
    params = ParametersI()
    params.addId(plate_id)
    query = "select well.id "\
            "from Well well "\
            "where well.plate.id = :id"
    p = query_service.projection(query, params, conn.SERVICE_OPTS)
    return [i[0].val for i in p]
def run():
    """
    """
    data_types = [rstring("Plate")]

    client = scripts.client(
        "Unlink_Images.py",
        "Unlink Images from a given Plate",

        scripts.String("Data_Type", optional=False, grouping="1",
                       description="The data type you want to work with.",
                       values=data_types,
                       default="Plate"),

        scripts.List("IDs", optional=False, grouping="2",
                     description="List of Plate IDs").ofType(rlong(0)),

        version="0.1",
        authors=["Chris Allan"],
        institutions=["Glencoe Software Inc."],
        contact="*****@*****.**",
    )

    try:
        script_params = {}
        for key in client.getInputKeys():
            if client.getInput(key):
                script_params[key] = client.getInput(key, unwrap=True)

        session = client.getSession()
        update_service = session.getUpdateService()
        query_service = session.getQueryService()

        count = 0
        for plate_id in script_params["IDs"]:
            params = ParametersI()
            params.addId(plate_id)
            plate = query_service.findByQuery(
                "SELECT p from Plate AS p "
                "LEFT JOIN FETCH p.wells as w "
                "LEFT JOIN FETCH w.wellSamples as ws "
                "WHERE p.id = :id", params)
            for well in plate.copyWells():
                count += well.sizeOfWellSamples()
                well.clearWellSamples()
            update_service.saveObject(plate)

        client.setOutput("Message", rstring(
            "Unlinking of %d Image(s) successful." % count))
    finally:
        client.closeSession()
def run():
    """
    """
    data_types = [rstring("Plate")]

    client = scripts.client(
        "Unlink_Images.py",
        "Unlink Images from a given Plate",

        scripts.String("Data_Type", optional=False, grouping="1",
                       description="The data type you want to work with.",
                       values=data_types,
                       default="Plate"),

        scripts.List("IDs", optional=False, grouping="2",
                     description="List of Plate IDs").ofType(rlong(0)),

        version="0.1",
        authors=["Chris Allan"],
        institutions=["Glencoe Software Inc."],
        contact="*****@*****.**",
    )

    try:
        script_params = {}
        for key in client.getInputKeys():
            if client.getInput(key):
                script_params[key] = client.getInput(key, unwrap=True)

        session = client.getSession()
        update_service = session.getUpdateService()
        query_service = session.getQueryService()

        count = 0
        for plate_id in script_params["IDs"]:
            params = ParametersI()
            params.addId(plate_id)
            plate = query_service.findByQuery(
                "SELECT p from Plate AS p "
                "LEFT JOIN FETCH p.wells as w "
                "LEFT JOIN FETCH w.wellSamples as ws "
                "WHERE p.id = :id", params)
            for well in plate.copyWells():
                count += well.sizeOfWellSamples()
                well.clearWellSamples()
            update_service.saveObject(plate)

        client.setOutput("Message", rstring(
            "Unlinking of %d Image(s) successful." % count))
    finally:
        client.closeSession()
Beispiel #12
0
def get_dataset_image_ids(conn, dataset_id):
    """Get image IDs for images in Dataset"""

    conn.SERVICE_OPTS.setOmeroGroup('-1')
    query_service = conn.getQueryService()
    params = ParametersI()
    params.addId(dataset_id)
    query = "select img.id "\
            "from DatasetImageLink link "\
            "join link.child as img "\
            "where link.parent.id = :id"
    p = query_service.projection(query, params, conn.SERVICE_OPTS)
    img_ids = [i[0].val for i in p]
    return img_ids
Beispiel #13
0
def get_project_image_ids(conn, project_id):
    """Get image IDs for images in Project"""
    conn.SERVICE_OPTS.setOmeroGroup('-1')
    query_service = conn.getQueryService()
    params = ParametersI()
    params.addId(project_id)
    query = "select link "\
            "from DatasetImageLink link "\
            "join fetch link.parent dataset "\
            "join fetch dataset.projectLinks plink "\
            "where plink.parent.id = :id "
    p = query_service.projection(query, params, conn.SERVICE_OPTS)
    img_ids = [i[0].val.child.id.val for i in p]
    return img_ids
Beispiel #14
0
def get_image_ids(conn, plate_id, field_id=0):
    """Get image IDs for images in Plate"""

    conn.SERVICE_OPTS.setOmeroGroup('-1')
    query_service = conn.getQueryService()
    params = ParametersI()
    params.addId(plate_id)
    params.add('wsidx', rint(field_id))
    query = "select img.id "\
            "from Well well "\
            "join well.wellSamples ws "\
            "join ws.image img "\
            "where well.plate.id = :id "\
            "and index(ws) = :wsidx"
    p = query_service.projection(query, params, conn.SERVICE_OPTS)
    img_ids = [i[0].val for i in p]
    return img_ids
Beispiel #15
0
def get_data(request, data_name, conn):
    """Return table data for images in a Plate."""
    plate_id = request.GET.get('plate')
    field_id = request.GET.get('field')

    # dict of well_id: img_id
    img_ids = get_well_image_ids(conn, plate_id, field_id)
    print 'img_ids', img_ids
    query_service = conn.getQueryService()

    if data_name.startswith("Table_"):
        column_name = data_name.replace("Table_", "")

        # Load table and get data for named column
        params = ParametersI()
        params.addId(plate_id)
        query = """select oal from PlateAnnotationLink as oal
            left outer join fetch oal.child as ch
            left outer join oal.parent as pa
            where pa.id=:id
            and ch.ns='%s'""" % NSBULKANNOTATIONS
        links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS)
        shared_resources = conn.getSharedResources()
        # Just use the first Table we find
        # TODO: handle multiple tables!?
        file_id = links[0].child.file.id.val

        table = shared_resources.openTable(OriginalFileI(file_id),
                                           conn.SERVICE_OPTS)
        headers = table.getHeaders()
        column_names = [col.name for col in headers]
        col_index = column_names.index(column_name)
        rows = table.getNumberOfRows()

        # Load first column 'Well' & named column
        col_data = table.read([0, col_index], 0, rows).columns

        table_data = {}
        well_ids = col_data[0].values
        values = col_data[1].values
        for well_id, value in zip(well_ids, values):
            print 'well_id', well_id, value
            img_id = img_ids[well_id]
            table_data[img_id] = value

        return table_data
Beispiel #16
0
 def copyFiles(self, orig_img, new_img, new_pix):
     # Then attach a copy of each of the used files in the fileset
     # to the synthetic image
     params = ParametersI()
     params.addId(orig_img.id.val)
     rows = unwrap(self.query.projection((
         "select f.id, f.name from Image i "
         "join i.fileset fs join fs.usedFiles uf "
         "join uf.originalFile f where i.id = :id"), params))
     for row in rows:
         file_id = row[0]
         file_name = row[1]
         target = create_path()
         src = OriginalFileI(file_id, False)
         self.client.download(ofile=src, filename=str(target))
         copy = self.client.upload(filename=str(target),
                                   name=file_name)
         link = PixelsOriginalFileMapI()
         link.parent = copy.proxy()
         link.child = new_pix
         self.update.saveObject(link)
Beispiel #17
0
 def copyFiles(self, orig_img, new_img, new_pix):
     # Then attach a copy of each of the used files in the fileset
     # to the synthetic image
     params = ParametersI()
     params.addId(orig_img.id.val)
     rows = unwrap(
         self.query.projection(("select f.id, f.name from Image i "
                                "join i.fileset fs join fs.usedFiles uf "
                                "join uf.originalFile f where i.id = :id"),
                               params))
     for row in rows:
         file_id = row[0]
         file_name = row[1]
         target = create_path()
         src = OriginalFileI(file_id, False)
         self.client.download(ofile=src, filename=str(target))
         copy = self.client.upload(filename=str(target), name=file_name)
         link = PixelsOriginalFileMapI()
         link.parent = copy.proxy()
         link.child = new_pix
         self.update.saveObject(link)
Beispiel #18
0
def get_dataproviders(request, conn):
    # Can provide data from any table column
    plate_id = request.GET.get('plate', None)
    if plate_id is None:
        return []

    query_service = conn.getQueryService()
    params = ParametersI()
    params.addId(plate_id)
    query = """select oal from PlateAnnotationLink as oal
        left outer join fetch oal.child as ch
        left outer join oal.parent as pa
        where pa.id=:id
        and ch.ns='%s'""" % NSBULKANNOTATIONS
    links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS)
    shared_resources = conn.getSharedResources()
    # Just use the first Table we find
    # TODO: handle multiple tables!?
    file_id = links[0].child.file.id.val
    table = shared_resources.openTable(OriginalFileI(file_id),
                                       conn.SERVICE_OPTS)
    column_names = [col.name for col in table.getHeaders()]
    return ["Table_%s" % c for c in column_names]
Beispiel #19
0
    well_row = row[3].val
    well_column = row[4].val
    plate_name = row[5].val

    # Figure the expected image name for this field.

    image_name_expected = '{} [Well {}{}, Field 1]' \
        .format(plate_name, chr(65 + well_row), 1 + well_column)

    if image_name == image_name_expected:
        continue

    # Wrong image, so find the correct image for this field.

    query = "SELECT id FROM Image WHERE name = :name AND fileset.id = :id"

    params = ParametersI()
    params.add('name', wrap(image_name_expected))
    params.addId(fileset_id)

    rows = query_service.projection(query, params)

    assert len(rows) == 1

    image_id_corrected = rows[0][0].val

    print('omero obj update WellSample:{} image=Image:{}'
          .format(field_id, image_id_corrected))

conn._closeSession()
Beispiel #20
0
    def sets(self, args):
        """List filesets by various criteria

Filesets are bundles of original data imported into OMERO 5 and above
which represent 1 *or more* images.

Examples:

    bin/omero fs sets --order=newest        # Default
    bin/omero fs sets --order=oldest
    bin/omero fs sets --order=largest
    bin/omero fs sets --without-images      # Corrupt filesets
    bin/omero fs sets --with-transfer=ln_s  # Symlinked filesets
    bin/omero fs sets --check               # Proof the checksums
        """

        from omero.constants.namespaces import NSFILETRANSFER
        from omero_sys_ParametersI import ParametersI
        from omero.rtypes import unwrap

        client = self.ctx.conn(args)
        service = client.sf.getQueryService()
        admin = client.sf.getAdminService()

        if args.check and not admin.getEventContext().isAdmin:
            self.error_admin_only(fatal=True)

        annselect = (
            "(select ann.textValue from Fileset f4 "
            "join f4.annotationLinks fal join fal.child ann "
            "where f4.id = fs.id and ann.ns =:ns) ")
        select = (
            "select fs.id, fs.templatePrefix, "
            "(select size(f2.images) from Fileset f2 "
            "where f2.id = fs.id),"
            "(select size(f3.usedFiles) from Fileset f3 "
            "where f3.id = fs.id),") \
            + annselect
        query1 = (
            "from Fileset fs "
            "where 1 = 1 ")
        query2 = (
            "group by fs.id, fs.templatePrefix ")

        if args.order:
            if args.order == "newest":
                query2 += "order by fs.id desc"
            elif args.order == "oldest":
                query2 += "order by fs.id asc"
            elif args.order == "prefix":
                query2 += "order by fs.templatePrefix"

        if args.without_images:
            query = "%s and fs.images is empty %s" % (query1, query2)
        else:
            query = "%s %s" % (query1, query2)

        params = ParametersI()
        params.addString("ns", NSFILETRANSFER)
        count = service.projection("select count(fs) " + query1,
                                   params, {"omero.group": "-1"})

        params.page(args.offset, args.limit)
        objs = service.projection(select + query,
                                  params, {"omero.group": "-1"})
        objs = unwrap(objs)
        count = unwrap(count)[0][0]

        cols = ["Id", "Prefix", "Images", "Files", "Transfer"]
        if args.check:
            cols.append("Check")
        tb = self._table(args)
        tb.cols(cols)
        tb.page(args.offset, args.limit, count)

        # Map any requested transfers as well
        if args.with_transfer:
            restricted = [TRANSFERS.get(x, x) for x in args.with_transfer[0]]
        else:
            restricted = None

        for idx, obj in enumerate(objs):

            # Map the transfer name to the CLI symbols
            ns = obj[-1]
            if ns is None:
                ns = ""
            elif ns in TRANSFERS:
                ns = TRANSFERS[ns]
            obj[-1] = ns

            # Filter based on the ns symbols
            if restricted and ns not in restricted:
                continue

            # Now perform check if required
            if args.check:
                from omero.grid import RawAccessRequest
                desc, prx = self.get_managed_repo(client)
                ctx = client.getContext(group=-1)
                check_params = ParametersI()
                check_params.addId(obj[0])
                rows = service.projection((
                    "select h.value, f.hash, "
                    "f.path || '/' || f.name "
                    "from Fileset fs join fs.usedFiles uf "
                    "join uf.originalFile f join f.hasher h "
                    "where fs.id = :id"
                    ), check_params, ctx)

                if not rows:
                    obj.append("Empty")

                err = None
                for row in rows:
                    row = unwrap(row)
                    raw = RawAccessRequest()
                    raw.repoUuid = desc.hash.val
                    raw.command = "checksum"
                    raw.args = map(str, row)
                    try:
                        cb = client.submit(raw)
                        cb.close(True)
                    except CmdError, ce:
                        err = ce.err
                        self.ctx.dbg(err)

                if err:
                    obj.append("ERROR!")
                elif rows:
                    obj.append("OK")

            tb.row(idx, *tuple(obj))
Beispiel #21
0
    def sets(self, args):
        """List filesets by various criteria

Filesets are bundles of original data imported into OMERO 5 and above
which represent 1 *or more* images.

Examples:

    bin/omero fs sets --order=newest        # Default
    bin/omero fs sets --order=oldest
    bin/omero fs sets --order=largest
    bin/omero fs sets --without-images      # Corrupt filesets
    bin/omero fs sets --with-transfer=ln_s  # Symlinked filesets
    bin/omero fs sets --check               # Proof the checksums
        """

        from omero.constants.namespaces import NSFILETRANSFER
        from omero_sys_ParametersI import ParametersI
        from omero.rtypes import unwrap
        from omero.cmd import OK

        client = self.ctx.conn(args)
        service = client.sf.getQueryService()

        select = (
            "select fs.id, fs.templatePrefix, "
            "(select size(f2.images) from Fileset f2 where f2.id = fs.id),"
            "(select size(f3.usedFiles) from Fileset f3 where f3.id = fs.id),"
            "ann.textValue ")
        query1 = (
            "from Fileset fs "
            "left outer join fs.annotationLinks fal "
            "left outer join fal.child ann "
            "where (ann is null or ann.ns = :ns) ")
        query2 = (
            "group by fs.id, fs.templatePrefix, ann.textValue ")

        if args.order:
            if args.order == "newest":
                query2 += "order by fs.id desc"
            elif args.order == "oldest":
                query2 += "order by fs.id asc"
            elif args.order == "prefix":
                query2 += "order by fs.templatePrefix"

        if args.without_images:
            query = "%s and fs.images is empty %s" % (query1, query2)
        else:
            query = "%s %s" % (query1, query2)

        params = ParametersI()
        params.addString("ns", NSFILETRANSFER)
        count = service.projection("select count(fs) " + query1,
                                   params, {"omero.group": "-1"})

        params.page(args.offset, args.limit)
        objs = service.projection(select + query,
                                  params, {"omero.group": "-1"})
        objs = unwrap(objs)
        count = unwrap(count)[0][0]

        cols = ["Id", "Prefix", "Images", "Files", "Transfer"]
        if args.check:
            cols.append("Check")
        tb = self._table(args)
        tb.cols(cols)
        tb.page(args.offset, args.limit, count)
        for idx, obj in enumerate(objs):

            # Map the transfer name to the CLI symbols
            ns = obj[-1]
            if ns is None:
                ns = ""
            elif ns in TRANSFERS:
                ns = TRANSFERS[ns]
            obj[-1] = ns

            # Map any requested transfers as well
            allowed = args.with_transfer is not None \
                and args.with_transfer or []
            for idx, x in enumerate(allowed):
                x = x[0]  # Strip argparse wrapper
                x = TRANSFERS.get(x, x)  # map
                allowed[idx] = x

            # Filter based on the ns symbols
            if allowed:
                if ns not in allowed:
                    continue

            # Now perform check if required
            if args.check:
                from omero.grid import RawAccessRequest
                desc, prx = self.get_managed_repo(client)
                ctx = client.getContext(group=-1)
                check_params = ParametersI()
                check_params.addId(obj[0])
                rows = service.projection((
                    "select h.value, f.hash, "
                    "f.path || '/' || f.name "
                    "from Fileset fs join fs.usedFiles uf "
                    "join uf.originalFile f join f.hasher h "
                    "where fs.id = :id"
                    ), check_params, ctx)

                if not rows:
                    obj.append("Empty")

                err = None
                for row in rows:
                    row = unwrap(row)
                    raw = RawAccessRequest()
                    raw.repoUuid = desc.hash.val
                    raw.command = "checksum"
                    raw.args = map(str, row)
                    cb = client.submit(raw)
                    try:
                        rsp = cb.getResponse()
                        if not isinstance(rsp, OK):
                            err = rsp
                            break
                    finally:
                        cb.close(True)

                if err:
                    obj.append("ERROR!")
                elif rows:
                    obj.append("OK")

            tb.row(idx, *tuple(obj))
        self.ctx.out(str(tb.build()))
Beispiel #22
0
def run():
    """
    """
    dataTypes = [rstring("Plate")]

    client = scripts.client(
        "Manage_Plate_Acquisitions.py",
        "Add or remove PlateAcquisition(s) in a given Plate",
        scripts.String("Data_Type",
                       optional=False,
                       grouping="1",
                       description="The data type you want to work with.",
                       values=dataTypes,
                       default="Plate"),
        scripts.List("IDs",
                     optional=False,
                     grouping="2",
                     description="List of Plate IDs").ofType(rlong(0)),
        scripts.String("Mode",
                       optional=False,
                       grouping="3",
                       description="Select if you want to add or "
                       "remove PlateAcquisitions",
                       values=[rstring("Add"),
                               rstring("Remove")],
                       default="Add"),
        version="0.2",
        authors=["Niko Klaric"],
        institutions=["Glencoe Software Inc."],
        contact="*****@*****.**",
    )

    try:
        scriptParams = {}
        for key in client.getInputKeys():
            if client.getInput(key):
                scriptParams[key] = client.getInput(key, unwrap=True)

        connection = BlitzGateway(client_obj=client)
        updateService = connection.getUpdateService()
        queryService = connection.getQueryService()

        processedMessages = []

        for plateId in scriptParams["IDs"]:
            plateObj = connection.getObject("Plate", plateId)
            if plateObj is None:
                client.setOutput(
                    "Message", rstring("ERROR: No Plate with ID %s" % plateId))
                return

            if scriptParams["Mode"] == "Add":
                plateAcquisitionObj = PlateAcquisitionI()
                plateAcquisitionObj.setPlate(PlateI(plateObj.getId(), False))

                wellGrid = plateObj.getWellGrid()
                for axis in wellGrid:
                    for wellObj in axis:
                        wellSampleList = wellObj.copyWellSamples()
                        plateAcquisitionObj.addAllWellSampleSet(wellSampleList)

                plateAcquisitionObj = updateService.saveAndReturnObject(
                    plateAcquisitionObj)
                plateAcquisitionId = plateAcquisitionObj.getId()._val

                processedMessages.append(
                    "Linked new PlateAcquisition with ID %d"
                    " to Plate with ID %d." % (plateAcquisitionId, plateId))
            else:
                params = ParametersI()
                params.addId(plateId)

                queryString = """
                    FROM PlateAcquisition AS pa
                    LEFT JOIN FETCH pa.wellSample
                    LEFT OUTER JOIN FETCH pa.annotationLinks
                        WHERE pa.plate.id = :id
                    """
                plateAcquisitionList = queryService.findAllByQuery(
                    queryString, params, connection.SERVICE_OPTS)
                if plateAcquisitionList:
                    updateList = []

                    for plate_acquisition in plateAcquisitionList:
                        for well_sample in plate_acquisition.copyWellSample():
                            well_sample.setPlateAcquisition(None)
                            updateList.append(well_sample)

                        updateService.saveArray(updateList)

                        plate_acquisition.clearWellSample()
                        plate_acquisition.clearAnnotationLinks()

                        plate_acquisition = updateService.saveAndReturnObject(
                            plate_acquisition)
                        updateService.deleteObject(plate_acquisition)

                processedMessages.append(
                    "%d PlateAcquisition(s) removed from Plate with ID %d." %
                    (len(plateAcquisitionList), plateId))

        client.setOutput(
            "Message", rstring("No errors. %s" % " ".join(processedMessages)))
    finally:
        client.closeSession()
Beispiel #23
0
    def sets(self, args):
        """List filesets by various criteria

Filesets are bundles of original data imported into OMERO 5 and above
which represent 1 *or more* images.

Examples:

    bin/omero fs sets --order=newest        # Default
    bin/omero fs sets --order=oldest
    bin/omero fs sets --order=largest
    bin/omero fs sets --without-images      # Corrupt filesets
    bin/omero fs sets --with-transfer=ln_s  # Symlinked filesets
    bin/omero fs sets --check               # Proof the checksums
        """

        from omero.constants.namespaces import NSFILETRANSFER
        from omero_sys_ParametersI import ParametersI
        from omero.rtypes import unwrap

        client = self.ctx.conn(args)
        service = client.sf.getQueryService()
        admin = client.sf.getAdminService()

        if args.check and not admin.getEventContext().isAdmin:
            self.error_admin_only(fatal=True)

        annselect = (
            "(select ann.textValue from Fileset f4 "
            "join f4.annotationLinks fal join fal.child ann "
            "where f4.id = fs.id and ann.ns =:ns) "
        )
        select = (
            "select fs.id, fs.templatePrefix, "
            "(select size(f2.images) from Fileset f2 "
            "where f2.id = fs.id),"
            "(select size(f3.usedFiles) from Fileset f3 "
            "where f3.id = fs.id),"
        ) + annselect
        query1 = "from Fileset fs " "where 1 = 1 "
        query2 = "group by fs.id, fs.templatePrefix "

        if args.order:
            if args.order == "newest":
                query2 += "order by fs.id desc"
            elif args.order == "oldest":
                query2 += "order by fs.id asc"
            elif args.order == "prefix":
                query2 += "order by fs.templatePrefix"

        if args.without_images:
            query = "%s and fs.images is empty %s" % (query1, query2)
        else:
            query = "%s %s" % (query1, query2)

        params = ParametersI()
        params.addString("ns", NSFILETRANSFER)
        count = service.projection("select count(fs) " + query1, params, {"omero.group": "-1"})

        params.page(args.offset, args.limit)
        objs = service.projection(select + query, params, {"omero.group": "-1"})
        objs = unwrap(objs)
        count = unwrap(count)[0][0]

        cols = ["Id", "Prefix", "Images", "Files", "Transfer"]
        if args.check:
            cols.append("Check")
        tb = self._table(args)
        tb.cols(cols)
        tb.page(args.offset, args.limit, count)

        # Map any requested transfers as well
        if args.with_transfer:
            restricted = [TRANSFERS.get(x, x) for x in args.with_transfer[0]]
        else:
            restricted = None

        for idx, obj in enumerate(objs):

            # Map the transfer name to the CLI symbols
            ns = obj[-1]
            if ns is None:
                ns = ""
            elif ns in TRANSFERS:
                ns = TRANSFERS[ns]
            obj[-1] = ns

            # Filter based on the ns symbols
            if restricted and ns not in restricted:
                continue

            # Now perform check if required
            if args.check:
                from omero.grid import RawAccessRequest

                desc, prx = self.get_managed_repo(client)
                ctx = client.getContext(group=-1)
                check_params = ParametersI()
                check_params.addId(obj[0])
                rows = service.projection(
                    (
                        "select h.value, f.hash, "
                        "f.path || '/' || f.name "
                        "from Fileset fs join fs.usedFiles uf "
                        "join uf.originalFile f join f.hasher h "
                        "where fs.id = :id"
                    ),
                    check_params,
                    ctx,
                )

                if not rows:
                    obj.append("Empty")

                err = None
                for row in rows:
                    row = unwrap(row)
                    raw = RawAccessRequest()
                    raw.repoUuid = desc.hash.val
                    raw.command = "checksum"
                    raw.args = map(str, row)
                    try:
                        cb = client.submit(raw)
                        cb.close(True)
                    except CmdError, ce:
                        err = ce.err
                        self.ctx.dbg(err)

                if err:
                    obj.append("ERROR!")
                elif rows:
                    obj.append("OK")

            tb.row(idx, *tuple(obj))
    well_row = row[3].val
    well_column = row[4].val
    plate_name = row[5].val

    # Figure the expected image name for this field.

    image_name_expected = '{} [Well {}{}, Field 1]' \
        .format(plate_name, chr(65 + well_row), 1 + well_column)

    if image_name == image_name_expected:
        continue

    # Wrong image, so find the correct image for this field.

    query = "SELECT id FROM Image WHERE name = :name AND fileset.id = :id"

    params = ParametersI()
    params.add('name', wrap(image_name_expected))
    params.addId(fileset_id)

    rows = query_service.projection(query, params)

    assert len(rows) == 1

    image_id_corrected = rows[0][0].val

    print('omero obj update WellSample:{} image=Image:{}'.format(
        field_id, image_id_corrected))

conn._closeSession()
def run():
    """
    """
    dataTypes = [rstring("Plate")]

    client = scripts.client(
        "Manage_Plate_Acquisitions.py",
        "Add or remove PlateAcquisition(s) in a given Plate",

        scripts.String("Data_Type", optional=False, grouping="1",
                       description="The data type you want to work with.",
                       values=dataTypes,
                       default="Plate"),

        scripts.List("IDs", optional=False, grouping="2",
                     description="List of Plate IDs").ofType(rlong(0)),

        scripts.String("Mode", optional=False, grouping="3",
                       description="Select if you want to add or "
                                   "remove PlateAcquisitions",
                       values=[rstring("Add"), rstring("Remove")],
                       default="Add"),

        version="0.2",
        authors=["Niko Klaric"],
        institutions=["Glencoe Software Inc."],
        contact="*****@*****.**",
    )

    try:
        scriptParams = {}
        for key in client.getInputKeys():
            if client.getInput(key):
                scriptParams[key] = client.getInput(key, unwrap=True)

        connection = BlitzGateway(client_obj=client)
        updateService = connection.getUpdateService()
        queryService = connection.getQueryService()

        processedMessages = []

        for plateId in scriptParams["IDs"]:
            plateObj = connection.getObject("Plate", plateId)
            if plateObj is None:
                client.setOutput(
                    "Message",
                    rstring("ERROR: No Plate with ID %s" % plateId))
                return

            if scriptParams["Mode"] == "Add":
                plateAcquisitionObj = PlateAcquisitionI()
                plateAcquisitionObj.setPlate(PlateI(plateObj.getId(), False))

                wellGrid = plateObj.getWellGrid()
                for axis in wellGrid:
                    for wellObj in axis:
                        wellSampleList = wellObj.copyWellSamples()
                        plateAcquisitionObj.addAllWellSampleSet(wellSampleList)

                plateAcquisitionObj = updateService.saveAndReturnObject(
                    plateAcquisitionObj)
                plateAcquisitionId = plateAcquisitionObj.getId()._val

                processedMessages.append(
                    "Linked new PlateAcquisition with ID %d"
                    " to Plate with ID %d." % (plateAcquisitionId, plateId))
            else:
                params = ParametersI()
                params.addId(plateId)

                queryString = """
                    FROM PlateAcquisition AS pa
                    LEFT JOIN FETCH pa.wellSample
                    LEFT OUTER JOIN FETCH pa.annotationLinks
                        WHERE pa.plate.id = :id
                    """
                plateAcquisitionList = queryService.findAllByQuery(
                    queryString, params, connection.SERVICE_OPTS)
                if plateAcquisitionList:
                    updateList = []

                    for plate_acquisition in plateAcquisitionList:
                        for well_sample in plate_acquisition.copyWellSample():
                            well_sample.setPlateAcquisition(None)
                            updateList.append(well_sample)

                        updateService.saveArray(updateList)

                        plate_acquisition.clearWellSample()
                        plate_acquisition.clearAnnotationLinks()

                        plate_acquisition = updateService.saveAndReturnObject(
                            plate_acquisition)
                        updateService.deleteObject(plate_acquisition)

                processedMessages.append(
                    "%d PlateAcquisition(s) removed from Plate with ID %d." %
                    (len(plateAcquisitionList), plateId))

        client.setOutput("Message", rstring("No errors. %s" %
                         " ".join(processedMessages)))
    finally:
        client.closeSession()
def processImages(client, conn, scriptParams):
    """
    Process the script params to make a list of channel_offsets, then iterate
    through the images creating a new image from each with the specified
    channel offsets
    """

    message = ""

    # Get the images
    objects, logMessage = script_utils.getObjects(conn, scriptParams)
    message += logMessage
    if not objects:
        return None, None, message

    # Concatenate images from datasets
    if scriptParams["Data_Type"] == "Image":
        images = objects
    else:
        images = []
        for ds in objects:
            images += ds.listChildren()

    queryService = conn.getQueryService()
    roiService = conn.getRoiService()

    print "Showing X ; Y coordinates in micrometer"

    for image in images:

         print "---------- {0} ---------- ".format(image.getName())

         metadata = dict(image.loadOriginalMetadata()[1])
         
         params = ParametersI()
         params.addId(image.getId())
         
         roiResult = roiService.findByImage(image.getId(), None)

         for roi in roiResult.rois:
              for shape in roi.copyShapes():
                   if type(shape) != omero.model.PointI:
                        continue
                   
                   # From tem-hole-finder:XYpic2XYstage.m
                   # RotM=1e-9*[tt(1),tt(2);tt(3),tt(4)];
                   # Offset=1e-6*[tt(5),tt(6)];
                   # XYstageHoles=RotM*XYpixHolesOverview'+repmat(Offset',[1,l1]);

                   # TODO: Eval is of course not really safe...
                   tt = eval(metadata['Conversion matrix'])
                   
                   RotM = [x * 1e-9 for x in [tt[0], tt[1], tt[2], tt[3]]]
                   Offset = [x * 1e-6 for x in [tt[4], tt[5]]]

                   xRoi = int(shape.getCx().getValue())
                   yRoi = int(shape.getCy().getValue())

                   stageX = RotM[0] * xRoi + RotM[1] * yRoi + Offset[0]
                   stageY = RotM[2] * xRoi + RotM[3] * yRoi + Offset[1]
                   name = roi.getName().getValue() if roi.getName() is not None else "Unnamed"

                   print "{0} [ {1} ; {2} ] ".format(name, stageX * 1e6, stageY * 1e6)

         print "--------------------------------------"

    return "Finished calculating"
Beispiel #27
0
parser.add_argument("target_image", type=int, help="output image")
ns = parser.parse_args()

image_id_dst = ns.target_image

local = BlitzGateway(ns.to_user, ns.to_pass, host=ns.to_host, secure=True)

local.connect()

query_service = local.getQueryService()
update_service = local.getUpdateService()

query = "FROM Image WHERE id = :id"

params = ParametersI()
params.addId(image_id_dst)

count = 0
image = query_service.findByQuery(query, params)


def make_circle(h, w):
    x = np.arange(0, w)
    y = np.arange(0, h)
    arr = np.zeros((y.size, x.size), dtype=bool)

    cx = w // 2
    cy = h // 2
    r = min(w, h) // 2

    mask = (x[np.newaxis, :] - cx) ** 2 + (y[:, np.newaxis] - cy) ** 2 < r**2
def get_script(request, script_name, conn):
    """Return a JS function to filter images by various params."""
    plate_id = request.GET.get('plate')
    query_service = conn.getQueryService()

    if plate_id is None:
        return JsonResponse({'Error': 'Plate ID not specified'})

    if script_name == "Table":

        params = ParametersI()
        params.addId(plate_id)
        query = """select oal from PlateAnnotationLink as oal
            left outer join fetch oal.child as ch
            left outer join oal.parent as pa
            where pa.id=:id
            and ch.ns='%s'""" % NSBULKANNOTATIONS
        links = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS)
        shared_resources = conn.getSharedResources()
        # Just use the first Table we find
        # TODO: handle multiple tables!?
        file_id = links[0].child.file.id.val

        table = shared_resources.openTable(OriginalFileI(file_id),
                                           conn.SERVICE_OPTS)
        if not table:
            return JsonResponse({'ERROR': 'Failed to open table'})

        headers = table.getHeaders()
        rows = table.getNumberOfRows()

        column_names = [col.name for col in headers]
        col_data = table.read(range(len(headers)), 0, rows).columns

        table_data = {}
        for name, col in zip(column_names, col_data):
            # key is column Name, values are list of col_data
            table_data[name] = col.values

        # Return a JS function that will be passed an object
        # e.g. {'type': 'Image', 'id': 1}
        # and should return true or false
        f = """(function filter(data, params) {
            if (isNaN(params.count) || params.count == '') return true;
            var table_data = %s;
            var rowIndex = table_data.Well.indexOf(data.wellId);
            var value = table_data[params.column_name][rowIndex];
            if (params.operator === '=') return value == params.count;
            if (params.operator === '<') return value < params.count;
            if (params.operator === '>') return value > params.count;
        })
        """ % json.dumps(table_data)

        filter_params = [{'name': 'column_name',
                          'type': 'text',
                          'values': column_names[1:],   # 1st column is Well
                          'default': column_names[1]},
                         {'name': 'operator',
                          'type': 'text',
                          'values': ['>', '=', '<'],
                          'default': '>'},
                         {'name': 'count',
                          'type': 'number',
                          'default': ''}]
        return JsonResponse(
            {
                'f': f,
                'params': filter_params,
            })
Beispiel #29
0
image_id_src = ns.source_image
image_id_dst = ns.target_image

idr = BlitzGateway(ns.from_user, ns.from_pass, host=ns.from_host, secure=True)
local = BlitzGateway(ns.to_user, ns.to_pass, host=ns.to_host, secure=True)

idr.connect()
local.connect()

query_service = idr.getQueryService()
update_service = local.getUpdateService()

query = "FROM Mask WHERE roi.image.id = :id"

params = ParametersI()
params.addId(image_id_src)

count = 0

for mask_src in query_service.findAllByQuery(query, params):
    mask_dst = MaskI()
    mask_dst.x = mask_src.x
    mask_dst.y = mask_src.y
    mask_dst.width = mask_src.width
    mask_dst.height = mask_src.height
    mask_dst.theZ = mask_src.theZ
    mask_dst.theC = mask_src.theC
    mask_dst.theT = mask_src.theT
    mask_dst.bytes = mask_src.bytes
    mask_dst.fillColor = mask_src.fillColor
    mask_dst.transform = mask_src.transform