예제 #1
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
예제 #2
0
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)
예제 #3
0
def submit_recalculate_pipeline_call_job(sample_id, analysis_id, waiting_time):
    try:
        analysis = Analysis.query.filter_by(id=analysis_id).one()

        if not analysis:
            raise Exception("Failed to get Analysis record")

        analysis.manual_set = None
        analysis.current_call = "Pending"
        db_util.db_commit()
        logger.info("Analysis record for manual_set column updated "
                    "successfully.")
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception(
            'Failed to update manual_set column of Analysis '
            'with error: ', e)

    # Get latest DB records and upload to s3
    db_util.upload_latest_db_records('junction', sample_id, analysis_id)
    db_util.upload_latest_db_records('variation', sample_id, analysis_id)
    db_util.upload_latest_db_records('config', sample_id, analysis_id)

    # submit batch job
    job_name = 'recalculate-call-{}-{}-{}' \
        .format(str(sample_id), str(analysis_id), date_hash())
    job_queue_name = cfg.recal_queue
    job_definition = cfg.rt_jobdef_name

    script = 'sbs-pipeline/bin/EC2_RecalculatePipeline.sh'
    command = [
        "sh", script,
        str(sample_id),
        str(analysis_id), "--bucket", cfg.s3_bucket
    ]

    response = submit_job(command, job_name, job_queue_name, job_definition)

    job_id = response['jobId']

    if waiting_time:
        timeout = time.time() + 18  # 18 secs from now
        status = get_job_status(job_id, timeout)
    else:
        status = {
            "job_id": job_id,
            "job_name": job_name,
            "sample_id": sample_id,
            "analysis_id": analysis_id
        }

    if status == TIMEOUT:
        logger.info('Failed to get status for recalculate pipeline call '
                    'job id {}. Timeout occurred.'.format(job_id))
        return TIMEOUT
    else:
        logger.info('Status for Recalculate pipeline call job {} is {}'.format(
            job_name, status))
        return status
예제 #4
0
def delete_observed_map(observed_map_id):
    """
    Delete Observed Map
    """
    try:
        ObservedMap.query.filter_by(id=observed_map_id).delete()
        db_util.db_commit()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to delete ObservedMap with error: ', e)
예제 #5
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
예제 #6
0
def update_sample(params):
    """
    Update sample record
    """
    try:
        sample = Sample.query.filter_by(sample_id=params.get('sample_id')).one()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to get Sample with error: ', e)

    # get rquest primary id
    request_primary_id = None
    request_id = params.get('request_id', None)
    request_object = req_svc.get_request_object(request_id)

    if request_object:
        request_primary_id = request_object.id

    if not request_primary_id:
        raise Exception(
            'Failed to get request primary id for Request: {}'.format(
                request_id))

    sample.primary_map = params.get('primary_map', sample.primary_map)
    sample.ev_man_event = params.get('ev_man_event', sample.ev_man_event)
    sample.other_maps = params.get('other_maps', sample.other_maps)
    sample.construct_name = params.get('construct_name', sample.construct_name)
    sample.event_id = params.get('event_id', sample.event_id)
    sample.geno_type = params.get('geno_type', sample.geno_type)
    sample.organism = params.get('organism', sample.organism)
    sample.sample_name = params.get('sample_name', sample.sample_name)
    sample.develop_stage = params.get('develop_stage', sample.develop_stage)
    sample.growth_location = params.get('growth_location',
                                        sample.growth_location)
    sample.treated = params.get('treated', sample.treated)
    sample.eu_id = params.get('eu_id', sample.eu_id)
    sample.request_id = params.get(request_primary_id, sample.request_id)

    if 'curr_alpha_analysis' in params and params['curr_alpha_analysis']:
        update_analysis_type(params['curr_alpha_analysis'])

    sample.curr_alpha_analysis = params.get('curr_alpha_analysis',
                                            sample.curr_alpha_analysis)

    try:
        db_util.db_commit()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to commit with error: ', e)

    logger.info("Successfully updated sample for id [{}]."
                .format(params['sample_id']))

    return sample
예제 #7
0
def update_request(params):
    """
    Update Request Info
    """
    request_primary_id = None

    try:
        request_object = get_request_object(params['request_id'])
        request_primary_id = request_object.id

        if not request_primary_id:
            raise Exception("Failed to update Request {}: Request does not "
                            "exists".format(params['request_id']))

        request = Request.query.get(request_primary_id)
        request.request_id = params.get('request_id', request.request_id)
        request.sample_prep_methods = params.get('sample_prep_methods',
                                                 request.sample_prep_methods)
        request.sbs_internalpipeline_version = params.get(
            'sbs_internalpipeline_version',
            request.sbs_internalpipeline_version)
        request.request_name = params.get('request_name', request.request_name)
        request.released_on = params.get('released_on', request.released_on)
        request.sbs_status = params.get('sbs_status', request.sbs_status)
        request.organism_id = params.get('organism_id', request.organism_id)

        if 'tx_method' in params and params['tx_method']:
            try:
                tx_method = TxMethod.query \
                    .filter_by(method_name=params['tx_method']) \
                    .first()
            except Exception as e:
                logger.error('Failed to get tx_method with error: {}'.format(
                    str(e)))
                raise Exception('Failed to get tx_method with error: ', e)
            if tx_method:
                request.tx_method_id = tx_method.id

        db_util.db_commit()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to update Request with error: ', e)

    if request:
        logger.info("Successfully updated Request for request_id [{}].".format(
            params['request_id']))

        return get_request_by_request_id(request_primary_id)
    else:
        return {
            "status":
            "Failed to update sbs_status/transformation method "
            "for id {}.".format(params['request_id'])
        }
예제 #8
0
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
예제 #9
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
예제 #10
0
def update_endogenous_set(analysis_id, junction_ids):
    if not junction_ids:
        return False

    # Get Endogenous Set name
    endo_set_name = PipelineConfiguration.query \
        .with_entities(PipelineConfiguration.endogenous_set) \
        .outerjoin(Analysis) \
        .filter(Analysis.id == analysis_id) \
        .one_or_none()

    if not endo_set_name:
        return False

    endo_set = add_endo_junction_set(endo_set_name)

    # Check if sequences exists in Endogenous set
    items = EndogenousJunction.query \
        .with_entities(EndogenousJunction,
                       Junction.junction_sequence.label('seq')) \
        .filter(EndogenousJunction.junction_id.in_(junction_ids)) \
        .outerjoin(Junction) \
        .all()

    sequences = {}  # Dict of {sequence: endogenous_junction_obj}
    for item in items:
        if item.seq not in sequences.values():
            sequences[item.seq] = item[0]

    avl_endoseqs = Junction.query() \
        .with_entities(distinct(Junction.junction_sequence.label('seq'))) \
        .outerjoin(EndogenousJunction) \
        .outerjoin(EndogenousJunctionSet) \
        .filter(EndogenousJunctionSet.name == endo_set_name) \
        .filter(Junction.junction_sequence.in_(sequences)) \
        .all()

    avl_endoseqs_list = []
    for s in avl_endoseqs:
        if s.seq not in avl_endoseqs_list:
            avl_endoseqs_list.append(s.seq)

    # Update Endogenous Junction record with endo set id
    for seq in sequences.values():
        if seq not in avl_endoseqs_list:
            endo_junc = sequences[seq]
            endo_junc.junction_set = endo_set.id
            db_util.db_commit()

    return True
예제 #11
0
def update_record(observed_map, params):
    try:
        observed_map.name = params.get('name', observed_map.name)
        observed_map.status = params.get('status', observed_map.status)
        observed_map.load_time = params.get('load_time', observed_map.load_time)
        observed_map.length = params.get('length', observed_map.length)
        observed_map.send_to_evman = params.get('send_to_evman',
                                                observed_map.send_to_evman)
        db_util.db_commit()
        logger.info("Observed Map record updated successfully.")
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to update Observed Map with error: ', e)

    return observed_map
예제 #12
0
def update_junction_duplicate_mask(junction_id_list, comment, curr_req_id,
                                   is_masked, masked_by):
    map_analyses = request_service.get_map_analysis_by_request(curr_req_id)
    for map_analysis_id in map_analyses:
        for jn_id in junction_id_list:
            map_analysis_jn = MapAnalysisJunctions.query.filter_by(
                junction_id=jn_id).filter_by(
                map_analysis_id=map_analysis_id[0]).one_or_none()
            if map_analysis_jn and map_analysis_jn.masked != is_masked:
                map_analysis_jn.masked = is_masked
                map_analysis_jn.junction_comment = comment
                map_analysis_jn.masked_by = masked_by
                db_util.db_commit()

    return SUCCESS
예제 #13
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
예제 #14
0
def delete_sample(id):
    """
    delete sample
    """
    try:
        sample = Sample.query \
            .filter_by(id=id) \
            .delete()
        db_util.db_commit()

        if sample:
            return {"status": "Deleted sample for id [{}]".format(id)}
        else:
            return {"status": "Error while deleting id [{}]".format(id)}
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to delete Sample with error: ', e)
예제 #15
0
def delete_crop(crop_id):
    """
    delete crop record
    """
    try:
        crop = Crop.query \
            .filter_by(id=crop_id) \
            .delete()
        db_util.db_commit()

        if crop:
            return {"status": "Deleted crop for id [{}]".format(id)}
        else:
            return {"status": "Error while deleting crop id [{}]".format(id)}
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to delete crop with error: ', e)
예제 #16
0
def update_junction(junction):
    cur_junction = Junction.query \
        .filter_by(junction_id=junction['id']) \
        .one_or_none()

    if cur_junction:
        cur_junction.end = junction.get('end', cur_junction.end)
        cur_junction.position = junction.get('position', cur_junction.position)
        cur_junction.proximal_mapping = junction.get('proximal_mapping',
                                                     cur_junction.proximal_mapping)
        cur_junction.proximal_percent_identity = junction.get(
            'proximal_percent_identity', cur_junction.proximal_percent_identity)
        cur_junction.proximal_sequence = junction.get('proximal_sequence',
                                                      cur_junction.proximal_sequence)
        cur_junction.junction_sequence = junction.get('junction_sequence',
                                                      cur_junction.junction_sequence)
        cur_junction.distal_sequence = junction.get('distal_sequence',
                                                    cur_junction.distal_sequence)
        cur_junction.proximal_sequence_length = junction.get(
            'proximal_sequence_length', cur_junction.proximal_sequence_length)
        cur_junction.distal_mapping = junction.get('distal_mapping',
                                                   cur_junction.distal_mapping)
        cur_junction.distal_percent_identity = junction.get(
            'distal_percent_identity', cur_junction.distal_percent_identity)
        cur_junction.distal_sequence_length = junction.get(
            'distal_sequence_length', cur_junction.distal_sequence_length)
        cur_junction.element = junction.get('element', cur_junction.element)
        cur_junction.unique_reads = junction.get('unique_reads',
                                                 cur_junction.unique_reads)
        cur_junction.supporting_reads = junction.get('supporting_reads',
                                                     cur_junction.supporting_reads)
        cur_junction.endogenous = junction.get('endogenous',
                                               cur_junction.endogenous)
        cur_junction.source = junction.get('source', cur_junction.source)
        cur_junction.duplicates = junction.get('duplicates',
                                               cur_junction.duplicates)

    try:
        db_util.db_commit()
        logger.info("Junction record updated successfully.")
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to update Junction with error: ', e)

    return cur_junction
예제 #17
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))
예제 #18
0
def update_map_analysis_jn(constructs, junctions_dict_list, is_masked, comment,
                           masked_by, is_duplicate):
    for junction in junctions_dict_list:
        for map in constructs:
            map_analysis_jn = MapAnalysisJunctions.query.filter_by(
                junction_id=junction['id']).filter_by(
                map_analysis_id=map['map_analysis_id']).one_or_none()
            if map_analysis_jn and map_analysis_jn.masked != is_masked:
                map_analysis_jn.masked = is_masked
                if is_duplicate and is_masked:
                    map_analysis_jn.junction_comment = comment.format(
                        junction['position'])
                elif is_duplicate and not is_masked:
                    map_analysis_jn.junction_comment = None
                else:
                    map_analysis_jn.junction_comment = comment
                map_analysis_jn.masked_by = masked_by
                db_util.db_commit()
예제 #19
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
예제 #20
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)
예제 #21
0
def update_crop(params):
    """
    Update crop record
    """
    try:
        crop = Crop.query.get(params['id'])
        if not crop:
            raise Exception(
                'Crop not found for given id {}: '.format(params['id']))
        crop.organism = params.get('organism', crop.organism)
        db_util.db_commit()

        logger.info("Successfully updated crop for id [{}]." \
                    .format(params['id']))
        crop = Crop.query.filter_by(id=params['id']).one_or_none()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to update Crop with error: ', e)

    return crop
예제 #22
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
예제 #23
0
def update_analysis_tool_details(params):
    tool_details = AnalysisToolsDetails.query \
        .filter_by(analysis_id=int(params['analysis_id'])) \
        .filter_by(tool_id=int(params['tool_id'])) \
        .one_or_none()
    if tool_details:
        # only update Current Analysis Tool Details record
        tool_details.analysis_id = params.get('analysis_id',
                                              tool_details.analysis_id)
        tool_details.tool_id = params.get('tool_id', tool_details.tool_id)
        tool_details.current_call = params.get('call',
                                               tool_details.current_call)
        tool_details.current_msg = params.get('message',
                                              tool_details.current_msg)
        tool_details.current_detail = params.get('cna_detail',
                                                 tool_details.current_detail)
        db_util.db_commit()
        logger.info("Analysis Tools Details record updated successfully.")
        return tool_details
    return None
예제 #24
0
def delete_request(request_id):
    """
    Deletes request record
    """
    try:
        request = Request.query \
            .filter_by(request_id=request_id) \
            .delete()
        db_util.db_commit()
    except Exception as e:
        logger.error('An error occurred : {}'.format(str(e)))
        raise Exception('Failed to delete request with error: ', e)

    if request:
        return {
            "status": "Deleted request for Request id [{}]".format(request_id)
        }
    else:
        return {
            "status": "Error while deleting Request id [{}]".format(request_id)
        }
예제 #25
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
예제 #26
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
예제 #27
0
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)
예제 #28
0
def add_tool_record(analysis_tool_details):
    db_util.db_add_query(analysis_tool_details)
    db_util.db_commit()
    return analysis_tool_details
예제 #29
0
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
예제 #30
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