aboutsummaryrefslogtreecommitdiff
path: root/gn3/db/rdf/wiki.py
blob: f945f0666df8251502ee4b9b30ca9bf50eb4cf5f (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Sparql queries to get metadata about WIKI and RIF metadata.

"""
from string import Template
from gn3.db.rdf import (BASE_CONTEXT, RDF_PREFIXES,
                        query_frame_and_compact)


WIKI_CONTEXT = BASE_CONTEXT | {
    "foaf": "http://xmlns.com/foaf/0.1/",
    "dct": "http://purl.org/dc/terms/",
    "categories": "gnt:belongsToCategory",
    "web_url": "foaf:homepage",
    "version": "gnt:hasVersion",
    "symbol": "rdfs:label",
    "reason": "gnt:reason",
    "species": "gnt:species",
    "pubmed_ids": "dct:references",
    "email": "foaf:mbox",
    "initial": "gnt:initial",
    "comment": "rdfs:comment",
    "created": "dct:created",
    "id": "dct:identifier",
    # This points to the RDF Node which is the unique identifier
    # for this triplet.  It's constructed using the comment-id and
    # the comment-versionId
    "wiki_identifier": "@id",
}


def get_wiki_entries_by_symbol(symbol: str, sparql_uri: str) -> dict:
    """Fetch all the Wiki entries using the symbol"""
    # This query uses a sub-query to fetch the latest comment by the
    # version id.
    query = Template("""
$prefix

CONSTRUCT {
    ?uid rdfs:label ?symbolName;
         gnt:reason ?reason ;
         gnt:species ?species ;
         dct:references ?pmid ;
         foaf:homepage ?weburl ;
         rdfs:comment ?comment ;
         foaf:mbox ?email ;
         gnt:initial ?usercode ;
         gnt:belongsToCategory ?category ;
         gnt:hasVersion ?versionId ;
         dct:created ?created ;
         dct:identifier ?identifier .
} WHERE {
    ?symbolId rdfs:label ?symbolName .
    ?uid rdfs:comment ?comment ;
         gnt:symbol ?symbolId ;
         rdf:type gnc:GNWikiEntry ;
         dct:created ?createTime .
    FILTER ( LCASE(?symbolName) = LCASE('$symbol') ) .
    {
        SELECT (MAX(?vers) AS ?max) ?id_ WHERE {
            ?symbolId rdfs:label ?symbolName .
            ?uid dct:identifier ?id_ ;
                 dct:hasVersion ?vers ;
                 dct:identifier ?id_ ;
                 gnt:symbol ?symbolId .
            FILTER ( LCASE(?symbolName) = LCASE('$symbol') ) .
        }
    }
    ?uid dct:hasVersion ?max ;
         dct:identifier ?id_ .
    OPTIONAL { ?uid gnt:reason ?reason } .
    OPTIONAL {
        ?uid gnt:belongsToSpecies ?speciesId .
        ?speciesId gnt:shortName ?species .
    } .
    OPTIONAL { ?uid dct:references ?pubmedId . } .
    OPTIONAL { ?uid foaf:homepage ?weburl . } .
    OPTIONAL { ?uid gnt:initial ?usercode . } .
    OPTIONAL { ?uid foaf:mbox ?email . } .
    OPTIONAL { ?uid gnt:belongsToCategory ?category . } .
    BIND (str(?version) AS ?versionId) .
    BIND (str(?id_) AS ?identifier) .
    BIND (str(?pubmedId) AS ?pmid) .
    BIND (str(?createTime) AS ?created) .
}
""").substitute(prefix=RDF_PREFIXES, symbol=symbol,)
    results = query_frame_and_compact(
        query, WIKI_CONTEXT,
        sparql_uri
    )
    data = results.get("data")
    if not data:
        return results
    return results


def get_comment_history(comment_id: int, sparql_uri: str) -> dict:
    """Get all the historical data for a given id"""
    query = Template("""
$prefix

CONSTRUCT {
    ?uid rdfs:label ?symbolName ;
         gnt:reason ?reason ;
         gnt:species ?species ;
         dct:references ?pmid ;
         foaf:homepage ?weburl ;
         rdfs:comment ?comment ;
         foaf:mbox ?email ;
         gnt:initial ?usercode ;
         gnt:belongsToCategory ?category ;
         gnt:hasVersion ?versionId ;
         dct:created ?created .
} WHERE {
    ?symbolId rdfs:label ?symbolName .
    ?uid rdf:type gnc:GNWikiEntry ;
         rdfs:comment ?comment ;
         gnt:symbol ?symbolId ;
         dct:created ?createTime ;
         dct:hasVersion ?version ;
         dct:identifier $comment_id ;
         dct:identifier ?id_ .
    OPTIONAL { ?uid gnt:reason ?reason_ } .
    OPTIONAL {
        ?uid gnt:belongsToSpecies ?speciesId .
        ?speciesId gnt:shortName ?species_ .
    } .
    OPTIONAL { ?uid dct:references ?pmid . } .
    OPTIONAL { ?uid foaf:homepage ?weburl_ . } .
    OPTIONAL { ?uid gnt:initial ?usercode_ . } .
    OPTIONAL { ?uid foaf:mbox ?email_ . } .
    OPTIONAL { ?uid gnt:belongsToCategory ?category_ . } .
    BIND (str(?version) AS ?versionId) .
    BIND (str(?createTime) AS ?created) .
    BIND (COALESCE(?reason_, "") AS ?reason) .
    BIND (COALESCE(?weburl_, "") AS ?weburl) .
    BIND (COALESCE(?usercode_, "") AS ?usercode) .
    BIND (COALESCE(?email_, "") AS ?email) .
    BIND (COALESCE(?species_, "") AS ?species) .
    BIND (COALESCE(?category_, "") AS ?category) .
}
""").substitute(prefix=RDF_PREFIXES, comment_id=comment_id)
    results = query_frame_and_compact(
        query, WIKI_CONTEXT,
        sparql_uri
    )
    data = results.get("data")
    for result in data:
        categories = result.get("categories") or []
        if categories and isinstance(categories, str):
            result["categories"] = [categories]
        pmids = result.get("pubmed_ids")
        if pmids and isinstance(pmids, str):
            result["pubmed_ids"] = [pmids]
        elif pmids:
            result["pubmed_ids"] = [int(pmid) for pmid in pmids]
        else:
            result["pubmed_ids"] = []
        result["version"] = int(result["version"])
    # We manually sort the array, since somehow "ORDER BY" in the
    # sparql query is jumbled up during framing and compacting.  This
    # operation isn't heavy since we don't get many versions per wiki
    # entry.
    results["data"] = sorted(data, key=lambda d: d["version"], reverse=True)
    return results