Example #1
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 #2
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 #3
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