aboutsummaryrefslogtreecommitdiff
path: root/gn3/db/rdf.py
blob: 96aa0425548e40fd7d62c14cc952b5f83aad3de3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""RDF utilities

This module is a collection of functions that handle SPARQL queries.

"""
from typing import Tuple
from string import Template
from urllib.parse import unquote
from urllib.parse import urlparse

from SPARQLWrapper import JSON, SPARQLWrapper
from pymonad.maybe import Just, Nothing

from gn3.monads import MonadicDict


RDF_PREFIXES = """PREFIX dct: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX generif: <http://www.ncbi.nlm.nih.gov/gene?cmd=Retrieve&dopt=Graphics&list_uids=>
PREFIX gn: <http://genenetwork.org/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX pubmed: <http://rdf.ncbi.nlm.nih.gov/pubmed/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ncbiTaxon: <https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=>
PREFIX up: <http://purl.uniprot.org/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

"""


def sparql_query(
        sparql_conn: SPARQLWrapper, query: str
) -> Tuple[MonadicDict, ...]:
    """Run a SPARQL query and return the bound variables."""
    sparql_conn.setQuery(query)
    sparql_conn.setReturnFormat(JSON)
    results = sparql_conn.queryAndConvert()
    if _r := results["results"]["bindings"]:  # type: ignore
        return (*(MonadicDict(bindings) for bindings in _r),)  # type: ignore
    return (MonadicDict(),)


def strip_url(string: str) -> str:
    """Get the last item after a '/" from a URL"""
    if string.startswith("http"):
        url = urlparse(string)
        return unquote(url.path).rpartition("/")[-1]
    return string


def get_dataset_metadata(
        sparql_conn: SPARQLWrapper, name: str
) -> MonadicDict:
    """Return info about dataset with a given NAME"""
    __metadata_query = """
$prefix

CONSTRUCT {
    gn:dataset ?datasetTerm ?datasetValue .
    gn:dataset ?platformName ?platform_name .
    gn:dataset gn:normalization ?normalization .
    gn:dataset gn:investigatorName ?investigatorName .
    gn:dataset gn:investigatorWebUrl ?investigatorWebUrl .
    gn:dataset gn:tissueName ?tissueName .
} WHERE {
    ?subClass rdf:subClassOf gn:dataset .
    ?dataset rdf:type ?subclass ;
             gn:name "$name";
             ?datasetTerm ?datasetValue .
    OPTIONAL {
        ?dataset gn:datasetOfInvestigator ?investigator .
        ?investigator foaf:name ?investigatorName .
        ?investigator foaf:homepage ?investigatorWebUrl .
    } .
    OPTIONAL{
        ?dataset gn:normalization ?normalizationType .
        ?normalizationType gn:name ?normalization .
    } .
    OPTIONAL{
        ?dataset gn:datasetOfPlatform ?platform .
        ?platform gn:name ?platform_name .
    } .
    OPTIONAL{
        ?dataset gn:datasetOfTissue ?tissue .
        ?tissue gn:name ?tissueName .
    } .
    VALUES ?datasetTerm {
        dct:created gn:aboutCases gn:aboutDataProcessing gn:aboutPlatform
        gn:aboutTissue gn:accessionId gn:acknowledgment gn:citation
        gn:contributors gn:datasetGroup gn:datasetOfinvestigator
        gn:experimentDesign gn:geoSeries gn:name gn:notes
        gn:specifics gn:summary gn:title
    }
}
"""
    result: MonadicDict = MonadicDict()
    results = sparql_query(
        sparql_conn,
        Template(__metadata_query).substitute(prefix=RDF_PREFIXES, name=name),
    )
    for item in results:
        predicate = (item["p"]
                     .map(lambda x: x["value"]) # type: ignore
                     .map(strip_url))
        subject = (
            item["s"]
            .map(lambda x: x["value"]) # type: ignore
            .map(strip_url)
            .maybe(None, lambda x: x)
        )
        object_ = (item["o"]
                   .maybe(
                       Nothing,
                       lambda x: Just(x["value"])) # type: ignore
                   )
        if subject == "homepage":
            object_ = object_.map(strip_url) # type: ignore
        if _p := predicate.maybe(None, lambda x: x):  # type: ignore
            result[_p] = object_
    return result


def get_trait_metadata(
        sparql_conn: SPARQLWrapper,
        trait_name: str,
        dataset_name: str
):
    """Return metadata about a given trait"""
    __metadata_query = """
PREFIX gn: <http://genenetwork.org/>

SELECT strafter((str(?key)), "http://genenetwork.org/sampledata:") as ?key
    ?value WHERE {
    gn:sampledata_$trait_name gn:sampledata:dataset "$dataset_name" .
    gn:sampledata_$trait_name ?key ?value .
}
"""
    result: MonadicDict = MonadicDict()
    for _r in sparql_query(
            sparql_conn,
            Template(__metadata_query)
            .substitute(trait_name=trait_name,
                        dataset_name=dataset_name)
    ):
        _key = _r["key"].bind(lambda x: x["value"])  # type:ignore
        if _key:
            result[_key] = _r["value"].bind(lambda x: Just(x["value"]))  # type:ignore
    return result