def run_analysis_preprocessing(analysis):
    '''perform preprocessing (innermost task, does the actual work)

    '''
    logger.debug("analysis_manager.run_analysis_preprocessing called")

    connection = analysis.get_galaxy_connection()

    # creates new library in galaxy
    try:
        library_id = connection.create_library("{} Analysis - {} ({})".format(
            Site.objects.get_current().name, analysis.uuid, datetime.now()))
    except RuntimeError as exc:
        error_msg = (
            "Pre-processing failed: " +
            "error creating Galaxy library for analysis '{}': {}").format(
                analysis.name, exc.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        return

    ### generates same ret_list purely based on analysis object ###
    ret_list = get_analysis_config(analysis)

    # getting expanded workflow configured based on input: ret_list
    try:
        new_workflow, history_download, analysis_node_connections = \
            configure_workflow(analysis.workflow, ret_list, connection)
    except RuntimeError as exc:
        error_msg = (
            "Pre-processing failed: " +
            "error configuring Galaxy workflow for analysis '{}': {}").format(
                analysis.name, exc.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        if not isinstance(exc, (ConnectionError, TimeoutError, AuthError)):
            try:
                analysis.delete_galaxy_library()
            except RuntimeError:
                logger.error("Cleanup failed for analysis '{}'".format(
                    analysis.name))
        return

    # import connections into database
    for analysis_node_connection in analysis_node_connections:
        # lookup node object
        if (analysis_node_connection["node_uuid"]):
            node = Node.objects.get(uuid=analysis_node_connection["node_uuid"])
        else:
            node = None
        AnalysisNodeConnection.objects.create(
            analysis=analysis,
            subanalysis=analysis_node_connection['subanalysis'],
            node=node,
            step=int(analysis_node_connection['step']),
            name=analysis_node_connection['name'],
            filename=analysis_node_connection['filename'],
            filetype=analysis_node_connection['filetype'],
            direction=analysis_node_connection['direction'],
            is_refinery_file=analysis_node_connection['is_refinery_file'])
    # saving ouputs of workflow to download
    for file_dl in history_download:
        temp_dl = WorkflowFilesDL(step_id=file_dl['step_id'],
                                  pair_id=file_dl['pair_id'],
                                  filename=file_dl['name'])
        temp_dl.save()
        analysis.workflow_dl_files.add(temp_dl)
        analysis.save()

    # import newly generated workflow
    try:
        new_workflow_info = connection.import_workflow(new_workflow)
    except RuntimeError as exc:
        error_msg = (
            "Pre-processing failed: " +
            "error importing workflow into Galaxy for analysis '{}': {}"
        ).format(analysis.name, exc.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        if not isinstance(exc, (ConnectionError, TimeoutError, AuthError)):
            try:
                analysis.delete_galaxy_library()
            except RuntimeError:
                logger.error("Cleanup failed for analysis '{}'".format(
                    analysis.name))
        return

    ######### ANALYSIS MODEL
    # getting number of steps for current workflow
    new_workflow_steps = countWorkflowSteps(new_workflow)

    # creates new history in galaxy
    try:
        history_id = connection.create_history("{} Analysis - {} ({})".format(
            Site.objects.get_current().name, analysis.uuid, datetime.now()))
    except RuntimeError as e:
        error_msg = "Pre-processing failed: " + \
                    "error creating Galaxy history for analysis '{}': {}" \
                    .format(analysis.name, e.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        if not isinstance(exc, (ConnectionError, TimeoutError, AuthError)):
            try:
                analysis.delete_galaxy_library()
                analysis.delete_galaxy_workflow()
            except RuntimeError:
                logger.error("Cleanup failed for analysis '{}'".format(
                    analysis.name))
        return

    # updating analysis object
    analysis.workflow_copy = new_workflow
    analysis.workflow_steps_num = new_workflow_steps
    analysis.workflow_galaxy_id = new_workflow_info['id']
    analysis.library_id = library_id
    analysis.history_id = history_id
    analysis.save()
    return
def run_analysis_preprocessing(analysis):
    """perform preprocessing (innermost task, does the actual work)"""
    logger.debug("analysis_manager.run_analysis_preprocessing called")

    connection = analysis.galaxy_connection()
    error_msg = "Pre-processing failed: "

    # creates new library in galaxy
    library_name = "{} Analysis - {} ({})".format(
        Site.objects.get_current().name, analysis.uuid, datetime.now())
    try:
        library_id = connection.libraries.create_library(library_name)['id']
    except galaxy.client.ConnectionError as exc:
        error_msg += "error creating Galaxy library for analysis '%s': %s"
        logger.error(error_msg, analysis.name, exc.message)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        return

    # generates same ret_list purely based on analysis object ###
    ret_list = get_analysis_config(analysis)
    try:
        workflow_dict = connection.workflows.export_workflow_json(
            analysis.workflow.internal_id)
    except galaxy.client.ConnectionError as exc:
        error_msg += "error downloading Galaxy workflow for analysis '%s': %s"
        logger.error(error_msg, analysis.name, exc.message)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        analysis.cleanup()
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        return

    # getting expanded workflow configured based on input: ret_list
    new_workflow, history_download, analysis_node_connections = \
        configure_workflow(workflow_dict, ret_list)

    # import connections into database
    for analysis_node_connection in analysis_node_connections:
        # lookup node object
        if analysis_node_connection["node_uuid"]:
            node = Node.objects.get(uuid=analysis_node_connection["node_uuid"])
        else:
            node = None
        AnalysisNodeConnection.objects.create(
            analysis=analysis,
            subanalysis=analysis_node_connection['subanalysis'],
            node=node,
            step=int(analysis_node_connection['step']),
            name=analysis_node_connection['name'],
            filename=analysis_node_connection['filename'],
            filetype=analysis_node_connection['filetype'],
            direction=analysis_node_connection['direction'],
            is_refinery_file=analysis_node_connection['is_refinery_file']
            )
    # saving outputs of workflow to download
    for file_dl in history_download:
        temp_dl = WorkflowFilesDL(step_id=file_dl['step_id'],
                                  pair_id=file_dl['pair_id'],
                                  filename=file_dl['name'])
        temp_dl.save()
        analysis.workflow_dl_files.add(temp_dl)
        analysis.save()

    # import newly generated workflow
    try:
        new_workflow_info = connection.workflows.import_workflow_json(
            new_workflow)
    except galaxy.client.ConnectionError as exc:
        error_msg += "error importing workflow into Galaxy for "
        error_msg += "analysis '%s': %s"
        logger.error(error_msg, analysis.name, exc.message)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        analysis.cleanup()
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        return

    # getting number of steps for current workflow
    new_workflow_steps = countWorkflowSteps(new_workflow)

    # creates new history in galaxy
    history_name = "{} Analysis - {} ({})".format(
        Site.objects.get_current().name, analysis.uuid, datetime.now())
    try:
        history_id = connection.histories.create_history(history_name)['id']
    except galaxy.client.ConnectionError as e:
        error_msg += "error creating Galaxy history for analysis '%s': %s"
        logger.error(error_msg, analysis.name, e.message)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        analysis.cleanup()
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        return

    # updating analysis object
    analysis.workflow_copy = new_workflow
    analysis.workflow_steps_num = new_workflow_steps
    analysis.workflow_galaxy_id = new_workflow_info['id']
    analysis.library_id = library_id
    analysis.history_id = history_id
    analysis.save()
    return
def run_analysis_preprocessing(analysis):
    '''perform preprocessing (innermost task, does the actual work)

    '''
    logger.debug("analysis_manager.run_analysis_preprocessing called")

    connection = analysis.get_galaxy_connection()

    # creates new library in galaxy
    try:
        library_id = connection.create_library(
            "{} Analysis - {} ({})".format(
                Site.objects.get_current().name, analysis.uuid, datetime.now())
            )
    except RuntimeError as exc:
        error_msg = (
            "Pre-processing failed: " +
            "error creating Galaxy library for analysis '{}': {}"
            ).format(analysis.name, exc.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        return

    ### generates same ret_list purely based on analysis object ###
    ret_list = get_analysis_config(analysis)

    # getting expanded workflow configured based on input: ret_list
    try:
        new_workflow, history_download, analysis_node_connections = \
            configure_workflow(analysis.workflow, ret_list, connection)
    except RuntimeError as exc:
        error_msg = (
            "Pre-processing failed: " +
            "error configuring Galaxy workflow for analysis '{}': {}"
            ).format(analysis.name, exc.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        if not isinstance(exc, (ConnectionError, TimeoutError, AuthError)):
            try:
                analysis.delete_galaxy_library()
            except RuntimeError:
                logger.error(
                    "Cleanup failed for analysis '{}'".format(analysis.name))
        return

    # import connections into database
    for analysis_node_connection in analysis_node_connections:
        # lookup node object
        if (analysis_node_connection["node_uuid"]):
            node = Node.objects.get(uuid=analysis_node_connection["node_uuid"])
        else:
            node = None
        AnalysisNodeConnection.objects.create(
            analysis=analysis,
            subanalysis=analysis_node_connection['subanalysis'],
            node=node,
            step=int(analysis_node_connection['step']),
            name=analysis_node_connection['name'],
            filename=analysis_node_connection['filename'],
            filetype=analysis_node_connection['filetype'],
            direction=analysis_node_connection['direction'],
            is_refinery_file=analysis_node_connection['is_refinery_file']
            )
    # saving ouputs of workflow to download
    for file_dl in history_download:
        temp_dl = WorkflowFilesDL(step_id=file_dl['step_id'],
                                  pair_id=file_dl['pair_id'],
                                  filename=file_dl['name'])
        temp_dl.save()
        analysis.workflow_dl_files.add( temp_dl ) 
        analysis.save()

    # import newly generated workflow
    try:
        new_workflow_info = connection.import_workflow(new_workflow);
    except RuntimeError as exc:
        error_msg = (
            "Pre-processing failed: " +
            "error importing workflow into Galaxy for analysis '{}': {}"
            ).format(analysis.name, exc.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        if not isinstance(exc, (ConnectionError, TimeoutError, AuthError)):
            try:
                analysis.delete_galaxy_library()
            except RuntimeError:
                logger.error(
                    "Cleanup failed for analysis '{}'".format(analysis.name))
        return

    ######### ANALYSIS MODEL 
    # getting number of steps for current workflow
    new_workflow_steps = countWorkflowSteps(new_workflow)

    # creates new history in galaxy
    try:
        history_id = connection.create_history(
            "{} Analysis - {} ({})".format(
                Site.objects.get_current().name, analysis.uuid, datetime.now())
            )
    except RuntimeError as e:
        error_msg = "Pre-processing failed: " + \
                    "error creating Galaxy history for analysis '{}': {}" \
                    .format(analysis.name, e.message)
        logger.error(error_msg)
        analysis.set_status(Analysis.FAILURE_STATUS, error_msg)
        run_analysis_preprocessing.update_state(state=celery.states.FAILURE)
        if not isinstance(exc, (ConnectionError, TimeoutError, AuthError)):
            try:
                analysis.delete_galaxy_library()
                analysis.delete_galaxy_workflow()
            except RuntimeError:
                logger.error(
                    "Cleanup failed for analysis '{}'".format(analysis.name))
        return

    # updating analysis object
    analysis.workflow_copy = new_workflow
    analysis.workflow_steps_num = new_workflow_steps
    analysis.workflow_galaxy_id = new_workflow_info['id']
    analysis.library_id = library_id
    analysis.history_id = history_id
    analysis.save()
    return