def _save_samples(samples, acls, sample_url, token): """""" saved_samples = [] for data in samples: sample = data['sample'] prev_sample = data['prev_sample'] name = data['name'] sample_id, sample_ver = save_sample(sample, sample_url, token, previous_version=prev_sample) saved_samples.append({ "id": sample_id, "name": name, "version": sample_ver }) # check input for any reason to update access control list # should have a "write", "read", "admin" entry writer = data.get('write') reader = data.get('read') admin = data.get('admin') if writer or reader or admin: acls["read"] += [r for r in reader] acls["write"] += [w for w in writer] acls["admin"] += [a for a in admin] if len(acls["read"]) > 0 or len(acls['write']) > 0 or len( acls['admin']) > 0: _ = update_acls(sample_url, sample_id, acls, token) return saved_samples
def update_sample_set_acls(self, ctx, params): """ :param params: instance of type "update_sample_set_acls_params" -> structure: parameter "workspace_name" of String, parameter "workspace_id" of Long, parameter "sample_set_ref" of String, parameter "new_users" of list of String, parameter "is_reader" of Long, parameter "is_writer" of Long, parameter "is_admin" of Long, parameter "share_within_workspace" of Long :returns: instance of type "update_sample_set_acls_output" -> structure: parameter "status" of String """ # ctx is the context object # return variables are: output #BEGIN update_sample_set_acls # first get sample_set object sample_set_ref = params.get('sample_set_ref') ret = self.dfu.get_objects({'object_refs': [sample_set_ref]})['data'][0] sample_set = ret['data'] sample_url = get_sample_service_url(self.sw_url) acls = {'read': [], 'write': [], 'admin': []} if params.get('share_within_workspace'): acls = get_workspace_user_perms(self.workspace_url, params.get('workspace_id'), ctx['token'], ctx['user_id'], acls) for new_user in params.get('new_users'): if params.get('is_admin'): acls['admin'].append(new_user) elif params.get('is_writer'): acls['write'].append(new_user) elif params.get('is_reader'): acls['read'].append(new_user) for sample in sample_set['samples']: sample_id = sample['id'] status = update_acls(sample_url, sample_id, acls, ctx['token']) output = {"status": status} #END update_sample_set_acls # At some point might do deeper type checking... if not isinstance(output, dict): raise ValueError('Method update_sample_set_acls return value ' + 'output is not type dict as required.') # return the results return [output]
def produce_samples( df, cols, column_groups, column_unit_regex, sample_url, token, existing_samples, columns_to_input_names, acls ): """""" samples = [] existing_sample_names = {sample['name']: sample for sample in existing_samples} for idx, row in df.iterrows(): if row.get('id'): # first we check if a 'kbase_sample_id' column is specified kbase_sample_id = None if row.get('kbase_sample_id'): kbase_sample_id = str(row.pop('kbase_sample_id')) if 'kbase_sample_id' in cols: cols.pop(cols.index('kbase_sample_id')) # use name field as name, if there is non-reuse id. if row.get('name'): name = str(row['name']) else: name = str(row['id']) if row.get('parent_id'): parent = str(row.pop('parent_id')) if 'parent_id' in cols: cols.pop(cols.index('parent_id')) if 'id' in cols: cols.pop(cols.index('id')) if 'name' in cols: cols.pop(cols.index('name')) controlled_metadata = generate_controlled_metadata( row, column_groups ) user_metadata = generate_user_metadata( row, cols, column_groups, column_unit_regex ) source_meta = generate_source_meta( row, controlled_metadata.keys(), columns_to_input_names ) sample = { 'node_tree': [{ "id": str(row['id']), "parent": None, "type": "BioReplicate", "meta_controlled": controlled_metadata, "meta_user": user_metadata, 'source_meta': source_meta }], 'name': name, } prev_sample = None if kbase_sample_id: prev_sample = get_sample({"id": kbase_sample_id}, sample_url, token) if name in existing_sample_names and prev_sample['name'] == name: # now we check if the sample 'id' and 'name' are the same if existing_sample_names[name]['id'] != prev_sample['id']: raise ValueError(f"'kbase_sample_id' and input sample set have different ID's for sample: {name}") elif name in existing_sample_names and name != prev_sample['name']: # not sure if this is an error case raise ValueError(f"Cannot rename existing sample from {prev_sample['name']} to {name}") elif name in existing_sample_names: prev_sample = get_sample(existing_sample_names[name], sample_url, token) if compare_samples(sample, prev_sample): if sample.get('name') not in existing_sample_names: existing_sample_names[sample['name']] = prev_sample continue elif name in existing_sample_names: existing_sample_names.pop(name) sample_id, sample_ver = save_sample(sample, sample_url, token, previous_version=prev_sample) samples.append({ "id": sample_id, "name": name, "version": sample_ver }) # check input for any reason to update access control list # should have a "write", "read", "admin" entry writer = row.get('write') reader = row.get('read') admin = row.get('admin') if writer or reader or admin: acls["read"] += [r for r in reader] acls["write"] += [w for w in writer] acls["admin"] += [a for a in admin] if len(acls["read"]) > 0 or len(acls['write']) > 0 or len(acls['admin']) > 0: resp = update_acls(sample_url, sample_id, acls, token) else: raise RuntimeError(f"{row.get('id')} evaluates as false - {row.keys()}") # add the missing samples from existing_sample_names samples += [existing_sample_names[key] for key in existing_sample_names] return samples