def add_request_comments(request_id, params):
    """
    Adds Comment for particular request
    """
    request_primary_id = None

    try:
        request_object = get_request_object(request_id)
        if request_object:
            request_primary_id = request_object.id

        if request_primary_id:
            request_comment = RequestComment(params['comment'],
                                             request_primary_id,
                                             params['username'],
                                             params['username'])
            db_util.db_add_query(request_comment)
            db_util.db_commit()
            logger.info(
                "Comment record for request id [{}] added successfully.".
                format(request_id))

            return "Successfully added comment for request {}".format(
                request_id)

        else:
            raise Exception(
                "Failed to add comment for request {}".format(request_id))

    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception(
            'Failed to add Comments for Request as error in query: ', e)
Beispiel #2
0
def add_tx_method(params):
    """
    Adds tx_method record or returns tx_method record if already exists
    """
    method_name = params['tx_method']
    try:
        tx_method = TxMethod.query.filter_by(method_name=method_name) \
            .one_or_none()

        if tx_method:
            logger.info(
                "TxMethod with method name [{}] already exists.".format(
                    method_name))
            return tx_method

        tx_method = TxMethod(method_name)
        db_util.db_add_query(tx_method)
        db_util.db_commit()
        logger.info(
            "Transformation method record for method {} added successfully.".
            format(method_name))
        tx_method = TxMethod.query.filter_by(method_name=method_name) \
            .one_or_none()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to fetch Transformation method id for method '
                        '[{}] with error {} '.format(method_name, e))

    return tx_method
Beispiel #3
0
def add_record(observed_map):
    try:
        db_util.db_add_query(observed_map)
        db_util.db_commit()
        logger.info("Observed Map added successfully.")
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to add Observed Map with error: ', e)

    return observed_map
def add_endo_junction_set(endo_set_name):
    # Add Endogenous Set record if not present
    endo_set = EndogenousJunctionSet.query \
        .filter_by(name=endo_set_name) \
        .one_or_none

    if not endo_set:
        crop_id = None
        endo_set = EndogenousJunctionSet(crop_id, endo_set_name)
        db_util.db_add_query(endo_set)
        db_util.db_commit()

    return endo_set
Beispiel #5
0
def add_analysis(params):
    """
    Adds ANALYSIS record
    """
    try:
        analysis = create_analysis(params)
        db_util.db_add_query(analysis)
        db_util.db_commit()
        logger.info("Analysis record added successfully.")
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to add Analysis with error: ', e)

    return analysis
Beispiel #6
0
def add_map(params):
    """
    Adds Map
    """
    version = 0
    construct_id = params.get('construct_id', None)

    if construct_id:
        logger.info('Adding Map for construct_id [{}]'.format(construct_id))
        map = get_map_by_construct_id(construct_id)
        if map:
            logger.info('Map {} already exists'.format(map.construct_id))
            return map
    else:
        construct_id_no_ver = '.'.join(
            [params['map_id'], params['sample_id'], '']).lower()

        obs_map_ver = ObservedMap.query \
            .with_entities(ObservedMap.version) \
            .outerjoin(Map) \
            .outerjoin(MapAnalysis) \
            .filter(MapAnalysis.analysis_id == params['analysis_id']) \
            .filter(Map.construct_id.ilike(construct_id_no_ver + '%')) \
            .order_by(ObservedMap.version.desc()) \
            .limit(1) \
            .one_or_none()

        if obs_map_ver:
            version = int(obs_map_ver[0]) + 1
        else:
            version = 1

        construct_id = construct_id_no_ver + str(version)

    try:
        map = Map(construct_id.upper())
        db_util.db_add_query(map)
        db_util.db_commit()
        if version > 0:
            map.obs_map_version = version
        logger.info("Successfully added Map {}".format(construct_id))
    except Exception as e:
        logger.error('Failed to add Map with error: {}'.format(str(e)))
        raise Exception('Failed to add Map with error: ', e)

    return map
Beispiel #7
0
def batch_insert_integrities(integrities_dict_list):
    """
    Adds variation
    """
    try:
        for variation in integrities_dict_list:
            if variation:
                position = variation.get('position', None)
                type = variation.get('type', None)
                gt = variation.get('gt', None)
                ref_base = variation.get('ref_base', None)
                total_read_depth = variation.get('total_read_depth', None)
                sample_base = variation.get('sample_base', None)
                read_depth = variation.get('read_depth', None)
                purity = variation.get('purity', None)
                feature_type = variation.get('feature_type', None)
                annotation = variation.get('annotation', None)
                effects = variation.get('effects', None)
                translation = variation.get('translation', None)
                organism = variation.get('organism', None)
                tier_label = variation.get('tier_label', None)
                coverage = variation.get('coverage', None)
                tier = variation.get('tier', None)
                is_tier_label_updated = variation.get('is_tier_label_updated',
                                                      False)
                map_analysis_id = variation.get('map_analysis_id', None)
                if not check_duplicate_variation(
                        position, type, gt, ref_base, total_read_depth,
                        sample_base, read_depth, purity, feature_type,
                        annotation, effects, translation, organism, tier_label,
                        map_analysis_id):
                    variation = Variation(position, type, ref_base,
                                          sample_base, translation, coverage,
                                          purity, tier, read_depth, annotation,
                                          tier_label, is_tier_label_updated,
                                          gt, total_read_depth, feature_type,
                                          effects, organism, map_analysis_id)
                    db_util.db_add_query(variation)
                    db_util.db_commit()
                    logger.info("Variation record added successfully.")
    except Exception as e:
        logger.error(
            'Failed to batch insert Variation records with error: {}'.format(
                str(e)))
        raise Exception('Error occurred: {}'.format(e))
Beispiel #8
0
def add_analysis_tools(analysis_tools):
    """
    Adds Analysis Tools record
    """
    analysis_tool_list = []
    for name in analysis_tools:
        analysis_tool = AnalysisTools.query.filter(
            AnalysisTools.name == name).one_or_none()
        if not analysis_tool:
            analysis_tool = AnalysisTools(name)
            db_util.db_add_query(analysis_tool)
    db_util.db_commit()
    # Get all the five tools records
    all_tool_records = AnalysisTools.query.all()
    for tool_obj in all_tool_records:
        analysis_tool_list.append(tool_obj)

    return analysis_tool_list
Beispiel #9
0
def add_analysis_comments(analysis_id, params):
    """
    Adds Comment for particular analysis
    """
    try:
        analysis_comment = AnalysisComment(params['comment'], analysis_id,
                                           params['username'],
                                           params['username'])
        db_util.db_add_query(analysis_comment)
        db_util.db_commit()
        logger.info("Comment record for analysis id [{}] added "
                    "successfully.".format(analysis_id))

        return "Successfully added comment for analysis {}".format(analysis_id)

    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to add Comments for Analysis as error in '
                        'query: ', e)
Beispiel #10
0
def add_map_analysis(analysis_id, map_id, read_count):
    """
    Adds Map record
    """
    logger.info('Adding MapAnalysis for map_id [{}] and analysis_id [{}]')
    map_analysis = get_map_analysis(map_id, analysis_id)
    if map_analysis:
        logger.info('MapAnalysis already exists')
        return map_analysis

    try:
        map_analysis = MapAnalysis(read_count, analysis_id, map_id)
        db_util.db_add_query(map_analysis)
        db_util.db_commit()
        logger.info("Successfully added MapAnalysis.".format(
            analysis_id, map_id))
    except Exception as e:
        logger.error('Failed to add MapAnalysis with error: {}'.format(str(e)))
        raise Exception('Failed to add MapAnalysis with error: ', e)
    return map_analysis
Beispiel #11
0
def add_observed_map_comments(observed_map_id, params):
    """
    Adds Comment for particular ObservedMap
    """
    try:
        observed_map_comment = ObservedMapComment(params['comment'],
                                                  observed_map_id,
                                                  params['username'],
                                                  params['username'])
        db_util.db_add_query(observed_map_comment)
        db_util.db.session.commit()
        observed_map_comment = observed_map_comment.as_dict()
        db_util.db.session.close()
    except Exception as e:
        logger.error('Failed to add ObservedMap comments with error: {}'
                     .format(str(e)))
        raise Exception('Failed to add Comments for ObservedMap as error in '
                        'query: ', e)
    logger.info("Comment record for ObservedMap id [{}] added successfully."
                .format(observed_map_id))

    return observed_map_comment
Beispiel #12
0
def add_request(params):
    """
    Adds request record or raise exception if already exists
    """
    request = None
    try:
        request_id = params['request_id'].rstrip('/')
        request = Request.query.filter_by(request_id=request_id).one_or_none()
        if request:
            logger.info("Request with request id [{}] already exists." \
                        .format(params['request_id']))
            return request

        sample_prep_methods = params.get('sample_prep_methods', None)
        sbs_internalpipeline_version = params.get(
            'sbs_internalpipeline_version', None)
        request_name = params.get('request_name', None)
        released_on = params.get('released_on', None)
        sbs_status = params.get('sbs_status', None)
        researcher = params.get('researcher', None)
        tx_method_id = params.get('tx_method_id', None)
        organism_id = params.get('organism_id', None)

        request = Request(request_id, sample_prep_methods,
                          sbs_internalpipeline_version, request_name,
                          released_on, sbs_status, tx_method_id, researcher,
                          organism_id)
        db_util.db_add_query(request)
        db_util.db_commit()
        logger.info(
            "Request record for request id [{}] added successfully.".format(
                params['request_id']))
        request = Request.query.filter_by(request_id=request_id).one_or_none()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to add Request with error: ', e)

    return request
Beispiel #13
0
def add_crop(params):
    """
    Adds crop record or returns crop record if already exists
    """
    organism = params['organism']
    try:
        crop = Crop.query.filter_by(organism=organism).one_or_none()
        if crop:
            logger.info("Crop with organism [{}] already exists." \
                        .format(organism))
            return crop

        crop = Crop(organism)
        db_util.db_add_query(crop)
        db_util.db_commit()
        logger.info("Crop record for organism [{}] added successfully."
                    .format(organism))
        crop = Crop.query.filter_by(organism=organism).one_or_none()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to add Crop with error: ', e)

    return crop
def save_map_analysis_junctions(map_analysis_id, junction_id, masked):
    try:
        map_analysis_junction = MapAnalysisJunctions.query \
            .with_entities(MapAnalysisJunctions) \
            .filter(MapAnalysisJunctions.map_analysis_id == map_analysis_id) \
            .filter(MapAnalysisJunctions.junction_id == junction_id) \
            .one_or_none()
        if not map_analysis_junction:
            map_analysis_junction = MapAnalysisJunctions(map_analysis_id,
                                                         junction_id, masked,
                                                         None,
                                                         None)
        else:
            map_analysis_junction.masked = masked

        db_util.db_add_query(map_analysis_junction)
        db_util.db_commit()
        logger.info("Record for map_analysis_junction id [{}] added "
                    "successfully.".format(junction_id))
    except Exception as e:
        logger.error(
            'An error occurred while saving map analysis junctions : {}'
                .format(str(e)))
        raise Exception('Failed to save map analysis junction : ', e)
def insert_junctions(junctions_dict_list):
    """
    Adds JUNCTION records
    """
    try:
        for junction in junctions_dict_list:
            if junction:
                if 'map_analysis_id' not in junction or not junction[
                    'map_analysis_id']:
                    logger.error('map_analysis_id is required')
                    raise Exception('map_analysis_id is required in junction')
                map_analysis_id = junction['map_analysis_id']
                masked = junction['masked']
                masked = set_boolean_junction_values(masked)
                endogenous = junction['endogenous']
                endogenous = set_boolean_junction_values(endogenous)
                junction['endogenous'] = endogenous
                junction, is_duplicate = get_duplicate_junctions(junction)
                if is_duplicate:
                    try:
                        junction_id = junction.id
                        save_map_analysis_junctions(map_analysis_id,
                                                    junction_id, masked)
                    except Exception as e:
                        logger.error('An error occurred : {}'.format(str(e)))
                        raise Exception(
                            'Failed to insert map_analysis_junction records with error: {}'.format(
                                e))
                elif not is_duplicate:
                    try:
                        end = junction.get('end', None)
                        position = junction.get('position', None)
                        proximal_mapping = junction.get('proximal_mapping',
                                                        None)
                        if proximal_mapping == "NA" or not proximal_mapping:
                            proximal_percent_identity = None
                        else:
                            proximal_percent_identity = proximal_mapping \
                                .split(",")[-1]

                        proximal_sequence = junction.get('proximal_sequence',
                                                         None)
                        junction_sequence = junction.get('junction_sequence',
                                                         None)
                        proximal_sequence_length = junction.get(
                            'proximal_sequence_length', None)
                        distal_sequence = junction.get('distal_sequence', None)
                        distal_mapping = junction.get('distal_mapping', None)

                        if distal_mapping == "NA" or not distal_mapping:
                            distal_percent_identity = None
                        else:
                            distal_percent_identity = distal_mapping \
                                .split(",")[-1]

                        distal_sequence_length = junction.get(
                            'distal_sequence_length', None)
                        element = junction.get('element', None)
                        unique_reads = junction.get('unique_reads', None)
                        supporting_reads = junction.get('supporting_reads',
                                                        None)
                        endogenous = junction.get('endogenous', endogenous)
                        source = junction.get('source', None)
                        duplicates = junction.get('duplicates', None)
                        junction_record = Junction(end, position,
                                                   proximal_mapping,
                                                   proximal_percent_identity,
                                                   proximal_sequence,
                                                   junction_sequence,
                                                   proximal_sequence_length,
                                                   distal_sequence,
                                                   distal_mapping,
                                                   distal_percent_identity,
                                                   distal_sequence_length,
                                                   element, unique_reads,
                                                   supporting_reads,
                                                   endogenous, source,
                                                   duplicates)
                        db_util.db_add_query(junction_record)
                        db_util.db_commit()
                        logger.info(
                            "Junction record for added successfully {}".format(
                                junction_record))

                        junction_id = junction_record.id
                        save_map_analysis_junctions(map_analysis_id,
                                                    junction_id, masked)
                    except Exception as e:
                        logger.error('An error occurred : {}'.format(str(e)))
                        raise Exception(
                            'Failed to insert map_analysis_junction records with error: {}'.format(
                                e))
                else:
                    logger.info("something went wrong")
        logger.info("Junction record added successfully.")
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception(
            'Failed to batch insert Junction records with error: {}'.format(e))
    return SUCCESS
Beispiel #16
0
def add_sample(params):
    """
    Adds sample record or raise exception if already exists
    """
    try:
        request_id = params['request_id']
        request = Request.query.filter_by(id=request_id).one_or_none()

        if not request:
            logger.error("Request with id [{}] does not exist."
                         .format(params['request_id']))

        sample_id = params['sample_id']
        sample = Sample.query.filter_by(sample_id=sample_id).one_or_none()

        if sample:
            logger.info("Sample with sample_id [{}] already exists."
                        .format(params['sample_id']))
            return sample

        primary_map = params.get('primary_map', None)
        ev_man_event = params.get('ev_man_event', None)
        other_maps = params.get('other_maps', None)
        construct_name = params.get('construct_name', None)
        event_id = params.get('event_id', None)
        geno_type = params.get('geno_type', None)
        organism = params.get('organism', None)
        sample_name = params.get('sample_name', None)
        develop_stage = params.get('develop_stage', None)
        growth_location = params.get('growth_location', None)
        treated = params.get('treated', False)
        eu_id = params.get('eu_id', None)
        curr_alpha_analysis = params.get('curr_alpha_analysis', None)

        if curr_alpha_analysis:
            update_analysis_type(curr_alpha_analysis)

        sample = Sample(sample_id,
                        primary_map,
                        ev_man_event,
                        other_maps,
                        request_id,
                        construct_name,
                        event_id,
                        geno_type,
                        organism,
                        sample_name,
                        develop_stage,
                        growth_location,
                        treated,
                        eu_id,
                        curr_alpha_analysis)
        db_util.db_add_query(sample)
        db_util.db_commit()
        logger.info("Sample record for request id [{}] added successfully."
                    .format(params['request_id']))

        sample = Sample.query.filter_by(sample_id=sample_id).one_or_none()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to add Sample with error: ', e)

    return sample
Beispiel #17
0
def add_tool_record(analysis_tool_details):
    db_util.db_add_query(analysis_tool_details)
    db_util.db_commit()
    return analysis_tool_details