Skip to content

GraphKB Variant Matching Tutorial

Open In Colab

This tutorial is an interactive notebook which can be run using google colab or a local jupyter server (recommended if matching patient data). This tutorial will cover basic matching of variants using the python GraphKB adapter against an instance of the GraphKB API.

Users must first have login credentials to an instance of GraphKB API (or use the demo server). Note for users using the demo credentials and server, the data is limited and more complete annotations would be expected for a production instance of GraphKB.

For the purposes of this tutorial we will be matching the known KRAS variant p.G12D to the demo instance of GraphKB. You can adjust the API instance by changing the setup variables below

To run this locally, download this file and start the server from the command line as follows

jupyter notebook notebook.ipynb

You should now be able to see the notebook by opening http://localhost:8888 in your browser

!pip3 install graphkb
Looking in indexes: https://pypi.bcgsc.ca/gsc/packages/
Requirement already satisfied: graphkb in /projects/dat/workspace/creisle/graphkb/graphkb_python (1.5.1)
Requirement already satisfied: requests<3,>=2.22.0 in /projects/dat/workspace/creisle/graphkb/graphkb_python/venv/lib/python3.8/site-packages (from graphkb) (2.22.0)
Requirement already satisfied: typing_extensions<4,>=3.7.4.2 in /projects/dat/workspace/creisle/graphkb/graphkb_python/venv/lib/python3.8/site-packages (from graphkb) (3.7.4.2)
Requirement already satisfied: certifi>=2017.4.17 in /projects/dat/workspace/creisle/graphkb/graphkb_python/venv/lib/python3.8/site-packages (from requests<3,>=2.22.0->graphkb) (2020.4.5.1)
Requirement already satisfied: idna<2.9,>=2.5 in /projects/dat/workspace/creisle/graphkb/graphkb_python/venv/lib/python3.8/site-packages (from requests<3,>=2.22.0->graphkb) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /projects/dat/workspace/creisle/graphkb/graphkb_python/venv/lib/python3.8/site-packages (from requests<3,>=2.22.0->graphkb) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /projects/dat/workspace/creisle/graphkb/graphkb_python/venv/lib/python3.8/site-packages (from requests<3,>=2.22.0->graphkb) (1.25.9)
WARNING: You are using pip version 19.2.3, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

from graphkb import GraphKBConnection

GKB_API_URL = 'https://pori-demo.bcgsc.ca/graphkb-api/api'
GKB_USER = 'colab_demo'
GKB_PASSWORD = 'colab_demo'

graphkb_conn = GraphKBConnection(GKB_API_URL, use_global_cache=False)

graphkb_conn.login(GKB_USER, GKB_PASSWORD)

Matching Variants

Now you are ready to match variants

from graphkb.match import match_positional_variant

variant_name = 'KRAS:p.G12D'
variant_matches = match_positional_variant(graphkb_conn, variant_name)

print(f'{variant_name} matched {len(variant_matches)} other variant representations')
print()

for match in variant_matches:
    print(variant_name, 'will match', match['displayName'])
KRAS:p.G12D matched 7 other variant representations

KRAS:p.G12D will match KRAS:p.(G12_G13)mut
KRAS:p.G12D will match KRAS:p.G12mut
KRAS:p.G12D will match KRAS:p.G12D
KRAS:p.G12D will match chr12:g.25398284C>T
KRAS:p.G12D will match KRAS:p.G12
KRAS:p.G12D will match KRAS:p.?12mut
KRAS:p.G12D will match KRAS mutation

We can see above that the KRAS protein variant has been matched to a number of other less specific mentions (ex. KRAS:p.G12mut) and also genomic equivalents (chr12:g.25398284C>T). Note that the results here will be dependent on the instance of GraphKB you are accessing.

Annotating Variants

Now that we have matched the variant we will fetch the related statements to annotate this variant with its possible relevance

from graphkb.constants import BASE_RETURN_PROPERTIES, GENERIC_RETURN_PROPERTIES
from graphkb.util import convert_to_rid_list

# return properties should be customized to the users needs
return_props = (
    BASE_RETURN_PROPERTIES
    + ['sourceId', 'source.name', 'source.displayName']
    + [f'conditions.{p}' for p in GENERIC_RETURN_PROPERTIES]
    + [f'subject.{p}' for p in GENERIC_RETURN_PROPERTIES]
    + [f'evidence.{p}' for p in GENERIC_RETURN_PROPERTIES]
    + [f'relevance.{p}' for p in GENERIC_RETURN_PROPERTIES]
    + [f'evidenceLevel.{p}' for p in GENERIC_RETURN_PROPERTIES]
)

statements = graphkb_conn.query(
    {
        'target': 'Statement',
        'filters': {'conditions': convert_to_rid_list(variant_matches), 'operator': 'CONTAINSANY'},
        'returnProperties': return_props,
    }
)
print(f'annotated {len(variant_matches)} variant matches with {len(statements)} statements')
print()

for statement in statements[:5]:
    print(
        [c['displayName'] for c in statement['conditions'] if c['@class'].endswith('Variant')],
        statement['relevance']['displayName'],
        statement['subject']['displayName'],
        statement['source']['displayName'] if statement['source'] else '',
        [c['displayName'] for c in statement['evidence']],
    )
annotated 7 variant matches with 96 statements

['KRAS:p.(G12_G13)mut'] resistance Gefitinib [c1855] CIViC ['pmid:15696205']
['KRAS:p.(G12_G13)mut'] resistance Panitumumab [c1857] CIViC ['pmid:19223544']
['KRAS:p.(G12_G13)mut'] resistance Cetuximab [c1723] CIViC ['pmid:19223544']
['KRAS:p.(G12_G13)mut'] resistance Cetuximab [c1723] CIViC ['pmid:19603024']
['KRAS:p.(G12_G13)mut'] resistance Panitumumab [c1857] CIViC ['pmid:18316791']

Categorizing Statements

Something we often want to know is if a statement is therapeutic, or prognostic, etc. The naive approach is to base this on a list of known terms or a regex pattern. In GraphKB we can leverage the ontology structure instead.

In this example we will look for all terms that would indicate a therapeutically relevent statement.

To do this we pick our 'base' terms. These are the terms we consider to be the highest level of the ontology tree, the most general term for that category.

from graphkb.vocab import get_term_tree


BASE_THERAPEUTIC_TERMS = 'therapeutic efficacy'

therapeutic_terms = get_term_tree(graphkb_conn, BASE_THERAPEUTIC_TERMS, include_superclasses=False)

print(f'Found {len(therapeutic_terms)} equivalent terms')

for term in therapeutic_terms:
    print('-', term['name'])
Found 13 equivalent terms
- therapeutic efficacy
- targetable
- response
- sensitivity
- likely sensitivity
- no sensitivity
- no response
- resistance
- reduced sensitivity
- likely resistance
- innate resistance
- acquired resistance
- no resistance

We can filter the statements we have already retrieved, or we can add this to our original query and filter before we retrive from the API

statements = graphkb_conn.query(
    {
        'target': 'Statement',
        'filters': {
            'AND': [
                {'conditions': convert_to_rid_list(variant_matches), 'operator': 'CONTAINSANY'},
                {'relevance': convert_to_rid_list(therapeutic_terms), 'operator': 'IN'},
            ]
        },
        'returnProperties': return_props,
    }
)

for statement in statements:
    print(
        [c['displayName'] for c in statement['conditions'] if c['@class'].endswith('Variant')],
        statement['relevance']['displayName'],
        statement['subject']['displayName'],
        statement['source']['displayName'] if statement['source'] else '',
        [c['displayName'] for c in statement['evidence']],
    )
['KRAS:p.G12mut'] response mek inhibitor [c69145] CGI ['pmid:18701506']
['KRAS:p.G12D'] sensitivity dactolisib + selumetinib CIViC ['pmid:19029981']
['KRAS mutation'] sensitivity Decitabine [c981] CIViC ['pmid:25968887']
['KRAS mutation'] sensitivity Trametinib [c77908] CIViC ['pmid:22169769']
['KRAS:p.G12D'] sensitivity Akt Inhibitor MK2206 [c90581] CIViC ['pmid:22025163']
['KRAS mutation'] sensitivity cetuximab + dasatinib CIViC ['pmid:20956938']
['KRAS mutation'] sensitivity b-raf/vegfr-2 inhibitor raf265 + selumetinib CIViC ['pmid:25199829']
['KRAS mutation'] sensitivity b-raf/vegfr-2 inhibitor raf265 + selumetinib CIViC ['pmid:25199829']
['KRAS mutation'] sensitivity afatinib + trametinib CIViC ['pmid:24685132']
['KRAS mutation'] sensitivity afatinib + trametinib CIViC ['pmid:24685132']
['KRAS mutation'] sensitivity docetaxel + selumetinib CIViC ['pmid:23200175']
['KRAS mutation'] sensitivity selumetinib + teprotumumab CIViC ['pmid:21985784']
['KRAS mutation'] sensitivity MEK Inhibitor GDC-0623 [c95738] CIViC ['pmid:23934108']
['KRAS mutation'] sensitivity Trametinib [c77908] CIViC ['pmid:22805291']
['KRAS mutation'] sensitivity refametinib + sorafenib CIViC ['pmid:25294897']
['KRAS:p.G12D'] sensitivity Therapeutic Tumor Infiltrating Lymphocytes [c28699] CIViC ['pmid:27959684']
['KRAS:p.G12D'] sensitivity dactolisib + selumetinib CIViC ['pmid:22392911']
['KRAS mutation'] sensitivity erlotinib + teprotumumab CIViC ['pmid:22025157']
['KRAS mutation'] sensitivity abemaciclib [c97660] CIViC ['pmid:27217383']
['KRAS mutation'] sensitivity docetaxel + pemetrexed + trametinib CIViC ['pmid:27876675']
['KRAS mutation'] sensitivity Atezolizumab [c106250] CIViC ['pmid:28525386']
['KRAS mutation'] sensitivity Nivolumab [c68814] CIViC ['pmid:28525386']
['KRAS mutation'] sensitivity Immune Checkpoint Inhibitor [c143250] CIViC ['pmid:28259530']
['KRAS mutation'] sensitivity binimetinib + palbociclib CIViC ['pmid:27167191']
['KRAS mutation'] sensitivity Metformin [c61612] CIViC ['pmid:32444490']
['KRAS mutation'] sensitivity Binimetinib [c84865] CIViC ['pmid:32822286']
['KRAS:p.(G12_G13)mut'] resistance Cetuximab [c1723] CIViC ['pmid:18202412']
['KRAS:p.(G12_G13)mut'] resistance cetuximab + chemotherapy CIViC ['pmid:20619739']
['KRAS:p.(G12_G13)mut'] resistance Erlotinib [c65530] CIViC ['pmid:15696205']
['KRAS:p.(G12_G13)mut'] resistance Gefitinib [c1855] CIViC ['pmid:15696205']
['KRAS:p.(G12_G13)mut'] resistance gemcitabine + trametinib CIViC ['pmid:24915778']
['KRAS mutation'] resistance bevacizumab + chemotherapy CIViC ['pmid:23828442']
['KRAS mutation'] resistance ixazomib [c97940] CIViC ['pmid:26709701']
['KRAS:p.G12D'] resistance Vemurafenib [c64768] CIViC ['pmid:26352686']
['KRAS mutation'] resistance Cetuximab [c1723] CIViC ['pmid:22586653']
['KRAS:p.(G12_G13)mut'] resistance Panitumumab [c1857] CIViC ['pmid:18316791']
['KRAS:p.G12D'] resistance Panitumumab [c1857] CIViC ['pmid:20619739']
['KRAS:p.G12D'] resistance Cetuximab [c1723] CIViC ['pmid:20619739']
['KRAS:p.G12D'] resistance Panitumumab [c1857] CIViC ['pmid:18316791']
['KRAS:p.G12D'] resistance Gefitinib [c1855] CIViC ['pmid:17409929']
['KRAS mutation'] resistance Erlotinib [c65530] CIViC ['pmid:22025157']
['KRAS:p.G12D'] resistance Melphalan [c633] CIViC ['pmid:19284554']
['KRAS:p.G12D'] resistance Melphalan [c633] CIViC ['pmid:11050000']
['KRAS:p.G12D'] resistance Melphalan [c633] CIViC ['pmid:12483530']
['KRAS:p.G12D'] resistance Melphalan [c633] CIViC ['pmid:16497971']
['KRAS mutation'] resistance Erlotinib [c65530] CIViC ['pmid:21258250']
['KRAS mutation'] resistance Gefitinib [c1855] CIViC ['pmid:21258250']
['KRAS mutation'] resistance docetaxel + selumetinib CIViC ['pmid:28492898']
['KRAS mutation'] resistance cetuximab + chemotherapy CIViC ['pmid:20619739']
['KRAS mutation'] resistance Erlotinib [c65530] CIViC ['pmid:21969500']
['KRAS:p.G12D'] resistance Regorafenib [c78204] CIViC ['pmid:26161928']
['KRAS:p.G12D'] resistance Vemurafenib [c64768] CIViC ['pmid:24265155']
['KRAS:p.G12D'] resistance Cetuximab [c1723] CIViC ['pmid:22246397']
['KRAS mutation'] resistance Panitumumab [c1857] CIViC ['pmid:28275037']
['KRAS mutation'] resistance Cetuximab [c1723] CIViC ['pmid:28275037']
['KRAS mutation'] resistance Vemurafenib [c64768] CIViC ['pmid:24265155']
['KRAS mutation'] resistance Dabrafenib [c82386] CIViC ['pmid:24265155']
['KRAS:p.(G12_G13)mut'] resistance Cetuximab [c1723] CIViC ['pmid:19603024']
['KRAS:p.G12D'] resistance Cetuximab [c1723] CIViC ['pmid:19223544']
['KRAS:p.G12D'] resistance Panitumumab [c1857] CIViC ['pmid:19223544']
['KRAS:p.(G12_G13)mut'] resistance Cetuximab [c1723] CIViC ['pmid:19223544']
['KRAS:p.(G12_G13)mut'] resistance Panitumumab [c1857] CIViC ['pmid:19223544']
['KRAS:p.?12mut'] resistance Cetuximab [c1723] CGI ['CGI']
['KRAS:p.?12mut'] resistance Panitumumab [c1857] CGI ['CGI']


Back to top