Beispiel #1
0
def lookup_approx(term):
    """
    Performs an approximate lookup and optionally returns best result
    """
    resp = rx.get('/approximateTerm.json', params=dict(term=term))
    resp.raise_for_status()

    data = resp.json()
    candidates = data.get('approximateGroup').get('candidate')

    if candidates is None or len(candidates) == 0:
        return []

    rank_lookup = {}
    for candidate in candidates:
        cui = candidate.get('rxcui')

        if cui not in rank_lookup:
            rank_lookup[cui] = candidate

    cleaned_candidates = []
    for cui, candidate in rank_lookup.items():
        rank = int(candidate.get('rank'))
        score = int(candidate.get('score'))

        cleaned_candidates.append(Candidate(rank=rank, score=score, cui=cui))

    cleaned_candidates.sort(key=lambda canidate: canidate.rank)
    return cleaned_candidates
Beispiel #2
0
def related_by_types(cui, types):
    path = "/rxcui/{}/related.json".format(cui)

    resp = rx.get(path, params=dict(tty=" ".join(types)))
    resp.raise_for_status()

    data = resp.json()

    groups = data.get("relatedGroup")
    concept_groups = groups.get("conceptGroup")

    if concept_groups is None:
        return []

    rel_concepts = []

    # Iterates over different types
    for concept_group in concept_groups:
        concept_props = concept_group.get("conceptProperties")
        if concept_props is None:
            continue

        for concept_prop in concept_props:
            rel_concepts.append(
                RelatedConcept(name=concept_prop.get("name"),
                               synonym=concept_prop.get("synonym"),
                               cui=concept_prop.get("rxcui"),
                               tty=concept_prop.get("tty")))

    return rel_concepts
Beispiel #3
0
def get_tree_raw(cid):
    path = "/rxclass/classTree.json"

    resp = rx.get(path, params=dict(classId=cid))
    resp.raise_for_status()

    data = resp.json()
    return data
Beispiel #4
0
def lookup_ndc(ndc):
    """
    Returns the id associated with a drug given its's NDC code
    """
    resp = rx.get('/rxcui.json', params=dict(idtype="NDC", id=ndc))
    resp.raise_for_status()

    data = resp.json()
    return extract_cui(data)
Beispiel #5
0
def lookup_name(name):
    """
    Returns the id associated with a drug given its's name
    """
    resp = rx.get('/rxcui.json', params=dict(name=name))
    resp.raise_for_status()

    data = resp.json()
    return extract_cui(data)
Beispiel #6
0
def with_list(cuis):
    """
    Gets all interactions between drugs in the list `cuis`
    """
    path = "/interaction/list.json"

    resp = rx.get(path, params=dict(rxcuis=" ".join(cuis)))
    resp.raise_for_status()

    data = resp.json().get("fullInteractionTypeGroup")
    if data is None:
        return {}

    interactions = []
    for group in data:
        types = group.get("fullInteractionType")

        for typ in types:
            concepts = typ.get("minConcept")

            drug1 = InteractionDrugInfo(
                name = concepts[0].get("name"),
                tty = concepts[0].get("tty"),
                cui = concepts[0].get("rxcui")
            )

            drug2 = InteractionDrugInfo(
                name = concepts[1].get("name"),
                tty = concepts[1].get("tty"),
                cui = concepts[1].get("rxcui")
            )

            if drug1.cui == drug2.cui:
                continue

            pairs = typ.get("interactionPair")
            for pair in pairs:
                interactions.append(
                    InteractionInList(
                        drug1 = drug1,
                        drug2 = drug2,
                        info = InteractionInfo(
                            desc = pair.get("description"),
                            severity = pair.get("severity")
                        )
                    )
                )

    # Now, merge interactions between the same two drugs into grouped lists
    merged_interactions = col.defaultdict(list)
    for inter in interactions:
        pair = [inter.drug1, inter.drug2]
        pair.sort(key=lambda drug: drug.cui)

        merged_interactions[tuple(pair)].append(inter.info)
    
    return merged_interactions
Beispiel #7
0
def props(cui):
    """
    Gets associated properties for a CUI, including its type and name
    """
    path = "/rxcui/{}/properties.json".format(cui)

    data = rx.get(path, params={})
    data.raise_for_status()

    return data.json().get('properties')
Beispiel #8
0
def classes_for_raw(cui):
    """
    Returns all classes that a given RxCui belongs to, along with related concepts 
    """
    path = "/rxclass/class/byRxcui.json"

    resp = rx.get(path, params=dict(rxcui=cui))
    resp.raise_for_status()

    data = resp.json().get('rxclassDrugInfoList').get('rxclassDrugInfo')
    return data
Beispiel #9
0
def classes_of_types(types):
    """
    Returns all classes of a given type
    """
    path = "/rxclass/allClasses.json"

    resp = rx.get(path, params=dict(classTypes=" ".join(types)))
    resp.raise_for_status()

    classes = []
    concepts = resp.json().get("rxclassMinConceptList").get(
        "rxclassMinConcept")

    for concept in concepts:
        classes.append(
            ClassDef(name=concept.get("className"),
                     cid=concept.get("classId"),
                     typ=concept.get("classType")))

    return classes
Beispiel #10
0
def cuis_with_class(cid, rel=None, types=None):
    path = "/rxclass/classMembers.json"

    # TODO use different sources for relations
    params = dict(classId=cid, relaSource="NDFRT", rela=rel, trans=0)

    if types is not None:
        params["ttys"] = " ".join(types)

    resp = rx.get(path, params=params)
    resp.raise_for_status()

    data = resp.json().get("drugMemberGroup").get("drugMember")

    concepts = []
    for node in data:
        concept = node.get("minConcept")
        concepts.append(
            Concept(name=concept.get("name"),
                    cui=concept.get("rxcui"),
                    tty=concept.get("tty")))

    return concepts
Beispiel #11
0
def for_cui(cui):
    """
    Gets all interactions with a given drug
    """
    path = "/interaction/interaction.json"

    resp = rx.get(path, params=dict(rxcui=cui))
    resp.raise_for_status()

    data = resp.json().get("interactionTypeGroup")

    interactions = []
    for group in data:
        types = group.get("interactionType")

        for typ in types:
            pairs = typ.get("interactionPair")

            for pair in pairs:
                concept = pair.get("interactionConcept")
                other_drug = concept[1].get("minConceptItem")

                interactions.append(
                    InteractionForCui(
                        drug = InteractionDrugInfo(
                            name = other_drug.get("name"),
                            cui = other_drug.get("rxcui"),
                            tty = other_drug.get("tty")
                        ),
                        info = InteractionInfo(
                            desc = pair.get("description"),
                            severity = pair.get("severity")
                        )
                    )
                )
    
    return interactions
Beispiel #12
0
def class_by_name(name, types=None):
    path = "/rxclass/class/byName.json"

    params = dict(className=name)
    if types is not None:
        params["types"] = " ".join(types)

    resp = rx.get(path, params=params)
    resp.raise_for_status()

    data = resp.json()
    concepts = data.get("rxclassMinConceptList")

    if concepts is None:
        return None

    concepts = concepts.get("rxclassMinConcept")
    if len(concepts) == 0:
        return None

    concept = concepts[0]
    return ClassDef(name=concept.get("className"),
                    cid=concept.get("classId"),
                    typ=concept.get("classType"))