SPARQL engine configuration


In [2]:
PC_Endpoint = \
  "http://rdf.pathwaycommons.org/sparql"

We will use the sparqlwrapper library. Executing the sparql queries is straightforward (cf. the first four lines of runQuery(...) below), but we introduce some auxiliary functions for nicely displaying the results.


In [3]:
from SPARQLWrapper import SPARQLWrapper, JSON
from IPython.display import display, Markdown 
    # for telling jupyter to display the result as markdown

def runQuery(queryString, outputFormat="tsv", varList=[], truncateAt=30):
    """ Send the query to the endpoint and attempt 
    to nicely display the result.
    
    Possible values for outputFormat: "tsv", "markdown"
    """
    sparql = SPARQLWrapper(PC_Endpoint)
    sparql.setQuery(queryString)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    if outputFormat == "tsv":
        displayQueryResultAsTSV(results, varList)
    elif outputFormat == "markdown":
        displayQueryResultAsMarkdown(results, varList, truncateAt)

def displayQueryResultAsTSV(queryResult, varList=[], truncateAt=30):
    if len(queryResult["results"]["bindings"]) == 0:
        print("Empty result")
        return
    if varList == []:
        varList = [varName for varName in queryResult["results"]["bindings"][0].keys()]
    displayResult = ""
    for currentVar in varList:
        displayResult += currentVar + "\t"
    displayResult = displayResult[:-1] + "\n"
    for result in queryResult["results"]["bindings"]:
        for currentVar in varList:
            if currentVar in result.keys():
                displayResult += truncateString(result[currentVar]['value'], truncateAt) + "\t"
            else:
                displayResult += "\t"
        displayResult = displayResult[:-1] + "\n"
    print(displayResult)

def displayQueryResultAsMarkdown(queryResult, varList=[], truncateAt=30):
    if len(queryResult["results"]["bindings"]) == 0:
        print("Empty result")
        return
    if varList == []:
        varList = [varName for varName in queryResult["results"]["bindings"][0].keys()]
    displayResult = ""
    sepLine = ""
    for currentVar in varList:
        displayResult += " | " + currentVar
        sepLine += "| ---"
    displayResult += "\n" + sepLine + "\n"
    for result in queryResult["results"]["bindings"]:
        for currentVar in varList:
            if currentVar in result.keys():
                displayResult += "| " + truncateString(result[currentVar]['value'], truncateAt) + " "
            else:
                displayResult += "|  "
        displayResult += " \n"
    display(Markdown(displayResult))

def truncateString(message, length=30):
    if (length == -1) or (len(message) <= length):
        return message
    return message[:15] + "..." + message[-12:]

For the sake of clarity, we will define the usual prefixes in the commonPrefixes variable.


In [17]:
commonPrefixes = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX bp3: <http://www.biopax.org/release/biopax-level3.owl#>
PREFIX taxon: <http://identifiers.org/taxonomy/>
PREFIX reactome: <http://identifiers.org/reactome/>
PREFIX release: <http://www.reactome.org/biopax/49/48887#>

PREFIX up: <http://purl.uniprot.org/core/> 
PREFIX uniprot: <http://purl.uniprot.org/uniprot/>
PREFIX bp: <http://www.biopax.org/release/biopax-level3.owl#>
PREFIX chebi: <http://purl.obolibrary.org/obo/CHEBI_>
PREFIX obo2: <http://purl.obolibrary.org/obo#>

"""

Live PathwayCommons queries


In [15]:
queryTFControllers = """
SELECT ?tempReac ?type ?controlledName ?controllerName ?source WHERE{ 
    FILTER( (?controlledName = 'Transcription of SCN5A'^^<http://www.w3.org/2001/XMLSchema#string>)
        and (?controllerName != 'SCN5A')
        and (?source != 'mirtarbase') ) .
    ?tempReac a bp:TemplateReactionRegulation .
    ?tempReac bp:displayName ?reacName ; 
        bp:controlled ?controlled ; 
        bp:controller ?controller ; 
        bp:controlType ?type ; 
        bp:dataSource ?source .
    ?controlled bp:displayName ?controlledName .
    ?controller bp:displayName ?controllerName . }
GROUP BY ?controlledName ?controllerName
"""
runQuery(commonPrefixes + queryTFControllers, "markdown", [])


controllerName type source tempReac controlledName
REPIN1 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...ff97bc8f97a5 Transcription of SCN5A
TCF3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...a88033fa5a29 Transcription of SCN5A
MYB ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...5900714d2b9e Transcription of SCN5A
EGR3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...bab294e9646c Transcription of SCN5A
ESRRA ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...3fe2961481f1 Transcription of SCN5A
ESRRA ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...e4a28c60ee6f Transcription of SCN5A
TFAP2C ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...d2eae99095cd Transcription of SCN5A
JUN ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...5c2d68c57812 Transcription of SCN5A
SP3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...4d4d1e1a03c5 Transcription of SCN5A
TCF3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...aaabee8d8806 Transcription of SCN5A
TCF3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...9fa6ec03fe6d Transcription of SCN5A
TCF3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...27a80b256982 Transcription of SCN5A
TCF3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...21ad5a88fc55 Transcription of SCN5A
TFAP2A ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...16bef8806f83 Transcription of SCN5A
TCF3 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...2432667540ab Transcription of SCN5A
EGR2 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...203810ddf863 Transcription of SCN5A
SF1 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...2984d6aa58ee Transcription of SCN5A
MAZ ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...0624a07f2b22 Transcription of SCN5A
ZEB1 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...bf8d03d91ec0 Transcription of SCN5A
MYOD1 ACTIVATION http://pathwayc...pc2/transfac http://pathwayc...ba3ae6babac4 Transcription of SCN5A

In [ ]: