def create_annotation_table(connection, table_name, column_names, column_descriptions, values, namespace=None, table_description=None): """Creates a table annotation from a list of lists""" table_name = f'{table_name}_{"".join([choice(ascii_letters) for n in range(32)])}.h5' columns = _create_table(column_names=column_names, columns_descriptions=column_descriptions, values=values) resources = connection.c.sf.sharedResources() repository_id = resources.repositories().descriptions[0].getId().getValue() table = resources.newTable(repository_id, table_name) table.initialize(columns) table.addData(columns) original_file = table.getOriginalFile() table.close() # when we are done, close. file_ann = gw.FileAnnotationWrapper(connection) file_ann.setNs(namespace) file_ann.setDescription(table_description) file_ann.setFile(model.OriginalFileI( original_file.id.val, False)) # TODO: try to get this with a wrapper file_ann.save() return file_ann
def create_table(self, dataset_id, dataframe, table_name): columns = dataframe.columns resources = self.SESSION.sharedResources() # resources = conn.c.sf.sharedResources() repository_id = resources.repositories().descriptions[0].getId( ).getValue() # create columns and data types, and populate with data data_types = dataframe.dtypes table_data = [] init_cols = [] for index, col in enumerate(dataframe.columns): if dataframe[col].dtype == object: max_len = dataframe[col].str.len().max() init_col = grid.StringColumn(col, '', max_len, []) init_cols.append(init_col) data_col = grid.StringColumn( col, '', max_len, list(dataframe.iloc[:, index].values)) table_data.append(data_col) table = resources.newTable(repository_id, ''.join(["/", table_name, ".h5"])) # table = resources.newTable(dataset_id, table_name) table.initialize(init_cols) table.addData(table_data) # note that this worked after the table.close() statement was invoked in 5.4.10 orig_file = table.getOriginalFile() table.close() # when we are done, close. orig_file_id = orig_file.id.val # ...so you can attach this data to an object e.g. Dataset file_ann = model.FileAnnotationI() # use unloaded OriginalFileI file_ann.setFile(model.OriginalFileI(orig_file_id, False)) file_ann = self.SESSION.getUpdateService().saveAndReturnObject( file_ann) link = model.DatasetAnnotationLinkI() link.setParent(model.DatasetI(dataset_id, False)) link.setChild(model.FileAnnotationI(file_ann.getId().getValue(), False)) table = self.SESSION.getUpdateService().saveAndReturnObject(link) return table
def save_original_file(connection, name, path, mimetype, size, sha1): of = get_original_file(connection, name, mimetype) if of is None: of = om.OriginalFileI() of.setName(ot.wrap(name)) of.setPath(ot.wrap(path)) of.setMimetype(ot.wrap(mimetype)) of.setSize(ot.rlong(size)) of.setHash(ot.wrap(sha1)) hasher = om.ChecksumAlgorithmI() hasher.setValue(ot.wrap(ChecksumAlgorithmSHA1160)) of.setHasher(hasher) of = connection.getUpdateService().saveAndReturnObject(of) return of.getId().getValue() else: raise DuplicatedEntryError( 'OriginalFile with name %s and mimetype %s already exists' % (name, mimetype))