1/*
    2
    3expose a subclass of dbpedia for demo purposes
    4
    5  For complete ontology use rdfs2pl
    6
    7Note: this module uses macros to generate predicates. For every pname_wid/2 and cname_wid/2, predicates will be generated 
    8  
    9*/
   10
   11:- module(wikidata,
   12          [
   13
   14           property_constraint_pv/4,
   15           
   16           var_drug_condition/4,
   17           
   18           enlabel/2,
   19           exact_match/2
   20
   21           ]).   22
   23:- use_module(library(sparqlprog)).   24:- use_module(library(semweb/rdf11)).   25
   26:- sparql_endpoint( wd, 'http://query.wikidata.org/sparql').   27
   28:- rdf_register_prefix(foaf,'http://xmlns.com/foaf/0.1/').   29:- rdf_register_prefix(dbont,'http://dbpedia.org/ontology/').   30%:- rdf_register_prefix(dcterms,'http://purl.org/dc/terms').
   31:- rdf_register_prefix(wikipathways,'http://vocabularies.wikipathways.org/wp#').   32:- rdf_register_prefix(obo,'http://purl.obolibrary.org/obo/').   33:- rdf_register_prefix(so,'http://purl.obolibrary.org/obo/SO_').   34
   35% https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Prefixes_used
   36:- rdf_register_prefix(wd,'http://www.wikidata.org/entity/').   37:- rdf_register_prefix(wdt,'http://www.wikidata.org/prop/direct/').   38:- rdf_register_prefix(instance_of,'http://www.wikidata.org/prop/direct/P31').   39
   40user:term_expansion(pname_wid(P,Id),
   41                    [(   Head :- Body),
   42                     (   Head_s :- Body_s),
   43                     (   Head_ps :- Body_ps),
   44                     (   Head_q :- Body_q),
   45                     (   Head_iri :- true),
   46                     (   Head_eiri :- true),
   47                     (   :- initialization(export(P_s/2), now)),
   48                     (   :- initialization(export(P_ps/2), now)),
   49                     (   :- initialization(export(P_q/2), now)),
   50                     (   :- initialization(export(P_iri/1), now)),
   51                     (   :- initialization(export(P_eiri/1), now)),
   52                     (   :- initialization(export(P/2), now))
   53                    ]) :-
   54
   55        % e.g. p9 ==> P9
   56        upcase_atom(Id,Frag),
   57        
   58        
   59        % Truthy assertions about the data, links entity to value directly
   60        % wd:Q2  wdt:P9 <http://acme.com/> ==> P9(Q2,"...")
   61        Head =.. [P,S,O],
   62        atom_concat('http://www.wikidata.org/prop/direct/',Frag,Px),
   63        Body = rdf(S,Px,O),
   64        
   65        % p: Links entity to statement
   66        % wd:Q2 p:P9 wds:Q2-82a6e009 ==> P9_statement(Q2,wds:....)
   67        atom_concat(P,'_e2s',P_s),
   68        Head_s =.. [P_s,S,O],
   69        atom_concat('http://www.wikidata.org/prop/',Frag,Px_s),
   70        Body_s = rdf(S,Px_s,O),
   71
   72        atom_concat(P,'_iri',P_iri),
   73        Head_iri =.. [P_iri,Px],
   74
   75        atom_concat('http://www.wikidata.org/entity/',Frag,Pe),        
   76        atom_concat(P,'_eiri',P_eiri),
   77        Head_eiri =.. [P_eiri,Pe],
   78        
   79        % ps: Links value from statement
   80        % wds:Q3-24bf3704-4c5d-083a-9b59-1881f82b6b37 ps:P8 "-13000000000-01-01T00:00:00Z"^^xsd:dateTime
   81        atom_concat(P,'_s2v',P_ps),
   82        Head_ps =.. [P_ps,S,O],
   83        atom_concat('http://www.wikidata.org/prop/statement/',Frag,Px_ps),
   84        Body_ps = rdf(S,Px_ps,O),
   85        
   86        % pq: Links qualifier from statement node
   87        % wds:Q3-24bf3704-4c5d-083a-9b59-1881f82b6b37 pq:P8 "-13000000000-01-01T00:00:00Z"^^xsd:dateTime
   88        % => P8_q(wds:..., "..."^^...)
   89        atom_concat(P,'_s2q',P_q),
   90        Head_q =.. [P_q,S,O],
   91        atom_concat('http://www.wikidata.org/prop/qualifier/',Frag,Px_q),
   92        Body_q = rdf(S,Px_q,O).
   93
   94        
   95
   96user:term_expansion(cname_wid(C,Id),
   97                    [Rule,
   98                     RuleInf,
   99                     RuleIsa,
  100                     (   Head_iri :- true),
  101                     (:- initialization(export(InfC/1), now)),
  102                     (:- initialization(export(SubC/1), now)),
  103                     (:- initialization(export(C_iri/1), now)),
  104                     (:- initialization(export(C/1), now))
  105                     ]) :-
  106        upcase_atom(Id,Frag),
  107        atom_concat('http://www.wikidata.org/entity/',Frag,Cx),
  108        
  109        Head =.. [C,I],
  110        Body = rdf(I,'http://www.wikidata.org/prop/direct/P31',Cx),
  111        Rule = (Head :- Body),
  112
  113        atom_concat(C,'_iri',C_iri),
  114        Head_iri =.. [C_iri,Cx],
  115        
  116        atom_concat(C,'_inf',InfC),
  117        Head2 =.. [InfC,I],
  118        Body2 = rdf(I,('http://www.wikidata.org/prop/direct/P31'/zeroOrMore('http://www.wikidata.org/prop/direct/P279')),Cx),
  119        RuleInf = (Head2 :- Body2),
  120        
  121        atom_concat('isa_',C,SubC),
  122        Head3 =.. [SubC,I],
  123        Body3 = rdf(I,zeroOrMore('http://www.wikidata.org/prop/direct/P279'),Cx),
  124        RuleIsa = (Head3 :- Body3).
  125
  126
  127enlabel(E,N) :- label(E,N),lang(N)="en".
  128
  129% --------------------
  130% classes
  131% --------------------
  132
  133% geography
  134
  135% disease
  136%cancer(I) :- rdf(I,instance_of:'',wd:'Q12078').
  137%:- initialization(export(cancer/1), now).
  138
  139% --------------------
  140% predicates
  141% --------------------
  142
  143% meta
  144subproperty_of(S,O) :- rdf(S,'http://www.wikidata.org/prop/direct/P1647',O).
  145%exact_match(S,O) :- rdf(S,'http://www.wikidata.org/prop/direct/P2888',O).
  146
  147% info
  148%author(S,O) :- rdf(S,'http://www.wikidata.org/prop/direct/P50',O).
  149
  150% geo
  151%coordinate_location(S,O) :- rdf(S,'http://www.wikidata.org/prop/direct/P625',O).
  152
  153
  154
  155% PROPS
  156
  157% meta
  158pname_wid(instance_of, p31).
  159pname_wid(subclass_of, p279).
  160pname_wid(equivalent_property, p1628).
  161pname_wid(property_constraint, p2302).
  162
  163property_constraint_pv(P,C,PP,V) :-
  164        property_constraint_e2s(P,S),
  165        rdf(S,PP,V),
  166        property_constraint_s2v(S,C).
  167
  168% general
  169pname_wid(author, p50).
  170pname_wid(exact_match, p2888).
  171
  172% geo
  173pname_wid(coordinate_location, p625).
  174
  175% bio
  176% IDs
  177pname_wid(hp_id, p3841).
  178pname_wid(envo_id, p3859).
  179pname_wid(doid_id, p699).
  180pname_wid(chebi_id, p683).
  181pname_wid(uniprot_id, p352).
  182pname_wid(ncbigene_id, p351).
  183pname_wid(ipr_id, p2926).
  184pname_wid(civic_id, p3329).
  185pname_wid(ro_id, p3590).
  186pname_wid(mesh_id, p486).
  187pname_wid(go_id, p686).
  188pname_wid(ncbitaxon_id, p685).
  189pname_wid(uberon_id, p1554).
  190pname_wid(umls_id, p2892).
  191
  192% rels
  193pname_wid(encodes, p688).
  194pname_wid(genetic_association, p2293).
  195pname_wid(treated_by_drug, p2176).
  196pname_wid(symptoms, p780).
  197pname_wid(pathogen_transmission_process, p1060).
  198pname_wid(has_cause, p828).
  199pname_wid(biological_variant_of, p3433).
  200pname_wid(has_part, p527).
  201
  202% https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples#Get_known_variants_reported_in_CIViC_database_(Q27612411)_of_genes_reported_in_a_Wikipathways_pathway:_Bladder_Cancer_(Q30230812)
  203pname_wid(positive_therapeutic_predictor, p3354).
  204pname_wid(negative_therapeutic_predictor, p3355).
  205pname_wid(positive_diagnostic_predictor, p3356).
  206pname_wid(negative_diagnostic_predictor, p3357).
  207pname_wid(positive_prognostic_predictor, p3358).
  208pname_wid(negative_prognostic_predictor, p3359).
  209pname_wid(medical_condition_treated, p2175).
  210
  211% beacon
  212pname_wid(physically_interacts_with, p129).
  213pname_wid(location, p276).
  214pname_wid(manifestation_of, p1557).
  215pname_wid(part_of, p361).
  216pname_wid(followed_by, p156).
  217pname_wid(product_or_material_produced, p1056).
  218pname_wid(uses, p2283).
  219pname_wid(has_effect, p1542).
  220pname_wid(drug_used_for_treatment, p2176).
  221pname_wid(found_in_taxon, p703).
  222pname_wid(ortholog, p684).
  223pname_wid(biological_process, p682).
  224pname_wid(cell_component, p681).
  225pname_wid(molecular_function, p680).
  226pname_wid(has_quality, p1552).
  227pname_wid(regulates, p128).
  228
  229
  230    
  231% CLASSES
  232
  233% geo
  234cname_wid(continent, q5107).
  235cname_wid(country, q6256).
  236cname_wid(city, q515).
  237pname_wid(population, p1082).
  238pname_wid(part_of_continent, p30).
  239
  240
  241% bio
  242cname_wid(cancer, q12078).
  243cname_wid(disease, q12136).
  244cname_wid(infectious_disease, q18123741).
  245
  246cname_wid(chemical_compound, q11173).
  247cname_wid(chemical_element, q11344).
  248cname_wid(drug, q12140).
  249
  250cname_wid(symptom, q169872).
  251cname_wid(medical_finding, q639907).
  252cname_wid(trait, q1211967).
  253cname_wid(pathway, q4915012).
  254cname_wid(macromolecular_complex, qQ22325163).
  255cname_wid(gene, qQ7187).
  256cname_wid(gene_product, qQ424689).
  257cname_wid(sequence_variant, qQ15304597).
  258
  259cname_wid(therapy, q179661).
  260cname_wid(medical_procedure, qQ796194).
  261
  262
  263% random
  264cname_wid(power_station, q159719).
  265
  266% TODO
  267nary(ptp_var_drug_condition, positive_therapeutic_predictor, medical_condition_treated).
  268
  269
  270
  271var_drug_condition(V,D,C,positive_therapeutic_predictor) :-
  272        positive_therapeutic_predictor_e2s(V,S),
  273        medical_condition_treated_s2q(S,C),
  274        positive_therapeutic_predictor_s2v(S,D)