Beispiel #1
0
def histogram(download=None):

    apps = current_user.user_apps
    reset_info = check_session_app(session, "histogram", 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"] = "histogram"
    """
    renders the plot on the fly.
    https://gist.github.com/illume/1f19a2cf9f26425b1761b63d9506331f
    """
    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "histogram")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/histogram.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"], "histogram")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/histogram.html',
                                           filename=session["filename"],
                                           apps=apps,
                                           **plot_arguments)
                flash(msg, "info")

            # IF THE USER UPLOADS A NEW FILE
            # THEN 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()

                    # INDICATE THE USER TO SELECT THE COLUMNS TO PLOT AS HISTOGRAMS
                    session["plot_arguments"] = figure_defaults()
                    session["plot_arguments"]["cols"] = cols

                    sometext = "Please select the columns from which we will plot your histograms"
                    plot_arguments = session["plot_arguments"]
                    flash(sometext, 'info')
                    return render_template('/apps/histogram.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/histogram.html',
                                           filename="Select file..",
                                           apps=apps,
                                           **plot_arguments)

            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/histogram.html',
                                       filename="Select file..",
                                       apps=apps,
                                       **plot_arguments)

            if not request.files["inputsessionfile"] and not request.files[
                    "inputargumentsfile"]:

                df = pd.read_json(session["df"])
                cols = df.columns.tolist()
                filename = session["filename"]

                #IN CASE THE USER HAS UNSELECTED ALL THE COLUMNS THAT WE NEED TO PLOT THE HISTOGRAMS
                if request.form.getlist("vals") == []:
                    filename = session["filename"]
                    session["plot_arguments"] = figure_defaults()
                    session["plot_arguments"]["cols"] = cols
                    sometext = "Please select the columns from which we will plot your histograms"
                    plot_arguments = session["plot_arguments"]
                    flash(sometext, 'info')
                    return render_template('/apps/histogram.html',
                                           filename=filename,
                                           apps=apps,
                                           **plot_arguments)

                #IF THE USER SELECTED THE COLUMNS TO BE PLOTTED FOR THE FIRST TIME,
                #WE INITIALIZE THE DICTIONARY GROUPS SETTINGS
                plot_arguments = session["plot_arguments"]
                if plot_arguments["groups_settings"] == dict():
                    plot_arguments["vals"] = request.form.getlist("vals")
                    groups = plot_arguments["vals"]
                    groups.sort()
                    groups_settings = dict()
                    for group in groups:
                        groups_settings[group]={"name":group,\
                            "values":df[group],\
                            "label":group,\
                            "color_value":None,\
                            "color_rgb":"",\
                            "bins_value":"auto",\
                            "bins_number":"",\
                            "orientation_value":"vertical",\
                            "histtype_value":"bar",\
                            "linewidth":0.5,\
                            "linestyle_value":"solid",\
                            "line_color":None,\
                            "line_rgb":"",\
                            "fill_alpha":0.8,\
                            "density":".off",\
                            "cumulative":".off"}

                    plot_arguments["groups_settings"] = groups_settings
                    plot_arguments = read_request(request)

                    #CALL FIGURE FUNCTION
                    fig = make_figure(df, plot_arguments)
                    #TRANSFORM FIGURE TO BYTES AND BASE64 STRING
                    figfile = io.BytesIO()
                    plt.savefig(figfile, format='png')
                    plt.close()
                    figfile.seek(0)  # rewind to beginning of file
                    figure_url = base64.b64encode(
                        figfile.getvalue()).decode('utf-8')
                    return render_template('/apps/histogram.html',
                                           figure_url=figure_url,
                                           filename=filename,
                                           apps=apps,
                                           **plot_arguments)

                #IF THE USER HAS SELECTED NEW COLUMNS TO BE PLOTTED
                # if plot_arguments["vals"]!=request.form.getlist("vals"):

                #groups=plot_arguments["vals"]
                groups = request.form.getlist("vals")
                groups.sort()
                groups_settings = dict()

                for group in groups:
                    if group not in list(
                            plot_arguments["groups_settings"].keys()):
                        groups_settings[group]={"name":group,\
                                "label":group,\
                                "color_value":None,\
                                "color_rgb":"",\
                                "bins_value":"auto",\
                                "bins_number":"",\
                                "orientation_value":"vertical",\
                                "histtype_value":"bar",\
                                "linewidth":0.5,\
                                "linestyle_value":"solid",\
                                "line_color":None,\
                                "line_rgb":"",\
                                "fill_alpha":0.8,
                                "density":".off",\
                                "cumulative":".off"}

                    else:
                        groups_settings[group]={"name":group,\
                        "label":request.form["%s.label" %group],\
                        "color_value":request.form["%s.color_value" %group],\
                        "color_rgb":request.form["%s.color_rgb" %group],\
                        "bins_value":request.form["%s.bins_value" %group],\
                        "bins_number":request.form["%s.bins_number" %group],\
                        "orientation_value":request.form["%s.orientation_value" %group],\
                        "histtype_value":request.form["%s.histtype_value" %group],\
                        "linewidth":request.form["%s.linewidth" %group],\
                        "linestyle_value":request.form["%s.linestyle_value" %group],\
                        "line_color":request.form["%s.line_color" %group],\
                        "line_rgb":request.form["%s.line_rgb" %group],\
                        "fill_alpha":request.form["%s.fill_alpha" %group]}

                        #If the user does not tick the options the arguments do not appear as keys in request.form
                        if "%s.density" % group in request.form.keys():
                            groups_settings[group][
                                "density"] = "on"  #request.form["%s.density" %group]
                        else:
                            groups_settings[group]["density"] = "off"

                        if "%s.cumulative" % group in request.form.keys():
                            groups_settings[group][
                                "cumulative"] = "on"  #request.form["%s.cumulative" %group]
                        else:
                            groups_settings[group]["cumulative"] = "off"

                plot_arguments["groups_settings"] = groups_settings

                plot_arguments = read_request(request)
                #session["plot_arguments"]=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 BYTES AND BASE64 STRING
            figfile = io.BytesIO()
            plt.savefig(figfile, format='png')
            plt.close()
            figfile.seek(0)  # rewind to beginning of file
            figure_url = base64.b64encode(figfile.getvalue()).decode('utf-8')

            return render_template('/apps/histogram.html',
                                   figure_url=figure_url,
                                   filename=filename,
                                   apps=apps,
                                   **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="histogram",
                                      session=session)
            flash(tb_str, 'traceback')
            return render_template('/apps/histogram.html',
                                   filename=session["filename"],
                                   apps=apps,
                                   **session["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)

            figfile = io.BytesIO()
            mimetypes = {
                "png": 'image/png',
                "pdf": "application/pdf",
                "svg": "image/svg+xml"
            }
            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 histogram")
            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/histogram.html',
                               filename=session["filename"],
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #2
0
def violinplot(download=None):

    apps = current_user.user_apps
    plot_arguments = None

    reset_info = check_session_app(session, "violinplot", 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"] = "violinplot"

    if request.method == 'POST':
        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "violinplot")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/violinplot.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"], "violinplot")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/violinplot.html',
                                           filename=session["filename"],
                                           apps=apps,
                                           **plot_arguments)
                flash(msg, "info")

            # IF THE USER UPLOADS A NEW FILE
            # THEN 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()
                    vals = [None] + cols

                    # sometext="Please select at least one numeric column to create your violin plot."
                    session["plot_arguments"]["cols"] = cols
                    session["plot_arguments"]["vals"] = vals
                    plot_arguments = session["plot_arguments"]
                    # plot_arguments=read_request(request)

                    # flash(sometext,'info')
                    return render_template('/apps/violinplot.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/violinplot.html',
                                           filename=session["filename"],
                                           apps=apps,
                                           **plot_arguments)

            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/violinplot.html',
                                       filename="Select file..",
                                       apps=apps,
                                       **plot_arguments)

            # USER INPUT/PLOT_ARGUMENTS GETS UPDATED TO THE LATEST INPUT
            plot_arguments = read_request(request)
            # vals=request.form.getlist("vals")
            vals = plot_arguments["vals"]
            df = pd.read_json(session["df"])
            filename = session["filename"]

            #IN CASE THE USER HAS NOT SELECTED X_VAL or Y_VAL
            if plot_arguments["x_val"] == "None" or plot_arguments[
                    "y_val"] == "None":
                sometext = "Please select a valid value to plot in your X and Y axes"
                plot_arguments = session["plot_arguments"]
                plot_arguments["vals"] = vals
                flash(sometext, 'info')
                return render_template('/apps/iviolinplot.html',
                                       filename=filename,
                                       apps=apps,
                                       **plot_arguments)

            #IN CASE THE USER HAS UNSELECTED ALL THE COLUMNS THAT WE NEED TO PLOT THE VIOLINPLOT
            if vals == []:
                sometext = "Please select at least one numeric column from which we will plot your violinplot"
                plot_arguments = session["plot_arguments"]
                plot_arguments["vals"] = vals
                flash(sometext, 'info')
                return render_template('/apps/violinplot.html',
                                       filename=filename,
                                       apps=apps,
                                       **plot_arguments)

            #VERIFY THERE IS AT LEAST ONE NUMERIC COLUMN SELECTED BY THE USER
            vals_copy = vals.copy()
            vals_copy.remove(None)
            if not any(df[vals_copy].dtypes.apply(is_numeric_dtype)):
                sometext = "Remember that at least one of the columns you select has to be numeric"
                session["plot_arguments"]["vals"] = [None] + vals
                plot_arguments = session["plot_arguments"]
                plot_arguments["vals"] = vals
                flash(sometext, 'info')
                return render_template('/apps/violinplot.html',
                                       filename=filename,
                                       apps=apps,
                                       **plot_arguments)

                # #IF THE USER HAS CHANGED THE COLUMNS TO PLOT
                # if vals+["None"] != plot_arguments["vals"]:
                #     plot_arguments=figure_defaults()
                #     cols=df.columns.tolist()
                #     plot_arguments["vals"]=vals+["None"]
                #     plot_arguments["cols"]=cols
                #     session["plot_arguments"]=plot_arguments
                #     sometext="Please tweak the arguments of your violin plot"
                #     flash(sometext,'info')
                #     return render_template('/apps/violinplot.html' , filename=filename, apps=apps,**plot_arguments)

            session["plot_arguments"] = plot_arguments

            # MAKE SURE WE HAVE THE LATEST ARGUMENTS FOR THIS SESSION
            #filename=session["filename"]
            #plot_arguments=session["plot_arguments"]
            #plot_arguments["vals"]=vals
            #session["plot_arguments"]["vals"]=vals+["None"]

            fig = make_figure(df, plot_arguments)

            #TRANSFORM FIGURE TO BYTES AND BASE64 STRING
            figfile = io.BytesIO()
            plt.savefig(figfile, format='png')
            plt.close()
            figfile.seek(0)  # rewind to beginning of file
            figure_url = base64.b64encode(figfile.getvalue()).decode('utf-8')
            return render_template('/apps/violinplot.html',
                                   figure_url=figure_url,
                                   filename=filename,
                                   apps=apps,
                                   **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="violinplot",
                                      session=session)
            flash(tb_str, 'traceback')
            if not plot_arguments:
                plot_arguments = session["plot_arguments"]
            return render_template('/apps/violinplot.html',
                                   filename=session["filename"],
                                   apps=apps,
                                   **session["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)

            figfile = io.BytesIO()
            mimetypes = {
                "png": 'image/png',
                "pdf": "application/pdf",
                "svg": "image/svg+xml"
            }
            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 violinplot")
            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/violinplot.html',
                               filename=session["filename"],
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #3
0
def heatmap(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,"heatmap",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"]="heatmap"

    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"] :
                msg, plot_arguments, error=read_session_file(request.files["inputsessionfile"],"heatmap")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/heatmap.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"],"heatmap")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/heatmap.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 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()

                    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"]["yvals"] not in cols) | (session["plot_arguments"]["xvals"] not in cols):

                        if session["plot_arguments"]["xvals"] not in cols:
                            session["plot_arguments"]["xvals"]=cols[0]
                            session["plot_arguments"]["xvals_colors_list"]=["select a row.."]+df[session["plot_arguments"]["xvals"]].tolist()

                        if session["plot_arguments"]["yvals"] not in cols:
                            session["plot_arguments"]["yvals"]=cols[1:]
                                    
                        sometext="Please select which columns should be used for plotting."
                        plot_arguments=session["plot_arguments"]
                        flash(sometext,'info')
                        return render_template('/apps/heatmap.html' , filename=filename, apps=apps,**plot_arguments)
                    
                    plot_arguments=session["plot_arguments"]
                    flash("New file uploaded.",'info')
                    return render_template('/apps/heatmap.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_message,'error')
                    return render_template('/apps/heatmap.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/heatmap.html' , filename="Select file..", apps=apps,  **plot_arguments)

            # READ INPUT DATA FROM SESSION JSON
            df=pd.read_json(session["df"])

            if len(df) == 1:
                session["plot_arguments"]["row_cluster"]="off"
                flash("You only have one row. Row dendrogram is now off.")
            if len(session["plot_arguments"]["yvals"]) == 1:
                session["plot_arguments"]["col_cluster"]="off"
                flash("You only have one column. Columns dendrogram is now off.")

            session["plot_arguments"]["xvals_colors_list"]=["select a row.."]+df[request.form["xvals"]].tolist()
            # MAKE SURE WE HAVE THE LATEST ARGUMENTS FOR THIS SESSION
            filename=session["filename"]
            plot_arguments=session["plot_arguments"]

          


            # CALL FIGURE FUNCTION
            fig, cols_cluster_numbers, index_cluster_numbers, df_=make_figure(df,plot_arguments)

            # TRANSFORM FIGURE TO BYTES AND BASE64 STRING
            figfile = io.BytesIO()
            plt.savefig(figfile, format='png')
            plt.close()
            figfile.seek(0)  # rewind to beginning of file
            figure_url = base64.b64encode(figfile.getvalue()).decode('utf-8')

            return render_template('/apps/heatmap.html', figure_url=figure_url, filename=filename, apps=apps, **plot_arguments)

        except Exception as e:
            tb_str=handle_exception(e,user=current_user,eapp="heatmap",session=session)
            flash(tb_str,'traceback')
            if not plot_arguments:
                plot_arguments=session["plot_arguments"]
            return render_template('/apps/heatmap.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, cols_cluster_numbers, index_cluster_numbers, df_=make_figure(df,plot_arguments)

            figfile = io.BytesIO()
            mimetypes={"png":'image/png',"pdf":"application/pdf","svg":"image/svg+xml"}
            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 heatmap")
            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 download == "clusters":

            plot_arguments=session["plot_arguments"]
            if int(plot_arguments["n_cols_cluster"]) == 0:
                fixed_cols=True
                plot_arguments["n_cols_cluster"]=1
            else:
                fixed_cols=False

            if int(plot_arguments["n_rows_cluster"]) == 0:
                fixed_rows=True
                plot_arguments["n_rows_cluster"]=1
            else:
                fixed_rows=False

            # READ INPUT DATA FROM SESSION JSON
            df=pd.read_json(session["df"])

            plot_arguments=session["plot_arguments"]

            # CALL FIGURE FUNCTION

            fig, cols_cluster_numbers, index_cluster_numbers, df_=make_figure(df,plot_arguments)

            excelfile = io.BytesIO()
            EXC=pd.ExcelWriter(excelfile)
            df_.to_excel(EXC,sheet_name="heatmap",index=None)
            cols_cluster_numbers.to_excel(EXC,sheet_name="rows",index=None)
            index_cluster_numbers.to_excel(EXC,sheet_name="cols",index=None)
            EXC.save()

            excelfile.seek(0)
           
            if fixed_rows:
                plot_arguments["n_rows_cluster"]=0

            if fixed_cols:
                plot_arguments["n_cols_cluster"]=0

            eventlog = UserLogging(email=current_user.email,action="download figure heatmap clusters")
            db.session.add(eventlog)
            db.session.commit()
            
            return send_file(excelfile, attachment_filename=plot_arguments["downloadn"]+".xlsx")
        
        return render_template('apps/heatmap.html',  filename=session["filename"], apps=apps, **session["plot_arguments"])
def lifespan(download=None):

    apps = current_user.user_apps
    plot_arguments = None

    reset_info = check_session_app(session, "lifespan", 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"] = "lifespan"

    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "lifespan")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/lifespan.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"], "lifespan")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/lifespan.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 session["plot_arguments"]["censors_col"] not in cols:
                        session["plot_arguments"]["censors_col"] = ["None"
                                                                    ] + cols

                    if session["plot_arguments"]["groups"] not in cols:
                        session["plot_arguments"]["groups"] = ["None"] + cols

                    # 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:]

                        sometext = "Please select which columns should be used for plotting."
                        plot_arguments = session["plot_arguments"]
                        flash(sometext, 'info')
                        return render_template('/apps/lifespan.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/lifespan.html',
                                           filename="Select file..",
                                           apps=apps,
                                           **plot_arguments)

            if not request.files["inputsessionfile"] and not request.files[
                    "inputargumentsfile"]:

                # USER INPUT/PLOT_ARGUMENTS GETS UPDATED TO THE LATEST INPUT
                # WITH THE EXCEPTION OF SELECTION LISTS
                plot_arguments = session["plot_arguments"]

                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,\
                                "censor_marker_value":plot_arguments["censor_marker_value"], \
                                "censor_marker_size_val":plot_arguments["censor_marker_size_val"], \
                                "edgecolor":plot_arguments["edgecolor"], \
                                "edgecolor_write":"", \
                                "edge_linewidth":plot_arguments["edge_linewidth"], \
                                "markerc":plot_arguments["markerc"], \
                                "markerc_write":"", \
                                "marker_alpha":plot_arguments["marker_alpha"], \
                                "ci_alpha":plot_arguments["ci_alpha"], \
                                "linestyle_value":plot_arguments["linestyle_value"], \
                                "linestyle_write":"", \
                                "linewidth_write":plot_arguments["linewidth_write"], \
                                "line_color_value":plot_arguments["line_color_value"],\
                                "linecolor_write":"", \
                                "show_censors":plot_arguments["show_censors"], \
                                "Conf_Interval":plot_arguments["Conf_Interval"], \
                                "ci_legend":plot_arguments["ci_legend"], \
                                "ci_force_lines":plot_arguments["ci_force_lines"]}
                            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,\
                            "censor_marker_value":request.form["%s.censor_marker_value" %group], \
                            "censor_marker_size_val":request.form["%s.censor_marker_size_val" %group], \
                            "edgecolor":request.form["%s.edgecolor" %group], \
                            "edgecolor_write":request.form["%s.edgecolor_write" %group], \
                            "edge_linewidth":request.form["%s.edge_linewidth" %group], \
                            "markerc":request.form["%s.markerc" %group], \
                            "markerc_write":request.form["%s.markerc_write" %group], \
                            "marker_alpha":request.form["%s.marker_alpha" %group], \
                            "ci_alpha":request.form["%s.ci_alpha" %group], \
                            "linestyle_value":request.form["%s.linestyle_value" %group], \
                            "linestyle_write":request.form["%s.linestyle_write" %group], \
                            "linewidth_write":request.form["%s.linewidth_write" %group], \
                            "line_color_value":request.form["%s.line_color_value" %group],\
                            "linecolor_write":request.form["%s.linecolor_write" %group]
                        }

                        if request.form.get("%s.show_censors" % group) == 'on':
                            group_dic["show_censors"] = 'on'
                        else:
                            group_dic["show_censors"] = 'off'
                        if request.form.get("%s.Conf_Interval" %
                                            group) == 'on':
                            group_dic["Conf_Interval"] = 'on'
                        else:
                            group_dic["Conf_Interval"] = 'off'
                        if request.form.get("%s.ci_legend" % group) == 'on':
                            group_dic["ci_legend"] = 'on'
                        else:
                            group_dic["ci_legend"] = 'off'
                        if request.form.get("%s.ci_force_lines" %
                                            group) == 'on':
                            group_dic["ci_force_lines"] = 'on'
                        else:
                            group_dic["ci_force_lines"] = 'off'

                        groups_settings.append(group_dic)
                    plot_arguments["groups_settings"] = groups_settings

                session["plot_arguments"] = plot_arguments
                plot_arguments = read_request(request)

            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/lifespan.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:
            if str(plot_arguments["groups_value"]) == "None":
                df_ls, fig = make_figure(df, plot_arguments)

                df_ls = df_ls.astype(str)
                session["df_ls"] = df_ls.to_json()

                # TRANSFORM FIGURE TO BYTES AND BASE64 STRING
                figfile = io.BytesIO()
                plt.savefig(figfile, format='png')
                plt.close()
                figfile.seek(0)  # rewind to beginning of file
                figure_url = base64.b64encode(
                    figfile.getvalue()).decode('utf-8')

                #return render_template('/apps/lifespan.html', figure_url=figure_url, filename=filename, apps=apps, **plot_arguments)

                df_selected = df_ls[:50]
                cols_to_format = df_selected.columns.tolist()
                table_headers = cols_to_format

                for c in cols_to_format:
                    df_selected[c] = df_selected[c].apply(lambda x: nFormat(x))

                df_selected = list(df_selected.values)
                df_selected = [list(s) for s in df_selected]

                return render_template('/apps/lifespan.html',
                                       figure_url=figure_url,
                                       table_headers=table_headers,
                                       table_contents=df_selected,
                                       filename=filename,
                                       apps=apps,
                                       **plot_arguments)

            elif str(plot_arguments["groups_value"]) != "None":
                df_ls, fig, cph_stats, cph_coeffs = make_figure(
                    df, plot_arguments)

                df_ls = df_ls.astype(str)
                session["df_ls"] = df_ls.to_json()

                cph_stats = cph_stats.astype(str)
                session["cph_stats"] = cph_stats.to_json()

                cph_coeffs = cph_coeffs.astype(str)
                session["cph_coeffs"] = cph_coeffs.to_json()

                # TRANSFORM FIGURE TO BYTES AND BASE64 STRING
                figfile = io.BytesIO()
                plt.savefig(figfile, format='png')
                plt.close()
                figfile.seek(0)  # rewind to beginning of file
                figure_url = base64.b64encode(
                    figfile.getvalue()).decode('utf-8')

                df_selected = df_ls[:50]
                cols_to_format = df_selected.columns.tolist()
                table_headers = cols_to_format

                for c in cols_to_format:
                    df_selected[c] = df_selected[c].apply(lambda x: nFormat(x))

                df_selected = list(df_selected.values)
                df_selected = [list(s) for s in df_selected]

                df_coeffs = cph_coeffs[:50]
                cols_to_format_coeffs = df_coeffs.columns.tolist()
                table_headers_coeffs = cols_to_format_coeffs

                for c in cols_to_format_coeffs:
                    df_coeffs[c] = df_coeffs[c].apply(lambda x: nFormat(x))

                df_coeffs = list(df_coeffs.values)
                df_coeffs = [list(s) for s in df_coeffs]

                df_stats = cph_stats[:50]
                cols_to_format_stats = df_stats.columns.tolist()
                table_headers_stats = cols_to_format_stats

                for c in cols_to_format_stats:
                    df_stats[c] = df_stats[c].apply(lambda x: nFormat(x))

                df_stats = list(df_stats.values)
                df_stats = [list(s) for s in df_stats]

                return render_template(
                    '/apps/lifespan.html',
                    figure_url=figure_url,
                    table_headers=table_headers,
                    table_contents=df_selected,
                    table_headers_coeffs=table_headers_coeffs,
                    table_contents_coeff=df_coeffs,
                    table_headers_stats=table_headers_stats,
                    table_contents_stats=df_stats,
                    filename=filename,
                    apps=apps,
                    **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="lifespan",
                                      session=session)
            filename = session["filename"]
            flash(tb_str, 'traceback')
            if not plot_arguments:
                plot_arguments = session["plot_arguments"]
            return render_template('/apps/lifespan.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"]

            if str(plot_arguments["groups_value"]) == "None":
                # CALL FIGURE FUNCTION
                df_ls, fig = make_figure(df, plot_arguments)

            elif str(plot_arguments["groups_value"]) != "None":
                # CALL FIGURE FUNCTION
                df_ls, fig, cph_coeff, cph_stats = make_figure(
                    df, plot_arguments)

            figfile = io.BytesIO()
            mimetypes = {
                "png": 'image/png',
                "pdf": "application/pdf",
                "svg": "image/svg+xml"
            }
            plt.savefig(figfile, format=plot_arguments["download_fig"])
            plt.close()
            figfile.seek(0)  # rewind to beginning of file

            eventlog = UserLogging(email=current_user.email,
                                   action="download figure lifespan curve")
            db.session.add(eventlog)
            db.session.commit()

            return send_file(
                figfile,
                mimetype=mimetypes[plot_arguments["download_fig"]],
                as_attachment=True,
                attachment_filename=plot_arguments["downloadn_fig"] + "." +
                plot_arguments["download_fig"])

        if download == "results":

            # READ INPUT DATA FROM SESSION JSON
            df = pd.read_json(session["df"])
            plot_arguments = session["plot_arguments"]

            if str(plot_arguments["groups_value"]) == "None":
                # CALL FIGURE FUNCTION
                df_ls, fig = make_figure(df, plot_arguments)

            elif str(plot_arguments["groups_value"]) != "None":
                # CALL FIGURE FUNCTION
                df_ls, fig, cph_coeff, cph_stats = make_figure(
                    df, plot_arguments)

            eventlog = UserLogging(email=current_user.email,
                                   action="download table survival analysis")
            db.session.add(eventlog)
            db.session.commit()

            if plot_arguments["downloadf"] == "xlsx":
                excelfile = io.BytesIO()
                EXC = pd.ExcelWriter(excelfile)
                df_ls.to_excel(EXC, sheet_name="survival_analysis", 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_ls.to_csv(sep="\t"),
                                mimetype="text/csv",
                                headers={
                                    "Content-disposition":
                                    "attachment; filename=%s.tsv" %
                                    plot_arguments["downloadn"]
                                })

        if download == "cph":

            # READ INPUT DATA FROM SESSION JSON
            df = pd.read_json(session["df"])
            plot_arguments = session["plot_arguments"]

            if str(plot_arguments["groups_value"]) != "None":
                # CALL FIGURE FUNCTION
                df_ls, fig, cph_coeff, cph_stats = make_figure(
                    df, plot_arguments)

            eventlog = UserLogging(
                email=current_user.email,
                action="download table cox proportional hazard")
            db.session.add(eventlog)
            db.session.commit()

            if plot_arguments["downloadf"] == "xlsx":
                excelfile = io.BytesIO()
                EXC = pd.ExcelWriter(excelfile)
                cph_stats.to_excel(EXC, sheet_name="Statistics", index=None)
                cph_coeff.to_excel(EXC,
                                   sheet_name="CoxProportionalHazard_coeff",
                                   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(cph_coeff.to_csv(sep="\t", mode='a'),
                                mimetype="text/csv",
                                headers={
                                    "Content-disposition":
                                    "attachment; filename=%s.tsv" %
                                    plot_arguments["downloadn"]
                                })

        return render_template('apps/lifespan.html',
                               filename=session["filename"],
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #5
0
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"])
Beispiel #6
0
def ihistogram(download=None):

    apps = current_user.user_apps
    plot_arguments = None

    reset_info = check_session_app(session, "ihistogram", 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"] = "ihistogram"
    """
    renders the plot on the fly.
    https://gist.github.com/illume/1f19a2cf9f26425b1761b63d9506331f
    """
    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "ihistogram")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/ihistogram.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"], "ihistogram")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/ihistogram.html',
                                           filename=session["filename"],
                                           apps=apps,
                                           **plot_arguments)
                flash(msg, "info")

            # IF THE USER UPLOADS A NEW FILE
            # THEN 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()

                    # INDICATE THE USER TO SELECT THE COLUMNS TO PLOT AS HISTOGRAMS
                    session["plot_arguments"] = figure_defaults()
                    session["plot_arguments"]["cols"] = cols

                    sometext = "Please select the columns from which we will plot your iHistograms"
                    plot_arguments = session["plot_arguments"]
                    flash(sometext, 'info')
                    return render_template('/apps/ihistogram.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/ihistogram.html',
                                           filename="Select file..",
                                           apps=apps,
                                           **plot_arguments)

            if not request.files["inputsessionfile"] and not request.files[
                    "inputargumentsfile"]:

                df = pd.read_json(session["df"])
                cols = df.columns.tolist()
                filename = session["filename"]

                #IN CASE THE USER HAS UNSELECTED ALL THE COLUMNS THAT WE NEED TO PLOT THE HISTOGRAMS
                if request.form.getlist("vals") == []:
                    session["plot_arguments"] = figure_defaults()
                    session["plot_arguments"]["cols"] = cols
                    sometext = "Please select the columns from which we will plot your iHistograms"
                    plot_arguments = session["plot_arguments"]
                    flash(sometext, 'info')
                    return render_template('/apps/ihistogram.html',
                                           filename=filename,
                                           apps=apps,
                                           **plot_arguments)

                #IF THE USER SELECTED THE COLUMNS TO BE PLOTTED FOR THE FIRST TIME OR IF THE USER CHANGES FROM KDE TO HIST OR FROM HIST TO KDE

                # if "kde" in request.form.keys():
                #     kde=True
                # else:
                #     kde=False

                # if "kde" in request.form.keys() and session["plot_arguments"]["kde"]=="on":
                #     kde=False
                # elif "kde" not in request.form.keys() and session["plot_arguments"]["kde"] in [".off","off"]:
                #     kde=False
                # else:
                #     kde=True

                # if "kde" in request.form.keys():
                #     print("route!!!", session["plot_arguments"]["kde"])
                # else:
                #     print("NO ROUTE", session["plot_arguments"]["kde"])

                # set_histogram_groups
                # set_kde_groups
                # if (session["plot_arguments"]["vals"]!=request.form.getlist("vals")):
                if ("kde" in request.form.keys()) and (
                        session["plot_arguments"]["groups_settings"]
                        == dict()):
                    # kde and have no groups settings
                    set_kde_groups = True
                    set_histogram_groups = False
                    session["plot_arguments"]["kde"] = "on"
                elif ("kde" not in request.form.keys()) and (
                        session["plot_arguments"]["groups_settings"]
                        == dict()):
                    # histogram and no groups settings
                    set_kde_groups = False
                    set_histogram_groups = True
                    session["plot_arguments"]["kde"] = "off"
                elif ("kde" in request.form.keys()) and (
                        session["plot_arguments"]["kde"] in [".off", "off"]):
                    # coming from histogram to kde
                    set_kde_groups = True
                    set_histogram_groups = False
                    session["plot_arguments"]["kde"] = "on"
                elif ("kde" not in request.form.keys()) and (
                        session["plot_arguments"]["kde"] in [".on", "on"]):
                    # coming from kde to histogram
                    set_kde_groups = False
                    set_histogram_groups = True
                    session["plot_arguments"]["kde"] = "off"
                else:
                    set_kde_groups = False
                    set_histogram_groups = False
                    session["plot_arguments"]["kde"] = "off"
                # else:

                #     set_kde_groups=False
                #     set_histogram_groups=True

                # print(set_kde_groups, set_histogram_groups)

                # if (session["plot_arguments"]["vals"]!=request.form.getlist("vals")):# or (session["plot_arguments"]["groups_settings"] == dict()):
                # # if session["plot_arguments"]["groups_settings"] == dict() or kde or session["plot_arguments"]["vals"]!=request.form.getlist("vals"):
                if set_histogram_groups:
                    plot_arguments = session["plot_arguments"]
                    plot_arguments["vals"] = request.form.getlist("vals")
                    groups = plot_arguments["vals"]
                    groups.sort()
                    groups_settings = dict()
                    for group in groups:
                        groups_settings[group]={"name":group,\
                            "values":df[group],\
                            "label":group,\
                            "color_value":"None",\
                            "color_rgb":"",\
                            "histnorm":"",\
                            "orientation_value":"vertical",\
                            "linewidth":0.5,\
                            "linestyle_value":"solid",\
                            "line_color":"lightgrey",\
                            "line_rgb":"",\
                            "opacity":"0.8",\
                            "text":"",\
                            "bins_number":"",\
                            "hoverinfo":"all",\
                            "hover_bgcolor":"None",\
                            "hover_bordercolor":"None",\
                            "hover_align":"auto",\
                            "hover_fontfamily":"Default",\
                            "hover_fontsize":"12",\
                            "hover_fontcolor":"None",\
                            "histfunc":"count",\
                            "density":".off",\
                            "cumulative_direction":"increasing",\
                            "cumulative":".off"}

                    #plot_arguments=read_request(request)
                    plot_arguments["groups_settings"] = groups_settings
                    session["plot_arguments"] = plot_arguments
                    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/ihistogram.html',
                                           figure_url=figure_url,
                                           filename=filename,
                                           apps=apps,
                                           **plot_arguments)

                elif set_kde_groups:
                    plot_arguments = session["plot_arguments"]
                    plot_arguments["vals"] = request.form.getlist("vals")
                    groups = plot_arguments["vals"]
                    groups.sort()
                    groups_settings = dict()
                    for group in groups:
                        groups_settings[group]={
                            "label":group,\
                            "color_value":"None",\
                            "color_rgb":"",\
                            }
                    plot_arguments = read_request(request)
                    plot_arguments["groups_settings"] = groups_settings
                    session["plot_arguments"] = plot_arguments
                    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/ihistogram.html',
                                           figure_url=figure_url,
                                           filename=filename,
                                           apps=apps,
                                           **plot_arguments)

            if "df" not in list(session.keys()):
                error_msg = "No data to plot, please upload a data or session  file."
                flash(error_msg, 'error')
                if not plot_arguments:
                    plot_arguments = session["plot_arguments"]
                return render_template('/apps/ihistogram.html',
                                       filename="Select file..",
                                       apps=apps,
                                       **plot_arguments)

            # MAKE SURE WE HAVE THE LATEST ARGUMENTS FOR THIS SESSION
            plot_arguments = read_request(request)

            # UPDATE VALUES FROM GROUPS_SETTINGS WHICH DO NOT GET UPDATED WITH THE READ_REQUEST FUNCTION
            #plot_arguments=session["plot_arguments"]
            groups = request.form.getlist("vals")
            groups_settings = dict()
            groups.sort()
            #IF THE USER WANTS TO PLOT A REGULAR HISTOGRAM
            if plot_arguments["kde"] != "on":
                #FOR COLUMNS ALREADY SELECTED BY USER
                for group in groups:
                    if group not in plot_arguments["groups_settings"].keys():
                        groups_settings[group]={"name":group,\
                            "label":request.form["%s.label" %group],\
                            "color_value":request.form["%s.color_value" %group],\
                            "color_rgb":request.form["%s.color_rgb" %group],\
                            "histnorm":request.form["%s.histnorm" %group],\
                            "orientation_value":request.form["%s.orientation_value" %group],\
                            "linewidth":request.form["%s.linewidth" %group],\
                            "linestyle_value":request.form["%s.linestyle_value" %group],\
                            "line_color":request.form["%s.line_color" %group],\
                            "line_rgb":request.form["%s.line_rgb" %group],\
                            "opacity":request.form["%s.opacity" %group],\
                            "text":request.form["%s.text" %group],\
                            "bins_number":request.form["%s.bins_number" %group],\
                            "hoverinfo":request.form["%s.hoverinfo" %group],\
                            "hover_bgcolor":request.form["%s.hover_bgcolor" %group],\
                            "hover_bordercolor":request.form["%s.hover_bordercolor" %group],\
                            "hover_align":request.form["%s.hover_align" %group],\
                            "hover_fontsize":request.form["%s.hover_fontsize" %group],\
                            "hover_fontcolor":request.form["%s.hover_fontcolor" %group],\
                            "hover_fontfamily":request.form["%s.hover_fontfamily" %group],\
                            "cumulative_direction":request.form["%s.cumulative_direction" %group],\
                            "histfunc":request.form["%s.histfunc" %group]
                            }

                        #If the user does not tick the options the arguments do not appear as keys in request.form
                        if "%s.density" % group in request.form.keys():
                            groups_settings[group]["density"] = request.form[
                                "%s.density" % group]
                        else:
                            groups_settings[group]["density"] = "off"

                        if "%s.cumulative" % group in request.form.keys():
                            groups_settings[group][
                                "cumulative"] = request.form["%s.cumulative" %
                                                             group]
                        else:
                            groups_settings[group]["cumulative"] = "off"

                    #NEW COLUMNS SELECTED BY USER
                    else:
                        groups_settings[group]={"name":group,\
                            "label":request.form["%s.label" %group],\
                            "color_value":request.form["%s.color_value" %group],\
                            "color_rgb":request.form["%s.color_rgb" %group],\
                            "histnorm":request.form["%s.histnorm" %group],\
                            "orientation_value":request.form["%s.orientation_value" %group],\
                            "linewidth":request.form["%s.linewidth" %group],\
                            "linestyle_value":request.form["%s.linestyle_value" %group],\
                            "line_color":request.form["%s.line_color" %group],\
                            "line_rgb":request.form["%s.line_rgb" %group],\
                            "opacity":request.form["%s.opacity" %group],\
                            "text":request.form["%s.text" %group],\
                            "bins_number":request.form["%s.bins_number" %group],\
                            "hoverinfo":request.form["%s.hoverinfo" %group],\
                            "hover_bgcolor":request.form["%s.hover_bgcolor" %group],\
                            "hover_bordercolor":request.form["%s.hover_bordercolor" %group],\
                            "hover_align":request.form["%s.hover_align" %group],\
                            "hover_fontsize":request.form["%s.hover_fontsize" %group],\
                            "hover_fontcolor":request.form["%s.hover_fontcolor" %group],\
                            "hover_fontfamily":request.form["%s.hover_fontfamily" %group],\
                            "cumulative_direction":request.form["%s.cumulative_direction" %group],\
                            "histfunc":request.form["%s.histfunc" %group]}

                        #If the user does not tick the options the arguments do not appear as keys in request.form
                        if "%s.density" % group in request.form.keys():
                            groups_settings[group]["density"] = request.form[
                                "%s.density" % group]
                        else:
                            groups_settings[group]["density"] = "off"

                        if "%s.cumulative" % group in request.form.keys():
                            groups_settings[group][
                                "cumulative"] = request.form["%s.cumulative" %
                                                             group]
                        else:
                            groups_settings[group]["cumulative"] = "off"

            #IF THE USER WANTS TO PLOT A KDE PLOT
            else:
                plot_arguments = session["plot_arguments"]
                plot_arguments["vals"] = request.form.getlist("vals")
                groups = plot_arguments["vals"]
                groups.sort()
                groups_settings = dict()
                for group in groups:
                    #FOR COLUMNS NEW COLUMNS SELECTED BY USER
                    if group not in plot_arguments["groups_settings"].keys():
                        for group in groups:
                            groups_settings[group]={"label":group,\
                                    "color_value":"None",\
                                    "color_rgb":""}

                    #FOR COLUMNS ALREADY SELECTED BY USER
                    else:
                        groups_settings[group]={"label":group,\
                            "color_value":request.form["%s.color_value" %group],\
                            "color_rgb":request.form["%s.color_rgb" %group]}

            plot_arguments["groups_settings"] = groups_settings
            session["plot_arguments"] = plot_arguments
            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/ihistogram.html',
                                   figure_url=figure_url,
                                   filename=filename,
                                   apps=apps,
                                   **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="ihistogram",
                                      session=session)
            flash(tb_str, 'traceback')
            return render_template('/apps/ihistogram.html',
                                   filename=session["filename"],
                                   apps=apps,
                                   **session["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
            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 ihistogram")
            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/ihistogram.html',
                               filename=session["filename"],
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #7
0
def iheatmap(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, "iheatmap", 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"] = "iheatmap"

    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "iheatmap")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/iheatmap.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"], "iheatmap")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/iheatmap.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/iheatmap.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/iheatmap.html',
                                           filename="Select file..",
                                           apps=apps,
                                           **plot_arguments)

            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/iheatmap.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"])

            if len(df) == 1:
                session["plot_arguments"]["row_cluster"] = "off"
                flash("You only have one row. Row dendrogram is now off.")
            if len(session["plot_arguments"]["yvals"]) == 1:
                session["plot_arguments"]["col_cluster"] = "off"
                flash(
                    "You only have one column. Columns dendrogram is now off.")

            # CALL FIGURE FUNCTION
            # try:
            fig, cols_cluster_numbers, index_cluster_numbers, df_ = make_figure(
                df, plot_arguments)

            figure_url = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)

            return render_template('/apps/iheatmap.html',
                                   figure_url=figure_url,
                                   filename=filename,
                                   apps=apps,
                                   **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="iheatmap",
                                      session=session)
            filename = session["filename"]
            flash(tb_str, 'traceback')
            if not plot_arguments:
                plot_arguments = session["plot_arguments"]
            return render_template('/apps/iheatmap.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, cols_cluster_numbers, index_cluster_numbers, df_ = 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"
            }
            fig.write_image(figfile,
                            format=plot_arguments["downloadf"],
                            height=float(plot_arguments["fig_height"]),
                            width=float(plot_arguments["fig_width"]))
            figfile.seek(0)  # rewind to beginning of file

            eventlog = UserLogging(email=current_user.email,
                                   action="download figure iheatmap")
            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 download == "clusters":

            plot_arguments = session["plot_arguments"]

            # READ INPUT DATA FROM SESSION JSON
            df = pd.read_json(session["df"])

            # CALL FIGURE FUNCTION

            fig, cols_cluster_numbers, index_cluster_numbers, df_ = make_figure(
                df, plot_arguments)

            excelfile = io.BytesIO()
            EXC = pd.ExcelWriter(excelfile)
            df_.to_excel(EXC, sheet_name="heatmap", index=None)
            if type(cols_cluster_numbers) != type(None):
                cols_cluster_numbers.to_excel(EXC,
                                              sheet_name="rows",
                                              index=None)
            if type(index_cluster_numbers) != type(None):
                index_cluster_numbers.to_excel(EXC,
                                               sheet_name="cols",
                                               index=None)
            EXC.save()
            excelfile.seek(0)

            eventlog = UserLogging(email=current_user.email,
                                   action="download figure heatmap clusters")
            db.session.add(eventlog)
            db.session.commit()

            return send_file(excelfile,
                             attachment_filename=plot_arguments["downloadn"] +
                             ".xlsx")

        return render_template('apps/iheatmap.html',
                               filename=session["filename"],
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #8
0
def venndiagram(download=None):

    apps = current_user.user_apps
    plot_arguments = None

    reset_info = check_session_app(session, "venndiagram", apps)

    if reset_info:
        flash(reset_info, 'error')
        plot_arguments = figure_defaults()
        session["plot_arguments"] = plot_arguments
        session["COMMIT"] = app.config['COMMIT']
        session["app"] = "venndiagram"

    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "venndiagram")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/venndiagram.html',
                                           apps=apps,
                                           **plot_arguments)
                flash(msg, "info")

            if request.files["inputargumentsfile"]:
                msg, plot_arguments, error = read_argument_file(
                    request.files["inputargumentsfile"], "venndiagram")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/venndiagram.html',
                                           apps=apps,
                                           **plot_arguments)
                flash(msg, "info")

            if not request.files["inputsessionfile"] and not request.files[
                    "inputargumentsfile"]:
                plot_arguments = read_request(request)

                i = 0
                for set_index in ["set1", "set2", "set3"]:
                    if plot_arguments["%s_values" % set_index] != "":
                        i = i + 1

                if i < 2:
                    error_msg = "No data to plot, please upload data."
                    flash(error_msg, 'error')
                    return render_template('/apps/venndiagram.html',
                                           apps=apps,
                                           **plot_arguments)

            if ("." in str(session["plot_arguments"]["population_size"])) | (
                    "," in str(session["plot_arguments"]["population_size"])):
                error_msg = "Population size needs to be an integer."
                flash(error_msg, 'error')
                return render_template('/apps/venndiagram.html',
                                       apps=apps,
                                       **plot_arguments)

            # make sure we have the latest given arguments
            plot_arguments = session["plot_arguments"]

            # CALL FIGURE FUNCTION
            fig, df, pvalues = make_figure(plot_arguments)

            # TRANSFORM FIGURE TO BYTES AND BASE64 STRING
            figfile = io.BytesIO()
            plt.savefig(figfile, format='png')
            plt.close()
            figfile.seek(0)  # rewind to beginning of file
            figure_url = base64.b64encode(figfile.getvalue()).decode('utf-8')

            if pvalues:
                message = "Hypergeometric test:<br><br>"
                for pvalue in list(pvalues.keys()):
                    samples_keys = list(pvalues[pvalue].keys())
                    message=message+str(pvalue)+":<br>"+\
                        "- %s: "%samples_keys[0]  + str(pvalues[pvalue][samples_keys[0]])+"<br>"+\
                        "- %s: "%samples_keys[1] + str(pvalues[pvalue][samples_keys[1]])+"<br>"+\
                        "- n common: " + str(pvalues[pvalue]["common"])+"<br>"+\
                        "- n total: " + str(int(pvalues[pvalue]["total"]))+"<br>"+\
                        "- p value: " + str(pvalues[pvalue]["p value"])+"<br><br>"

                flash(message)

            return render_template('/apps/venndiagram.html',
                                   figure_url=figure_url,
                                   apps=apps,
                                   **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="venndiagram",
                                      session=session)
            flash(tb_str, 'traceback')
            if not plot_arguments:
                plot_arguments = session["plot_arguments"]
            return render_template('/apps/venndiagram.html',
                                   apps=apps,
                                   **session["plot_arguments"])

    else:
        if download == "download":

            plot_arguments = session["plot_arguments"]

            # CALL FIGURE FUNCTION
            fig, df, pvalues = make_figure(plot_arguments)

            figfile = io.BytesIO()
            mimetypes = {
                "png": 'image/png',
                "pdf": "application/pdf",
                "svg": "image/svg+xml"
            }
            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 venndiagram")
            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 download == "data":

            plot_arguments = session["plot_arguments"]
            fig, df, pvalues = make_figure(plot_arguments)

            if pvalues:
                message = pd.DataFrame()
                for pvalue in pvalues:
                    tmp = pd.DataFrame(pvalues[pvalue], index=[pvalue])
                    tmp.columns = [
                        "n group 1", "n group 2", "n common", "n total",
                        "p value"
                    ]
                    message = pd.concat([message, tmp])

            excelfile = io.BytesIO()
            EXC = pd.ExcelWriter(excelfile)
            df.to_excel(EXC, sheet_name="venndiagram", index=None)
            if pvalues:
                message.to_excel(EXC, sheet_name="hyperg.test", index=True)
            EXC.save()
            excelfile.seek(0)

            eventlog = UserLogging(email=current_user.email,
                                   action="download figure venndiagram data")
            db.session.add(eventlog)
            db.session.commit()

            return send_file(excelfile,
                             attachment_filename=plot_arguments["downloadn"] +
                             ".xlsx")

        return render_template('/apps/venndiagram.html',
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #9
0
def iviolinplot(download=None):
    
    apps=current_user.user_apps
    reset_info=check_session_app(session,"iviolinplot",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"]="iviolinplot"
           
    if request.method == 'POST' :
        try:
            if request.files["inputsessionfile"] :
                msg, plot_arguments, error=read_session_file(request.files["inputsessionfile"],"iviolinplot")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/iviolinplot.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"],"iviolinplot")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/iviolinplot.html' , filename=session["filename"], apps=apps, **plot_arguments)
                flash(msg,"info")
            
            # IF THE USER UPLOADS A NEW FILE
            # THEN 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()
                    vals=[None]+cols
                                    
                    # sometext="Please select at least one numeric column to create your violin plot."
                    session["plot_arguments"]["cols"]=cols
                    session["plot_arguments"]["vals"]=vals
                    plot_arguments=session["plot_arguments"]
                    # plot_arguments=read_request(request)

                    # flash(sometext,'info')
                    return render_template('/apps/iviolinplot.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/iviolinplot.html' , filename=session["filename"], apps=apps, **plot_arguments)
            
            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/iviolinplot.html' , filename="Select file..", apps=apps,  **plot_arguments)

            # if not request.files["inputsessionfile"] and not request.files["inputargumentsfile"] :
            
            # USER INPUT/PLOT_ARGUMENTS GETS UPDATED TO THE LATEST INPUT
            plot_arguments=read_request(request)
            # vals=request.form.getlist("vals")
            vals=plot_arguments["vals"]
            df=pd.read_json(session["df"])
            filename=session["filename"]


            #IN CASE THE USER HAS NOT SELECTED X_VAL or Y_VAL
            if  plot_arguments["x_val"] == "None" or plot_arguments["y_val"]=="None":
                sometext="Please select a valid value to plot in your X and Y axes"
                plot_arguments=session["plot_arguments"]
                plot_arguments["vals"]=vals
                flash(sometext,'info')
                return render_template('/apps/iviolinplot.html' , filename=filename, apps=apps,**plot_arguments)
                            
            session["plot_arguments"]=plot_arguments
            #CALL FIGURE FUNCTION
            fig=make_figure(df,plot_arguments)
            figure_url = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
            return render_template('/apps/iviolinplot.html', figure_url=figure_url, filename=filename, apps=apps, **plot_arguments)

        except Exception as e:
            tb_str=handle_exception(e,user=current_user,eapp="iviolinplot",session=session)
            flash(tb_str,'traceback')
            return render_template('/apps/iviolinplot.html', filename=session["filename"], apps=apps, **session["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)

            figfile = io.BytesIO()
            mimetypes={"png":'image/png',"pdf":"application/pdf","svg":"image/svg+xml"}
            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 iviolinplot")
            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/iviolinplot.html',  filename=session["filename"], apps=apps, **session["plot_arguments"])
Beispiel #10
0
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"])
Beispiel #11
0
def david(download=None):

    apps = current_user.user_apps

    reset_info = check_session_app(session, "david", 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"] = "david"

    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "david")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/david.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"], "david")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/david.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 plot_arguments["user"] == "":
                flash(
                    'Please give in a register DAVID email in "Input" > "DAVID registered email". If you do not yet have a registered address you need to register with DAVID - https://david.ncifcrf.gov/webservice/register.htm. Please be aware that you will not receive any confirmation email. ',
                    'error')
                return render_template('/apps/david.html',
                                       apps=apps,
                                       **plot_arguments)

            # CALL FIGURE FUNCTION
            david_df, report_stats, msg = run_david(plot_arguments)
            if msg:
                flash(msg, "error")
                return render_template('/apps/david.html',
                                       apps=apps,
                                       **plot_arguments)

            # if (not david_df) and (not report_stats):
            #     flash("DAVID doesn't seem to recognize your email account. Make sure you have registered your account on https://david.ncifcrf.gov/webservice/register.htm. If you have just registered please be aware that it might take some hours for you account to be in their systems. Please try again later.",'error')
            #     return render_template('/apps/david.html', apps=apps, **plot_arguments)

            ## get this into json like in former apps
            david_df = david_df.astype(str)
            report_stats = report_stats.astype(str)
            # mapped=mapped.astype(str)

            session["david_df"] = david_df.to_json()
            session["report_stats"] = report_stats.to_json()
            # session["mapped"]=mapped.to_json()

            table_headers = david_df.columns.tolist()
            tmp = david_df[:50]
            for col in ["%", "Fold Enrichment"]:
                tmp[col] = tmp[col].apply(lambda x: "{0:.2f}".format(float(x)))
            for col in ["PValue", "Bonferroni", "Benjamini", "FDR"]:
                tmp[col] = tmp[col].apply(lambda x: '{:.2e}'.format(float(x)))
            for col in ["Genes"] + table_headers[13:]:
                tmp[col] = tmp[col].apply(lambda x: str(x)[:40] + "..")
            david_contents = []
            for i in tmp.index.tolist():
                david_contents.append(list(tmp.loc[i, ]))

            # david_in_store
            if len(david_df) > 0:
                session["david_in_store"] = True
                return render_template('/apps/david.html',
                                       david_in_store=True,
                                       apps=apps,
                                       table_headers=table_headers,
                                       david_contents=david_contents,
                                       **plot_arguments)

            elif "david_in_store" in list(session.keys()):
                del (session["david_in_store"])
                return render_template('/apps/david.html',
                                       apps=apps,
                                       table_headers=table_headers,
                                       david_contents=david_contents,
                                       **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="david",
                                      session=session)
            flash(tb_str, 'traceback')
            if "david_in_store" in list(session.keys()):
                del (session["david_in_store"])
            return render_template('/apps/david.html',
                                   apps=apps,
                                   **plot_arguments)

    else:

        if download == "download":

            plot_arguments = session["plot_arguments"]

            # READ INPUT DATA FROM SESSION JSON
            try:
                david_df = pd.read_json(session["david_df"])
                report_stats = pd.read_json(session["report_stats"])
            except:
                flash("No DAVID table in memory.", 'error')
                return render_template('/apps/david.html',
                                       apps=apps,
                                       **plot_arguments)
            # mapped=pd.read_json(session["mapped"])
            # mapped=mapped.replace("nan",np.nan)

            eventlog = UserLogging(email=current_user.email,
                                   action="download david")
            db.session.add(eventlog)
            db.session.commit()

            if plot_arguments["download_format_value"] == "xlsx":

                def getGOID(x):
                    if "GO:" in x:
                        r = x.split("~")[0]
                    else:
                        r = np.nan
                    return r

                revigo = david_df[["Term", "PValue"]]
                revigo["Term"] = revigo["Term"].apply(lambda x: getGOID(x))
                revigo = revigo.dropna()

                outfile = io.BytesIO()
                EXC = pd.ExcelWriter(outfile)
                david_df.to_excel(EXC, sheet_name="david", index=None)
                report_stats.to_excel(EXC, sheet_name="stats", index=None)
                revigo.to_excel(EXC, sheet_name="revigo", index=None)
                # mapped.to_excel(EXC,sheet_name="mapped",index=None)
                EXC.save()
                outfile.seek(0)
                return send_file(
                    outfile,
                    attachment_filename=plot_arguments["download_name"] + "." +
                    plot_arguments["download_format_value"],
                    as_attachment=True)

            elif plot_arguments["download_format_value"] == "tsv":
                return Response(david_df.to_csv(sep="\t"),
                                mimetype="text/csv",
                                headers={
                                    "Content-disposition":
                                    "attachment; filename=%s.tsv" %
                                    plot_arguments["download_name"]
                                })
                #outfile.seek(0)

        if download == "cellplot":
            # READ INPUT DATA FROM SESSION JSON
            david_df = pd.read_json(session["david_df"])
            david_df = david_df.astype(str)

            reset_info = check_session_app(session, "icellplot", apps)
            if reset_info:
                flash(reset_info, 'error')

            # INITIATE SESSION
            session["filename"] = "<from DAVID>"
            session["ge_filename"] = "Select file.."

            plot_arguments = icellplot.figure_defaults()

            session["plot_arguments"] = plot_arguments
            session["COMMIT"] = app.config['COMMIT']
            session["app"] = "icellplot"

            david_df = david_df.astype(str)
            session["df"] = david_df.to_json()

            cols = david_df.columns.tolist()
            session["plot_arguments"]["david_cols"] = ["select a column.."
                                                       ] + cols
            session["plot_arguments"]["annotation_column"] = ["none"] + cols
            session["plot_arguments"]["categories_to_plot"] = list(
                set(david_df["Category"].tolist()))
            session["plot_arguments"]["categories_to_plot_value"] = list(
                set(david_df["Category"].tolist()))

            return render_template('/apps/icellplot.html',
                                   filename=session["filename"],
                                   ge_filename=session["ge_filename"],
                                   apps=apps,
                                   **plot_arguments)

        return render_template('apps/david.html',
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #12
0
def icellplot(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, "icellplot", apps)
    if reset_info:
        flash(reset_info, 'error')
        # INITIATE SESSION
        session["filename"] = "Select file.."
        session["ge_filename"] = "Select file.."

        plot_arguments = figure_defaults()

        session["plot_arguments"] = plot_arguments
        session["COMMIT"] = app.config['COMMIT']
        session["app"] = "icellplot"

    if request.method == 'POST':

        try:
            if request.files["inputsessionfile"]:
                msg, plot_arguments, error = read_session_file(
                    request.files["inputsessionfile"], "icellplot")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/icellplot.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"], "icellplot")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/icellplot.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)

            missing_args = False
            # 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()
                    session["plot_arguments"]["david_cols"] = [
                        "select a column.."
                    ] + cols
                    session["plot_arguments"]["annotation_column"] = ["none"
                                                                      ] + cols

                    # IF THE USER HAS NOT YET CHOSEN X AND Y VALUES THAN PLEASE SELECT
                    if (session["plot_arguments"]["terms_column"] not in cols
                        ) | (session["plot_arguments"]["terms_column"]
                             == "select a column.."):
                        session["plot_arguments"][
                            "terms_column"] = "select a column.."
                        missing_args = True

                    if (session["plot_arguments"]["categories_column"]
                            not in cols) | (
                                session["plot_arguments"]["categories_column"]
                                == "select a column.."):
                        session["plot_arguments"][
                            "categories_column"] = "select a column.."
                        missing_args = True
                    else:
                        session["plot_arguments"]["categories_to_plot"] = list(
                            set(df[session["plot_arguments"]
                                   ["categories_column"]].tolist()))
                        session["plot_arguments"][
                            "categories_to_plot_value"] = list(
                                set(df[session["plot_arguments"]
                                       ["categories_column"]].tolist()))

                    if (session["plot_arguments"]["david_gene_ids"] not in cols
                        ) | (session["plot_arguments"]["david_gene_ids"]
                             == "select a column.."):
                        session["plot_arguments"][
                            "david_gene_ids"] = "select a column.."
                        missing_args = True

                    if (session["plot_arguments"]["plotvalue"] not in cols) | (
                            session["plot_arguments"]["plotvalue"]
                            == "select a column.."):
                        session["plot_arguments"][
                            "plotvalue"] = "select a column.."
                        missing_args = True

                    if missing_args:
                        sometext = "Please select matching columns from DAVID file."
                        flash(sometext, 'info')

                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 DAVID's output again." % filename
                    flash(error_msg, 'error')
                    session["filename"] = "Select file.."

            annotation_columns = (
                session["plot_arguments"]["annotation_column_value"] != "none"
            ) & (session["plot_arguments"]["annotation2_column_value"] !=
                 "none")

            if (not annotation_columns):
                ge_inputfile = request.files["ge_inputfile"]
                if ge_inputfile:
                    filename = secure_filename(ge_inputfile.filename)
                    if allowed_file(ge_inputfile.filename):
                        df = read_tables(ge_inputfile,
                                         session_key="ge_df",
                                         file_field="ge_filename")

                        cols = df.columns.tolist()
                        session["plot_arguments"]["ge_cols"] = [
                            "select a column.."
                        ] + cols

                        # IF THE USER HAS NOT YET CHOSEN X AND Y VALUES THAN PLEASE SELECT
                        if (session["plot_arguments"]["gene_identifier"]
                                not in cols
                            ) | (session["plot_arguments"]["gene_identifier"]
                                 == "select a column.."):
                            session["plot_arguments"][
                                "gene_identifier"] = "select a column.."
                            missing_args = True
                        if (session["plot_arguments"]["expression_values"]
                                not in cols
                            ) | (session["plot_arguments"]["expression_values"]
                                 == "select a column.."):
                            session["plot_arguments"][
                                "expression_values"] = "select a column.."
                            missing_args = True
                        if (session["plot_arguments"]["gene_name"] not in cols
                            ) | (session["plot_arguments"]["gene_name"]
                                 == "select a column.."):
                            session["plot_arguments"][
                                "gene_name"] = "select a column.."
                            missing_args = True

                        if missing_args:
                            sometext = "Please select matching columns in gene expression file for mapping."
                            flash(sometext, 'info')

                    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 the gene expression file again." % filename
                        flash(error_message, 'error')
                        session["ge_filename"] = "Select file.."

            if ((session["ge_filename"] == "Select file..") &
                (not annotation_columns)) | (session["filename"]
                                             == "Select file.."):
                plot_arguments = session["plot_arguments"]
                return render_template('/apps/icellplot.html',
                                       filename=session["filename"],
                                       ge_filename=session["ge_filename"],
                                       apps=apps,
                                       **plot_arguments)

            if missing_args:
                plot_arguments = session["plot_arguments"]
                return render_template('/apps/icellplot.html',
                                       filename=session["filename"],
                                       ge_filename=session["ge_filename"],
                                       apps=apps,
                                       **plot_arguments)

            annotation_columns = (
                session["plot_arguments"]["annotation_column_value"] != "none"
            ) & (session["plot_arguments"]["annotation2_column_value"] !=
                 "none")

            if ("df" not in list(
                    session.keys())) | (("ge_df" not in list(session.keys())) &
                                        (not annotation_columns)):
                error_message = "No data to plot, please upload a data or session  file."
                flash(error_msg, 'error')
                return render_template('/apps/icellplot.html',
                                       filename=session["filename"],
                                       ge_filename=session["ge_filename"],
                                       apps=apps,
                                       **plot_arguments)

            # MAKE SURE WE HAVE THE LATEST ARGUMENTS FOR THIS SESSION
            plot_arguments = session["plot_arguments"]

            # READ INPUT DATA FROM SESSION JSON
            df = pd.read_json(session["df"])
            if (annotation_columns) & ("ge_df" not in list(session.keys())):
                ge_df = pd.DataFrame()
                session["ge_df"] = ge_df.to_json()
            ge_df = pd.read_json(session["ge_df"])

            # CALL FIGURE FUNCTION
            # try:
            fig = make_figure(df, ge_df, plot_arguments)

            figure_url = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)

            return render_template('/apps/icellplot.html',
                                   figure_url=figure_url,
                                   filename=session["filename"],
                                   ge_filename=session["ge_filename"],
                                   apps=apps,
                                   **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="icellplot",
                                      session=session)
            flash(tb_str, 'traceback')
            if not plot_arguments:
                plot_arguments = session["plot_arguments"]
            return render_template('/apps/icellplot.html',
                                   filename=session["filename"],
                                   ge_filename=session["ge_filename"],
                                   apps=apps,
                                   **plot_arguments)

    else:
        if download == "download":
            # READ INPUT DATA FROM SESSION JSON
            df = pd.read_json(session["df"])
            ge_df = pd.read_json(session["ge_df"])

            plot_arguments = session["plot_arguments"]

            # CALL FIGURE FUNCTION
            fig = make_figure(df, ge_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 ["height", "width"]:
                if plot_arguments[v] != "":
                    pa_[v] = None
                elif plot_arguments[v]:
                    pa_[v] = float(plot_arguments[v])
                else:
                    pa_[v] = None

            if pa_["height"]:
                if pa_["width"]:
                    fig.write_image(figfile,
                                    format=plot_arguments["downloadf"],
                                    height=pa_["height"],
                                    width=pa_["width"])
                else:
                    fig.write_image(figfile,
                                    format=plot_arguments["downloadf"])
            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 icellplot")
            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/icellplot.html',
                               filename=session["filename"],
                               ge_filename=session["ge_filename"],
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #13
0
def igseaplot(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, "igseaplot", 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"] = "igseaplot"

    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"], "igseaplot")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/igseaplot.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"], "igseaplot")
                if error:
                    flash(msg, 'error')
                    return render_template('/apps/igseaplot.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/igseaplot.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","gseacolor_cols","gsea_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/igseaplot.html',
                                               filename=filename,
                                               apps=apps,
                                               **plot_arguments)

                    plot_arguments = session["plot_arguments"]
                    flash("New file uploaded.", 'info')
                    return render_template('/apps/igseaplot.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/igseaplot.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"],\
                                "gsea_linewidth":plot_arguments["gsea_linewidth"],\
                                "gsea_linewidth_col":"select a column..",\
                                "gseacolor":plot_arguments["gseacolor"],\
                                "gseacolor_col":"select a column..",\
                                "gseacolor_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],\
                            "gsea_linewidth":request.form["%s.gsea_linewidth" %group],\
                            "gsea_linewidth_col":request.form["%s.gsea_linewidth_col" %group],\
                            "gseacolor":request.form["%s.gseacolor" %group],\
                            "gseacolor_col":request.form["%s.gseacolor_col" %group],\
                            "gseacolor_write":request.form["%s.gseacolor_write" %group],\
                            "centerline":request.form["%s.centerline" %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/igseaplot.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/igseaplot.html',
                                   figure_url=figure_url,
                                   filename=filename,
                                   apps=apps,
                                   **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="igseaplot",
                                      session=session)
            flash(tb_str, 'traceback')
            if not plot_arguments:
                plot_arguments = session["plot_arguments"]
            filename = session["filename"]
            return render_template('/apps/igseaplot.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 igseaplot")
            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/igseaplot.html',
                               filename=session["filename"],
                               apps=apps,
                               **session["plot_arguments"])
Beispiel #14
0
def kegg(download=None):

    apps=current_user.user_apps

    # pa={}
    # pa["ids"]="HMDB00001\tred\nHMDB0004935\tred"
    # pa["species"]="mmu"

    # make_figure(pa)

    # return render_template('/index.html' ,apps=apps)

    reset_info=check_session_app(session,"kegg",apps)
    if reset_info:
        flash(reset_info,'error')
        # INITIATE SESSION
        plot_arguments=figure_defaults()

        print(plot_arguments)

        session["plot_arguments"]=plot_arguments
        session["COMMIT"]=app.config['COMMIT']
        session["app"]="kegg"
  
    if request.method == 'POST' :

        try:
            if request.files["inputsessionfile"] :
                msg, plot_arguments, error=read_session_file(request.files["inputsessionfile"],"kegg")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/kegg.html' ,apps=apps, **plot_arguments)
                flash(msg,"info")

            if request.files["inputargumentsfile"] :
                msg, plot_arguments, error=read_argument_file(request.files["inputargumentsfile"],"kegg")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/kegg.html' , apps=apps, **plot_arguments)
                flash(msg,"info")

            if not request.files["inputsessionfile"] and not request.files["inputargumentsfile"] :
                plot_arguments=read_request(request)

            # CALL FIGURE FUNCTION
            df, msg=make_figure(plot_arguments)

            ## get this into json like in former apps
            df=df.astype(str)
            # mapped=mapped.astype(str)

            session["df"]=df.to_json()
            # session["mapped"]=mapped.to_json()

            # print(df.head())

            tmp=df[:50]
            table_headers=tmp.columns.tolist()

            def break_text(s,w=20):
               r=[s[i:i + w] for i in range(0, len(s), w)]
               r="".join(r)
               return r

            for c in ["ref_link","species_link"]:
               tmp[c]=tmp[c].apply(lambda x: break_text(x) )

            kegg_contents=[]
            for i in tmp.index.tolist():
                kegg_contents.append(list(tmp.loc[i,]))

            #print(kegg_contents[:2])
            # david_in_store
            if len(df)>0:
                session["kegg_in_store"]=True
                return render_template('/apps/kegg.html', kegg_in_store=True, apps=apps, table_headers=table_headers, kegg_contents=kegg_contents, **plot_arguments)

            elif "kegg_in_store" in list(session.keys()):
                del(session["kegg_in_store"])
                return render_template('/apps/kegg.html', apps=apps, table_headers=table_headers, kegg_contents=kegg_contents, **plot_arguments)

        except Exception as e:
            tb_str=handle_exception(e,user=current_user,eapp="kegg",session=session)
            flash(tb_str,'traceback')
            if "kegg_in_store" in list(session.keys()):
                del(session["kegg_in_store"])
            return render_template('/apps/kegg.html', apps=apps, **plot_arguments)

    else:

        if download == "download":

            plot_arguments=session["plot_arguments"]

            # READ INPUT DATA FROM SESSION JSON
            df=pd.read_json(session["df"])
            
            # mapped=pd.read_json(session["mapped"])
            # mapped=mapped.replace("nan",np.nan)

            eventlog = UserLogging(email=current_user.email,action="download kegg")
            db.session.add(eventlog)
            db.session.commit()

            if plot_arguments["download_format_value"] == "xlsx":
               
                outfile = io.BytesIO()
                EXC=pd.ExcelWriter(outfile)
                df.to_excel(EXC,sheet_name="kegg",index=None)
                # mapped.to_excel(EXC,sheet_name="mapped",index=None)
                EXC.save()
                outfile.seek(0)
                return send_file(outfile, attachment_filename=plot_arguments["download_name"]+ "." + plot_arguments["download_format_value"],as_attachment=True )

            elif plot_arguments["download_format_value"] == "tsv":  
                return Response(df.to_csv(sep="\t"), mimetype="text/csv", headers={"Content-disposition": "attachment; filename=%s.tsv" %plot_arguments["download_name"]})
                #outfile.seek(0)
        
        return render_template('apps/kegg.html',  apps=apps, **session["plot_arguments"])
Beispiel #15
0
def submissions():

    apps = current_user.user_apps

    reset_info = check_session_app(session, "submissions", apps)
    if reset_info:
        flash(reset_info, 'error')

        # INITIATE SESSION
        session["filename"] = "Select file.."

        # submission_arguments=submission_defaults()

        # session["plot_arguments"]=submission_arguments
        session["COMMIT"] = app.config['COMMIT']
        session["app"] = "submissions"

    if request.method == 'POST':

        try:
            # if request.files["inputsessionfile"] :
            #     msg, plot_arguments, error=read_session_file(request.files["inputsessionfile"],"submissions")
            #     if error:
            #         flash(msg,'error')
            #         return render_template('/apps/submissions.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"],"david")
            #     if error:
            #         flash(msg,'error')
            #         return render_template('/apps/submissions.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)

            inputfile = request.files["inputfile"]
            if inputfile:
                filename = secure_filename(inputfile.filename)
                if not allowed_file(inputfile.filename):
                    msg = "This file is not allowed."
                    flash(msg, "error")
                    return render_template('/apps/submissions.html', apps=apps)

            # CALL FIGURE FUNCTION
            status, msg, attachment_path = submission_check(inputfile)
            if not status:
                flash(msg, "error")
                return render_template('/apps/submissions.html',
                                       apps=apps)  #, **plot_arguments)

            if status:
                flash(msg)
                send_submission_email(user=current_user,
                                      submission_type=status,
                                      submission_file=inputfile,
                                      attachment_path=attachment_path)
                return render_template('/apps/submissions.html',
                                       apps=apps)  #, **plot_arguments)

            # return render_template('/apps/submissions.html', apps=apps, **plot_arguments)

        except Exception as e:
            tb_str = handle_exception(e,
                                      user=current_user,
                                      eapp="submissions",
                                      session=session)
            flash(tb_str, 'traceback')
            return render_template('/apps/submissions.html',
                                   apps=apps)  #, **plot_arguments)

    else:
        initial_message = Markup(
            'For submitting samples please follow the instructions in  <a href="https://github.com/mpg-age-bioinformatics/submissions/blob/main/README.md#submissions" class="alert-link">here</a>.'
        )
        flash(initial_message)
        return render_template('apps/submissions.html',
                               apps=apps)  #, **session["plot_arguments"])
Beispiel #16
0
def scatterplot(download=None):
    
    apps=current_user.user_apps
    reset_info=check_session_app(session,"scatterplot",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"]="scatterplot"
           
    if request.method == 'POST' :
        try:
            if request.files["inputsessionfile"] :
                msg, plot_arguments, error=read_session_file(request.files["inputsessionfile"],"scatterplot")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/scatterplot.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"],"scatterplot")
                if error:
                    flash(msg,'error')
                    return render_template('/apps/scatterplot.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/scatterplot.html' , filename=session["filename"], apps=apps, **plot_arguments)

                    session["filename"]=filename

                    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):

                        if session["plot_arguments"]["xvals"] not in cols : 
                            session["plot_arguments"]["xvals"]=cols[0]

                        if session["plot_arguments"]["yvals"] not in 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/scatterplot.html' , filename=filename, apps=apps,**plot_arguments)
                    
                    plot_arguments=session["plot_arguments"]
                    flash("New file uploaded.",'info')
                    return render_template('/apps/scatterplot.html' , filename=filename, apps=apps,**plot_arguments)
                    
                else:
                    plot_arguments=session["plot_arguments"]
                    # 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/scatterplot.html' , filename=session["filename"], apps=apps, **plot_arguments)
            
            if not request.files["inputsessionfile"] and not request.files["inputargumentsfile"] :
                # 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=[]
                        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)
                            # for k in list( group_dic[group].keys() ):
                            #     plot_arguments[k+"_"+group]=group_dic[group][k]
                        plot_arguments["groups_settings"]=groups_settings

                    else:
                        groups_settings=[]

                elif plot_arguments["groups_value"] != "None":
                    groups_settings=[]
                    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
                
                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/scatterplot.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"])

            fig=make_figure(df,plot_arguments)

            #TRANSFORM FIGURE TO BYTES AND BASE64 STRING
            figfile = io.BytesIO()
            plt.savefig(figfile, format='png')
            plt.close()
            figfile.seek(0)  # rewind to beginning of file
            figure_url = base64.b64encode(figfile.getvalue()).decode('utf-8')

            return render_template('/apps/scatterplot.html', figure_url=figure_url, filename=filename, apps=apps, **plot_arguments)

        except Exception as e:
            tb_str=handle_exception(e,user=current_user,eapp="scatterplot",session=session)
            flash(tb_str,'traceback')
            return render_template('/apps/scatterplot.html', filename=session["filename"], apps=apps, **session["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)

            figfile = io.BytesIO()
            mimetypes={"png":'image/png',"pdf":"application/pdf","svg":"image/svg+xml"}
            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 scatterplot")
            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/scatterplot.html',  filename=session["filename"], apps=apps, **session["plot_arguments"])