def tsne(download=None): apps = current_user.user_apps plot_arguments = None reset_info = check_session_app(session, "tsne", apps) if reset_info: flash(reset_info, 'error') # INITIATE SESSION session["filename"] = "Select file.." plot_arguments = figure_defaults() session["plot_arguments"] = plot_arguments session["COMMIT"] = app.config['COMMIT'] session["app"] = "tsne" if request.method == 'POST': try: if request.files["inputsessionfile"]: msg, plot_arguments, error = read_session_file( request.files["inputsessionfile"], "tsne") if error: flash(msg, 'error') return render_template('/apps/tsne.html', filename=session["filename"], apps=apps, **plot_arguments) flash(msg, "info") if request.files["inputargumentsfile"]: msg, plot_arguments, error = read_argument_file( request.files["inputargumentsfile"], "tsne") if error: flash(msg, 'error') return render_template('/apps/tsne.html', filename=session["filename"], apps=apps, **plot_arguments) flash(msg, "info") if not request.files["inputsessionfile"] and not request.files[ "inputargumentsfile"]: plot_arguments = read_request(request) if "df" in list(session.keys()): available_rows = pd.read_json(session["df"]) if plot_arguments[ "xvals"] in available_rows.columns.tolist(): available_rows = available_rows[ plot_arguments["xvals"]].tolist() available_rows = list(set(available_rows)) available_rows.sort() plot_arguments["available_rows"] = available_rows # UPDATE SESSION VALUES session["plot_arguments"] = plot_arguments # IF THE UPLOADS A NEW FILE # THAN UPDATE THE SESSION FILE # READ INPUT FILE inputfile = request.files["inputfile"] if inputfile: filename = secure_filename(inputfile.filename) if allowed_file(inputfile.filename): df = read_tables(inputfile) cols = df.columns.tolist() # IF THE USER HAS NOT YET CHOOSEN X AND Y VALUES THAN PLEASE SELECT if (session["plot_arguments"]["yvals"] not in cols): session["plot_arguments"]["xcols"] = cols session["plot_arguments"]["xvals"] = cols[0] session["plot_arguments"]["ycols"] = cols session["plot_arguments"]["yvals"] = cols[1:] available_rows = pd.read_json(session["df"]) if plot_arguments[ "xvals"] in available_rows.columns.tolist(): available_rows = available_rows[ plot_arguments["xvals"]].tolist() available_rows = list(set(available_rows)) available_rows.sort() session["plot_arguments"][ "available_rows"] = available_rows sometext = "Please select which columns should be used for plotting." plot_arguments = session["plot_arguments"] flash(sometext, 'info') return render_template('/apps/tsne.html', filename=filename, apps=apps, **plot_arguments) else: # IF UPLOADED FILE DOES NOT CONTAIN A VALID EXTENSION PLEASE UPDATE error_msg = "You can can only upload files with the following extensions: 'xlsx', 'tsv', 'csv'. Please make sure the file '%s' \ has the correct format and respective extension and try uploadling it again." % filename flash(error_msg, 'error') return render_template('/apps/tsne.html', filename="Select file..", apps=apps, **plot_arguments) if "df" not in list(session.keys()): error_message = "No data to plot, please upload a data or session file." flash(error_message, 'error') return render_template('/apps/tsne.html', filename="Select file..", apps=apps, **plot_arguments) # MAKE SURE WE HAVE THE LATEST ARGUMENTS FOR THIS SESSION filename = session["filename"] plot_arguments = session["plot_arguments"] # READ INPUT DATA FROM SESSION JSON df = pd.read_json(session["df"]) # CALL FIGURE FUNCTION # try: df_tsne = make_figure(df, plot_arguments) df_tsne = df_tsne.astype(str) session["df_tsne"] = df_tsne.to_json() #features=features.astype(str) #session["features"]=features.to_json() df_selected = df_tsne[:50] cols_to_format = df_selected.columns.tolist() #cols_to_format=[ s for s in cols_to_format if s not in ["gene_id","gene_name"] ] table_headers = [cols_to_format[0]] for c in cols_to_format[1:]: df_selected[c] = df_selected[c].apply(lambda x: nFormat(x)) #new_name=c.split(" - ")[0]+" - "+nFormat(float(c.split(" - ")[-1].split("%")[0]))+"%" new_name = c table_headers.append(new_name) df_selected = list(df_selected.values) df_selected = [list(s) for s in df_selected] #features_selected=features[:50] #cols_to_format=features_selected.columns.tolist() #cols_to_format=[ s for s in cols_to_format if "key" not in s ] #features_headers=features_selected.columns.tolist() #for c in cols_to_format[1:]: # features_selected[c]=features_selected[c].apply(lambda x: nFormat(x) ) #features_selected=features_selected.astype(str) #features_selected=features_selected.replace("nan","") #features_selected=list(features_selected.values) #features_selected=[ list(s) for s in features_selected ] return render_template('/apps/tsne.html', table_headers=table_headers, table_contents=df_selected, filename=filename, apps=apps, **plot_arguments) except Exception as e: tb_str = handle_exception(e, user=current_user, eapp="tsne", session=session) filename = session["filename"] flash(tb_str, 'traceback') if not plot_arguments: plot_arguments = session["plot_arguments"] return render_template('/apps/tsne.html', filename=filename, apps=apps, **plot_arguments) else: if download == "download": plot_arguments = session["plot_arguments"] # READ INPUT DATA FROM SESSION JSON df_tsne = pd.read_json(session["df_tsne"]) #features=pd.read_json(session["features"]) # CALL FIGURE FUNCTION eventlog = UserLogging(email=current_user.email, action="download table tsne values") db.session.add(eventlog) db.session.commit() if plot_arguments["downloadf"] == "xlsx": excelfile = io.BytesIO() EXC = pd.ExcelWriter(excelfile) df_tsne.to_excel(EXC, sheet_name="tsne", index=None) #features.to_excel(EXC,sheet_name="features", index=None) EXC.save() excelfile.seek(0) return send_file( excelfile, attachment_filename=plot_arguments["downloadn"] + ".xlsx", as_attachment=True) elif plot_arguments["downloadf"] == "tsv": return Response(df_tsne.to_csv(sep="\t"), mimetype="text/csv", headers={ "Content-disposition": "attachment; filename=%s.tsv" % plot_arguments["downloadn"] }) if download == "scatter": # READ INPUT DATA FROM SESSION JSON df_tsne = pd.read_json(session["df_tsne"]) reset_info = check_session_app(session, "iscatterplot", apps) if reset_info: flash(reset_info, 'error') # INITIATE SESSION session["filename"] = "<from tSNE>" plot_arguments = iscatterplot.figure_defaults() session["COMMIT"] = app.config['COMMIT'] session["app"] = "iscatterplot" df_tsne = df_tsne.astype(str) session["df"] = df_tsne.to_json() plot_arguments["xcols"] = df_tsne.columns.tolist() plot_arguments["ycols"] = df_tsne.columns.tolist() plot_arguments["groups"] = plot_arguments[ "groups"] + df_tsne.columns.tolist() plot_arguments["labels_col"] = df_tsne.columns.tolist() plot_arguments["xvals"] = df_tsne.columns.tolist()[1] plot_arguments["yvals"] = df_tsne.columns.tolist()[2] plot_arguments["labels_col_value"] = df_tsne.columns.tolist()[0] plot_arguments["title"] = "tSNE" plot_arguments["xlabel"] = df_tsne.columns.tolist()[1] plot_arguments["ylabel"] = df_tsne.columns.tolist()[2] session["plot_arguments"] = plot_arguments return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) return render_template('apps/tsne.html', filename=session["filename"], apps=apps, **session["plot_arguments"])
def iscatterplot(download=None): """ renders the plot on the fly. https://gist.github.com/illume/1f19a2cf9f26425b1761b63d9506331f """ apps = current_user.user_apps plot_arguments = None reset_info = check_session_app(session, "iscatterplot", apps) if reset_info: flash(reset_info, 'error') # INITIATE SESSION session["filename"] = "Select file.." plot_arguments = figure_defaults() session["plot_arguments"] = plot_arguments session["COMMIT"] = app.config['COMMIT'] session["app"] = "iscatterplot" if request.method == 'POST': try: # READ SESSION FILE IF AVAILABLE # AND OVERWRITE VARIABLES if request.files["inputsessionfile"]: msg, plot_arguments, error = read_session_file( request.files["inputsessionfile"], "iscatterplot") if error: flash(msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) flash(msg, "info") if request.files["inputargumentsfile"]: msg, plot_arguments, error = read_argument_file( request.files["inputargumentsfile"], "iscatterplot") if error: flash(msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) flash(msg, "info") # IF THE UPLOADS A NEW FILE # THAN UPDATE THE SESSION FILE # READ INPUT FILE inputfile = request.files["inputfile"] if inputfile: filename = secure_filename(inputfile.filename) if allowed_file(inputfile.filename): df = read_tables(inputfile) cols = df.columns.tolist() if len(cols) < 2: error_msg = "Your table needs to have at least 2 columns. One for the x- and one for the y-value." flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) if session["plot_arguments"]["groups"] not in cols: session["plot_arguments"]["groups"] = ["None"] + cols columns_select=["markerstyles_cols", "markerc_cols", "markersizes_cols","markeralpha_col",\ "labels_col","edgecolor_cols","edge_linewidth_cols",] for parg in columns_select: if session["plot_arguments"][ "markerstyles_cols"] not in cols: session["plot_arguments"][parg] = [ "select a column.." ] + cols session["plot_arguments"]["xcols"] = cols session["plot_arguments"]["ycols"] = cols # IF THE USER HAS NOT YET CHOOSEN X AND Y VALUES THAN PLEASE SELECT if (session["plot_arguments"]["xvals"] not in cols) | ( session["plot_arguments"]["yvals"] not in cols): session["plot_arguments"]["xvals"] = cols[0] session["plot_arguments"]["yvals"] = cols[1] sometext = "Please select which values should map to the x and y axes." plot_arguments = session["plot_arguments"] flash(sometext, 'info') return render_template('/apps/iscatterplot.html', filename=filename, apps=apps, **plot_arguments) plot_arguments = session["plot_arguments"] flash("New file uploaded.", 'info') return render_template('/apps/iscatterplot.html', filename=filename, apps=apps, **plot_arguments) else: # IF UPLOADED FILE DOES NOT CONTAIN A VALID EXTENSION PLEASE UPDATE error_msg = "You can can only upload files with the following extensions: 'xlsx', 'tsv', 'csv'. Please make sure the file '%s' \ has the correct format and respective extension and try uploadling it again." % filename flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename="Select file..", apps=apps, **plot_arguments) if not request.files["inputsessionfile"] and not request.files[ "inputargumentsfile"]: # SELECTION LISTS DO NOT GET UPDATED # lists=session["lists"] # USER INPUT/PLOT_ARGUMENTS GETS UPDATED TO THE LATEST INPUT # WITH THE EXCEPTION OF SELECTION LISTS plot_arguments = session["plot_arguments"] # if request.form["groups_value"] == "None": # plot_arguments["groups_value"]="None" if plot_arguments["groups_value"] != request.form[ "groups_value"]: if request.form["groups_value"] != "None": df = pd.read_json(session["df"]) df[request.form["groups_value"]] = df[ request.form["groups_value"]].apply( lambda x: secure_filename(str(x))) df = df.astype(str) session["df"] = df.to_json() groups = df[request.form["groups_value"]] groups = list(set(groups)) groups.sort() plot_arguments["list_of_groups"] = groups groups_settings = [] group_dic = {} for group in groups: group_dic={"name":group,\ "markers":plot_arguments["markers"],\ "markersizes_col":"select a column..",\ "markerc":random.choice([ cc for cc in plot_arguments["marker_color"] if cc != "white"]),\ "markerc_col":"select a column..",\ "markerc_write":plot_arguments["markerc_write"],\ "edge_linewidth":plot_arguments["edge_linewidth"],\ "edge_linewidth_col":"select a column..",\ "edgecolor":plot_arguments["edgecolor"],\ "edgecolor_col":"select a column..",\ "edgecolor_write":"",\ "marker":random.choice(plot_arguments["markerstyles"]),\ "markerstyles_col":"select a column..",\ "marker_alpha":plot_arguments["marker_alpha"],\ "markeralpha_col_value":"select a column.."} groups_settings.append(group_dic) plot_arguments["groups_settings"] = groups_settings elif request.form["groups_value"] == "None": plot_arguments["groups_settings"] = [] plot_arguments["list_of_groups"] = [] elif plot_arguments["groups_value"] != "None": groups_settings = [] group_dic = {} for group in plot_arguments["list_of_groups"]: group_dic={"name":group,\ "markers":request.form["%s.markers" %group],\ "markersizes_col":request.form["%s.markersizes_col" %group],\ "markerc":request.form["%s.markerc" %group],\ "markerc_col":request.form["%s.markerc_col" %group],\ "markerc_write":request.form["%s.markerc_write" %group],\ "edge_linewidth":request.form["%s.edge_linewidth" %group],\ "edge_linewidth_col":request.form["%s.edge_linewidth_col" %group],\ "edgecolor":request.form["%s.edgecolor" %group],\ "edgecolor_col":request.form["%s.edgecolor_col" %group],\ "edgecolor_write":request.form["%s.edgecolor_write" %group],\ "marker":request.form["%s.marker" %group],\ "markerstyles_col":request.form["%s.markerstyles_col" %group],\ "marker_alpha":request.form["%s.marker_alpha" %group],\ "markeralpha_col_value":request.form["%s.markeralpha_col_value" %group] } groups_settings.append(group_dic) plot_arguments["groups_settings"] = groups_settings if request.form["labels_col_value"] != "select a column..": df = pd.read_json(session["df"]) plot_arguments["available_labels"] = list( set(df[request.form["labels_col_value"]].tolist())) session["plot_arguments"] = plot_arguments plot_arguments = read_request(request) if "df" not in list(session.keys()): error_msg = "No data to plot, please upload a data or session file." flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename="Select file..", apps=apps, **plot_arguments) # MAKE SURE WE HAVE THE LATEST ARGUMENTS FOR THIS SESSION filename = session["filename"] plot_arguments = session["plot_arguments"] # READ INPUT DATA FROM SESSION JSON df = pd.read_json(session["df"]) #CALL FIGURE FUNCTION fig = make_figure(df, plot_arguments) figure_url = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) return render_template('/apps/iscatterplot.html', figure_url=figure_url, filename=filename, apps=apps, **plot_arguments) except Exception as e: tb_str = handle_exception(e, user=current_user, eapp="iscatterplot", session=session) flash(tb_str, 'traceback') if not plot_arguments: plot_arguments = session["plot_arguments"] filename = session["filename"] return render_template('/apps/iscatterplot.html', filename=filename, apps=apps, **plot_arguments) else: if download == "download": # READ INPUT DATA FROM SESSION JSON df = pd.read_json(session["df"]) plot_arguments = session["plot_arguments"] # CALL FIGURE FUNCTION fig = make_figure(df, plot_arguments) pio.orca.config.executable = '/miniconda/bin/orca' pio.orca.config.use_xvfb = True #pio.orca.config.save() figfile = io.BytesIO() mimetypes = { "png": 'image/png', "pdf": "application/pdf", "svg": "image/svg+xml" } pa_ = {} for v in ["fig_height", "fig_width"]: if plot_arguments[v] != "": pa_[v] = False elif plot_arguments[v]: pa_[v] = float(plot_arguments[v]) else: pa_[v] = False if (pa_["fig_height"]) & (pa_["fig_width"]): fig.write_image(figfile, format=plot_arguments["downloadf"], height=pa_["fig_height"], width=pa_["fig_width"]) else: fig.write_image(figfile, format=plot_arguments["downloadf"]) figfile.seek(0) # rewind to beginning of file eventlog = UserLogging(email=current_user.email, action="download figure iscatterplot") db.session.add(eventlog) db.session.commit() return send_file(figfile, mimetype=mimetypes[plot_arguments["downloadf"]], as_attachment=True, attachment_filename=plot_arguments["downloadn"] + "." + plot_arguments["downloadf"]) return render_template('apps/iscatterplot.html', filename=session["filename"], apps=apps, **session["plot_arguments"])
def aarnaseqlake(download=None): apps = current_user.user_apps if "aarnaseqlake" not in [s["link"] for s in apps]: return redirect(url_for('index')) reset_info = check_session_app(session, "aarnaseqlake", apps) if reset_info: flash(reset_info, 'error') # INITIATE SESSION session["filename"] = "Select file.." session["plot_arguments"] = {} session["plot_arguments"][ "path_to_files"] = "/flaski_private/aarnaseqlake/" gedf = pd.read_csv(session["plot_arguments"]["path_to_files"] + "gene_expression.tsv", sep="\t", index_col=[0]) results_files = pd.read_csv( session["plot_arguments"]["path_to_files"] + "files2ids.tsv", sep="\t") genes = pd.read_csv(session["plot_arguments"]["path_to_files"] + "genes.tsv", sep="\t") available_data_sets = list(set(results_files["Set"].tolist())) available_data_sets.sort() session["plot_arguments"]["available_data_sets"] = [ "all" ] + available_data_sets session["plot_arguments"]["selected_data_sets"] = ["all"] groups = list(set(results_files["Group"].tolist())) groups.sort() session["plot_arguments"]["available_groups"] = ["all"] + groups session["plot_arguments"]["selected_groups"] = ["all"] available_reps = list(set(results_files["Reps"].tolist())) available_reps.sort() session["plot_arguments"]["available_reps"] = ["all"] + available_reps session["plot_arguments"]["selected_reps"] = ["all"] available_gene_names = list(set(genes["gene_name"].tolist())) available_gene_names.sort() session["plot_arguments"][ "available_gene_names"] = available_gene_names session["plot_arguments"]["selected_gene_names"] = "" available_gene_ids = list(set(genes["gene_id"].tolist())) available_gene_ids.sort() session["plot_arguments"]["available_gene_ids"] = available_gene_ids session["plot_arguments"]["selected_gene_ids"] = "" session["plot_arguments"]["download_format"] = ["tsv", "xlsx"] session["plot_arguments"]["download_format_value"] = "xlsx" session["plot_arguments"]["download_name"] = "RNAseqLake" session["plot_arguments"][ "session_download_name"] = "MySession.RNAseqLake" session["plot_arguments"]["inputsessionfile"] = "Select file.." session["plot_arguments"][ "session_argumentsn"] = "MyArguments.RNAseqLake" session["plot_arguments"]["inputargumentsfile"] = "Select file.." plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) session["plot_arguments"] = plot_arguments session["COMMIT"] = app.config['COMMIT'] session["app"] = "aarnaseqlake" if request.method == 'POST': try: plot_arguments = read_request(request) plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) session["plot_arguments"] = plot_arguments return render_template('/apps/aarnaseqlake.html', apps=apps, **plot_arguments) except Exception as e: tb_str = handle_exception(e, user=current_user, eapp="aarnaseqlake", session=session) flash(tb_str, 'traceback') return render_template('/apps/aarnaseqlake.html', apps=apps, **plot_arguments) else: if download == "metadata": plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) eventlog = UserLogging(email=current_user.email, action="download aarnaseqlake") db.session.add(eventlog) db.session.commit() if plot_arguments["download_format_value"] == "xlsx": outfile = io.BytesIO() EXC = pd.ExcelWriter(outfile) df_metadata.to_excel(EXC, sheet_name="metadata", index=None) EXC.save() outfile.seek(0) return send_file( outfile, attachment_filename=plot_arguments["download_name"] + ".metadata." + plot_arguments["download_format_value"], as_attachment=True) elif plot_arguments["download_format_value"] == "tsv": return Response( df_metadata.to_csv(sep="\t"), mimetype="text/csv", headers={ "Content-disposition": "attachment; filename=%s.tsv" % (plot_arguments["download_name"] + ".metadata") }) if download == "siggenes": plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) eventlog = UserLogging(email=current_user.email, action="download aarnaseqlake") db.session.add(eventlog) db.session.commit() if plot_arguments["download_format_value"] == "xlsx": outfile = io.BytesIO() EXC = pd.ExcelWriter(outfile) siggenesdf.to_excel(EXC, sheet_name="sig.genes", index=None) EXC.save() outfile.seek(0) return send_file( outfile, attachment_filename=plot_arguments["download_name"] + ".siggenesdf." + plot_arguments["download_format_value"], as_attachment=True) elif plot_arguments["download_format_value"] == "tsv": return Response( df_metadata.to_csv(sep="\t"), mimetype="text/csv", headers={ "Content-disposition": "attachment; filename=%s.tsv" % (plot_arguments["download_name"] + ".siggenesdf") }) if download == "geneexpression": plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) GO = pd.read_csv(plot_arguments["path_to_files"] + "GO.tsv", sep="\t") GO.columns = ["gene_id", "go_id", "go_name", "go_definition"] # for c in GO.columns.tolist(): # GO[c]=GO[c].apply(lambda x: fix_go(x) ) df_ge = pd.merge(df_ge, GO, on=["gene_id"], how="left") eventlog = UserLogging(email=current_user.email, action="download aarnaseqlake") db.session.add(eventlog) db.session.commit() if plot_arguments["download_format_value"] == "xlsx": outfile = io.BytesIO() EXC = pd.ExcelWriter(outfile) df_ge.to_excel(EXC, sheet_name="geneexp.", index=None) EXC.save() outfile.seek(0) return send_file( outfile, attachment_filename=plot_arguments["download_name"] + ".gene_expression." + plot_arguments["download_format_value"], as_attachment=True) elif plot_arguments["download_format_value"] == "tsv": return Response( df_ge.to_csv(sep="\t"), mimetype="text/csv", headers={ "Content-disposition": "attachment; filename=%s.tsv" % (plot_arguments["download_name"] + ".gene_expression") }) if download == "dge": plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) GO = pd.read_csv(plot_arguments["path_to_files"] + "GO.tsv", sep="\t") GO.columns = ["gene_id", "go_id", "go_name", "go_definition"] df_dge = pd.merge(df_dge, GO, on=["gene_id"], how="left") for c in GO.columns.tolist(): GO[c] = GO[c].apply(lambda x: fix_go(x)) eventlog = UserLogging(email=current_user.email, action="download aarnaseqlake") db.session.add(eventlog) db.session.commit() if plot_arguments["download_format_value"] == "xlsx": outfile = io.BytesIO() EXC = pd.ExcelWriter(outfile) df_dge.to_excel(EXC, sheet_name="dge", index=None) EXC.save() outfile.seek(0) return send_file( outfile, attachment_filename=plot_arguments["download_name"] + ".dge." + plot_arguments["download_format_value"], as_attachment=True) elif plot_arguments["download_format_value"] == "tsv": return Response(df_dge.to_csv(sep="\t"), mimetype="text/csv", headers={ "Content-disposition": "attachment; filename=%s.tsv" % (plot_arguments["download_name"] + ".dge") }) if download == "MAplot": plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) if type(df_dge) != type(pd.DataFrame()): flash( "No differential available to perform gene expression for an MA plot.", 'error') return render_template('apps/aarnaseqlake.html', apps=apps, **session["plot_arguments"]) reset_info = check_session_app(session, "iscatterplot", apps) if reset_info: flash(reset_info, 'error') session["filename"] = "<from RNAseq lake>" plot_arguments = iscatterplot.figure_defaults() session["plot_arguments"] = plot_arguments session["COMMIT"] = app.config['COMMIT'] session["app"] = "iscatterplot" df_dge["log10(baseMean)"] = df_dge["baseMean"].apply( lambda x: np.log10(x)) df_dge.loc[df_dge["padj"] <= 0.05, "Significant"] = "yes" df_dge.loc[df_dge["padj"] > 0.05, "Significant"] = "no" df_dge = df_dge.astype(str) session["df"] = df_dge.to_json() plot_arguments["xcols"] = df_dge.columns.tolist() plot_arguments["ycols"] = df_dge.columns.tolist() plot_arguments["groups"] = df_dge.columns.tolist() plot_arguments["labels_col"] = df_dge.columns.tolist() plot_arguments["xvals"] = "log10(baseMean)" plot_arguments["yvals"] = "log2FoldChange" plot_arguments["labels_col_value"] = "gene_name" plot_arguments["groups_value"] = "Significant" plot_arguments["list_of_groups"] = ["yes", "no"] plot_arguments["title"] = "MA plot" plot_arguments["xlabel"] = "log10(base Mean)" plot_arguments["ylabel"] = "log2(FC)" groups_settings = [] group_dic={"name":"yes",\ "markers":"4",\ "markersizes_col":"select a column..",\ "markerc":"red",\ "markerc_col":"select a column..",\ "markerc_write":plot_arguments["markerc_write"],\ "edge_linewidth":plot_arguments["edge_linewidth"],\ "edge_linewidth_col":"select a column..",\ "edgecolor":plot_arguments["edgecolor"],\ "edgecolor_col":"select a column..",\ "edgecolor_write":"",\ "marker":"circle",\ "markerstyles_col":"select a column..",\ "marker_alpha":"0.25",\ "markeralpha_col_value":"select a column.."} groups_settings.append(group_dic) group_dic={"name":"no",\ "markers":"4",\ "markersizes_col":"select a column..",\ "markerc":"black",\ "markerc_col":"select a column..",\ "markerc_write":plot_arguments["markerc_write"],\ "edge_linewidth":plot_arguments["edge_linewidth"],\ "edge_linewidth_col":"select a column..",\ "edgecolor":plot_arguments["edgecolor"],\ "edgecolor_col":"select a column..",\ "edgecolor_write":"",\ "marker":"circle",\ "markerstyles_col":"select a column..",\ "marker_alpha":"0.25",\ "markeralpha_col_value":"select a column.."} groups_settings.append(group_dic) plot_arguments["groups_settings"] = groups_settings session["plot_arguments"] = plot_arguments return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) if download == "Volcanoplot": plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) if type(df_ge) != type(pd.DataFrame()): flash( "No differential available to perform gene expression for an MA plot.", 'error') return render_template('apps/aarnaseqlake.html', apps=apps, **session["plot_arguments"]) reset_info = check_session_app(session, "iscatterplot", apps) if reset_info: flash(reset_info, 'error') session["filename"] = "<from RNAseq lake>" plot_arguments = iscatterplot.figure_defaults() session["COMMIT"] = app.config['COMMIT'] session["app"] = "iscatterplot" df_dge["-log10(padj)"] = df_dge["padj"].apply( lambda x: np.log10(x) * -1) df_dge.loc[df_dge["padj"] <= 0.05, "Significant"] = "yes" df_dge.loc[df_dge["padj"] > 0.05, "Significant"] = "no" df_dge = df_dge.astype(str) session["df"] = df_dge.to_json() plot_arguments["xcols"] = df_dge.columns.tolist() plot_arguments["ycols"] = df_dge.columns.tolist() plot_arguments["groups"] = df_dge.columns.tolist() plot_arguments["labels_col"] = df_dge.columns.tolist() plot_arguments["xvals"] = "log2FoldChange" plot_arguments["yvals"] = "-log10(padj)" plot_arguments["labels_col_value"] = "gene_name" plot_arguments["groups_value"] = "Significant" plot_arguments["list_of_groups"] = ["yes", "no"] plot_arguments["title"] = "Volcano plot" plot_arguments["xlabel"] = "log2(FC)" plot_arguments["ylabel"] = "-log10(padj)" groups_settings = [] group_dic={"name":"yes",\ "markers":"4",\ "markersizes_col":"select a column..",\ "markerc":"red",\ "markerc_col":"select a column..",\ "markerc_write":plot_arguments["markerc_write"],\ "edge_linewidth":plot_arguments["edge_linewidth"],\ "edge_linewidth_col":"select a column..",\ "edgecolor":plot_arguments["edgecolor"],\ "edgecolor_col":"select a column..",\ "edgecolor_write":"",\ "marker":"circle",\ "markerstyles_col":"select a column..",\ "marker_alpha":"0.25",\ "markeralpha_col_value":"select a column.."} groups_settings.append(group_dic) group_dic={"name":"no",\ "markers":"4",\ "markersizes_col":"select a column..",\ "markerc":"black",\ "markerc_col":"select a column..",\ "markerc_write":plot_arguments["markerc_write"],\ "edge_linewidth":plot_arguments["edge_linewidth"],\ "edge_linewidth_col":"select a column..",\ "edgecolor":plot_arguments["edgecolor"],\ "edgecolor_col":"select a column..",\ "edgecolor_write":"",\ "marker":"circle",\ "markerstyles_col":"select a column..",\ "marker_alpha":"0.25",\ "markeralpha_col_value":"select a column.."} groups_settings.append(group_dic) plot_arguments["groups_settings"] = groups_settings session["plot_arguments"] = plot_arguments return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) if download == "iheatmap": plot_arguments = session["plot_arguments"] plot_arguments, df_metadata, df_dge, df_ge, siggenesdf = get_tables( plot_arguments) sig_genes = df_dge[df_dge["padj"] < 0.05]["gene_name"].tolist() df_ge = df_ge[df_ge["gene_name"].isin(sig_genes)] if type(df_ge) != type(pd.DataFrame()): flash( "No differential available to perform gene expression for an MA plot.", 'error') return render_template('apps/aarnaseqlake.html', apps=apps, **session["plot_arguments"]) reset_info = check_session_app(session, "iheatmap", apps) if reset_info: flash(reset_info, 'error') plot_arguments = iheatmap.figure_defaults() session["filename"] = "<from RNAseq lake>" session["plot_arguments"] = plot_arguments session["COMMIT"] = app.config['COMMIT'] session["app"] = "iheatmap" cols = df_ge.columns.tolist() df_de = df_ge.astype(str) session["df"] = df_ge.to_json() plot_arguments["xcols"] = cols plot_arguments["ycols"] = cols plot_arguments["xvals"] = "gene_name" available_rows = list(set(df_ge["gene_name"].tolist())) available_rows.sort() plot_arguments["available_rows"] = available_rows plot_arguments["yvals"] = [ s for s in cols if s not in ["gene_name", "gene_id"] ] plot_arguments["title"] = "Heatmap" plot_arguments["zscore_value"] = "row" plot_arguments["colorscale_value"] = 'bluered' plot_arguments["lower_value"] = "-2" plot_arguments["center_value"] = "0" plot_arguments["upper_value"] = "2" plot_arguments["lower_color"] = "blue" plot_arguments["center_color"] = "white" plot_arguments["upper_color"] = "red" plot_arguments["col_cluster"] = "off" plot_arguments["title_size_value"] = "25" plot_arguments["color_bar_label"] = "z-score" plot_arguments["findrowup"] = "10" plot_arguments["findrowdown"] = "10" plot_arguments["xticklabels"] = "on" session["plot_arguments"] = plot_arguments return render_template('/apps/iheatmap.html', filename=session["filename"], apps=apps, **plot_arguments) return render_template('apps/aarnaseqlake.html', apps=apps, **session["plot_arguments"])
def iscatterplot(download=None): """ renders the plot on the fly. https://gist.github.com/illume/1f19a2cf9f26425b1761b63d9506331f """ reset_info = check_session_app(session, "scatterplot", apps) if reset_info: flash(reset_info, 'error') apps = FREEAPPS + session["PRIVATE_APPS"] if request.method == 'POST': # READ SESSION FILE IF AVAILABLE # AND OVERWRITE VARIABLES inputsessionfile = request.files["inputsessionfile"] if inputsessionfile: if inputsessionfile.filename.rsplit('.', 1)[1].lower() != "ses": plot_arguments = session["plot_arguments"] error_msg = "The file you have uploaded is not a session file. Please make sure you upload a session file with the correct `ses` extension." flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) session_ = json.load(inputsessionfile) if session_["ftype"] != "session": plot_arguments = session["plot_arguments"] error_msg = "The file you have uploaded is not a session file. Please make sure you upload a session file." flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) if session_["app"] != "iscatterplot": plot_arguments = session["plot_arguments"] error_msg = "The file was not loaded as it is associated with the '%s' and not with this app." % session_[ "app"] flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) del (session_["ftype"]) del (session_["COMMIT"]) for k in list(session_.keys()): session[k] = session_[k] plot_arguments = session["plot_arguments"] flash('Session file sucessufuly read.') # READ ARGUMENTS FILE IF AVAILABLE # AND OVERWRITE VARIABLES inputargumentsfile = request.files["inputargumentsfile"] if inputargumentsfile: if inputargumentsfile.filename.rsplit('.', 1)[1].lower() != "arg": plot_arguments = session["plot_arguments"] error_msg = "The file you have uploaded is not a arguments file. Please make sure you upload a session file with the correct `arg` extension." flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) session_ = json.load(inputargumentsfile) if session_["ftype"] != "arguments": plot_arguments = session["plot_arguments"] error_msg = "The file you have uploaded is not an arguments file. Please make sure you upload an arguments file." flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) if session_["app"] != "iscatterplot": plot_arguments = session["plot_arguments"] error_msg = "The file was not load as it is associated with the '%s' and not with this app." % session_[ "app"] flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename=session["filename"], apps=apps, **plot_arguments) del (session_["ftype"]) del (session_["COMMIT"]) for k in list(session_.keys()): session[k] = session_[k] plot_arguments = session["plot_arguments"] flash('Arguments file sucessufuly read.', "info") if not inputsessionfile and not inputargumentsfile: # SELECTION LISTS DO NOT GET UPDATED lists = session["lists"] # USER INPUT/PLOT_ARGUMENTS GETS UPDATED TO THE LATEST INPUT # WITH THE EXCEPTION OF SELECTION LISTS plot_arguments = session["plot_arguments"] for a in list(plot_arguments.keys()): if (a in list(request.form.keys())) & (a not in list( lists.keys()) + session["notUpdateList"]): plot_arguments[a] = request.form[a] # # VALUES SELECTED FROM SELECTION LISTS # # GET UPDATED TO THE LATEST CHOICE # for k in list(lists.keys()): # if k in list(request.form.keys()): # plot_arguments[lists[k]]=request.form[k] # checkboxes for checkbox in session["checkboxes"]: if checkbox in list(request.form.keys()): plot_arguments[checkbox] = "on" else: try: plot_arguments[checkbox] = request.form[checkbox] except: if plot_arguments[checkbox][0] != ".": plot_arguments[checkbox] = "off" # UPDATE SESSION VALUES session["plot_arguments"] = plot_arguments # IF THE UPLOADS A NEW FILE # THAN UPDATE THE SESSION FILE # READ INPUT FILE inputfile = request.files["inputfile"] if inputfile: filename = secure_filename(inputfile.filename) if allowed_file(inputfile.filename): session["filename"] = filename fileread = inputfile.read() filestream = io.BytesIO(fileread) extension = filename.rsplit('.', 1)[1].lower() if extension == "xlsx": df = pd.read_excel(filestream) elif extension == "csv": df = pd.read_csv(filestream) elif extension == "tsv": df = pd.read_csv(filestream, sep="\t") df = df.astype(str) session["df"] = df.to_json() cols = df.columns.tolist() if session["plot_arguments"]["groups"] not in cols: session["plot_arguments"]["groups"] = ["None"] + cols if session["plot_arguments"]["markerstyles_cols"] not in cols: session["plot_arguments"]["markerstyles_cols"] = [ "select a column.." ] + cols if session["plot_arguments"]["markerc_cols"] not in cols: session["plot_arguments"]["markerc_cols"] = [ "select a column.." ] + cols if session["plot_arguments"]["markersizes_cols"] not in cols: session["plot_arguments"]["markersizes_cols"] = [ "select a column.." ] + cols if session["plot_arguments"]["markeralpha_col"] not in cols: session["plot_arguments"]["markeralpha_col"] = [ "select a column.." ] + cols if session["plot_arguments"]["labels_col"] not in cols: session["plot_arguments"]["labels_col"] = [ "select a column.." ] + cols # IF THE USER HAS NOT YET CHOOSEN X AND Y VALUES THAN PLEASE SELECT if (session["plot_arguments"]["xvals"] not in cols) & ( session["plot_arguments"]["yvals"] not in cols): session["plot_arguments"]["xcols"] = cols session["plot_arguments"]["xvals"] = cols[0] session["plot_arguments"]["ycols"] = cols session["plot_arguments"]["yvals"] = cols[1] sometext = "Please select which values should map to the x and y axes." plot_arguments = session["plot_arguments"] flash(sometext, 'info') return render_template('/apps/iscatterplot.html', filename=filename, apps=apps, **plot_arguments) else: # IF UPLOADED FILE DOES NOT CONTAIN A VALID EXTENSION PLEASE UPDATE error_message = "You can can only upload files with the following extensions: 'xlsx', 'tsv', 'csv'. Please make sure the file '%s' \ has the correct format and respective extension and try uploadling it again." % filename flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename="Select file..", apps=apps, **plot_arguments) if "df" not in list(session.keys()): error_message = "No data to plot, please upload a data or session file." flash(error_msg, 'error') return render_template('/apps/iscatterplot.html', filename="Select file..", apps=apps, **plot_arguments) # MAKE SURE WE HAVE THE LATEST ARGUMENTS FOR THIS SESSION filename = session["filename"] plot_arguments = session["plot_arguments"] # READ INPUT DATA FROM SESSION JSON df = pd.read_json(session["df"]) # CALL FIGURE FUNCTION try: fig = make_figure(df, plot_arguments) # TRANSFORM FIGURE TO HTML components script, div = components(fig) return render_template('/apps/iscatterplot.html', the_script=script, the_div=div, filename=filename, apps=apps, **plot_arguments) except Exception as e: send_exception_email(user=current_user, eapp="iscatterplot", emsg=e, etime=str(datetime.now())) flash(e, 'error') return render_template('/apps/iscatterplot.html', filename=filename, apps=apps, **plot_arguments) else: # if download == "download": # # READ INPUT DATA FROM SESSION JSON # df=pd.read_json(session["df"]) # plot_arguments=session["plot_arguments"] # # CALL FIGURE FUNCTION # fig=make_figure(df,plot_arguments) # #flash('Figure is being sent to download but will not be updated on your screen.') # figfile = io.BytesIO() # figfile = io.StringIO() # mimetypes={"png":'image/png',"pdf":"application/pdf","svg":"image/svg+xml"} # if plot_arguments["downloadf"] == "svg": # from bokeh.io import export_svgs # export_svgs(fig, filename=fout) # #plt.savefig(figfile, format=plot_arguments["downloadf"]) # #plt.close() # figfile.seek(0) # rewind to beginning of file # eventlog = UserLogging(email=current_user.email,action="download figure iscatterplot") # db.session.add(eventlog) # db.session.commit() # return send_file(figfile, mimetype=mimetypes[plot_arguments["downloadf"]], as_attachment=True, attachment_filename=plot_arguments["downloadn"]+"."+plot_arguments["downloadf"] ) if "app" not in list(session.keys()): return_to_plot = False elif session["app"] != "iscatterplot": return_to_plot = False else: return_to_plot = True if not return_to_plot: # INITIATE SESSION session["filename"] = "Select file.." plot_arguments, lists, notUpdateList, checkboxes = figure_defaults( ) session["plot_arguments"] = plot_arguments session["lists"] = lists session["notUpdateList"] = notUpdateList session["COMMIT"] = app.config['COMMIT'] session["app"] = "iscatterplot" session["checkboxes"] = checkboxes eventlog = UserLogging(email=current_user.email, action="visit iscatterplot") db.session.add(eventlog) db.session.commit() return render_template('apps/iscatterplot.html', filename=session["filename"], apps=apps, **session["plot_arguments"]) # @app.route('/download/<json_type>', methods=['GET','POST']) # @login_required # def download(json_type="arg"): # # READ INPUT DATA FROM SESSION JSON # session_=session_to_file(session,json_type) # session_file = io.BytesIO() # session_file.write(json.dumps(session_).encode()) # session_file.seek(0) # plot_arguments=session["plot_arguments"] # eventlog = UserLogging(email=current_user.email,action="download %s iscatterplot" %json_type) # db.session.add(eventlog) # db.session.commit() # return send_file(session_file, mimetype='application/json', as_attachment=True, attachment_filename=plot_arguments["session_argumentsn"]+"."+json_type )