def new_collection_head(request): # create the new collection c_type = request.POST['collection_type'] c_name = request.POST['collection_name'] collection_head_id = Collection_Head().PUT() collection_head_dc = d_utils.get_db_template("COPO_COLLECTION_HEAD_FILE") collection_head_dc['name'] = c_name collection_head_dc['type'] = c_type Collection_Head().update(collection_head_id, collection_head_dc) # add a template for ENA submission coll_type = request.POST['collection_type'] if coll_type.lower() == 'ena submission': # create a blank ENA collection based on template db_template = d_utils.get_db_template("ENA") if not db_template: return render(request, 'copo/error_page.html', {'message': DB_ERROR_CODES["TEMPLATE_NOT_FOUND"]}, ) return HttpResponseRedirect(reverse('copo:index')) ena_collection_id = EnaCollection().PUT(db_template) # add collection details Collection_Head().add_collection_details(collection_head_id, ena_collection_id) # get studies st_list = [] for k, v in request.POST.items(): if k.startswith('study_type_select_'): st_dict = {'study_type': request.POST.get(k, ""), 'study_type_reference': request.POST.get('study_type_reference_' + k[-1:], "") } st_list.append(st_dict) EnaCollection().add_ena_study(ena_collection_id, st_list) profile_id = request.session['profile_id'] Profile().add_collection_head(profile_id, collection_head_id) return HttpResponseRedirect(reverse('copo:view_profile', kwargs={'profile_id': profile_id}))
def add_file_to_ena_study(self, study_id, ena_collection_id, file_id): # get study dataFile template from the ENA db template datafile_template = d_utils.get_db_template("ENA")['studies'][0]['studyCOPOMetadata']['dataFiles'][0] data_file_id = uuid.uuid4().hex if datafile_template: datafile_dict = datafile_template datafile_dict["id"] = data_file_id datafile_dict["fileId"] = file_id datafile_dict["deleted"] = "0" EnaCollections.update({"_id": ObjectId(ena_collection_id), "studies.studyCOPOMetadata.id": study_id}, {'$push': {"studies.$.studyCOPOMetadata.dataFiles": datafile_dict}}) return data_file_id
def add_ena_study(self, ena_collection_id, study_type_list): # get study template from the ENA db template study_template = d_utils.get_db_template("ENA")['studies'][0] if study_template: for st in study_type_list: study_dict = study_template study_dict["studyCOPOMetadata"]["id"] = uuid.uuid4().hex study_dict["studyCOPOMetadata"]["studyType"] = st['study_type'] # handles empty study reference assignment study_dict["studyCOPOMetadata"]["studyReference"] = ''.join( random.choice(string.ascii_uppercase) for i in range(4)) if st["study_type_reference"]: study_dict["studyCOPOMetadata"]["studyReference"] = st["study_type_reference"] # ...since the model study is deleted by default study_dict["studyCOPOMetadata"]["deleted"] = "0" EnaCollections.update({"_id": ObjectId(ena_collection_id)}, {"$push": {"studies": study_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 refactor_ena_study_assays(self, ena_collection_id, study_id): study_assay = d_utils.get_db_template("ENA")['studies'][0]['study']['assays'][0] # get study type to determine the context to represent study_type = self.get_ena_study(study_id, ena_collection_id)["studyCOPOMetadata"]["studyType"] normalised_ranked_list = self.get_normalised_ranked_list([], DataSchemas("ENA").get_ui_template()[ "studies"]["study"][ "assays"]["assaysTable"][study_type]) assays_table = [] datafiles = self.get_study_datafiles(ena_collection_id, study_id) if datafiles: for df in datafiles: # get samples, every sample attached to a file will also have an entry in assaysTables if df["samples"]: for sample_id in df["samples"]: sample_details = self.get_ena_sample(ena_collection_id, sample_id) if sample_details: temp_dict = sample_details # sort out data file temp_dict["rawDataFile"] = ChunkedUpload.objects.get(id=int(df["fileId"])).file.name # sort out elements captured under attributes temp_dict["attributes"] = df["attributes"] # now start making entries assay = [] modified_ranked_list = self.get_modified_ranked_list(normalised_ranked_list) for elem_dict in modified_ranked_list: entry_dict = elem_dict if entry_dict["ref"] in temp_dict: entry_dict["value"] = temp_dict[entry_dict["ref"]] items = entry_dict["items"] # remove redundant fields del entry_dict["ref"] del entry_dict["items"] assay.append(entry_dict) if items: # spin off another entry_dict to cater for these items # it might well be an "all for one, one for all" arrangement here... # i.e., structure for a single entry under "items" suffices for the rest structure = items[0]["structure"].replace(" ", "").lower() entry_dict = d_utils.get_isajson_refactor_type(structure) # delete the blank entry in items del entry_dict["items"][0] for item in items: for attribute in temp_dict["attributes"]: if attribute["question"] == item["id"]: # get the template, and sort out this entry items_entry = d_utils.get_isajson_refactor_type(structure)["items"][0] if "parameter" in structure: items_entry["parameterTerm"] = item["term"] items_entry["parameterValue"] = attribute["answer"]["value"] items_entry["termAccessionNumber"] = attribute["answer"][ "termAccessionNumber"] items_entry["termSourceREF"] = attribute["answer"]["termSourceREF"] elif "characteristics" in structure: pass elif "factor" in structure: pass entry_dict["items"].append(items_entry) break # del entry_dict["items"][0] # no need keeping this dummy entry assay.append(entry_dict) assays_table.append(assay) study_assay["assaysTable"] = assays_table return study_assay