Example #1
0
def pathwayAcquisitionSaveImage(request, response):
    jobID = ""
    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        # logging.info("STEP0 - CHECK IF VALID USER....")
        # userID  = request.cookies.get('userID')
        # sessionToken  = request.cookies.get('sessionToken')
        # UserSessionManager().isValidUser(userID, sessionToken)

        jobID = request.form.get("jobID")
        jobInstance = JobInformationManager().loadJobInstance(jobID)

        svgData = request.form.get("svgCode")
        fileName = "paintomics_" + request.form.get("fileName").replace(
            " ", "_") + "_" + jobID
        fileFormat = request.form.get("format")

        userID = jobInstance.getUserID()
        userDirID = userID if userID is not None else "nologin"
        path = CLIENT_TMP_DIR + userDirID + jobInstance.getOutputDir().replace(
            CLIENT_TMP_DIR + userDirID, "")

        if (fileFormat == "png"):

            def createImage(svgData):
                svg = rsvg.Handle(data=svgData)
                width = svg.props.width
                height = svg.props.height
                surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width,
                                             height)
                context = cairo.Context(surface)
                open(path + fileName + "." + fileFormat, 'a').close()
                svg.render_cairo(context)
                surface.write_to_png(path + fileName + "." + fileFormat)

            try:
                logging.info("TRYING...")
                createImage(svgData=svgData)
            except Exception as ex:
                logging.info("TRYING again...")
                createImage(svgData=svgData)

        elif (fileFormat == "svg"):
            file_ = open(path + fileName + "." + fileFormat, 'w')
            file_.write(svgData)
            file_.close()

        path = "/get_cluster_image/" + jobID + "/output/"

        response.setContent({
            "success": True,
            "filepath": path + fileName + "." + fileFormat
        })
    except Exception as ex:
        handleException(response, ex, __file__, "pathwayAcquisitionSaveImage")
    finally:
        return response
Example #2
0
def pathwayAcquisitionMetagenes_PART1(REQUEST, RESPONSE, QUEUE_INSTANCE,
                                      JOB_ID, ROOT_DIRECTORY):
    # ****************************************************************
    # Step 0. VARIABLE DECLARATION
    # The following variables are defined:
    #  - jobInstance: instance of the PathwayAcquisitionJob class.
    #                 Contains all the information for the current job.
    #  - userID: the ID for the user
    # ****************************************************************
    jobInstance = None
    userID = None

    try:
        # ****************************************************************
        # Step 1. CHECK IF VALID USER SESSION
        # ****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = REQUEST.cookies.get('userID')
        sessionToken = REQUEST.cookies.get('sessionToken')
        UserSessionManager().isValidUser(userID, sessionToken)

        # ****************************************************************
        # Step 2. LOAD THE JOB INSTANCE AND RETRIEVE FORM INFO
        # ****************************************************************
        savedJobID = REQUEST.form.get("jobID")
        savedJobInstance = JobInformationManager().loadJobInstance(savedJobID)

        if savedJobInstance.getReadOnly() and str(
                savedJobInstance.getUserID()) != str(userID):
            raise Exception("Invalid user for the job generating metagenes.")

        omicName = REQUEST.form.get("omic")
        clusterNumber = int(REQUEST.form.get("number"))

        # Make sure the number of clusters is inside [1, 20]
        clusterNumber = 1 if clusterNumber < 1 else 20 if clusterNumber > 20 else clusterNumber

        # ************************************************************************
        # Step 4. Queue job
        # ************************************************************************
        QUEUE_INSTANCE.enqueue(fn=pathwayAcquisitionMetagenes_PART2,
                               args=(ROOT_DIRECTORY, userID, savedJobInstance,
                                     omicName, clusterNumber, RESPONSE),
                               timeout=600,
                               job_id=JOB_ID)

        # ************************************************************************
        # Step 5. Return the Job ID
        # ************************************************************************
        RESPONSE.setContent({"success": True, "jobID": JOB_ID})
    except Exception as ex:
        handleException(RESPONSE,
                        ex,
                        __file__,
                        "pathwayAcquisitionMetagenes_PART1",
                        userID=userID)
    finally:
        return RESPONSE
Example #3
0
        def get_cluster_image(filename):
            jobID = filename.split('/')[0]
            jobInstance = JobInformationManager().loadJobInstance(jobID)

            # Check if the file really exist, if not, then we are probably accessing a public job from a logged
            # account.
            userDir = "nologin" if jobInstance.getUserID(
            ) is None else jobInstance.getUserID()
            image_path = CLIENT_TMP_DIR + userDir + "/jobsData/"

            return send_from_directory(image_path, filename)
Example #4
0
def pathwayAcquisitionSaveVisualOptions(request, response):
    #VARIABLE DECLARATION
    visualOptionsInstance = None
    jobID = ""
    userID = ""

    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = request.cookies.get('userID')
        sessionToken = request.cookies.get('sessionToken')
        UserSessionManager().isValidUser(userID, sessionToken)

        #****************************************************************
        # Step 1.GET THE INSTANCE OF visual Options
        #****************************************************************
        visualOptions = request.get_json()
        jobID = visualOptions.get("jobID")

        jobInstance = JobInformationManager().loadJobInstance(jobID)

        if jobInstance.getReadOnly() and str(
                jobInstance.getUserID()) != str(userID):
            raise Exception("Invalid user for the job saving visual options")

        newTimestamp = int(time())
        visualOptions["timestamp"] = newTimestamp

        #************************************************************************
        # Step 3. Save the visual Options in the MongoDB
        #************************************************************************
        logging.info("STEP 3 - SAVING VISUAL OPTIONS FOR JOB " + jobID + "...")
        JobInformationManager().storeVisualOptions(jobID, visualOptions)
        logging.info("STEP 3 - SAVING VISUAL OPTIONS FOR JOB " + jobID +
                     "...DONE")

        response.setContent({"success": True, "timestamp": newTimestamp})

    except Exception as ex:
        handleException(response,
                        ex,
                        __file__,
                        "pathwayAcquisitionSaveVisualOptions",
                        userID=userID)
    finally:
        return response
Example #5
0
def pathwayAcquisitionTouchJob(request, response):
    try:
        jobID = request.form.get("jobID")
        JobInformationManager().touchAccessDate(jobID)

        response.setContent({"success": True})
    except Exception as ex:
        handleException(response,
                        ex,
                        __file__,
                        "pathwayAcquisitionTouchJob",
                        jobID=jobID)
    finally:
        return response
Example #6
0
def pathwayAcquisitionSaveSharingOptions(request, response):
    #VARIABLE DECLARATION
    jobID = ""
    userID = ""

    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = request.cookies.get('userID')
        sessionToken = request.cookies.get('sessionToken')
        UserSessionManager().isValidUser(userID, sessionToken)

        #****************************************************************
        # Step 1.GET THE INSTANCE OF sharing options
        #****************************************************************
        jobID = request.form.get("jobID")
        jobInstance = JobInformationManager().loadJobInstance(jobID)

        if str(jobInstance.getUserID()) != str(userID):
            raise Exception("Invalid user for this jobID")

        #************************************************************************
        # Step 3. Save the visual Options in the MongoDB
        #************************************************************************
        jobInstance.setAllowSharing(
            request.form.get("allowSharing", 'false') == 'true')
        jobInstance.setReadOnly(
            request.form.get("readOnly", 'false') == 'true')

        logging.info("STEP 3 - SAVING SHARING OPTIONS FOR JOB " + jobID +
                     "...")
        JobInformationManager().storeSharingOptions(jobInstance)
        logging.info("STEP 3 - SAVING SHARING OPTIONS FOR JOB " + jobID +
                     "...DONE")

        response.setContent({"success": True})
    except Exception as ex:
        handleException(response,
                        ex,
                        __file__,
                        "pathwayAcquisitionSaveSharingOptions",
                        userID=userID)
    finally:
        return response
Example #7
0
def pathwayAcquisitionMetagenes_PART2(ROOT_DIRECTORY, userID, savedJobInstance,
                                      omicName, clusterNumber, RESPONSE):
    #VARIABLE DECLARATION
    jobID = ""
    userID = ""

    try:
        #************************************************************************
        # Step 3. Save the visual Options in the MongoDB
        #************************************************************************
        logging.info("UPDATE METAGENES - STEP 2 FOR JOB " + jobID + "...")
        savedJobInstance.generateMetagenesList(ROOT_DIRECTORY,
                                               {omicName: clusterNumber},
                                               [omicName])
        logging.info("UPDATE METAGENES - STEP 2 FOR JOB " + jobID + "...DONE")
        JobInformationManager().storePathways(savedJobInstance)

        matchedPathwaysJSONList = []
        for matchedPathway in savedJobInstance.getMatchedPathways().itervalues(
        ):
            matchedPathwaysJSONList.append(matchedPathway.toBSON())

        RESPONSE.setContent({
            "success": True,
            "jobID": jobID,
            # "timestamp": newTimestamp,
            "pathwaysInfo": matchedPathwaysJSONList
        })

    except Exception as ex:
        handleException(RESPONSE,
                        ex,
                        __file__,
                        "pathwayAcquisitionMetagenes_PART2",
                        userID=userID)
    finally:
        return RESPONSE
Example #8
0
def pathwayAcquisitionRecoverJob(request, response, QUEUE_INSTANCE):
    #VARIABLE DECLARATION
    jobInstance = None
    jobID = ""
    userID = ""
    #TODO: COMPROBAR OWNERS
    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = request.cookies.get('userID')
        sessionToken = request.cookies.get('sessionToken')
        UserSessionManager().isValidUser(userID, sessionToken)

        #****************************************************************
        # Step 1.LOAD THE INSTANCE OF JOB
        #****************************************************************
        formFields = request.form
        jobID = formFields.get("jobID")

        logging.info("RECOVER_JOB - LOADING JOB " + jobID + "...")
        jobInstance = JobInformationManager().loadJobInstance(jobID)
        queueJob = QUEUE_INSTANCE.fetch_job(jobID)

        if (queueJob is not None and not queueJob.is_finished()):
            logging.info("RECOVER_JOB - JOB " + jobID + " HAS NOT FINISHED ")
            response.setContent({
                "success":
                False,
                "message":
                "Your job " + jobID +
                " is still running in the queue. Please, try again later to check if it has finished."
            })
            return response

        if (jobInstance == None):
            #TODO DIAS BORRADO?
            logging.info("RECOVER_JOB - JOB " + jobID +
                         " NOT FOUND AT DATABASE.")
            response.setContent({
                "success":
                False,
                "errorMessage":
                "Job " + jobID +
                " not found at database.<br>Please, note that jobs are automatically removed after 7 days for guests and 14 days for registered users."
            })
            return response

        # Allow "no user" jobs to be viewed by anyone, logged or not
        if (str(jobInstance.getUserID()) != 'None'
                and jobInstance.getUserID() != userID
                and not jobInstance.getAllowSharing()):
            logging.info("RECOVER_JOB - JOB " + jobID +
                         " DOES NOT BELONG TO USER " + str(userID) +
                         " JOB HAS USER " + str(jobInstance.getUserID()))
            response.setContent({
                "success":
                False,
                "errorMessage":
                "Invalid Job ID (" + jobID +
                ") for current user.<br>Please, check the Job ID and try again."
            })
            return response

        logging.info("RECOVER_JOB - JOB " + jobInstance.getJobID() +
                     " LOADED SUCCESSFULLY.")

        matchedCompoundsJSONList = map(
            lambda foundFeature: foundFeature.toBSON(),
            jobInstance.getFoundCompounds())

        matchedPathwaysJSONList = []
        for matchedPathway in jobInstance.getMatchedPathways().itervalues():
            matchedPathwaysJSONList.append(matchedPathway.toBSON())
        logging.info("RECOVER_JOB - GENERATING PATHWAYS INFORMATION...DONE")

        if (len(matchedCompoundsJSONList) == 0
                and jobInstance.getLastStep() == 2
                and len(jobInstance.getCompoundBasedInputOmics()) > 0):
            logging.info(
                "RECOVER_JOB - JOB " + jobID +
                " DOES NOT CONTAINS FOUND COMPOUNDS (STEP 2: OLD FORMAT?).")
            response.setContent({
                "success":
                False,
                "errorMessage":
                "Job " + jobID +
                " does not contains saved information about the found compounds, please run it again."
            })
        elif (len(matchedPathwaysJSONList) == 0
              and jobInstance.getLastStep() > 2):
            logging.info("RECOVER_JOB - JOB " + jobID +
                         " DOES NOT CONTAINS PATHWAYS.")
            response.setContent({
                "success":
                False,
                "errorMessage":
                "Job " + jobID +
                " does not contains information about pathways. Please, run it again."
            })
        else:

            response.setContent({
                "success":
                True,
                "jobID":
                jobInstance.getJobID(),
                "userID":
                jobInstance.getUserID(),
                "pathwaysInfo":
                matchedPathwaysJSONList,
                "geneBasedInputOmics":
                jobInstance.getGeneBasedInputOmics(),
                "compoundBasedInputOmics":
                jobInstance.getCompoundBasedInputOmics(),
                "organism":
                jobInstance.getOrganism(),
                "summary":
                jobInstance.summary,
                "visualOptions":
                JobInformationManager().getVisualOptions(jobID),
                "databases":
                jobInstance.getDatabases(),
                "matchedMetabolites":
                matchedCompoundsJSONList,
                "stepNumber":
                jobInstance.getLastStep(),
                "name":
                jobInstance.getName(),
                "timestamp":
                int(time()),
                "allowSharing":
                jobInstance.getAllowSharing(),
                "readOnly":
                jobInstance.getReadOnly(),
                "omicsValuesID":
                jobInstance.getValueIdTable()
            })

    except Exception as ex:
        handleException(response,
                        ex,
                        __file__,
                        "pathwayAcquisitionRecoverJob",
                        userID=userID)
    finally:
        return response
Example #9
0
def pathwayAcquisitionStep3(request, response):
    #VARIABLE DECLARATION
    jobInstance = None
    jobID = ""
    userID = ""

    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = request.cookies.get('userID')
        sessionToken = request.cookies.get('sessionToken')
        UserSessionManager().isValidUser(userID, sessionToken)

        #****************************************************************
        # Step 1.LOAD THE INSTANCE OF JOB
        #****************************************************************
        formFields = request.form
        jobID = formFields.get("jobID")

        #TODO: IN PREVIOUS STEPS THE USER COULD SPECIFY THE DEFAULT OMICS TO SHOW
        visibleOmics = []

        logging.info("STEP3 - LOADING JOB " + jobID + "...")

        jobInstance = JobInformationManager().loadJobInstance(jobID)
        jobInstance.setUserID(userID)

        if (jobInstance == None):
            raise UserWarning("Job " + jobID + " was not found at database.")
        logging.info("STEP3 - JOB " + jobInstance.getJobID() +
                     " LOADED SUCCESSFULLY.")

        #****************************************************************
        # Step 2.READ THE SELECTED PATHWAYS
        #****************************************************************
        logging.info("STEP3 - GENERATING PATHWAYS INFORMATION...")
        selectedPathways = formFields.getlist("selectedPathways")
        #TODO: SOLO GENERAR INFO PARA LAS QUE NO LA TENGAN YA GUARDADA EN LA BBDD
        [
            selectedPathwayInstances, graphicalOptionsInstancesBSON,
            omicsValuesSubset
        ] = jobInstance.generateSelectedPathwaysInformation(
            selectedPathways, visibleOmics, True)

        logging.info("STEP3 - GENERATING PATHWAYS INFORMATION...DONE")

        #************************************************************************
        # Step 3. Save the jobInstance in the MongoDB
        #************************************************************************
        logging.info("STEP 3 - SAVING NEW JOB DATA...")
        JobInformationManager().storeJobInstance(jobInstance, 3)
        logging.info("STEP 3 - SAVING NEW JOB DATA...DONE")

        response.setContent({
            "success": True,
            "jobID": jobInstance.getJobID(),
            "graphicalOptionsInstances": graphicalOptionsInstancesBSON,
            "omicsValues": omicsValuesSubset,
            "organism": jobInstance.getOrganism()
        })

    except Exception as ex:
        handleException(response,
                        ex,
                        __file__,
                        "pathwayAcquisitionStep3",
                        userID=userID)
    finally:
        return response
Example #10
0
def pathwayAcquisitionStep1_PART1(REQUEST, RESPONSE, QUEUE_INSTANCE, JOB_ID,
                                  EXAMPLE_FILES_DIR, exampleMode):
    """
    This function corresponds to FIRST PART of the FIRST step in the Pathways acquisition process.
    First, it takes a Request object which contains the fields of the form that started the process.
    This is a summarization for the steps in the process:
        Step 0. VARIABLE DECLARATION
        Step 1. CHECK IF VALID USER SESSION
        Step 2. CREATE THE NEW INSTANCE OF JOB
        Step 3. SAVE THE UPLOADED FILES
        Step 4. QUEUE THE JOB INSTANCE
        Step 5. RETURN THE NEW JOB ID

    @param {Request} REQUEST
    @param {Response} RESPONSE
    @param {RQ QUEUE} QUEUE_INSTANCE
    @param {String} JOB_ID
    @param {Boolean} exampleMode
    @returns Response
    """
    #TODO:ALLOWED_EXTENSIONS http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
    #TODO: secure_filename
    #****************************************************************
    #Step 0. VARIABLE DECLARATION
    #The following variables are defined:
    #  - jobInstance: instance of the PathwayAcquisitionJob class.
    #                 Contains all the information for the current job.
    #  - userID: the ID for the user
    #****************************************************************
    jobInstance = None
    userID = None

    try:
        #****************************************************************
        # Step 1. CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = REQUEST.cookies.get('userID')
        sessionToken = REQUEST.cookies.get('sessionToken')
        UserSessionManager().isValidUser(userID, sessionToken)

        #****************************************************************
        # Step 2. CREATE THE NEW INSTANCE OF JOB
        #****************************************************************
        jobInstance = PathwayAcquisitionJob(JOB_ID, userID, CLIENT_TMP_DIR)
        jobInstance.initializeDirectories()
        logging.info("STEP1 - NEW JOB SUBMITTED " + jobInstance.getJobID())

        #****************************************************************
        # Step 3. SAVE THE UPLOADED FILES
        #****************************************************************
        if (exampleMode == False):
            logging.info("STEP1 - FILE UPLOADING REQUEST RECEIVED")
            uploadedFiles = REQUEST.files
            formFields = REQUEST.form
            jobInstance.description = ""
            jobInstance.setName(formFields.get("jobDescription", "")[:100])
            specie = formFields.get("specie")  #GET THE SPECIE NAME
            databases = REQUEST.form.getlist('databases[]')
            jobInstance.setOrganism(specie)
            # Check the available databases for species
            organismDB = set(dicDatabases.get(specie, [{}])[0].keys())
            # TODO: disabled multiple databases for the moment
            # jobInstance.setDatabases(list(set([u'KEGG']) | set(databases).intersection(organismDB)))
            jobInstance.setDatabases([u'KEGG'])
            logging.info("STEP1 - SELECTED SPECIE IS " + specie)

            logging.info("STEP1 - READING FILES....")
            JobInformationManager().saveFiles(uploadedFiles, formFields,
                                              userID, jobInstance,
                                              CLIENT_TMP_DIR)
            logging.info("STEP1 - READING FILES....DONE")

        elif (exampleMode == "example"):
            #****************************************************************
            # Step 2.SAVE THE UPLOADED FILES
            #****************************************************************
            logging.info("STEP1 - EXAMPLE MODE SELECTED")
            logging.info("STEP1 - COPYING FILES....")

            exampleOmics = {
                "Gene expression": False,
                "Metabolomics": True,
                "Proteomics": True,
                "miRNA-seq": False,
                "DNase-seq": False
            }
            for omicName, featureEnrichment in exampleOmics.iteritems():
                dataFileName = omicName.replace(" ", "_").replace(
                    "-seq", "").lower() + "_values.tab"
                logging.info(
                    "STEP1 - USING ALREADY SUBMITTED FILE (data file) " +
                    EXAMPLE_FILES_DIR + dataFileName + " FOR  " + omicName)

                relevantFileName = omicName.replace(" ", "_").replace(
                    "-seq", "").lower() + "_relevant.tab"
                logging.info(
                    "STEP1 - USING ALREADY SUBMITTED FILE (relevant features file) "
                    + EXAMPLE_FILES_DIR + relevantFileName + " FOR  " +
                    omicName)

                if (["Metabolomics"].count(omicName)):
                    jobInstance.addCompoundBasedInputOmic({
                        "omicName":
                        omicName,
                        "inputDataFile":
                        EXAMPLE_FILES_DIR + dataFileName,
                        "relevantFeaturesFile":
                        EXAMPLE_FILES_DIR + relevantFileName,
                        "isExample":
                        True,
                        "featureEnrichment":
                        featureEnrichment
                    })
                else:
                    jobInstance.addGeneBasedInputOmic({
                        "omicName":
                        omicName,
                        "inputDataFile":
                        EXAMPLE_FILES_DIR + dataFileName,
                        "relevantFeaturesFile":
                        EXAMPLE_FILES_DIR + relevantFileName,
                        "isExample":
                        True,
                        "featureEnrichment":
                        featureEnrichment
                    })

            specie = "mmu"
            jobInstance.setOrganism(specie)
            jobInstance.setDatabases(['KEGG'])
        else:
            raise NotImplementedError

        #************************************************************************
        # Step 4. Queue job
        #************************************************************************
        QUEUE_INSTANCE.enqueue(fn=pathwayAcquisitionStep1_PART2,
                               args=(jobInstance, userID, exampleMode,
                                     RESPONSE),
                               timeout=600,
                               job_id=JOB_ID)

        #************************************************************************
        # Step 5. Return the Job ID
        #************************************************************************
        RESPONSE.setContent({"success": True, "jobID": JOB_ID})
    except Exception as ex:
        if (jobInstance != None):
            jobInstance.cleanDirectories(remove_output=True)

        handleException(RESPONSE,
                        ex,
                        __file__,
                        "pathwayAcquisitionStep1_PART1",
                        userID=userID)
    finally:
        return RESPONSE
Example #11
0
def pathwayAcquisitionStep2_PART2(jobID, userID, selectedCompounds,
                                  clusterNumber, RESPONSE, ROOT_DIRECTORY):
    """
    This function corresponds to SECOND PART of the SECOND step in the Pathways acquisition process.
    Given a JOB INSTANCE, first processes the uploaded files (identifiers matching and compound list generation)
    and finally generates the response.
    This code is executed at the Redis Queue.

    This is a summarization for the steps in the process:
        Step 1. READ AND UPDATE THE SELECTED METABOLITES
        Step 2. GENERATE PATHWAYS INFORMATION
        Step 3. GENERATE THE METAGENES INFORMATION
        Step 4. UPDATE JOB INFORMATION AT DATABASE
        Step 5. GENERATE RESPONSE AND FINISH

    @param {PathwayAcquisitionJob} jobInstance
    @param {String[]} selectedCompounds
    @param {Response} RESPONSE
    @param {String} userID
    @param {Boolean} exampleMode

    @returns Response
    """
    jobInstance = None

    try:
        logging.info("STEP2 - LOADING JOB " + jobID + "...")
        jobInstance = JobInformationManager().loadJobInstance(jobID)
        jobInstance.setUserID(userID)

        if (jobInstance == None):
            raise UserWarning("Job " + jobID + " was not found at database.")

        jobInstance.setDirectories(CLIENT_TMP_DIR)
        jobInstance.initializeDirectories()

        logging.info("STEP2 - JOB " + jobInstance.getJobID() +
                     " LOADED SUCCESSFULLY.")

        #****************************************************************
        # Step 1.READ THE SELECTED METABOLITES
        #****************************************************************
        logging.info("STEP2 - UPDATING SELECTED COMPOUNDS LIST...")
        #TODO: CHANGE THIS TO ALLOW BACK
        jobInstance.updateSubmitedCompoundsList(selectedCompounds)
        logging.info("STEP2 - UPDATING SELECTED COMPOUNDS LIST...DONE")

        #****************************************************************
        # Step 2. GENERATING PATHWAYS INFORMATION
        #****************************************************************
        logging.info("STEP2 - GENERATING PATHWAYS INFORMATION...")
        summary = jobInstance.generatePathwaysList()
        logging.info("STEP2 - GENERATING PATHWAYS INFORMATION...DONE")

        #****************************************************************
        # Step 3. GENERATING METAGENES INFORMATION
        #****************************************************************
        logging.info("STEP2 - GENERATING METAGENES INFORMATION...")
        jobInstance.generateMetagenesList(ROOT_DIRECTORY, clusterNumber)
        logging.info("STEP2 - GENERATING METAGENES INFORMATION...DONE")

        jobInstance.setLastStep(3)

        #************************************************************************
        # Step 4. Save the all the Matched Compounds and pathways in MongoDB
        #************************************************************************
        logging.info("STEP2 - SAVING NEW JOB DATA...")
        JobInformationManager().storeJobInstance(jobInstance, 2)
        logging.info("STEP2 - SAVING NEW JOB DATA...DONE")

        #************************************************************************
        # Step 5. Update the response content
        #************************************************************************
        matchedPathwaysJSONList = []
        for matchedPathway in jobInstance.getMatchedPathways().itervalues():
            matchedPathwaysJSONList.append(matchedPathway.toBSON())

        RESPONSE.setContent({
            "success":
            True,
            "organism":
            jobInstance.getOrganism(),
            "jobID":
            jobInstance.getJobID(),
            "summary":
            summary,
            "pathwaysInfo":
            matchedPathwaysJSONList,
            "geneBasedInputOmics":
            jobInstance.getGeneBasedInputOmics(),
            "compoundBasedInputOmics":
            jobInstance.getCompoundBasedInputOmics(),
            "databases":
            jobInstance.getDatabases(),
            "omicsValuesID":
            jobInstance.getValueIdTable(),
            "timestamp":
            int(time())
        })

    except Exception as ex:
        handleException(RESPONSE,
                        ex,
                        __file__,
                        "pathwayAcquisitionStep2_PART2",
                        userID=userID)
    finally:
        jobInstance.cleanDirectories()
        return RESPONSE
Example #12
0
def pathwayAcquisitionStep1_PART2(jobInstance, userID, exampleMode, RESPONSE):
    """
    This function corresponds to SECOND PART of the FIRST step in the Pathways acquisition process.
    Given a JOB INSTANCE, first processes the uploaded files (identifiers matching and compound list generation)
    and finally generates the response.
    This code is executed at the PyQlite Queue.

    This is a summarization for the steps in the process:
        Step 0. CHECK FILES CONTENT
        Step 1. PROCESS THE FILES DATA
        Step 2. SAVE THE JOB INSTANCE AT THE DATABASE
        Step 3. GENERATE RESPONSE AND FINISH

    @param {PathwayAcquisitionJob} jobInstance
    @param {Response} RESPONSE
    @param {String} userID
    @param {Boolean} exampleMode

    @returns Response
    """
    try:
        #****************************************************************
        # Step 0.VALIDATE THE FILES DATA
        #****************************************************************
        logging.info("STEP0 - VALIDATING INPUT...")
        jobInstance.validateInput()
        logging.info("STEP1 - VALIDATING INPUT...DONE")

        #****************************************************************
        # Step 1.PROCESS THE FILES DATA
        #****************************************************************
        logging.info("STEP1 - PROCESSING FILES...")
        matchedMetabolites = jobInstance.processFilesContent(
        )  #This function processes all the files and returns a checkboxes list to show to the user
        logging.info("STEP1 - PROCESSING FILES...DONE")

        #************************************************************************
        # Step 2. Save the jobInstance in the MongoDB
        #************************************************************************
        logging.info("STEP1 - SAVING JOB DATA...")
        jobInstance.setLastStep(2)
        jobInstance.getJobDescription(True, exampleMode == "example")
        JobInformationManager().storeJobInstance(jobInstance, 1)
        logging.info("STEP1 - SAVING JOB DATA...DONE")

        jobInstance.cleanDirectories()

        #************************************************************************
        # Step 3. Update the response content
        #************************************************************************
        RESPONSE.setContent({
            "success":
            True,
            "organism":
            jobInstance.getOrganism(),
            "jobID":
            jobInstance.getJobID(),
            "userID":
            jobInstance.getUserID(),
            "matchedMetabolites":
            map(lambda foundFeature: foundFeature.toBSON(),
                matchedMetabolites),
            "geneBasedInputOmics":
            jobInstance.getGeneBasedInputOmics(),
            "compoundBasedInputOmics":
            jobInstance.getCompoundBasedInputOmics(),
            "databases":
            jobInstance.getDatabases(),
            "name":
            jobInstance.getName(),
            "timestamp":
            int(time())
        })

    except Exception as ex:
        jobInstance.cleanDirectories(remove_output=True)

        # TODO: at this point we should notify the queue system about the error, or else
        # will keep returning success to the job.
        handleException(RESPONSE,
                        ex,
                        __file__,
                        "pathwayAcquisitionStep1_PART2",
                        userID=userID)
    finally:
        return RESPONSE
Example #13
0
    def __init__(self):
        ##*******************************************************************************************
        ##****SERVLET DEFINITION*********************************************************************
        ##*******************************************************************************************
        self.readConfigurationFile()
        self.app = Flask(__name__)

        self.app.config['MAX_CONTENT_LENGTH'] = SERVER_MAX_CONTENT_LENGTH
        self.app.json_encoder = MyJSONEncoder

        KeggInformationManager(KEGG_DATA_DIR)  #INITIALIZE THE SINGLETON
        JobInformationManager()  #INITIALIZE THE SINGLETON

        self.startScheludeTasks()  #CLEAN DATA EVERY N HOURS

        self.queue = Queue()
        self.queue.start_worker(N_WORKERS)

        #******************************************************************************************
        #     ______ _____ _      ______  _____
        #   |  ____|_   _| |    |  ____|/ ____|
        #   | |__    | | | |    | |__  | (___
        #   |  __|   | | | |    |  __|  \___ \
        #   | |     _| |_| |____| |____ ____) |
        #   |_|    |_____|______|______|_____/
        #
        #  COMMON STEPS HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/')
        def main():
            return send_from_directory(self.ROOT_DIRECTORY + 'public_html',
                                       'index.html')

        ##*******************************************************************************************
        ##* GET THUMBNAILS, PATHWAY IMAGE, etc
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/kegg_data/<path:filename>')
        def get_kegg_data(filename):
            if str(filename) == "species.json":
                return send_from_directory(KEGG_DATA_DIR + 'current/',
                                           'species.json')
            else:
                # Possible accepted format <path>_<source>_thumb
                split_name = filename.replace('_thumb', '').split('_')

                # Sanitize input
                source_type = sub(
                    r'\W+', '', split_name[1]) if len(split_name) > 1 else None
                source_dir = 'current/' + source_type.lower(
                ) if source_type is not None else 'current/common'

                # Add "map" prefix for KEGG pathways
                filename_prefix = 'map' if source_type is None else str()

                filename_cleaned = sub(
                    "[^0-9]", "",
                    split_name[0]) if source_type is None else split_name[0]

                if str(filename).endswith("_thumb"):
                    return send_from_directory(
                        KEGG_DATA_DIR + source_dir + '/png/thumbnails/',
                        filename_prefix + filename_cleaned + '_thumb.png')
                else:
                    return send_from_directory(
                        KEGG_DATA_DIR + source_dir + '/png/',
                        filename_prefix + filename_cleaned + '.png')

        ##*******************************************************************************************
        ##* GET PATHWAY IMAGE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN +
                        '/kegg_data/pathway_network/<path:specie>')
        def get_pathway_network(specie):
            return send_from_directory(KEGG_DATA_DIR + 'current/' + specie,
                                       'pathways_network.json')

        ##*******************************************************************************************
        ##* GET DATA FROM CLIENT TMP DIR
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/CLIENT_TMP/<path:filename>')
        def get_client_file(filename):
            #TODO: CHECK CREDENTIALS?
            UserSessionManager().isValidUser(
                request.cookies.get('userID'),
                request.cookies.get('sessionToken'))
            return send_from_directory(self.ROOT_DIRECTORY + 'CLIENT_TMP',
                                       filename)

        @self.app.route(SERVER_SUBDOMAIN + '/get_cluster_image/<path:filename>'
                        )
        def get_cluster_image(filename):
            jobID = filename.split('/')[0]
            jobInstance = JobInformationManager().loadJobInstance(jobID)

            # Check if the file really exist, if not, then we are probably accessing a public job from a logged
            # account.
            userDir = "nologin" if jobInstance.getUserID(
            ) is None else jobInstance.getUserID()
            image_path = CLIENT_TMP_DIR + userDir + "/jobsData/"

            return send_from_directory(image_path, filename)

        ##*******************************************************************************************
        ##* GET FILE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/<path:filename>')
        def get_static(filename):
            return send_from_directory(self.ROOT_DIRECTORY + 'public_html',
                                       filename)

        #******************************************************************************************
        #    _    _  _____ ______ _____   _____
        #   | |  | |/ ____|  ____|  __ \ / ____|
        #   | |  | | (___ | |__  | |__) | (___
        #   | |  | |\___ \|  __| |  _  / \___ \
        #   | |__| |____) | |____| | \ \ ____) |
        #    \____/|_____/|______|_|  \_\_____/
        #
        #  USER MANAGEMENT SERVLETS HANDLERS
        #*******************************************************************************************
        ##* LOGIN
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/um_signin',
                        methods=['OPTIONS', 'POST'])
        def signInHandler():
            return userManagementSignIn(request, Response()).getResponse()

        #*******************************************************************************************
        ##* LOGOUT
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/um_signout',
                        methods=['OPTIONS', 'POST'])
        def signOutHandler():
            return userManagementSignOut(request, Response()).getResponse()

        #*******************************************************************************************
        ##* LOGOUT
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/um_signup',
                        methods=['OPTIONS', 'POST'])
        def signUpHandler():
            return userManagementSignUp(request, Response(),
                                        self.ROOT_DIRECTORY).getResponse()

        #*******************************************************************************************
        ##* LOGOUT
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/um_guestsession',
                        methods=['OPTIONS', 'POST'])
        def newGuestSessionHandler():
            return userManagementNewGuestSession(request,
                                                 Response()).getResponse()

        #*******************************************************************************************
        ##* LOGOUT
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/um_nologinsession',
                        methods=['OPTIONS', 'POST'])
        def newNoLoginSessionHandler():
            return userManagementNewNoLoginSession(request,
                                                   Response()).getResponse()

        #*******************************************************************************************
        ##* CHANGE PASS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/um_changepassword',
                        methods=['OPTIONS', 'POST'])
        def changePasswordHandler():
            return userManagementChangePassword(request,
                                                Response()).getResponse()

        #*******************************************************************************************
        ##* USER MANAGEMENT SERVLETS HANDLERS - END
        #******************************************************************************************

        #******************************************************************************************
        #     ______ _____ _      ______  _____
        #   |  ____|_   _| |    |  ____|/ ____|
        #   | |__    | | | |    | |__  | (___
        #   |  __|   | | | |    |  __|  \___ \
        #   | |     _| |_| |____| |____ ____) |
        #   |_|    |_____|______|______|_____/
        #
        #   FILE UPLOAD HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_upload_file',
                        methods=['OPTIONS', 'POST'])
        def uploadFileHandler():
            return dataManagementUploadFile(request, Response(),
                                            CLIENT_TMP_DIR).getResponse()

        #*******************************************************************************************
        ##* FILE LIST HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_get_myfiles',
                        methods=['OPTIONS', 'POST'])
        def getMyFilesHandler():
            return dataManagementGetMyFiles(request, Response(),
                                            CLIENT_TMP_DIR,
                                            MAX_CLIENT_SPACE).getResponse()

        #*******************************************************************************************
        ##* FILE DELETION HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_delete_file',
                        methods=['OPTIONS', 'POST'])
        def deleteFileHandler():
            return dataManagementDeleteFile(request, Response(),
                                            CLIENT_TMP_DIR,
                                            MAX_CLIENT_SPACE).getResponse()

        #*******************************************************************************************
        ##* JOB LIST HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_get_myjobs',
                        methods=['OPTIONS', 'POST'])
        def getMyJobsHandler():
            return dataManagementGetMyJobs(request, Response()).getResponse()

        #*******************************************************************************************
        ##* JOB DELETION HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_delete_job',
                        methods=['OPTIONS', 'POST'])
        def deleteJobHandler():
            return dataManagementDeleteJob(request, Response()).getResponse()

        #*******************************************************************************************
        ##* JOB RESULTS HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_downloadFile',
                        methods=['OPTIONS', 'GET'])
        def downloadFileHandler():
            response = dataManagementDownloadFile(request, Response())
            if hasattr(response, "getResponse"):
                response = response.getResponse()
            return response

        #*******************************************************************************************
        ##* GFT FILES HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_get_gtffiles',
                        methods=['OPTIONS', 'POST'])
        def getGTFFilesHandler():
            return dataManagementGetMyFiles(request,
                                            Response(),
                                            self.EXAMPLE_FILES_DIR,
                                            MAX_CLIENT_SPACE,
                                            isReference=True).getResponse()

        #*******************************************************************************************
        ##* DATA MANIPULATION SERVLETS HANDLERS - END
        #*******************************************************************************************

        #*******************************************************************************************
        #         _  ____  ____   _____
        #        | |/ __ \|  _ \ / ____|
        #        | | |  | | |_) | (___
        #    _   | | |  | |  _ < \___ \
        #   | |__| | |__| | |_) |____) |
        #    \____/ \____/|____/|_____/
        #
        #############################################################################################
        #  COMMON JOB HANDLERS
        #
        #  CHECK JOB STATUS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/check_job_status/<path:jobID>',
                        methods=['OPTIONS', 'POST'])
        def checkJobStatus(jobID):
            jobInstance = self.queue.fetch_job(jobID)

            if jobInstance is None:
                return Response().setContent({
                    "success":
                    False,
                    "status":
                    "failed",
                    "message":
                    "Your job is not on the queue anymore. Check your job list, if it's not there the process stopped and you must resend the data again."
                }).getResponse()
            elif jobInstance.is_finished():
                return self.queue.get_result(jobID).getResponse()
            elif jobInstance.is_failed():
                self.queue.get_result(jobID)  #remove job
                return Response().setContent({
                    "success":
                    False,
                    "status":
                    str(jobInstance.get_status()),
                    "message":
                    jobInstance.error_message
                }).getResponse()
            else:
                return Response().setContent({
                    "success":
                    False,
                    "status":
                    str(jobInstance.get_status())
                }).getResponse()

        #*******************************************************************************************
        ##* COMMON JOB HANDLERS - END
        #############################################################################################
        #############################################################################################
        #
        # PATHWAY ACQUISITION SERVLETS HANDLERS
        #
        # STEP 1 HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_step1/<path:exampleMode>',
                        methods=['OPTIONS', 'POST'])
        @self.app.route(SERVER_SUBDOMAIN + '/pa_step1',
                        methods=['OPTIONS', 'POST'])
        def pathwayAcquisitionStep1Handler(exampleMode=False):
            return pathwayAcquisitionStep1_PART1(request, Response(),
                                                 self.queue,
                                                 self.generateRandomID(),
                                                 self.EXAMPLE_FILES_DIR,
                                                 exampleMode).getResponse()

        #*******************************************************************************************
        # STEP 2 HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_step2',
                        methods=['OPTIONS', 'POST'])
        def pathwayAcquisitionStep2Handler():
            return pathwayAcquisitionStep2_PART1(
                request, Response(), self.queue,
                self.ROOT_DIRECTORY).getResponse()

        #*******************************************************************************************
        # STEP 3 HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_step3',
                        methods=['OPTIONS', 'POST'])
        def pathwayAcquisitionStep3Handler():
            return pathwayAcquisitionStep3(request, Response()).getResponse()

        #*******************************************************************************************
        # RECOVER JOB HANDLER
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_recover_job',
                        methods=['OPTIONS', 'POST'])
        def recoverJobHandler():
            return pathwayAcquisitionRecoverJob(request, Response(),
                                                self.queue).getResponse()

        # *******************************************************************************************
        # TOUCH JOB HANDLER
        # *******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_touch_job',
                        methods=['OPTIONS', 'POST'])
        def touchJobHandler():
            return pathwayAcquisitionTouchJob(request,
                                              Response()).getResponse()

        #*******************************************************************************************
        # SAVE IMAGE HANDLER
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_save_image',
                        methods=['OPTIONS', 'POST'])
        def saveImageHandler():
            return pathwayAcquisitionSaveImage(request,
                                               Response()).getResponse()

        #*******************************************************************************************
        # SAVE VISUAL OPTIONS HANDLER
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_save_visual_options',
                        methods=['OPTIONS', 'POST'])
        def saveVisualOptionsHandler():
            return pathwayAcquisitionSaveVisualOptions(
                request, Response()).getResponse()

        #*******************************************************************************************
        # SAVE SHARING OPTIONS HANDLER
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_save_sharing_options',
                        methods=['OPTIONS', 'POST'])
        def saveSharingOptionsHandler():
            return pathwayAcquisitionSaveSharingOptions(
                request, Response()).getResponse()

        # *******************************************************************************************
        # RETRIEVE NEW P-VALUES HANDLER
        # *******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_adjust_pvalues',
                        methods=['OPTIONS', 'POST'])
        def adjustPvaluesHandler():
            return pathwayAcquisitionAdjustPvalues(request,
                                                   Response()).getResponse()

        # *******************************************************************************************
        # REGENERATE METAGENES HANDLER
        # *******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/pa_get_clusters',
                        methods=['OPTIONS', 'POST'])
        def metagenesHandler():
            return pathwayAcquisitionMetagenes_PART1(
                request, Response(), self.queue, self.generateRandomID(),
                self.ROOT_DIRECTORY).getResponse()

        #*******************************************************************************************
        # PATHWAY SERVLETS HANDLERS - END
        #############################################################################################
        #############################################################################################
        #
        # ALTERNATIVE PIPELINES SERVLETS HANDLERS
        #
        # fromBEDtoGenes HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN +
                        '/dm_fromBEDtoGenes/<path:exampleMode>',
                        methods=['OPTIONS', 'POST'])
        @self.app.route(SERVER_SUBDOMAIN + '/dm_fromBEDtoGenes',
                        methods=['OPTIONS', 'POST'])
        def fromBEDtoGenesHandler(exampleMode=False):
            result = fromBEDtoGenes_STEP1(request, Response(), self.queue,
                                          self.generateRandomID(),
                                          self.EXAMPLE_FILES_DIR,
                                          exampleMode).getResponse()
            return result

        #*******************************************************************************************
        # fromMiRNAtoGenes HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN +
                        '/dm_fromMiRNAtoGenes/<path:exampleMode>',
                        methods=['OPTIONS', 'POST'])
        @self.app.route(SERVER_SUBDOMAIN + '/dm_fromMiRNAtoGenes',
                        methods=['OPTIONS', 'POST'])
        def fromMiRNAtoGenesHandler(exampleMode=False):
            result = fromMiRNAtoGenes_STEP1(request, Response(), self.queue,
                                            self.generateRandomID(),
                                            self.EXAMPLE_FILES_DIR,
                                            exampleMode).getResponse()
            return result

        #*******************************************************************************************
        ##* ALTERNATIVE PIPELINES SERVLETS HANDLERS - END
        #############################################################################################

        #*******************************************************************************************
        #             _____  __  __ _____ _   _
        #       /\   |  __ \|  \/  |_   _| \ | |
        #      /  \  | |  | | \  / | | | |  \| |
        #     / /\ \ | |  | | |\/| | | | | . ` |
        #    / ____ \| |__| | |  | |_| |_| |\  |
        #   /_/    \_\_____/|_|  |_|_____|_| \_|
        #
        ##* ADMIN SERVLETS HANDLERS
        ##*
        ##* GET ADMIN SITE FILES HANDLERS
        ##*******************************************************************************************
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/admin/')
        def get_admin_static():
            try:
                userID = request.cookies.get('userID')
                sessionToken = request.cookies.get('sessionToken')
                userName = request.cookies.get('userName')
                UserSessionManager().isValidAdminUser(userID, userName,
                                                      sessionToken)
                return send_from_directory(
                    self.ROOT_DIRECTORY + 'public_html/admin', "index.html")
            except Exception as ex:
                return send_from_directory(
                    self.ROOT_DIRECTORY + 'public_html/admin', "404.html")

        ##*******************************************************************************************
        ##* GET LIST OF INSTALLED SPECIES
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/databases/',
                        methods=['OPTIONS', 'GET'])
        def getInstalledDatabasesInfo():
            return adminServletGetInstalledOrganisms(request,
                                                     Response()).getResponse()

        ##*******************************************************************************************
        ##* GET AVAILABLE SPECIES
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/databases/available',
                        methods=['OPTIONS', 'GET'])
        def getAvailableDatabasesInfo():
            return adminServletGetAvailableOrganisms(request,
                                                     Response()).getResponse()

        ##*******************************************************************************************
        ##* INSTALL OR UPDATE SELECTED SPECIE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN +
                        '/api/admin/databases/<path:organism_code>',
                        methods=['OPTIONS', 'POST'])
        def installOrganismDatabaseData(organism_code):
            return adminServletInstallOrganism(
                request, Response(), organism_code,
                self.ROOT_DIRECTORY).getResponse()

        ##* DELETE SELECTED SPECIE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN +
                        '/api/admin/databases/<path:organism_code>',
                        methods=['OPTIONS', 'DELETE'])
        def deleteOrganismDatabaseData(organism_code):
            response = Response()
            response.setContent({"success": False})
            return response.getResponse()
            #return adminServletDeleteOrganism(request, Response(), organism_code, self.ROOT_DIRECTORY).getResponse()

        ##*******************************************************************************************
        ##* MONITOR THE USAGE OF RAM AND CPU
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/system-info/',
                        methods=['OPTIONS', 'GET'])
        def systemInformation():
            return adminServletSystemInformation(request,
                                                 Response()).getResponse()

        ##*******************************************************************************************
        ##* GET ALL USERS AND DISK USAGE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/users/',
                        methods=['OPTIONS', 'GET'])
        def getAllUsers():
            return adminServletGetAllUsers(request, Response()).getResponse()

        ##*******************************************************************************************
        ##* REMOVE USERS
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/users/<path:userID>',
                        methods=['OPTIONS', 'DELETE'])
        def deleteUser(userID):
            return adminServletDeleteUser(request, Response(),
                                          userID).getResponse()

        ##*******************************************************************************************
        ##* REMOVE OLD USERS AND CLEAN OLD DATA
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/clean-databases/',
                        methods=['OPTIONS', 'DELETE'])
        def cleanDatabases():
            return adminCleanDatabases(request, Response()).getResponse()

        ##*******************************************************************************************
        ##* ADD FILES HANDLERS
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/files/',
                        methods=['OPTIONS', 'POST'])
        def addReferenceFileHandler():
            return dataManagementUploadFile(request,
                                            Response(),
                                            self.EXAMPLE_FILES_DIR,
                                            isReference=True).getResponse()

        #*******************************************************************************************
        ##* FILE LIST HANDLERS
        #*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/files/',
                        methods=['OPTIONS', 'GET'])
        def getReferenceFilesHandler():
            return dataManagementGetMyFiles(request,
                                            Response(),
                                            self.EXAMPLE_FILES_DIR,
                                            MAX_CLIENT_SPACE,
                                            isReference=True).getResponse()

        ##*******************************************************************************************
        ##* GFT FILE DELETION HANDLERS
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/files/<path:fileName>',
                        methods=['OPTIONS', 'DELETE'])
        def deleteReferenceFileHandler(fileName):
            return dataManagementDeleteFile(request,
                                            Response(),
                                            self.EXAMPLE_FILES_DIR,
                                            MAX_CLIENT_SPACE,
                                            isReference=True,
                                            fileName=fileName).getResponse()

        ##*******************************************************************************************
        ##* SAVE THE  MESSAGE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/messages/',
                        methods=['OPTIONS', 'POST'])
        def saveMessage():
            return adminServletSaveMessage(request, Response()).getResponse()

        ##*******************************************************************************************
        ##* RETRIEVE THE MESSAGES
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/um_get_message',
                        methods=['OPTIONS', 'POST'])
        @self.app.route(SERVER_SUBDOMAIN + '/api/admin/messages/',
                        methods=['OPTIONS', 'GET'])
        def getMessage():
            return adminServletGetMessage(request, Response()).getResponse()

        ##*******************************************************************************************
        ##* DELETE MESSAGE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN +
                        '/api/admin/messages/<path:message_type>',
                        methods=['OPTIONS', 'DELETE'])
        def deleteMessage(message_type):
            return adminServletDeleteMessage(request, Response(),
                                             message_type).getResponse()

        ##*******************************************************************************************
        ##* SEND A REPORT MESSAGE
        ##*******************************************************************************************
        @self.app.route(SERVER_SUBDOMAIN + '/dm_sendReport',
                        methods=['OPTIONS', 'POST'])
        def sendReportHandler():
            return adminServletSendReport(request, Response(),
                                          self.ROOT_DIRECTORY).getResponse()
Example #14
0
def fromBEDtoGenes_STEP1(REQUEST,
                         RESPONSE,
                         QUEUE_INSTANCE,
                         JOB_ID,
                         EXAMPLE_FILES_DIR,
                         exampleMode=False):
    """
    This function corresponds to FIRST PART of the FIRST step in the Bed2Genes process.
    First, it takes a Request object which contains the fields of the form that started the process.
    This is a summary for the steps in the process:
        Step 0. VARIABLE DECLARATION
        Step 1. CHECK IF VALID USER SESSION
        Step 2. CREATE THE NEW INSTANCE OF JOB
        Step 3. SAVE THE UPLOADED FILES
        Step 4. READ PARAMS
        Step 5. QUEUE THE JOB INSTANCE
        Step 6. RETURN THE NEW JOB ID

    @param {Request} REQUEST
    @param {Response} RESPONSE
    @param {RQ QUEUE} QUEUE_INSTANCE
    @param {String} JOB_ID
    @param {Boolean} exampleMode
    @returns Response
    """
    #TODO: ALLOWED_EXTENSIONS http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
    #TODO: secure_filename
    #****************************************************************
    #Step 0. VARIABLE DECLARATION
    #The following variables are defined:
    #  - jobInstance: instance of the Bed2GeneJob class. Contains all the information for the current job.
    #  - userID: the ID for the user
    #****************************************************************
    jobInstance = None
    userID = None

    try:
        #****************************************************************
        # Step 1. CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = REQUEST.cookies.get('userID')
        sessionToken = REQUEST.cookies.get('sessionToken')
        UserSessionManager().isValidUser(userID, sessionToken)

        #****************************************************************
        # Step 2. CREATE THE NEW INSTANCE OF JOB
        #****************************************************************
        jobInstance = Bed2GeneJob(JOB_ID, userID, CLIENT_TMP_DIR)
        jobInstance.initializeDirectories()
        logging.info("STEP1 - NEW JOB SUBMITTED " + jobInstance.getJobID())

        #****************************************************************
        # Step 3. SAVE THE UPLOADED FILES
        #****************************************************************
        formFields = REQUEST.form

        if (exampleMode == False):
            logging.info("STEP1 - FILE UPLOADING REQUEST RECEIVED")
            uploadedFiles = REQUEST.files

            logging.info("STEP1 - READING FILES....")
            JobInformationManager().saveFiles(uploadedFiles, formFields,
                                              userID, jobInstance,
                                              CLIENT_TMP_DIR,
                                              EXAMPLE_FILES_DIR)
            logging.info("STEP1 - READING FILES....DONE")

        elif (exampleMode == "example"):
            #****************************************************************
            # Step 2.SAVE THE UPLOADED FILES
            #****************************************************************
            logging.info("STEP1 - EXAMPLE MODE SELECTED")
            logging.info("STEP1 - COPYING FILES....")

            exampleOmics = ["DNase unmapped"]
            for omicName in exampleOmics:
                dataFileName = omicName.replace(" ",
                                                "_").lower() + "_values.tab"
                logging.info(
                    "STEP1 - USING ALREADY SUBMITTED FILE (data file) " +
                    EXAMPLE_FILES_DIR + dataFileName + " FOR  " + omicName)
                relevantFileName = omicName.replace(
                    " ", "_").lower() + "_relevant.tab"
                logging.info(
                    "STEP1 - USING ALREADY SUBMITTED FILE (relevant features file) "
                    + EXAMPLE_FILES_DIR + relevantFileName + " FOR  " +
                    omicName)

                jobInstance.addGeneBasedInputOmic({
                    "omicName":
                    omicName,
                    "inputDataFile":
                    EXAMPLE_FILES_DIR + dataFileName,
                    "relevantFeaturesFile":
                    EXAMPLE_FILES_DIR + relevantFileName,
                    "isExample":
                    True
                })

                jobInstance.addReferenceInput({
                    "omicName":
                    omicName,
                    "fileType":
                    "Reference file",
                    "inputDataFile":
                    EXAMPLE_FILES_DIR + "GTF/sorted_mmu.gtf"
                })

            specie = "mmu"
            jobInstance.setOrganism(specie)
        else:
            raise NotImplementedError

        #****************************************************************
        # Step 4. READ PARAMS
        #****************************************************************
        namePrefix = formFields.get("name_prefix")
        logging.info("STEP2 - INPUT VALUES ARE:")
        jobInstance.omicName = formFields.get(namePrefix + "_omic_name",
                                              "DNase-seq")
        logging.info("  - omicName             :" + jobInstance.omicName)
        jobInstance.presortedGTF = formFields.get(namePrefix + "_presortedGTF",
                                                  False)
        logging.info("  - presortedGTF         :" +
                     str(jobInstance.presortedGTF))
        jobInstance.report = formFields.get(namePrefix + "_report", "gene")
        logging.info("  - report               :" + jobInstance.report)
        jobInstance.distance = formFields.get(namePrefix + "_distance", 10)
        logging.info("  - distance             :" + str(jobInstance.distance))
        jobInstance.tss = formFields.get(namePrefix + "_tss", 200)
        logging.info("  - tss                  :" + str(jobInstance.tss))
        jobInstance.promoter = formFields.get(namePrefix + "_promoter", 1300)
        logging.info("  - promoter             :" + str(jobInstance.promoter))
        jobInstance.geneAreaPercentage = formFields.get(
            namePrefix + "_geneAreaPercentage", 90)
        logging.info("  - geneAreaPercentage   :" +
                     str(jobInstance.geneAreaPercentage))
        jobInstance.regionAreaPercentage = formFields.get(
            namePrefix + "_regionAreaPercentage", 50)
        logging.info("  - regionAreaPercentage :" +
                     str(jobInstance.regionAreaPercentage))
        jobInstance.ignoreMissing = True if namePrefix + "_ignoremissing" in formFields.keys(
        ) else False
        logging.info("  - ignore missing       :" +
                     str(jobInstance.ignoreMissing))
        jobInstance.featureEnrichment = formFields.get(
            namePrefix + "_feature_enrichment_pre", False)
        logging.info("  - Feature Enrichment   :" +
                     str(jobInstance.featureEnrichment))

        #rules
        jobInstance.geneIDtag = formFields.get(namePrefix + "_geneIDtag",
                                               "gene_id")
        logging.info("  - geneIDtag            :" + str(jobInstance.geneIDtag))
        jobInstance.summarizationMethod = formFields.get(
            namePrefix + "_summarization_method", "mean")
        logging.info("  - summarization_method :" +
                     jobInstance.summarizationMethod)
        jobInstance.reportRegions = formFields.getlist(namePrefix +
                                                       "_reportRegions")
        if len(jobInstance.reportRegions) == 0:
            jobInstance.reportRegions = ["all"]
        logging.info("  - reportRegions :" + str(jobInstance.reportRegions))

        #************************************************************************
        # Step 4. Queue job
        #************************************************************************
        QUEUE_INSTANCE.enqueue(fn=fromBEDtoGenes_STEP2,
                               args=(jobInstance, userID, exampleMode,
                                     RESPONSE),
                               timeout=600,
                               job_id=JOB_ID)

        #************************************************************************
        # Step 5. Return the Job ID
        #************************************************************************
        RESPONSE.setContent({"success": True, "jobID": JOB_ID})
    except Exception as ex:
        handleException(RESPONSE, ex, __file__, "fromBEDtoGenes_STEP1")
    finally:
        return RESPONSE
Example #15
0
def fromBEDtoGenes_STEP2(jobInstance, userID, exampleMode, RESPONSE):
    """
    This function corresponds to SECOND PART of the FIRST step in the Bed2Genes process.
    Given a JOB INSTANCE, first executes the Bed2Gene function (map regions to genes)
    and finally generates the response.
    This code is executed at the PyQlite Queue.

    This is a summarization for the steps in the process:
        Step 1. PROCESS THE FILES DATA
        Step 2. SAVE THE JOB INSTANCE AT THE DATABASE
        Step 3. GENERATE RESPONSE AND FINISH

    @param {Bed2GeneJob} jobInstance
    @param {Response} RESPONSE
    @param {String} userID
    @param {Boolean} exampleMode

    @returns Response
    """
    try:
        #****************************************************************
        # Step 0.VALIDATE THE FILES DATA
        #****************************************************************
        logging.info("STEP0 - VALIDATING INPUT...")
        jobInstance.validateInput()
        logging.info("STEP1 - VALIDATING INPUT...DONE")

        #****************************************************************
        # Step 1.PROCESS THE FILES DATA
        #****************************************************************
        logging.info("STEP1 - Executing Bed2Gene function...")
        fileNames = jobInstance.fromBED2Genes()
        logging.info("STEP1 - Executing Bed2Gene function... DONE")

        #************************************************************************
        # Step 2. Save the jobInstance in the MongoDB
        #************************************************************************
        logging.info("STEP1 - SAVING JOB DATA...")
        JobInformationManager().storeJobInstance(jobInstance, 1)
        #TODO: JOB DESCRIPTION?
        logging.info("STEP1 - SAVING JOB DATA...DONE")

        #************************************************************************
        # Step 3. Update the response content
        #************************************************************************
        RESPONSE.setContent({
            "success": True,
            "jobID": jobInstance.getJobID(),
            "compressedFileName": fileNames[0],
            "mainOutputFileName": fileNames[1],
            "secondOutputFileName": fileNames[2],
            "description": jobInstance.description,
            "featureEnrichment": jobInstance.featureEnrichment
        })

    except Exception as ex:
        #****************************************************************
        # DELETE JOB FROM USER DIRECTORY
        #****************************************************************
        jobInstance.cleanDirectories(remove_output=True)

        handleException(RESPONSE, ex, __file__, "fromBEDtoGenes_STEP2")

    finally:
        return RESPONSE