Пример #1
0
def adminServletGetMessage(request, response):
    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        message_type = request.form.get("message_type")

        if (message_type != "starting_message"):
            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 MESSAGES FROM THE DATABASE
        #****************************************************************
        daoInstance = MessageDAO()
        matchedMessages = daoInstance.findAll(
            otherParams={"message_type": message_type})
        daoInstance.closeConnection()
        response.setContent({"success": True, "messageList": matchedMessages})

    except Exception as ex:
        handleException(response, ex, __file__, "adminServletGetMessage")
    finally:
        return response
Пример #2
0
def adminCleanDatabases(request, response):
    """
    This function...

    @param {Request} request, the request object
    @param {Response} response, the response object
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        #****************************************************************
        # Step 1. RUN THE SCRIPT
        #****************************************************************
        from src.AdminTools.scripts.clean_databases import cleanDatabases as clean_databases_routine
        clean_databases_routine(force=True)

        response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "cleanDatabases")

    finally:
        return response
Пример #3
0
def adminServletDeleteMessage(request, response, message_type=None):
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        #****************************************************************
        # Step 1.GET THE MESSAGES FROM THE DATABASE
        #****************************************************************
        if message_type == None:
            message_type = request.form.get("message_type")
        daoInstance = MessageDAO()
        daoInstance.removeAll(otherParams={"message_type": message_type})
        daoInstance.closeConnection()

        response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "adminServletDeleteMessage")
    finally:
        return response
Пример #4
0
def userManagementSignOut(request, response):
    userInstance = None
    daoInstance = None

    try:
        #****************************************************************
        # Step 1.READ PARAMS
        #****************************************************************
        logging.info("STEP1 - READ PARAMS...")
        formFields = request.form
        userID = formFields.get("userID")
        sessionToken = formFields.get("sessionToken")
        #****************************************************************
        # Step 2. CLOSE SESSION
        #****************************************************************
        logging.info("STEP2 - REMOVING USER..")
        UserSessionManager().removeUser(userID, sessionToken)
        response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "userManagementSignOut")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #5
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
Пример #6
0
def adminServletDeleteUser(request, response, toDeleteUserID):
    """
    This function...

    @param {Request} request, the request object
    @param {Response} response, the response object
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        if toDeleteUserID == "0":
            response.setContent({"success": False})
        else:
            jobDAOInstance = JobDAO()
            filesDAOInstance = FileDAO()
            userDAOInstance = UserDAO()

            logging.info("STEP1 - CLEANING DATA FOR " + toDeleteUserID + "...")
            #****************************************************************
            # Step 1. DELETE ALL JOBS FOR THE USER
            #****************************************************************
            allJobs = jobDAOInstance.findAll(
                otherParams={"userID": toDeleteUserID})
            jobID = ""
            for jobInstance in allJobs:
                jobID = jobInstance.getJobID()
                logging.info("STEP2 - REMOVING " + jobID + " FROM DATABASE...")
                jobDAOInstance.remove(jobInstance.getJobID(),
                                      otherParams={"userID": toDeleteUserID})

            #****************************************************************
            # Step 3. DELETE ALL FILES FOR THE USER
            #****************************************************************
            logging.info("STEP3 - REMOVING ALL FILES FROM DATABASE...")
            filesDAOInstance.removeAll(otherParams={"userID": toDeleteUserID})
            logging.info("STEP3 - REMOVING ALL FILES FROM USER DIRECTORY...")
            if os_path.isdir(CLIENT_TMP_DIR + toDeleteUserID):
                shutil_rmtree(CLIENT_TMP_DIR + toDeleteUserID)

            #****************************************************************
            # Step 4. DELETE THE USER INSTANCE FROM DATABASE
            #****************************************************************
            logging.info("STEP6 - REMOVING ALL FILES FROM DATABASE...")
            userDAOInstance.remove(int(toDeleteUserID))

            response.setContent({"success": True})
    except Exception as ex:
        handleException(response, ex, __file__, "adminServletDeleteUser")
    finally:
        return response
Пример #7
0
def dataManagementGetMyFiles(request,
                             response,
                             DESTINATION_DIR,
                             MAX_CLIENT_SPACE,
                             isReference=False):
    #VARIABLE DECLARATION
    fileInstance = None
    fileInstances = []
    daoInstance = None
    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)

        if not isReference:
            DESTINATION_DIR += userID
        else:
            userID = "-1"
            DESTINATION_DIR += "GTF/"

        #****************************************************************
        # Step 1.GET THE LIST OF FILES
        #****************************************************************
        logging.info("STEP1 - GET MY FILE LIST REQUEST RECEIVED")
        daoInstance = FileDAO()
        matchedFiles = daoInstance.findAll(otherParams={"userID": userID})
        logging.info("STEP1 - GET MY FILE LIST REQUEST RECEIVED...DONE")

        #****************************************************************
        # Step 2.CALCULATE USED SPACE
        #****************************************************************
        logging.info("STEP2 - GET THE CURRENT USED SPACE...")
        dataSummary = {
            "usedSpace": dir_total_size(DESTINATION_DIR),
            "availableSpace": MAX_CLIENT_SPACE
        }
        logging.info("STEP2 - GET THE CURRENT USED SPACE...DONE")

        response.setContent({
            "success": True,
            "fileList": matchedFiles,
            "dataSummary": dataSummary
        })

    except Exception as ex:
        handleException(response, ex, __file__, "dataManagementGetMyFiles")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #8
0
def adminServletSystemInformation(request, response):
    """
    This function...

    @param {Request} request, the request object
    @param {Response} response, the response object
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)
        disk_use = []
        try:
            df = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
            output = df.communicate()[0]
            output = output.split("\n")
            output.pop(0)
            for line in output:
                disk_use.append(line.split())
        except Exception as e:
            pass

        return response.setContent({
            'cpu_count':
            psutil.cpu_count(),
            "cpu_use":
            psutil.cpu_percent(),
            "mem_total":
            psutil.virtual_memory().total / (1024.0**3),
            "mem_use":
            psutil.virtual_memory().percent,
            "swap_total":
            psutil.swap_memory().total / (1024.0**3),
            "swap_use":
            psutil.swap_memory().percent,
            "disk_use":
            disk_use
        }).getResponse()

    except Exception as ex:
        handleException(response, ex, __file__, "monitorCPU")
    finally:
        return response
Пример #9
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
Пример #10
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
Пример #11
0
def userManagementChangePassword(request, response):
    # VARIABLE DECLARATION
    userInstance = None
    daoInstance = 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.READ THE NEW PASS
        # ****************************************************************
        logging.info("STEP1 - READ PARAMS AND CHECK IF USER ALREADY EXISTS...")
        password = request.form.get("password")
        from hashlib import sha1
        password = sha1(password.encode('ascii')).hexdigest()

        daoInstance = UserDAO()
        userInstance = daoInstance.findByID(userID)
        if userInstance == None:
            raise CredentialException(
                "The email or password you entered is incorrect.")

        # ****************************************************************
        # Step 3. UPDATE THE MODEL
        # ****************************************************************
        userInstance.setPassword(password)
        daoInstance.update(userInstance, {})

        response.setContent({"success": True})

    except CredentialException as ex:
        handleException(response, ex, __file__, "userManagementChangePassword",
                        200)
    except Exception as ex:
        handleException(response, ex, __file__, "userManagementChangePassword")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
    return response
Пример #12
0
def adminServletRestoreData(request, response):
    """
    This function...

    @param {Request} request, the request object
    @param {Response} response, the response object
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        #****************************************************************
        # Step 1.GET THE SPECIE CODE AND THE UPDATE OPTION
        #****************************************************************
        formFields = request.form

        from subprocess import check_output, CalledProcessError, STDOUT

        logging.info("STARTING DBManager Restore PROCESS.")
        scriptArgs = [
            ROOT_DIRECTORY + "AdminTools/DBManager.py", "restore",
            "--remove=1", "--force=1"
        ]
        try:
            check_output(scriptArgs, stderr=STDOUT)
        except CalledProcessError as exc:
            raise Exception(
                "Error while calling DBManager Restore: Exit status " +
                str(exc.returncode) + ". Error message: " + exc.output)
        logging.info("FINISHED DBManager Restore PROCESS.")

        response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "adminServletRestoreData")

    finally:
        return response
Пример #13
0
def adminServletGetAllUsers(request, response):
    """
    This function obtains a list of all the users registered in the system including different details
    such as the used space, the registration date, etc.

    @param {Request} request, the request object
    @param {Response} response, the response object
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        #****************************************************************
        # Step 1. GET THE LIST OF ALL USERS
        #****************************************************************
        logging.info("STEP1 - GET THE LIST OF ALL USERS...")
        userList = UserDAO().findAll()
        for userInstance in userList:
            userInstance.usedSpace = 0
            if os_path.isdir(CLIENT_TMP_DIR + str(userInstance.getUserId())):
                userInstance.usedSpace = dir_total_size(
                    CLIENT_TMP_DIR + str(userInstance.getUserId()))

        response.setContent({
            "success": True,
            "userList": userList,
            "availableSpace": MAX_CLIENT_SPACE,
            "max_jobs_days": MAX_JOB_DAYS,
            "max_guest_days": MAX_GUEST_DAYS
        })

    except Exception as ex:
        handleException(response, ex, __file__, "adminServletGetAllUsers")

    finally:
        return response
Пример #14
0
def dataManagementGetMyJobs(request, response):
    #VARIABLE DECLARATION
    jobInstance = None
    jobInstances = []
    daoInstance = None
    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)

        if (userID is None):
            response.setContent({
                "success":
                False,
                "errorMessage":
                "Log in required</br>Sorry but the feature you are requesting is only available to registered accounts."
            })
        else:
            #****************************************************************
            # Step 2.GET THE LIST OF JOBS FOR GIVEN USER
            #****************************************************************
            logging.info("STEP1 - GET MY JOB LIST REQUEST RECEIVED")
            daoInstance = JobDAO()
            matchedFiles = daoInstance.findAll(otherParams={"userID": userID})
            logging.info("STEP1 - GET MY JOB LIST REQUEST RECEIVED...DONE")

            response.setContent({"success": True, "jobList": matchedFiles})

    except Exception as ex:
        handleException(response, ex, __file__, "dataManagementGetMyJobs")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #15
0
def dataManagementDeleteFile(request,
                             response,
                             DESTINATION_DIR,
                             MAX_CLIENT_SPACE,
                             isReference=False,
                             fileName=None):
    #VARIABLE DECLARATION
    daoInstance = None
    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = request.cookies.get('userID')
        userName = request.cookies.get('userName')
        sessionToken = request.cookies.get('sessionToken')

        if (userID is None):
            response.setContent({
                "success":
                False,
                "errorMessage":
                "Log in required</br>Sorry but the feature you are requesting is only available to registered accounts."
            })
        else:
            UserSessionManager().isValidUser(userID, sessionToken)

            #ONLY ADMIN USER (id=0) CAN UPLOAD NEW INBUILT GTF FILES
            if (isReference and UserSessionManager().isValidAdminUser(
                    userID, userName, sessionToken)):
                userID = "-1"

            if not isReference:
                DESTINATION_DIR += userID + "/inputData/"
            else:
                userID = "-1"
                DESTINATION_DIR += "GTF/"

            #****************************************************************
            # Step 1. GET THE LIST OF JOB IDs
            #****************************************************************
            if fileName == None:
                fileName = request.form.get("fileName")
            files = fileName.split(",")

            #****************************************************************
            # Step 2. DELETE EACH FILE
            #****************************************************************
            daoInstance = FileDAO()

            for fileName in files:
                #****************************************************************
                # Step 2.1.DELETE THE GIVEN FILE FROM DATABASE
                #****************************************************************
                logging.info("STEP1 - REMOVING " + fileName +
                             " FROM DATABASE...")
                daoInstance.remove(fileName, otherParams={"userID": userID})
                logging.info("STEP1 - REMOVING " + fileName +
                             " FROM DATABASE...DONE")

                #****************************************************************
                # Step 2.2.DELETE THE GIVEN FILE FROM DIRECTORY
                #****************************************************************
                logging.info("STEP2 - REMOVING " + fileName +
                             " FROM USER DIRECTORY...")
                if os.path.isfile(DESTINATION_DIR + fileName):
                    os.remove(DESTINATION_DIR + fileName)
                    logging.info("STEP2 - REMOVING " + fileName +
                                 " FROM USER DIRECTORY...DONE")
                else:
                    logging.info("STEP2 - REMOVING " + fileName +
                                 " FROM USER DIRECTORY...FILE NOT FOUND")

            response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "dataManagementDeleteFile")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #16
0
def dataManagementDeleteJob(request, response):
    #VARIABLE DECLARATION
    daoInstance = None
    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 LIST OF JOB IDs
        #****************************************************************.
        jobID = request.form.get("jobID")
        jobs = jobID.split(",")

        #****************************************************************
        # Step 2. DELETE EACH JOB
        #****************************************************************.
        daoInstance = JobDAO()
        userDirID = userID if userID is not None else "nologin"
        userDir = CLIENT_TMP_DIR + userDirID + "/jobsData/"
        tmpDir = CLIENT_TMP_DIR + userDirID + "/tmp/"

        for jobID in jobs:
            #****************************************************************
            # Step 2a. DELETE GIVEN JOB FROM DATABASE
            #****************************************************************
            logging.info("STEP1 - REMOVING " + jobID + " FROM DATABASE...")
            daoInstance.remove(jobID, otherParams={"userID": userID})
            logging.info("STEP1 - REMOVING " + jobID + " FROM DATABASE...DONE")

            #****************************************************************
            # Step 2b. DELETE GIVEN JOB FROM USER DIRECTORY
            #****************************************************************
            logging.info("STEP2 - REMOVING " + userDir + jobID +
                         " FROM USER DIRECTORY...")
            if os.path.isdir(userDir + jobID):
                shutil.rmtree(userDir + jobID)
                logging.info("STEP2 - REMOVING " + userDir + jobID +
                             " FROM USER DIRECTORY...DONE")
            else:
                logging.info("STEP2 - REMOVING " + userDir + jobID +
                             " FROM USER DIRECTORY...FILE NOT FOUND")

            logging.info("STEP2 - REMOVING TEMPORAL DIR " + tmpDir + jobID +
                         " FROM USER DIRECTORY...")
            if os.path.isdir(tmpDir + jobID):
                shutil.rmtree(tmpDir + jobID)
                logging.info("STEP2 - REMOVING TEMPORAL DIR " + tmpDir +
                             jobID + " FROM USER DIRECTORY...")
            else:
                logging.info("STEP2 - REMOVING TEMPORAL DIR " + tmpDir +
                             jobID + " FROM USER DIRECTORY...FILE NOT FOUND")

        response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "dataManagementDeleteJob")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #17
0
def dataManagementUploadFile(request,
                             response,
                             DESTINATION_DIR,
                             isReference=False):
    #VARIABLE DECLARATION
    fileInstance = None
    daoInstance = None
    try:
        #****************************************************************
        # Step 0.CHECK IF VALID USER SESSION
        #****************************************************************
        logging.info("STEP0 - CHECK IF VALID USER....")
        userID = request.cookies.get('userID')
        userName = request.cookies.get('userName')
        sessionToken = request.cookies.get('sessionToken')

        UserSessionManager().isValidUser(userID, sessionToken)

        #ONLY ADMIN USER (id=0) CAN UPLOAD NEW INBUILT GTF FILES
        if (isReference and UserSessionManager().isValidAdminUser(
                userID, userName, sessionToken)):
            userID = "-1"

        #****************************************************************
        #1. SAVE THE UPLOADED FILE TO THE USER DIRECTORY AND TO THE DATABASE
        #****************************************************************
        logging.info("STEP1 - FILE UPLOADING REQUEST RECEIVED")
        formFields = request.form
        uploadedFiles = request.files

        if not isReference:
            DESTINATION_DIR = DESTINATION_DIR + userID + "/inputData/"
        else:
            userID = "-1"
            DESTINATION_DIR = DESTINATION_DIR + "GTF/"

        logging.info("STEP1 - READING FILES....")
        fields = {}
        for field in formFields.keys():
            if formFields[field] == "undefined":
                continue
            fields[field] = formFields[field]

        if isReference and formFields.get("fileName", None) != None:
            registerFile(userID, formFields.get("fileName"), fields,
                         DESTINATION_DIR)
        else:
            for uploadedFileName in uploadedFiles.keys():
                if (uploadedFileName is not None):
                    #GET THE FILE OBJECT
                    uploadedFile = request.files.get(uploadedFileName)
                    uploadedFileName = uploadedFile.filename
                    saveFile(userID, uploadedFileName, fields, uploadedFile,
                             DESTINATION_DIR)

        response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "dataManagementUploadFile")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #18
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
Пример #19
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
Пример #20
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
Пример #21
0
def pathwayAcquisitionStep2_PART1(REQUEST, RESPONSE, QUEUE_INSTANCE,
                                  ROOT_DIRECTORY):
    """
    This function corresponds to FIRST PART of the SECOND 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 summary for the steps in the process:
        Step 0. VARIABLE DECLARATION
        Step 1. CHECK IF VALID USER SESSION
        Step 2. LOAD THE INSTANCE OF JOB
        Step 3. QUEUE THE JOB INSTANCE
        Step 4. RETURN THE JOB ID

    @param {Request} REQUEST
    @param {Response} RESPONSE
    @param {RQ QUEUE} QUEUE_INSTANCE
    @returns Response
    """
    #****************************************************************
    #Step 0. VARIABLE DECLARATION
    #The following variables are defined:
    #  - jobID: the ID for the job instance
    #  - userID: the ID for the user
    #****************************************************************
    jobID = ""
    userID = ""

    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 INSTANCE OF JOB
        #****************************************************************
        formFields = REQUEST.form
        jobID = formFields.get("jobID")
        selectedCompounds = REQUEST.form.getlist("selectedCompounds[]")
        # Retrieve the number of cluster on a per omic basis
        # Note: this will contain the omic name transformed to remove spaces and special chars
        clusterNumber = {
            key.replace("clusterNumber:", ""): value
            for key, value in formFields.iteritems()
            if key.startswith("clusterNumber:")
        }

        #************************************************************************
        # Step 3. Queue job
        #************************************************************************
        QUEUE_INSTANCE.enqueue(fn=pathwayAcquisitionStep2_PART2,
                               args=(
                                   jobID,
                                   userID,
                                   selectedCompounds,
                                   clusterNumber,
                                   RESPONSE,
                                   ROOT_DIRECTORY,
                               ),
                               timeout=600,
                               job_id=jobID)

        #************************************************************************
        # Step 5. Return the Job ID
        #************************************************************************
        RESPONSE.setContent({"success": True, "jobID": jobID})

    except Exception as ex:
        handleException(RESPONSE,
                        ex,
                        __file__,
                        "pathwayAcquisitionStep2_PART1",
                        userID=userID)
    finally:
        return RESPONSE
Пример #22
0
def dataManagementDownloadFile(request, response):
    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.READ PARAMS
        #****************************************************************
        fileName = request.args.get("fileName", "")
        fileType = request.args.get("fileType", "")
        jobID = request.args.get("jobID", "")
        serve = (request.args.get("serve", "").lower() == "true")
        offset = int(request.args.get("offset", 0))

        #send_from_directory(self.FILES_SETTINGS.ROOT_DIRECTORY + 'public_html', filename)

        #****************************************************************
        # Step 2.GENERATE THE PATH TO FILE
        #****************************************************************
        logging.info("STEP1 - GET FILE REQUEST RECEIVED")

        userDirID = userID if userID is not None else "nologin"

        if fileType == "job_result":
            userDir = "/jobsData/" + jobID + "/output/"
        elif fileType == "input":
            userDir = "/inputData/"
        else:
            userDir = "/tmp/" + jobID

        userDir = CLIENT_TMP_DIR + userDirID + userDir

        file_path = "{path}/{file}".format(path=userDir, file=fileName)

        if os.path.isfile(file_path):
            #IF THE REQUEST WANTS THE FILE IN A STREAM
            if serve == True:
                #TODO: HACER ESTO<- http://flask.pocoo.org/docs/0.10/patterns/streaming/
                def generate():
                    with open(file_path) as f:
                        lines = f.readlines()
                        first = min(len(lines), offset)
                        last = min(len(lines), offset + 51)
                        lines = lines[first:last]
                        for row in lines:
                            yield row.rstrip() + "\n"
                    f.close()

                from flask import Response
                return Response(generate(), mimetype='text/plain')
                #response.imetype='text/plain')
            else:
                return send_from_directory(userDir,
                                           fileName,
                                           as_attachment=True,
                                           attachment_filename=fileName)
        else:
            response.setContent({
                "success":
                False,
                "errorMessage":
                "File not found.</br>Sorry but it looks like the requested file was removed from system."
            })
            return response
    except Exception as ex:
        handleException(response, ex, __file__, "dataManagementDownloadFile")
        return response
Пример #23
0
def userManagementNewGuestSession(request, response):
    #VARIABLE DECLARATION
    userInstance = None
    daoInstance = None

    try:
        #****************************************************************
        # Step 1.GENERATE RANDOM PASSWORD AND A RANDOM EMAIL FOR GUEST USER
        #****************************************************************
        logging.info("STEP1 - GETTING RANDOM PASS AND USER...")

        password = getRandowWord(6)  #GENERATE A RANDOM PASSWORD USING A WORD

        daoInstance = UserDAO()
        valid = False
        userName = ""
        from random import randrange
        while valid == False:
            userName = "******" + str(randrange(99999))
            valid = daoInstance.findByEmail(userName +
                                            "@paintomics.org") == None

        #****************************************************************
        # Step 2. ADD NEW USER TO DATABASE
        #****************************************************************
        logging.info(
            "STEP2 - CREATING USER INSTANCE AND SAVING TO DATABASE...")
        userInstance = User("")
        userInstance.setEmail(userName + "@paintomics.org")
        from hashlib import sha1
        userInstance.setPassword(sha1(password.encode('ascii')).hexdigest())
        userInstance.setUserName(userName)
        userInstance.setAffiliation("GUEST USER")
        #Update the last login date at the database
        from time import strftime
        today = strftime("%Y%m%d")
        userInstance.setCreationDate(today)
        userInstance.setLastLogin(today)
        userInstance.setIsGuest(True)

        userID = daoInstance.insert(userInstance)

        #****************************************************************
        # Step 3. Create user directories
        #****************************************************************
        logging.info("STEP3 - INITIALIZING DIRECTORIES...")
        initializeUserDirectories(str(userID))

        #****************************************************************
        # Step 4. Create new session
        #****************************************************************
        logging.info("STEP4 - GETTING A NEW SESSION TOKEN...")
        sessionToken = UserSessionManager().registerNewUser("" + str(userID))

        response.setContent({
            "success": True,
            "userID": userID,
            "userName": userInstance.getUserName(),
            "sessionToken": sessionToken,
            "p": password
        })

    except Exception as ex:
        handleException(response, ex, __file__,
                        "userManagementNewGuestSession")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #24
0
def userManagementSignIn(request, response):
    #VARIABLE DECLARATION
    userInstance = None
    daoInstance = None

    try:
        #****************************************************************
        # Step 1.READ PARAMS AND CHECK IF USER ALREADY EXISTS
        #****************************************************************
        logging.info("STEP1 - READ PARAMS AND CHECK IF USER ALREADY EXISTS...")
        formFields = request.form
        email = formFields.get("email")
        password = formFields.get("password")
        from hashlib import sha1
        password = sha1(password.encode('ascii')).hexdigest()

        daoInstance = UserDAO()
        userInstance = daoInstance.findByEmail(email, {"password": password})

        if userInstance == None:
            raise CredentialException(
                "The email or password you entered is incorrect.")
        #TODO: LINK PARA ACTIVAR CUENTAS
        # elif userInstance.isActivated() == False:
        #     raise CredentialException("Account not activated, please check your email inbox and follow the instructions for account activation.")

        logging.info(
            "STEP1 - READ PARAMS AND CHECK IF USER ALREADY EXISTS...OK USER EXISTS"
        )
        #****************************************************************
        # Step 2. REGISTER NEW SESSION
        #****************************************************************
        logging.info("STEP2 - GETTING A NEW SESSION TOKEN...")
        sessionToken = UserSessionManager().registerNewUser(
            userInstance.getUserId())

        #Update the last login date at the database
        from time import strftime
        today = strftime("%Y%m%d")
        userInstance.setLastLogin(today)
        daoInstance.update(userInstance, {"fieldList": ["last_login"]})
        logging.info("STEP2 - GETTING A NEW SESSION TOKEN...DONE")

        #****************************************************************
        # Step 3. GET INIT SESSION MESSAGE
        #****************************************************************
        logging.info("STEP2 - GETTING NEW SESSION MESSAGE...")
        daoInstance = MessageDAO()
        loginMessage = daoInstance.findByType(message_type="login_message")

        response.setContent({
            "success": True,
            "userID": userInstance.getUserId(),
            "userName": userInstance.getUserName(),
            "sessionToken": sessionToken,
            "loginMessage": loginMessage
        })

    except CredentialException as ex:
        handleException(response, ex, __file__, "userManagementSignIn", 200)
    except Exception as ex:
        handleException(response, ex, __file__, "userManagementSignIn")
    finally:
        if (daoInstance != None):
            daoInstance.closeConnection()
        return response
Пример #25
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
Пример #26
0
def adminServletGetInstalledOrganisms(request, response):
    """
    This function...

    @param {Request} request, the request object
    @param {Response} response, the response object
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        #****************************************************************
        # Step 1.GET THE LIST OF INSTALLED SPECIES (DATABASES and SPECIES.JSON)
        #****************************************************************
        organisms_names = {}
        with open(KEGG_DATA_DIR +
                  'current/common/organisms_all.list') as organisms_all:
            reader = csv.reader(organisms_all, delimiter='\t')
            for row in reader:
                organisms_names[row[1]] = row[2]
        organisms_all.close()

        installedSpecies = []
        from pymongo import MongoClient

        client = MongoClient(MONGODB_HOST, MONGODB_PORT)
        databases = client.database_names()

        #****************************************************************
        # Step 2.FOR EACH INSTALLED DATABASE GET THE INFORMATION
        #****************************************************************
        databaseList = []
        common_info_date = ""

        for database in databases:
            if not "-paintomics" in database:
                continue
            elif "global-paintomics" == database:
                db = client[database]
                common_info_date = db.versions.find({"name":
                                                     "COMMON"})[0].get("date")

                # Step 2.4 Check if the organism has non installed data available
                if os_path.isfile(KEGG_DATA_DIR + 'download/common/VERSION'):
                    downloaded = True
                elif os_path.isfile(KEGG_DATA_DIR +
                                    'download/common/DOWNLOADING'):
                    downloaded = "downloading"
                else:
                    downloaded = False
                    #Erroneous download not removed --> remove
                    if os_path.isdir(KEGG_DATA_DIR + 'download/common/'):
                        shutil_rmtree(KEGG_DATA_DIR + 'download/common/')

                databaseList.append({
                    "organism_name": "Common KEGG data",
                    "organism_code": "common",
                    "kegg_date": common_info_date,
                    "downloaded": downloaded
                })

            else:
                # Step 2.1 GET THE SPECIE CODE
                organism_code = database.replace("-paintomics", "")
                # Step 2.2 GET THE SPECIE NAME
                organism_name = organisms_names.get(organism_code,
                                                    "Unknown specie")

                # Step 2.3 GET THE SPECIE VERSIONS
                db = client[database]
                kegg_date = db.versions.find({"name": "KEGG"})[0].get("date")
                mapping_date = db.versions.find({"name":
                                                 "MAPPING"})[0].get("date")
                acceptedIDs = db.versions.find({"name": "ACCEPTED_IDS"})

                if acceptedIDs.count() > 0:
                    acceptedIDs = acceptedIDs[0].get("ids")
                else:
                    acceptedIDs = ""

                # Step 2.4 Check if the organism has non installed data available
                if os_path.isfile(KEGG_DATA_DIR + 'download/' + organism_code +
                                  '/VERSION'):
                    downloaded = True
                elif os_path.isfile(KEGG_DATA_DIR + 'download/' +
                                    organism_code + '/DOWNLOADING'):
                    downloaded = "downloading"
                else:
                    downloaded = False
                    #Erroneous download not removed --> remove
                    if os_path.isdir(KEGG_DATA_DIR + 'download/' +
                                     organism_code):
                        shutil_rmtree(KEGG_DATA_DIR + 'download/' +
                                      organism_code)

                databaseList.append({
                    "organism_name": organism_name,
                    "organism_code": organism_code,
                    "kegg_date": kegg_date,
                    "mapping_date": mapping_date,
                    "acceptedIDs": acceptedIDs,
                    "downloaded": downloaded
                })

        client.close()

        response.setContent({
            "common_info_date": common_info_date,
            "databaseList": databaseList
        })

    except Exception as ex:
        handleException(response, ex, __file__,
                        "adminServletGetInstalledOrganisms")

    finally:
        return response
Пример #27
0
def adminServletGetAvailableOrganisms(request, response):
    """
    This function...

    @param {Request} request, the request object
    @param {Response} response, the response object
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        #****************************************************************
        # Step 1.GET THE LIST OF INSTALLED SPECIES (DATABASES and SPECIES.JSON)
        #****************************************************************
        import csv
        databaseList = []
        with open(KEGG_DATA_DIR +
                  'current/common/organisms_all.list') as availableSpeciesFile:
            reader = csv.reader(availableSpeciesFile, delimiter='\t')
            for row in reader:
                organism_code = row[1]
                # Step 2.4 Check if the organism has non installed data available
                if os_path.isfile(KEGG_DATA_DIR + 'download/' + organism_code +
                                  '/VERSION'):
                    downloaded = True
                elif os_path.isfile(KEGG_DATA_DIR + 'download/' +
                                    organism_code + '/DOWNLOADING'):
                    downloaded = "downloading"
                else:
                    downloaded = False
                    #Erroneous download not removed --> remove
                    if os_path.isdir(KEGG_DATA_DIR + 'download/' +
                                     organism_code):
                        shutil_rmtree(KEGG_DATA_DIR + 'download/' +
                                      organism_code)

                databaseList.append({
                    "organism_name": row[2],
                    "organism_code": organism_code,
                    "categories": row[3].split(";"),
                    "organism_id": row[0],
                    "downloaded": downloaded
                })
        availableSpeciesFile.close()

        response.setContent({
            "databaseList":
            databaseList,
            "download_log":
            KEGG_DATA_DIR + "download/download.log",
            "install_log":
            KEGG_DATA_DIR + "current/install.log"
        })

    except Exception as ex:
        handleException(response, ex, __file__,
                        "adminServletGetInstalledOrganisms")

    finally:
        return response
Пример #28
0
def adminServletInstallOrganism(request, response, organism_code,
                                ROOT_DIRECTORY):
    """
    This function manages an 'Install/Update Organism' request by calling to the
    DBManager tool.

    @param {Request} request, the request object
    @param {Response} response, the response object
    @param {String} organism_code,
    @param {String} ROOT_DIRECTORY,
    """
    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')
        userName = request.cookies.get('userName')
        UserSessionManager().isValidAdminUser(userID, userName, sessionToken)

        #****************************************************************
        # Step 1.GET THE SPECIE CODE AND THE UPDATE OPTION
        #****************************************************************
        download = json.loads(request.data).get("download")
        update_kegg = 1
        update_mapping = 1
        common = 0

        if organism_code == "common":
            common = 1
            organism_code = "#common"

        from subprocess import check_output, CalledProcessError, STDOUT

        #****************************************************************
        # Step 2a. IF THE SELECTED OPTION IS DOWNLOAD
        #****************************************************************
        if download:
            logging.info("STARTING DBManager download PROCESS.")
            scriptArgs = [
                ROOT_DIRECTORY + "AdminTools/DBManager.py", "download",
                "--specie=" + organism_code, "--kegg=" + str(update_kegg),
                "--mapping=" + str(update_mapping), "--common=" + str(common)
            ]
            try:
                check_output(scriptArgs, stderr=STDOUT)
            except CalledProcessError as exc:
                raise Exception(
                    "Error while calling DBManager download: Exit status " +
                    str(exc.returncode) + ". Error message: " + exc.output)
            logging.info("FINISHED DBManager Download PROCESS.")

        # ****************************************************************
        # Step 2B. IF THE SELECTED OPTION IS INSTALL
        # ****************************************************************
        else:
            logging.info("STARTING DBManager Install PROCESS.")
            scriptArgs = [
                ROOT_DIRECTORY + "AdminTools/DBManager.py", "install",
                "--specie=" + organism_code, "--common=" + str(common)
            ]
            try:
                check_output(scriptArgs, stderr=STDOUT)
            except CalledProcessError as exc:
                raise Exception(
                    "Error while calling DBManager Install: Exit status " +
                    str(exc.returncode) + ". Error message: " + exc.output)
            logging.info("FINISHED DBManager Install PROCESS.")

        response.setContent({"success": True})

    except Exception as ex:
        handleException(response, ex, __file__, "adminServletUpdateOrganism")

    finally:
        return response