def add_ena_sample(self, ena_collection_id, study_type_list, auto_fields): ena_d = d_utils.get_ui_template_as_obj("ENA").studies.study.studySamples.fields auto_fields = ast.literal_eval(auto_fields) sample_id = uuid.uuid4().hex a = {'id': sample_id} characteristics = [] for f in ena_d: key_split = f.id.split(".") a[key_split[len(key_split) - 1]] = "" # accommodates fields not displayed on form if f.id in auto_fields.keys(): a[key_split[len(key_split) - 1]] = auto_fields[f.id] if key_split[len(key_split) - 1] == "organism": characteristics.append({ "categoryTerm": "organism", "characteristics": auto_fields[f.id], "termSourceREF": auto_fields["termSourceREF_organism"], "termAccessionNumber": auto_fields["termAccessionNumber_organism"] }) ena_d = d_utils.get_ui_template_as_obj("ENA").studies.study.studySamples.sampleCollection.fields for f in ena_d: key_split = f.id.split(".") a[key_split[len(key_split) - 1]] = "" if f.id in auto_fields.keys(): a[key_split[len(key_split) - 1]] = auto_fields[f.id] # get characteristics, we have already begun with the organism, # retrieve and sort to maintain order as displayed form categories = [key for key, value in auto_fields.items() if key.startswith('categoryTerm_')] categories.sort() for category in categories: index_part = category.split("categoryTerm_")[1] if auto_fields['categoryTerm_' + index_part]: ch = { "categoryTerm": auto_fields['categoryTerm_' + index_part], "characteristics": auto_fields['characteristics_' + index_part], "termSourceREF": auto_fields['termSourceREF_' + index_part], "termAccessionNumber": auto_fields['termAccessionNumber_' + index_part], "unit": auto_fields['unit_' + index_part] } characteristics.append(ch) a["characteristics"] = characteristics EnaCollections.update({"_id": ObjectId(ena_collection_id)}, {"$push": {"collectionCOPOMetadata.samples": a}}) # assign sample to studies for study_id in study_type_list: a = {'id': sample_id, 'deleted': '0'} self.add_sample_to_ena_study(study_id, ena_collection_id, a) return sample_id
def update_study_details(self, ena_collection_id, study_id, auto_fields): ena_d = d_utils.get_ui_template_as_obj("ENA").studies.study.fields auto_fields = ast.literal_eval(auto_fields) auto_dict = {} for f in ena_d: key_split = f.id.split(".") if f.id in auto_fields.keys(): auto_dict["studies.$.study." + key_split[len(key_split) - 1]] = auto_fields[f.id] EnaCollections.update({"_id": ObjectId(ena_collection_id), "studies.studyCOPOMetadata.id": study_id}, { '$set': auto_dict})
def add_study_publication(self, study_id, ena_collection_id, auto_fields): ena_d = d_utils.get_ui_template_as_obj("ENA").studies.study.studyPublications.fields auto_fields = ast.literal_eval(auto_fields) # get target study study = self.get_ena_study(study_id, ena_collection_id) # each study should have an empty publication document for creating others publication_dict = study["study"]["studyPublications"][0] if publication_dict: publication_dict["id"] = uuid.uuid4().hex publication_dict["deleted"] = "0" for f in ena_d: key_split = f.id.split(".") if f.id in auto_fields.keys(): publication_dict[key_split[len(key_split) - 1]] = auto_fields[f.id] EnaCollections.update( {"_id": ObjectId(ena_collection_id), "studies.studyCOPOMetadata.id": study_id}, {'$push': {"studies.$.study.studyPublications": publication_dict}})
def clone_ena_study(self, ena_collection_id, cloned_elements): # get study template from the ENA db template study_template = d_utils.get_db_template("ENA")['studies'][0] if study_template: study_dict = study_template study_dict["studyCOPOMetadata"]["id"] = uuid.uuid4().hex study_dict["studyCOPOMetadata"]["deleted"] = "0" if cloned_elements["studyType"]: study_dict["studyCOPOMetadata"]["studyType"] = cloned_elements["studyType"] study_dict["studyCOPOMetadata"]["studyReference"] = ''.join( random.choice(string.ascii_uppercase) for i in range(4)) + "_CLONE" if cloned_elements["studyReference"]: study_dict["studyCOPOMetadata"]["studyReference"] = cloned_elements["studyReference"] # check for samples and other composite types new_samples = [] for k, v in cloned_elements.items(): if k[:-2] == "sample": new_samples.append({'id': v, 'deleted': '0'}) if new_samples: study_dict["studyCOPOMetadata"]['samples'] = new_samples # get study fields ena_d = d_utils.get_ui_template_as_obj("ENA").studies.study.fields for f in ena_d: key_split = f.id.split(".") target_key = key_split[len(key_split) - 1] if target_key in cloned_elements.keys(): study_dict["study"][target_key] = cloned_elements[target_key] EnaCollections.update({"_id": ObjectId(ena_collection_id)}, {"$push": {"studies": study_dict}})
def edit_ena_sample(self, ena_collection_id, sample_id, study_type_list, auto_fields): ena_d = d_utils.get_ui_template_as_obj("ENA").studies.study.studySamples.fields auto_fields = ast.literal_eval(auto_fields) characteristics = [] for f in ena_d: key_split = f.id.split(".") if f.id in auto_fields.keys(): EnaCollections.update( {"_id": ObjectId(ena_collection_id), "collectionCOPOMetadata.samples.id": sample_id}, {'$set': {"collectionCOPOMetadata.samples.$." + key_split[len(key_split) - 1]: auto_fields[f.id]}}) if key_split[len(key_split) - 1] == "organism": characteristics.append({ "categoryTerm": "organism", "characteristics": auto_fields[f.id], "termSourceREF": auto_fields["termSourceREF_organism"], "termAccessionNumber": auto_fields["termAccessionNumber_organism"] }) ena_d = d_utils.get_ui_template_as_obj("ENA").studies.study.studySamples.sampleCollection.fields for f in ena_d: key_split = f.id.split(".") if f.id in auto_fields.keys(): EnaCollections.update( {"_id": ObjectId(ena_collection_id), "collectionCOPOMetadata.samples.id": sample_id}, {'$set': {"collectionCOPOMetadata.samples.$." + key_split[len(key_split) - 1]: auto_fields[f.id]}}) # get characteristics # categories = [key for key, value in auto_fields.items() if key.startswith('categoryTerm_')] categories.sort() for category in categories: index_part = category.split("categoryTerm_")[1] if auto_fields['categoryTerm_' + index_part]: ch = { "categoryTerm": auto_fields['categoryTerm_' + index_part], "characteristics": auto_fields['characteristics_' + index_part], "termSourceREF": auto_fields['termSourceREF_' + index_part], "termAccessionNumber": auto_fields['termAccessionNumber_' + index_part], "unit": auto_fields['unit_' + index_part] } characteristics.append(ch) EnaCollections.update( {"_id": ObjectId(ena_collection_id), "collectionCOPOMetadata.samples.id": sample_id}, {'$set': {"collectionCOPOMetadata.samples.$.characteristics": characteristics}}) # update studies: add sample to study if study in the selected list, # delete from study not selected studies = EnaCollection().get_ena_studies(ena_collection_id) for st in studies: study_id = st["studyCOPOMetadata"]["id"] a = {'id': sample_id, 'deleted': '1'} if study_id in study_type_list: a = {'id': sample_id, 'deleted': '0'} self.hard_delete_sample_from_study(sample_id, study_id, ena_collection_id) self.add_sample_to_ena_study(study_id, ena_collection_id, a)