def clinvar_submissions(institute_id): def generate_csv(header, lines): yield header + '\n' for line in lines: # lines have already quoted fields yield line + '\n' if request.method == 'POST': submission_id = request.form.get('submission_id') if request.form.get('update_submission'): if request.form.get('update_submission') == 'close': # close a submission store.update_clinvar_submission_status(current_user.email, submission_id, 'closed') elif request.form.get('update_submission') == 'open': store.update_clinvar_submission_status(current_user.email, submission_id, 'open') # open a submission elif request.form.get('update_submission') == 'register_id' and request.form.get('clinvar_id'): # provide an official clinvar submission ID result = store.update_clinvar_id(clinvar_id = request.form.get('clinvar_id'), submission_id = submission_id) elif request.form.get('update_submission') == 'delete': # delete a submission deleted_objects, deleted_submissions = store.delete_submission(submission_id = submission_id) flash("Removed {} objects and {} submission from database".format(deleted_objects, deleted_submissions), 'info') elif request.form.get('delete_variant'): # delete a variant from a submission store.delete_clinvar_object(object_id = request.form.get('delete_variant'), object_type='variant_data', submission_id = submission_id) # remove variant and associated_casedata elif request.form.get('delete_casedata'): # delete a case from a submission store.delete_clinvar_object(object_id = request.form.get('delete_casedata'), object_type='case_data', submission_id = submission_id) # remove just the casedata associated to a variant else: # Download submission CSV files (for variants or casedata) clinvar_subm_id = request.form.get('clinvar_id') if clinvar_subm_id =='': flash('In order to download a submission CSV file you should register a Clinvar submission Name first!','warning') return redirect(request.referrer) csv_type = '' csv_type = request.form.get('csv_type') submission_objs = store.clinvar_objs(submission_id=submission_id, key_id=csv_type) # a list of clinvar submission objects (variants or casedata) if submission_objs: csv_header_obj = controllers.clinvar_header(submission_objs, csv_type) # custom csv header (dict as in constants CLINVAR_HEADER and CASEDATA_HEADER, but with required fields only) csv_lines = controllers.clinvar_lines(submission_objs, csv_header_obj) # csv lines (one for each variant/casedata to be submitted) csv_header = list(csv_header_obj.values()) csv_header = ['"'+str(x)+'"' for x in csv_header] # quote columns in header for csv rendering headers = Headers() headers.add('Content-Disposition','attachment', filename=clinvar_subm_id+'_'+str(datetime.datetime.now().strftime("%Y-%m-%d"))+'.csv') return Response(generate_csv(','.join(csv_header), csv_lines), mimetype='text/csv', headers=headers) else: flash('There are no submission objects of type "{}" to include in the csv file!'.format(csv_type),'warning') data = { 'submissions' : controllers.clinvar_submissions(store, current_user.email, institute_id), 'institute_id' : institute_id, 'variant_header_fields' : CLINVAR_HEADER , 'casedata_header_fields' : CASEDATA_HEADER } return data
def clinvar_submission_file(store, submission_id, csv_type, clinvar_subm_id): """Prepare content (header and lines) of a csv clinvar submission file Args: store(adapter.MongoAdapter) submission_id(str): the database id of a clinvar submission csv_type(str): 'variant_data' or 'case_data' clinvar_subm_id(str): The ID assigned to this submission by clinVar Returns: (filename, csv_header, csv_lines): filename(str) name of file to be downloaded csv_header(list) string list content of file header csv_lines(list) string list content of file lines """ if clinvar_subm_id == "None": flash( "In order to download a submission CSV file you should register a Clinvar submission Name first!", "warning", ) return submission_objs = store.clinvar_objs(submission_id=submission_id, key_id=csv_type) if submission_objs is None or len(submission_objs) == 0: flash( f"There are no submission objects of type '{csv_type}' to include in the csv file!", "warning", ) return # Download file csv_header_obj = clinvar_header(submission_objs, csv_type) csv_lines = clinvar_lines(submission_objs, csv_header_obj) csv_header = list(csv_header_obj.values()) csv_header = ['"' + str(x) + '"' for x in csv_header ] # quote columns in header for csv rendering today = str(datetime.datetime.now().strftime("%Y-%m-%d")) if csv_type == "variant_data": filename = f"{clinvar_subm_id}_{today}.Variant.csv" else: filename = f"{clinvar_subm_id}_{today}.CaseData.csv" return (filename, csv_header, csv_lines)
def clinvar_submissions(institute_id): def generate_csv(header, lines): yield header + "\n" for line in lines: # lines have already quoted fields yield line + "\n" if request.method == "POST": submission_id = request.form.get("submission_id") if request.form.get("update_submission"): if request.form.get( "update_submission") == "close": # close a submission store.update_clinvar_submission_status(institute_id, submission_id, "closed") elif request.form.get("update_submission") == "open": store.update_clinvar_submission_status( institute_id, submission_id, "open") # open a submission elif request.form.get( "update_submission") == "register_id" and request.form.get( "clinvar_id" ): # provide an official clinvar submission ID result = store.update_clinvar_id( clinvar_id=request.form.get("clinvar_id"), submission_id=submission_id, ) elif (request.form.get("update_submission") == "delete" ): # delete a submission deleted_objects, deleted_submissions = store.delete_submission( submission_id=submission_id) flash( "Removed {} objects and {} submission from database". format(deleted_objects, deleted_submissions), "info", ) elif request.form.get( "delete_variant"): # delete a variant from a submission store.delete_clinvar_object( object_id=request.form.get("delete_variant"), object_type="variant_data", submission_id=submission_id, ) # remove variant and associated_casedata elif request.form.get( "delete_casedata"): # delete a case from a submission store.delete_clinvar_object( object_id=request.form.get("delete_casedata"), object_type="case_data", submission_id=submission_id, ) # remove just the casedata associated to a variant else: # Download submission CSV files (for variants or casedata) clinvar_subm_id = request.form.get("clinvar_id") if clinvar_subm_id == "": flash( "In order to download a submission CSV file you should register a Clinvar submission Name first!", "warning", ) return redirect(request.referrer) csv_type = request.form.get("csv_type", "") submission_objs = store.clinvar_objs( submission_id=submission_id, key_id=csv_type ) # a list of clinvar submission objects (variants or casedata) if submission_objs: csv_header_obj = controllers.clinvar_header( submission_objs, csv_type ) # custom csv header (dict as in constants CLINVAR_HEADER and CASEDATA_HEADER, but with required fields only) csv_lines = controllers.clinvar_lines( submission_objs, csv_header_obj ) # csv lines (one for each variant/casedata to be submitted) csv_header = list(csv_header_obj.values()) csv_header = ['"' + str(x) + '"' for x in csv_header ] # quote columns in header for csv rendering download_day = str( datetime.datetime.now().strftime("%Y-%m-%d")) headers = Headers() headers.add( "Content-Disposition", "attachment", filename=f"{clinvar_subm_id}_{csv_type}_{download_day}.csv", ) return Response( generate_csv(",".join(csv_header), csv_lines), mimetype="text/csv", headers=headers, ) else: flash( 'There are no submission objects of type "{}" to include in the csv file!' .format(csv_type), "warning", ) institute_obj = institute_and_case(store, institute_id) data = { "submissions": controllers.clinvar_submissions(store, institute_id), "institute": institute_obj, "variant_header_fields": CLINVAR_HEADER, "casedata_header_fields": CASEDATA_HEADER, } return data
def clinvar_submissions(institute_id): def generate_csv(header, lines): yield header + '\n' for line in lines: # lines have already quoted fields yield line + '\n' if request.method == 'POST': submission_id = request.form.get('submission_id') if request.form.get('update_submission'): if request.form.get( 'update_submission') == 'close': # close a submission store.update_clinvar_submission_status(current_user.email, submission_id, 'closed') elif request.form.get('update_submission') == 'open': store.update_clinvar_submission_status( current_user.email, submission_id, 'open') # open a submission elif request.form.get( 'update_submission') == 'register_id' and request.form.get( 'clinvar_id' ): # provide an official clinvar submission ID result = store.update_clinvar_id( clinvar_id=request.form.get('clinvar_id'), submission_id=submission_id) elif request.form.get( 'update_submission') == 'delete': # delete a submission deleted_objects, deleted_submissions = store.delete_submission( submission_id=submission_id) flash( "Removed {} objects and {} submission from database". format(deleted_objects, deleted_submissions), 'info') elif request.form.get( 'delete_variant'): # delete a variant from a submission store.delete_clinvar_object( object_id=request.form.get('delete_variant'), object_type='variant_data', submission_id=submission_id ) # remove variant and associated_casedata elif request.form.get( 'delete_casedata'): # delete a case from a submission store.delete_clinvar_object( object_id=request.form.get('delete_casedata'), object_type='case_data', submission_id=submission_id ) # remove just the casedata associated to a variant else: # Download submission CSV files (for variants or casedata) clinvar_subm_id = request.form.get('clinvar_id') if clinvar_subm_id == '': flash( 'In order to download a submission CSV file you should register a Clinvar submission Name first!', 'warning') return redirect(request.referrer) csv_type = '' csv_type = request.form.get('csv_type') submission_objs = store.clinvar_objs( submission_id=submission_id, key_id=csv_type ) # a list of clinvar submission objects (variants or casedata) if submission_objs: csv_header_obj = controllers.clinvar_header( submission_objs, csv_type ) # custom csv header (dict as in constants CLINVAR_HEADER and CASEDATA_HEADER, but with required fields only) csv_lines = controllers.clinvar_lines( submission_objs, csv_header_obj ) # csv lines (one for each variant/casedata to be submitted) csv_header = list(csv_header_obj.values()) csv_header = ['"' + str(x) + '"' for x in csv_header ] # quote columns in header for csv rendering headers = Headers() headers.add('Content-Disposition', 'attachment', filename=clinvar_subm_id + '_' + str(datetime.datetime.now().strftime("%Y-%m-%d")) + '.csv') return Response(generate_csv(','.join(csv_header), csv_lines), mimetype='text/csv', headers=headers) else: flash( 'There are no submission objects of type "{}" to include in the csv file!' .format(csv_type), 'warning') data = { 'submissions': controllers.clinvar_submissions(store, current_user.email, institute_id), 'institute_id': institute_id, 'variant_header_fields': CLINVAR_HEADER, 'casedata_header_fields': CASEDATA_HEADER } return data