def id_url(ag): # Return identifier URLs in a prioritized order # TODO: we should add handling for UPPRO here, however, that would require # access to UniProt client resources in the context of the DB REST API # which could be problematic for db_name in link_namespace_order: if db_name in ag.db_refs: # Handle a special case where a list of IDs is given if isinstance(ag.db_refs[db_name], list): db_id = ag.db_refs[db_name][0] if db_name == 'WM': db_id = db_id[0] else: db_id = ag.db_refs[db_name] # We can add more name spaces here if there are issues if db_name in {'CHEBI'}: db_id = ensure_prefix('CHEBI', db_id) # Here we validate IDs to make sure we don't surface invalid # links. if not validate_id(db_name, db_id): logger.debug('Invalid grounding encountered: %s:%s' % (db_name, db_id)) continue # Finally, we return a valid identifiers.org URL return get_identifiers_url(db_name, db_id)
def extract_context(annotations, annot_manager): """Return a BioContext object extracted from the annotations. The entries that are extracted into the BioContext are popped from the annotations. Parameters ---------- annotations : dict PyBEL annotations dict annot_manager : AnnotationManager An annotation manager to get name/db reference mappings for each ot the annotation types. Returns ------- bc : BioContext An INDRA BioContext object """ def get_annot(annotations, key): """Return a specific annotation given a key.""" val = annotations.pop(key, None) if val: val_list = [v for v, tf in val.items() if tf] if len(val_list) > 1: logger.warning('More than one "%s" in annotations' % key) elif not val_list: return None return val_list[0] return None bc = BioContext() species = get_annot(annotations, 'Species') if species: name = annot_manager.get_mapping('Species', species) bc.species = RefContext(name=name, db_refs={'TAXONOMY': species}) mappings = (('CellLine', 'cell_line', None), ('Disease', 'disease', None), ('Anatomy', 'organ', None), ('Cell', 'cell_type', None), ('CellStructure', 'location', 'MESH')) for bel_name, indra_name, ns in mappings: ann = get_annot(annotations, bel_name) if ann: ref = annot_manager.get_mapping(bel_name, ann) if ref is None: continue if not ns: db_ns, db_id = ref.split('_', 1) else: db_ns, db_id = ns, ref if db_ns == 'CLO': db_ns = 'CL' if db_ns in {'CL', 'UBERON', 'DOID'}: db_id = identifiers.ensure_prefix(db_ns, db_id) setattr(bc, indra_name, RefContext(name=ann, db_refs={db_ns: db_id})) # Overwrite blank BioContext if not bc: bc = None return bc
def get_ref_context(db_ns, db_id): db_id = db_id.strip() if db_ns in {'BTO'}: db_id = ensure_prefix(db_ns, db_id) standard_name, db_refs = standardize_name_db_refs({db_ns: db_id}) return RefContext(standard_name, db_refs)