def generate_copo_samples_data(profile_id): # This method generates the 'json' for building the UI table s = Sample(profile_id) src = Source(profile_id) samples = s.get_all_samples() columns = [] dataSet = [] # headers for el in src.schema: if not hasattr(el, 'show_in_table') or el.show_in_table == 'true': columns.append({"title": el.label}) for el in s.schema: if not hasattr(el, 'show_in_table') or el.show_in_table == 'true': columns.append({"title": el.label}) columns.append({"title": " "}) # extra 'blank' header for record actions column # data for sm in samples: source = Source(profile_id).get_source_from_sample_id(sm['source_id']) src_row = get_record_data(source, src.schema) src_row.pop() sm_row = get_record_data(sm, s.schema) dataSet.append(src_row + sm_row) return {"columns": columns, "dataSet": dataSet, "table_id": "sample_table"}
def get_isasamples_json(): """ returns isa samples in a profile :return: """ from dal.copo_da import Sample profile_id = get_current_request().session['profile_id'] samples = Sample(profile_id).get_all_records() value_field = str("id") label_field = str("sample_name") search_field = ["id", "sample_name"] secondary_label_field = ["meta_sample_name"] elem_json = dict(value_field=value_field, label_field=label_field, secondary_label_field=secondary_label_field, search_field=search_field, options=list()) for sd in samples: if sd["sample_type"] == "isasample": elem_json.get("options").append({ value_field: str(sd["_id"]), label_field: sd["name"], secondary_label_field[0]: sd["name"], "sample_type": sd.get("sample_type", str()) }) return elem_json
def get_samples_options(): from dal.copo_da import Sample profile_id = get_current_request().session['profile_id'] samples = Sample(profile_id).get_all_records() option_values = [] for sd in samples: label = sd["name"] option_values.append({"value": str(sd["_id"]), "label": label}) return option_values
def get_sample(df): # get sample from datafile s_id = df["description"]["attributes"]["attach_study_samples"][ "sample_copo"] if s_id != "": smp = Sample().get_record(s_id) else: s_id = df["description"]["attributes"]["attach_study_samples"][ "sample_ena"] # TODO - get sample metadata from ENA tools return smp
def save_samples(self, generated_samples, sample_type): bulk = Sample().get_collection_handle().initialize_unordered_bulk_op() for sample in generated_samples: auto_fields = dict() auto_fields[Sample().get_qualified_field("name")] = sample["name"] auto_fields[Sample().get_qualified_field("sample_type")] = sample_type # set qualified path for attributes for k, v in sample["attributes"].items(): if k: auto_fields[Sample().get_qualified_field(k)] = v kwargs = dict() kwargs["target_id"] = str() kwargs["validate_only"] = True # preventing save per record in order to do bulk save record = Sample(profile_id=self.profile_id).save_record(auto_fields, **kwargs) bulk.insert(record) bulk.execute() return True
def get_samples_json(target_id=None): """ returns all samples in a profile (i.e isa and biosamples) :return: """ from dal.copo_da import Sample profile_id = get_current_request().session['profile_id'] if target_id: samples = list() samples.append(Sample().get_record(target_id)) else: samples = Sample(profile_id).get_all_records() value_field = str("id") label_field = str("sample_name") search_field = ["id", "sample_name"] secondary_label_field = ["meta_sample_name"] elem_json = dict(value_field=value_field, label_field=label_field, secondary_label_field=secondary_label_field, search_field=search_field, options=list()) for sd in samples: elem_json.get("options").append({ value_field: str(sd["_id"]), label_field: sd["name"], secondary_label_field[0]: sd["name"], "sample_type": sd.get("sample_type", str()) }) return elem_json
def get_samples_for_study(request): # get all samples which have corresponding profile_id number profile_id = request.POST['profile_id'] samples = Sample().get_from_profile_id(profile_id=profile_id) output = list() for s in samples: source = Source().get_record(s['derivesFrom'][0]) # s.update(source) d = { "organism": source["organism"], "_id": s["_id"], "name": s["name"] } output.append(d) return HttpResponse(json_util.dumps(output))
def get(request, id): """ Method to handle a request for a single sample object from the API :param request: a Django HTTPRequest object :param id: the id of the Sample object (can be string or ObjectID) :return: an HttpResponse object embedded with the completed return template for Sample """ # get sample and source objects try: ss = Sample().get_sample_and_source(id) except TypeError as e: print(e) return finish_request(error=API_ERRORS['NOT_FOUND']) except InvalidId as e: print(e) return finish_request(error=API_ERRORS['INVALID_PARAMETER']) except: print("Unexpected error:", sys.exc_info()[0]) raise source = ss['source'] sample = ss['sample'] # get template for return type t_source = get_return_template('SOURCE') t_sample = get_return_template('SAMPLE') # extract fields for both source and sample tmp_source = extract_to_template(object=source, template=t_source) tmp_sample = extract_to_template(object=sample, template=t_sample) tmp_sample['source'] = tmp_source out_list = [] out_list.append(tmp_sample) return finish_request(out_list)
def get_all(request): """ Method to handle a request for all :param request: a Django HttpRequest object :return: A dictionary containing all samples in COPO """ out_list = [] # get sample and source objects try: sample_list = Sample().get_samples_across_profiles() except TypeError as e: print(e) return finish_request(error=API_ERRORS['NOT_FOUND']) except InvalidId as e: print(e) return finish_request(error=API_ERRORS['INVALID_PARAMETER']) except: print("Unexpected error:", sys.exc_info()[0]) raise for s in sample_list: # get template for return type t_source = get_return_template('SOURCE') t_sample = get_return_template('SAMPLE') # get source for sample source = Source().GET(s['source_id']) # extract fields for both source and sample tmp_source = extract_to_template(object=source, template=t_source) tmp_sample = extract_to_template(object=s, template=t_sample) tmp_sample['source'] = tmp_source out_list.append(tmp_sample) return finish_request(out_list)
def __init__(self, description_token=str(), profile_id=str()): self.description_token = description_token self.profile_id = self.set_profile_id(profile_id) self.schema = Sample().get_schema().get("schema_dict")
def get_allsamples(self): """ lookup for all samples irrespective of sample type :return: """ import web.apps.web_copo.templatetags.html_tags as htags df = pd.DataFrame() if self.accession: if isinstance(self.accession, str): self.accession = self.accession.split(",") object_ids = [ObjectId(x) for x in self.accession if x.strip()] records = cursor_to_list(Sample().get_collection_handle().find( {"_id": { "$in": object_ids }})) if records: df = pd.DataFrame(records) df['accession'] = df._id.astype(str) df['label'] = df['name'] df['desc'] = df['accession'].apply( lambda x: htags.generate_attributes("sample", x)) df['description'] = df['desc'].apply( lambda x: self.format_description(x)) df['server-side'] = True # ...to request callback to server for resolving item description elif self.search_term: projection = dict(name=1) filter_by = dict() filter_by["name"] = {'$regex': self.search_term, "$options": 'i'} sort_by = 'name' sort_direction = -1 records = Sample( profile_id=self.profile_id).get_all_records_columns( filter_by=filter_by, projection=projection, sort_by=sort_by, sort_direction=sort_direction) if not records: # try getting all records del filter_by['name'] records = Sample( profile_id=self.profile_id).get_all_records_columns( filter_by=filter_by, projection=projection, sort_by=sort_by, sort_direction=sort_direction) if records: df = pd.DataFrame(records) df['accession'] = df._id.astype(str) df['label'] = df['name'] df['description'] = '' df['server-side'] = True # ...to request callback to server for resolving item description result = list() if not df.empty: df = df[['accession', 'label', 'description', 'server-side']] result = df.to_dict('records') return result