├── .gitignore ├── .travis.yml ├── CHANGES.md ├── Dockerfile ├── INSTALL.md ├── Makefile ├── README.md ├── SPECIFICATION.md ├── bin ├── ontobee-search ├── ontoquery ├── pl2sparql ├── pq-allie ├── pq-bgee ├── pq-biogateway ├── pq-biotea ├── pq-dbpedia ├── pq-disgenet ├── pq-ebi ├── pq-enipedia ├── pq-gaz-remote ├── pq-go ├── pq-keggoc ├── pq-mbgd ├── pq-mi ├── pq-ncats-red ├── pq-ncit ├── pq-nextprot ├── pq-oma ├── pq-ontobee ├── pq-pmg ├── pq-rhea ├── pq-ubergraph ├── pq-up ├── pq-wd ├── pq-wp ├── sprog-client.js ├── sprog-client.py ├── sprog_client.pl └── sprog_service.pl ├── examples ├── README.md ├── RWStore.properties ├── allie-examples.sh ├── biotea-examples.sh ├── data │ └── goslim_generic.ttl.gz ├── dataloader.txt ├── dbpedia-examples.sh ├── dbpedia │ └── dbpedia_rules.pl ├── disgenet-examples.sh ├── ebi-examples.sh ├── gocam-examples.sh ├── keggoc-examples.sh ├── kg-covid-examples.sh ├── local-queries.sh ├── local-sparql-queries.sh ├── mbgd-examples.sh ├── monarch-examples.sh ├── ncats-red-examples.sh ├── nextprot-examples.sh ├── oma-examples.sh ├── oma_ebi_federated.pl ├── ontobee-examples.sh ├── pubmedgraph-examples.sh ├── ubergraph-examples.sh ├── uniprot-examples.sh ├── void.ttl ├── wikidata-examples.sh └── wikipathways-examples.sh ├── install.pl ├── pack.pl ├── packplus.pl ├── prolog ├── concurrency.pl ├── sparql_dcg.pl ├── sparqlprog.pl └── sparqlprog │ ├── blazegraph.pl │ ├── dataframe.pl │ ├── emulate_builtins.pl │ ├── endpoints.pl │ ├── graph_util.pl │ ├── io_utils.pl │ ├── labelutils.pl │ ├── nlp_util.pl │ ├── obo_util.pl │ ├── ontologies │ ├── allie_sflf.pl │ ├── bgee.pl │ ├── biofed.pl │ ├── biogateway.pl │ ├── biopax3.pl │ ├── biotea.pl │ ├── chembl.pl │ ├── dbpedia.pl │ ├── dbpedia │ │ └── dbpedia_matcher.pl │ ├── disgenet.pl │ ├── ebi.pl │ ├── ebi │ │ └── reactome.pl │ ├── enipedia.pl │ ├── faldo.pl │ ├── gaz.pl │ ├── gocam.pl │ ├── jcm.pl │ ├── keggoc.pl │ ├── kghub.pl │ ├── load.pl │ ├── mbgd.pl │ ├── monarch.pl │ ├── ncit.pl │ ├── nextprot.pl │ ├── oban.pl │ ├── obo.pl │ ├── oma.pl │ ├── ontobee.pl │ ├── owl.pl │ ├── pubmedgraph.pl │ ├── rhea.pl │ ├── sequence_feature.pl │ ├── sio.pl │ ├── skos.pl │ ├── sosa.pl │ ├── togovar.pl │ ├── ubergraph.pl │ ├── uniprot.pl │ ├── wikidata.pl │ └── wikipathways.pl │ ├── owl_search_viz.pl │ ├── owl_types.pl │ ├── owl_util.pl │ ├── pathlang.pl │ ├── search_util.pl │ ├── stacktrace.pl │ └── test_aux.pl ├── release.sh └── tests ├── basic_test.pl ├── bigtests.pl ├── builtins_test.pl ├── dataframe_test.pl ├── dbpedia_test.pl ├── go_nucleus.ttl ├── gocam_test.pl ├── graph_util_test.pl ├── integration_tests.pl ├── labelutils_test.pl ├── lexmap_test.ttl ├── localsparql_test.pl ├── nlp_util_table_test.pl ├── nlp_util_test.pl ├── obo_util_test.pl ├── owl_test.pl ├── owl_util_test.pl ├── prolog_test.pl ├── search_test.pl ├── search_viz_test.pl ├── search_viz_tests.pl └── tests.pl /.gitignore: -------------------------------------------------------------------------------- 1 | .swipl_history 2 | RDF-Cache/ 3 | 4 | examples/load-data 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | ## from http://rlaanemets.com/post/show/adding-travis-to-swi-prolog-packs 2 | 3 | before_script: 4 | - sudo apt-add-repository ppa:swi-prolog/devel -y 5 | - sudo apt-get update -q 6 | - sudo apt-get install swi-prolog-nox 7 | - swipl -g "Opts=[interactive(false)],pack_install(dcgutils,Opts),pack_install(regex,Opts),halt" 8 | 9 | script: make realtest 10 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | 0.3.0 2 | 3 | * Adding --no-autopage and --limit options, request from @yayamamo 4 | 5 | 0.2.9 6 | 7 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM swipl:8.0.3 2 | 3 | # Run the image as a non-root user 4 | RUN useradd -m -s /bin/sh myuser 5 | USER myuser 6 | WORKDIR /home/myuser 7 | 8 | ADD . $HOME 9 | 10 | USER root 11 | RUN chown myuser tests tests/* && ls -alt tests 12 | USER myuser 13 | 14 | EXPOSE ${PORT} 15 | 16 | ## RUN swipl -g "getenv('HOME',Home),atom_concat('file://',Home,Path),Opts=[interactive(false)],pack_install(Path,Opts),halt" 17 | RUN swipl -g "Opts=[interactive(false)],pack_install(dcgutils,Opts),pack_install(regex,Opts),pack_install(typedef,Opts),halt" 18 | RUN swipl -p library=prolog -l tests/tests.pl -g run_tests,halt 19 | 20 | CMD swipl -p library=prolog -g "[bin/sprog_service]" -g 'server,T is 10**10,sleep(T)' 21 | -------------------------------------------------------------------------------- /INSTALL.md: -------------------------------------------------------------------------------- 1 | # SPARQLProg installation 2 | 3 | ## Install from pack 4 | 5 | 1. Install SWI-Prolog from http://www.swi-prolog.org 6 | 7 | 2. Install using SWI pack installer 8 | 9 | $ swipl 10 | ? pack_install(sparqlprog). 11 | 12 | ## Install from GitHub 13 | 14 | 1. Install SWI-Prolog from http://www.swi-prolog.org 15 | 16 | 2. Install requirements 17 | 18 | swipl -g "[install],install_requirements" 19 | 20 | 3. Get the latest sparqlprog source from github. No installation steps are 21 | required. Just add it to your path (changing the directory if necessary): 22 | 23 | `export PATH=$PATH:$HOME/sparqlprog/bin` 24 | 25 | You may also need to ensure the library is added to your path 26 | 27 | The easiest way is to create a symlink: 28 | 29 | ~/lib/swipl/pack/sparqlprog -> ~/repos/sparqlprog 30 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # ---------------- configuration ---------------------- 2 | 3 | # if you have multiple SWI Prolog installations or an installation 4 | # in a non-standard place, set PLLD to the appropriate plld invokation, eg 5 | # PLLD=/usr/local/bin/plld -p /usr/local/bin/swipl 6 | 7 | #PACKNAME=sparkle 8 | #include ../Makefile.inc 9 | 10 | SWIPL = swipl -p library=prolog 11 | 12 | all: test 13 | 14 | check: 15 | install: 16 | clean: 17 | 18 | test: 19 | echo "skipping tests. make realtest for actual tests" 20 | 21 | realtest: 22 | $(SWIPL) -l tests/tests.pl -g run_tests,halt 23 | 24 | bigtest: 25 | $(SWIPL) -l tests/bigtests.pl -g run_tests,halt 26 | 27 | coverage: 28 | $(SWIPL) -l tests/bigtests.pl -l tests/tests.pl -g "show_coverage(run_tests),halt" 29 | 30 | t-%: 31 | $(SWIPL) -l tests/$*_test.pl -g run_tests,halt 32 | 33 | 34 | 35 | # -------------------- 36 | # Schemas 37 | # -------------------- 38 | biopax-level3.owl: 39 | wget http://www.biopax.org/release/biopax-level3.owl -O $@ 40 | 41 | 42 | # -------------------- 43 | # Run pengine 44 | # -------------------- 45 | 46 | # TODO: get from pack 47 | VERSION = v$(shell swipl -l pack.pl -g "version(V),writeln(V),halt.") 48 | 49 | show-version: 50 | echo $(VERSION) 51 | 52 | IM = cmungall/sparqlprog 53 | 54 | pe: pe-clean pe-build pe-run 55 | 56 | pe-clean: 57 | docker kill $(IM) || echo not running ; 58 | docker rm $(IM) || echo not made 59 | 60 | pe-build: 61 | (test -f tests/out.tmp && rm tests/out.tmp || echo ok) && \ 62 | docker build -t $(IM):$(VERSION) . \ 63 | && docker tag $(IM):$(VERSION) $(IM):latest 64 | 65 | 66 | pe-run: 67 | docker run -p 9083:9083 -e PORT=9083 --name sparqlprog $(IM) 68 | 69 | pe-run-bg: 70 | docker run -p 9083:9083 -e PORT=9083 --name sparqlprog $(IM) & 71 | 72 | pe-test: pe-run-bg 73 | sleep 3 ;\ 74 | python3 ../sparqlprog-python/sparqlprog.py -u http://localhost:9083 -e wd "continent(X)" ;\ 75 | docker kill sparqlprog ;\ 76 | docker rm sparqlprog 77 | 78 | pe-publish: pe-build 79 | @docker push $(IM):$(VERSION) \ 80 | && docker push $(IM):latest 81 | 82 | # First requires: heroku container:login 83 | heroku-new-app: 84 | heroku create 85 | 86 | heroku-deploy: 87 | heroku container:push web &&\ 88 | heroku logs 89 | 90 | # -------------------- 91 | # Run pldoc server in Docker 92 | # -------------------- 93 | 94 | # after running, connect to 95 | # http://localhost:4000/pldoc/doc/work/prolog/sparqlprog.pl 96 | pldoc-run: 97 | docker run -v $(PWD)/:/work/ -w /work --name pldoc -p 4000:4000 -e PORT=4000 -it cmungall/sparqlprog swipl -p library=prolog -g [debug] && \ 98 | echo "server started, see http://localhost:4000/pldoc/" 99 | 100 | # -------------------- 101 | # Run SPARQL service inside Docker 102 | # -------------------- 103 | BGVERSION = 2.1.4 104 | bg: bg-run pause bg-load 105 | 106 | bg-data: examples/load-data/goslim_generic.ttl 107 | 108 | examples/load-data: 109 | mkdir $@ 110 | examples/load-data/%.ttl: examples/data/%.ttl.gz examples/load-data 111 | gzip -dc $< > $@ 112 | 113 | bg-run: 114 | docker run --name blazegraph -d -p 8889:8080 -v $(PWD)/examples/RWStore.properties:/RWStore.properties -v $(PWD)/examples/load-data/:/data lyrasis/blazegraph:$(BGVERSION) 115 | 116 | 117 | pause: 118 | sleep 1 119 | 120 | bg-load: bg-data 121 | curl -X POST --data-binary @examples/dataloader.txt --header 'Content-Type:text/plain' http://localhost:8889/bigdata/dataloader 122 | 123 | bg-stop: 124 | docker kill blazegraph; docker rm blazegraph 125 | 126 | -------------------------------------------------------------------------------- /SPECIFICATION.md: -------------------------------------------------------------------------------- 1 | # Specification 2 | 3 | Sparqlprog is a subset of prolog, roughly equivalent to 4 | [datalog](https://en.wikipedia.org/wiki/Datalog), that can be compiled 5 | to SPARQL. Sparqlprog can also be executed natively by a native logic 6 | programming engine, allowing for delegation of large database queries 7 | to a remote service combined with local programming. See the 8 | [README](./README.md) for more information. 9 | 10 | ## Queries and Compound Terms 11 | 12 | A query is a boolean combination of `compound terms`. A compound term 13 | is composed of a predicate plus zero or more arguments, where each 14 | argument can be a term or a variable. 15 | 16 | Predicates may be built-in or defined. The core built-in predicate is 17 | `rdf/3` (the `/3` denotes the predicate has 3 arguments). 18 | 19 | The most basic query is a query for all triples: 20 | 21 | rdf(S,P,O) 22 | 23 | Variables are denoted by a leading uppercase symbol. In the above 24 | example, all arguments are variables to the query succeeds for all 25 | triples. It is equivalent to the SPARQL query 26 | 27 | SELECT * WHERE {?x ?p ?o} 28 | 29 | The following query unifies the variable `Cls` with all subjects of a 30 | triple in which the predicate is `rdf:type` and the value/object is 31 | `owl:Class`. Note that "Class" is in single quotes to avoid being 32 | treated as a variable: 33 | 34 | rdf(Cls,rdf:type,owl:'Class') 35 | 36 | ## Boolean combinations 37 | 38 | Terms can be combined with any combinations of conjunction, 39 | disjunction or negation. These are denoted by the symbols ',', ';' and 40 | '\+' respectively. Formally these are all predicates (conjunctions and 41 | disjunction are binary, negation is unary), but these can be written 42 | using infix notation for syntacic convenience. 43 | 44 | The ',/2' predicate denotes conjunction: 45 | 46 | `rdf(Cls,rdf:type,owl:'Class'),rdf(Cls,rdfs:subClassOf,Super)` 47 | 48 | The ';/2' predicate denotes disjunction: 49 | 50 | `rdf(Obj,rdf:type,owl:'ObjectProperty');rdf(Obj,rdf:type,owl:'DataProperty')` 51 | 52 | The '\+/1` predicate denotes negation: 53 | 54 | ... 55 | 56 | Parentheses can be used to group conbinations 57 | 58 | ## Rules 59 | 60 | A rule is written `Head :- Body`, where the head of the rule is a single term and the body is any boolean combination 61 | 62 | The following rule defines `is_a/2` which is trivially equivalent to an `rdf/3` call with the RDFS subclass predicate: 63 | 64 | ``` 65 | is_a(A,B) :- rdf(A,rdfs:subClassOf,B). 66 | ``` 67 | 68 | Multiple rules are treated disjunctions. E.g. the following definition 69 | of `is_a/2` succeeds when the subject is a subclass of the object, or 70 | an instance. 71 | 72 | ``` 73 | is_a(A,B) :- rdf(A,rdfs:subClassOf,B). 74 | is_a(A,B) :- rdf(A,rdf:type,B). 75 | ``` 76 | 77 | Recursive rules *cannot* be written in sparqlprog. For example, if you write: 78 | 79 | ``` 80 | is_a(A,B) :- rdf(A,rdfs:subClassOf,B). 81 | is_a(A,B) :- is_a(A,Z),is_a(Z,B). 82 | ``` 83 | 84 | This cannot be directly converted to SPARQL. However, in this particular example, a property path can be used - see below. 85 | 86 | For examples of rules, see some of the programs in the [ontologies](prolog/sparqlprog/ontologies/) folder 87 | 88 | ## Builtins 89 | 90 | The builtins typically correspond to predicates defined in the 91 | swi-prolog semweb package. Additional definitions can optionally be 92 | imported. 93 | 94 | The core two builtins are: 95 | 96 | * `rdf(Subject, Predicate, Object)` (triple queries) 97 | * `rdf(Subject, Predicate, Object, Graph)` (quad queries) 98 | 99 | Additionally: 100 | 101 | * `rdfs_subclass_of/2` - inferred subClassOf 102 | * `rdfs_individual_of/2` - inferred subClassOf 103 | 104 | There are also predicate builtins for a subset of SPARQL functions that return booleans, such as 105 | 106 | * `str_starts/3` 107 | * `str_ends/3` 108 | * `between/3` 109 | 110 | The complete list is not yet implemented. 111 | 112 | Additionally, comparison operators are also supported. We use standard prolog infix operators, e.g. 113 | 114 | ``` 115 | A @<= B 116 | ``` 117 | 118 | for string comparisons 119 | 120 | ``` 121 | A <= B 122 | ``` 123 | 124 | for numeric comparisons 125 | 126 | ## Functions 127 | 128 | In addition to predicates, functions can be used in a query. These can be builtins, but functions can also be defined. 129 | 130 | `bind/2` can be used to explicitly set a value. 131 | 132 | 133 | * concat 134 | * ucase 135 | 136 | ## Predicate paths 137 | 138 | 139 | ## Aggregate Queries 140 | 141 | ## Namespaces 142 | 143 | ## Programs and Modules 144 | 145 | ## Execution of sparqlprog queries 146 | 147 | A query can be executed using the `pl2sparql` command, or from any 148 | language via an API call to a sparqlprog pengine. See the 149 | [README](README.md) for details. 150 | 151 | Additionally, queries can be executed from within a prolog program using `??/2`. 152 | 153 | For example: 154 | 155 | ``` 156 | TODO 157 | ``` 158 | 159 | ## Embedding within logic programs 160 | 161 | Any sparqlprog program should be executable directly using a prolog 162 | engine that defines the `rdf/3` predicate, such as swi-prolog. 163 | 164 | Additionally, native execution and remote execution can be mixed. 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /bin/ontobee-search: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/endpoints -u sparqlprog/ontologies/ontobee -s ontobee / searchall _ "$@" _ _ 3 | -------------------------------------------------------------------------------- /bin/ontoquery: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env swipl 2 | 3 | :- initialization main. 4 | 5 | :- use_module(library(main)). 6 | :- use_module(library(optparse)). 7 | :- use_module(library(option)). 8 | 9 | :- use_module(library(semweb/rdf_library)). 10 | :- use_module(library(semweb/rdf_http_plugin)). 11 | :- use_module(library(semweb/rdf_cache)). 12 | :- use_module(library(semweb/rdf_zlib_plugin)). 13 | :- use_module(library(semweb/rdf11)). 14 | :- use_module(library(semweb/rdfs)). 15 | :- use_module(library(semweb/rdf_turtle)). 16 | :- use_module(library(semweb/rdf_ntriples)). 17 | :- use_module(library(sparqlprog/emulate_builtins)). 18 | :- use_module(library(sparqlprog/labelutils)). 19 | :- use_module(library(sparqlprog/ontologies/owl), []). 20 | :- use_module(library(sparqlprog/owl_util)). 21 | :- use_module(library(sparqlprog/owl_search_viz)). 22 | :- use_module(library(sparqlprog/search_util)). 23 | 24 | % TODO: make configurable 25 | :- (expand_file_name('~/.RDF-Cache', [Dir|_]), 26 | rdf_set_cache_options([ global_directory(Dir), 27 | create_global_directory(true) 28 | ])). 29 | 30 | 31 | 32 | main(Argv) :- 33 | Spec = 34 | [ 35 | [opt(output), type(atom), 36 | longflags(['output']), 37 | shortflags([o]), 38 | help('Outfile') 39 | ], 40 | [opt(search_property), type(atom), 41 | longflags(['property']), 42 | shortflags([p]), 43 | default(l), 44 | help('Search property: l (label), s (synonyms+labels), a (all) or i (iri/curie/id). X = match anything') 45 | ], 46 | [opt(relations), type(atom), 47 | longflags(['relation']), 48 | shortflags([r]), 49 | help('relation') 50 | ], 51 | [opt(extend_lambda), type(atom), 52 | default('.'), 53 | longflags(['extend']), 54 | shortflags(['X']), 55 | help('extend lambda') 56 | ], 57 | [opt(format), type(atom), 58 | longflags(['to']), 59 | shortflags([t]), 60 | default(info), 61 | help('Output format: info, viz, json, obo') 62 | ], 63 | [opt(input), type(atom), 64 | longflags(['input']), 65 | shortflags([i]), 66 | help('Input RDF file (use in combo with -x)') 67 | ], 68 | [opt(goal), type(term), 69 | longflags([goal]), 70 | shortflags([g]), 71 | help('Prolog goal to call') 72 | ], 73 | [opt(consult), type(atom), 74 | longflags([consult]), 75 | shortflags([c]), 76 | help('Prolog program to load/consult') 77 | ], 78 | [opt(use), type(atom), 79 | longflags([use]), 80 | shortflags([u]), 81 | help('Prolog module to use') 82 | ], 83 | [opt(use_no_import), type(atom), 84 | longflags([use_no_import]), 85 | shortflags(['U']), 86 | help('Prolog module to use, do not import all') 87 | ], 88 | [opt(debug), type(term), 89 | longflags([debug]), 90 | shortflags([d]), 91 | help('term passed to debug/1') 92 | ], 93 | [opt(attach), type(atom), 94 | longflags([attach]), 95 | shortflags(['A']), 96 | help('rdf_attach_library - path to void.ttl') 97 | ], 98 | [opt(prefix_mapping), type(atom), 99 | longflags(['prefix_mapping']), 100 | shortflags([m]), 101 | help('Declare a prefix mapping. The value should be of the form "PREFIX=URIBase"') 102 | ], 103 | [opt(expand_uris), type(boolean), 104 | longflags(['expand-uris']), 105 | shortflags(['W']), 106 | help('If true use full URIs in output') 107 | ], 108 | [opt(prolog), 109 | type(boolean), 110 | default(false), 111 | longflags([prolog]), 112 | shortflags(['P']), 113 | help('Interactive prolog') 114 | ], 115 | [opt(verbose), 116 | type(boolean), 117 | default(false), 118 | longflags([verbose]), 119 | shortflags([v]), 120 | help('Same as --debug sparqlprog') 121 | ], 122 | [opt(stacktrace), 123 | type(boolean), 124 | default(false), 125 | longflags([stacktrace]), 126 | shortflags(['T']), 127 | help('Shows stack trace on error') 128 | ], 129 | [opt(query), type(term), 130 | longflags([query]), 131 | shortflags([q]), 132 | help('Prolog query') 133 | ] 134 | ], 135 | opt_parse(Spec, Argv, Opts, SearchTerms, [duplicated_flags(keepall)]), 136 | handle_opts(Opts), 137 | declare_shacl_prefixes, 138 | opt_if_call(prolog,prolog_shell(Opts),Opts), 139 | catch(owl_search_and_display(SearchTerms, Opts), 140 | E, 141 | ( format(user_error,'~q~n',[E]), 142 | halt(1))), 143 | halt. 144 | 145 | add_prefix(X) :- 146 | concat_atom([Pre,Base],'=',X), 147 | debug(sparqlprog,'Registering: ~w = ~w',[Pre,Base]), 148 | rdf_register_prefix(Pre,Base). 149 | 150 | handle_opts(Opts) :- 151 | opt_if_call(verbose,debug(sparqlprog),Opts), 152 | opt_if_call(distinct,force_distinct,Opts), 153 | opt_if_call(stacktrace,use_module(library(sparqlprog/stacktrace)),Opts), 154 | opt_forall(attach(X),rdf_attach_library(X),Opts), 155 | opt_forall(debug(X),debug(X),Opts), 156 | opt_forall(prefix_mapping(X),add_prefix(X),Opts), 157 | opt_forall(use(X),use_module(library(X)),Opts), 158 | opt_forall(use_no_import(X),use_module(library(X),[]),Opts), 159 | opt_forall(consult(X),consult(X),Opts), 160 | opt_forall(input(X),rdf_load_wrap(X),Opts), 161 | opt_forall(goal(X),X,Opts). 162 | 163 | force_distinct :- 164 | setting(sparqlprog:select_options, Opts), 165 | select(distinct(_),Opts,Opts2), 166 | set_setting(sparqlprog:select_options, [distinct(true)|Opts2]). 167 | 168 | 169 | 170 | rdf_load_wrap(X) :- 171 | concat_atom([Pre,Post],':',X), 172 | rdf_current_prefix(Pre,_), 173 | rdf_global_id(Pre:Post,Y), 174 | Y\=X, 175 | !, 176 | rdf_load_wrap(Y). 177 | rdf_load_wrap(X) :- 178 | catch(rdf_load(X), 179 | _E, 180 | rdf_load_library(X)). 181 | 182 | 183 | 184 | 185 | 186 | 187 | % execute a goal for every ground instance of Template 188 | opt_forall(Template,Goal,Opts) :- 189 | debug(sparqlprog,'Running ~q for all ground ~q in ~q',[Goal,Template,Opts]), 190 | forall((member(Template,Opts),ground(Template)), 191 | Goal). 192 | 193 | opt_if_call(Opt,Goal,Opts) :- 194 | T =.. [Opt,Var], 195 | member(T,Opts), 196 | ground(Var), 197 | Var=true, 198 | !, 199 | Goal. 200 | opt_if_call(_,_,_). 201 | 202 | opt_if(T,Opts) :- 203 | member(T,Opts), 204 | ground(T), 205 | !. 206 | opt_if(T,Opts,Opts2) :- 207 | select(T,Opts,Opts2), 208 | ground(T), 209 | !. 210 | 211 | 212 | tr_opts([],[]). 213 | tr_opts([H|T],[H|T2]) :- 214 | H=..[P|_], 215 | is_pass_thru(P), 216 | !, 217 | tr_opts(T,T2). 218 | tr_opts([_|T],T2) :- 219 | tr_opts(T,T2). 220 | 221 | prolog_shell(_Opts):- 222 | format('% Starting prolog shell~n'), 223 | HFile='.plhistory', 224 | ( exists_file(HFile) 225 | -> rl_read_history(HFile) 226 | ; true), 227 | prolog, 228 | format('% Bye!~n'), 229 | rl_write_history(HFile), 230 | halt. 231 | 232 | 233 | 234 | 235 | /* 236 | 237 | */ 238 | -------------------------------------------------------------------------------- /bin/pq-allie: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/allie_sflf -s allie "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-bgee: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/bgee -s bgee "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-biogateway: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/biogateway -s biogateway "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-biotea: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/endpoints -s biotea -u sparqlprog/ontologies/biotea "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-dbpedia: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #echo "$@" 3 | pl2sparql -s dbpedia -u sparqlprog/endpoints -u sparqlprog/ontologies/dbpedia "$@" 4 | -------------------------------------------------------------------------------- /bin/pq-disgenet: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -s disgenet -u sparqlprog/ontologies/disgenet "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-ebi: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/ebi -u sparqlprog/ontologies/chembl -u sparqlprog/ontologies/ebi/reactome -u sparqlprog/ontologies/biopax3 -u sparqlprog/ontologies/faldo -s ebi "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-enipedia: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/enipedia -s enipedia "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-gaz-remote: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #echo "$@" 3 | pl2sparql -s ontobee -A ~/repos/onto-mirror/void.ttl -u obo_ro/ro -u sparqlprog/endpoints -u sparqlprog/ontologies/gaz "$@" 4 | -------------------------------------------------------------------------------- /bin/pq-go: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #echo "$@" 3 | pl2sparql -u obo_ro/ro -u obo_core/goslim -u sparqlprog/endpoints -u sparqlprog/ontologies/gocam -s go "$@" 4 | -------------------------------------------------------------------------------- /bin/pq-keggoc: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/keggoc -s keggoc "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-mbgd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/mbgd -s mbgd "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-mi: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #echo "$@" 3 | pl2sparql -u obo_ro/ro -u obo_metadata/oio -u sparqlprog/endpoints -u sparqlprog/blazegraph -u sparqlprog/ontologies/monarch -s monarch "$@" 4 | -------------------------------------------------------------------------------- /bin/pq-ncats-red: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/dumontierlab -u biolink_model -s dumontierlab "$@" 3 | 4 | # pq-ncats-red -f tsv "drug(X)" -l -L name 5 | -------------------------------------------------------------------------------- /bin/pq-ncit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/endpoints -u sparqlprog/ontologies/ncit -s ncit "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-nextprot: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -s nextprot -u sparqlprog/ontologies/nextprot "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-oma: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/oma -s oma "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-ontobee: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -A ~/repos/onto-mirror/void.ttl -u sparqlprog/endpoints -u sparqlprog/ontologies/ontobee -u sparqlprog/owl_util -u obo_metadata/oio -s ontobee "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-pmg: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/endpoints -s pmg -u sparqlprog/ontologies/pubmedgraph -u sparqlprog/ontologies/monarch "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-rhea: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/endpoints -s rhea -u sparqlprog/ontologies/rhea "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-ubergraph: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/endpoints -u sparqlprog/ontologies/ubergraph -u sparqlprog/owl_util -u obo_ro/ro -u obo_metadata/oio -s ubergraph "$@" 3 | -------------------------------------------------------------------------------- /bin/pq-up: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #echo "$@" 3 | pl2sparql -u sparqlprog/ontologies/uniprot -u sparqlprog/ontologies/faldo -s uniprot "$@" 4 | -------------------------------------------------------------------------------- /bin/pq-wd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #echo "$@" 3 | echo DEPRECATED - use pq-wikidata 4 | pl2sparql -u sparqlprog/ontologies/wikidata -s wd "$@" 5 | -------------------------------------------------------------------------------- /bin/pq-wp: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | pl2sparql -u sparqlprog/ontologies/wikipathways -s wikipathways "$@" 3 | -------------------------------------------------------------------------------- /bin/sprog-client.js: -------------------------------------------------------------------------------- 1 | pengines = require('pengines'); 2 | 3 | peng = pengines({ 4 | server: "http://127.0.0.1:9083/pengine", 5 | //ask: "member(X,[a,b,c,d,7])", 6 | ask: "t(X)", 7 | //ask: "between(1,1000,N)", 8 | chunk: 10, 9 | //sourceText: "t(X):- (go ?? rdf(X,rdf:type,owl:'ObjectProperty')).", 10 | //sourceText: "t(X):- (go ?? rdf(X,rdf:type,owl:'ObjectProperty')).\n", 11 | sourceText: "t(X):- (wd ?? continent(X)).\n", 12 | 13 | } 14 | ).on('success', handleSuccess).on('error', handleError); 15 | function handleSuccess(result) { 16 | console.log('# Results: '+ result.data.length); 17 | for (var i = 0; i < result.data.length; i++) { 18 | console.log(result.data[i]) 19 | } 20 | if (result.data.length == 0) { 21 | console.log("No results!") 22 | } 23 | console.log('# More? '+ result.more); 24 | if (result.more) { 25 | n = peng.next(); 26 | console.log('# Next = '+ n); 27 | 28 | } 29 | } 30 | function handleError(result) { 31 | console.error(result) 32 | } 33 | -------------------------------------------------------------------------------- /bin/sprog-client.py: -------------------------------------------------------------------------------- 1 | from pengines.Builder import PengineBuilder 2 | from pengines.Pengine import Pengine 3 | query = "wd ?? continent(X)" 4 | pengine_builder = PengineBuilder(urlserver="http://localhost:9083", destroy=False, ask=query) 5 | pengine = Pengine(builder=pengine_builder, debug=True) 6 | #pengine.create() 7 | #query = "member(X, [1,2,3])" 8 | #pengine.ask(query) 9 | #pengine.doAsk(pengine.currentQuery) 10 | #print() 11 | print(pengine.currentQuery.availProofs, "Has More? ", pengine.currentQuery.hasMore) 12 | #print() 13 | # Get next query. 14 | #print(pengine.state.current_state) 15 | while pengine.currentQuery.hasMore: 16 | pengine.doNext(pengine.currentQuery) 17 | print(pengine.currentQuery.availProofs) 18 | 19 | -------------------------------------------------------------------------------- /bin/sprog_client.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env swipl 2 | 3 | :- use_module(library(pengines)). 4 | 5 | main :- 6 | pengine_create([ 7 | server('http://127.0.0.1:9083'), 8 | src_text(" 9 | t(X):- '??'(go, rdf(X,rdf:type,owl:'ObjectProperty')). 10 | t(X) :- q(X). 11 | t(X) :- tobjprop(X). 12 | q(X) :- p(X). 13 | p(a). p(b). p(c). 14 | ") 15 | ]), 16 | pengine_event_loop(handle, []). 17 | 18 | 19 | handle(create(ID, _)) :- 20 | pengine_ask(ID, p(_), []). 21 | % pengine_ask(ID, (go ?? rdf(X,rdf:type,owl:'ObjectProperty')), []). 22 | handle(success(_ID, [X], false)) :- 23 | writeln(X). 24 | handle(success(ID, [X], true)) :- 25 | writeln(X), 26 | pengine_next(ID, []). 27 | -------------------------------------------------------------------------------- /bin/sprog_service.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env swipl 2 | 3 | 4 | :- use_module(library(main)). 5 | :- use_module(library(optparse)). 6 | %:- use_module(library(semweb/rdf_db)). 7 | :- use_module(library(option)). 8 | 9 | :- use_module(library(semweb/rdf_library)). 10 | :- use_module(library(semweb/rdf_http_plugin)). 11 | :- use_module(library(semweb/rdf_cache)). 12 | :- use_module(library(semweb/rdf11)). 13 | :- use_module(library(semweb/rdfs)). 14 | :- use_module(library(sparqlprog/ontologies/owl), []). 15 | :- use_module(library(sparqlprog/owl_util)). 16 | 17 | :- use_module(library(sparqlprog)). 18 | :- use_module(library(sparqlprog/labelutils)). 19 | :- use_module(library(sparqlprog/endpoints)). 20 | :- use_module(library(sparqlprog/ontologies/wikidata)). 21 | :- [library(sparqlprog/ontologies/load)]. 22 | :- packinfo:consult('pack.pl'). 23 | 24 | :- multifile http:location/3. 25 | :- dynamic http:location/3. 26 | 27 | http:location(root, '/', []). 28 | 29 | :- use_module(library(http/thread_httpd)). 30 | :- use_module(library(http/http_dispatch)). 31 | :- use_module(library(http/http_error)). 32 | :- use_module(library(http/html_write)). 33 | 34 | :- use_module(library(pengines)). 35 | 36 | :- use_module(pengine_sandbox:library(sparqlprog)). 37 | :- use_module(pengine_sandbox:library(sparqlprog/labelutils)). 38 | :- use_module(pengine_sandbox:library(semweb/rdf11)). 39 | :- use_module(library(sandbox)). 40 | :- use_module(library(semweb/rdf11)). 41 | :- use_module(library(semweb/rdf_sandbox)). 42 | 43 | :- multifile sandbox:safe_primitive/1. 44 | sandbox:safe_primitive(packinfo:version(_)). 45 | sandbox:safe_primitive(rdf11:rdf(_,_,_)). 46 | sandbox:safe_primitive(rdf11:rdf_iri(_)). 47 | sandbox:safe_primitive(sparqlprog:'??'(_)). 48 | sandbox:safe_primitive(sparqlprog:'??'(_,_)). 49 | sandbox:safe_primitive(sparqlprog:'??'(_,_,_)). 50 | sandbox:safe_primitive(sparqlprog:'??'(_,_,_,_)). 51 | %sandbox:safe_primitive(clause(_,_,_)). 52 | 53 | 54 | http:location(pldoc, root(documentation), [priority(100)]). 55 | 56 | 57 | :- rdf_set_cache_options([ global_directory('RDF-Cache'), 58 | create_global_directory(true) 59 | ]). 60 | 61 | 62 | %:- initialization prolog_ide(thread_monitor). 63 | :- initialization debug(sparqlprog). 64 | :- initialization debug. 65 | 66 | 67 | server :- 68 | ( getenv('PORT',PortAtom) 69 | -> atom_number(PortAtom,Port) 70 | ; Port=9083), 71 | server(Port). 72 | 73 | server(Port) :- 74 | http_server(http_dispatch, [port(Port)]). 75 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # Command line examples 2 | 3 | There are multiple ways to use sparqlprog 4 | 5 | - services/pengines 6 | - invocation within prolog 7 | - command line, with pl2sparql or wrapper script (typically named pq-*) 8 | 9 | This directory provides examples of command line usage 10 | -------------------------------------------------------------------------------- /examples/RWStore.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Note: These options are applied when the journal and the triple store are 3 | # first created. 4 | 5 | ## 6 | ## Journal options. 7 | ## 8 | 9 | # The backing file. This contains all your data. You want to put this someplace 10 | # safe. The default locator will wind up in the directory from which you start 11 | # your servlet container. 12 | com.bigdata.journal.AbstractJournal.file=bigdata.jnl 13 | 14 | # The persistence engine. Use 'Disk' for the WORM or 'DiskRW' for the RWStore. 15 | com.bigdata.journal.AbstractJournal.bufferMode=DiskRW 16 | 17 | # Setup for the RWStore recycler rather than session protection. 18 | com.bigdata.service.AbstractTransactionService.minReleaseAge=1 19 | 20 | # Enable group commit. See http://wiki.blazegraph.com/wiki/index.php/GroupCommit 21 | # Note: Group commit is a beta feature in BlazeGraph release 1.5.1. 22 | #com.bigdata.journal.Journal.groupCommit=true 23 | 24 | com.bigdata.btree.writeRetentionQueue.capacity=4000 25 | com.bigdata.btree.BTree.branchingFactor=128 26 | 27 | # 200M initial extent. 28 | com.bigdata.journal.AbstractJournal.initialExtent=209715200 29 | com.bigdata.journal.AbstractJournal.maximumExtent=209715200 30 | 31 | ## 32 | ## Setup for QUADS mode without the full text index. 33 | ## 34 | com.bigdata.rdf.sail.truthMaintenance=false 35 | com.bigdata.rdf.store.AbstractTripleStore.quads=false 36 | com.bigdata.rdf.store.AbstractTripleStore.statementIdentifiers=false 37 | com.bigdata.rdf.store.AbstractTripleStore.textIndex=true 38 | com.bigdata.rdf.store.AbstractTripleStore.axiomsClass=com.bigdata.rdf.axioms.NoAxioms 39 | 40 | # Bump up the branching factor for the lexicon indices on the default kb. 41 | com.bigdata.namespace.kb.lex.com.bigdata.btree.BTree.branchingFactor=400 42 | 43 | # Bump up the branching factor for the statement indices on the default kb. 44 | com.bigdata.namespace.kb.spo.com.bigdata.btree.BTree.branchingFactor=1024 45 | 46 | # Uncomment to enable collection of OS level performance counters. When 47 | # collected they will be self-reported through the /counters servlet and 48 | # the workbench "Performance" tab. 49 | # 50 | # com.bigdata.journal.Journal.collectPlatformStatistics=true 51 | -------------------------------------------------------------------------------- /examples/allie-examples.sh: -------------------------------------------------------------------------------- 1 | # retrieve long forms for the given abbreviation (aka short form), "SPF" 2 | pq-allie 'long_form_list("SPF",L1,L2,"en")' 3 | -------------------------------------------------------------------------------- /examples/biotea-examples.sh: -------------------------------------------------------------------------------- 1 | # Examples for querying BioTea pubmedgraph 2 | # 3 | # Uses the pq-biotea wrapper that defines the endpoint 4 | 5 | # --- 6 | # what does PMID:1 reference? 7 | # --- 8 | pq-pmg "references(pmid:'1',X)" 9 | 10 | # Find all publications that either reference a class, or a descendant of that class. 11 | # E.g., MONDO:0015229 ! Bardet-Biedl syndrome 12 | # 13 | # This is an example of federation: we use the monarch graph to get subclasses of 14 | # the disease of interest, and look these up iteratively on pubmedgraph 15 | # 16 | # For this we use the -e option as the outer query is executed directly in prolog. 17 | # The ??/2 predicate is used to query a remote triplestore. 18 | pq-pmg -e "(monarch ?? rdfs_subclass_of(C,obo:'MONDO_0015229')),(pmg ?? references(P,C))" 19 | 20 | pq-pmg -i mondo -e "subclass_axiom_validation(A,B,P)" 21 | -------------------------------------------------------------------------------- /examples/data/goslim_generic.ttl.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmungall/sparqlprog/ccd647204e4fd55fe5c9707a6c2905739bb98f48/examples/data/goslim_generic.ttl.gz -------------------------------------------------------------------------------- /examples/dataloader.txt: -------------------------------------------------------------------------------- 1 | namespace=kb 2 | propertyFile=/RWStore.properties 3 | fileOrDirs=/data 4 | -format=n-triples 5 | quiet=false 6 | verbose=0 7 | closure=false 8 | durableQueues=true 9 | -------------------------------------------------------------------------------- /examples/dbpedia-examples.sh: -------------------------------------------------------------------------------- 1 | # DBPedia Examples 2 | # --- 3 | # These all use pq-dbpedia in the bin/ directory, which is a one-line wrapper onto pl2sparql, 4 | # which loads the dbpedia.pl module and sets the endpoint to http://dbpedia.org/sparql/ 5 | 6 | # --- 7 | # movies directors with children who are musicians 8 | # --- 9 | # this example uses bare rdf/3 predicates 10 | pq-dbpedia "rdf(F,dbo:director,D),rdf(D,dbo:child,C),rdfs_individual_of(C,dbo:'MusicalArtist')" 11 | 12 | # --- 13 | # movies directors with children who are musicians 14 | # --- 15 | # use convenience predicates 16 | pq-dbpedia "has_director(F,D),child(D,C),musical_artist(C)" 17 | 18 | 19 | # --- 20 | # examples of JOINs 21 | # --- 22 | # outer loop over all cities, querying each one for its label 23 | # (this is an example: don't do this as it is inefficient) 24 | pq-dbpedia "rdf(City,rdf:type,dbont:'City') join label(City,CityLabel),lang(CityLabel)=\"en\"" 25 | 26 | # bands with a city in their name; we do this via looping rather than single query 27 | pq-dbpedia "city(C),label(C,CN),lang(CN)=\"en\",bind(str(CN),CNS) join band(B),label(B,BN),contains(BN,CNS)" 28 | -------------------------------------------------------------------------------- /examples/dbpedia/dbpedia_rules.pl: -------------------------------------------------------------------------------- 1 | :- use_module(library(tabling)). 2 | :- use_module(library(sparqlprog/emulate_builtins)). 3 | 4 | %! band(?B) is nondet 5 | % 6 | % true if B is a band 7 | band(B) :- rdf(B,rdf:type,dbont:'Band'). 8 | 9 | %! has_shared_band_member(?B1, ?B2, ?SharedMember) is nondet 10 | % 11 | % links bands by members in common. E.g. Ronnie James Dio in both Rainbow and Black Sabbath 12 | % 13 | % true if SharedMember is a member of both B1 and B2 (and B1 and B2 are distinct) 14 | % 15 | has_shared_band_member(B1,B2,A) :- 16 | rdf(A,dbont:associatedBand,B1), 17 | rdf(A,dbont:associatedBand,B2), 18 | B1\=B2. 19 | 20 | %! has_shared_band_genre(?B1, ?B2, ?SharedGenre) is nondet 21 | % 22 | % links bands by shared genre common. 23 | % 24 | % true if SharedGenre is a genre of both B1 and B2 (and B1 and B2 are distinct) 25 | % 26 | has_shared_genre(B1,B2,A) :- 27 | rdf(B1,dbont:genre,A), 28 | rdf(B2,dbont:genre,A), 29 | B1\=B2. 30 | 31 | genre_pair(G1,G2,A) :- 32 | rdf(A,dbont:genre,G1), 33 | rdf(A,dbont:genre,G2), 34 | G1\=G2. 35 | 36 | 37 | 38 | %! similarity_by_genre(?BandA, ?BandB, ?SumIC) 39 | % 40 | % calculates the jaccard similarity between two entities based on genres in common 41 | % 42 | % the entities should be of similar types (e.g. two bands, or two books) 43 | % 44 | % == 45 | % | genres(A) /\ genres(B) | / | genres(A) \/ genres(B) | 46 | % == 47 | % 48 | % if no genres are in common, then this should equal 0 49 | % if all genres are in common, then this should equal 1 50 | % 51 | % note this does not take into account how *meaningful* it is for a genre to be shared; 52 | % e.g. sharing the common genre 'pop' counts as much as a rarer genre like 'psytrance'. 53 | % see further on for IC-based metrics. 54 | % 55 | similarity_by_genre(A,B,Sim) :- 56 | get_all_genres(A,SA), 57 | get_all_genres(B,SB), 58 | jaccard(SA,SB,Sim). 59 | 60 | jaccard(SA,SB,Sim) :- 61 | ord_intersection(SA,SB,I), 62 | ord_union(SA,SB,U), 63 | length(I,NI), 64 | length(U,NU), 65 | Sim is NI/NU. 66 | 67 | get_all_genres(Entity,L) :- 68 | service_query_all(dbpedia,G,rdf(Entity,dbont:genre,G),L). 69 | 70 | 71 | 72 | :- table get_num_bands/1. 73 | %! get_num_bands(?Count) is det 74 | % 75 | % unifies Count with the total number of bands in the database 76 | % 77 | % note this is tabled (cached) so that repeated calls do not invoke new SPARQL queries 78 | % 79 | get_num_bands(Count) :- 80 | ??(dbpedia,num_bands(Count)). 81 | num_bands(Count) :- 82 | aggregate(count(distinct(B)),band(B),Count). 83 | 84 | 85 | 86 | %! get_genre_num_bands(?Genre,?Count) is nondet. 87 | %! get_genre_num_bands(+Genre,?Count) is det. 88 | % 89 | % unifies Count with the total number of bands that are categorized as Genre 90 | % 91 | % 92 | %%%%:- table get_genre_num_bands/2. 93 | get_genre_num_bands(G,Count) :- 94 | ??(dbpedia,genre_num_bands(G,Count)). 95 | 96 | genre_num_bands(G,Count) :- 97 | aggregate_group(count(distinct(B)),[G],(rdf(B,dbont:genre,G),band(B)),Count). 98 | 99 | 100 | 101 | %! pair_genre_sum_ic(?BandA, ?BandB, ?SumIC) 102 | % 103 | % for a pair of bands, SumIC is the sum of the ICs of the genres shared in common. 104 | % 105 | % 106 | % Example: =pair_genre_ic(dbr:'Metallica', dbr:'Megadeth', IC)= 107 | pair_genre_sum_ic(A,B,SumIC) :- 108 | get_all_genres(A,SA), 109 | ??(dbpedia,(band(B),has_shared_genre(A,B,_))), 110 | get_all_genres(B,SB), 111 | ord_intersection(SA,SB,I), 112 | debug(dbpedia,'~w vs ~w :: INTERSECTION(~w + ~w) = ~w',[A,B,SA,SB,I]), 113 | aggregate(sum(IC),G^(member(G,I),genre_ic(G,IC)),SumIC). 114 | 115 | %! genre_ic(?Genre, ?InformationContent:float) is nondet. 116 | % 117 | % gets the IC of a particular genre. The higher the IC, the rarer and more 'surprising' or information-rich it is. 118 | % 119 | % for example, many bands are pop, so this would have a low IC. Progressive sludge metal is relatively rare and would have a high IC 120 | % 121 | % == 122 | % InformationContent = -log2( Pr(Genre) ) 123 | % == 124 | % 125 | genre_ic(G,IC) :- 126 | get_genre_num_bands(G,Count), 127 | debug(dbpedia,'|bands| in ~w = ~w',[G,Count]), 128 | get_num_bands(Total), 129 | debug(dbpedia,'Total bands = ~w',[Total]), 130 | seval(-log(Count/Total)/log(2), IC). 131 | 132 | -------------------------------------------------------------------------------- /examples/disgenet-examples.sh: -------------------------------------------------------------------------------- 1 | # find all diseases and their labels 2 | pq-disgenet -l disease 3 | 4 | # all gene-disease-associations 5 | pq-disgenet -f tsv -l "gene_disease_association(A,G,D)" 6 | 7 | # all gene-disease-associations with evidence/publication 8 | pq-disgenet -f tsv -l "gene_disease_association(A,G,D),has_evidence(D,E)" 9 | 10 | # diseases that have an implicated gene that is also implicated in small lung neoplasm 11 | pq-disgenet -l "disease_pair_by_shared_gene(umls:'C0751688',D2,G)" 12 | -------------------------------------------------------------------------------- /examples/ebi-examples.sh: -------------------------------------------------------------------------------- 1 | # Show all transcripts of human BRCA2 gene and their coordinates 2 | # https://www.ebi.ac.uk/rdf/services/sparql 3 | pq-ebi "transcribed_from(T,ensembl:'ENSG00000139618'),location(T,L,B,E,R),rdf(T,rdf:type,TType)" 4 | 5 | # Get all mouse protein-coding genes on chromosome 11 between location 101,100,523 and 101,190,725 forward strand 6 | pq-ebi -f csv "protein_coding_gene(G),location(G,L,B,E,grcm38:'11'), B >= 101100523,E =< 101190725" G 7 | 8 | # sample all samples 9 | pq-ebi -l sample 10 | 11 | # everything about https://www.ebi.ac.uk/biosamples/samples/SAMN02847463 12 | pq-ebi -l "rdf(biosd:'SAMN02847463',P,Y)" 13 | 14 | # sample property-values 15 | pq-ebi -l "sample_property_value(biosd:'SAMN02847463',P,V)" 16 | 17 | # distincty attributes 18 | # (may be too slow...) 19 | pq-ebi "aggregate(count(distinct(P)),rdf(A,ebi_atlas:propertyType,P),Num)" "x(Num)" 20 | 21 | # all distinct attributes (24732) 22 | pq-ebi --distinct "rdf(A,ebi_atlas:propertyType,P)" "x(P)" 23 | 24 | # federated query 25 | # see examples/oma_ebi_federated.pl for explanation 26 | pl2sparql -e -d sparqlprog --consult examples/oma_ebi_federated.pl "orthologs_with_coordinates(ensembl:'ENSG00000198840',G2,Tax,B,E,R)" 27 | -------------------------------------------------------------------------------- /examples/gocam-examples.sh: -------------------------------------------------------------------------------- 1 | # -------------------- 2 | # GO-CAM EXAMPLES 3 | # -------------------- 4 | # Uses rdf.geneontology.org endpoint (alias 'go') 5 | # The pq-go script is a wrapper for 6 | # pl2sparql -u sparqlprog/endpoints -u obo_core/goslim -u obo_ro/ro -s go 7 | 8 | # --- 9 | # genes executing kinase activity as a part of signal transduction 10 | # --- 11 | # timeout?? 12 | pq-go "kinase_activity(A),part_of(A,P),signal_transduction(P),enabled_by(A,G)" 13 | 14 | # --- 15 | # kinase regulating other activity 16 | # --- 17 | pq-go "kinase_activity(A),regulates(A,A2),enabled_by(A,G)" 18 | 19 | # --- 20 | # user to model 21 | # --- 22 | pq-go -C "rdf(X,dc:contributor,Y),rdf(X,rdf:type,owl:'Ontology')" 23 | 24 | # --- 25 | # search: classes with label ending in morphogenesis 26 | # --- 27 | pq-go 'label(C,L),regex(str(L),"morphogenesis$")' "x(C,L)" 28 | 29 | # with flags (case insensitive) 30 | pq-go 'label(C,L),regex(str(L),"morphogenesis$","i")' "x(C,L)" 31 | 32 | # same, using owl_util lsearch/4 33 | pq-go 'lsearch("morphogenesis$",C,L,"i")' 34 | 35 | # same, using shorthand syntax 36 | pq-go / lsearch morphogenesis$ .. i 37 | -------------------------------------------------------------------------------- /examples/keggoc-examples.sh: -------------------------------------------------------------------------------- 1 | # orthologs of a specific gene 2 | pq-keggoc "keggoc_ortholog('http://identifiers.org/kegg.genes/hsa:326',G)" 3 | -------------------------------------------------------------------------------- /examples/kg-covid-examples.sh: -------------------------------------------------------------------------------- 1 | # g2p relations 2 | pq-kg-covid "X^gene,Y^protein,rdf(X,R,Y)" 3 | 4 | # count of entities by category 5 | pq-kg-covid "aggregate_group(count(I),[C],category(I,C),N)" 6 | 7 | -------------------------------------------------------------------------------- /examples/local-queries.sh: -------------------------------------------------------------------------------- 1 | # queries that use internal swi triplestore 2 | # no need to run local sparql endpoint, or connect to remote one 3 | # directly loads from examples/data 4 | 5 | # basic query 6 | pl2sparql -i ./examples/data/goslim_generic.ttl.gz -e "subClassOf(X,Y)" 7 | 8 | # identical query, using void registry 9 | pl2sparql -A examples/void.ttl -i goslim_generic -e "subClassOf(X,Y)" 10 | 11 | # direct superclasses of nucleus 12 | pl2sparql -u obo_core/goslim -i ./examples/data/goslim_generic.ttl.gz -e "nucleus_iri(N),subClassOf(N,X)" 13 | 14 | # all superclasses of nucleus (including blank nodes representing restrictions) 15 | pl2sparql -u obo_core/goslim -i ./examples/data/goslim_generic.ttl.gz -e "nucleus_iri(N),rdfs_subclass_of(N,X)" 16 | 17 | # everything the nucleus is part of 18 | pl2sparql -u obo_core/goslim -l -i ./examples/data/goslim_generic.ttl.gz -e "nucleus_iri(N),rdfs_subclass_of(N,Restr),owl_some(Restr,R,Y)" R-Y 19 | 20 | # same as above 21 | pl2sparql -l -i ./examples/data/goslim_generic.ttl.gz -e "lmatch(\"nucleus\",N),rdfs_subclass_of(N,Restr),owl_some(Restr,R,Y)" R-Y 22 | -------------------------------------------------------------------------------- /examples/local-sparql-queries.sh: -------------------------------------------------------------------------------- 1 | # -------------------- 2 | # SIMPLE LOCAL SPARQL TESTS 3 | # -------------------- 4 | # This assumes you have a SPARQL endpoint running with the provided sample triples. 5 | # Just do this: 6 | # 7 | # make bg 8 | # 9 | # From the top-level directory (docker required). This will 10 | # 1. Start a blazegraph instance 11 | # 2. Load that instance with triples from examples/data/ 12 | # 13 | # - The server will be at http://127.0.0.1:8889/bigdata 14 | # - The endpoint will be http://127.0.0.1:8889/bigdata/sparql 15 | # - The sparqlprog default endpoints includes a shortcut 'local' for this URL. 16 | # We use this here 17 | 18 | # --- 19 | # basic query; all subclass axioms 20 | # --- 21 | pl2sparql -s local "subClassOf(X,Y),label(X,XL),label(Y,YL)" 22 | 23 | # Expected results: 24 | # http://purl.obolibrary.org/obo/GO_0040007,http://purl.obolibrary.org/obo/GO_0008150,growth,biological_process 25 | # http://purl.obolibrary.org/obo/GO_0040011,http://purl.obolibrary.org/obo/GO_0008150,locomotion,biological_process 26 | # ... 27 | 28 | # --- 29 | # compile query; all subclass axioms 30 | # --- 31 | pl2sparql -C "subClassOf(X,Y)" 32 | 33 | # Expected results: 34 | # SELECT ?x ?y WHERE {?x ?y} 35 | 36 | # --- 37 | # direct superclasses of nucleus 38 | # --- 39 | pl2sparql -s local 'label(N,"nucleus"),subClassOf(N,X),label(X,XL)' X-XL 40 | 41 | # Expected results: 42 | # http://purl.obolibrary.org/obo/GO_0043226,organelle 43 | 44 | # convenience method: requires pack_install(obo_core) to fetch goslim module 45 | pl2sparql -u obo_core/goslim -s local "nucleus_iri(N),subClassOf(N,X),label(X,XL)" X-XL 46 | 47 | # Expected results: 48 | # http://purl.obolibrary.org/obo/GO_0043226,organelle 49 | 50 | # --- 51 | # all superclasses of nucleus 52 | # --- 53 | pl2sparql -u obo_core/goslim -s local "nucleus_iri(N),rdfs_subclass_of(N,X),label(X,XL)" "s(X,XL)" 54 | 55 | # Expected results: 56 | # http://purl.obolibrary.org/obo/GO_0005575,cellular_component 57 | # http://purl.obolibrary.org/obo/GO_0043226,organelle 58 | 59 | # --- 60 | # what is the nucleus part of? 61 | # --- 62 | pl2sparql -u obo_core/goslim -s local "nucleus_iri(N),rdfs_subclass_of(N,Restr),owl_some(Restr,R,Y),label(Y,YL)" "s(Y,YL)" 63 | 64 | # Expected results: 65 | # http://purl.obolibrary.org/obo/GO_0005622,intracellular 66 | 67 | # --- 68 | # synonyms 69 | # --- 70 | # this requires pack_install(obo_metadata) to fetch oio 71 | # basic query for all exact synonyms 72 | pl2sparql -u obo_metadata/oio -s local "has_exact_synonym_axiom(C,Syn,A)" 73 | 74 | # --- 75 | # synonyms + axiom annotations 76 | # --- 77 | # in ontologies that use the oio modeling approach, synonym axioms may be decorated with 78 | # axiom annotations providing additional info about each synonym assignment, including source (hasDbXref) 79 | pl2sparql -u obo_metadata/oio -s local "has_exact_synonym_axiom(C,Syn,A),has_dbxref(A,X)" "s(C,Syn,X)" 80 | 81 | # Expected results: 82 | # http://purl.obolibrary.org/obo/GO_0007005,mitochondrion organisation,GOC:mah 83 | # http://purl.obolibrary.org/obo/GO_0016887,adenosine triphosphatase activity,EC:3.6.1.3 84 | # http://purl.obolibrary.org/obo/GO_0006520,amino acid and derivative metabolism,GOC:curators 85 | 86 | -------------------------------------------------------------------------------- /examples/mbgd-examples.sh: -------------------------------------------------------------------------------- 1 | # orthologs of a specific gene 2 | pq-mbgd "mbgd_ortholog('http://mbgd.genome.ad.jp/rdf/resource/gene/cthe:CHRO_RS03290',G)" 3 | -------------------------------------------------------------------------------- /examples/monarch-examples.sh: -------------------------------------------------------------------------------- 1 | 2 | # phenotypes for subtypes of PBD 3 | pq-mi -f prolog 'label(D,DN),literal_exact_match(DN,"peroxisome biogenesis disorder"),rdfs_subclass_of(D,C),label(C,CN)' "x(C,CN)" 4 | 5 | # phenotypes for subtypes of PBD (using equivalence) 6 | pq-mi -f prolog 'label(D,DN),literal_exact_match(DN,"peroxisome biogenesis disorder"),rdfs_subclass_of(D,C),label(C,CN),owl_equivalent_class(C,E),has_phenotype(E,Z)' "x(C,CN,E,Z)" 7 | -------------------------------------------------------------------------------- /examples/ncats-red-examples.sh: -------------------------------------------------------------------------------- 1 | # all drugs with names 2 | pq-ncats-red -f tsv "drug(X)" -l -L name 3 | 4 | # everything about Imatinib 5 | pq-ncats-red -f tsv 'name(X,"Imatinib"),rdf(X,R,Y)' -l -L name 6 | -------------------------------------------------------------------------------- /examples/nextprot-examples.sh: -------------------------------------------------------------------------------- 1 | # -------------------- 2 | # NEXTPROT EXAMPLES 3 | # -------------------- 4 | # Taken from https://snorql.nextprot.org/ 5 | 6 | # --- 7 | # entries with an isoform that is phosphorylated and in cytoplasm 8 | # --- 9 | pq-nextprot "isoform(P,F),phosphoprotein(F),cellular_component_term_child_of(F,nextprot_cv:'SL-0086')" 10 | -------------------------------------------------------------------------------- /examples/oma-examples.sh: -------------------------------------------------------------------------------- 1 | 2 | # all orthologs of an ensembl gene 3 | # taken from https://sparql.omabrowser.org/lode/sparql? 4 | pq-oma "is_orthologous_to(X,Y),encoded_by_ensembl_gene(X,ensembl:'ENSLACG00000002497'),encoded_by_ensembl_gene(Y,YG)" 5 | 6 | # federated query 7 | # see examples/oma_ebi_federated.pl for explanation 8 | pl2sparql -e -d sparqlprog --consult examples/oma_ebi_federated.pl "orthologs_with_coordinates(ensembl:'ENSG00000198840',G2,Tax,B,E,R)" 9 | 10 | -------------------------------------------------------------------------------- /examples/oma_ebi_federated.pl: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | Example: 5 | 6 | pl2sparql -e --consult examples/oma_ebi_federated.pl "orthologs_with_coordinates(ensembl:'ENSG00000198840',G2,Tax,E,R)" 7 | 8 | We pass the -e option to force execution within the prolog environment (rather attempting to compile the function) 9 | 10 | Within the prolog environment, the ?? predicate will explicitly invoke sparqlprog on an explicit endpoint 11 | 12 | */ 13 | 14 | :- use_module(library(sparqlprog/ontologies/oma)). 15 | :- use_module(library(sparqlprog/ontologies/ebi),[]). 16 | :- use_module(library(sparqlprog/ontologies/faldo)). 17 | 18 | %! orthologs_with_coordinates(?Gene1, ?Gene2, ?Taxon, ?Begin:int, End:int, ?Ref) :- 19 | orthologs_with_coordinates(G1,G2,T,B,E,R) :- 20 | ( oma ?? (is_orthologous_to(P1,P2), 21 | organism_in_taxon(P2,T), 22 | encoded_by_ensembl_gene(P1,G1), 23 | encoded_by_ensembl_gene(P2,G2) 24 | )), 25 | ( ebi ?? location(G2,B,E,R)). 26 | 27 | -------------------------------------------------------------------------------- /examples/ontobee-examples.sh: -------------------------------------------------------------------------------- 1 | # All properties in PATO 2 | pq-ontobee "in_ontology(X,pato,owl:'ObjectProperty')" 3 | 4 | # All properties in NCIT, plus labels 5 | pq-ontobee -f prolog "in_ontology(X,ncit,owl:'ObjectProperty'),label(X,N)" "x(X,N)" 6 | 7 | # All properties in NCIT, auto-labels, contract URIs, show domain and range 8 | pq-ontobee -u sparqlprog/ontologies/ncit -l -f csv "in_ontology(X,ncit,owl:'ObjectProperty'),owl:domain(X,D),owl:range(X,R)" "x(X,D,R)" 9 | 10 | # Same, explicit 11 | pq-ontobee -u sparqlprog/ontologies/ncit -f csv "in_ontology(P,ncit,owl:'ObjectProperty'),label(P,PN),owl:domain(P,D),owl:range(P,R),label(D,DN),label(R,RN)" "x(P,PN,D,DN,R,RN)" 12 | 13 | # all property usages across ontobee (TODO: select DISTINCT) 14 | pq-ontobee "rdf(Restr,owl:onProperty,P,G),label(P,PN)" "x(Restr,P,PN,G)" 15 | 16 | # similar to above, but instead load RO triples into memory, loop over all OP directly in prolog engine, 17 | # executing separate sparql query for each 18 | pq-ontobee -e -i ro.owl "owl:objectProperty(P), (ontobee ?? rdf(Restr,owl:onProperty,P,G),label(P,PN))" "x(P,PN,G,Restr)" 19 | pq-ontobee -e -i ro.owl "owl:objectProperty(P), label(P,PN), (ontobee ?? aggregate_group(count(Restr),[P,G],rdf(Restr,owl:onProperty,P,G),Num))" "x(P,PN,G,Num)" 20 | 21 | # also: usages of property by graph 22 | pq-ontobee "aggregate_group(count(P),[P,G],rdf(_,owl:onProperty,P,G),Num)" 23 | 24 | # number of distinct xrefs per graph 25 | pq-ontobee -f tsv "aggregate_group(count(distinct(X)),[G],has_dbxref(C,X,G),NumX)" 26 | 27 | # exact matches between mondo and other resources 28 | pq-ontobee -u sparqlprog/ontologies/skos -C "in_ontology(X,mondo),has_exact_match(X,Y)" 29 | 30 | # all triples with a literal with a trailing whitespace 31 | pq-ontobee 'rdf(C,P,V),is_literal(V),str_ends(str(V)," ")' 32 | 33 | # all redundant subclass assertions 34 | pq-ontobee -l "subClassOf(A,B),subClassOf(B,C),subClassOf(A,C)" 35 | 36 | # class labels that match exact synonyms of other classes 37 | pq-ontobee -l -u obo_metadata/oio 'has_exact_synonym(C1,N),label(C2,N),C1\=C2' 38 | 39 | # all labels used more than once 40 | 41 | # This causes virtuoso error: Virtuoso 22026 Error SR319: Max row length is exceeded when trying to store a string of 17 chars into a temp col 42 | ## pq-ontobee -f tsv "aggregate_group(count(distinct(C)),[N],(rdf(C,rdfs:label,N1),N is lcase(str(N1))),count(distinct(C))>1,R)" "x(N,R)" 43 | 44 | pq-ontobee -f tsv "aggregate_group(count(distinct(C)),[N],(rdf(C,rdfs:label,N1),N is str(N1)),count(distinct(C))>1,R)" "x(N,R)" 45 | 46 | # same, exclude obsoletes 47 | pq-ontobee -f tsv "aggregate_group(count(distinct(C)),[N],(rdf(C,rdfs:label,N1),N is str(N1), \+deprecated(C)),count(C)>1,R)" "x(N,R)" 48 | 49 | # -------- 50 | # SEARCH 51 | # -------- 52 | # The '/' indicates that all subsequent arguments are to be assembled into a query term, in this case `ontsearch("uberon","limb$",_,_)` 53 | # The ontobee module defines ontsearch/4 as a search within an ontology, using the mapping from the obo id (e.g. 'uberon') to 54 | # the graph in which this is stored in ontobee 55 | pq-ontobee / ontsearch uberon limb$ _ _ 56 | 57 | # also searches synonyms 58 | pq-ontobee -l / tsearch ^hippocamp _ _ 59 | -------------------------------------------------------------------------------- /examples/pubmedgraph-examples.sh: -------------------------------------------------------------------------------- 1 | # Examples for querying the RENCI pubmedgraph 2 | # 3 | # Uses the pq-pmd wrapper that defines the endpoint plus prefixes such as pmid 4 | 5 | # --- 6 | # what does PMID:1 reference? 7 | # --- 8 | pq-pmg "references(pmid:'1',X)" 9 | 10 | # Find all publications that either reference a class, or a descendant of that class. 11 | # E.g., MONDO:0015229 ! Bardet-Biedl syndrome 12 | # 13 | # This is an example of federation: we use the monarch graph to get subclasses of 14 | # the disease of interest, and look these up iteratively on pubmedgraph 15 | # 16 | # For this we use the -e option as the outer query is executed directly in prolog. 17 | # The ??/2 predicate is used to query a remote triplestore. 18 | pq-pmg -e "(monarch ?? rdfs_subclass_of(C,obo:'MONDO_0015229')),(pmg ?? references(P,C))" 19 | 20 | pq-pmg -i mondo -e "subclass_axiom_validation(A,B,P)" 21 | -------------------------------------------------------------------------------- /examples/ubergraph-examples.sh: -------------------------------------------------------------------------------- 1 | # All cells 2 | pq-ubergraph -l "rdf_closure(C,rdfs:subClassOf,obo:'CL_0000000')" 3 | 4 | # All cells and what they are part of 5 | pq-ubergraph -l "rdf_closure(C,rdfs:subClassOf,obo:'CL_0000000'),part_of(C,Y,ubergraph:nonredundant)" 6 | 7 | # All cells in the kidney 8 | pq-ubergraph -l "subClassOf(C,obo:'CL_0000000'),rdf(C,obo:'BFO_0000050',A),subClassOf(A,obo:'UBERON_0002113')" 9 | 10 | # All cells and what they are part of; this time for any class that fits the label 11 | pq-ubergraph -l "lsearch('^cell$',Cell),rdf_closure(C,rdfs:subClassOf,Cell),part_of(C,Y,ubergraph:nonredundant)" "row(C,Y)" 12 | 13 | # All properties, plus labels 14 | pq-ubergraph -f prolog "rdf(X,rdf:type,owl:'ObjectProperty'),label(X,N)" "x(X,N)" 15 | 16 | # All properties, plus labels, and domain and ranges 17 | # not entailed. See https://github.com/NCATS-Tangerine/ubergraph/issues/13 18 | pq-ubergraph -l -f csv "rdf(X,rdf:type,owl:'ObjectProperty'),owl:domain(X,D),owl:range(X,R)" "x(X,D,R)" 19 | 20 | # all properties used 21 | pq-ubergraph --distinct "rdf_ontology(S,P,O),label(P,PN)" P 22 | 23 | # summary stats for above 24 | pq-ubergraph "aggregate_group(count(distinct(S)),[P],rdf_ontology(S,P,O),N)" "x(P,N)" 25 | 26 | # all triples with a literal with a trailing whitespace 27 | pq-ubergraph 'rdf(C,P,V),is_literal(V),str_ends(str(V)," ")' 28 | 29 | # MRCA for pairs of neurons 30 | # note we use egraph_mrca/3 not mrca/3 because we have subclass 31 | # entailments materialized, no need for sparql paths 32 | pq-ubergraph -l "egraph_mrca(obo:'CL_1000379',obo:'CL_0002285',A)" 33 | 34 | 35 | # -------- 36 | # SEARCH 37 | # -------- 38 | # The '/' indicates that all subsequent arguments are to be assembled into a query term, in this case `ontsearch("uberon","limb$",_,_)` 39 | 40 | # also searches synonyms 41 | pq-ubergraph -l / tsearch ^hippocamp _ _ 42 | -------------------------------------------------------------------------------- /examples/uniprot-examples.sh: -------------------------------------------------------------------------------- 1 | # -------------------- 2 | # UNIPROT EXAMPLES 3 | # -------------------- 4 | 5 | # --- 6 | # human proteins with disease and interpro 7 | # --- 8 | pq-up "in_human(P),annotation(P,A),disease_annotation(A),interpro(P,X)" 9 | 10 | # --- 11 | # diseases for a specific protein and their annotations 12 | # --- 13 | pq-up "protein_natural_variant_disease(uniprot:'Q15465',A,D),substitution(A,Seq)" "x(A,D,Seq)" 14 | 15 | # link from a specific protein to its variant to associated disease and the dbsnp id for the variant 16 | pq-up "protein_natural_variant_disease_dbsnp(uniprot:'Q15465',A,D,X)" 17 | 18 | # all xrefs 19 | pq-up "protein_natural_variant_disease_xref(uniprot:'Q15465',A,D,X)" 20 | 21 | # all annotations and their types on a specific protein 22 | pq-up "annotation(uniprot:'Q15465',A),rdf(A,rdf:type,T)" 23 | 24 | # all sequence annotation types and their parent classification 25 | pq-up "rdfs_subclass_of(T,up:'Sequence_Annotation'),subClassOf(T,Parent),rdf_is_iri(Parent)" 26 | 27 | # GO, KW annotations 28 | pq-up "classified_with(uniprot:'B5MCD5',T)" 29 | 30 | # rhea annotations 31 | pq-up "protein_has_catalyzed_reaction(uniprot:'A0A1H0K750',X)" 32 | pq-up "protein_has_catalyzed_reaction(P,rhea:'17641')" 33 | 34 | # --- 35 | # FEDERATION 36 | # --- 37 | # 38 | # we show federated queries in 2 directions, and alternating between two sparql endpoints as base 39 | # 40 | # 1a. EC->rhea->Protein, uniprot as base 41 | pq-up-rhea "service(rhea,rhea_ec(R,enzyme:'4.1.1.19')),protein_has_catalyzed_reaction(P,R)" 42 | # 1b. EC->rhea->Protein, rhea as base 43 | pq-rhea-up "rhea_ec(X,enzyme:'4.1.1.19'),service(uniprot,protein_has_catalyzed_reaction(P,X))" 44 | 45 | # 2a. Protein->Rhea->EC, rhea as base 46 | pq-rhea-up "service(uniprot,protein_has_catalyzed_reaction(uniprot:'A0A1H0K750',X)),rhea_ec(X,Y)" 47 | # 2b. Protein->Rhea->EC, uniprot as base 48 | pq-up-rhea "protein_has_catalyzed_reaction(uniprot:'A0A1H0K750',X),service(rhea,rhea_ec(X,Y))" 49 | 50 | 51 | # same logic but federating via sparqlprog 52 | pq-up-rhea -e "(uniprot ?? protein_has_catalyzed_reaction(uniprot:'A0A1H0K750',X)),(rhea ?? rhea_ec(X,Y))" 53 | pq-up-rhea -e "(rhea ?? rhea_ec(X,enzyme:'4.1.1.19')), (uniprot ?? protein_has_catalyzed_reaction(P,X))" 54 | 55 | # human RHEA annotations 56 | pq-up "in_reference_proteome(P),rdf(P,up:reviewed,Rev),in_taxon(P,uptaxon:'9606'),protein_has_catalyzed_reaction(P,R)" "row(P,R)" 57 | 58 | # proteins associated with >1 disease 59 | pq-up -l -L pref_label "protein_natural_variant_disease(P,V1,D1),protein_natural_variant_disease(P,V2,D2),D1\=D2" 60 | 61 | # count of all proteins 62 | $ pq-up "aggregate(count(P),protein(P),N)" "count(P)" 63 | #262095770 64 | 65 | 66 | # --- 67 | # distinct databases used in xrefs 68 | # --- 69 | pq-up --distinct "database(X,D)" "x(D)" 70 | 71 | # --- 72 | # count of xrefs broken down by database 73 | # --- 74 | pq-up "aggregate_group(count(X),[D],database(X,D),N)" 75 | 76 | # --- 77 | # all MGI xrefs 78 | # --- 79 | pq-up "xref(P,X),database(X,updb:'MGI')" 80 | 81 | # reviewed subset 82 | pq-up "xref(P,X),database(X,updb:'MGI'),reviewed(P)" 83 | 84 | # xrefs to OMIM (this will be both proteins and diseases) 85 | pq-up -l "xref(P,X),database(X,updb:'MIM')" 86 | 87 | # --- 88 | # describe a specific mouse protein 89 | # --- 90 | pq-up describe http://purl.uniprot.org/uniprot/Q78E61 91 | 92 | 93 | # --- 94 | # all in a taxon 95 | # --- 96 | pq-up "protein(P),in_taxon(P,uptaxon:'7955')" 97 | 98 | # --- 99 | # all reference proteins in a genome 100 | # --- 101 | pq-up "in_reference_proteome(P),in_taxon(P,uptaxon:'7955')" 102 | 103 | 104 | # --- 105 | # zebrafish reference proteins with no ZFIN mapping 106 | # --- 107 | pq-up "in_reference_proteome(P),rdf(P,up:reviewed,Rev),in_taxon(P,uptaxon:'7955'),\+xref_in(P,X,'ZFIN')" "row(P,Rev)" 108 | 109 | # wormbase genes 110 | pq-up "in_reference_proteome(P),rdf(P,up:reviewed,Rev),in_taxon(P,uptaxon:'6239'),xref_in(P,X,'WormBase')" 111 | pq-up "in_reference_proteome(P),rdf(P,up:reviewed,Rev),xref(P,X),database(X,D),is_wormbase(D)" "row(P,X)" 112 | 113 | # all araport 114 | pq-up "in_reference_proteome(P),rdf(P,up:reviewed,Rev),in_taxon(P,Tax),xref_in(P,X,'Araport')" 115 | 116 | # https://douroucouli.wordpress.com/2020/08/05/what-is-the-sars-cov-2-molecular-parts-list/#comments 117 | # peptides with descriptions 118 | pq-up "peptide_annotation(Pep),rdf(Pep,rdfs:comment,D),annotation(P,Pep),in_taxon(P,uptaxon:'9606')" 119 | -------------------------------------------------------------------------------- /examples/void.ttl: -------------------------------------------------------------------------------- 1 | @prefix void: . 2 | @prefix lib: . 3 | @prefix owl: . 4 | @prefix rdf: . 5 | @prefix rdfs: . 6 | @prefix dcterms: . 7 | @prefix foaf: . 8 | @prefix : . 9 | 10 | 11 | a void:Dataset ; 12 | dcterms:title "uberon" ; 13 | void:dataDump ; 14 | lib:source :uberon.owl . 15 | 16 | a void:Dataset ; 17 | dcterms:title "goslim for testing" ; 18 | void:dataDump ; 19 | lib:source :go.owl . 20 | -------------------------------------------------------------------------------- /examples/wikidata-examples.sh: -------------------------------------------------------------------------------- 1 | # WikiData Examples 2 | # --- 3 | # These all use pq-wd in the bin/ directory, which is a one-line wrapper onto pl2sparql, 4 | # which loads the wikidata.pl module and sets the endpoint to the main wikidata endpoint. 5 | 6 | # -- 7 | # Non-Biology Examples 8 | # -- 9 | 10 | # continents and their english-language labels 11 | pq-wd 'continent(C),label(C,CN),lang(CN)="en"' 12 | 13 | # as above, using convenience predicate 14 | pq-wd 'continent(C),enlabel(C,CN)' 15 | 16 | # same, passing in enlabel/2 as predicate for fetching labe 17 | pq-wd -l -L enlabel 'continent(C)' 18 | 19 | # same 20 | pq-wd -l -L enlabel continent 21 | 22 | # all countries (direct) 23 | pq-wd -l -L enlabel country 24 | 25 | # all countries (direct and indirect) 26 | pq-wd -l -L enlabel country_inf 27 | 28 | # same, with their types 29 | pq-wd -f tsv -l -L enlabel 'country_inf(C),instance_of(C,T)' 30 | 31 | # countries and triples linking countries to other things 32 | # note we use enlabel_any/2 which is capable of labeling wdt:properties 33 | pq-wd -l -L enlabel_any 'country(C),rdf(C,P,D),instance_of(D,DT)' 34 | 35 | # all transitive superclasses of the class 'country' 36 | pq-wd -l -L enlabel 'country_iri(C),subclass_of_transitive(C,D)' 37 | pq-wd -f tsv -l -L enlabel 'country_iri(C),subclass_of_transitive(C,D)' 'parent(D)' 38 | 39 | # power stations and their locations 40 | # Note: affixing "_inf" on the end of a class predicate will return inferred classification 41 | pq-wd "power_station_inf(X),coordinate_location(X,Loc),enlabel(X,XN)" 42 | 43 | # cities with populations over 10m, plus the contininent they are part of 44 | pq-wd -l -L enlabel "city_inf(City),part_of_continent(City,Continent),population(City,Pop),Pop>10000000" 45 | 46 | # cities in more than one continent 47 | pq-wd "city_inf(C),part_of_continent(C,X1),part_of_continent(C,X2),X1@>X2,enlabel(C,CN)" 48 | 49 | # -- 50 | # Wikidata schema/ontology 51 | # -- 52 | 53 | # is-a links 54 | pq-wd -L enlabel -l subclass_of 55 | 56 | # -- 57 | # Biology Examples 58 | # -- 59 | 60 | # disease-to-gene-to-protein 61 | # this uses enlabel/2, which is the english-language label 62 | pq-wd "disease(D),genetic_association(D,G),encodes(G,P),enlabel(D,DN),enlabel(P,PN)" 63 | 64 | # diseases, causes and treatments 65 | pq-wd "isa_disease(X),has_cause(X,C),treated_by_drug(X,S),enlabel(X,XN),enlabel(C,CN),enlabel(S,SN)" 66 | 67 | # civic variants and their types 68 | pq-wd "civic_id(V,X),instance_of(V,T),enlabel(V,VN),enlabel(T,TN)" 69 | 70 | # variant that is PTP for a drug-condition 71 | # See WD example queries 72 | pq-wd "V='http://www.wikidata.org/entity/Q25100112', civic_id(V,X),positive_therapeutic_predictor_e2s(V,S),medical_condition_treated_s2q(S,C),positive_therapeutic_predictor_s2v(S,Drug),enlabel(Drug,DrugN),enlabel(V,VN),enlabel(C,CN)" "v2d_cond(V,VN,Drug,DrugN,C,CN)" 73 | 74 | # -- 75 | # Bio-ontology Examples 76 | # -- 77 | 78 | # concepts with an exact match to something in SO 79 | pq-wd 'exact_match(X,URI),str_starts(str(URI),"http://purl.obolibrary.org/obo/SO_")' 80 | 81 | # properties mapped to RO 82 | pq-wd 'ro_id(P,X)' 83 | 84 | # Get GO to MESH mappings (via shared WD entry) 85 | pq-wd "mesh_id(X,M),go_id(X,G)" 86 | 87 | # clinical trials involving PKIs 88 | pq-wikidata -l -L enlabel "clinical_trial(T),research_intervention(T,D),isa_protein_kinase_inhibitor(D)" 89 | -------------------------------------------------------------------------------- /examples/wikipathways-examples.sh: -------------------------------------------------------------------------------- 1 | # WikiPathway Examples 2 | # --- 3 | # Adapted from https://www.wikipathways.org/index.php/Help:WikiPathways_Metabolomics 4 | 5 | # all pathways 6 | pq-wp pathway/1 7 | 8 | # all metabolites 9 | pq-wp metabolite/1 10 | 11 | # all metabolite properties 12 | pq-wp --distinct "metabolite(M),rdf(M,P,V)" "x(P)" 13 | 14 | # metabolite count 15 | pq-wp "aggregate(count(distinct(M)),metabolite(M),Num)" "x(Num)" 16 | # 5381 17 | 18 | # metabolites part of pathways 19 | pq-wp "metabolite(M),part_of(M,P),pathway(P)" 20 | 21 | # distinct human metabolites 22 | pq-wp --distinct "metabolite(M),part_of(M,P),organism(P,ncbitaxon:'9606')" "x(M)" 23 | 24 | # integrative queries: find all classic metabolic pathways in wikidata using PW ontology 25 | # first do this: 26 | curl -L -s http://purl.obolibrary.org/obo/pw.owl > /tmp/pw.owl 27 | # then we will load this into the in-memory swipl RDF triplestore; 28 | # we will find all inferred subclasses of our pathway, and all wikipathway pathways associated. 29 | # note that we use "-e" to run in prolog evaluation mode, and use ??/2 to explicitly make a sparqlprog query 30 | pq-wp -i /tmp/pw.owl -e "label_of('classic metabolic pathway',Match),rdfs_subclass_of(T,Match),(wikipathways ?? ontology_tag(P,T))" "x(P,T)" 31 | -------------------------------------------------------------------------------- /install.pl: -------------------------------------------------------------------------------- 1 | 2 | install_requirements :- 3 | writeln(loading), 4 | ensure_loaded(pack), 5 | Opts=[interactive(false)], 6 | forall(requires(X), 7 | pack_install(X, Opts)). 8 | 9 | install_optional_packs :- 10 | writeln(loading), 11 | ensure_loaded(pack), 12 | ensure_loaded(packplus), 13 | Opts=[interactive(false)], 14 | \+ \+ clause(optional_pack(_),_), 15 | forall(optional_pack(X), 16 | pack_install(X, Opts)). 17 | 18 | install_this :- 19 | Opts=[interactive(false)], 20 | install_requirements, 21 | install_optional_packs, 22 | name(X), 23 | pack_install(X,Opts). 24 | 25 | install_this_full :- 26 | install_this, 27 | install_optional_packs. 28 | -------------------------------------------------------------------------------- /pack.pl: -------------------------------------------------------------------------------- 1 | name('sparqlprog'). 2 | title('Logic programming with SPARQL'). 3 | version('0.2.9'). 4 | author('Chris Mungall','cjmungall@lbl.gov'). 5 | author('Samer Abdallah','s.abdallah@ucl.ac.uk'). 6 | maintainer('Chris Mungall','cjmungall@lbl.gov'). 7 | packager('Chris Mungall','cjmungall@lbl.gov'). 8 | download('https://github.com/cmungall/sparqlprog/releases/*.zip' ). 9 | requires(dcgutils). 10 | requires(regex). 11 | requires(typedef). 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /packplus.pl: -------------------------------------------------------------------------------- 1 | optional_pack(obo_metadata). 2 | -------------------------------------------------------------------------------- /prolog/concurrency.pl: -------------------------------------------------------------------------------- 1 | /* Part of sparkle 2 | Copyright 2014-2015 Samer Abdallah (UCL) 3 | 4 | This program is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public License 6 | as published by the Free Software Foundation; either version 2 7 | of the License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | :- module(concurrency, [concurrent_or/1, concurrent_or/3]). 20 | 21 | :- meta_predicate concurrent_or(-,:,+). 22 | :- meta_predicate concurrent_or(:). 23 | 24 | %% concurrent_or( +Goals:list(callable)) is nondet. 25 | % 26 | % Simple interface to concurrent_or/3. Equivalent to 27 | % concurrent_or(Vars,Goals,[]) where Vars is a list of all the variables in 28 | % Goals. 29 | concurrent_or(Goals) :- 30 | term_variables(Goals,Vars), 31 | concurrent_or(Vars,Goals,[]). 32 | 33 | %% concurrent_or( -Vars, +Goals:list(callable), +Options:list(option)) is nondet. 34 | % 35 | % Succeeds once for each solution of each goal in Goals, with Vars bound 36 | % to sharing variables in Goals. Goals are executed in parallel. Valid 37 | % options are 38 | % * on_error(OnError:oneof([stop,continue])) 39 | % If OnError=stop, then an exception occuring in any goal stops all 40 | % goals and is propagated back to and then thrown from the main thread. 41 | % If OnError=continue, then an exception in a goal terminates only 42 | % that thread, with a error message printed. The default is stop. 43 | % * queue_factor(K:natural) 44 | % Solutions are communicated via a message queue of size K*length(Goals). 45 | % This limits the extent to which threads compute solutions that have not 46 | % yet been requested. The default is 1. 47 | % Any remaining options are passed to thread_create/3. 48 | 49 | concurrent_or(Vars, M:List, Options) :- 50 | select_option(on_error(OnError),Options,Opts1,stop), 51 | select_option(queue_factor(K),Opts1,Opts2,1), 52 | length(List, JobCount), 53 | QueueSize is K*JobCount, 54 | message_queue_create(Done,[max_size(QueueSize)]), 55 | setup_call_cleanup( 56 | maplist(create_worker(M,Vars,Done,Opts2),List,Solvers), 57 | wait_for_one(JobCount, Done, Vars, OnError), 58 | ( debug(concurrency,'Sending kill signal to workers',[]), 59 | maplist(kill_thread,Solvers), drain(Done), 60 | debug(concurrency,'Waiting for workers to die.',[]), 61 | maplist(thread_join,Solvers,_), 62 | message_queue_destroy(Done) 63 | ) 64 | ). 65 | 66 | drain(Q) :- thread_get_message(Q,_,[timeout(0)]) -> drain(Q); true. 67 | kill_thread(Id) :- catch(thread_signal(Id,throw(abort)),_,true). 68 | create_worker(M,V,Q,O,H,Id) :- thread_create(worker(M:H,V,Q),Id,O). 69 | 70 | wait_for_one(N, Q, X, OnError) :- 71 | succ(N1,N), 72 | thread_get_message(Q, Msg), 73 | ( Msg=success(_,Var) -> (X=Var; wait_for_one(N,Q,X,OnError)) 74 | ; Msg=failed(_) -> wait_for_one(N1,Q,X,OnError) 75 | ; Msg=error(_,E) -> ( OnError=stop -> throw(error(E)) 76 | ; print_message(error,E), 77 | wait_for_one(N1,Q,X,OnError) 78 | ) 79 | ). 80 | 81 | worker(Goal,Var,Q) :- 82 | thread_self(Me), 83 | debug(concurrency,'Worker started on ~q.',[Goal]), 84 | ( catch( Goal,E, (thread_send_message(Q,error(Me,E)), throw(error))), 85 | thread_send_message(Q,success(Me,Var)), fail 86 | ; thread_send_message(Q,failed(Me)), 87 | debug(concurrency,'Worker finished normally.',[]) 88 | ). 89 | -------------------------------------------------------------------------------- /prolog/sparqlprog/blazegraph.pl: -------------------------------------------------------------------------------- 1 | /** blazegraph 2 | 3 | convenience predicates for working with blazegraph (formelerly bigdata) triplestores 4 | 5 | */ 6 | 7 | :- module(blazegraph, 8 | [ 9 | literal_exact_match/2, 10 | 11 | literal_match/2, 12 | literal_match_relevance/3 13 | ]). 14 | :- use_module(library(sparqlprog)). 15 | 16 | :- rdf_register_prefix(bds,'http://www.bigdata.com/rdf/search#'). 17 | 18 | %! literal_match(?Lit, +Term) is nondet. 19 | % 20 | % wraps bds:search 21 | literal_match(Lit,Term) :- 22 | rdf(Lit,bds:search,Term). 23 | 24 | 25 | %! literal_exact_match(?Lit, +Term) is nondet. 26 | % 27 | % wraps bds:search, sets bds:matchExact to true 28 | literal_exact_match(Lit,Term) :- 29 | rdf(Lit,bds:search,Term), 30 | rdf(Lit,bds:maxRank,"1"), 31 | rdf(Lit,bds:matchExact,"true"). 32 | 33 | %! literal_match_relevance(?Lit, +Term, ?Relevance) is nondet. 34 | % 35 | % searches for literal and scores match by relevance 36 | literal_match_relevance(Lit,Term,Relevance) :- 37 | rdf(Lit,bds:search,Term), 38 | rdf(Lit,bds:relevance,Relevance). 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /prolog/sparqlprog/dataframe.pl: -------------------------------------------------------------------------------- 1 | :- module(dataframe, 2 | [dataframe_header/2, 3 | dataframe_header/3, 4 | dataframe_row/2, 5 | dataframe_row/3, 6 | dataframe_row/4, 7 | 8 | dataframe_to_csv/2, 9 | dataframe_to_csv/3]). 10 | 11 | :- use_module(library(semweb/rdf11)). 12 | 13 | /** 14 | 15 | Example 16 | 17 | dataframe(foo, 18 | [id=ID, 19 | name=Name]-person(ID,Name), 20 | [street=Street, 21 | city=City]-address(ID,Street,City), 22 | ...], 23 | [description('person and location report'), 24 | entity(city)]). 25 | 26 | TODO 27 | 28 | frame('person location report') << 29 | foo(id=ID :: entity, name=Name) where person(ID,Name), 30 | foo(street=Street, city=City :: entity) where address(ID,Street,City). 31 | 32 | 33 | */ 34 | 35 | :- multifile dataframe/2. 36 | :- multifile dataframe/3. 37 | 38 | dataframe_specs_opts(Name,Specs,Opts) :- dataframe(Name,Specs,Opts). 39 | dataframe_specs_opts(Name,Specs,[]) :- dataframe(Name,Specs). 40 | 41 | :- module_transparent(dataframe_header/2). 42 | :- module_transparent(dataframe_header/3). 43 | dataframe_header(Name,Header) :- 44 | dataframe_header(Name,Header,[]). 45 | dataframe_header(Name,Header,Opts) :- 46 | dataframe_specs_opts(Name,Specs,SpecOpts), 47 | findall(K, 48 | ( member(Sub-_,Specs), 49 | member(K=_,Sub)), 50 | Header1), 51 | inject_labels_to_header(Header1,Header,SpecOpts,Opts). 52 | 53 | inject_labels_to_header([],[],_,_). 54 | inject_labels_to_header([K|L],[K,KN|L2],SpecOpts,Opts) :- 55 | member(entity(K),SpecOpts), 56 | !, 57 | atom_concat(K,' label',KN), 58 | inject_labels_to_header(L,L2,SpecOpts,Opts). 59 | inject_labels_to_header([K|L],[K|L2],SpecOpts,Opts) :- 60 | inject_labels_to_header(L,L2,SpecOpts,Opts). 61 | 62 | 63 | 64 | :- module_transparent(dataframe_row/2). 65 | :- module_transparent(dataframe_row/3). 66 | :- module_transparent(dataframe_row/4). 67 | dataframe_row(Name,Row) :- 68 | dataframe_row(Name,Row,[]). 69 | dataframe_row(Name,Row,Opts) :- 70 | dataframe_specs_opts(Name,_Specs,SpecOpts), 71 | dataframe_row(Name,Row,Opts,SpecOpts). 72 | dataframe_row(Name,Row,Opts,SpecOpts) :- 73 | dataframe_specs_opts(Name,[Spec|Specs],SpecOpts), 74 | spec_bindings_goal(Spec,Bs,G), 75 | dataframe_header(Name,Header,Opts), 76 | call_wrap(G,SpecOpts), 77 | maplist([_=V,V]>>true,Bs,Row1), 78 | apply_specs(Specs,Row2), 79 | append(Row1,Row2,Row3), 80 | flatten_row(Row3,Row,Header,[Spec|Specs],SpecOpts,Opts). 81 | 82 | :- module_transparent(call_wrap/2). 83 | call_wrap(G,Opts) :- 84 | member(endpoint(Endpoint),Opts), 85 | !, 86 | ??(Endpoint, G). 87 | call_wrap(G,_) :- 88 | G. 89 | 90 | :- module_transparent(apply_specs/2). 91 | apply_specs([],[]). 92 | apply_specs([Spec|Specs],Row) :- 93 | spec_bindings_goal(Spec,Bs,G), 94 | ( setof(Bs,G,BsSet) 95 | -> true 96 | ; BsSet=[]), 97 | maplist({BsSet}/[Var=_,Vals] >> findall(X,(member(Bs1,BsSet),member(Var=X,Bs1)),Vals),Bs,Row1), 98 | append(Row1,Row2,Row), 99 | apply_specs(Specs,Row2). 100 | 101 | :- module_transparent(spec_bindings_goal/3). 102 | spec_bindings_goal(Bs-G,Bs,G). 103 | 104 | 105 | 106 | flatten_row([],[],_,_,_,_). 107 | flatten_row([V|Row],Row2,Keys,Specs,SpecOpts,Opts) :- 108 | Keys=[K|_], 109 | select(entity(K),SpecOpts,SpecOpts2), 110 | !, 111 | get_labels(V,Ns,Opts), 112 | contract_uris(V,V2,Opts), 113 | flatten_row([V2,Ns|Row],Row2,Keys,Specs,SpecOpts2,Opts). 114 | flatten_row([V|Row],[V2|Row2],Keys,Specs,SpecOpts,Opts) :- 115 | Keys=[K|Keys2], 116 | select(iri(K),SpecOpts,SpecOpts2), 117 | !, 118 | contract_uris(V,V1,Opts), 119 | serialize_value(V1,V2,Opts), 120 | flatten_row(Row,Row2,Keys2,Specs,SpecOpts2,Opts). 121 | flatten_row([V|Row],[V2|Row2],[_K|Keys],Specs,SpecOpts,Opts) :- 122 | serialize_value(V,V2,Opts), 123 | flatten_row(Row,Row2,Keys,Specs,SpecOpts,Opts). 124 | 125 | % todo: hook 126 | get_labels(L,Vs,Opts) :- 127 | is_list(L), 128 | !, 129 | maplist({Opts}/[X,X2]>>get_labels(X,X2,Opts),L,L2), 130 | flatten(L2,Vs). 131 | get_labels(X,V,_Opts) :- 132 | rdf_is_iri(X), 133 | rdf(X,rdfs:label,V), 134 | !. 135 | get_labels(_,'',_). 136 | 137 | contract_uris(L,L2,Opts) :- is_list(L),!,maplist({Opts}/[A,B]>>contract_uri(A,B,Opts),L,L2). 138 | contract_uris(A,B,Opts) :- contract_uri(A,B,Opts). 139 | contract_uri(A,B,_) :- rdf_is_iri(A),rdf_global_id(Pre:Local,A),!,concat_atom([Pre,Local],':',B). 140 | contract_uri(A,A,_). 141 | 142 | 143 | serialize_value(V,'_',_) :- 144 | var(V), 145 | !. 146 | serialize_value(L,V,Opts) :- 147 | is_list(L), 148 | !, 149 | maplist({Opts}/[X,X2]>>serialize_value(X,X2,Opts),L,L2), 150 | option(internal_sep(Sep),Opts,'|'), 151 | concat_atom(L2,Sep,V). 152 | serialize_value(S@_,V,_) :- 153 | !, 154 | atom_string(V,S). 155 | serialize_value(S^^_,V,_) :- 156 | !, 157 | atom_string(V,S). 158 | serialize_value(literal(type(_,V)),V,_) :- 159 | !. 160 | serialize_value(literal(lang(_,V)),V,_) :- 161 | !. 162 | serialize_value(literal(V),V,_) :- 163 | !. 164 | serialize_value(S,V,_) :- 165 | string(S), 166 | !, 167 | atom_string(V,S). 168 | serialize_value(X,V,_) :- 169 | sformat(S,'~w',[X]), 170 | string_to_atom(S,V). 171 | 172 | 173 | :- module_transparent(dataframe_to_csv/2). 174 | :- module_transparent(dataframe_to_csv/3). 175 | dataframe_to_csv(Name, Opts) :- 176 | Stream = current_output, 177 | dataframe_to_csv(Name, Stream, Opts). 178 | dataframe_to_csv(Name, Stream, Opts) :- 179 | dataframe_header(Name, Header, Opts), 180 | option(separator(Sep), Opts, 0'\t), 181 | HTerm =.. [row|Header], 182 | csv_write_stream(Stream, [HTerm], [separator(Sep)]), 183 | dataframe_specs_opts(Name,_Specs,SpecOpts), 184 | ( member(sort(_),SpecOpts) 185 | -> findall(Row,dataframe_row(Name, Row, Opts, SpecOpts),Rows), 186 | % TODO sort by key 187 | sort(Rows,RowsSorted), 188 | forall((member(Row,RowsSorted), RTerm =.. [row|Row]), 189 | csv_write_stream(Stream, [RTerm], [separator(Sep)])) 190 | ; forall((dataframe_row(Name, Row, Opts, SpecOpts), RTerm =.. [row|Row]), 191 | csv_write_stream(Stream, [RTerm], [separator(Sep)]))), 192 | !. 193 | dataframe_to_csv(Name, Stream, Opts) :- 194 | throw(exception(no_dataframe(Name, Stream, Opts))). 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | -------------------------------------------------------------------------------- /prolog/sparqlprog/endpoints.pl: -------------------------------------------------------------------------------- 1 | /** sparql endpoint registry 2 | 3 | This module is provider for convenience. It provides default names for standard endpoint URLs, 4 | with a bias towards life-science endpoints. 5 | 6 | */ 7 | 8 | :- module(endpoints, 9 | []). 10 | :- use_module(library(sparqlprog)). 11 | 12 | %:- rdf_register_prefix(dbont,'http://dbpedia.org/ontology/'). 13 | :- sparql_endpoint( dbp, 'http://dbpedia.org/sparql/'). 14 | :- sparql_endpoint( wd, 'http://query.wikidata.org/sparql'). 15 | :- sparql_endpoint( go, 'http://rdf.geneontology.org/sparql'). 16 | :- sparql_endpoint( godev, 'http://rdf-dev.geneontology.org/sparql'). 17 | :- sparql_endpoint( uniprot, 'https://sparql.uniprot.org/sparql'). 18 | :- sparql_endpoint( dbpedia, 'http://dbpedia.org/sparql/'). 19 | :- sparql_endpoint( ontobee, 'http://sparql.hegroup.org/sparql'). 20 | :- sparql_endpoint( nextprot, 'https://api.nextprot.org/sparql'). 21 | :- sparql_endpoint( disgenet, 'http://rdf.disgenet.org/sparql/'). 22 | :- sparql_endpoint( wikipathways, 'http://sparql.wikipathways.org'). 23 | :- sparql_endpoint( biotea, 'http://biotea.linkeddata.es/sparql'). 24 | :- sparql_endpoint( rhea, 'https://sparql.rhea-db.org/sparql'). 25 | :- sparql_endpoint( ubergraph, 'https://stars-app.renci.org/ubergraph/sparql'). 26 | :- sparql_endpoint( local, 'http://127.0.0.1:9999/blazegraph/sparql'). 27 | :- sparql_endpoint( local2, 'http://127.0.0.1:8922/bigdata/sparql'). 28 | 29 | -------------------------------------------------------------------------------- /prolog/sparqlprog/graph_util.pl: -------------------------------------------------------------------------------- 1 | :- module(graph_util, 2 | [ 3 | extract_subgraph/3, 4 | 5 | edges_to_dict/2, 6 | edges_to_dotfile/2, 7 | 8 | edges_to_imagefile/2, 9 | edges_to_imagefile/3 10 | ]). 11 | 12 | :- use_module(library(http/json)). 13 | 14 | :- use_module(library(semweb/rdf11)). 15 | :- use_module(library(semweb/rdfs)). 16 | :- use_module(library(sparqlprog/owl_util)). 17 | :- use_module(library(sparqlprog/emulate_builtins)). 18 | 19 | my_maplist(L1,P,L2) :- 20 | G =.. [P,In,Out], 21 | maplist([In,Out]>>G, L1, L2). 22 | 23 | %! extract_subgraph(+Nodes:list, ?Edges:list, +Opts:list) is det 24 | % 25 | % extracts a subgraph starting from seed nodes in Nodes 26 | % 27 | % Options: 28 | % 29 | % - direction: up (default) or down 30 | % 31 | extract_subgraph(Nodes, Edges, Opts) :- 32 | extract_subgraph(Nodes, Edges, [], Opts). 33 | 34 | extract_subgraph([], [], _, _). 35 | extract_subgraph([N|Nodes], Edges, Visited, Opts) :- 36 | memberchk(N,Visited), 37 | !, 38 | extract_subgraph(Nodes, Edges, Visited, Opts). 39 | extract_subgraph([N|Nodes], Edges, Visited, Opts) :- 40 | extend_node(N,Edges1,Opts), 41 | collect_nodes(Edges1,Nodes1), 42 | append(Nodes1,Nodes,Nodes2), 43 | extract_subgraph(Nodes2, Edges2, [N|Visited], Opts), 44 | append(Edges1, Edges2, Edges). 45 | 46 | extend_node(N,Edges,Opts) :- 47 | memberchk(direction(down),Opts), 48 | !, 49 | findall(edge(S,N,P,[graph=G]), 50 | owl_edge(S,P,N,G), 51 | Edges). 52 | extend_node(N,Edges,Opts) :- 53 | findall(edge(N,O,P,[graph=G]), 54 | owl_edge(N,P,O,G), 55 | Edges). 56 | 57 | 58 | 59 | shorten(URI,Id) :- rdf_global_id(Pre:Local, URI),concat_atom([Pre,Local],:,Id),!. 60 | shorten(X,X). 61 | 62 | 63 | 64 | 65 | 66 | edges_to_dict(Edges, doc{graphs:[G]}) :- 67 | collect_nodes(Edges,Nodes), 68 | my_maplist(Nodes,node_obj,NodeObjs), 69 | my_maplist(Edges,edge_obj,EdgeObjs), 70 | G = nodelist{nodes: NodeObjs, edges: EdgeObjs}. 71 | 72 | edges_to_dotfile(Edges,DF) :- 73 | edges_to_dotfile(Edges,DF,[]). 74 | edges_to_dotfile(Edges,DF,Opts) :- 75 | edges_to_dict(Edges,D), 76 | tmp_file(obograph,GF), 77 | open(GF,write,Stream,[]), 78 | json_write_dict(Stream,D), 79 | close(Stream), 80 | findall(A, 81 | ( member(Opt=Val,Opts), 82 | sformat(A,'~w ~w',[Opt,Val])), 83 | OptsAtoms), 84 | concat_atom(OptsAtoms,OptsAtom), 85 | sformat(Cmd,'og2dot.js ~w -o ~w ~w',[OptsAtom,DF,GF]), 86 | shell(Cmd). 87 | 88 | edges_to_imagefile(Edges,F) :- 89 | edges_to_imagefile(Edges,F, png). 90 | edges_to_imagefile(Edges,F, Fmt) :- 91 | edges_to_dotfile(Edges,F,['-t'=Fmt]). 92 | 93 | 94 | node_obj(N, Obj) :- 95 | findall(A,node_attr(N,A),Attrs), 96 | ( Attrs=[] 97 | -> true 98 | ; true), 99 | shorten(N,Nx), 100 | Obj = node{id:Nx}.put(Attrs). 101 | 102 | :- rdf_meta node_attr(r,-). 103 | node_attr(rdfs:subClassOf, lbl='is_a'). 104 | node_attr(N, lbl=Label) :- 105 | rdf(N, rdfs:label, Label1), 106 | eval_to_atom(str(Label1),Label). 107 | 108 | 109 | 110 | 111 | edge_obj(edge(S,O,P), Dict) :- edge_obj(edge(S,O,P,[]), Dict). 112 | edge_obj(edge(S,O,P,Attrs), Dict) :- 113 | shorten(S,Sx), 114 | shorten(O,Ox), 115 | shorten(P,Px), 116 | Dict1 = edge{sub:Sx, obj:Ox, pred:Px}, 117 | ( Attrs = [] 118 | -> Dict = Dict1 119 | ; Meta=meta{}.put(Attrs), 120 | Dict = Dict1.put([meta=Meta])). 121 | 122 | 123 | edge_node(edge(S,O,P,_),N) :- edge_node(edge(S,O,P),N). 124 | edge_node(edge(X,_,_),X). 125 | edge_node(edge(_,X,_),X). 126 | edge_node(edge(_,_,X),X). 127 | 128 | 129 | collect_nodes(Edges,Nodes) :- 130 | setof(N,Edge^(member(Edge,Edges),edge_node(Edge,N)),Nodes), 131 | !. 132 | collect_nodes(_,[]). 133 | 134 | -------------------------------------------------------------------------------- /prolog/sparqlprog/io_utils.pl: -------------------------------------------------------------------------------- 1 | :- module(io_utils, 2 | [ 3 | write_result/1, 4 | write_result/2 5 | ]). 6 | 7 | :- use_module(library(semweb/rdf11)). 8 | :- use_module(library(sparqlprog/labelutils)). 9 | 10 | is_pass_thru(inject_labels). 11 | 12 | 13 | write_result(Term) :- 14 | write_result(Term,[]). 15 | write_result(Term,Opts) :- 16 | option(format(Fmt),Opts), 17 | Fmt == prolog, 18 | !, 19 | format('~q.~n',[Term]). 20 | 21 | write_result(Term,Opts) :- 22 | opt_if(dynlabel(true),Opts,Opts2), 23 | !, 24 | row_labelify(Term,Term2), 25 | write_result(Term2,Opts2). 26 | write_result(Term,Opts) :- 27 | member(format(Fmt),Opts), 28 | csv_format_separator(Fmt,Sep), 29 | term_saferow(Term,Term2), 30 | debug(row,'ROW: ~q',[Term2]), 31 | csv_write_stream(current_output, [Term2], [separator(Sep)]), 32 | !. 33 | write_result(Term,_Opts) :- 34 | write_canonical(Term), 35 | writeln('.'). 36 | 37 | atomize(T,A) :- 38 | sformat(A,'~w',[T]). 39 | 40 | % translate a prolog term into an object that is suitable to send to csv_write_stream 41 | % - translate literals to atoms 42 | % - flatten lists 43 | % - translate args in a compound term 44 | 45 | term_saferow(T,T3) :- 46 | term_saferow1(T,T2), 47 | % flatten to one leve; 48 | T2 =.. [P|Args], 49 | maplist(atomize,Args,Args2), 50 | T3 =.. [P|Args2]. 51 | 52 | 53 | term_saferow1(T,'?') :- var(T),!. 54 | term_saferow1(T^^_,A) :- string(T),!, atom_string(A,T). 55 | term_saferow1(T@_, A) :- string(T),!, atom_string(A,T). 56 | term_saferow1(T@_, A) :- string(T),!, atom_string(A,T). 57 | term_saferow1(literal(type(_,A)), A) :- !. 58 | term_saferow1(literal(lang(_,A)), A) :- !. 59 | term_saferow1(literal(A), A) :- !. 60 | term_saferow1(L,A) :- is_list(L), !, maplist(term_saferow1,L,L2),maplist(atomize,L2,L3),concat_atom(L3,',',A). 61 | term_saferow1(T,T2) :- 62 | T =.. [P|Args], 63 | Args = [_|_], 64 | !, 65 | maplist(term_saferow1,Args,Args2), 66 | T2 =.. [P|Args2]. 67 | term_saferow1(T,T2) :- 68 | rdf_global_id(Pre:Id,T), 69 | !, 70 | concat_atom([Pre,Id],:,T2). 71 | term_saferow1(T,T). 72 | 73 | 74 | csv_format_separator(csv,0',). 75 | csv_format_separator(tsv,0'\t). 76 | csv_format_separator(psv,0'|). 77 | -------------------------------------------------------------------------------- /prolog/sparqlprog/labelutils.pl: -------------------------------------------------------------------------------- 1 | :- module(labelutils, 2 | [ 3 | label_atom/2, 4 | label_atom/3, 5 | row_labelify/2, 6 | term_labelify/2 7 | ]). 8 | 9 | /** utilities for labeling entities 10 | 11 | Convenience predicate for attaching labels to simple or compound entities. 12 | 13 | 14 | */ 15 | 16 | :- use_module(library(semweb/rdf11)). 17 | :- use_module(library(sparqlprog/emulate_builtins)). 18 | 19 | label_atom(S,A) :- 20 | label_atom(S,A,[]). 21 | 22 | %! label_atom(+Input, ?Output, +Opts:list) is det 23 | % 24 | label_atom(S,A, _Opts) :- 25 | string(S), 26 | !, 27 | atom_string(A,S). 28 | label_atom(X,A, Opts) :- 29 | atom(X), 30 | concat_atom([Pre,Local],':',X), 31 | \+ \+ rdf_current_prefix(Pre,_), 32 | rdf_global_id(Pre:Local,Y), 33 | Y\=X, 34 | member(expand_uris(true), Opts), 35 | !, 36 | label_atom(Y, A, Opts). 37 | label_atom(X,A, Opts) :- 38 | atom(X), 39 | member(expand_uris(true), Opts), 40 | concat_atom([Pre,Local],':',X), 41 | \+ rdf_current_prefix(Pre,_), 42 | concat_atom(['http://purl.obolibrary.org/obo/',Pre,'_',Local],IRI), 43 | \+ \+ rdf(IRI,_,_), 44 | IRI\=X, 45 | !, 46 | label_atom(IRI, A, Opts). 47 | label_atom(X,A, Opts) :- 48 | \+ compound(X), 49 | atom(X), 50 | atom_iri(X,IRI), 51 | get_label_predicate(P,Opts), 52 | rdf(IRI,P,Literal), 53 | !, 54 | ensure_atom(Literal,A). 55 | 56 | get_label_predicate(P, Opts) :- option(label_predicate(P),Opts). 57 | get_label_predicate(rdfs:label, _). 58 | get_label_predicate(skos:prefLabel, _). 59 | 60 | 61 | 62 | atom_iri(X,I) :- 63 | concat_atom([Pre,Local],:,X), 64 | \+ \+ rdf_current_prefix(Pre,_), 65 | rdf_global_id(Pre:Local,I), 66 | !. 67 | atom_iri(X,X). 68 | 69 | %! term_labelify(+Term, ?LabeledTerm) is det 70 | % 71 | % If Term is a compound term, LabeledTerm is the same term but with all 72 | % atomic components A replaced with A-LabelA 73 | % 74 | % where LabelA is the rdfs:label for A 75 | term_labelify(V,V) :- 76 | var(V), 77 | !. 78 | term_labelify([],[]) :- !. 79 | term_labelify([H|T],[H2|T2]) :- 80 | !, 81 | term_labelify(H,H2), 82 | term_labelify(T,T2). 83 | term_labelify(T,T2) :- 84 | T =.. [P|Args], 85 | Args=[_|_], 86 | !, 87 | term_labelify(Args,Args2), 88 | T2 =.. [P|Args2]. 89 | 90 | term_labelify(T,T-A) :- 91 | label_atom(T,A), 92 | !. 93 | term_labelify(T,T). 94 | 95 | %! row_labelify(+Row,?LabeledRow) is det 96 | % 97 | % given a row term Foo(V1,V2,...,Vn) 98 | % add an extra argument for the label for each 99 | row_labelify(X,X2) :- 100 | X = _^^_, 101 | !, 102 | label_atom(X,X2). 103 | row_labelify(X,X2) :- 104 | X = _@_, 105 | !, 106 | label_atom(X,X2). 107 | row_labelify(Row,Row2) :- 108 | Row =.. [P|Args], 109 | rowargs_labelify(Args,Args2), 110 | Row2 =.. [P|Args2]. 111 | 112 | rowargs_labelify([],[]). 113 | rowargs_labelify([H|T],[H2,Label|T2]) :- 114 | ( label_or_list_to_atom(H,Label) 115 | -> true 116 | ; Label=''), 117 | compactify_arg(H,H2), 118 | rowargs_labelify(T,T2). 119 | 120 | compactify_arg(Var,'_') :- 121 | var(Var), 122 | !. 123 | compactify_arg(L,A) :- 124 | is_list(L), 125 | !, 126 | maplist(compactify_arg,L,L2), 127 | concat_atom(L2,'|',A). 128 | compactify_arg(H,H2) :- 129 | compound(H), 130 | !, 131 | term_labelify(H,H2). 132 | %format(atom(H2),'~w',[H]). 133 | compactify_arg(H,H2) :- 134 | rdf_global_id(Curie,H), 135 | Curie\=H, 136 | Curie = Pre:Local, 137 | !, 138 | format(atom(H2),'~w:~w',[Pre,Local]). 139 | compactify_arg(Str^^_,H2) :- 140 | atom_string(H2,Str), 141 | !. 142 | compactify_arg(H,H). 143 | 144 | label_atom_det(L,A) :- label_atom(L,A),!. 145 | label_atom_det(_,''). 146 | 147 | label_or_list_to_atom(X,A) :- 148 | label_atom(X,A), 149 | !. 150 | label_or_list_to_atom(L,A) :- 151 | is_list(L), 152 | !, 153 | maplist(label_atom_det,L,L2), 154 | concat_atom(L2,'|',A). 155 | 156 | -------------------------------------------------------------------------------- /prolog/sparqlprog/nlp_util.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Deprecated - use rdf_matcher now 4 | */ 5 | :- module(nlp_util, 6 | [rdf_nliteral/3, 7 | rdf_nliteral/4, 8 | rdf_nliteral_tr/5, 9 | shares_literal/6, 10 | 11 | entity_match/6, 12 | new_entity_match/6]). 13 | 14 | %:- use_module(library(tabling)). 15 | %:- use_module(library(memo)). 16 | %:- use_module(library(typedef)). 17 | 18 | :- rdf_register_ns(oio, 'http://www.geneontology.org/formats/oboInOwl#'). 19 | 20 | 21 | :- rdf_meta lprop(r). 22 | 23 | % TODO: reuse 24 | lprop(rdfs:label). 25 | lprop(oio:hasExactSynonym). 26 | lprop(oio:hasRelatedSynonym). 27 | lprop(oio:hasNarrowSynonym). 28 | lprop(oio:hasBroadSynonym). 29 | 30 | 31 | %:- table rdf_nliteral_tr/5. 32 | 33 | rdf_nliteral(X,P,V,G) :- rdf(X,P,Lit,G), bind(lcase(Lit), V). 34 | rdf_nliteral(X,P,V) :- rdf_nliteral(X,P,V,_). 35 | 36 | rdf_nliteral_tr(X,P,V,lcase,G) :- lprop(P),rdf(X,P,Lit,G), bind(lcase(Lit), V). 37 | 38 | %:- table shares_literal/6. 39 | shares_literal(A,B,PA,PB,V,F) :- 40 | rdf_nliteral_tr(A,PA,V,F,_),rdf_nliteral_tr(B,PB,V,F,_),A\=B. 41 | 42 | entity_match(A,B,PA,PB,V,F) :- 43 | shares_literal(A,B,PA,PB,V,F), 44 | different_prefix(A,B). 45 | 46 | new_entity_match(A,B,PA,PB,V,F) :- 47 | entity_match(A,B,PA,PB,V,F), 48 | rdf_global_id(PrefixA:_, A), 49 | rdf_global_id(PrefixB:_, B), 50 | \+ alt_equiv(A, PrefixB), 51 | \+ alt_equiv(B, PrefixA). 52 | 53 | alt_equiv(A, PrefixB) :- 54 | rdf_path(A,((owl:equivalentClass)| \(owl:equivalentClass)),B), 55 | rdf_global_id(PrefixB:_, B). 56 | 57 | 58 | different_prefix(A,B) :- 59 | rdf_global_id(PrefixA:_, A), 60 | rdf_global_id(PrefixB:_, B), 61 | PrefixA \= PrefixB. 62 | 63 | -------------------------------------------------------------------------------- /prolog/sparqlprog/obo_util.pl: -------------------------------------------------------------------------------- 1 | :- module(obo_util, 2 | [gen_obo/2, 3 | gen_stanza/3, 4 | gen_header/3, 5 | 6 | entity_def_xrefs/3 7 | ]). 8 | 9 | :- use_module(library(obo_metadata/oio)). 10 | 11 | :- use_module(library(semweb/rdf11)). 12 | :- use_module(library(semweb/rdfs)). 13 | :- use_module(library(sparqlprog/owl_util)). 14 | :- use_module(library(sparqlprog/ontologies/obo)). 15 | :- use_module(library(sparqlprog/emulate_builtins)). 16 | 17 | entity_obotype(E,T) :- entity_obotype(E,T,_). 18 | entity_obotype(E,'Term',G) :- rdf(E,rdf:type,owl:'Class',G), rdf_is_iri(E). 19 | entity_obotype(E,'Typedef',G) :- rdf(E,rdf:type,owl:'ObjectProperty',G). 20 | 21 | ensure_curie_wrap(U,X) :- 22 | ensure_curie(U,X1), 23 | ( atom_concat('http://purl.obolibrary.org/obo/',Frag,X1), 24 | concat_atom([Pre,Post],'_',Frag) 25 | -> concat_atom([Pre,Post],':',X) 26 | ; X=X1). 27 | 28 | 29 | 30 | ensure_id(U,Id) :- 31 | ensure_curie_wrap(U,X), 32 | ( atom_concat(Id,':',X) 33 | -> true 34 | ; Id=X). 35 | 36 | 37 | entity_def_xrefs(E,D,Xs) :- 38 | triple_property_axiom_annotations(E,def:'',D1,oio:hasDbXref,Xs1), 39 | ensure_atom(D1,D), 40 | ensure_atoms(Xs1,Xs). 41 | 42 | 43 | syntype_uri_to_id(V1,V) :- 44 | concat_atom([_,V],'#',V1), 45 | !. 46 | syntype_uri_to_id(V,V). 47 | 48 | entity_xref_xrefs(E,X,QVs) :- 49 | triple_axiom_annotations(E,oio:hasDbXref,X1,QVs), 50 | ensure_atom(X1,X). 51 | 52 | 53 | entity_xref_prefix_srcont(C,X,P,S) :- 54 | entity_xref_prefix(C,X,P), 55 | entity_xref_src(C,X,SC), 56 | curie_prefix(SC,S). 57 | 58 | entity_synonym_scope_type_xrefs(E,V,Scope,Type,Xrefs) :- 59 | synprop_scope(P,Scope), 60 | triple_axiom_annotations(E,P,V1,Anns), 61 | ensure_atom(V1,V), 62 | convlist(['http://www.geneontology.org/formats/oboInOwl#hasDbXref'-Y1, 63 | Y]>>ensure_atom(Y1,Y), 64 | Anns,Xrefs), 65 | ( member('http://www.geneontology.org/formats/oboInOwl#hasSynonymType'-Type1,Anns) 66 | -> syntype_uri_to_id(Type1,Type) 67 | ; Type=''). 68 | 69 | subset_uri_to_id(V1,V) :- 70 | % envo has some subset tags as strings 71 | rdf_is_literal(V1), 72 | !, 73 | ensure_atom(V1,V). 74 | subset_uri_to_id(V1,V) :- 75 | concat_atom([_,V],'#',V1), 76 | !. 77 | subset_uri_to_id(V,V). 78 | 79 | 80 | entity_subset_id(E,V) :- 81 | rdf(E,oio:inSubset,V1), 82 | subset_uri_to_id(V1,V). 83 | 84 | 85 | 86 | %! curie_prefix(Literal:str, Pre:str) 87 | %curie_prefix(Literal,Pre) :- 88 | % str_before(Literal,":",Pre). 89 | 90 | entity_nameatom(E,N) :- 91 | label(E,N1), 92 | ensure_atom(N1,N). 93 | 94 | 95 | gen_obo(S,Opts) :- 96 | gen_obo(S,_,Opts). 97 | 98 | gen_obo(S,G,Opts) :- 99 | forall(gen_header(S,G,Opts),true), 100 | format(S,'~n',[]), 101 | setof(E,T^entity_obotype(E,T,G),Es), 102 | forall((member(E,Es),\+is_dangling(E)), 103 | gen_stanza(S,E,G,Opts)). 104 | 105 | 106 | 107 | gen_header(S,G,Opts) :- 108 | forall(gen_header1(S,G,Opts),true). 109 | 110 | gen_header1(S,G,_) :- 111 | rdf_graph(G), 112 | ensure_id(G,Id), 113 | format(S,'ontology: ~w~n',Id). 114 | gen_header1(S,_G,_) :- 115 | rdf(V1,rdfs:subPropertyOf,oio:'SubsetProperty'), 116 | subset_uri_to_id(V1,V), 117 | format(S,'subsetdef: ~w "~w"~n',[V,V]). 118 | 119 | gen_stanza(S,E,Opts) :- 120 | gen_stanza(S,E,_G,Opts). 121 | 122 | gen_stanza(S,E,G,Opts) :- 123 | entity_obotype(E,T), 124 | ensure_id(E,Id), 125 | format(S,'[~w]~n',[T]), 126 | format(S,'id: ~w~n',[Id]), 127 | forall(gen_tag(S,E,G,Opts),true), 128 | format(S,'~n',[]), 129 | !. 130 | gen_stanza(_S,E,G,_Opts) :- 131 | format(user_error,'Cannot write: ~w ~w~n',[E,G]). 132 | 133 | 134 | 135 | gen_tag(S,E,_,_) :- 136 | entity_nameatom(E,N), 137 | format(S,'name: ~w~n',[N]). 138 | gen_tag(S,E,_,_) :- 139 | entity_def_xrefs(E,N,Xrefs), 140 | escq(N,N1), 141 | serialize_xrefs(Xrefs,X), 142 | format(S,'def: "~w" ~w~n',[N1,X]). 143 | 144 | gen_tag(S,E,_,_) :- 145 | entity_subset_id(E,X), 146 | format(S,'subset: ~w~n',[X]). 147 | 148 | gen_tag(S,E,_,_) :- 149 | entity_synonym_scope_type_xrefs(E,V1,Scope,Type,Xrefs), 150 | serialize_xrefs(Xrefs,X), 151 | escq(V1,V), 152 | format(S,'synonym: "~w" ~w ~w ~w~n',[V,Scope,Type,X]). 153 | 154 | gen_tag(S,E,_,_) :- 155 | entity_xref_xrefs(E,X,_PVs), 156 | format(S,'xref: ~w~n',[X]). 157 | 158 | gen_tag(S,E,_,_) :- 159 | is_a(E,X), 160 | tv(S,is_a,[X]). 161 | 162 | gen_tag(S,E,_,_) :- 163 | relationship(E,R,O), 164 | tv(S,relationship,[R,O]). 165 | 166 | gen_tag(S,E,_,_) :- 167 | class_genus(E,G), 168 | \+ \+ class_differentia(E,_,_), 169 | tv(S,intersection_of,[G]), 170 | forall(class_differentia(E,P,Y), 171 | tv(S,intersection_of,[P,Y])). 172 | 173 | gen_tag(S,E,_,_) :- 174 | rdf(E,rdf:type,owl:'TransitiveProperty'), 175 | tv(S,is_transitive,[true]). 176 | 177 | tv(S,T,Vs) :- 178 | format(S,'~w:',[T]), 179 | forall((member(V,Vs),ensure_id(V,Id)), 180 | format(S,' ~w',[Id])), 181 | findall(N,(member(V,Vs),entity_nameatom(V,N)), 182 | Ns), 183 | ( Ns=[] 184 | -> true 185 | ; format(S,' !',[]), 186 | forall(member(N,Ns), 187 | format(S,' ~w',[N]))), 188 | nl(S). 189 | 190 | 191 | serialize_xrefs(Xs,A) :- 192 | concat_atom(Xs,', ',A1), 193 | concat_atom(['[',A1,']'],A). 194 | 195 | 196 | 197 | escq(A,C) :- 198 | concat_atom(Xs,'"',A), 199 | concat_atom(Xs,'\\"',B), 200 | concat_atom(Xs2,'\n',B), 201 | concat_atom(Xs2,' ',C). 202 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/allie_sflf.pl: -------------------------------------------------------------------------------- 1 | :- module(allie_sflf, 2 | [ 3 | cluster_member/2, 4 | long_form_representation/2, 5 | long_form/2, 6 | short_form/2, 7 | long_form_list/4 8 | ]). 9 | 10 | :- sparql_endpoint(allie, 'https://data.allie.dbcls.jp/sparql'). 11 | 12 | :- rdf_register_prefix(allie, 'http://purl.org/allie/ontology/201108#'). 13 | 14 | cluster_member(A, B) :- rdf_path(A, ( (allie:contains) / (allie:hasMemberOf) ), B). 15 | long_form_representation(A, B) :- rdf(A, allie:hasLongFormRepresentationOf, B). 16 | long_form(A, B) :- rdf(A, allie:hasLongFormOf, B). 17 | short_form(A, B) :- rdf(A, allie:hasShortFormOf, B). 18 | 19 | long_form_list(S, L1, L2, LANG) :- 20 | cluster_member(A, B), 21 | short_form(B, F), 22 | label_of(S, F, "en"), 23 | optional( 24 | (long_form_representation(A, D), 25 | rdf(D,rdfs:label,L1),lang(L1)=LANG)), 26 | optional( 27 | (long_form(B, E), 28 | rdf(E,rdfs:label,L2),lang(L2)=LANG)). 29 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/bgee.pl: -------------------------------------------------------------------------------- 1 | :- module(bgee, 2 | [ 3 | expressed_in/2, 4 | expressed_in/4 5 | ]). 6 | 7 | :- use_module(library(sparqlprog/ontologies/faldo)). 8 | :- reexport(library(sparqlprog/ontologies/ebi), [in_taxon/2]). 9 | 10 | :- sparql_endpoint( bgee, 'http://biosoda.expasy.org:8080/rdf4j-server/repositories/bgeelight'). 11 | 12 | :- rdf_register_prefix(up,'http://purl.uniprot.org/core/'). 13 | :- rdf_register_prefix(genex, 'http://purl.org/genex#'). 14 | :- rdf_register_prefix(orth, 'http://purl.org/net/orth#'). 15 | :- rdf_register_prefix(obo, 'http://purl.obolibrary.org/obo/'). 16 | 17 | expressed_in(Seq,Anat) :- 18 | expressed_in(Seq,Anat,_,_). 19 | expressed_in(Seq,Anat,Expr,Cond) :- 20 | rdf(Expr,genex:hasSequenceUnit,Seq), 21 | rdf(Expr,genex:hasExpressionCondition,Cond), 22 | rdf(Cond,genex:hasAnatomicalEntity,Anat). 23 | 24 | % IMPORTED FROM EBI 25 | % note Taxon has purls such as http://purl.uniprot.org/taxonomy/10116 26 | %in_taxon(Cond,Taxon) :- 27 | % rdf(Cond,obo:'RO_0002162',Taxon). 28 | 29 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/biofed.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | federated 4 | 5 | */ 6 | 7 | :- module(biofed, 8 | [ 9 | ]). 10 | 11 | :- use_module(library(sparqlprog)). 12 | :- use_module(library(semweb/rdf11)). 13 | :- use_module(monarch). 14 | :- use_module(wikidata). 15 | 16 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/biogateway.pl: -------------------------------------------------------------------------------- 1 | :- module(biogateway, 2 | [ 3 | 4 | ]). 5 | 6 | :- use_module(library(sparqlprog/ontologies/faldo)). 7 | 8 | :- sparql_endpoint( biogateway, 'https://biogw-db.nt.ntnu.no:4333/sparql' ). 9 | 10 | :- rdf_register_prefix(involved_in_regulation_of, 'http://purl.obolibrary.org/obo/RO_0002428'). 11 | :- rdf_register_prefix(involved_in_of, 'http://purl.obolibrary.org/obo/RO_0002311'). 12 | :- rdf_register_prefix(molecularly_interacts_with, 'http://purl.obolibrary.org/obo/RO_0002436'). 13 | :- rdf_register_prefix(encodes, 'http://semanticscience.org/resource/SIO_010078'). 14 | 15 | 16 | :- rdf_register_prefix(id, 'http://identifiers.org/'). 17 | :- rdf_register_prefix(sio, 'http://semanticscience.org/resource/SIO_'). 18 | :- rdf_register_prefix(blmod, 'http://w3id.org/biolink/vocab/'). 19 | :- rdf_register_prefix(d2s, 'https://w3id.org/data2services/data/'). 20 | 21 | 22 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/biotea.pl: -------------------------------------------------------------------------------- 1 | :- module(biotea, 2 | [ 3 | src_term/2, 4 | ann_src_term/5, 5 | coannotation/6 6 | ]). 7 | 8 | :- rdf_register_prefix(ncitevs, 'http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#'). 9 | :- rdf_register_prefix(snomed, 'http://purl.bioontology.org/ontology/SNOMEDCT/'). 10 | 11 | :- rdf_register_prefix(biotea,'https://biotea.github.io/biotea-ontololgy#'). 12 | :- rdf_register_prefix(oa,'http://www.w3.org/ns/oa#'). 13 | :- rdf_register_prefix(pmc,'http://linkingdata.io/pmcdoc/pmc/'). 14 | 15 | ann_src_term(A, Src, Para, TermUri, Label) :- 16 | rdf(A, oa:hasTarget, Para), 17 | rdf(Para, oa:hasSource, Src), 18 | rdf(A, oa:hasBody, Text), 19 | rdf(Text, rdf:value, Label), 20 | rdf(A, oa:hasBody, TermUri), 21 | \+ rdf(TermUri, rdf:type, oa:'TextualBody'). 22 | 23 | 24 | 25 | src_term(Src, TermUri) :- 26 | ann_src_term(_, Src, _, TermUri). 27 | 28 | % e.g. `coannotation(pmc:'3875424',pmc:'3933681',_,_,_,_)` 29 | coannotation(P1,P2,TermUri,Label,F1,F2) :- 30 | ann_src_term(A1,P1,_,TermUri,Label), 31 | ann_src_term(A2,P2,_,TermUri,_), 32 | rdf(A1,biotea:tf,F1), 33 | rdf(A2,biotea:tf,F2). 34 | 35 | 36 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/chembl.pl: -------------------------------------------------------------------------------- 1 | /** chembl 2 | 3 | See https://www.ebi.ac.uk/rdf/documentation/chembl/ 4 | */ 5 | :- module(chembl, 6 | [ 7 | molecule/1, 8 | target/1, 9 | source/1, 10 | activity/1, 11 | uniprotref/1, 12 | 13 | class_level/2, 14 | has_target_descendant/2, 15 | has_molecule/2, 16 | has_assay/2, 17 | has_target_component/2, 18 | target_cmpt_xref/2 19 | 20 | ]). 21 | 22 | :- use_module(ebi). 23 | 24 | :- rdf_register_prefix(cco, 'http://rdf.ebi.ac.uk/terms/chembl#'). 25 | :- rdf_register_prefix(chembl_molecule, 'http://rdf.ebi.ac.uk/resource/chembl/molecule/'). 26 | :- rdf_register_prefix(chembl_source, 'http://rdf.ebi.ac.uk/resource/chembl/source/'). 27 | :- rdf_register_prefix(chembl_target, 'http://rdf.ebi.ac.uk/resource/chembl/target/'). 28 | :- rdf_register_prefix(chembl_protclass, 'http://rdf.ebi.ac.uk/resource/chembl/protclass/'). 29 | :- rdf_register_prefix(chembl_assay, 'http://rdf.ebi.ac.uk/resource/chembl/assay/'). 30 | 31 | molecule(X) :- rdfs_individual_of(X,cco:'Substance'). 32 | target(X) :- rdfs_individual_of(X,cco:'Target'). 33 | source(X) :- rdfs_individual_of(X,cco:'Source'). 34 | activity(X) :- rdfs_individual_of(X,cco:'Activity'). 35 | 36 | uniprotref(X) :- rdf(X,rdf:type,cco:'UniprotRef'). 37 | 38 | class_level(P,L) :- rdf(P,cco:classLevel,L). 39 | has_target_descendant(C,P) :- rdf(C,cco:hasTargetDescendant,P). 40 | has_molecule(A,M) :- rdf(A,cco:hasMolecule,M). 41 | has_assay(A,T) :- rdf(A,cco:hasAssay,T). 42 | has_target_component(T,C) :- rdf(T,cco:hasTargetComponent,C). 43 | target_cmpt_xref(C,X) :- rdf(C,cco:targetCmptXref,X). 44 | 45 | has_child_molecule(P,C) :- rdf(P,cco:hasChildMolecule,C). 46 | has_parent_molecule(C,P) :- rdf(C,cco:hasParentMolecule,P). 47 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/dbpedia.pl: -------------------------------------------------------------------------------- 1 | /* wrappers for dbpedia 2 | 3 | Note that this only wraps a small subset of dbpedia, for demo purposes. 4 | 5 | For complete ontology use rdfs2pl 6 | 7 | 8 | 9 | */ 10 | 11 | :- module(dbpedia, 12 | [dbpedia_class/1, 13 | dbpedia_rootclass/1, 14 | person/1, 15 | musical_artist/1, 16 | band/1, 17 | photographer/1, 18 | disease/1, 19 | food/1, 20 | 21 | related_to/2, 22 | has_child/2, 23 | child_of/2, 24 | descendant_of/2, 25 | has_director/2, 26 | directed/2, 27 | has_genre/2, 28 | band_member/2, 29 | has_name/2, 30 | 31 | city/1, 32 | country/1, 33 | sport_competition_result/1]). 34 | 35 | :- use_module(library(sparqlprog)). 36 | :- use_module(library(semweb/rdf11)). 37 | 38 | :- sparql_endpoint( dbp, 'http://dbpedia.org/sparql/'). 39 | 40 | :- rdf_register_prefix(foaf,'http://xmlns.com/foaf/0.1/'). 41 | :- rdf_register_prefix(dbont,'http://dbpedia.org/ontology/'). 42 | :- rdf_register_prefix(dbr,'http://dbpedia.org/resource/'). 43 | 44 | 45 | dbpedia_class(C) :- rdf(C,rdf:type,owl:'Class'). 46 | 47 | dbpedia_rootclass(C) :- dbpedia_class(C),rdf(C,rdfs:subClassOf,owl:'Thing'). 48 | 49 | 50 | person(Person) :- rdf(Person,rdf:type,foaf:'Person'). 51 | 52 | %! has_name(?S, ?L) is nondet. 53 | % 54 | % binds foaf:Name 55 | % 56 | has_name(S,L) :- rdf(S,foaf:'Name',L). 57 | 58 | %! has_genre(?S, ?G) is nondet. 59 | % 60 | % binds dbont:genre 61 | % 62 | has_genre(S,G) :- rdf(S,dbont:genre,G). 63 | 64 | 65 | %! has_director(?S, ?O) is nondet. 66 | % 67 | % 68 | % binds dbont:director 69 | % 70 | has_director(S,O) :- rdf(S,dbont:director,O). 71 | 72 | %! directed(?S, ?O) is nondet. 73 | % 74 | % 75 | % binds dbont:directed 76 | % 77 | directed(S,O) :- rdf(O,dbont:director,S). 78 | 79 | %! has_child(?S, ?O) is nondet. 80 | % 81 | % 82 | % 83 | has_child(S,O) :- rdf(S,dbont:child,O). 84 | 85 | %! child_of(?S, ?O) is nondet. 86 | % 87 | % 88 | % 89 | child_of(S,O) :- rdf(O,dbont:child,S). 90 | 91 | %! descendant_of(?S, ?O) is nondet. 92 | % 93 | % true if S can be connected to O via one or more has_child/2 relationships 94 | % 95 | descendant_of(S,O) :- rdf_path(O,oneOrMore(dbont:child),S). 96 | 97 | 98 | %! band_member(?S, ?O) is nondet. 99 | % 100 | % 101 | % 102 | band_member(S,O) :- rdf(S,dbont:bandMember,O). 103 | 104 | related_to(S,O) :- related_to(S,O,_). 105 | related_to(S,O,has_child) :- has_child(S,O). 106 | related_to(S,O,has_parent) :- has_child(O,S). 107 | 108 | grandchild_of(S,O) :- child_of(S,Z),child_of(Z,O). 109 | 110 | 111 | food(X) :- rdf(X,rdf:type,dbont:'Food'). 112 | 113 | 114 | 115 | %! band(?X) is nondet. 116 | % 117 | % 118 | % 119 | band(X) :- rdf(X,rdf:type,dbont:'Band'). 120 | 121 | %! photographer(?X) is nondet. 122 | % 123 | % 124 | % 125 | photographer(X) :- rdf(X,rdf:type,dbont:'Photographer'). 126 | 127 | %! musical_artist(?X) is nondet. 128 | % 129 | % 130 | % 131 | musical_artist(X) :- rdf(X,rdf:type,dbont:'MusicalArtist'). 132 | 133 | 134 | %! disease(?X) is nondet. 135 | % 136 | % 137 | % 138 | disease(X) :- rdf(X,rdf:type,dbont:'Disease'). 139 | 140 | city(X) :- rdf(X,rdf:type,dbont:'City'). 141 | country(X) :- rdf(X,rdf:type,dbont:'Country'). 142 | sport_competition_result(X) :- rdf(X,rdf:type,dbont:'SportCompetitionResult'). 143 | 144 | 145 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/dbpedia/dbpedia_matcher.pl: -------------------------------------------------------------------------------- 1 | :- module(dbpedia_matcher, 2 | [band/1, 3 | has_shared_band_member/3, 4 | has_shared_genre/3, 5 | similarity_by_genre/3, 6 | jaccard/3, 7 | pair_genre_sum_ic/3]). 8 | 9 | /** perform entity matching/similarity using dbpedia 10 | 11 | This module is intended primarily as an example of how to mix local logic and remote sparql logic. 12 | 13 | * similarity_by_genre/3 - compares two entities using jaccard similarity of genres 14 | * pair_genre_sum_ic/3 - alternate metric that uses information content of genres in common 15 | 16 | */ 17 | 18 | 19 | % now builtin 20 | %:- use_module(library(tabling)). 21 | :- use_module(library(sparqlprog/emulate_builtins)). 22 | :- use_module(library(sparqlprog/ontologies/dbpedia)). 23 | 24 | 25 | %! has_shared_band_member(?B1, ?B2, ?SharedMember) is nondet 26 | % 27 | % links bands by members in common. E.g. Ronnie James Dio in both Rainbow and Black Sabbath 28 | % 29 | % true if SharedMember is a member of both B1 and B2 (and B1 and B2 are distinct) 30 | % 31 | has_shared_band_member(B1,B2,A) :- 32 | rdf(A,dbont:associatedBand,B1), 33 | rdf(A,dbont:associatedBand,B2), 34 | B1\=B2. 35 | 36 | %! has_shared_genre(?B1, ?B2, ?SharedGenre) is nondet 37 | % 38 | % links bands by shared genre common. 39 | % 40 | % true if SharedGenre is a genre of both B1 and B2 (and B1 and B2 are distinct) 41 | % 42 | has_shared_genre(B1,B2,A) :- 43 | rdf(B1,dbont:genre,A), 44 | rdf(B2,dbont:genre,A), 45 | B1\=B2. 46 | 47 | genre_pair(G1,G2,A) :- 48 | rdf(A,dbont:genre,G1), 49 | rdf(A,dbont:genre,G2), 50 | G1\=G2. 51 | 52 | 53 | 54 | %! similarity_by_genre(?BandA, ?BandB, ?SumIC) 55 | % 56 | % calculates the jaccard similarity between two entities based on genres in common 57 | % 58 | % the entities should be of similar types (e.g. two bands, or two books) 59 | % 60 | % == 61 | % | genres(A) /\ genres(B) | / | genres(A) \/ genres(B) | 62 | % == 63 | % 64 | % if no genres are in common, then this should equal 0 65 | % if all genres are in common, then this should equal 1 66 | % 67 | % note this does not take into account how *meaningful* it is for a genre to be shared; 68 | % e.g. sharing the common genre 'pop' counts as much as a rarer genre like 'psytrance'. 69 | % see further on for IC-based metrics. 70 | % 71 | similarity_by_genre(A,B,Sim) :- 72 | get_all_genres(A,SA), 73 | get_all_genres(B,SB), 74 | jaccard(SA,SB,Sim). 75 | 76 | jaccard(SA,SB,Sim) :- 77 | ord_intersection(SA,SB,I), 78 | ord_union(SA,SB,U), 79 | length(I,NI), 80 | length(U,NU), 81 | Sim is NI/NU. 82 | 83 | get_all_genres(Entity,L) :- 84 | service_query_all(dbpedia,G,rdf(Entity,dbont:genre,G),L). 85 | 86 | 87 | 88 | :- table get_num_bands/1. 89 | %! get_num_bands(?Count) is det 90 | % 91 | % unifies Count with the total number of bands in the database 92 | % 93 | % note this is tabled (cached) so that repeated calls do not invoke new SPARQL queries 94 | % 95 | get_num_bands(Count) :- 96 | ??(dbpedia,num_bands(Count)). 97 | num_bands(Count) :- 98 | aggregate(count(distinct(B)),band(B),Count). 99 | 100 | 101 | 102 | %! get_genre_num_bands(?Genre,?Count) is nondet. 103 | %! get_genre_num_bands(+Genre,?Count) is det. 104 | % 105 | % unifies Count with the total number of bands that are categorized as Genre 106 | % 107 | % 108 | %%%%:- table get_genre_num_bands/2. 109 | get_genre_num_bands(G,Count) :- 110 | ??(dbpedia,genre_num_bands(G,Count)). 111 | 112 | genre_num_bands(G,Count) :- 113 | aggregate_group(count(distinct(B)),[G],(rdf(B,dbont:genre,G),band(B)),Count). 114 | 115 | 116 | 117 | %! pair_genre_sum_ic(?BandA, ?BandB, ?SumIC) 118 | % 119 | % for a pair of bands, SumIC is the sum of the ICs of the genres shared in common. 120 | % 121 | % 122 | % Example: =pair_genre_ic(dbr:'Metallica', dbr:'Megadeth', IC)= 123 | pair_genre_sum_ic(A,B,SumIC) :- 124 | get_all_genres(A,SA), 125 | ??(dbpedia,(band(B),has_shared_genre(A,B,_))), 126 | get_all_genres(B,SB), 127 | ord_intersection(SA,SB,I), 128 | debug(dbpedia,'~w vs ~w :: INTERSECTION(~w + ~w) = ~w',[A,B,SA,SB,I]), 129 | aggregate(sum(IC),G^(member(G,I),genre_ic(G,IC)),SumIC). 130 | 131 | %! genre_ic(?Genre, ?InformationContent:float) is nondet. 132 | % 133 | % gets the IC of a particular genre. The higher the IC, the rarer and more 'surprising' or information-rich it is. 134 | % 135 | % for example, many bands are pop, so this would have a low IC. Progressive sludge metal is relatively rare and would have a high IC 136 | % 137 | % == 138 | % InformationContent = -log2( Pr(Genre) ) 139 | % == 140 | % 141 | genre_ic(G,IC) :- 142 | get_genre_num_bands(G,Count), 143 | debug(dbpedia,'|bands| in ~w = ~w',[G,Count]), 144 | get_num_bands(Total), 145 | debug(dbpedia,'Total bands = ~w',[Total]), 146 | seval(-log(Count/Total)/log(2), IC). 147 | 148 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/disgenet.pl: -------------------------------------------------------------------------------- 1 | /** wrapper for disgenet endpoint 2 | 3 | See http://www.disgenet.org/rdf 4 | 5 | */ 6 | 7 | :- module(disgenet, 8 | [ 9 | gene/1, 10 | disease/1, 11 | 12 | refers_to/2, 13 | has_evidence/2, 14 | 15 | association/1, 16 | gene_disease_association/1, 17 | gene_disease_association/3, 18 | gene_disease_association/4, 19 | 20 | disease_pair_by_shared_gene/3 21 | ]). 22 | 23 | :- use_module(library(sparqlprog)). 24 | :- use_module(library(semweb/rdf11)). 25 | 26 | :- use_module(library(sparqlprog/owl_types)). 27 | :- use_module(library(sparqlprog/ontologies/sio),[has_evidence/2, has_source/2, refers_to/2, has_measurement_value/2]). 28 | :- use_module(library(typedef)). 29 | 30 | :- type disgenet_protein ---> atomic_iri. 31 | :- type disgenet_annotation ---> atomic_iri. 32 | :- type disgenet_gene ---> atomic_iri. 33 | :- type disgenet_publication ---> atomic_iri. 34 | 35 | :- sparql_endpoint( disgenet, 'http://rdf.disgenet.org/sparql/'). 36 | 37 | :- rdf_register_prefix(disgenet,'http://rdf.disgenet.org/'). 38 | :- rdf_register_prefix(disgenet_gene_disease_association,'http://rdf.disgenet.org/resource/gene_disease_association/'). 39 | :- rdf_register_prefix(sio, 'http://semanticscience.org/resource/SIO_'). 40 | :- rdf_register_prefix(ncitevs, 'http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#'). 41 | :- rdf_register_prefix(umls, 'http://linkedlifedata.com/resource/umls/id/'). 42 | :- rdf_register_prefix(ncbigene, 'http://identifiers.org/ncbigene/'). 43 | 44 | 45 | 46 | %! disease(?D : disgenet_disease) is nondet. 47 | disease(D) :- rdf(D,rdf:type,ncitevs:'C7057'). 48 | 49 | %! gene(?G : disgenet_gene) is nondet. 50 | gene(G) :- rdf(G,rdf:type,ncitevs:'C16612'). 51 | 52 | %! association(?A : disgenet_annotation) is nondet. 53 | association(A) :- rdfs_individual_of(A,sio:'000897'). 54 | 55 | %! gene_disease_association(?A : disgenet_annotation) is nondet. 56 | %! gene_disease_association(?A : disgenet_annotation, ?G : disgenet_gene, ?D : disgenet_disease) is nondet. 57 | %! gene_disease_association(?A : disgenet_annotation, ?G : disgenet_gene, ?D : disgenet_disease, ?P) is nondet. 58 | % 59 | % gene G is associated with disease D via evidence A 60 | gene_disease_association(A) :- rdfs_individual_of(A,sio:'001121'). 61 | gene_disease_association(A,G,D) :- gene_disease_association(A),refers_to(A,G),gene(G),refers_to(A,D),disease(D). 62 | gene_disease_association(A,G,D,P) :- gene_disease_association(A),refers_to(A,G),gene(G),refers_to(A,D),disease(D),has_evidence(A,P). 63 | 64 | %! disease_pair_by_shared_gene(?D1 : disgenet_disease, ?D2 : disgenet_disease, ?G : disgenet_gene) is nondet. 65 | % 66 | % both D1 and D2 are associated with the same gene G 67 | disease_pair_by_shared_gene(D1,D2,G) :- gene_disease_association(_,G,D1),gene_disease_association(_,G,D2). 68 | 69 | % these should go to a generic SIO module 70 | %has_measurement_value(A,V) :- rdf(A,sio:'000772',V). 71 | %has_source(A,V) :- rdf(A,sio:'000253',V). 72 | %has_value(A,V) :- rdf(A,sio:'000300',V). 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/ebi.pl: -------------------------------------------------------------------------------- 1 | :- module(ebi, 2 | [ 3 | 4 | sample/1, 5 | sample_attribute/2, 6 | attribute_property_value/3, 7 | attribute_property_value/4, 8 | attribute_property_value/5, 9 | sample_biocharacteristic/2, 10 | sample_property_value/3, 11 | sample_property_value/4, 12 | sample_depth/2, 13 | sample_environment_feature/2, 14 | 15 | identifier/2, 16 | description/2, 17 | direct_mapping/2, 18 | see_also/2, 19 | alt_label/2, 20 | exon/1, 21 | protein_coding_gene/1, 22 | paralogous_to/2, 23 | orthologous_to/2, 24 | homologous_to/2, 25 | in_taxon/2, 26 | transcribed_from/2, 27 | translates_to/2, 28 | has_part/2, 29 | 30 | feature_in_range/4, 31 | mouse_ortho_by_range/5, 32 | has_mouse_ortholog/2 33 | ]). 34 | 35 | :- use_module(library(sparqlprog/ontologies/faldo)). 36 | :- use_module(library(sparqlprog/ontologies/sequence_feature), []). 37 | 38 | :- sparql_endpoint( ebi, 'https://www.ebi.ac.uk/rdf/services/sparql'). 39 | %:- sparql_endpoint( ebi, ['https://www.ebi.ac.uk/rdf/services/sparql', 'https://integbio.jp/rdf/mirror/ebi/sparql']). 40 | 41 | % samples 42 | :- rdf_register_prefix(biosd_terms, 'http://rdf.ebi.ac.uk/terms/biosd/'). 43 | :- rdf_register_prefix(biosd, 'http://rdf.ebi.ac.uk/resource/biosamples/sample/'). 44 | 45 | 46 | :- rdf_register_prefix(ebi_atlas, 'http://rdf.ebi.ac.uk/terms/atlas/'). 47 | 48 | :- rdf_register_prefix(skos, 'http://www.w3.org/2004/02/skos/core#'). 49 | :- rdf_register_prefix(ensembl,'http://rdf.ebi.ac.uk/resource/ensembl/'). 50 | :- rdf_register_prefix(ensembl_protein,'http://rdf.ebi.ac.uk/resource/ensembl.protein/'). 51 | :- rdf_register_prefix(dcterms,'http://purl.org/dc/terms/'). 52 | :- rdf_register_prefix(so, 'http://purl.obolibrary.org/obo/SO_'). 53 | :- rdf_register_prefix(ro, 'http://purl.obolibrary.org/obo/RO_'). 54 | :- rdf_register_prefix(sio, 'http://semanticscience.org/resource/SIO_'). 55 | :- rdf_register_prefix(taxon, 'http://identifiers.org/taxonomy/'). 56 | :- rdf_register_prefix(human, 'http://identifiers.org/taxonomy/9606'). 57 | 58 | % Note: this needs updating every ensemnl release... 59 | :- rdf_register_prefix(grch38, 'http://rdf.ebi.ac.uk/resource/ensembl/92/homo_sapiens/GRCh38/'). 60 | :- rdf_register_prefix(grcm38, 'http://rdf.ebi.ac.uk/resource/ensembl/92/mus_musculus/GRCm38/'). 61 | 62 | 63 | sample(A) :- rdf(A,rdf:type,biosd_terms:'Sample'). 64 | 65 | %! sample_biocharacteristic(?S : ebi_biosample, ?A : ebi_attribute_node) is nondet. 66 | sample_biocharacteristic(S,A) :- 67 | rdf(S,biosd_terms:'has-bio-characteristic',A). 68 | 69 | 70 | 71 | %! sample_attribute(?S : ebi_biosample, ?A : ebi_attribute_node) is nondet. 72 | sample_attribute(S,A) :- 73 | rdf(S,biosd_terms:'has-sample-attribute',A). 74 | 75 | %! attribute_property_value(?A : ebi_attribute_node, ?P : ebi_biosample_property, ?V : ebi_biosample_value) is nondet. 76 | % 77 | attribute_property_value(A, P, V) :- 78 | rdf(A,ebi_atlas:propertyType,P), 79 | rdf(A,ebi_atlas:propertyValue,V). 80 | attribute_property_value(A, P, V, T) :- 81 | rdf(A,rdf:type,T), 82 | rdf(A,ebi_atlas:propertyType,P), 83 | rdf(A,ebi_atlas:propertyValue,V). 84 | attribute_property_value(A, P, V, T, G) :- 85 | rdf(A,rdf:type,T, G), 86 | rdf(A,ebi_atlas:propertyType,P), 87 | rdf(A,ebi_atlas:propertyValue,V). 88 | 89 | %! sample_property_value(?S : ebi_biosample, ?P : ebi_biosample_property, ?V : ebi_biosample_value) is nondet. 90 | % 91 | sample_property_value(S, P, V) :- 92 | sample_attribute(S, A), 93 | attribute_property_value(A,P,V). 94 | sample_property_value(S, P, V, T) :- 95 | sample_attribute(S, A), 96 | attribute_property_value(A,P,V, T). 97 | 98 | sample_depth(S,V) :- 99 | sample_property_value(S, "depth"^^xsd:string, V). 100 | sample_environment_feature(S,V) :- 101 | sample_property_value(S, "environment feature"^^xsd:string, V). 102 | 103 | % B seems to always be identifiers.org 104 | see_also(A,B) :- rdf(A,rdfs:seeAlso,B). 105 | 106 | alt_label(A,B) :- rdf(A,skos:altLabel,B). 107 | 108 | in_taxon(A,B) :- rdf(A,ro:'0002162',B). 109 | 110 | identifier(A,X) :- rdf(A,dcterms:identifier,X). 111 | description(A,X) :- rdf(A,dcterms:description,X). 112 | translates_to(T,P) :- rdf(T,so:translates_to,P). % e.g. T to G 113 | transcribed_from(T,G) :- rdf(T,so:transcribed_from,G). % e.g. T to G 114 | has_part(T,E) :- rdf(T,so:has_part,E). % e.g. T to E 115 | 116 | direct_mapping(A,B) :- rdf(A,'http://rdf.ebi.ac.uk/terms/ensembl/DIRECT',B). 117 | 118 | protein_coding_gene(G) :- rdf(G,rdf:type,so:'0001217'). 119 | exon(X) :- rdf(X,rdf:type,so:'0000147'). 120 | transcript(X) :- rdf(X,rdf:type,so:'0000234'). 121 | 122 | homologous_to(A,B) :- orthologous_to(A,B). 123 | homologous_to(A,B) :- paralogous_to(A,B). 124 | 125 | orthologous_to(A,B) :- rdf(A,sio:'000558',B). 126 | paralogous_to(A,B) :- rdf(A,'http://semanticscience.org/resource/SIO:000630',B). % EBI uses incorrect PURL 127 | 128 | foo1('0'). 129 | 130 | feature_in_range(Chr,B,E,F) :- 131 | location(F,FB,FE,Chr), 132 | FB >= B, 133 | FE =< E. 134 | 135 | % mostly for demo: 136 | mouse_ortho_by_range(Chr,B,E,G,H) :- 137 | protein_coding_gene(G), % SO 138 | feature_in_range(Chr,B,E,G), 139 | orthologous_to(G,H), 140 | in_taxon(H,taxon:'10090'). 141 | 142 | foo2('0'). 143 | 144 | has_mouse_ortholog(HumanGene, MouseGene) :- 145 | orthologous_to(MouseGene, HumanGene), 146 | in_taxon(MouseGene,taxon:'10090'). 147 | 148 | 149 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/ebi/reactome.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | protein . entityReference => uniprot PURL 4 | smallMolecule . entityReference => obo:CHEBI 5 | dna . entityReference => id:ENSG 6 | rna . entityReference => ENST, mirbase 7 | 8 | */ 9 | 10 | :- module(reactome, 11 | [ 12 | equiv_go/2 13 | ]). 14 | 15 | :- use_module(library(sparqlprog/ontologies/biopax3)). 16 | :- use_module(library(sparqlprog/ontologies/ebi)). 17 | 18 | :- rdf_register_prefix(reactome,'http://identifiers.org/reactome/'). 19 | :- rdf_register_prefix(chebi, 'http://purl.obolibrary.org/obo/CHEBI_'). 20 | :- rdf_register_prefix(go, 'http://purl.obolibrary.org/obo/GO_'). 21 | 22 | 23 | % note: this is unidirectional only 24 | equiv_go(E,C) :- 25 | xref(E,X), 26 | id(X,Id), 27 | str_starts(Id,"GO:"), 28 | bind(replace(Id,"GO:","http://purl.obolibrary.org/obo/GO_"), C). 29 | 30 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/enipedia.pl: -------------------------------------------------------------------------------- 1 | :- module(enipedia, 2 | [ 3 | powerplant/1, 4 | 5 | primary_fuel_type/2, 6 | fuel_type_ppc/2, 7 | 8 | kg_co2pmwh/2, 9 | kg_co2pmwh_nd/2 10 | ]). 11 | 12 | :- sparql_endpoint( enipedia, 'http://enipedia.tudelft.nl/sparql'). 13 | 14 | :- rdf_register_prefix(skos, 'http://www.w3.org/2004/02/skos/core#'). 15 | :- rdf_register_prefix(dcterms,'http://purl.org/dc/terms/'). 16 | 17 | :- rdf_register_prefix(cat,'http://enipedia.org/wiki/Category:'). 18 | :- rdf_register_prefix(prop,'http://enipedia.org/wiki/Property:'). 19 | 20 | powerplant(X) :- rdf(X,rdf:type,cat:'Powerplant'). 21 | primary_fuel_type(P,F) :- rdf(P,prop:'Primary_fuel_type',F). 22 | 23 | fuel_type_ppc(F,Num) :- 24 | aggregate_group(count(P),[F],primary_fuel_type(P,F),Num). 25 | 26 | 27 | kg_co2pmwh(P,Q) :- rdf(P,prop:'Property:Intensity_kg_CO2_per_MWh_elec',Q). 28 | kg_co2pmwh_nd(P,Q) :- rdf(P,prop:'Property:Intensitynextdecade_kg_CO2_per_MWh_elec',Q). 29 | 30 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/faldo.pl: -------------------------------------------------------------------------------- 1 | :- module(faldo, 2 | [ 3 | 4 | location/2, 5 | location/4, 6 | location/5, 7 | stranded_location/6, 8 | begin/2, 9 | end/2, 10 | 11 | begin_coord/3, 12 | end_coord/3, 13 | position/2, 14 | reference/2, 15 | 16 | feature_contains/2 17 | ]). 18 | 19 | 20 | /** wrapper for FALDO genome interval vocabulary 21 | 22 | [FALDO](https://github.com/OBF/FALDO) is a vocabulary for specifying 23 | locations of sequence features along sequence intervals (DNA, RNA, or 24 | protein sequences). This module provides convenient wrappers for this 25 | vocabulary. 26 | 27 | */ 28 | 29 | :- use_module(library(typedef)). 30 | :- use_module(library(sparqlprog/owl_types)). 31 | 32 | :- rdf_register_prefix(faldo,'http://biohackathon.org/resource/faldo#'). 33 | 34 | % TYPES 35 | 36 | :- type faldo_feature ---> rdf_resource. 37 | :- type faldo_location ---> rdf_resource. 38 | 39 | 40 | 41 | %! location(?F : faldo_feature, ?L : faldo_location) is nondet. 42 | % 43 | % L is the location of feature F 44 | % 45 | location(F,L) :- rdf(F,faldo:location,L). 46 | 47 | %! location(?F, ?L, ?B, ?E, ?R) is nondet. 48 | %! location(?F, ?B, ?E, ?R) is nondet. 49 | % 50 | % feature F has location L, which has starts at B, ends at E, on reference R 51 | % 52 | % assumes feature is not split across references 53 | location(F,L,B,E,R) :- rdf(F,faldo:location,L),begin(L,PB),position(PB,B),reference(PB,R),end(L,PE),position(PE,E),reference(PE,R). 54 | location(F,B,E,R) :- location(F,_,B,E,R). 55 | 56 | 57 | 58 | %stranded_location(F,L,B,E,R,Str) :- location(F,L,B,E,R), bind(if(B E, bind(-1,Str). 62 | stranded_location(F,L,B,B,R,Str) :- location(F,L,B,B,R), bind(0,Str). 63 | 64 | 65 | 66 | %! begin(?L, ?P) is nondet. 67 | % 68 | % location L starts at position P 69 | % 70 | begin(L,P):- rdf(L,faldo:begin,P). 71 | 72 | %! end(?L, ?P) is nondet. 73 | % 74 | % location L ends at position P 75 | % 76 | end(L,P):- rdf(L,faldo:end,P). 77 | 78 | 79 | %! begin_coord(?L, ?C, ?R) is nondet. 80 | % 81 | % location L has start coordinate value C on reference R 82 | % 83 | begin_coord(L,C,R):- begin(L,P),position(P,C),reference(P,R). 84 | 85 | %! end_coord(?L, ?C, ?R) is nondet. 86 | % 87 | % location L has end coordinate value C on reference R 88 | % 89 | end_coord(L,C,R):- end(L,P),position(P,C),reference(P,R). 90 | 91 | 92 | %! position(?L, ?P) is nondet. 93 | % 94 | % location L has position P 95 | % 96 | position(L,P):- rdf(L,faldo:position,P). 97 | 98 | %! reference(?L, ?R) is nondet. 99 | % 100 | % location L has reference R 101 | % 102 | reference(L,R):- rdf(L,faldo:reference,R). 103 | 104 | feature_contains(F1,F2) :- 105 | location(F1,B1,E1,R), 106 | E1>B1, 107 | location(F2,B2,E2,R), 108 | B2 =< E1, 109 | B2 >= B1, 110 | E2 =< E1, 111 | E2 >= B1, 112 | F1\=F2. 113 | 114 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/gaz.pl: -------------------------------------------------------------------------------- 1 | :- module(gaz, 2 | [ 3 | geolocated_in/2, 4 | shared_location/3, 5 | nge/1 6 | ]). 7 | 8 | 9 | %geolocated_in(X,Y) :- subclass_of_some(X,'http://purl.obolibrary.org/obo/gaz#located_in',Y). 10 | geolocated_in(X,Y) :- rdf(X,'http://purl.obolibrary.org/obo/RO_0001025',Y). 11 | shared_location(X,Y,Z) :- geolocated_in(X,Y),geolocated_in(X,Z),Y\=Z. 12 | 13 | nge(X) :- rdf(X,rdf:type,'http://purl.obolibrary.org/obo/ENVO_00000009'). 14 | 15 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/gocam.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | */ 5 | 6 | :- module(gocam, 7 | [ 8 | model/1, 9 | model_title/2, 10 | model_state/2, 11 | production_model/1, 12 | model_gene/2, 13 | 14 | gene_to_function/2, 15 | gene_to_process/2, 16 | gene_to_component/2, 17 | gene_to_go/2, 18 | 19 | gene_to_gene/3, 20 | igene_to_igene/3, 21 | 22 | inf_biological_process/1 23 | 24 | ]). 25 | 26 | :- use_module(library(sparqlprog)). 27 | :- use_module(library(semweb/rdf11)). 28 | :- use_module(library(obo_ro/ro)). 29 | :- use_module(library(obo_core/goslim)). 30 | 31 | 32 | :- rdf_register_prefix(obo,'http://purl.obolibrary.org/obo/'). 33 | :- rdf_register_prefix(go,'http://purl.obolibrary.org/obo/GO_'). 34 | :- rdf_register_prefix(bds,'http://www.bigdata.com/rdf/search#'). 35 | :- rdf_register_prefix(lego,'http://geneontology.org/lego/'). 36 | :- rdf_register_prefix(prov,'http://www.w3.org/ns/prov#'). 37 | :- rdf_register_prefix(gocam,'http://model.geneontology.org/'). 38 | 39 | 40 | gene_to_go(GC,C) :- gene_to_process(GC,C). 41 | gene_to_go(GC,C) :- gene_to_component(GC,C). 42 | gene_to_go(GC,C) :- gene_to_function(GC,C). 43 | 44 | gene_to_process(GC,PC) :- 45 | ro:enabled_by(F,G), 46 | ro:part_of(F,P), 47 | rdf(G,rdf:type,GC), 48 | rdf(P,rdf:type,PC). 49 | 50 | gene_to_component(GC,CC) :- 51 | ro:enabled_by(F,G), 52 | ro:occurs_in(F,C), 53 | rdf(G,rdf:type,GC), 54 | rdf(C,rdf:type,CC). 55 | 56 | gene_to_function(GC,FC) :- 57 | ro:enabled_by(F,G), 58 | rdf(G,rdf:type,GC), 59 | rdf(F,rdf:type,FC). 60 | 61 | gene_to_gene(G1C,R,G2C) :- 62 | ro:enabled_by(F1,G1), 63 | rdf(F1,R,F2), 64 | ro:enabled_by(F2,G2), 65 | rdf(G1,rdf:type,G1C), 66 | rdf(G2,rdf:type,G2C). 67 | 68 | igene_to_igene(G1,R,G2) :- 69 | ro:enabled_by(F1,G1), 70 | rdf(F1,R,F2), 71 | ro:enabled_by(F2,G2). 72 | 73 | 74 | % TODO - restrict 75 | model(M) :- rdf(M,rdf:type,owl:'Ontology'). 76 | 77 | 78 | model_title(M,T) :- model(M),rdf(M,dc:title,T). 79 | model_state(M,X) :- model(M),rdf(M,lego:modelstate,X). 80 | production_model(M) :- model_state(M,"production"). 81 | 82 | model_gene(M,GC) :- 83 | ro:enabled_by(_,G,M), 84 | rdf(G,rdf:type,GC). 85 | 86 | inf_biological_process(X) :- 87 | rdf(X,rdf:type,T), 88 | rdf(T,rdfs:subClassOf,'http://purl.obolibrary.org/obo/GO_0008150'). 89 | 90 | % obo:go/extensions/go-graphstore.owl#subclass_closure 91 | 92 | /* 93 | 94 | rdfs_subclass_of 95 | 96 | $ pq-go "aggregate(count(X),inf_biological_process(X),N)" "x(N)" 97 | 3410772 98 | 99 | direct 100 | 101 | ~/repos/linkml-gocam $ pq-go "aggregate(count(X),inf_biological_process(X),N)" "x(N)" 102 | 79538 103 | 104 | rdfs:subClassOf 105 | 106 | ~/repos/linkml-gocam $ pq-go "aggregate(count(X),inf_biological_process(X),N)" "x(N)" 107 | 3410772 108 | 109 | 110 | */ 111 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/jcm.pl: -------------------------------------------------------------------------------- 1 | :- module(jcm, 2 | [ 3 | microbial_strain/1, 4 | microbial_strain_id/2, 5 | species_name/2, 6 | qualified_species_name/2, 7 | microbial_strain_sample/2, 8 | sample_environment/3, 9 | sample_environment/4, 10 | microbial_strain_sample_environment/2, 11 | microbial_strain_sample_environment/3, 12 | microbial_strain_sample_environment/4 13 | ]). 14 | 15 | :- use_module(library(semweb/rdf11)). 16 | 17 | %:- use_module(library(sparqlprog/ontologies/faldo)). 18 | 19 | :- sparql_endpoint(rdfportal, 'https://integbio.jp/rdf/sparql'). 20 | %:- sparql_endpoint(local, 'http://localhost'). 21 | 22 | :- rdf_register_prefix(rdfs, 'http://www.w3.org/2000/01/rdf-schema#'). 23 | :- rdf_register_prefix(dcterms, 'http://purl.org/dc/terms/'). 24 | :- rdf_register_prefix(mccv, 'http://purl.jp/bio/10/mccv#'). 25 | :- rdf_register_prefix(obo, 'http://purl.obolibrary.org/obo/'). 26 | :- rdf_register_prefix(jcms, 'http://metadb.riken.jp/db/rikenbrc_jcm_microbe/'). 27 | 28 | %! microbial_strain(?S) is nondet 29 | % 30 | % S is a microbial strain stocked in a biological resource center 31 | microbial_strain(S) :- rdf(S,rdf:type,mccv:'MCCV_000001'). 32 | 33 | %! microbial_strain_id(?S) is nondet 34 | % 35 | % microbial strain S has entry identifier I 36 | microbial_strain_id(S,I) :- microbial_strain(S),rdf(S,dcterms:identifier,I). 37 | 38 | %! species_name(?S,?N) is nondet 39 | % 40 | % microbial strain S has species name N 41 | species_name(S,N) :- microbial_strain(S),rdf(S,mccv:'MCCV_000011',N). 42 | 43 | %! qualified_species_name(?S) is nondet 44 | % 45 | % microbial strain S has qualified species name N 46 | qualified_species_name(S,N) :- microbial_strain(S),rdf(S,mccv:'MCCV_000010',N). 47 | 48 | %! microbial_strain_sample(?S,?SM) is nondet 49 | % 50 | % microbial strain S was isolated from sampled SM 51 | microbial_strain_sample(S,SM) :- microbial_strain(S),rdf(S,mccv:'MCCV_000028',SM). 52 | 53 | %! sample_environment(?S,?SM,?E) is nondet 54 | % 55 | % sample S from which microbial strain S was isolated was sapmled from 56 | % palce with environmental feature E 57 | sample_environment(S,SM,E) :- microbial_strain(S),microbial_strain_sample(S,SM),rdf(SM,mccv:'MCCV_000072',P),rdf(P,mccv:'MCCV_000071',E). 58 | sample_environment(S,SM,E,L) :- microbial_strain(S),microbial_strain_sample(S,SM),rdf(SM,mccv:'MCCV_000072',P),rdf(P,mccv:'MCCV_000071',E),rdf(E,rdfs:label,L). 59 | 60 | %! microbial_strain_sample_environment(?S,?E) is nondet 61 | % 62 | % microbial strain S inhabits place with environmetal feature E 63 | microbial_strain_sample_environment(S,E) :- microbial_strain(S),microbial_strain_sample(S,SM),sample_environment(S,SM,E). 64 | 65 | microbial_strain_sample_environment(S,N,E) :- microbial_strain(S),species_name(S,N),microbial_strain_sample(S,SM),sample_environment(S,SM,E). 66 | 67 | microbial_strain_sample_environment(S,N,E,L) :- microbial_strain(S),species_name(S,N),microbial_strain_sample(S,SM),sample_environment(S,SM,E),rdf(E,rdfs:label,L). 68 | 69 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/keggoc.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | KEGG Ortholog Clusters 4 | 5 | See https://www.genome.jp/oc/ 6 | 7 | */ 8 | 9 | :- module(keggoc, 10 | [ 11 | keggoc_ortholog/2 12 | ]). 13 | 14 | :- sparql_endpoint(keggoc, 'https://www.genome.jp/oc/proxy/sparql'). 15 | 16 | :- rdf_register_prefix(orth, 'http://purl.org/net/orth#'). 17 | 18 | keggoc_ortholog(G1, G2) :- 19 | rdf_path(OC, zeroOrMore(orth:hasHomologous), G1), 20 | rdf_path(OC, zeroOrMore(orth:hasHomologous), G2), 21 | rdf(G1, rdf:type, orth:'Gene'), 22 | rdf(G2, rdf:type, orth:'Gene'). 23 | 24 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/kghub.pl: -------------------------------------------------------------------------------- 1 | :- module(kghub, 2 | [ 3 | category/2, 4 | '^'/2, 5 | 6 | edge/5, 7 | edge/6, 8 | edge_property_value/3, 9 | provided_by/2, 10 | provided_by/4, 11 | 12 | relation_count/2, 13 | category_count/2, 14 | chained_provider/3, 15 | 16 | category_provider_count/3, 17 | category_node_property_count/3, 18 | category_relation_count/3, 19 | category_link_count/3, 20 | category_link_count/4, 21 | category_link_count/5, 22 | chained_edge_provider_count/3, 23 | edge_provider_count/2, 24 | provided_by_count/2, 25 | relation_provider_count/3 26 | %edge_property_count/2 27 | ]). 28 | 29 | %:- use_module(library(biolink_model)). 30 | %:- reexport([library(biolink_model)]). 31 | 32 | 33 | :- rdf_register_prefix(biolink, 'https://w3id.org/biolink/vocab/'). 34 | :- rdf_register_prefix(uniprotkb, 'http://identifiers.org/uniprot/'). 35 | :- rdf_register_prefix(tmp, 'https://www.example.org/UNKNOWN/'). 36 | :- sparql_endpoint( kgcovid19, 'http://kg-hub-rdf.berkeleybop.io/sparql'). 37 | 38 | category(I,C) :- rdf(I,biolink:category,C). 39 | 40 | capitalize(W,W2) :- 41 | atom_chars(W,[C|W1]), 42 | upcase_atom(C,C2), 43 | atom_chars(W2,[C2|W1]). 44 | 45 | 46 | '^'(I,C) :- 47 | pre(( 48 | kghub:capitalize(C,Cx), 49 | rdf_global_id(biolink:Cx,URI) 50 | )), 51 | category(I,URI). 52 | 53 | edge(I,R,J,P,V) :- 54 | edge(_,I,R,J,P,V). 55 | edge(A,I,R,J,P,V) :- 56 | rdf(A,biolink:subject,I), 57 | rdf(A,biolink:relation,R), 58 | rdf(A,biolink:object,J), 59 | rdf(A,P,V). 60 | 61 | edge_property_value(A,P,V) :- 62 | rdf(A,biolink:subject,_), 63 | rdf(A,P,V). 64 | 65 | provided_by(A,X) :- 66 | rdf(A,biolink:provided_by,X). 67 | provided_by(I,R,J,X) :- 68 | rdf(A,biolink:subject,I), 69 | rdf(A,biolink:relation,R), 70 | rdf(A,biolink:object,J), 71 | rdf(A,biolink:provided_by,X). 72 | 73 | 74 | chained_provider(Src1,Src2,JoinNode) :- 75 | rdf(A1,biolink:object,JoinNode), 76 | rdf(A2,biolink:subject,JoinNode), 77 | rdf(A1,biolink:provided_by,Src1), 78 | rdf(A2,biolink:provided_by,Src2). 79 | 80 | 81 | relation_count(P,N) :- aggregate_group(count(*),[P],rdf(_,P,_),N). 82 | category_count(C,N) :- aggregate_group(count(I),[C],category(I,C),N). 83 | category_provider_count(C,S,N) :- aggregate_group(count(I),[C,S],(category(I,C),provided_by(I,S)),N). 84 | category_relation_count(C,P,N) :- aggregate_group(count(*),[C,P],(category(I,C),rdf(I,P,_)),N). 85 | category_node_property_count(C,P,N) :- aggregate_group(count(*),[C,P],(category(I,C),rdf(I,P,O),is_literal(O)),N). 86 | category_link_count(C1,C2,N) :- aggregate_group(count(*),[C1,C2],(category(I1,C1),rdf(I1,_P,I2),category(I2,C2)),N). 87 | category_link_count(C1,R,C2,N) :- aggregate_group(count(*),[C1,R,C2],(category(I1,C1),rdf(I1,R,I2),category(I2,C2)),N). 88 | category_link_count(C1,R,C2,Src,N) :- aggregate_group(count(*),[C1,R,C2],(category(I1,C1),rdf(I1,R,I2),category(I2,C2),provided_by(I1,R,I2,Src)),N). 89 | chained_edge_provider_count(Src1,Src2,N) :- aggregate_group(count(*),[Src1,Src2],chained_provider(Src1,Src2,_),N). 90 | provided_by_count(P,N) :- aggregate_group(count(*),[P],provided_by(_,P),N). 91 | edge_provider_count(S,N) :- aggregate_group(count(*),[],(provided_by(_I1,_R,_I2,S)),N). % TODO 92 | relation_provider_count(R,S,N) :- aggregate_group(count(A),[R],(rdf(A,biolink:relation,R),rdf(A,biolink:provided_by,S)),N). 93 | 94 | %edge_property_count(S,N) :- aggregate_group(count(*),[],(provided_by(_I1,_R,_I2,S)),N). % TODO 95 | %relation_count(P,N) :- aggregate_group(count(*),[P],rdf(_,P,_),N). 96 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/load.pl: -------------------------------------------------------------------------------- 1 | :- use_module(biogateway, []). 2 | :- use_module(biopax3,[]). 3 | :- use_module(biotea, []). 4 | :- use_module(chembl, []). 5 | :- use_module(dbpedia, []). 6 | :- use_module(dbpedia/dbpedia_matcher, []). 7 | :- use_module(disgenet,[]). 8 | :- use_module(ebi, []). 9 | :- use_module(enipedia, []). 10 | :- use_module(faldo, []). 11 | :- use_module(gaz, []). 12 | %:- use_module(gocam, []). 13 | :- use_module(monarch, []). 14 | :- use_module(ncit, []). 15 | :- use_module(nextprot, []). 16 | :- use_module(oban, []). 17 | :- use_module(ontobee, []). 18 | :- use_module(owl, []). 19 | :- use_module(pubmedgraph, []). 20 | :- use_module(rhea, []). 21 | :- use_module(sosa, []). 22 | :- use_module(uniprot, []). 23 | %:- use_module(wikidata, []). 24 | :- use_module(wikipathways, []). 25 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/mbgd.pl: -------------------------------------------------------------------------------- 1 | :- module(mbgd, 2 | [ 3 | complete_genome/1, 4 | homologous_member/2, 5 | in_mbgd_default/1, 6 | mbgd_ortholog/2 7 | ]). 8 | 9 | :- sparql_endpoint(mbgd, 'https://sparql.nibb.ac.jp/sparql'). 10 | 11 | :- rdf_register_prefix(orth, 'http://purl.org/net/orth#'). 12 | :- rdf_register_prefix(mbgd, 'http://purl.jp/bio/11/mbgd#'). 13 | :- rdf_register_prefix(mbgdr, 'http://mbgd.genome.ad.jp/rdf/resource/'). 14 | 15 | complete_genome(G) :- 16 | rdf(G, rdf:type, mbgd:'CompleteGenome'). 17 | 18 | homologous_member(O, G) :- 19 | rdf_path(O, zeroOrMore(orth:hasHomologous), G), 20 | rdf(G, rdf:type, orth:'Gene'). 21 | 22 | in_mbgd_default(OC) :- 23 | rdf(OC, rdf:type, orth:'OrthologsCluster'), 24 | rdf(OC, void:inDataset, mbgdr:default). 25 | 26 | mbgd_ortholog(G1, G2) :- 27 | in_mbgd_default(OC), 28 | homologous_member(OC, G1), 29 | homologous_member(OC, G2). 30 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/monarch.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | */ 5 | 6 | :- module(monarch, 7 | [ 8 | 9 | protein_coding_gene/1, 10 | human_gene/1, 11 | disease_to_gene/2, 12 | %is_marker_for/2, 13 | association/4, 14 | association/5, 15 | clinvar_to_rs_number/2, 16 | dbsnp_to_rs_number/2, 17 | has_phenotype_association/3, 18 | has_phenotype_association/4, 19 | has_phenotype_freq/3, 20 | 21 | phenotype_rel/3, 22 | disease_to_phenotype_SD/2, 23 | disease_to_phenotype_EE/4, 24 | disease_to_phenotype_EE/2, 25 | 26 | disease_to_phenotype_SX/4 27 | ]). 28 | 29 | :- use_module(library(sparqlprog)). 30 | :- use_module(library(semweb/rdf11)). 31 | :- use_module(library(sparqlprog/ontologies/oban)). 32 | 33 | %:- sparql_endpoint( monarch, 'http://rdf.monarchinitiative.org/sparql'). 34 | :- sparql_endpoint( monarch, 'https://translator.ncats.io/monarch-blazegraph/namespace/kb/sparql/'). 35 | 36 | 37 | :- rdf_register_ns(oio,'http://www.geneontology.org/formats/oboInOwl#'). 38 | :- rdf_register_prefix(oban,'http://purl.org/oban/'). 39 | :- rdf_register_prefix(foaf,'http://xmlns.com/foaf/0.1/'). 40 | :- rdf_register_prefix(obo,'http://purl.obolibrary.org/obo/'). 41 | :- rdf_register_prefix(mondo,'http://purl.obolibrary.org/obo/MONDO_'). 42 | :- rdf_register_prefix(so,'http://purl.obolibrary.org/obo/SO_'). 43 | :- rdf_register_prefix(bds,'http://www.bigdata.com/rdf/search#'). 44 | :- rdf_register_prefix(monarch,'https://monarchinitiative.org/'). 45 | 46 | % TODO 47 | :- rdf_register_prefix(mgi,'http://www.informatics.jax.org/accession/MGI:'). 48 | :- rdf_register_prefix(orphanet,'http://www.orpha.net/ORDO/Orphanet_'). 49 | :- rdf_register_prefix(clinvar,'http://www.ncbi.nlm.nih.gov/clinvar/'). 50 | 51 | id_uri(ID,URI) :- 52 | concat_atom([Pre,Frag],':',ID), 53 | concat_atom(['http://purl.obolibrary.org/obo/',Pre,'_',Frag],URI). 54 | 55 | % Each property declared with pname_id generates a binary predicate 56 | user:term_expansion(pname_id(P,Id), 57 | [( Head :- Body), 58 | (:- initialization(export(P/2), now)) 59 | ]) :- 60 | Head =.. [P,S,O], 61 | id_uri(Id,Px), 62 | Body = rdf(S,Px,O). 63 | 64 | % Each class name declared with cname_id generates 65 | % - A unary predicate cname(Inst) 66 | % - A unary predicate cname_inf(Ins) [inferred] 67 | % - A unary predicate isa_cname(C) [inferred] 68 | user:term_expansion(cname_id(C,Id), 69 | [Rule, 70 | RuleInf, 71 | RuleIsa, 72 | (:- initialization(export(InfC/1), now)), 73 | (:- initialization(export(SubC/1), now)), 74 | (:- initialization(export(C/1), now)) 75 | ]) :- 76 | id_uri(Id,Cx), 77 | 78 | Head =.. [C,I], 79 | Body = rdf(I,rdf:type,Cx), 80 | Rule = (Head :- Body), 81 | 82 | atom_concat(C,'_inf',InfC), 83 | Head2 =.. [InfC,I], 84 | Body2 = rdfs_individual_of(I,Cx), 85 | RuleInf = (Head2 :- Body2), 86 | 87 | atom_concat('isa_',C,SubC), 88 | Head3 =.. [SubC,I], 89 | Body3 = rdfs_subclass_of(I,Cx), 90 | RuleIsa = (Head3 :- Body3). 91 | 92 | 93 | 94 | has_phenotype_association(A,S,O) :- 95 | has_phenotype_iri(P), 96 | association(A,S,P,O). 97 | 98 | has_phenotype_association(A,S,O,Src) :- 99 | has_phenotype_association(A,S,O), 100 | rdf(A,dc:source,Src). 101 | 102 | has_phenotype_freq(X,P,F) :- 103 | has_phenotype_association(X,P,A), 104 | rdf(A,monarch:frequencyOfPhenotype,F). 105 | 106 | 107 | %! disease_to_phenotype_EE(?D,?P) is nondet 108 | % 109 | % true if D has P, 110 | % and where the assertion may be anything equivalent to D 111 | % or anything equivalent to P 112 | disease_to_phenotype_EE(D,P) :- 113 | disease_to_phenotype_EE(D,P,_,_). 114 | disease_to_phenotype_EE(D,P,Dx,Px) :- 115 | owl_equivalent_class(D,Dx), 116 | has_phenotype(Dx,Px), 117 | owl_equivalent_class(Px,P). 118 | 119 | %! disease_to_phenotype_ED(?D,?P) is nondet 120 | % 121 | % true if D has P, 122 | % and where the assertion may be anything equivalent to D 123 | % and directly to P 124 | disease_to_phenotype_ED(D,P) :- 125 | owl_equivalent_class(D,Dx), 126 | has_phenotype(Dx,P). 127 | 128 | %! disease_to_phenotype_ED(?D,?P) is nondet 129 | % 130 | % true if D has P, 131 | % and where the assertion may be any subclass of D 132 | % and directly to P 133 | disease_to_phenotype_SD(D,P) :- 134 | rdfs_subclass_of(Ds,D), 135 | owl_equivalent_class(Ds,Dx), 136 | has_phenotype(Dx,P). 137 | 138 | disease_to_phenotype_SX(D,P,Dx,Px) :- 139 | rdfs_subclass_of(Ds,D), 140 | owl_equivalent_class(Ds,Dx), 141 | has_phenotype(Dx,Px), 142 | owl_equivalent_class(Px,P). 143 | 144 | 145 | d2v(D,V) :- is_marker_for(V,D). 146 | disease_to_gene(D,G) :- is_marker_for(V,D),rdf(V,obo:'GENO_0000418',G). 147 | 148 | 149 | 150 | protein_coding_gene(G) :- rdfs_subclass_of(G,obo:'SO_0001217'). 151 | human_gene(G) :- protein_coding_gene(G), in_taxon(G,obo:'NCBITaxon_9606'). 152 | 153 | 154 | phenotype_rel(Ph,Pr,Y) :- 155 | rdf(Ph,owl:equivalentClass,EP), 156 | has_part_iri(HasPart), 157 | owl_some(EP,HasPart,I), 158 | intersection_member(I,M), 159 | owl_some(M,Pr,Y). 160 | 161 | 162 | clinvar_to_rs_number(V,Id) :- 163 | % see https://github.com/monarch-initiative/dipper/issues/822 164 | rdf(V,rdf:type,obo:'GENO_0000030'), 165 | rdf(V,oio:hasdbxref,X), 166 | dbsnp_to_rs_number(X,Id). 167 | 168 | dbsnp_to_rs_number(X,Id) :- 169 | % CHANGE THIS: see https://github.com/monarch-initiative/dipper/issues/822 170 | replace(str(X),"http://www.ncbi.nlm.nih.gov/projects/SNP/snp_ref.cgi?rs=","",Id). 171 | dbsnp_to_curie(X,Id) :- 172 | replace(str(X),"http://www.ncbi.nlm.nih.gov/projects/SNP/snp_ref.cgi?rs=","dbSNP:",Id). 173 | 174 | 175 | 176 | % in RO 177 | %pname_id(has_phenotype, 'RO:0002200'). 178 | 179 | 180 | % cut -f1,2 biolink-model.tsv | grep : | perl -npe 's@\|SIO:\d+@@' | tbl2p -p cname_id 181 | 182 | cname_id('individual_organism', 'SIO:010000'). 183 | cname_id('disease', 'MONDO:0000001'). 184 | cname_id('phenotypic_feature', 'UPHENO:0000001'). 185 | cname_id('confidence_level', 'CIO:0000028'). 186 | cname_id('evidence_type', 'ECO:0000000'). 187 | cname_id('publication', 'IAO:0000311'). 188 | cname_id('chemical_substance', 'SIO:010004'). 189 | cname_id('genomic_entity', 'SO:0000110'). 190 | cname_id('genome', 'SO:0001026'). 191 | cname_id('transcript', 'SO:0000673'). 192 | cname_id('exon', 'SO:0000147'). 193 | cname_id('coding_sequence', 'SO:0000316'). 194 | cname_id('gene', 'SO:0000704'). 195 | cname_id('protein', 'PR:000000001'). 196 | cname_id('RNA_product', 'CHEBI:33697'). 197 | cname_id('microRNA', 'SO:0000276'). 198 | cname_id('macromolecular_complex', 'GO:0032991'). 199 | cname_id('gene_family', 'NCIT:C20130'). 200 | cname_id('zygosity', 'GENO:0000133'). 201 | cname_id('sequence_variant', 'GENO:0000512'). 202 | cname_id('drug_exposure', 'ECTO:0000509'). 203 | cname_id('treatment', 'OGMS:0000090'). 204 | cname_id('molecular_activity', 'GO:0003674'). 205 | cname_id('biological_process', 'GO:0008150'). 206 | cname_id('cellular_component', 'GO:0005575'). 207 | cname_id('cell', 'GO:0005623'). 208 | 209 | /* 210 | 211 | 212 | */ 213 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/ncit.pl: -------------------------------------------------------------------------------- 1 | :- module(ncit, 2 | [ 3 | gene_associated_with_disease/2, 4 | gene_associated_with_disease_inf/3, 5 | 6 | disease_has_finding_svf/2 7 | ]). 8 | 9 | :- use_module(library(sparqlprog)). 10 | :- use_module(library(semweb/rdf11)). 11 | 12 | :- sparql_endpoint( ncit, 'https://stars-app.renci.org/ncitgraph/sparql'). 13 | 14 | :- rdf_register_prefix('NCIT','http://purl.obolibrary.org/obo/NCIT_'). 15 | 16 | gene_associated_with_disease(G,D) :- rdf(G,'NCIT':'R38',D). 17 | gene_associated_with_disease_inf(G,D,X) :- rdf(G,'NCIT':'R38',X),rdfs_subclass_of(X,D). 18 | 19 | disease_has_finding_svf(D,F) :- subclass_of_some(D,'NCIT':'R108',F). 20 | 21 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/nextprot.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | */ 4 | 5 | :- module(nextprot, 6 | [ 7 | 8 | keyword/2, 9 | keyword_term/2, 10 | 11 | isoform/2, 12 | gene/2, 13 | child_of/2, 14 | term_child_of/2, 15 | 16 | cellular_component/2, 17 | cellular_component_term_child_of/2, 18 | 19 | expression/2, 20 | expression_term_child_of/2, 21 | expression_term_child_of/3, 22 | evidence_expression_level/2, 23 | 24 | topology/2, 25 | topology_transmembrane/2, 26 | 27 | phosphoprotein/1, 28 | 29 | ensembl_localid/2, 30 | 31 | protein_phosphorylated_in_cytoplasm/1, 32 | high_expression_in_brain/1 33 | 34 | ]). 35 | 36 | :- use_module(library(sparqlprog)). 37 | :- use_module(library(semweb/rdf11)). 38 | 39 | :- sparql_endpoint( nextprot, 'https://api.nextprot.org/sparql'). 40 | 41 | :- rdf_register_prefix(nextprot,'http://nextprot.org/rdf#'). 42 | :- rdf_register_prefix(nextprot_cv,'http://nextprot.org/rdf/terminology/'). 43 | 44 | keyword(G,K) :- rdf(G,nextprot:keyword,K). 45 | keyword_term(G,T) :- 46 | rdf(G,nextprot:keyword,K), 47 | rdf(K,nextprot:term,T). 48 | 49 | isoform(A,B) :- rdf(A,nextprot:isoform,B). 50 | gene(A,B) :- rdf(A,nextprot:gene,B). 51 | 52 | child_of(A,B) :- rdf(A,nextprot:childOf,B). 53 | term_child_of(T,B) :- rdf(T,nextprot:term,A), rdf(A,nextprot:childOf,B). 54 | 55 | cellular_component(P,C) :- rdf(P,nextprot:cellularComponent,C). 56 | cellular_component_term_child_of(P,C) :- cellular_component(P,Z),term_child_of(Z,C). 57 | 58 | expression(P,E) :- rdf(P,nextprot:expression,E). 59 | expression_term_child_of(P,T,E) :- expression(P,E),term_child_of(E,T). 60 | expression_term_child_of(P,T) :- expression_term_child_of(P,T,_). 61 | 62 | evidence_expression_level(E,L) :- rdf(E,nextprot:evidence,Ev), rdf(Ev,nextprot:expressionLevel,L). 63 | 64 | topology(P,S) :- rdf(P,nextprot:topology,S). 65 | 66 | topology_transmembrane(P,S) :- topology(P,S),rdf(S,rdf:type,nextprot:'TransmembraneRegion'). 67 | 68 | 69 | phosphoprotein(P) :- keyword_term(P,nextprot_cv:'KW-0597'). 70 | 71 | % ---------------------------------------- 72 | % IDs 73 | % ---------------------------------------- 74 | ensembl_localid(G,Id) :- bind(replace(str(G),"http://nextprot.org/rdf/gene/",""), Id). 75 | 76 | 77 | % ---------------------------------------- 78 | % Example queries 79 | % ---------------------------------------- 80 | 81 | protein_phosphorylated_in_cytoplasm(P) :- 82 | isoform(P,F), 83 | phosphoprotein(F), 84 | cellular_component_term_child_of(F,nextprot_cv:'SL-0086'). 85 | 86 | high_expression_in_brain(P) :- 87 | isoform(P,F), 88 | expression(F,E), 89 | term_child_of(E,nextprot_cv:'TS-0095'), 90 | evidence_expression_level(E,nextprot:'High'). 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/oban.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | */ 5 | 6 | :- module(oban, 7 | [ 8 | association/4, 9 | association/5 10 | ]). 11 | 12 | :- use_module(library(sparqlprog)). 13 | :- use_module(library(semweb/rdf11)). 14 | 15 | :- rdf_register_prefix(oban,'http://purl.org/oban/'). 16 | 17 | association(A,S,P,O) :- 18 | rdf(A,oban:association_has_subject,S), 19 | rdf(A,oban:association_has_object,O), 20 | rdf(A,oban:association_has_predicate,P). 21 | 22 | association(A,S,P,O,Src) :- 23 | association(A,S,P,O), 24 | rdf(A,dc:source,Src). 25 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/obo.pl: -------------------------------------------------------------------------------- 1 | /** wrapper that provides an obo-format like view over an OWL ontology 2 | 3 | More details on obo format: 4 | 5 | http://owlcollab.github.io/oboformat/doc/obo-syntax.html 6 | 7 | In particular, this module provides: 8 | 9 | - A graph-like view over an ontology via is_a/2 and relationship/3 10 | - the obo synonym model: entity_synonym_scope/3 11 | - the obo xref model: entity_xref/2 is re-exported from obo_metadata/oio; additional convenience preds such as entity_xref_src/4 12 | 13 | 14 | */ 15 | 16 | :- module(obo, 17 | [ 18 | is_a/2, 19 | relationship/3, 20 | is_dangling/1, 21 | synprop_scope/2, 22 | entity_synonym_scope/2, 23 | entity_synonym_scope/3, 24 | entity_synonym_scope_type/4, 25 | entity_synonym_scope_xref/4, 26 | entity_xref_src/3, 27 | entity_xref_src/4, 28 | entity_xref_prefix/3 29 | ]). 30 | 31 | :- use_module(library(obo_metadata/oio)). 32 | 33 | :- use_module(library(semweb/rdf11)). 34 | :- use_module(library(semweb/rdfs)). 35 | :- use_module(library(sparqlprog/owl_util)). 36 | 37 | :- reexport(library(obo_metadata/oio), [has_dbxref/2]). 38 | 39 | :- rdf_register_ns(oio,'http://www.geneontology.org/formats/oboInOwl#'). 40 | :- rdf_register_ns(def,'http://purl.obolibrary.org/obo/IAO_0000115'). 41 | 42 | synprop_scope('http://www.geneontology.org/formats/oboInOwl#hasRelatedSynonym','RELATED'). 43 | synprop_scope('http://www.geneontology.org/formats/oboInOwl#hasNarrowSynonym','NARROW'). 44 | synprop_scope('http://www.geneontology.org/formats/oboInOwl#hasBroadSynonym','BROAD'). 45 | synprop_scope('http://www.geneontology.org/formats/oboInOwl#hasExactSynonym','EXACT'). 46 | 47 | 48 | %! is_a(?A, ?B) is nondet. 49 | % 50 | % subClassOf between two names classes 51 | % 52 | is_a(A,B) :- 53 | rdf(A,rdfs:subClassOf,B), 54 | rdf_is_iri(B), 55 | rdf_is_iri(A). 56 | is_a(A,B) :- 57 | rdf(A,rdfs:subPropertyOf,B), 58 | rdf_is_iri(B), 59 | rdf_is_iri(A). 60 | 61 | %! relationship(?E, ?R, ?O) is nondet. 62 | % 63 | % true if E subClassOf R some O 64 | % 65 | relationship(E,R,O) :- 66 | rdf(E,rdfs:subClassOf,Res), 67 | owl_some(Res,R,O), 68 | \+ rdf_is_bnode(O). 69 | 70 | 71 | %! is_dangling(?E) is semidet. 72 | % 73 | % true if E has no label 74 | % 75 | is_dangling(E) :- 76 | \+ label(E,_). 77 | 78 | 79 | 80 | %! entity_synonym_scope(?E, ?V) is nondet. 81 | %! entity_synonym_scope(?E, ?V, ?Scope) is nondet. 82 | % 83 | % true if V is a synonym for entity E, with a Scope of either: exact, broad, narrow, related 84 | % 85 | entity_synonym_scope(E,V,Scope) :- 86 | synprop_scope(P,Scope), 87 | rdf(E,P,V). 88 | entity_synonym_scope(E,V) :- 89 | entity_synonym_scope(E,V,_). 90 | 91 | 92 | %! entity_synonym_scope_type(?E, ?V, ?Scope, ?Type) is nondet. 93 | % 94 | % As entity_synonym_scope/3 but also include the synonym type 95 | % 96 | entity_synonym_scope_type(E,V,Scope,Type) :- 97 | synprop_scope(P,Scope), 98 | triple_axiom_annotation(E,P,V,oio:hasSynonymType,Type). 99 | 100 | 101 | %! entity_synonym_scope_xref(?E, ?V, ?Scope, ?Xref) is nondet. 102 | % 103 | % As entity_synonym_scope/3 but also include the xref provenance for the synonym 104 | % 105 | entity_synonym_scope_xref(E,V,Scope,Xref) :- 106 | synprop_scope(P,Scope), 107 | triple_axiom_annotation(E,P,V,oio:hasDbXref,Xref). 108 | 109 | 110 | 111 | %! entity_xref_src(?Cls:atm, ?X:str, ?S:str) 112 | % 113 | % true if Cls has X as an xref, and axiom annotated with S 114 | entity_xref_src(C,X) :- 115 | entity_xref_src(C,X,_). 116 | entity_xref_src(C,X,S) :- 117 | entity_xref_src(C,X,_,S). 118 | entity_xref_src(C,X,A,S) :- 119 | has_dbxref_axiom(C,X,A), 120 | rdf(A,oio:source,S). 121 | 122 | %! entity_xref_prefix(?Cls:atm, ?X:str, ?Pre:str) 123 | % 124 | % true if Cls has X as an xref, and X has prefix Pre 125 | % 126 | :- rdf_meta entity_xref_prefix(r,o,o). 127 | entity_xref_prefix(C,X,P) :- 128 | has_dbxref(C,X), 129 | curie_prefix(X,P). 130 | 131 | %! one_to_one_xref(?E,X?,?S) is nondet 132 | one_to_one_xref(E,X,S) :- 133 | entity_xref_idspace(E,X,S), 134 | \+ ((entity_xref_idspace(E,X2,S), 135 | X2\=X)), 136 | \+ ((entity_xref_idspace(E2,X,S), 137 | E2\=E)). 138 | 139 | non_one_to_one_xref(E,X,S) :- 140 | entity_xref_idspace(E,X,S), 141 | \+ one_to_one_xref(E,X,S). 142 | 143 | one_to_many_xref(E,X,S) :- 144 | one_to_many_xref(E,X,S,_). 145 | one_to_many_xref(E,X,S,X2) :- 146 | entity_xref_idspace(E,X,S), 147 | entity_xref_idspace(E,X2,S), 148 | X2\=X. 149 | 150 | many_to_one_xref(E,X,S) :- 151 | many_to_one_xref(E,X,S,_). 152 | many_to_one_xref(E,X,S,E2) :- 153 | entity_xref_idspace(E,X,S), 154 | entity_xref_idspace(E2,X,S), 155 | E2\=E. 156 | 157 | many_to_many_xref(E,X,S,E2,X2) :- 158 | many_to_one_xref(E,X,S,X2), 159 | one_to_many_xref(E,X,S,E2). 160 | 161 | many_to_many_xref(E,X,S) :- 162 | entity_xref_idspace(E,X,S), 163 | \+ \+ many_to_one_xref(E,X,S), 164 | \+ \+ one_to_many_xref(E,X,S). 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/oma.pl: -------------------------------------------------------------------------------- 1 | :- module(oma, 2 | [ 3 | is_orthologous_to/2, 4 | is_orthologous_to/5, 5 | organism_in_taxon/2, 6 | encoded_by/2, 7 | ensembl_xref/2, 8 | encoded_by_ensembl_gene/2 9 | ]). 10 | 11 | :- use_module(library(sparqlprog/ontologies/faldo)). 12 | :- reexport(library(sparqlprog/ontologies/ebi), [in_taxon/2]). 13 | 14 | :- sparql_endpoint( oma, 'https://sparql.omabrowser.org/sparql'). 15 | 16 | :- rdf_register_prefix(up,'http://purl.uniprot.org/core/'). 17 | :- rdf_register_prefix(orth, 'http://purl.org/net/orth#'). 18 | :- rdf_register_prefix(lscr, 'http://purl.org/lscr#'). 19 | 20 | is_orthologous_to(P1,P2) :- 21 | is_orthologous_to(P1,P2,_,_,_). 22 | is_orthologous_to(P1,P2,N1,N2,C) :- 23 | rdf(C,rdf:type,orth:'OrthologsCluster'), 24 | rdf(C,orth:hasHomologousMember,N1), 25 | rdf(C,orth:hasHomologousMember,N2), 26 | N1\=N2, 27 | rdf_path(N1,zeroOrMore(orth:hasHomologousMember),P1), 28 | rdf_path(N1,zeroOrMore(orth:hasHomologousMember),P2). 29 | 30 | encoded_by(P,G) :- 31 | rdf(P,sio:'010079',G). 32 | 33 | ensembl_xref(G,X) :- 34 | rdf(G,lscr:xrefEnsemblGene,X). 35 | 36 | encoded_by_ensembl_gene(P,X) :- 37 | encoded_by(P,G),ensembl_xref(G,X). 38 | 39 | organism_in_taxon(P,T) :- 40 | rdf(P,orth:organism,O), 41 | in_taxon(O,T). 42 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/ontobee.pl: -------------------------------------------------------------------------------- 1 | :- module(ontobee, 2 | [ 3 | in_graph/2, 4 | typed_in_graph/3, 5 | in_ontology/2, 6 | in_ontology/3, 7 | 8 | graph_ontology/2, 9 | 10 | searchall/4, 11 | ontsearch/4, 12 | 13 | compare_via_xref/1, 14 | compare_via_xref/6, 15 | ontobee_edge/3 16 | ]). 17 | 18 | :- use_module(library(sparqlprog)). 19 | :- use_module(library(semweb/rdf11)). 20 | 21 | :- rdf_register_prefix(obomerged,'http://purl.obolibrary.org/obo/merged/'). 22 | :- rdf_register_prefix(obo,'http://purl.obolibrary.org/obo/'). 23 | 24 | :- rdf_register_prefix('MONDO','http://purl.obolibrary.org/obo/MONDO_'). 25 | 26 | 27 | 28 | in_graph(X,G) :- 29 | typed_in_graph(X,G,_). 30 | typed_in_graph(X,G,T) :- 31 | rdf(X,rdf:type,T,G). 32 | 33 | 34 | :- srule(in_ontology,[entity:iri, ontology:iri], 35 | 'Entity is declared to be of some type in Ontology'). 36 | in_ontology(X,O) :- 37 | in_ontology(X,O,_). 38 | 39 | :- srule(in_ontology,[entity:iri, ontology:iri, type:iri], 40 | 'Entity is declared to be of type T in Ontology'). 41 | in_ontology(X,O,T) :- 42 | typed_in_graph(X,G,T), 43 | graph_ontology(G,O). 44 | 45 | :- srule(graph_ontology,[graph:iri, ontology:iri], 'maps an ontobee graph IRI to an ontology name (e.g. hp, go, uberon, ncit)'). 46 | graph_ontology(G,O) :- 47 | G == uri(concat('http://purl.obolibrary.org/obo/merged/',ucase(O))). 48 | 49 | 50 | ontsearch(O,P,C,L) :- graph_ontology(G,O),rdf(C,rdfs:label,L,G),regex(str(L),P). 51 | searchall(G,P,C,L) :- rdf(C,rdfs:label,L,G),regex(str(L),P). 52 | 53 | 54 | 55 | curie_uri_graph(X,URI,G) :- 56 | concat_atom([DB,Local],':',X), 57 | DB\=http, 58 | atom_concat('http://purl.obolibrary.org/obo/merged/',DB,G), 59 | concat_atom(['http://purl.obolibrary.org/obo/',DB,'_',Local],URI). 60 | 61 | % UBERON:0002228 ! rib 62 | compare_via_xref(C) :- 63 | compare_via_xref(C,_R,_P,_CX,_XG,_Status). 64 | 65 | compare_via_xref(C_id,R,P,CX,XG,Status) :- 66 | ensure_uri(C_id,C), 67 | format('# Testing: ~w~n',[C]), 68 | ontobee_edge(C,R,P), 69 | ontobee_label(P,PN), 70 | format('# Edge: ~w ~w ~w ~w~n',[C,R,P,PN]), 71 | ontobee_has_dbxref(C,CX), 72 | format('# C xref: ~w~n',[CX]), 73 | curie_uri_graph(CX,CXU,XG), 74 | format('# Checking if present in : ~w~n',[XG]), 75 | find_and_compare_edge(C,R,P,CXU,XG, Status), 76 | format('# Status: Checking for relationship ~w ~w "~w" in ~w, Status: ~w~n',[R,P,PN,XG,Status]). 77 | 78 | 79 | 80 | find_and_compare_edge(_C,R,P,CXU,XG, Status) :- 81 | ontobee_has_dbxref(P,PX), 82 | curie_uri_graph(PX,PXU,XG), 83 | !, 84 | test_for_edge(R,CXU, PXU, Status). 85 | find_and_compare_edge(_,_,P,_,XG, no_equiv_to(P,XG)). 86 | 87 | test_for_edge(RU,CXU, PXU, analogous_edge(Same)) :- 88 | ontobee_edge(CXU,RXU,PXU), 89 | !, 90 | is_same_rel(RU,RXU,Same). 91 | xxxxxtest_for_edge(RU,CXU, PXU, indirect_subclass(Same)) :- 92 | service(ontobee,rdfs_subclass_of(CXU,PXU)), 93 | !, 94 | is_same_rel(RU,rdfs:subClassOf,Same). 95 | test_for_edge(_,_, _, no_relationship_between_equivs) :- !. 96 | 97 | is_same_rel(RU,RXU,Same) :- 98 | ( RU=RXU 99 | -> Same=same_rel 100 | ; Same=diff_rel). 101 | 102 | 103 | ontobee_edge(C,R,P) :- 104 | service(ontobee,subclass_of_some(C,R,P)). 105 | ontobee_edge(C,subClassOf,P) :- 106 | % service(ontobee,((subClassOf(C,P),\+rdf_bnode(P)))). 107 | % service(ontobee,(subClassOf(C,P),\+is_blank(P))). 108 | service(ontobee,(subClassOf(C,P))). 109 | 110 | ontobee_has_dbxref(C,CX):- 111 | service(ontobee,has_dbxref(C,CX1)), 112 | literal_atom(CX1,CX). 113 | ontobee_label(C,N):- 114 | service(ontobee,rdf(C,rdfs:label,N1)), 115 | !, 116 | literal_atom(N1,N). 117 | ontobee_label(_,''). 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/pubmedgraph.pl: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | ideally this is used in combination with ontobee 4 | 5 | */ 6 | 7 | :- module(pubmedgraph, 8 | [references/2, 9 | coreference/3, 10 | 11 | subclass_axiom_validation/3]). 12 | 13 | :- use_module(library(sparqlprog)). 14 | :- use_module(library(semweb/rdf11)). 15 | 16 | :- sparql_endpoint( pmg, 'https://stars-app.renci.org/pubmedgraph/sparql'). 17 | 18 | :- rdf_register_prefix(dcterm,'http://purl.org/dc/terms/'). 19 | :- rdf_register_prefix(pmid,'https://www.ncbi.nlm.nih.gov/pubmed/'). 20 | 21 | references(P,T) :- rdf(P,dcterm:references,T). 22 | coreference(P1,P2,T) :- references(P1,T),references(P2,T). 23 | 24 | subclass_axiom_validation(A,B,P) :- 25 | owl:subClassOf(A,B), 26 | pmg ?? (references(P,A),references(P,B)). 27 | 28 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/rhea.pl: -------------------------------------------------------------------------------- 1 | :- module(rhea, 2 | [ 3 | rhea_status/2, 4 | is_approved/1, 5 | is_transport/1, 6 | reaction/1, 7 | nondirectional_reaction/1, 8 | directional_reaction/1, 9 | bidirectional_reaction/1, 10 | directional_form/2, 11 | bidirectional_form/2, 12 | reaction_all_forms/4, 13 | directional_forms/3, 14 | reaction_form/3, 15 | rhea_xref/2, 16 | rhea_ec/2, 17 | rhea_xref_conflated/2, 18 | rhea_xref_skos/3, 19 | 20 | reaction_side/2, 21 | side_contains/2, 22 | 23 | reaction_participant/2, 24 | participant_compound/2, 25 | compound_chebi/2, 26 | participant_chebi/2, 27 | reaction_chebi_participant/2, 28 | 29 | rhea_equation/2 30 | 31 | ]). 32 | 33 | :- use_module(library(semweb/rdf11)). 34 | 35 | :- rdf_register_prefix(rh, 'http://rdf.rhea-db.org/'). 36 | :- rdf_register_prefix(chebi, 'http://purl.obolibrary.org/obo/CHEBI_'). 37 | 38 | :- rdf_meta status(r,r). 39 | rhea_status(A,B) :- rdf(A, rh:status, B). 40 | 41 | :- rdf_meta is_approved(r). 42 | is_approved(A) :- rhea_status(A,rh:'Approved'). 43 | 44 | is_transport(R) :- rdf(R,rh:isTransport,"true"^^xsd:boolean). 45 | 46 | :- rdf_meta reaction(r). 47 | :- rdf_meta nondirectional_reaction(r). 48 | :- rdf_meta directional_reaction(r). 49 | :- rdf_meta bidirectional_reaction(r). 50 | reaction(R) :- nondirectional_reaction(R) ; directional_reaction(R) ; bidirectional_reaction(R). 51 | nondirectional_reaction(R) :- rdf(R,rdfs:subClassOf,rh:'Reaction'). 52 | directional_reaction(R) :- rdf(R,rdfs:subClassOf,rh:'DirectionalReaction'). 53 | bidirectional_reaction(R) :- rdf(R,rdfs:subClassOf,rh:'BidirectionalReaction'). 54 | 55 | directional_form(R,DR) :- rdf(R,rh:directionalReaction,DR). 56 | bidirectional_form(R,DR) :- rdf(R,rh:bidirectionalReaction,DR). 57 | 58 | reaction_all_forms(R,D1,D2,BD) :- directional_form(R,D1),directional_form(R,D2),D1@= B,C =< E,snv_interpreted_condition(V,CL). 58 | 59 | %! frequency(F) is nondet 60 | % 61 | % F is instance of tgvo:Frequency 62 | frequency(F) :- rdf(F,rdf:type,tgvo:'Frequency'). 63 | 64 | %! dataset(D) is nondet 65 | % 66 | % single nucleotide variant V is included in dataset D 67 | dataset(V,D) :- rdf(V,tgvo:hasFrequency,N),rdf(N,rdfs:label,D). 68 | 69 | %! snv_frequency(?V,?D,?F) is nondet 70 | % 71 | % single nucleotide variant V in dataset D has frequency F 72 | snv_frequency(V,D,F) :- rdf(V,tgvo:hasFrequency,N),frequency(N),rdf(N,rdfs:label,D),rdf(N,tgvo:frequency,F). 73 | 74 | %! snv_japanese_frequency(?V,?RA,?AA,?D,?F) is nondet 75 | % 76 | % single nucleotide variant V with reference allele RA and 77 | % alternative allele AA in dataset D has frequency F 78 | % (example) 79 | % pl2sparql -u sparqlprog/ontologies/togovar -s togovar "snv_japanese_frequency(togovar:tgv1006196,"T","C",D,F)" "h(D,F)" 80 | % 81 | snv_japanese_frequency(V,RA,AA,D,F) :- snv(V),reference_allele(V,RA),alternative_allele(V,AA),snv_frequency(V,D,F). 82 | 83 | 84 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/ubergraph.pl: -------------------------------------------------------------------------------- 1 | :- module(ubergraph, 2 | [ 3 | rdf_ontology/3, 4 | rdf_closure/3, 5 | rdf_nr/3, 6 | rdf_redundant/3, 7 | 8 | metatype/2, 9 | 10 | anc/3, 11 | 12 | simj/3, 13 | simj/4, 14 | simj_e/3, 15 | simj_e/4, 16 | ancestor_intersection/3, 17 | ancestor_intersection/4, 18 | ancestor_union/3, 19 | ancestor_union/4, 20 | 21 | taxon/1, 22 | never_in_taxon/2, 23 | not_in_taxon_1/3, 24 | not_in_taxon_2/3, 25 | conservative_not_in_taxon/2, 26 | not_in_taxon/3, 27 | taxon_propagating_property/1 28 | ]). 29 | 30 | :- use_module(library(sparqlprog)). 31 | :- use_module(library(semweb/rdf11)). 32 | 33 | :- rdf_register_prefix(ubergraph,'http://reasoner.renci.org/'). 34 | :- rdf_register_prefix(obo,'http://purl.obolibrary.org/obo/'). 35 | :- rdf_register_prefix(biolink, 'https://w3id.org/biolink/vocab/'). 36 | :- rdf_register_prefix('NCBITaxon', 'http://purl.obolibrary.org/obo/NCBITaxon_'). 37 | :- rdf_register_prefix('UBERON', 'http://purl.obolibrary.org/obo/UBERON_'). 38 | 39 | 40 | rdf_ontology(S,P,O):- rdf(S,P,O,ubergraph:ontology). 41 | rdf_closure(S,P,O):- rdf(S,P,O,ubergraph:'ontology/closure'). 42 | rdf_nr(S,P,O):- rdf(S,P,O,ubergraph:nonredundant). 43 | rdf_redundant(S,P,O):- rdf(S,P,O,ubergraph:redundant). 44 | 45 | %% metatype(?Cls,?MetaCls) is nondet. 46 | % 47 | % e.g. metatype(C,biolink:'Disease') 48 | metatype(C,MC) :- 49 | rdf(MC,skos:exactMatch,M), 50 | subClassOf(C,M). 51 | 52 | %% anc(?Cls,+Rel,?Parent) is nondet 53 | % 54 | % reflexive ancestor over Rel 55 | % note that ubergraph does not materialize reflexive 56 | anc(C,R,P) :- rdf_path(C,R|rdfs:subClassOf,P). 57 | 58 | 59 | ancestor_intersection(C1,C2,Num) :- 60 | aggregate(count(distinct(P)),(subClassOf(C1,P),subClassOf(C2,P)),Num). 61 | ancestor_union(C1,C2,Num) :- 62 | aggregate(count(distinct(P)),(subClassOf(C1,P);subClassOf(C2,P)),Num). 63 | 64 | ancestor_intersection(C1,C2,R,Num) :- 65 | aggregate(count(distinct(P)),(anc(C1,R,P),anc(C2,R,P)),Num). 66 | ancestor_union(C1,C2,R,Num) :- 67 | aggregate(count(distinct(P)),(anc(C1,R,P);anc(C2,R,P)),Num). 68 | 69 | %% simj(+C1,C2,?S) 70 | %% simj(+C1,C2,+R,?S) 71 | % 72 | % Jaccard similarity (entirely in SPARQL) 73 | % 74 | simj(C1,C2,S) :- 75 | ancestor_intersection(C1,C2,AI), 76 | ancestor_union(C1,C2,AU), 77 | S is AI/AU. 78 | simj(C1,C2,R,S) :- 79 | ancestor_intersection(C1,C2,R,AI), 80 | ancestor_union(C1,C2,R,AU), 81 | S is AI/AU. 82 | 83 | %% simj_e(+C1,C2,?S) 84 | %% simj_e(+C1,C2,+R,?S) 85 | % 86 | % Jaccard similarity (mixed sparql/prolog) 87 | % 88 | % If R is specified then ancestor is calculated over this relation as well as subclass 89 | % 90 | % inner goals are evaluated using 2 sparql calls 91 | simj_e(C1,C2,S) :- 92 | ??(ubergraph, ancestor_intersection(C1,C2,AI)), 93 | ??(ubergraph, ancestor_union(C1,C2,AU)), 94 | bind(AI/AU,S). 95 | simj_e(C1,C2,R,S) :- 96 | ??(ubergraph, ancestor_intersection(C1,C2,R,AI)), 97 | ??(ubergraph, ancestor_union(C1,C2,R,AU)), 98 | bind(AI/AU,S). 99 | 100 | %% -- 101 | %% taxon constraints 102 | %% -- 103 | 104 | % NCBITaxon:4930 ! Saccharomyces 105 | % UBERON:0000955 ! brain 106 | 107 | taxon(T) :- subClassOf(T,'NCBITaxon':'1'). 108 | 109 | never_in_taxon(C,T) :- rdf(C,'http://purl.obolibrary.org/obo/RO_0002161',T). 110 | 111 | not_in_taxon_1(C,P,T) :- 112 | rdf(C,P,C1), 113 | in_taxon(C1,T1), 114 | \+ subClassOf(T,T1). 115 | not_in_taxon_2(C,P,T) :- 116 | rdf(C,P,C1), 117 | never_in_taxon(C1,T1), 118 | subClassOf(T,T1). 119 | 120 | not_in_taxon(C,P,T) :- not_in_taxon_1(C,P,T). 121 | not_in_taxon(C,P,T) :- not_in_taxon_2(C,P,T). 122 | 123 | conservative_not_in_taxon(C,T) :- 124 | not_in_taxon(C,P,T), 125 | taxon_propagating_property(P). 126 | 127 | taxon_propagating_property(rdfs:subClassOf). 128 | taxon_propagating_property(obo:'BFO_0000066'). 129 | taxon_propagating_property(obo:'BFO_0000051'). 130 | taxon_propagating_property(obo:'BFO_0000050'). 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /prolog/sparqlprog/ontologies/wikipathways.pl: -------------------------------------------------------------------------------- 1 | /** wrapper for wikipathways endpoint 2 | 3 | See https://www.wikipathways.org/index.php/Portal:Semantic_Web 4 | 5 | https://www.wikipathways.org/index.php/Help:WikiPathways_Sparql_queries 6 | 7 | https://www.wikipathways.org/index.php/Help:WikiPathways_Metabolomics 8 | 9 | */ 10 | 11 | :- module(wikipathways, 12 | [ 13 | interaction/1, 14 | pathway/1, 15 | metabolite/1, 16 | identifier/2, 17 | part_of/2, 18 | has_participant/2, 19 | organism/2, 20 | ontology_tag/2, 21 | 22 | pairwise_interaction/2, 23 | pairwise_interaction/3, 24 | 25 | pathway_version/2 26 | ]). 27 | 28 | :- use_module(library(sparqlprog/owl_types)). 29 | :- use_module(library(typedef)). 30 | 31 | :- sparql_endpoint( wikipathways, 'http://sparql.wikipathways.org'). 32 | 33 | :- rdf_register_prefix(dcterms,'http://purl.org/dc/terms/'). 34 | :- rdf_register_prefix(wp,'http://vocabularies.wikipathways.org/wp#'). 35 | :- rdf_register_prefix(pav,'http://purl.org/pav/'). 36 | :- rdf_register_prefix(ncbitaxon,'http://purl.obolibrary.org/obo/NCBITaxon_'). 37 | :- rdf_register_prefix(pw,'http://purl.obolibrary.org/obo/PW_'). 38 | 39 | :- type wp_pathway ---> atomic_iri. 40 | :- type wp_interaction ---> atomic_iri. 41 | 42 | 43 | interaction(X) :- rdf(X,rdf:type,wp:'Interaction'). 44 | pathway(X) :- rdf(X,rdf:type,wp:'Pathway'). 45 | metabolite(X) :- rdf(X,rdf:type,wp:'Metabolite'). 46 | identifier(P,Id) :- rdf(P,dcterms:identifier,Id). 47 | part_of(P,W) :- rdf(P,dcterms:isPartOf,W). 48 | has_participant(P,X) :- rdf(P,wp:participants,X). 49 | organism(P,O) :- rdf(P,wp:organism,O). 50 | ontology_tag(P,O) :- rdf(P,wp:ontologyTag,O). 51 | 52 | %! pairwise_interaction(?P1,?P2,?I) is nondet. 53 | %! pairwise_interaction(?P1,?P2) is nondet. 54 | pairwise_interaction(P1,P2) :- 55 | pairwise_interaction(P1,P2,_). 56 | pairwise_interaction(P1,P2,I) :- 57 | interaction(I), 58 | has_participant(I,P1), 59 | has_participant(I,P2). 60 | 61 | %! pathway_version(+P,?V) is nondet. 62 | % 63 | % note: this is in https://www.wikipathways.org/index.php/Help:WikiPathways_Metabolomics 64 | % but does not seem to be used. 65 | pathway_version(P,V) :- 66 | rdf(P,pav:version,V). 67 | -------------------------------------------------------------------------------- /prolog/sparqlprog/owl_types.pl: -------------------------------------------------------------------------------- 1 | :- module(owl_types, 2 | [ 3 | ]). 4 | 5 | :- use_module(library(typedef)). 6 | 7 | % ==================== 8 | % RDF TYPES 9 | % ==================== 10 | 11 | :- type iri_ref ---> curie ; atomic_iri. 12 | 13 | :- type atomic_iri == atom. 14 | :- type curie ---> iri_prefix : iri_local_part. 15 | :- type iri_prefix == atom. 16 | :- type iri_local_part == atom. 17 | 18 | :- type blank_node == atom. 19 | :- type rdf_resource ---> iri_ref ; blank_node. 20 | :- type rdf_resource_or_literal ---> rdf_resource ; rdf_literal. 21 | :- type rdf_literal ---> rdf_plain_literal ; rdf_lang_literal ; rdf_typed_literal. 22 | :- type rdf_plain_literal == string. 23 | :- type rdf_lang_literal ---> '@'(string,atom). % todo - consider enum 24 | :- type rdf_typed_literal ---> '^^'(string,xsd_type). 25 | :- type xsd_type ---> iri_ref. 26 | 27 | :- type rdf_triple ---> rdf(rdf_resource, iri_ref, rdf_resource_or_literal). 28 | :- type rdf_quad ---> rdf(rdf_resource, iri_ref, rdf_resource_or_literal, rdf_resource). 29 | :- type rdf_fact ---> rdf_triple ; rdf_quad. 30 | 31 | % ==================== 32 | % OWL TYPES 33 | % ==================== 34 | 35 | :- type owl_class_expression ---> owl_restriction ; owl_class. 36 | :- type owl_class ---> iri_ref. 37 | :- type owl_restriction ---> owl_some_values_from ; owl_all_values_from. 38 | :- type owl_some_values_from ---> some(owl_property_expression, owl_class_expression). 39 | :- type owl_all_values_from ---> some(owl_property_expression, owl_class_expression). 40 | 41 | :- type owl_property_expression ---> owl_restriction ; owl_property. 42 | :- type owl_property ---> iri_ref. 43 | 44 | 45 | -------------------------------------------------------------------------------- /prolog/sparqlprog/pathlang.pl: -------------------------------------------------------------------------------- 1 | :- module(pathlang, 2 | []). 3 | 4 | prim_goal(Pred, X, Y, Goal) :- 5 | var(Pred), 6 | !, 7 | Goal = rdf(X, PredIRI, Y). 8 | prim_goal(inverse(Pred), X, Y, Goal) :- 9 | !, 10 | prim_goal(Pred, Y, X, Goal). 11 | prim_goal(Pred, X, Y, Goal) :- 12 | rdf_global_id(Pred,PredIRI), 13 | rdf_is_iri(PredIRI), 14 | !, 15 | Goal = rdf(X, PredIRI, Y). 16 | prim_goal(Pred, X, Y, Goal) :- 17 | !, 18 | Goal =.. [Pred, X, Y]. 19 | 20 | eval( A -> B, Goal ) :- 21 | % assume head term is an object 22 | eval( A, B, Goal ). 23 | 24 | eval( A, B -> C, (Goal, Goal2) ) :- 25 | % e.g. A, has_friend -> lives_in ==> has_friend(A, Out), lives_in(Out, ...) 26 | \+ compound(B), 27 | !, 28 | prim_goal(B, A, Out, Goal), 29 | eval(Out, C, Goal2). 30 | 31 | eval( A, B -> C, (Goal, Goal2) ) :- 32 | % e.g. A, has_friend(Friend) -> lives_in ==> has_friend(A, Friend), lives_in(Friend, ...) 33 | compound(B), 34 | !, 35 | B =.. [Pred, Out], 36 | prim_goal(Pred, A, Out, Goal), 37 | eval(Out, C, Goal2). 38 | 39 | eval( A, (B,C), (Goal, Goal2) ) :- 40 | !, 41 | eval(A, B, Goal), 42 | eval(A, C, Goal2). 43 | 44 | eval( A, (B | C), (Goal ; Goal2) ) :- 45 | !, 46 | eval(A, B, Goal), 47 | eval(A, C, Goal2). 48 | 49 | 50 | -------------------------------------------------------------------------------- /prolog/sparqlprog/search_util.pl: -------------------------------------------------------------------------------- 1 | :- module(search_util, 2 | [ 3 | % must be exposed so it can be expanded in query rewrites 4 | label_or_synonym_pred_hook/1, 5 | 6 | lmatch/2, 7 | 8 | psearch/5, 9 | psearch/4, 10 | psearch/3, 11 | 12 | tsearch/6, 13 | tsearch/5, 14 | tsearch/4, 15 | tsearch/3, 16 | tsearch/2, 17 | 18 | lsearch/2, 19 | lsearch/3, 20 | lsearch/4, 21 | lsearch/5 22 | ]). 23 | 24 | :- use_module(library(semweb/rdf11)). 25 | :- use_module(library(semweb/rdfs)). 26 | :- use_module(library(sparqlprog/owl_util)). 27 | %:- use_module(library(sparqlprog/ontologies/owl), [label/2]). 28 | :- use_module(library(sparqlprog/emulate_builtins)). 29 | 30 | %! lmatch(+Pattern:atom, ?Object) is nondet 31 | % 32 | % 33 | % Uses swi-prolog rdf11 'like' matching: 34 | % - case insensitive 35 | % - '*' is wildcard 36 | % - exact match unless wildcards specified 37 | % 38 | % NOTE: this will NOT do a conversion to STR in SPARQL; e.g 39 | % SELECT ?x WHERE {FILTER (regex(?v0,"^cell$","i")) . ?x ?v0} 40 | lmatch(P,C) :- {like(L,P)},rdf(C,rdfs:label,L). 41 | 42 | 43 | %! lsearch(+Pattern, ?Object, ?Label, +Flags:str, ?Graph) is nondet. 44 | % 45 | % search for objects with an rdfs:label matching Pattern 46 | % 47 | % NOTE: this will do a conversion to STR in SPARQL; e.g 48 | % SELECT ?x WHERE {?x ?v0 . FILTER (regex(STR(?v0),"^cell$","i"))} 49 | lsearch(P,C,L,F,G) :- rdf(C,rdfs:label,L,G),regex(str(L),P,F). 50 | lsearch(P,C,L,F) :- label(C,L),regex(str(L),P,F). 51 | lsearch(P,C,L) :- label(C,L),regex(str(L),P,"i"). 52 | lsearch(P,C) :- label(C,L),regex(str(L),P,"i"). 53 | 54 | %! lsearch(+Pattern, ?Object, ?Pred, ?Label, +Flags:str) is nondet. 55 | % 56 | % search for objects with any property matching Pattern 57 | psearch(P,C,R,L,F) :- rdf(C,R,L),regex(str(L),P,F). 58 | psearch(P,C,R,L) :- rdf(C,R,L),regex(str(L),P,"i"). 59 | psearch(P,C,R) :- rdf(C,R,L),regex(str(L),P,"i"). 60 | 61 | %! tsearch(+Pattern, ?Object, ?Label, +Flags:str, ?Graph) is nondet. 62 | % 63 | % term search: find objects with an rdfs:label or synonym-like property matching Pattern 64 | tsearch(P,C,Pred,L,F,G) :- label_or_synonym_pred_hook(Pred), rdf(C,Pred,L,G),regex(str(L),P,F). 65 | tsearch(P,C,Pred,L,F) :- tsearch(P,C,Pred,L,F,_). 66 | tsearch(P,C,Pred,L) :- tsearch(P,C,Pred,L,"i",_). 67 | tsearch(P,C,Pred) :- tsearch(P,C,Pred,_,"i",_). 68 | tsearch(P,C) :- tsearch(P,C,_,_,"i",_). 69 | 70 | :- rdf_meta label_or_synonym_pred_hook(r). 71 | :- multifile label_or_synonym_pred_hook/1. 72 | label_or_synonym_pred_hook(rdfs:label). 73 | label_or_synonym_pred_hook('http://www.geneontology.org/formats/oboInOwl#hasRelatedSynonym'). 74 | label_or_synonym_pred_hook('http://www.geneontology.org/formats/oboInOwl#hasNarrowSynonym'). 75 | label_or_synonym_pred_hook('http://www.geneontology.org/formats/oboInOwl#hasBroadSynonym'). 76 | label_or_synonym_pred_hook('http://www.geneontology.org/formats/oboInOwl#hasExactSynonym'). 77 | 78 | -------------------------------------------------------------------------------- /prolog/sparqlprog/stacktrace.pl: -------------------------------------------------------------------------------- 1 | :- module(stacktrace,[]). 2 | user:prolog_exception_hook(_, 3 | _, _, _) :- 4 | backtrace(99), 5 | fail. 6 | -------------------------------------------------------------------------------- /prolog/sparqlprog/test_aux.pl: -------------------------------------------------------------------------------- 1 | /** test module 2 | 3 | This module is included only for running unit tests 4 | 5 | */ 6 | 7 | :- module(test_aux, 8 | [my_unary_pred/1, 9 | a_or_b/1, 10 | unify_with_iri/1, 11 | a/1, 12 | b/1, 13 | mammal/1, 14 | is_mammal/1, 15 | refl/2, 16 | recursive_subclass_of/2]). 17 | 18 | :- use_module(library(semweb/rdf11)). 19 | :- use_module(library(sparqlprog)). 20 | :- rdf_register_prefix('','http://example.org/'). 21 | 22 | %:- srule(my_unary_pred, [instance], 'demo predicate'). 23 | my_unary_pred(X) :- rdf(X,rdf:type,'':c1). 24 | my_unary_pred(foo). 25 | 26 | 27 | :- srule(recursive_subclass_of, [sub, super]). 28 | recursive_subclass_of(X,Y) :- rdf(X,rdfs:subClassOf,Y). 29 | recursive_subclass_of(X,Y) :- rdf(X,rdfs:subClassOf,Z),recursive_subclass_of(Z,Y). 30 | 31 | % NOTE: for this to be expanded, both a/1 and b/1 need to be exported 32 | a_or_b(X) :- a(X). 33 | a_or_b(X) :- b(X). 34 | 35 | a(X) :- rdf(X,rdf:type,'':a). 36 | b(X) :- rdf(X,rdf:type,'':b). 37 | 38 | unify_with_iri('http://x.org'). 39 | 40 | refl(A,B) :- rdf(A,'':r,B). 41 | refl(A,B) :- rdf(B,'':r,A). 42 | 43 | % awkward 44 | refl(A,B) :- a(A),B=A. 45 | 46 | %% TODO: allow this 47 | %% refl(A,A) :- a(A). 48 | 49 | mammal('':cat). 50 | mammal('':dog). 51 | 52 | is_mammal(X) :- mammal(T),rdf(X,rdf:type,T). 53 | 54 | -------------------------------------------------------------------------------- /release.sh: -------------------------------------------------------------------------------- 1 | # STEPS: 2 | # - update pack.pl 3 | # - commit and remember to push 4 | # - make a new release on GH 5 | # run below, modifying version 6 | pack-release.pl cmungall/sparqlprog `swipl -l pack.pl -g "version(V),writeln(V),halt."` 7 | -------------------------------------------------------------------------------- /tests/bigtests.pl: -------------------------------------------------------------------------------- 1 | :- load_files([ 2 | tests/tests, 3 | tests/dbpedia_test 4 | ], [ if(not_loaded) ]). 5 | -------------------------------------------------------------------------------- /tests/builtins_test.pl: -------------------------------------------------------------------------------- 1 | 2 | :- use_module(library(semweb/rdf11)). 3 | :- use_module(library(sparqlprog/emulate_builtins)). 4 | 5 | :- rdf_register_prefix(foaf,'http://xmlns.com/foaf/0.1/'). 6 | :- rdf_register_prefix('','http://example.org/'). 7 | 8 | :- begin_tests(bultins_test). 9 | 10 | 11 | 12 | % string test functions should accept as input: str, atom, lang-literal, str-literal 13 | test(str_starts1) :- assertion(str_starts("abc","ab")). 14 | test(str_starts2) :- assertion(str_starts("abc"@en,"ab")). 15 | test(str_starts3) :- assertion(str_starts("abc"^^xsd:string,"ab")). 16 | test(str_starts4) :- assertion(str_starts(abc,"ab")). 17 | test(str_starts5) :- assertion(str_starts(abc,ab)). 18 | test(str_starts6) :- assertion(str_starts("abc",ab)). 19 | 20 | test(str_ends) :- assertion(str_ends("abc","bc")). 21 | 22 | test(str_replace) :- assertion(str_replace("abc","ab","x","xc")). 23 | test(str_replace_eval) :- 24 | seval(str_replace("ABC DEF GHI"," ","X"),S), 25 | assertion(S == "ABCXDEFXGHI"). 26 | 27 | test(reflexive) :- bind("x",X), 28 | assertion( X=="x"). 29 | xxtest(nested) :- 30 | bind(lcase("X"), X), 31 | assertion( X=="x"). 32 | test(nested) :- bind(lcase(ucase(lcase("X"))), X), 33 | assertion( X=="x"). 34 | 35 | test(lcase1) :- assertion(lcase("ABC","abc")). 36 | test(lcase2) :- 37 | assertion(lcase("ABC",X)), 38 | assertion(X="abc"). 39 | test(lcase_eval) :- 40 | seval(lcase("AbC"),S), 41 | assertion(S == "abc"). 42 | 43 | test(seval1) :- 44 | assertion( seval(append([a,b],[c,d]),[a,b,c,d]) ). 45 | test(seval_nested) :- 46 | seval(lcase(str_replace("ABC DEF GHI"," ","X")),S), 47 | assertion(S == "abcxdefxghi"). 48 | 49 | test(arith1) :- 50 | assertion( seval(1+2,3) ). 51 | test(arith_nested) :- 52 | assertion( seval(1+(2*3),7) ). 53 | 54 | test(arith_types) :- 55 | One="1"^^xsd:integer, 56 | Two="2", 57 | assertion( seval(One+Two,3) ). 58 | 59 | test(agg) :- 60 | assertion( seval(count(append([a,b],[c])), 3) ). 61 | test(max) :- 62 | assertion( seval(agg_max(append([1,2],[3,5])), 5) ). 63 | 64 | test(agg_group) :- 65 | forall(aggregate_group(max(D),[C],entity_value(C,D),R), 66 | format('Max for ~w is ~w~n',[C,R])), 67 | findall(C-R, 68 | aggregate_group(max(D),[C],entity_value(C,D),R), 69 | L), 70 | true, 71 | assertion(L=[_,_,_]), 72 | assertion(member(a-3,L)), 73 | assertion(member(b-2,L)), 74 | assertion(member(c-4,L)). 75 | /* 76 | test(agg_group2) :- 77 | findall(sum(P,R), 78 | aggregate_group(sum(Sal),[P],sal(P,J,Sal),R), 79 | L), 80 | maplist(writeln,L), 81 | assertion(L=[_,_,_]). 82 | */ 83 | 84 | test(intersects) :- 85 | X=[a,b,c,d], 86 | Y=[c,d,e,f], 87 | assertion( seval(intersection(X,Y), [c,d]) ). 88 | test(n_intersects) :- 89 | X=[a,b,c,d], 90 | Y=[c,d,e,f], 91 | assertion( seval(count(intersection(X,Y)) / 2, 1) ). 92 | 93 | test(jac) :- 94 | X=[a,b,c,d], 95 | Y=[c,d,e,f,g,h], 96 | assertion( (seval(count(intersection(X,Y)) / count(union(X,Y)), N), 97 | N=0.25) 98 | ). 99 | 100 | entity_value(a,1). 101 | entity_value(a,2). 102 | entity_value(a,3). 103 | entity_value(b,1). 104 | entity_value(b,2). 105 | entity_value(c,4). 106 | 107 | sal(p1,j1,100). 108 | sal(p1,j2,200). 109 | 110 | sal(p2,j1,200). 111 | sal(p2,j2,400). 112 | 113 | sal(p3,j3,800). 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | :- end_tests(bultins_test). 123 | -------------------------------------------------------------------------------- /tests/dataframe_test.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | */ 4 | 5 | :- use_module(library(sparqlprog)). 6 | :- use_module(library(semweb/rdf11)). 7 | :- use_module(library(sparqlprog/dataframe)). 8 | 9 | :- begin_tests(dataframe, 10 | []). 11 | 12 | person(p1,joe). 13 | person(p2,fred). 14 | person(p3,sue). 15 | 16 | owns(p3,item1). 17 | owns(p3,item2). 18 | 19 | address(p1,foo_st,sf). 20 | address(p1,bar_st,boston). 21 | address(p2,bar_st,boston). 22 | 23 | dataframe:dataframe(person, 24 | [ 25 | [person=ID, 26 | name=Name]-person(ID,Name), 27 | [street=Street, 28 | city=City]-address(ID,Street,City), 29 | [owns=Item]-owns(ID,Item) 30 | ], 31 | [ 32 | entity(person), 33 | entity(owns) 34 | ]). 35 | 36 | 37 | 38 | test(df) :- 39 | rdf_assert(p1,rdfs:label,"foo"), 40 | rdf_assert(item1,rdfs:label,"fridge"), 41 | rdf_assert(item2,rdfs:label,"spork"), 42 | writeln('HEADER'), 43 | forall(dataframe_header(person,Hdr), 44 | writeln(Hdr)), 45 | writeln('ROWS'), 46 | forall(dataframe_row(person,Row), 47 | format('~q.~n',[Row])), 48 | assertion(dataframe_header(person,[person,'person label',name,street,city,owns,'owns label'])), 49 | assertion((dataframe_row(person,Row), 50 | Row=[p1,foo,joe,'bar_st|foo_st','boston|sf',_,_])), 51 | writeln('FRAME'), 52 | dataframe_to_csv(person,[]). 53 | 54 | 55 | 56 | 57 | 58 | :- end_tests(dataframe). 59 | 60 | 61 | -------------------------------------------------------------------------------- /tests/dbpedia_test.pl: -------------------------------------------------------------------------------- 1 | 2 | :- use_module(library(semweb/rdf11)). 3 | :- use_module(library(sparqlprog)). 4 | :- use_module(library(sparqlprog/ontologies/dbpedia)). 5 | 6 | :- begin_tests(dbpedia_test). 7 | 8 | dbp_artist_child_directed(Artist,Child,Movie) :- 9 | dbp ?? musical_artist(Artist),has_child(Artist,Child),directed(Child,Movie). 10 | 11 | test_q(Q,Expected) :- 12 | findall(Q,Q,Results), 13 | maplist(writeln,Results), 14 | assertion( member(Expected, Results) ). 15 | 16 | 17 | :- debug(sparqlprog). 18 | 19 | test(films_directed_by_musicians_children) :- 20 | test_q(dbp_artist_child_directed(_,_,_), 21 | dbp_artist_child_directed('http://dbpedia.org/resource/David_Bowie', 22 | 'http://dbpedia.org/resource/Duncan_Jones', 23 | 'http://dbpedia.org/resource/Moon_(film)')). 24 | 25 | 26 | :- end_tests(dbpedia_test). 27 | 28 | 29 | -------------------------------------------------------------------------------- /tests/gocam_test.pl: -------------------------------------------------------------------------------- 1 | 2 | :- use_module(library(semweb/rdf11)). 3 | :- use_module(library(sparqlprog)). 4 | :- use_module(library(sparqlprog/endpoints)). 5 | :- use_module(library(obo_ro/ro)). 6 | :- use_module(library(sparqlprog/ontologies/gocam)). 7 | :- use_module(library(obo_core/goslim)). 8 | 9 | :- begin_tests(gocam_test). 10 | 11 | test_q(Q,Expected) :- 12 | findall(Q,(Q),Results), 13 | maplist(writeln,Results), 14 | assertion( member(Expected, Results) ). 15 | 16 | 17 | :- debug(sparqlprog). 18 | 19 | /* 20 | k2k(A1,A2,G1C,G2C) :- 21 | go ?? ( 22 | kinase_activity(A1), 23 | enabled_by(A1,G1), 24 | part_of(A1,A2), 25 | signal_transduction(A2), 26 | enabled_by(A1,G2), 27 | rdf(G1,rdf:type,G1C), 28 | rdf(G2,rdf:type,G2C) 29 | ). 30 | */ 31 | 32 | k2k(A1,A2,G1C,G2C) :- 33 | go ?? ( 34 | kinase_activity(A1), 35 | kinase_activity(A2), 36 | enabled_by(A1,G1), 37 | rdf(A1,R,A2), 38 | enabled_by(A1,G2), 39 | rdf(G1,rdf:type,G1C), 40 | rdf(G2,rdf:type,G2C) 41 | ). 42 | 43 | 44 | 45 | test(kinase) :- 46 | test_q(k2k(_,_,_,_), 47 | _). 48 | 49 | 50 | :- end_tests(gocam_test). 51 | 52 | 53 | -------------------------------------------------------------------------------- /tests/graph_util_test.pl: -------------------------------------------------------------------------------- 1 | 2 | :- use_module(library(semweb/rdf11)). 3 | :- use_module(library(semweb/rdf_zlib_plugin)). 4 | :- use_module(library(sparqlprog)). 5 | :- use_module(library(sparqlprog/graph_util)). 6 | :- use_module(library(sparqlprog/search_util)). 7 | 8 | :- use_module(library(http/json)). 9 | 10 | :- rdf_register_prefix('GO','http://purl.obolibrary.org/obo/GO_'). 11 | :- rdf_register_prefix('BFO','http://purl.obolibrary.org/obo/BFO_'). 12 | 13 | 14 | :- begin_tests(graph_util_test, 15 | [setup(load_test_file), 16 | cleanup(rdf_retractall(_,_,_,_))]). 17 | 18 | load_test_file :- 19 | rdf_load('examples/data/goslim_generic.ttl.gz'). 20 | 21 | test(basic) :- 22 | Es=[edge(a,b,is_a), edge(b,c,is_a)], 23 | edges_to_dict(Es,Dict), 24 | nl, 25 | json_write_dict(current_output,Dict), 26 | edges_to_dotfile(Es,'/tmp/basic.dot'), 27 | !. 28 | 29 | test(subgraph) :- 30 | lmatch('cell',C1), 31 | %lmatch('golgi apparatus',C2), 32 | extract_subgraph([C1],Edges,[direction(down)]), 33 | edges_to_dict(Edges,Dict), 34 | json_write_dict(current_output,Dict), 35 | edges_to_dotfile(Edges,'/tmp/subgraph.dot'), 36 | edges_to_imagefile(Edges,'/tmp/subgraph.png', png), 37 | !. 38 | 39 | 40 | 41 | 42 | 43 | :- end_tests(graph_util_test). 44 | 45 | 46 | -------------------------------------------------------------------------------- /tests/integration_tests.pl: -------------------------------------------------------------------------------- 1 | 2 | :- load_files([ 3 | tests/dbpedia_test 4 | ], [ if(not_loaded) ]). 5 | -------------------------------------------------------------------------------- /tests/labelutils_test.pl: -------------------------------------------------------------------------------- 1 | 2 | :- use_module(library(semweb/rdf11)). 3 | :- use_module(library(sparqlprog/labelutils)). 4 | 5 | :- rdf_register_prefix('GO','http://purl.obolibrary.org/obo/GO_'). 6 | :- rdf_register_prefix('BFO','http://purl.obolibrary.org/obo/BFO_'). 7 | :- rdf_register_prefix(oboInOwl,'http://www.geneontology.org/formats/oboInOwl#'). 8 | 9 | 10 | :- begin_tests(labelutils_test, 11 | [setup(load_test_file), 12 | cleanup(rdf_retractall(_,_,_,_))]). 13 | 14 | load_test_file :- 15 | % load into a test-specific graph due to cache issue 16 | rdf_load('tests/go_nucleus.ttl',[cache(false), 17 | graph(labelutils)]). 18 | 19 | test(show) :- 20 | T=rdf(_,_,_), 21 | forall(T, 22 | ( row_labelify(T,X), 23 | writeln(X))). 24 | 25 | test(labelify) :- 26 | row_labelify(foo('http://purl.obolibrary.org/obo/GO_0044464',bar),Row), 27 | assertion(Row = foo('GO:0044464','cell part',bar,'')). 28 | test(labelify_list) :- 29 | row_labelify(foo(x,['http://purl.obolibrary.org/obo/GO_0044464',y]), Row), 30 | assertion(Row = foo(x,'','GO:0044464|y','cell part|')). 31 | 32 | 33 | 34 | 35 | :- end_tests(labelutils_test). 36 | 37 | 38 | -------------------------------------------------------------------------------- /tests/localsparql_test.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | tests direct execution on local triplestore 4 | 5 | make bg-run 6 | 7 | */ 8 | 9 | :- use_module(library(semweb/rdf11)). 10 | :- use_module(library(sparqlprog)). 11 | :- use_module(library(rdf_owl)). 12 | 13 | :- use_module(library(sparqlprog)). 14 | :- use_module(library(sparqlprog/endpoints)). 15 | 16 | %:- table ??/2. 17 | 18 | 19 | count(X,G,N) :- 20 | ( setof(X,(local ?? G),Xs) 21 | -> length(Xs,N) 22 | ; N=0). 23 | 24 | jaccard(X,Y,N) :- 25 | count(A,(rdfs_subclass_of(X,A),rdfs_subclass_of(Y,A)),N1), 26 | count(A,(rdfs_subclass_of(X,A);rdfs_subclass_of(Y,A)),N2), 27 | N is N1/N2. 28 | 29 | 30 | :- begin_tests(localsparql_test, 31 | []). 32 | 33 | wl(L) :- 34 | forall(member(X,L), 35 | format('~w~n', [X])). 36 | 37 | run_test_query(N,X,G,L,L1) :- 38 | setof(X,(local ?? G),L1), 39 | format('** ~w~n',[N]), 40 | wl(L1), 41 | assertion(L = L1). 42 | 43 | test(direct_subclass_of) :- 44 | run_test_query(direct,X, 45 | ( label(C,"nucleus"), 46 | rdf(C,rdfs:subClassOf,X)),[_,_],_). 47 | 48 | test(inferred_subclass_of) :- 49 | run_test_query(inferred,X, 50 | ( label(C,"nucleus"), 51 | rdfs_subclass_of(C,X)),[_,_,_],_). 52 | 53 | test(mrca) :- 54 | run_test_query(mrca_check,t(X,Y,A), 55 | ( label(X,"protein complex assembly"), 56 | label(Y,"macromolecular complex assembly"), 57 | label(A,"cellular component assembly"), 58 | mrca(X,Y,A)), 59 | [_],_). 60 | 61 | test(jaccard) :- 62 | forall((local ?? owl:class(X),owl:class(Y)), 63 | ( jaccard(X,Y,S), 64 | format('~w <-> ~w = ~w~n',[X,Y,S]))). 65 | 66 | 67 | 68 | 69 | 70 | :- end_tests(localsparql_test). 71 | 72 | 73 | -------------------------------------------------------------------------------- /tests/nlp_util_table_test.pl: -------------------------------------------------------------------------------- 1 | /* * -*- Mode: Prolog -*- */ 2 | 3 | :- use_module(library(semweb/rdf11)). 4 | :- use_module(library(sparqlprog)). 5 | :- use_module(library(sparqlprog/emulate_builtins)). 6 | :- use_module(library(sparqlprog/nlp_util)). 7 | :- use_module(library(sparqlprog/nlp_util/memoize)). 8 | :- use_module(library(sparqlprog/owl_util)). 9 | :- use_module(library(sparqlprog/ontologies/owl)). 10 | 11 | :- use_module(library(tabling)). 12 | 13 | 14 | 15 | :- begin_tests(nlp_util_table_test, 16 | [setup(load_test_file), 17 | cleanup(rdf_retractall(_,_,_,_))]). 18 | 19 | load_test_file :- 20 | rdf_load('tests/lexmap_test.ttl'). 21 | 22 | test(normalize) :- 23 | forall(rdf_nliteral(X,P,V), 24 | format('~w ~w ~w~n',[X,P,V])). 25 | 26 | test(shar) :- 27 | G = shares_literal(_A,_B,_PA,_PB,_V,_F), 28 | forall(G, 29 | format(':: ~w~n',[G])). 30 | 31 | 32 | :- end_tests(nlp_util_table_test). 33 | -------------------------------------------------------------------------------- /tests/nlp_util_test.pl: -------------------------------------------------------------------------------- 1 | /* * -*- Mode: Prolog -*- */ 2 | 3 | :- use_module(library(semweb/rdf11)). 4 | :- use_module(library(sparqlprog)). 5 | :- use_module(library(sparqlprog/emulate_builtins)). 6 | :- use_module(library(sparqlprog/nlp_util)). 7 | :- use_module(library(sparqlprog/owl_util)). 8 | :- use_module(library(sparqlprog/ontologies/owl)). 9 | 10 | :- rdf_register_ns(a, 'http://purl.obolibrary.org/obo/A_'). 11 | :- rdf_register_ns(b, 'http://purl.obolibrary.org/obo/B_'). 12 | :- rdf_register_ns(x, 'http://purl.obolibrary.org/obo/X_'). 13 | :- rdf_register_ns(y, 'http://purl.obolibrary.org/obo/Y_'). 14 | :- rdf_register_ns(z, 'http://purl.obolibrary.org/obo/Z_'). 15 | 16 | 17 | 18 | :- begin_tests(nlp_util_test, 19 | [setup(load_test_file), 20 | cleanup(rdf_retractall(_,_,_,_))]). 21 | 22 | load_test_file :- 23 | rdf_load('tests/lexmap_test.ttl'). 24 | 25 | test(normalize) :- 26 | forall(rdf_nliteral(X,P,V), 27 | format('~w ~w ~w~n',[X,P,V])). 28 | 29 | test(shares_literal) :- 30 | G = shares_literal(_A,_B,_PA,_PB,_V,_F), 31 | forall(G, 32 | format(':: ~w~n',[G])). 33 | 34 | test(match) :- 35 | G = entity_match(_A,_B,_PA,_PB,_V,_F), 36 | forall(G, 37 | format(':: ~w~n',[G])), 38 | rdf_global_id(z:'1',Z1), 39 | rdf_global_id(x:'1',X1), 40 | rdf_global_id(x:'2',X2), 41 | rdf_global_id(y:'2',Y2), 42 | rdf_global_id(z:'1',Z1), 43 | % case-insensitivity 44 | assertion( entity_match(X2,Y2,_,_,_,_) ), 45 | assertion( entity_match(X1,Z1,_,_,_,_) ), 46 | assertion( entity_match(Z1,X1,_,_,_,_) ). 47 | 48 | 49 | test(new_match) :- 50 | G = new_entity_match(_A,_B,_PA,_PB,_V,_F), 51 | forall(G, 52 | format(':: ~w~n',[G])), 53 | rdf_global_id(x:'1',X1), 54 | rdf_global_id(x:'2',X2), 55 | rdf_global_id(y:'2',Y2), 56 | rdf_global_id(z:'1',Z1), 57 | assertion( new_entity_match(X2,Y2,_,_,_,_) ), 58 | assertion( \+ new_entity_match(X1,Z1,_,_,_,_) ), 59 | assertion( \+ new_entity_match(Z1,X1,_,_,_,_) ). 60 | 61 | 62 | 63 | 64 | :- end_tests(nlp_util_test). 65 | -------------------------------------------------------------------------------- /tests/obo_util_test.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | tests direct execution on obo_util in-memory triples 4 | 5 | */ 6 | 7 | :- use_module(library(semweb/rdf11)). 8 | :- use_module(library(sparqlprog)). 9 | :- use_module(library(sparqlprog/emulate_builtins)). 10 | :- use_module(library(sparqlprog/obo_util)). 11 | :- use_module(library(sparqlprog/owl_util), []). 12 | :- use_module(library(sparqlprog/labelutils)). 13 | 14 | :- rdf_register_prefix(oboInOwl,'http://www.geneontology.org/formats/oboInOwl#'). 15 | :- rdf_register_prefix('GO','http://purl.obolibrary.org/obo/GO_'). 16 | :- rdf_register_prefix(part_of,'http://purl.obolibrary.org/obo/BFO_0000050'). 17 | foo('0'). 18 | 19 | :- begin_tests(obo_util_test, 20 | [setup(load_test_file), 21 | cleanup(rdf_retractall(_,_,_,_))]). 22 | 23 | load_test_file :- 24 | % load into a test-specific graph due to cache issue 25 | rdf_load('tests/go_nucleus.ttl',[cache(false), 26 | graph(obo_util)]). 27 | 28 | 29 | run_test_query(N,X,G,L,L1) :- 30 | setof(X,G,L1), 31 | format('** ~w~n',[N]), 32 | wl(L1), 33 | assertion(L = L1). 34 | 35 | wl(L) :- 36 | forall((member(X,L),term_labelify(X,L1)), 37 | writeln(L1)). 38 | 39 | 40 | test(def) :- 41 | forall(entity_def_xrefs(E,D,Xs), 42 | writeln(e(E,D,Xs))). 43 | 44 | test(gen) :- 45 | nl, 46 | nl, 47 | gen_obo(current_output,[]). 48 | 49 | 50 | 51 | :- end_tests(obo_util_test). 52 | 53 | 54 | -------------------------------------------------------------------------------- /tests/owl_test.pl: -------------------------------------------------------------------------------- 1 | 2 | :- use_module(library(semweb/rdf11)). 3 | :- use_module(library(sparqlprog)). 4 | :- use_module(library(sparqlprog/owl_util)). 5 | :- use_module(library(sparqlprog/ontologies/owl)). 6 | 7 | :- begin_tests(owl_test). 8 | 9 | 10 | test_select(Q,ExpectedSPARQL) :- 11 | create_sparql_select(Q,SPARQL,[]), 12 | format(' ~q ==> ~w~n',[ Q, SPARQL ]), 13 | assertion( SPARQL = ExpectedSPARQL ). 14 | 15 | test(subClassOf) :- 16 | test_select( subClassOf(_,_), "SELECT ?v0 ?v1 WHERE {?v0 ?v1}"). 17 | 18 | test(subClassOf_some) :- 19 | test_select( subclass_of_some(_,_,_), "SELECT ?v0 ?v1 ?v2 WHERE {?v0 ?v3 . ?v3 ?v1 . ?v3 ?v2}"). 20 | 21 | 22 | :- end_tests(owl_test). 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/owl_util_test.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | tests direct execution on owl_util in-memory triples 4 | 5 | */ 6 | 7 | :- use_module(library(semweb/rdf11)). 8 | :- use_module(library(sparqlprog)). 9 | :- use_module(library(sparqlprog/emulate_builtins)). 10 | :- use_module(library(sparqlprog/owl_util)). 11 | :- use_module(library(sparqlprog/labelutils)). 12 | :- use_module(library(sparqlprog/ontologies/owl)). 13 | 14 | :- rdf_register_prefix(oboInOwl,'http://www.geneontology.org/formats/oboInOwl#'). 15 | :- rdf_register_prefix('BFO','http://purl.obolibrary.org/obo/BFO_'). 16 | :- rdf_register_prefix(obo,'http://purl.obolibrary.org/obo/'). 17 | :- rdf_register_prefix(part_of,'http://purl.obolibrary.org/obo/BFO_0000050'). 18 | foo('0'). 19 | 20 | :- begin_tests(owl_util_test, 21 | [setup(load_test_file), 22 | cleanup(rdf_retractall(_,_,_,_))]). 23 | 24 | load_test_file :- 25 | % load into a test-specific graph due to cache issue 26 | rdf_load('tests/go_nucleus.ttl',[cache(false), 27 | graph(owl_util)]). 28 | 29 | 30 | run_test_query(N,X,G,L,L1) :- 31 | setof(X,G,L1), 32 | format('** ~w~n',[N]), 33 | wl(L1), 34 | assertion(L = L1). 35 | 36 | wl(L) :- 37 | forall((member(X,L),term_labelify(X,L1)), 38 | writeln(L1)). 39 | 40 | test(curie) :- 41 | URI='http://purl.obolibrary.org/obo/BFO_123', 42 | setof(Pre:Post,rdf_global_id(Pre:Post,URI),L), 43 | assertion( L=['BFO':'123'] ), 44 | ensure_curie(URI, CURIE), 45 | assertion( CURIE = 'BFO:123' ), 46 | rdf_global_id(part_of:'',PART_OF), 47 | ensure_curie(PART_OF, CURIE2), 48 | assertion( CURIE2 = 'part_of' ). 49 | 50 | test(prefix) :- 51 | G=subsumed_prefix_namespace(_,_,_,_), 52 | setof(G,G,Gs), 53 | assertion(Gs=[_,_,_]), 54 | assertion( member(subsumed_prefix_namespace(obo,_,'BFO',_),Gs) ). 55 | 56 | 57 | 58 | 59 | 60 | test(owl_edge) :- 61 | rdf_global_id(part_of:'',PART_OF), 62 | label_of('intracellular part',INTRACELLULAR_PART), 63 | label_of('intracellular',INTRACELLULAR), 64 | label_of('cell part',CELL_PART), 65 | label_of('cell',CELL), 66 | rdf_global_id(oboInOwl:hasExactSynonym,SYN), 67 | run_test_query(edge, 68 | part_of(S,O), 69 | owl_edge(S,PART_OF,O),[part_of(INTRACELLULAR_PART,INTRACELLULAR), 70 | part_of(CELL_PART,CELL)],_), 71 | run_test_query(axiom_annotations, 72 | a(P,V), 73 | triple_axiom_annotation(CELL_PART,SYN,_,P,V), 74 | [a('http://www.geneontology.org/formats/oboInOwl#hasDbXref',_)], 75 | _). 76 | 77 | 78 | 79 | :- end_tests(owl_util_test). 80 | 81 | 82 | -------------------------------------------------------------------------------- /tests/prolog_test.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | tests direct execution on prolog in-memory triples 4 | 5 | */ 6 | 7 | :- use_module(library(semweb/rdf11)). 8 | :- use_module(library(sparqlprog)). 9 | :- use_module(library(sparqlprog/emulate_builtins)). 10 | :- use_module(library(sparqlprog/owl_util)). 11 | :- use_module(library(sparqlprog/ontologies/owl)). 12 | 13 | % run docker 14 | %:- sparql_endpoint( local, 'http://127.0.0.1:8889/bigdata/sparql'). 15 | 16 | :- begin_tests(prolog_test, 17 | [setup(load_test_file), 18 | cleanup(rdf_retractall(_,_,_,_))]). 19 | 20 | load_test_file :- 21 | % load into a test-specific graph due to cache issue 22 | rdf_load('tests/go_nucleus.ttl',[cache(false), 23 | graph(prolog)]). 24 | 25 | 26 | run_test_query(N,X,G,L,L1) :- 27 | setof(X,G,L1), 28 | format('** ~w~n',[N]), 29 | wl(L1), 30 | assertion(L = L1). 31 | 32 | test(direct_subclass_of) :- 33 | label(C,"nucleus"), 34 | setof(D,subClassOf(C,D),Ds), 35 | assertion(Ds = [Parent]), 36 | assertion(label(Parent, "intracellular membrane-bounded organelle")). 37 | 38 | cls_label_ends_with(C,M) :- 39 | class(C),label(C,Label),str_ends(Label,M). 40 | cls_label_starts_with(C,M) :- 41 | class(C),label(C,Label),str_starts(Label,M). 42 | 43 | test(str_starts) :- 44 | setof(C,cls_label_starts_with(C,"cell"),Cs), 45 | assertion(Cs = [_,_,_]), 46 | assertion( (member(C,Cs),label(C,"cell")) ), 47 | assertion( (member(C,Cs),label(C,"cell part")) ). 48 | 49 | test(str_ends) :- 50 | setof(C,cls_label_ends_with(C,"organelle"),Cs), 51 | assertion(Cs = [_,_,_,_]), 52 | assertion( (member(C,Cs),label(C,"organelle")) ), 53 | assertion( (member(C,Cs),label(C,"intracellular membrane-bounded organelle")) ). 54 | 55 | has_dbxref(I,J):-rdf(I,'http://www.geneontology.org/formats/oboInOwl#hasDbXref',J). 56 | 57 | has_dbxref_with_prefix(C,X,P) :- has_dbxref(C,X),str_before(X,":",P). 58 | has_dbxref_with_prefix(C,P) :- has_dbxref_with_prefix(C,_,P). 59 | 60 | test(str_before) :- 61 | % test with argument bound 62 | setof(C,has_dbxref_with_prefix(C,"Wikipedia"),Cs), 63 | assertion(Cs = [_,_,_,_]), 64 | member(C,Cs), 65 | label(C,"organelle"), 66 | % test with argument not-bound 67 | setof(P,has_dbxref_with_prefix(C,P),Ps), 68 | assertion( Ps = ["NIF_Subcellular","Wikipedia"] ). 69 | 70 | wl(L) :- 71 | forall((member(X,L),optional(label(X,N))), 72 | format('~w ~w~n', [X,N])). 73 | 74 | 75 | %test(z) :- 76 | % label(C,"intracellular"), 77 | % run_test_query(foo,D,rdf(_,_,_),_,_). 78 | 79 | 80 | test(path) :- 81 | label(C,"intracellular"), 82 | run_test_query(onePlus,D,rdf_path(C,oneOrMore(rdfs:subClassOf),D),[_,_,_],_), 83 | run_test_query(zeroPlus,D,rdf_path(C,zeroOrMore(rdfs:subClassOf),D),[_,_,_,_],Lz), 84 | assertion(member(C,Lz)), 85 | label(C2,"intracellular part"), 86 | run_test_query(inv,D,rdf_path(C2,inverseOf(rdfs:subClassOf),D),[_],_), 87 | run_test_query(or,D,rdf_path(C2,(rdfs:subClassOf|inverseOf(rdfs:subClassOf)),D),[_,_,_],_), 88 | run_test_query(all,D,rdf_path(C2,zeroOrMore((rdfs:subClassOf)|(\(rdfs:subClassOf))),D),[_,_,_,_],_), 89 | true. 90 | 91 | 92 | :- end_tests(prolog_test). 93 | 94 | 95 | -------------------------------------------------------------------------------- /tests/search_test.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | tests search on prolog in-memory triples / builtin emulation 4 | 5 | */ 6 | 7 | :- use_module(library(semweb/rdf11)). 8 | :- use_module(library(sparqlprog)). 9 | :- use_module(library(sparqlprog/emulate_builtins)). 10 | :- use_module(library(sparqlprog/search_util)). 11 | :- use_module(library(sparqlprog/ontologies/owl)). 12 | 13 | :- begin_tests(search_test, 14 | [setup(load_test_file), 15 | cleanup(rdf_retractall(_,_,_,_))]). 16 | 17 | 18 | load_test_file :- 19 | % load into a test-specific graph due to cache issue 20 | rdf_load('tests/go_nucleus.ttl',[cache(false), graph(search)]). 21 | 22 | 23 | test_count(Goal,ExpectedCount) :- 24 | writeln(t=Goal), 25 | findall(Goal,Goal,L), 26 | maplist(writeln,L), 27 | length(L,Len), 28 | assertion(Len == ExpectedCount). 29 | 30 | test_once(Goal,Test) :- 31 | findall(Goal,(Goal,Test),L), 32 | assertion(L = [_]). 33 | 34 | 35 | test(contains) :- 36 | test_count(lsearch(cell,_,_),7), 37 | test_once(lsearch(cell,C,_), C='http://purl.obolibrary.org/obo/GO_0005622'). 38 | 39 | test(starts_regex) :- 40 | test_count(lsearch('^cell',_,_),3), 41 | test_once(lsearch('^cell',C,_), C='http://purl.obolibrary.org/obo/GO_0005623'). 42 | 43 | test(regex) :- 44 | RE = 'membrane[\\-|\\s]bounded', 45 | test_count(lsearch(RE,_,_),2), 46 | test_once(lsearch(RE,C,_), C='http://purl.obolibrary.org/obo/GO_0043231'). 47 | 48 | test(ends) :- 49 | test_count(lsearch('cell$',_,_),1), 50 | test_once(lsearch('cell$',C,_), C='http://purl.obolibrary.org/obo/GO_0005623'). 51 | 52 | test(wildcard) :- 53 | test_count(lsearch('cell.*component',_,_),1). 54 | 55 | test(synonym) :- 56 | test_count(tsearch('protoplast',_),2). 57 | 58 | 59 | 60 | 61 | 62 | :- end_tests(search_test). 63 | 64 | 65 | -------------------------------------------------------------------------------- /tests/search_viz_test.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | tests search on prolog in-memory triples / builtin emulation 4 | 5 | */ 6 | 7 | :- use_module(library(semweb/rdf11)). 8 | :- use_module(library(semweb/turtle)). 9 | :- use_module(library(sparqlprog)). 10 | :- use_module(library(sparqlprog/emulate_builtins)). 11 | :- use_module(library(sparqlprog/search_util)). 12 | :- use_module(library(sparqlprog/ontologies/owl)). 13 | :- use_module(library(sparqlprog/owl_search_viz)). 14 | 15 | :- rdf_register_prefix('GO', 'http://purl.obolibrary.org/obo/GO_'). 16 | 17 | :- begin_tests(search_viz_test, 18 | [setup(load_test_file), 19 | cleanup(rdf_retractall(_,_,_,_))]). 20 | 21 | 22 | load_test_file :- 23 | rdf_load('tests/go_nucleus.ttl'). 24 | 25 | test_output(S,Opts,Expecteds) :- 26 | test_output(S,Opts,Expecteds,[]). 27 | test_output(S,Opts,Expecteds,Unexpecteds) :- 28 | F='tests/out.tmp', 29 | format('Search: ~w Opts: ~w~n',[S,Opts]), 30 | owl_search_and_display(S,[output(F)|Opts]), 31 | read_file_to_string(F,Str,[]), 32 | atom_string(A,Str), 33 | writeln(A), 34 | forall(member(E,Expecteds), 35 | assertion(sub_atom(A,_,_,_,E))), 36 | forall(member(E,Unexpecteds), 37 | assertion(\+sub_atom(A,_,_,_,E))), 38 | !. 39 | 40 | 41 | 42 | 43 | % TODO: compare output to expected 44 | 45 | test(basic) :- 46 | test_output([cell],[],['intracellular membrane-bounded organelle'],[nucleus]). 47 | test(exact) :- 48 | test_output(['^nucleus$'],[],[nucleus],[cell]). 49 | test(obo) :- 50 | test_output(['^nucleus$'], 51 | [format(obo)], 52 | ['name: nucleus', 53 | 'A membrane-bounded organelle of eukaryotic cells'], 54 | ['name: cell']). 55 | test(superclasses) :- 56 | test_output(['^nucleus$'],[relations([s])], 57 | [nucleus, 'intracellular membrane-bounded organelle'], 58 | ['0005623']). 59 | test(ancs) :- 60 | test_output(['^nucleus$'],[relations([s,'part_of'])], 61 | [nucleus, 62 | '0005623 ! cell', 63 | 'intracellular membrane-bounded organelle'], 64 | []). 65 | test(subclasses) :- 66 | test_output(['^organelle$'],[relations([s]), extend_lambda(d)], 67 | [nucleus, 'intracellular membrane-bounded organelle'], 68 | ['0005623']). 69 | 70 | test(dot) :- 71 | ( shell('og2dot.js -h') 72 | -> test_output(['^nucleus$'],[format(dot),relations([s])], 73 | ['label=cellular_component'], 74 | []) 75 | ; writeln('Skipping test, as og2dot not installed')). 76 | 77 | test(id) :- 78 | test_output(['GO:0005634'],[search_property(id)], 79 | ['GO:0005634', nucleus], 80 | [cell]). 81 | 82 | 83 | %owl_search_and_display(nucleus, a, '.', [], obo, _). 84 | xtest(basic2) :- 85 | owl_search_and_display('^nucleus$', '.', '.', [], obo, _). 86 | 87 | :- end_tests(search_viz_test). 88 | 89 | 90 | -------------------------------------------------------------------------------- /tests/search_viz_tests.pl: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | tests search on prolog in-memory triples / builtin emulation 4 | 5 | */ 6 | 7 | :- use_module(library(semweb/rdf11)). 8 | :- use_module(library(sparqlprog)). 9 | :- use_module(library(sparqlprog/emulate_builtins)). 10 | :- use_module(library(sparqlprog/search_util)). 11 | :- use_module(library(sparqlprog/ontologies/owl)). 12 | :- use_module(library(sparqlprog/owl_search_viz)). 13 | 14 | :- begin_tests(search_test, 15 | [setup(load_test_file), 16 | cleanup(rdf_retractall(_,_,_,_))]). 17 | 18 | 19 | load_test_file :- 20 | rdf_load('tests/go_nucleus.ttl'), 21 | forall(rdf(S,P,O), 22 | writeln(x(S,P,O))). 23 | 24 | 25 | test(basic) :- 26 | owl_search_and_display(nuc, '.', '.', [], info, _). 27 | :- end_tests(search_test). 28 | 29 | 30 | -------------------------------------------------------------------------------- /tests/tests.pl: -------------------------------------------------------------------------------- 1 | 2 | :- load_files([ 3 | tests/basic_test, 4 | tests/builtins_test, 5 | tests/search_test, 6 | tests/dataframe_test, 7 | tests/labelutils_test, 8 | tests/builtins_test, 9 | tests/prolog_test, 10 | tests/search_viz_test, 11 | tests/owl_test 12 | ], [ if(not_loaded) ]). 13 | --------------------------------------------------------------------------------