Ejemplo n.º 1
0
    def testtables(self, args):
        "Tests whether tables can be created and initialized"
        client, conn = self._clientconn(args)

        sf = client.getSession()
        sr = sf.sharedResources()
        table = sr.newTable(1, 'testtables')
        if table is None:
            self.ctx.die(100, "Failed to create Table")

        # If we have a table...
        initialized = False
        try:
            table.initialize([LongColumn('ID', '', [])])
            initialized = True
        except:
            pass
        finally:
            table.close()

        try:
            orig_file = table.getOriginalFile()
            conn.deleteObject(orig_file)
        except:
            # Anything else to do here?
            pass

        if not initialized:
            self.ctx.die(100, "Failed to initialize Table")
def save_results(conn, files, plate):
    # Upload the results as OMERO.table
    print("saving results...")
    Nuclei = pandas.concat(files, ignore_index=True)
    summary = Nuclei.groupby('Image').mean()
    # Free memory
    del Nuclei
    cols = []
    for col in summary.columns:
        if col == 'Image':
            cols.append(ImageColumn(col, '', summary[col]))
        elif col == 'Well':
            cols.append(WellColumn(col, '', summary[col]))
        elif summary[col].dtype == 'int64':
            cols.append(LongColumn(col, '', summary[col]))
        elif summary[col].dtype == 'float64':
            cols.append(DoubleColumn(col, '', summary[col]))
    resources = conn.c.sf.sharedResources()
    repository_id = resources.repositories().descriptions[0].getId().getValue()
    table_name = "idr0002_cellprofiler"
    table = resources.newTable(repository_id, table_name)
    table.initialize(cols)
    table.addData(cols)

    # Link the table to the plate
    orig_file = table.getOriginalFile()
    file_ann = FileAnnotationWrapper(conn)
    file_ann.setNs(NSBULKANNOTATIONS)
    file_ann._obj.file = OriginalFileI(orig_file.id.val, False)
    file_ann.save()
    plate.linkAnnotation(file_ann)
    table.close()
Ejemplo n.º 3
0
 def parse(self):
     log.info("Parsing: %s" % self.original_file.name.val)
     provider = self.original_file_provider
     data = provider.get_original_file_data(self.original_file)
     try:
         events = ('start', 'end')
         well_data = None
         n_roi = 0
         n_measurements = 0
         cells_columns = {'Image': ImageColumn('Image', '', list()),
                          'Cell': LongColumn('Cell', '', list()),
                          'ROI': RoiColumn('ROI', '', list())
                          }
         organelles_columns = {'Image': ImageColumn('Image', '', list()),
                               'Cell': LongColumn('Cell', '', list()),
                               }
         nuclei_columns = {'Image': ImageColumn('Image', '', list()),
                           'Cell': LongColumn('Cell', '', list()),
                           'ROI': RoiColumn('ROI', '', list())
                           }
         for event, element in iterparse(data, events=events):
             if event == 'start' and element.tag == 'WellData' \
                and element.get('cell') != 'Summary':
                 row = int(element.get('row')) - 1
                 col = int(element.get('col')) - 1
                 i = int(element.get('field')) - 1
                 try:
                     well, images = self.get_well_images(row, col)
                     if not images:
                         continue
                     image = images[i]
                 except:
                     log.exception("ERROR: Failed to get well images")
                     continue
                 self.check_sparse_data(cells_columns.values())
                 self.check_sparse_data(nuclei_columns.values())
                 self.check_sparse_data(organelles_columns.values())
                 cell = long(element.get('cell'))
                 cells_columns['Cell'].values.append(cell)
                 nuclei_columns['Cell'].values.append(cell)
                 organelles_columns['Cell'].values.append(cell)
                 well_data = element
                 cells_columns['Image'].values.append(image.id.val)
                 nuclei_columns['Image'].values.append(image.id.val)
                 organelles_columns['Image'].values.append(image.id.val)
             elif well_data is not None and event == 'start' \
                     and element.tag == 'Measure':
                 source = element.get('source')
                 key = element.get('key')
                 value = float(element.get('value'))
                 if source == self.CELLS_SOURCE:
                     columns_list = [cells_columns]
                 elif source == self.NUCLEI_SOURCE:
                     columns_list = [nuclei_columns]
                 elif source == self.ORGANELLES_SOURCE:
                     columns_list = [organelles_columns]
                 else:
                     columns_list = [cells_columns, nuclei_columns,
                                     organelles_columns]
                 for columns in columns_list:
                     if key not in columns:
                         columns[key] = DoubleColumn(key, '', list())
                     columns[key].values.append(value)
                 n_measurements += 1
             elif event == 'end' and element.tag == 'WellData':
                 if well_data is not None:
                     n_roi += 1
                     well_data.clear()
                     well_data = None
             else:
                 element.clear()
         # Final row sparseness check
         self.check_sparse_data(cells_columns.values())
         self.check_sparse_data(nuclei_columns.values())
         self.check_sparse_data(organelles_columns.values())
         log.info("Total ROI: %d" % n_roi)
         log.info("Total measurements: %d" % n_measurements)
         sets_of_columns = [cells_columns.values(), nuclei_columns.values(),
                            organelles_columns.values()]
         return MeasurementParsingResult(sets_of_columns)
     finally:
         data.close()