Exemplo n.º 1
0
def update_configuration(config_id, new_config):
    """
    Update the configuration with id config_id with values stored in new_config.

    Args:
        config_id (int): Id of configuration to update.
        new_config (dict): A dictionary containing updated values.
        
    Returns:
        updated_config(Configuration): the updated configuration.

    """
    session = connection.Session()
    try:
        my_config = session.query(Configuration).filter(
            Configuration.config_id == config_id).first()
        updated_config = merge_configurations([my_config.config, new_config])
        my_config.config = updated_config
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()
    
    return my_config
Exemplo n.º 2
0
def set_ready(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_READY.
    Only possible if the current status is not RUN_STATUS_IN_PROGRESS.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status == RUN_STATUS_IN_PROGRESS:
        msg = "Attempting to put an In Progress run into Ready state, runID: {}".\
            format(my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:

        session = connection.Session()
        try:
            my_run.run_status = RUN_STATUS_READY
            now = datetime.datetime.now()
            my_run.last_updated_date = now

            session.add(my_run)
            session.commit()
        except:
            session.rollback()
            raise
        finally:
            session.close()
            connection.engine.dispose()
Exemplo n.º 3
0
def set_scheduled(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_SCHEDULED.
    Only possible if the current status is RUN_STATUS_READY.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_READY:
        msg = "Wrong run status - {}, Only a Ready run can be Scheduled, runID: {}".\
            format(my_run.run_status, my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:

        session = connection.Session()

        my_run.run_status = RUN_STATUS_SCHEDULED
        now = datetime.datetime.now()
        my_run.last_updated_date = now

        session.add(my_run)
        session.commit()
        session.close()
        connection.engine.dispose()
Exemplo n.º 4
0
def set_in_progress(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_IN_PROGRESS.
    Only possible if the current status is RUN_STATUS_SCHEDULED.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_SCHEDULED:
        msg = "Wrong run status - {}, Only a Scheduled run can be put In Progress, runID: {}".\
            format(my_run.run_status, my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:
        session = connection.Session()
        try:
            my_run.run_status = RUN_STATUS_IN_PROGRESS
            now = datetime.datetime.now()
            my_run.last_updated_date = now
            my_run.run_start_date = now

            session.add(my_run)
            session.commit()
        except:
            session.rollback()
            raise
        finally:
            session.close()
            connection.engine.dispose()
Exemplo n.º 5
0
def get_effective_configuration(analysis_run_id):
    """
    Given an analysis run return its effective configuration.
    The effective configuration consists of a merged set of
    workflow, analysis, and analysis_run configurations

    Args:
        analysis_run_id (int): Id to use for analysis run lookup.

    Returns:
        my_config (str): The effective configuration of this analysis run.

    """
    session = connection.Session()

    run_config = aliased(Configuration)
    analysis_config = aliased(Configuration)
    workflow_config = aliased(Configuration)

    my_configs = session.query(AnalysisRun.analysis_run_id, run_config.config.label("run_config"),
                               analysis_config.config.label("analysis_config"), 
                               workflow_config.config.label("workflow_config")).\
        join(Analysis, AnalysisRun.analysis_id == Analysis.analysis_id).\
        join(Workflow, AnalysisRun.workflow_id == Workflow.workflow_id).\
        outerjoin(run_config, AnalysisRun.config_id == run_config.config_id).\
        outerjoin(analysis_config, Analysis.config_id == analysis_config.config_id).\
        outerjoin(workflow_config, Workflow.config_id == workflow_config.config_id).\
        filter(AnalysisRun.analysis_run_id == analysis_run_id).first()
    config_list = [my_configs.workflow_config,
                   my_configs.analysis_config, my_configs.run_config]

    session.close()
    connection.engine.dispose()

    return merge_configurations(config_list)
Exemplo n.º 6
0
def create_analysis(analysis_name, start_date, config_id):
    """
    Create an Analysis and store in the database.

    Args:
        analysis_name (str): Name of the analysis
        start_date (datetime.datetime): Start date of the analysis
        config_id (uuid): Id of the corresponding Configuration

    Returns:
        my_analysis (Analysis): The newly created analysis.
    """

    session = connection.Session()
    try:
        my_analysis = Analysis()
        my_analysis.analysis_name = analysis_name
        my_analysis.start_date = start_date
        my_analysis.config_id = config_id

        now = datetime.datetime.now()
        my_analysis.last_updated_date = now
        my_analysis.created_date = now

        session.add(my_analysis)
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()
    return my_analysis
Exemplo n.º 7
0
def set_configuration_for_analysis(analysis_id, config_id):
    """
    Set the configuration for analysis with ID analysis_id.

    Args:
        analysis_id (id): ID of analysis
        config_id (uuid): Id of the corresponding Configuration

    Returns:
        my_analysis (Analysis): The updated analysis.
    """
    session = connection.Session()
    try:
        my_analysis = session.query(Analysis).filter(
            Analysis.analysis_id == analysis_id).first()

        my_analysis.config_id = config_id

        now = datetime.datetime.now()
        my_analysis.last_updated_date = now

        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()

    return my_analysis
Exemplo n.º 8
0
def set_completed(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_COMPLETED.
    Only possible if the current status is RUN_STATUS_IN_PROGRESS.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_IN_PROGRESS:
        err_msg = "Wrong run status - {}, Only an In Progress run can be Finished, runID: {}"
        raise ValueError(
            err_msg.format(my_run.run_status, my_run.analysis_run_id))
    else:

        session = connection.Session()
        try:
            my_run.run_status = RUN_STATUS_COMPLETED

            now = datetime.datetime.now()
            my_run.last_updated_date = now
            my_run.run_end_date = now

            session.add(my_run)
            session.commit()
        except:
            session.rollback()
            raise
        finally:
            session.close()
            connection.engine.dispose()
Exemplo n.º 9
0
def set_configuration_for_workflow(workflow_id, config_id):
    """
    Set the configuration for workflow with ID workflow_id.

    Args:
        workflow_id (id): ID of workflow
        config_id (uuid): Id of the corresponding Configuration

    Returns:
        my_workflow (Workflow): The updated workflow.
    """
    session = connection.Session()

    try:
        my_workflow = session.query(Workflow).filter(
            Workflow.workflow_id == workflow_id).first()

        my_workflow.config_id = config_id

        now = datetime.datetime.now()
        my_workflow.last_updated_date = now

        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()

    return my_workflow
Exemplo n.º 10
0
def create_workflow(workflow_name, workflow_version, config_id):
    """
    Create a Workflow and store in the database.

    Args:
        workflow_name (str): Name of this workflow.
        workflow_version (str): Version of this workflow.
        config_id (str): Id of a configuration to associate with this workflow.


    Returns:
        my_workflow (Workflow): The newly created workflow.

    """
    session = connection.Session()
    try:

        my_workflow = Workflow()
        my_workflow.workflow_name = workflow_name
        my_workflow.workflow_version = workflow_version
        my_workflow.config_id = config_id

        now = datetime.datetime.now()
        my_workflow.last_updated_date = now
        my_workflow.created_date = now

        session.add(my_workflow)
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()

    return my_workflow
Exemplo n.º 11
0
def set_configuration_for_analysis_run(analysis_run_id, config_id):
    """
    Set the configuration for analysis run with ID analysis_run_id.

    Args:
        analysis_run_id (id): ID of analysis run
        config_id (uuid): Id of the corresponding Configuration

    Returns:
        my_analysis_run (AnalysisRun): The updated analysis run.
    """
    session = connection.Session()

    my_analysis_run = session.query(AnalysisRun).filter(
        AnalysisRun.analysis_run_id == analysis_run_id).first()

    my_analysis_run.config_id = config_id

    now = datetime.datetime.now()
    my_analysis_run.last_updated_date = now

    session.commit()
    session.close()
    connection.engine.dispose()

    return my_analysis_run
Exemplo n.º 12
0
def create_analysis_run(analysis_id, config_id, workflow_id):
    """
    Create an AnalysisRun and store in the database.

    Args:
        analysis_id (int): Id of the corresponding analysis
        config_id (uuid): Id of the corresponding Configuration
        workflow_id (int): Id of the corresponding workflow


    Returns:
        my_analysis_run (AnalysisRun): The newly created analysis run.
    """
    session = connection.Session()
    try:
        my_analysis_run = AnalysisRun()
        my_analysis_run.analysis_id = analysis_id
        my_analysis_run.workflow_id = workflow_id
        my_analysis_run.config_id = config_id
        my_analysis_run.run_status = RUN_STATUS_READY

        now = datetime.datetime.now()
        my_analysis_run.last_updated_date = now
        my_analysis_run.created_date = now

        session.add(my_analysis_run)
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()
    return my_analysis_run
Exemplo n.º 13
0
def get_number_of_runs_with_status(analysis_id, run_status):
    session = connection.Session()
    num_runs = session.query(AnalysisRun).filter(and_(
        AnalysisRun.analysis_id == analysis_id, AnalysisRun.run_status == run_status)).count()
    session.close()
    connection.engine.dispose()

    return num_runs
Exemplo n.º 14
0
def get_workflow_by_id(workflow_id):
    session = connection.Session()

    my_workflow = session.query(Workflow).filter(
        Workflow.workflow_id == workflow_id).first()
    session.close()
    connection.engine.dispose()

    return my_workflow
Exemplo n.º 15
0
def create_configuration(config_id, config):
    """
    Create a Configuration and store in the database.

    Args:
        config_id (uuid): Id to use for this configuration.
        config (str): String representation of the JSON configuration.


    Returns:
        my_config (Configuration): The newly created configuration.

    Raises:
        ValueError: If the configuration object is not in json format.
        If the configuration ID is not a valid UUID.
    """
    if is_uuid(config_id):
        if is_json(config):
            session = connection.Session()
            
            try:

                my_config = Configuration()
                my_config.config_id = config_id
                my_config.config = json.loads(config)
    
                now = datetime.datetime.now()
                my_config.last_updated_date = now
                my_config.created_date = now
    
                session.add(my_config)
                session.commit()
            except:
                session.rollback()
                raise
            finally:
                session.close()
                connection.engine.dispose()

        else:
            raise ValueError("Configuration object not in json format.")
    else:
        raise ValueError("Configuration ID not a uuid")

    return my_config
def get_available_samples(analysis_id, tissue_type, num_runs):

    #PCAWG Samples are in their own database
    Base = automap_base()
    sample_engine = create_engine(
        'postgresql://*****:*****@postgresql.service.consul:5432/pcawg_sample_tracking'
    )
    Base.prepare(sample_engine, reflect=True)

    PCAWGSample = Base.classes.pcawg_samples
    SampleLocation = Base.classes.sample_locations

    sample_session = Session(sample_engine)

    #Butler run tracking is in its own database
    Analysis = connection.Base.classes.analysis
    AnalysisRun = connection.Base.classes.analysis_run
    Configuration = connection.Base.classes.configuration

    run_session = connection.Session()

    if tissue_type == "normal":
        sample_id = PCAWGSample.normal_wgs_alignment_gnos_id
        sample_location = SampleLocation.normal_sample_location
    else:
        sample_id = PCAWGSample.tumor_wgs_alignment_gnos_id
        sample_location = SampleLocation.tumor_sample_location

    current_runs = run_session.query(Configuration.config[("sample"," sample_id")].astext).\
        join(AnalysisRun, AnalysisRun.config_id == Configuration.config_id).\
        join(Analysis, Analysis.analysis_id == AnalysisRun.analysis_id).\
        filter(and_(Analysis.analysis_id == analysis_id, AnalysisRun.run_status != tracker.model.analysis_run.RUN_STATUS_ERROR)).all()

    available_samples = sample_session.query(PCAWGSample.index.label("index"), sample_id.label("sample_id"), sample_location.label("sample_location")).\
        join(SampleLocation, PCAWGSample.index == SampleLocation.donor_index).\
        filter(and_(sample_location != None, sample_id.notin_(current_runs))).\
        limit(num_runs).all()

    run_session.close()
    connection.engine.dispose()

    sample_session.close()
    sample_engine.dispose()

    return available_samples, len(available_samples)
Exemplo n.º 17
0
def set_error(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_ERROR.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update
    """
    session = connection.Session()

    my_run.run_status = RUN_STATUS_ERROR

    now = datetime.datetime.now()
    my_run.last_updated_date = now

    session.add(my_run)
    session.commit()
    session.close()
    connection.engine.dispose()
Exemplo n.º 18
0
def list_analyses():
    """
    Return all of the existing analyses.
    
    Returns:
        all_analyses(List): All of the existing analyses.
    """
    session = connection.Session()
    try:
        all_analyses = session.query(Analysis).all()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()

    return all_analyses
Exemplo n.º 19
0
def get_analysis_run_by_id(analysis_run_id):
    """
    Get the analysis run with ID analysis_run_id.

    Args:
        analysis_run_id (id): ID of analysis run

    Returns:
        my_analysis_run (AnalysisRun): The analysis run that has id analysis_run_id.
    """
    session = connection.Session()

    my_analysis_run = session.query(AnalysisRun).filter(
        AnalysisRun.analysis_run_id == analysis_run_id).first()

    session.close()
    connection.engine.dispose()
    return my_analysis_run
Exemplo n.º 20
0
def list_workflows():
    """
    Return all of the existing workflows
    
    Returns:
        all_workflows(List): All of the existing workflows.
    """
    session = connection.Session()

    try:
        all_workflows = session.query(Workflow).all()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()

    return all_workflows
Exemplo n.º 21
0
def get_configuration_by_id(config_id):
    """
    Get the configuration with ID config_id.

    Args:
        config_id (id): ID of configuration

    Returns:
        my_configuration (Configuration): The configuration that has id config_id.
    """
    session = connection.Session()
    try:
        my_configuration = session.query(Configuration).filter(
            Configuration.config_id == config_id).first()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()
    return my_configuration
Exemplo n.º 22
0
def get_workflow_by_id(workflow_id):
    """
    Get a workflow with ID workflow_ID
    
    Args:
        workflow_id (id): ID of workflow
    
    Returns:
        my_workflow (Workflow): The workflow with id workflow_id.
    """
    session = connection.Session()

    try:
        my_workflow = session.query(Workflow).filter(
            Workflow.workflow_id == workflow_id).first()
    except:
        session.rollback()
        raise
    finally:
        session.close()
        connection.engine.dispose()

    return my_workflow