# -*- orgstrap-cypher: sha256; orgstrap-norm-func-name: orgstrap-norm-func--dprp-1-0; orgstrap-block-checksum: 32b4c6dcae4b740062e4d4005c6dcec47c4bf1706b9fe2c46193167966b09430; -*-
#+title: Examples
# inherit configuration from queries.org
#+setupfile: ./queries.org

* Regions with processes from neuron population
Show all regions that contain some part of a given neuron population.
Here, [[ilxtr:neuron-type-keast-5][Neuron type keast 5]]

#+name: cypher-query-1
#+begin_src cypher
// neurites
MATCH (neupop:Class{iri: "http://uri.interlex.org/tgbugs/uris/readable/neuron-type-keast-5"})
-[:apinatomy:annotates*0..1]->(neugrp:NamedIndividual{`https://apinatomy.org/uris/readable/description`: "dynamic"})
-[:apinatomy:links]->(link)
-[:apinatomy:fasciculatesIn|apinatomy:endsIn*0..1]->(lyph_or_layer)
-[:apinatomy:layerIn*0..1]->(lyph)
-[:apinatomy:ontologyTerms]->(region)
RETURN region

UNION

// somas
MATCH (neupop:Class{iri: "http://uri.interlex.org/tgbugs/uris/readable/neuron-type-keast-5"})
-[:apinatomy:annotates*0..1]->(neugrp:NamedIndividual{`https://apinatomy.org/uris/readable/description`: "dynamic"})
-[:apinatomy:lyphs]->(lyph)
-[:apinatomy:internalIn]->()
-[:apinatomy:ontologyTerms]->(region)
RETURN region
#+end_src

#+RESULTS: cypher-query-1
| id             | label                                  |
|----------------+----------------------------------------|
| ILX:0738432    | Sixth lumbar spinal cord segment       |
| ILX:0792853    | Anterior root of first sacral nerve    |
| ILX:0793615    | Ventral root of the sixth lumbar nerve |
| UBERON:0006460 | first sacral spinal cord segment       |
| UBERON:0016508 | pelvic ganglion                        |
| UBERON:0018675 | pelvic splanchnic nerve                |

#+name: sparql-query-1
#+begin_src sparql
SELECT DISTINCT
?region
(str(?l) as ?label)
WHERE {
  VALUES ?neupop { ilxtr:neuron-type-keast-5 }
  ?neupop apinatomy:annotates ?neugrp .
  ?neugrp a elements:Group .  # externals annotate the whole model
  {
    ?neugrp apinatomy:links / ( apinatomy:fasciculatesIn | apinatomy:endsIn ) ?rlyph .
    ?rlyph apinatomy:ontologyTerms ?region .
    filter not exists { ?rlyph apinatomy:layer ?_ }  # require if ?region where not specified
  } UNION {
    ?neugrp apinatomy:links / ( apinatomy:fasciculatesIn | apinatomy:endsIn ) /
            apinatomy:layerIn / apinatomy:ontologyTerms ?region .
  }
  OPTIONAL { ?region rdfs:label ?l }
}
ORDER BY ?region
#+end_src

#+RESULTS: sparql-query-1
| region         | label                                  |
|----------------+----------------------------------------|
| ILX:0738432    | Sixth lumbar spinal cord segment       |
| ILX:0792853    | Anterior root of first sacral nerve    |
| ILX:0793615    | Ventral root of the sixth lumbar nerve |
| UBERON:0006460 | first sacral spinal cord segment       |
| UBERON:0016508 | pelvic ganglion                        |
| UBERON:0018675 | pelvic splanchnic nerve                |

* Neuron populations with processes in region
Show all neuron populations that have some part in a given region.
In this case the [[UBERON:0005453][inferior mesenteric ganglion]].
See this [[./queries.org::#neru-populations-with-processes-in][catalog entry]] for the generalized form of this query.

#+header: :label "http://www.w3.org/2004/02/skos/core#prefLabel"
#+name: cypher-query-2
#+begin_src cypher :limit 999 :label skos:prefLabel
// neurites
MATCH (neupop:Class)
-[:apinatomy:annotates*0..1]->(neugrp:NamedIndividual{`https://apinatomy.org/uris/readable/description`: "dynamic"})
-[:apinatomy:links]->(link)
-[:apinatomy:fasciculatesIn|apinatomy:endsIn*0..1]->(lyph_or_layer)
-[:apinatomy:layerIn*0..1]->(lyph)
-[:apinatomy:ontologyTerms*0..1]->(region:Class{iri: "UBERON:0005453"})
RETURN neupop

UNION

// somas
MATCH (neupop:Class)
-[:apinatomy:annotates*0..1]->(neugrp:NamedIndividual{`https://apinatomy.org/uris/readable/description`: "dynamic"})
-[:apinatomy:lyphs]->(lyph)
-[:apinatomy:internalIn]->()
-[:apinatomy:ontologyTerms*0..1]->(region:Class{iri: "UBERON:0005453"})
RETURN neupop
#+end_src

#+RESULTS: cypher-query-2
| id                              | label                                                                                    |
|---------------------------------+------------------------------------------------------------------------------------------|
| ilxtr:neuron-type-keast-3       | inferior mesenteric ganglion neuron (kblad)                                              |
| ilxtr:neuron-type-keast-6       | sympathetic preganglionic neuron innervating pelvic ganglion neuron (kblad)              |
| ilxtr:neuron-type-keast-7       | sympathetic preganglionic neuron innervating inferior mesenteric ganglion neuron (kblad) |
| ilxtr:neuron-type-keast-11      | L1-L2 sensory neuron innervating bladder                                                 |
| ilxtr:neuron-type-sdcol-b       | neuron type sdcol b                                                                      |
| ilxtr:neuron-type-sdcol-d       | neuron type sdcol d                                                                      |
| ilxtr:neuron-type-sdcol-f       | neuron type sdcol f                                                                      |
| ilxtr:neuron-type-sdcol-g       | neuron type sdcol g                                                                      |
| ilxtr:neuron-type-sdcol-h       | neuron type sdcol h                                                                      |
| ilxtr:neuron-type-sdcol-q       | neuron type sdcol q                                                                      |
| ilxtr:neuron-type-sdcol-q-prime | neuron type sdcol q'                                                                     |

#+name: sparql-query-2
#+begin_src sparql
SELECT DISTINCT
?neupop
(str(?l) as ?label)
(str(?pl) as ?prefLabel)
WHERE {
  VALUES (?region ?neuron) { (UBERON:0005453 SAO:1417703748) }
  ?neupop rdfs:subClassOf+ ?neuron ;
          rdfs:label ?l ;
          apinatomy:annotates ?neugrp .
  OPTIONAL { ?neupop skos:prefLabel ?pl }
  ?neugrp a elements:Group .  # externals annotate the whole model
  {
    ?neugrp apinatomy:links / ( apinatomy:fasciculatesIn | apinatomy:endsIn ) ?rlyph .
    ?rlyph apinatomy:ontologyTerms ?region .
    filter not exists { ?rlyph apinatomy:layer ?_ }  # require if ?region where not specified
  } UNION {
    ?neugrp apinatomy:links / ( apinatomy:fasciculatesIn | apinatomy:endsIn ) /
            apinatomy:layerIn / apinatomy:ontologyTerms ?region .
  }
}
ORDER BY ?neupop
#+end_src

#+RESULTS: sparql-query-2
| neupop                          | label                | prefLabel                                                                                |
|---------------------------------+----------------------+------------------------------------------------------------------------------------------|
| ilxtr:neuron-type-keast-3       | neuron type kblad 3  | inferior mesenteric ganglion neuron (kblad)                                              |
| ilxtr:neuron-type-keast-6       | neuron type kblad 6  | sympathetic preganglionic neuron innervating pelvic ganglion neuron (kblad)              |
| ilxtr:neuron-type-keast-7       | neuron type kblad 7  | sympathetic preganglionic neuron innervating inferior mesenteric ganglion neuron (kblad) |
| ilxtr:neuron-type-keast-11      | neuron type kblad 11 | L1-L2 sensory neuron innervating bladder                                                 |
| ilxtr:neuron-type-sdcol-b       | neuron type sdcol b  |                                                                                          |
| ilxtr:neuron-type-sdcol-d       | neuron type sdcol d  |                                                                                          |
| ilxtr:neuron-type-sdcol-f       | neuron type sdcol f  |                                                                                          |
| ilxtr:neuron-type-sdcol-g       | neuron type sdcol g  |                                                                                          |
| ilxtr:neuron-type-sdcol-h       | neuron type sdcol h  |                                                                                          |
| ilxtr:neuron-type-sdcol-q       | neuron type sdcol q  |                                                                                          |
| ilxtr:neuron-type-sdcol-q-prime | neuron type sdcol q' |                                                                                          |

* Bootstrap :ARCHIVE:noexport:
:properties:
:visibility: folded
:end:

#+name: orgstrap
#+begin_src elisp :results none :exports none :lexical yes
(defvar ow-do-devel nil)

(setq-local
 org-confirm-babel-evaluate
 (lambda (lang _body)
   (not (or (member lang '("cypher" "sparql"))))))

(unless ow-do-devel
  (find-file-noselect "./queries.org"))
#+end_src

** Local Variables :ARCHIVE:

# Local Variables:
# org-adapt-indentation: nil
# org-edit-src-content-indentation: 0
# eval: (progn (setq-local orgstrap-min-org-version "8.2.10") (let ((a (org-version)) (n orgstrap-min-org-version)) (or (fboundp #'orgstrap--confirm-eval) (not n) (string< n a) (string= n a) (error "Your Org is too old! %s < %s" a n))) (defun orgstrap-norm-func--dprp-1-0 (body) (let ((p (read (concat "(progn\n" body "\n)"))) (m '(defun defun-local defmacro defvar defvar-local defconst defcustom)) print-quoted print-length print-level) (cl-labels ((f (b) (cl-loop for e in b when (listp e) do (or (and (memq (car e) m) (let ((n (nthcdr 4 e))) (and (stringp (nth 3 e)) (or (cl-subseq m 3) n) (f n) (or (setcdr (cddr e) n) t)))) (f e))) p)) (prin1-to-string (f p))))) (unless (boundp 'orgstrap-norm-func) (defvar-local orgstrap-norm-func orgstrap-norm-func-name)) (defun orgstrap-norm-embd (body) (funcall orgstrap-norm-func body)) (unless (fboundp #'orgstrap-norm) (defalias 'orgstrap-norm #'orgstrap-norm-embd)) (defun orgstrap--confirm-eval-minimal (lang body) (not (and (member lang '("elisp" "emacs-lisp")) (eq orgstrap-block-checksum (intern (secure-hash orgstrap-cypher (orgstrap-norm body))))))) (unless (fboundp #'orgstrap--confirm-eval) (defalias 'orgstrap--confirm-eval #'orgstrap--confirm-eval-minimal)) (let (enable-local-eval) (vc-find-file-hook)) (let ((ocbe org-confirm-babel-evaluate) (obs (org-babel-find-named-block "orgstrap"))) (if obs (unwind-protect (save-excursion (setq-local orgstrap-norm-func orgstrap-norm-func-name) (setq-local org-confirm-babel-evaluate #'orgstrap--confirm-eval) (goto-char obs) (org-babel-execute-src-block)) (when (eq org-confirm-babel-evaluate #'orgstrap--confirm-eval) (setq-local org-confirm-babel-evaluate ocbe)) (org-set-visibility-according-to-property)) (warn "No orgstrap block."))))
# End:
