示例#1
0
def update_curate_activity(locus_summary_object):
    ''' Add curator locus-summary event to curator activity table
    
    Paramaters
    ----------
    locus_summary_object: LocusSummary
        locus-summary object
    
    Returns
    -------
    bool: The return value. True for success, False otherwise
    '''
    flag = False
    try:
        curator_session = get_curator_session(
            locus_summary_object['created_by'])
        existing = curator_session.query(CuratorActivity).filter(
            CuratorActivity.dbentity_id ==
            locus_summary_object['dbentity_id']).one_or_none()
        message = 'added'
        new_curate_activity = None
        if existing:
            #curator_session.delete(existing)
            message = 'updated'
            new_curate_activity = CuratorActivity(
                display_name=locus_summary_object['display_name'],
                obj_url=locus_summary_object['obj_url'],
                activity_category=locus_summary_object['activity_category'],
                dbentity_id=locus_summary_object['dbentity_id'],
                message=message,
                json=locus_summary_object['json'],
            )
        else:
            new_curate_activity = CuratorActivity(
                display_name=locus_summary_object['display_name'],
                obj_url=locus_summary_object['obj_url'],
                activity_category=locus_summary_object['activity_category'],
                dbentity_id=locus_summary_object['dbentity_id'],
                message=message,
                json=locus_summary_object['json'],
                created_by=locus_summary_object['created_by'])
        if new_curate_activity:
            curator_session.add(new_curate_activity)
            transaction.commit()
            flag = True
    except Exception as e:
        traceback.print_exc()
        transaction.abort()
        raise (e)

    return flag
示例#2
0
def delete_disease_annotation(request):
    try:
        id = request.matchdict['id']
        dbentity_id = request.matchdict['dbentity_id']

        if dbentity_id:
            gene_dbentity_id = DBSession.query(Dbentity).filter(or_(Dbentity.sgdid==dbentity_id, Dbentity.format_name==dbentity_id)).one_or_none()
            if not gene_dbentity_id:
                raise Exception('gene not found, please provide sgdid or systematic name')
    
        curator_session = get_curator_session(request.session['username'])
        isSuccess = False
        returnValue = ''
        disease_in_db = curator_session.query(Diseaseannotation).filter(Diseaseannotation.annotation_id == id).one_or_none()
        total_diseases_in_db = curator_session.query(Diseaseannotation).filter(Diseaseannotation.dbentity_id == gene_dbentity_id.dbentity_id).count()
        if(disease_in_db):
            try:
                curator_session.delete(disease_in_db)
                if (total_diseases_in_db == 1): #last annotation being deleted
                    update_ldb = {'has_disease': 'false'}
                    curator_session.query(Locusdbentity).filter(Locusdbentity.dbentity_id == gene_dbentity_id.dbentity_id).update(update_ldb)
                transaction.commit()
                isSuccess = True
                returnValue = 'Diseaseannotation successfully deleted.'
            except Exception as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Error occurred deleting diseaseannotation: ' + str(e.message)
            finally:
                if curator_session:
                    curator_session.close()

            if isSuccess:
                return HTTPOk(body=json.dumps({'success': returnValue}), content_type='text/json')

            return HTTPBadRequest(body=json.dumps({'error': returnValue}), content_type='text/json')

        return HTTPBadRequest(body=json.dumps({'error': 'diseaseannot not found in database.'}), content_type='text/json')

    except Exception as e:
        return HTTPBadRequest(body=json.dumps({'error': str(e)}), content_type='text/json')
示例#3
0
def update_author_response(request):

    try:
        CREATED_BY = request.session['username']
        curator_session = get_curator_session(request.session['username'])
        
        curation_id = request.params.get('curation_id')
        
        has_fast_track_tag = set_val(request.params.get('has_fast_track_tag'))
        curator_checked_datasets = set_val(request.params.get('curator_checked_datasets'))
        curator_checked_genelist = set_val(request.params.get('curator_checked_genelist'))
        no_action_required = set_val(request.params.get('no_action_required'))

        row = curator_session.query(Authorresponse).filter_by(curation_id=int(curation_id)).one_or_none()
    
        cols_changed = []
        if set_val(row.has_fast_track_tag) != has_fast_track_tag:
            row.has_fast_track_tag = has_fast_track_tag
            cols_changed.append('has_fast_track_tag')
        if set_val(row.curator_checked_datasets) != curator_checked_datasets:
            row.curator_checked_datasets = curator_checked_datasets
            cols_changed.append('curator_checked_datasets')
        if set_val(row.curator_checked_genelist) != curator_checked_genelist:
            row.curator_checked_genelist = curator_checked_genelist
            cols_changed.append('curator_checked_genelist')
        if set_val(row.no_action_required) != no_action_required:
            row.no_action_required = no_action_required
            cols_changed.append('no_action_required')

        if len(cols_changed) > 0:
            curator_session.add(row)
            transaction.commit()
            success_message = "The column <strong>" + ", ".join(cols_changed) + "</strong> got updated in authorresponse table."
        else:
            success_message = "Nothing is changed in authorresponse table."
        return HTTPOk(body=json.dumps({'success': success_message, 'authorResponse': "AUTHORRESPONSE"}), content_type='text/json')
    except Exception as e:
        transaction.abort()
        return HTTPBadRequest(body=json.dumps({'error': "ERROR: " + str(e)}), content_type='text/json')
示例#4
0
def upload_new_file(req_obj, session=None):
    try:
        if req_obj:
            other_extensions = ('zip', '.gz', '.sra')
            readme_file = {}
            other_files = []
            readme_file_id = None
            username = req_obj['uname']
            curator_session = get_curator_session(username)

            for key, val in req_obj.items():
                if key.endswith('.README'):
                    existing_readme_meta = get_existing_meta_data(
                        req_obj['displayName'], curator_session)
                    keywords = re.split(',|\|', req_obj['keywords'])

                    if existing_readme_meta:
                        existing_readme_meta.display_name = req_obj[
                            'displayName']
                        existing_readme_meta.description = req_obj[
                            'description']
                        existing_readme_meta.status = req_obj['status']
                        existing_readme_meta.is_public = True
                        existing_readme_meta.is_in_spell = False
                        existing_readme_meta.is_in_browser = False
                        existing_readme_meta.file_date = datetime.datetime.strptime(
                            req_obj['file_date'], '%Y-%m-%d')
                        readme_file_id = existing_readme_meta.dbentity_id
                        if existing_readme_meta.s3_url is None:
                            existing_readme_meta.upload_file_to_s3(
                                file=val,
                                filename=req_obj['displayName'],
                                is_web_file=True)
                        else:
                            existing_readme_meta.upload_file_to_s3(
                                val, req_obj['displayName'])
                        add_keywords(req_obj['displayName'], keywords,
                                     req_obj['source_id'], username,
                                     curator_session)
                        update_obj = {
                            'display_name':
                            req_obj['displayName'],
                            'created_by':
                            username,
                            's3_url':
                            existing_readme_meta.s3_url,
                            'activity_category':
                            'download',
                            'dbentity_id':
                            existing_readme_meta.dbentity_id,
                            'json':
                            json.dumps({
                                "file curation data":
                                existing_readme_meta.to_simple_dict(),
                                "modified_date":
                                str(datetime.datetime.now())
                            }),
                        }
                        msg = 'Add readme file for file curation'
                        update_curator_feed(update_obj, msg)
                    else:
                        new_obj = {
                            'display_name': req_obj['displayName'],
                            'description': req_obj['description'],
                            'status': req_obj['status'],
                            'is_public': True,
                            'is_in_spell': False,
                            'is_in_browser': False,
                            'file_date': req_obj['file_date'],
                            'file': val,
                        }
                        other_files.append(new_obj)

                if key.endswith(other_extensions):
                    other_files.append({
                        'display_name': req_obj['displayName'],
                        'file': val,
                        'file_name': key,
                        'description': req_obj['description'],
                        'status': req_obj['status'],
                        'file_date': req_obj['file_date']
                    })

            if len(other_files) > 0:
                update_from_s3 = None
                for item in other_files:
                    db_file = get_existing_meta_data(item['display_name'],
                                                     curator_session)
                    if db_file:
                        update_from_s3 = add_file_meta_db(
                            db_file, item, db_file.readme_file_id,
                            curator_session)
                        # update activities
                        update_obj = {
                            'display_name':
                            item['display_name'],
                            'created_by':
                            username,
                            's3_url':
                            db_file.s3_url,
                            'activity_category':
                            'download',
                            'dbentity_id':
                            db_file.dbentity_id,
                            'json':
                            json.dumps({
                                'file curation data':
                                db_file.to_simple_dict(),
                                "modified_date":
                                str(datetime.datetime.now())
                            }),
                        }
                        msg = 'Add file for file curation'
                        update_curator_feed(update_obj, msg, curator_session)
                    else:
                        if item['display_name']:
                            temp_file = item['file']
                            # regex to find matching metadata for new file
                            mod_display_name = re.sub('[^a-zA-Z0-9 \n\.]', '_',
                                                      item['display_name'])
                            search_string = re.search(
                                '(?:19|20)\d{2}\_\w+[0-9]{4}',
                                mod_display_name).group(0)
                            alt_string = search_string.replace('_', '-', 1)
                            pattern = '(%s|%s)' % (search_string, alt_string)
                            matches = curator_session.query(
                                Filedbentity).filter(
                                    Filedbentity.display_name.op('~*')(
                                        pattern)).all()

                            new_obj = None
                            new_file_extension = item['display_name'].split(
                                '.')[1]
                            file_date = datetime.datetime.strptime(
                                item['file_date'], '%Y-%m-%d')
                            if len(matches) > 0:
                                try:
                                    match = None
                                    file_size = 0
                                    for file_item in matches:
                                        if file_item.file_extension == new_file_extension:
                                            match = file_item
                                            break

                                    temp_file.seek(0, os.SEEK_END)
                                    file_size = temp_file.tell()
                                    temp_file.seek(0)

                                    new_obj = {
                                        'filename': item['display_name'],
                                        'description': item['description'],
                                        'status': item['status'].capitalize(),
                                        'is_public': True,
                                        'is_in_spell': False,
                                        'is_in_browser': False,
                                        'file_date': file_date,
                                        'file_extension': new_file_extension,
                                        'json': match.json,
                                        'year': match.year,
                                        'source_id': match.source_id,
                                        'display_name': item['display_name'],
                                        'readme_file_id': match.readme_file_id,
                                        'full_file_path': None,
                                        'md5sum': get_checksum(item['file']),
                                        'file_size': file_size,
                                        'data_id': match.data_id,
                                        'format_id': match.format_id,
                                        'topic_id': match.topic_id,
                                        'is_web_file': True
                                    }

                                    upload_file(username, item['file'],
                                                **new_obj)
                                    transaction.commit()

                                    db_new_file = get_existing_meta_data(
                                        item['display_name'], curator_session)

                                    update_obj = {
                                        'display_name':
                                        item['display_name'],
                                        'created_by':
                                        username,
                                        's3_url':
                                        db_new_file.s3_url,
                                        'activity_category':
                                        'download',
                                        'dbentity_id':
                                        db_new_file.dbentity_id,
                                        'json':
                                        json.dumps({
                                            'file curation data':
                                            db_new_file.to_simple_dict(),
                                            "modified_date":
                                            str(datetime.datetime.now())
                                        }),
                                    }
                                    msg = 'Add new file for file curation'
                                    update_curator_feed(
                                        update_obj, msg, curator_session)

                                except Exception as e:
                                    transaction.abort()
                                    raise (e)

            transaction.commit()
            DBSession.flush()
            return True
    except Exception as e:
        transaction.abort()
        raise (e)
示例#5
0
def add_litguide(request):

    try:
        CREATED_BY = request.session['username']
        curator_session = get_curator_session(request.session['username'])
        sgd = DBSession.query(Source).filter_by(
            display_name='SGD').one_or_none()
        source_id = sgd.source_id

        genes = request.params.get('genes', '')
        pmid = request.params.get('pmid', '')
        tag = request.params.get('tag', '')
        old_tag = request.params.get('old_tag', '')
        topic = request.params.get('topic', '')
        unlink = request.params.get('unlink', '')

        # return HTTPBadRequest(body=json.dumps({'error': "pmid="+pmid+", unlink="+unlink}), content_type='text/json')

        if pmid == '':
            return HTTPBadRequest(body=json.dumps(
                {'error': "Please enter a PMID."}),
                                  content_type='text/json')

        x = curator_session.query(Referencedbentity).filter_by(
            pmid=int(pmid)).one_or_none()
        if x is None:
            return HTTPBadRequest(body=json.dumps(
                {'error': "The PMID=" + pmid + " is not in the database."}),
                                  content_type='text/json')
        reference_id = x.dbentity_id

        success_message = ""

        if unlink == 'tag':
            x = curator_session.query(CurationReference).filter_by(
                curation_tag=old_tag, reference_id=reference_id).one_or_none()
            if x is not None:
                curator_session.delete(x)
                success_message = success_message + "The curation_tag <strong>" + old_tag + "</strong> is unlinked from this paper. "
            else:
                return HTTPBadRequest(body=json.dumps({
                    'error':
                    "The tag <strong>" + old_tag +
                    "</strong> is not linked with pmid=" + pmid +
                    ". So no need to unlink."
                }),
                                      content_type='text/json')

        elif genes == '':
            if old_tag:
                if old_tag != tag:
                    x = curator_session.query(CurationReference).filter_by(
                        curation_tag=old_tag,
                        reference_id=reference_id).one_or_none()
                    if x is not None:
                        x.curation_tag = tag
                        curator_session.add(x)
                        success_message = success_message + "The tag for this paper has been changed from <strong>" + old_tag + "</strong> to <strong>" + tag + "</strong>. "
            elif tag:
                x = CurationReference(reference_id=reference_id,
                                      source_id=source_id,
                                      curation_tag=tag,
                                      created_by=CREATED_BY)
                curator_session.add(x)
                success_message = success_message + "A new curation_reference row for pmid " + "<strong>" + pmid + "</strong>, and curation_tag <strong>" + tag + "</strong> has been added into the database. "
        else:
            gene_list = genes.replace('|', ' ').split(' ')
            dbentity_id_to_gene = {}
            dbentity_id_list = []
            for gene in gene_list:
                if gene == '':
                    continue
                dbentity = None
                if gene.startswith('SGD:') or gene.startswith('S00'):
                    dbentity = curator_session.query(Locusdbentity).filter_by(
                        sgdid=gene).one_or_none()
                else:
                    dbentity = curator_session.query(Locusdbentity).filter_by(
                        gene_name=gene).one_or_none()
                    if dbentity is None:
                        dbentity = curator_session.query(
                            Locusdbentity).filter_by(
                                systematic_name=gene).one_or_none()
                if dbentity is None:
                    dbentity = curator_session.query(Dbentity).filter_by(
                        format_name=gene, subclass='COOMPLEX').one_or_none()
                if dbentity is None:
                    dbentity = curator_session.query(
                        Pathwaydbentity).filter_by(
                            biocyc_id=gene).one_or_none()
                if dbentity is None:
                    return HTTPBadRequest(body=json.dumps({
                        'error':
                        "The gene " + gene +
                        " you entered is not in the database."
                    }),
                                          content_type='text/json')
                dbentity_id_to_gene[dbentity.dbentity_id] = gene
                dbentity_id_list.append(dbentity.dbentity_id)

            taxonomy_id = None
            if topic != '':
                taxon = curator_session.query(Taxonomy).filter_by(
                    taxid=TAXON).one_or_none()
                taxonomy_id = taxon.taxonomy_id

            for dbentity_id in dbentity_id_to_gene:
                if tag != '':
                    x = CurationReference(reference_id=reference_id,
                                          source_id=source_id,
                                          dbentity_id=dbentity_id,
                                          curation_tag=tag,
                                          created_by=CREATED_BY)
                    curator_session.add(x)
                    success_message = success_message + "A new curation_reference row for gene <strong>" + dbentity_id_to_gene[
                        dbentity_id] + "</strong>, pmid " + "<strong>" + pmid + "</strong>, and curation_tag <strong>" + tag + "</strong> has been added into the database. "

                if topic != '':
                    x = Literatureannotation(dbentity_id=dbentity_id,
                                             source_id=source_id,
                                             taxonomy_id=taxonomy_id,
                                             reference_id=reference_id,
                                             topic=topic,
                                             created_by=CREATED_BY)
                    curator_session.add(x)
                    success_message = success_message + "A new literatureannotation row for gene <strong>" + dbentity_id_to_gene[
                        dbentity_id] + "</strong>, pmid " + "<strong>" + pmid + "</strong>, and topic <strong>" + topic + "</strong> has been added into the database. "
            if len(dbentity_id_list) > 0:
                x = curator_session.query(CurationReference).filter_by(
                    curation_tag=old_tag,
                    reference_id=reference_id).one_or_none()
                if x is not None:
                    curator_session.delete(x)
                    success_message = success_message + "The curation_tag <strong>" + old_tag + "</strong> is unlinked from this paper. "

        if success_message == "":
            success_message = "Nothing is changed for tag/topic."
        success_message = success_message + " DONE!"
        transaction.commit()
        return HTTPOk(body=json.dumps({
            'success': success_message,
            'litguide': "LIT_GUIDE"
        }),
                      content_type='text/json')
    except Exception as e:
        return HTTPBadRequest(body=json.dumps(
            {'error': str(e) + ". DONE reporting ERROR!"}),
                              content_type='text/json')
示例#6
0
def update_litguide(request):

    try:
        CREATED_BY = request.session['username']
        curator_session = get_curator_session(request.session['username'])

        gene_id_list = request.params.get('gene_id_list', '')
        tag = request.params.get('tag', '')
        topic = request.params.get('topic', '')
        unlink = request.params.get('unlink', '')

        if gene_id_list == '':
            return HTTPBadRequest(body=json.dumps(
                {'error': "Pick one or more genes"}),
                                  content_type='text/json')

        curation_ids = []
        annotation_ids = []
        curation_id_to_gene = {}
        annotation_id_to_gene = {}
        if gene_id_list != '':
            gene_ids = gene_id_list.split(' ')
            for gene_id in gene_ids:
                [gene, curation_id, annotation_id] = gene_id.split('|')
                curation_id = int(curation_id)
                curation_ids.append(curation_id)
                if gene:
                    curation_id_to_gene[curation_id] = gene
                if annotation_id is not None and annotation_id.isdigit():
                    annotation_id = int(annotation_id)
                    annotation_ids.append(annotation_id)
                    if gene:
                        annotation_id_to_gene[annotation_id] = gene

        success_message = ""

        if unlink in ['tag', 'both']:
            for curation_id in curation_ids:
                x = curator_session.query(CurationReference).filter_by(
                    curation_id=curation_id).one_or_none()
                curator_session.delete(x)
                success_message = success_message + "The curation_tag <strong>" + tag + "</strong> is unlinked from gene <strong>" + curation_id_to_gene[
                    curation_id] + "</strong> and this paper. "

        if unlink in ['topic', 'both']:
            for annotation_id in annotation_ids:
                x = curator_session.query(Literatureannotation).filter_by(
                    annotation_id=annotation_id).one_or_none()
                curator_session.delete(x)
                success_message = success_message + "The topic <strong>" + topic + "</strong> is unlinked from gene <strong>" + annotation_id_to_gene[
                    annotation_id] + "</strong> and this paper. "

        if unlink == '':
            for curation_id in curation_ids:
                x = curator_session.query(CurationReference).filter_by(
                    curation_id=curation_id).one_or_none()
                gene = curation_id_to_gene.get(curation_id)
                if x.curation_tag != tag:
                    y = curator_session.query(CurationReference).filter_by(
                        curation_tag=tag,
                        reference_id=x.reference_id,
                        dbentity_id=x.dbentity_id).one_or_none()
                    if y is not None:
                        if gene:
                            success_message = success_message + "The tag <strong>" + tag + "</strong> is already linked with gene <strong>" + gene + "</strong> and this paper. "
                        else:
                            success_message = success_message + "The tag <strong>" + tag + "</strong> is alreadylinked with this paper. "
                    else:
                        if gene:
                            success_message = success_message + "The tag for gene <strong>" + gene + "</strong> has been changed from <strong>" + x.curation_tag + "</strong> to <strong>" + tag + "</strong>. "
                        else:
                            success_message = success_message + "The tag has been changed from <strong>" + x.curation_tag + "</strong> to <strong>" + tag + "</strong>. "
                        x.curation_tag = tag
                        curator_session.add(x)

            for annotation_id in annotation_ids:
                x = curator_session.query(Literatureannotation).filter_by(
                    annotation_id=annotation_id).one_or_none()
                if x.topic != topic:
                    gene = annotation_id_to_gene[annotation_id]
                    y = curator_session.query(Literatureannotation).filter_by(
                        topic=topic,
                        reference_id=x.reference_id,
                        dbentity_id=x.dbentity_id).one_or_none()
                    if y is not None:
                        success_message = success_message + "The topic <strong>" + topic + "</strong> is already linked with gene <strong>" + gene + "</strong> and this paper. "
                    else:
                        success_message = success_message + "The topic for gene <strong>" + gene + "</strong> has been changed from <strong>" + x.topic + "</strong> to <strong>" + topic + "</strong>. "
                        x.topic = topic
                        curator_session.add(x)

        if success_message == "":
            success_message = "Nothing is changed for tag/topic."
        success_message = success_message + " DONE!"
        transaction.commit()
        return HTTPOk(body=json.dumps({
            'success': success_message,
            'litguide': "LIT_GUIDE"
        }),
                      content_type='text/json')
    except Exception as e:
        return HTTPBadRequest(body=json.dumps(
            {'error': str(e) + ". DONE reporting ERROR!"}),
                              content_type='text/json')
示例#7
0
def upload_disease_file(request):

    try:
        file = request.POST['file'].file
        filename = request.POST['file'].filename
        CREATED_BY = request.session['username']
        xl = pd.ExcelFile(file)
        list_of_sheets = xl.sheet_names

        COLUMNS = {
            'taxonomy': 'Taxon',
            'gene': 'Gene',
            'disease_id': 'DOID',
            'with_ortholog':'With Ortholog',
            'eco_id': 'Evidence Code',
            'reference': 'DB:Reference',
            'created_by': 'Assigned By',    
        }

        SOURCE_ID = 834
        SEPARATOR = ','
        ANNOTATION_TYPE = 'high-throughput'

        list_of_diseases = []
        list_of_diseases_errors = []
        df = pd.read_excel(io=file, sheet_name="Sheet1")

        null_columns = df.columns[df.isnull().any()]
        for col in null_columns:
            if COLUMNS['with_ortholog'] != col and COLUMNS['evidence code'] !=col:
                rows = df[df[col].isnull()].index.tolist()
                rows = ','.join([str(r+2) for r in rows])
                list_of_diseases_errors.append('No values in column ' + col + ' rows ' + rows)
        
        if list_of_diseases_errors:
            err = [e + '\n' for e in list_of_diseases_errors]
            return HTTPBadRequest(body=json.dumps({"error": list_of_diseases_errors}), content_type='text/json')


        sgd_id_to_dbentity_id, systematic_name_to_dbentity_id = models_helper.get_dbentity_by_subclass(['LOCUS', 'REFERENCE'])
        strain_to_taxonomy_id = models_helper.get_common_strains()
        eco_displayname_to_id = models_helper.get_all_eco_mapping()
        doid_to_disease_id = models_helper.get_all_do_mapping()
        pubmed_id_to_reference, reference_to_dbentity_id = models_helper.get_references_all()

        for index_row,row in df.iterrows():
            index  = index_row + 2;
            column = ''
            try:
                disease_existing = {
                    'dbentity_id': '',
                    'source_id':SOURCE_ID,
                    'taxonomy_id': '',
                    'reference_id': '',
                    'eco': '',
                    'with_ortholog': None,
                    'disease_id': '',
                    'created_by': '',
                    'annotation_type': ANNOTATION_TYPE
                }
                disease_update = {
                    'annotation_type': ANNOTATION_TYPE
                }

                column = COLUMNS['gene']
                gene = row[column]
                gene_current = str(gene.strip())
                
                key = (gene_current,'LOCUS')
                if key in sgd_id_to_dbentity_id:
                    disease_existing['dbentity_id'] = sgd_id_to_dbentity_id[key]
                elif(key in systematic_name_to_dbentity_id):
                    disease_existing['dbentity_id'] = systematic_name_to_dbentity_id[key]
                else:
                    list_of_diseases_errors.append('Error in gene on row ' + str(index)+ ', column ' + column)
                    continue
                                       
                
                column = COLUMNS['reference']
                reference = row[column]
                reference_current = str(reference)
                key = (reference_current,'REFERENCE')
                if(key in sgd_id_to_dbentity_id):
                    disease_existing['reference_id'] = sgd_id_to_dbentity_id[key]
                elif(reference_current in pubmed_id_to_reference):
                    disease_existing['reference_id'] = pubmed_id_to_reference[reference_current]
                elif(reference_current in reference_to_dbentity_id):
                    disease_existing['reference_id'] = int(reference_current)
                else:
                    list_of_diseases_errors.append('Error in reference on row ' + str(index) + ', column ' + column)
                    continue
                 
                column = COLUMNS['taxonomy']
                taxonomy = row[column]
                taxonomy_current = str(taxonomy)
                if taxonomy_current in strain_to_taxonomy_id:
                    disease_existing['taxonomy_id'] = strain_to_taxonomy_id[taxonomy_current]
                else:
                    list_of_diseases_errors.append('Error in taxonomy on row ' + str(index) + ', column ' + column)
                    continue
                    
                column = COLUMNS['eco_id']
                eco = row[column]
                eco_current = str(eco)        

                column = COLUMNS['disease_id']
                disease_id = row[column]
                disease_id_current = str(disease_id).strip()
                if disease_id_current in doid_to_disease_id:
                    disease_existing['disease_id'] = doid_to_disease_id[disease_id_current]
                else:
                    list_of_diseases_errors.append('Error in disease_id on row ' + str(index) + ', column ' + column)
                    continue

                
                column = COLUMNS['with_ortholog']
                with_ortholog = row[column]
                with_ortholog_current = None if pd.isnull(with_ortholog) else None if not str(with_ortholog) else str(with_ortholog)
                disease_existing['with_ortholog'] = with_ortholog_current

                if not pd.isnull(with_ortholog):
                    with_ortholog_new = None if pd.isnull(with_ortholog) else None if not str(with_ortholog) else str(with_ortholog)
                    disease_existing['with_ortholog'] = with_ortholog_new

                list_of_diseases.append([disease_existing,disease_update])
            
            except Exception as e:
                list_of_diseases_errors.append('Error in on row ' + str(index) + ', column ' + column + ' ' + str(e))
        

        if list_of_diseases_errors:
            err = [e + '\n' for e in list_of_diseases_errors]
            logging.debug('Error in list_of_diseases_errors')
            return HTTPBadRequest(body=json.dumps({"error":list_of_diseases_errors}),content_type='text/json')
        
        INSERT = 0
        UPDATE = 0
        curator_session = get_curator_session(request.session['username'])
        isSuccess = False
        returnValue = ''
        
        if list_of_diseases:
            for item in list_of_diseases:
                disease, update_disease = item
                
                if (len(update_disease)>1):
                    disease_in_db = curator_session.query(Diseaseannotation).filter(and_(
                        Diseaseannotation.dbentity_id == disease['dbentity_id'],
                        Diseaseannotation.disease_id == disease['disease_id'],
                        Diseaseannotation.taxonomy_id == disease['taxonomy_id'],
                        Diseaseannotation.reference_id == disease['reference_id'],
                        Diseaseannotation.eco_id == disease['eco_id'],
                        Diseaseannotation.association_type == int(RO_ID),
                        Diseaseannotation.date_assigned == datetime.now(),
                        Diseaseannotation.created_by == disease['created_by']
                        )).one_or_none()
                    if disease_in_db is not None:
                        curator_session.query(Diseaseannotation).filter(and_(
                        Diseaseannotation.dbentity_id == disease['dbentity_id'],
                        Diseaseannotation.disease_id == disease['disease_id'],
                        Diseaseannotation.taxonomy_id == disease['taxonomy_id'],
                        Diseaseannotation.reference_id == disease['reference_id'],
                        Diseaseannotation.eco_id == disease['eco_id'],
                        Diseaseannotation.association_type == int(RO_ID),
                        Diseaseannotation.date_assigned == datetime.now(),
                        Diseaseannotation.created_by == disease['created_by']
                        )).update(update_disease)
                        curator_session.flush()
                        curator_session.query(Diseasesupportingevidence).filter(and_(
                        Diseasesupportingevidence.annotation_id == disease_in_db['annotation_id'],
                        )).update({'dbxref_id': disease['with_ortholog'], 'obj_url': OBJ_URL+disease['with_ortholog']})                      
                        UPDATE  = UPDATE + 1
                else:
                    codes = eco_current.split(';')
                    for code in codes:
                        code = code.strip()
                        if code in eco_displayname_to_id:
                            eco_id = eco_displayname_to_id[code]
                            disease['eco_id'] = eco_id
                            r = Diseaseannotation(
                                dbentity_id = disease['dbentity_id'],
                                disease_id = disease['disease_id'], 
                                source_id = SOURCE_ID,
                                taxonomy_id = disease['taxonomy_id'],
                                reference_id = disease['reference_id'], 
                                eco_id = disease['eco_id'],
                                association_type = int(RO_ID),
                                date_assigned = datetime.now(),
                                created_by = CREATED_BY,
                                annotation_type = disease['annotation_type']
                            )
                            curator_session.add(r)
                            curator_session.flush()
                            annotation_id = r.annotation_id
                            daf_evidence_row = Diseasesupportingevidence(
                                annotation_id=annotation_id,
                                group_id = GROUP_ID,
                                dbxref_id=disease['with_ortholog'],
                                obj_url=OBJ_URL+disease['with_ortholog'],
                                evidence_type = 'with',
                                created_by=CREATED_BY
                            )
                        curator_session.add(daf_evidence_row)
                        update_ldb = {'has_disease': 'true'}
                        curator_session.query(Locusdbentity).filter(Locusdbentity.dbentity_id == disease['dbentity_id']).update(update_ldb)
                        transaction.commit()
                        curator_session.flush()
                        INSERT = INSERT + 1            
            try:
                transaction.commit()
                err = '\n'.join(list_of_diseases_errors)
                isSuccess = True    
                returnValue = 'Inserted:  ' + str(INSERT) + ' <br />Updated: ' + str(UPDATE) + '<br />Errors: ' + err
            except IntegrityError as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Integrity Error: '+str(e.orig.pgerror)
            except DataError as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Data Error: '+str(e.orig.pgerror)
            except Exception as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = str(e)
            finally:
                if curator_session:
                    curator_session.close()
        
        if isSuccess:
            return HTTPOk(body=json.dumps({"success": returnValue}), content_type='text/json')
        
        return HTTPBadRequest(body=json.dumps({'error': returnValue}), content_type='text/json')    

    except Exception as e:
        return HTTPBadRequest(body=json.dumps({"error":str(e)}),content_type='text/json')
示例#8
0
def insert_update_disease_annotations(request):
    try:
        CREATED_BY = request.session['username']
        curator_session = get_curator_session(request.session['username'])
        source_id = 834
        annotation_id = request.params.get('annotation_id')
        dbentity_id = request.params.get('dbentity_id')
        if not dbentity_id:
            return HTTPBadRequest(body=json.dumps({'error': "gene is blank"}), content_type='text/json')

        taxonomy_id = request.params.get('taxonomy_id')
        if not taxonomy_id:
            return HTTPBadRequest(body=json.dumps({'error': "taxonomy is blank"}), content_type='text/json')

        reference_id = request.params.get('reference_id')
        if not reference_id:
            return HTTPBadRequest(body=json.dumps({'error': "reference is blank"}), content_type='text/json')

        eco_id = request.params.get('eco_id')
        if not eco_id:
            return HTTPBadRequest(body=json.dumps({'error': "eco is blank"}), content_type='text/json')
        
        disease_id = request.params.get('disease_id')
        if not disease_id:
            return HTTPBadRequest(body=json.dumps({'error': "disease_id is blank"}), content_type='text/json')       

        annotation_type = request.params.get('annotation_type')
        if not annotation_type:
            return HTTPBadRequest(body=json.dumps({'error': "annotation type is blank"}), content_type='text/json')

        with_ortholog = request.params.get('with_ortholog')
        if not with_ortholog:
            return HTTPBadRequest(body=json.dumps({'error': "with_ortholog is blank"}), content_type='text/json')
         
        try:
            dbentity_in_db = None
            dbentity_in_db = DBSession.query(Dbentity).filter(or_(Dbentity.sgdid == dbentity_id, Dbentity.format_name == dbentity_id)).filter(Dbentity.subclass == 'LOCUS').one_or_none()
            if dbentity_in_db is not None:
                dbentity_id = dbentity_in_db.dbentity_id
            else:
                return HTTPBadRequest(body=json.dumps({'error': "gene value not found in database"}), content_type='text/json')
        except Exception as e:
            print(e)
        
        dbentity_in_db = None
        pmid_in_db = None
        dbentity_in_db = DBSession.query(Dbentity).filter(and_(Dbentity.sgdid == reference_id,Dbentity.subclass == 'REFERENCE')).one_or_none()
        if dbentity_in_db is None:
            try:
                dbentity_in_db = DBSession.query(Dbentity).filter(and_(Dbentity.dbentity_id == int(reference_id), Dbentity.subclass == 'REFERENCE')).one_or_none()
            except ValueError as e:
                pass
        if dbentity_in_db is None:
            try:
                pmid_in_db = DBSession.query(Referencedbentity).filter(Referencedbentity.pmid == int(reference_id)).one_or_none()
            except ValueError as e:
                pass

        if dbentity_in_db is not None:
            reference_id = dbentity_in_db.dbentity_id
        elif (pmid_in_db is not None):
            reference_id = pmid_in_db.dbentity_id
        else:
            return HTTPBadRequest(body=json.dumps({'error': "reference value not found in database"}), content_type='text/json')
        
        isSuccess = False
        returnValue = ''
        disease_in_db = []

        if (int(annotation_id) > 0):
            
            try:
                update_disease = {'dbentity_id': dbentity_id,
                                    'source_id': source_id,
                                    'taxonomy_id': taxonomy_id,
                                    'reference_id': reference_id,
                                    'eco_id': eco_id,
                                    'association_type': int(RO_ID),
                                    'annotation_type': annotation_type,
                                    'disease_id': disease_id
                                    }
                update_dse = {'dbxref_id': with_ortholog,
                               'obj_url': OBJ_URL + with_ortholog}
                curator_session.query(Diseaseannotation).filter(Diseaseannotation.annotation_id == annotation_id).update(update_disease)
                curator_session.query(Diseasesupportingevidence).filter(Diseasesupportingevidence.annotation_id == annotation_id).update(update_dse)
                curator_session.flush()
                transaction.commit()
               
                isSuccess = True
                returnValue = 'Record updated successfully.'
                
                disease = curator_session.query(Diseaseannotation).filter(Diseaseannotation.annotation_id == annotation_id).one_or_none()
                dse = curator_session.query(Diseasesupportingevidence).filter(Diseasesupportingevidence.annotation_id == annotation_id).one_or_none()

                disease_in_db = {
                    'id': disease.annotation_id,
                    'dbentity_id': {
                        'id': disease.dbentity.format_name,
                        'display_name': disease.dbentity.display_name
                    },
                    'taxonomy_id': '',
                    'reference_id': disease.reference.pmid,
                    'eco_id': disease.eco_id,
                    'association_type': disease.association_type,
                    'source_id': disease.source_id,
                    'with_ortholog': dse.dbxref_id,
                    'annotation_type': disease.annotation_type,
                }
                if disease.eco:
                    disease_in_db['eco_id'] = str(disease.eco.eco_id)

                if disease.taxonomy:
                    disease_in_db['taxonomy_id'] = disease.taxonomy.taxonomy_id


            except IntegrityError as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Integrity Error: Update failed, record already exists' + str(e.orig.pgerror)
            except DataError as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Data Error: ' + str(e.orig.pgerror)
            except InternalError as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                error = str(e.orig).replace('_', ' ')
                error = error[0:error.index('.')]
                returnValue = 'Updated failed, ' + error
            except Exception as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Updated failed, ' + str(e)
            finally:
                if curator_session:
                    curator_session.close()

        if(int(annotation_id) == 0):
            try:
                y = None
                date_created = datetime.now()
                y = Diseaseannotation(dbentity_id = dbentity_id,
                                    source_id = source_id,
                                    taxonomy_id = taxonomy_id,
                                    reference_id = reference_id,
                                    eco_id = eco_id,
                                    disease_id = disease_id,
                                    association_type = int(RO_ID),
                                    annotation_type = annotation_type,
                                    created_by = CREATED_BY,
                                    date_assigned = date_created)
                curator_session.add(y)
                curator_session.flush()
                dse = Diseasesupportingevidence(
                                annotation_id=y.annotation_id,
                                group_id = GROUP_ID,
                                dbxref_id=with_ortholog,
                                obj_url=OBJ_URL+with_ortholog,
                                evidence_type = 'with',
                                created_by=CREATED_BY)
                curator_session.add(dse)
                update_ldb = {'has_disease': 'true'}
                curator_session.query(Locusdbentity).filter(Locusdbentity.dbentity_id == dbentity_id).update(update_ldb)
                transaction.commit()
                isSuccess = True
                returnValue = 'Record added successfully.'
            except IntegrityError as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Integrity Error: ' + str(e.orig.pgerror)
            except DataError as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Data Error: ' + str(e.orig.pgerror)
            except Exception as e:
                transaction.abort()
                if curator_session:
                    curator_session.rollback()
                isSuccess = False
                returnValue = 'Insert failed ' +str(e)
            finally:
                if curator_session:
                    curator_session.close()

        if isSuccess:
            return HTTPOk(body=json.dumps({'success': returnValue,'disease':disease_in_db}), content_type='text/json')

        return HTTPBadRequest(body=json.dumps({'error': returnValue}), content_type='text/json')

    except Exception as e:
        return HTTPBadRequest(body=json.dumps({'error': str(e)}), content_type='text/json')