<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>SPANG: A SPARQL Client with Templates, One-Liners, and Application Programming Interfaces</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Hirokazu Chiba</string-name>
          <email>chiba@dbcls.rois.ac.jp</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Database Center for Life Science</institution>
          ,
          <addr-line>Chiba 277-0871</addr-line>
          ,
          <country country="JP">Japan</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>SPARQL is a key component of the Semantic Web, and its reusability of is crucial for maximizing productivity on the Semantic Web. While an increasing number of datasets have been published in the Resource Description Framework (RDF) and the SPARQL standard allows for an interoperable framework for querying the RDF datasets, the reuse of SPARQL queries remains limited. Herein, we present SPANG, a framework used to facilitate the reuse of SPARQL queries by using a template engine, one-liner functionalities, and application programming interfaces. We demonstrate its use for making SPARQL queries more reusable; assign a set of metadata for annotating a SPARQL query, which enables the identi cation of the SPARQL query and provides useful information for the query; parameterize SPARQL queries, which makes each query reusable under di erent settings; assign a URI for each query; and construct a server for providing the indices of SPARQL queries. SPANG makes SPARQL queries more reusable in the local system or across the Web and facilitate the construction of an eco-system wherein SPARQL queries are shared to maximize the productivity of Semantic Web developers and users. SPANG is available at https://spang.dbcls.jp/.</p>
      </abstract>
      <kwd-group>
        <kwd>SPARQL</kwd>
        <kwd>template engine</kwd>
        <kwd>query library</kwd>
        <kwd>one-liner</kwd>
        <kwd>API</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>An increasing number of datasets have been published in the Resource
Description Framework (RDF). SPARQL is the standardized interface to RDF and
contributes to the e ective reuse of distributed RDF data. Although eco-systems
have been developed for developing SPARQL queries, writing SPARQL codes
is often a burden for Semantic Web developers and users. SPARQL queries
have been written in many projects and are the result of developers' e orts.
The value of the SPARQL queries accumulated thus far can be maximized by
reusing SPARQL queries. For the e ective construction of Semantic Web
applications, the reuse of such queries is invaluable. However, owing to the limitation
Copyright c 2020 for this paper by its author. Use permitted under Creative
Commons License Attribution 4.0 International (CC BY 4.0).</p>
      <p>Usage: spang2 [options] &lt;SPARQL_TEMPLATE&gt; [par1=val1 par2=val2 ...]
Options:
-e, --endpoint &lt;ENDPOINT&gt; target SPARQL endpoint
-o, --outfmt &lt;FORMAT&gt; tsv, json, n-triples (nt), turtle (ttl), rdf/xml (rdfxml), n3, xml, html
-a, --abbr abbreviate results using predefined prefixes
-v, --vars variable names are included in output (in the case of tsv format)
-S, --subject &lt;SUBJECT&gt; shortcut for specifying subject
-P, --predicate &lt;PREDICATE&gt; shortcut for specifying predicate
-O, --object &lt;OBJECT&gt; shortcut for specifying object
-L, --limit &lt;LIMIT&gt; LIMIT output (use by itself or with -[SPOF])
-F, --from &lt;FROM&gt; shortcut to search FROM specific graph (use alone or with -[SPOLN])
-N, --number shortcut to COUNT results (use by itself or with -[SPO])
-G, --graph shortcut for searching for graph names (use by itself or with -[SPO])
-r, --prefix &lt;PREFIX_FILES&gt; read prefix declarations (default: SPANG_DIR/etc/prefix,~/.spang/prefix)
-n, --ignore ignore user-specific file (~/.spang/prefix) for test purpose
-m, --method &lt;METHOD&gt; GET or POST (default: "GET")
-q, --show_query show query and quit
-f, --fmt format the query
-i, --indent &lt;DEPTH&gt; indent depth; use with --fmt (default: 2)
-l, --list_nick_name list available nicknames of endpoints and quit
-d, --debug debug (output query embedded in URL, or output AST with --fmt)
-V, --version output version number
-h, --help output usage information
a default value of C0751955 (\Brain Infarction").
# @title Get genes involved in a specific disease
# @endpoint http://rdf.disgenet.org/sparql/
# @prefix https://raw.githubusercontent.com/hchiba1/spang/master/prefix/bio
# @param arg1=C0751955
SELECT DISTINCT ?gene ?score ?gene_label ?source ?gda ?pmid ?description
WHERE {
?gda sio:SIO_000628 umls:{{arg1}} , ?gene ;
a ?type ;
sio:SIO_000253 ?source ;
sio:SIO_000216/sio:SIO_000300 ?score .
?gene a ncit:C16612 ;</p>
      <p>rdfs:label ?gene_label .</p>
      <p>OPTIONAL {
?gda sio:SIO_000772 ?pmid ;</p>
      <p>dct:description ?description .</p>
      <p>}
}
ORDER BY DESC(?score) ?source ?pmid
The Unix command-line environment is available as an interface for the SPANG
template library. While the SPANG templates can be stored in the local system,
users can also call templates that are publicly available across the Web if each
query is assigned a dereferenceable URI. The following example command line
gives a parameter to a local template query le for the DisGeNET endpoint.</p>
      <p>spang2 disease gene.rq disease=C0751955
The template can also be a URL that returns a SPANG template (https://raw.
githubusercontent.com/hchiba1/spang/master/library/disgenet/disease_
gene.rq). Thus, anyone can host the query libraries on GitHub. Other use cases
include one-liner functionalities, such as</p>
      <p>spang2 -e disgenet -N -O ncbigene:2247
where the query counts the number of triples that have ncbigene:2247 as the
object in the DisGeNET endpoint. The target endpoints and URI pre xes can
be prede ned via con guration les. The following command prints the
internally generated SPARQL query according to the command-line options to the
standard output.</p>
      <p>
        spang2 -e disgenet -N -O ncbigene:2247 -q
Most of these one-liner functionalities are compatible with previous versions of
SPANG [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. The full usage of the SPANG command is illustrated in Figure 1.
3.2
      </p>
      <p>Use through API
The SPARQL templates can also be called in the JavaScript code used to
construct a website. The documentation is available at the project home page. Users
can also execute templates that are prede ned and provided by the SPANG
server through the REST API. Accessing the following URI returns the result
of the query with the given parameter: https://spang-portal.dbcls.jp/api/
library/disgenet/disease_gene.rq?arg1=C0751955. The following URI
returns the list of queries in the library: https://spang-portal.dbcls.jp/api/
library/disgenet.</p>
      <p>Users can also obtain the results on the SPANG server by clicking on the
web pages while browsing the templates on the server (https://spang-portal.
dbcls.jp/library/disgenet/disease_gene.rq).
4</p>
    </sec>
    <sec id="sec-2">
      <title>Implementation</title>
      <p>
        The SPARQL templating syntax used in the SPANG framework is an extension
of sparql-doc [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. Although the use of sparql-doc was originally aimed at the
documentation of SPARQL, this notation is suitable for annotating each SPARQL
query with metadata to increase the usability of the query. Each query, with the
metadata added as comment lines, can be used as a standard SPARQL query.
Furthermore, if a parser is implemented for the comment lines, users can utilize
the metadata to search for useful information about the query. Herein, we further
parameterize the SPARQL queries and consider the parameters of the query as
the metadata of SPARQL. As a syntax for parameterization in SPARQL code,
we use mustache notation [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
      </p>
      <p>
        We implemented the SPANG template engine in JavaScript, where the
template is transformed into an abstract syntax tree, processed using the given
parameters, and then reconstructed into SPARQL to be submitted to an endpoint.
We used Parsing Expression Grammar (PEG) [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] expression of the SPARQL
grammar based on the EBNF of the SPARQL speci cations [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. PEG.js [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]
is a parser generator, and thus, we could generate a custom parser based on
a PEG description. The original implementation of PEG.js of the SPARQL
grammar is in the rdfstore-js project (https://github.com/antoniogarrote/
rdfstore-js). The PEG.js code was modi ed to t the use in the case of the
SPANG framework. While other e orts have been made to implement the parser
and formatter of SPARQL [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ], we developed a parser and formatter that stringify
the generated abstract syntax tree based on the PEG expression.
      </p>
      <p>We also developed a SPANG server, wherein the SPARQL queries can be
executed in a browser or through a REST API (https://spang.dbcls.jp/).
The SPANG server is developed using Ruby on Rails, and it can run on Docker.
Thus, users can also run a SPANG server on their own.
5</p>
    </sec>
    <sec id="sec-3">
      <title>Related Work</title>
      <p>
        SPARQL templating mechanisms have been implemented in various projects.
SPARQL libraries, such as Bioqueries [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ], have been implemented based on
templating mechanisms. Bioqueries comprise an e ort to collect SPARQL queries as
a community attempt, contributing to the increased ndability of the queries.
However, the users should still copy the queries into their own software in
speci c templating formats to reuse the queries. In this paper, we present SPANG
framework, where we attempt to build a software network with solid
programming interfaces based on the Semantic Web platform.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Chiba</surname>
            ,
            <given-names>H.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Uchiyama</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          (
          <year>2017</year>
          ).
          <article-title>SPANG: a SPARQL client supporting generation and reuse of queries for distributed RDF databases</article-title>
          .
          <source>BMC bioinformatics</source>
          ,
          <volume>18</volume>
          (
          <issue>1</issue>
          ),
          <fpage>93</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Queralt-Rosinach</surname>
            ,
            <given-names>N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Pinero</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bravo</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Sanz</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Furlong</surname>
            ,
            <given-names>L. I.</given-names>
          </string-name>
          (
          <year>2016</year>
          ).
          <article-title>DisGeNET-RDF: harnessing the innovative power of the Semantic Web to explore the genetic basis of diseases</article-title>
          .
          <source>Bioinformatics</source>
          ,
          <volume>32</volume>
          (
          <issue>14</issue>
          ),
          <fpage>2236</fpage>
          -
          <lpage>2238</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          <article-title>3. sparql-doc: Generate HTML documentation from SPARQL queries</article-title>
          . https:// github.com/ldodds/sparql-doc
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <article-title>Logic-less templates</article-title>
          . https://mustache.github.io/
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Ford</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          (
          <year>2004</year>
          , January).
          <article-title>Parsing expression grammars: a recognition-based syntactic foundation</article-title>
          .
          <source>In Proceedings of the 31st ACM SIGPLAN-SIGACT symposium on Principles of programming languages</source>
          (pp.
          <fpage>111</fpage>
          -
          <lpage>122</lpage>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          <article-title>6. SPARQL 1.1 Query Language</article-title>
          ,
          <source>W3C Recommendation 21 March</source>
          <year>2013</year>
          . http://www. w3.org/TR/sparql11-query/
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7. PEG.
          <article-title>js { Parser Generator for JavaScript</article-title>
          . https://pegjs.org/
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8. SPARQL.
          <article-title>js { A SPARQL 1.1 parser for JavaScript</article-title>
          . https://github.com/ RubenVerborgh/SPARQL.js
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Garc</surname>
            a-Godoy,
            <given-names>M. J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Navas-Delgado</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Aldana-Montes</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          (
          <year>2011</year>
          , December).
          <article-title>Bioqueries: a social community sharing experiences while querying biological linked data</article-title>
          .
          <source>In Proceedings of the 4th international workshop on semantic web applications</source>
          and
          <article-title>tools for the life sciences</article-title>
          (pp.
          <fpage>24</fpage>
          -
          <lpage>31</lpage>
          ).
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>