aggregate - Sparql group_concat - 停止排序

标签 aggregate sparql bioinformatics group-concat triplestore

我是 SPARQL 的新手,遇到了一些我不理解的有趣行为。

所以我有四个基因:

a-gene
b-gene
c-gene
d-gene

和两个菌株:

strain1
strain2

和以下三元组:

strain1 hasGene a-gene
strain1 hasGene b-gene
strain1 hasGene d-gene

strain2 hasGene b-gene
strain2 hasGene d-gene

我的目标是创建一个 SPARQL 查询,为所有菌株的 hasBinary 属性添加一个值,其中 hasBinary 是菌株具有的基因的相应二进制文件,并且没有。 例如:

strain1 hasBinary 1101
strain2 hasBinary 0101

strain1有基因a-gene, b-gene, d-gene,但没有c-gene >。 给定查询(注意菌株和基因分别属于菌株和基因类):

select ?s (group_concat(?result ; separator="") as ?binary)
where { ?g a Gene.
        ?s a Strain.
        optional{?s ?hasGene ?g.}.
        bind((if(bound(?hasGene), "1","0")) as ?result). }
group by ?s
order by ?s

输出是:

strain1 1101
strain2 0101 

这是正确的。但是当我查询时:

construct {?s hasBinary ?binary}
where{

select ?s (group_concat(?result ; separator="") as ?binary)
where { ?g a Gene.
        ?s a Strain.
        optional{?s ?hasGene ?g.}.
        bind((if(bound(?hasGene), "1","0")) as ?result).


      }
group by ?s
order by ?s

}

输出是:

strain1 hasBinary 0111
strain2 hasBinary 0011

这是完全错误的。就好像 group_concat 正在对结果进行排序。我不知道为什么要这样做,二进制文件如果被订购就没用了。对此问题的任何帮助将不胜感激。

最佳答案

据我所知,您的查询是正确的,您观察到的问题是您使用的任何 SPARQL 引擎中的一个错误。或者至少:当我在芝麻商店(版本 2.8.8)上尝试您的案例时,它给了我预期的结果。

编辑 我得到正确结果的原因是芝麻恰好以预期的顺序返回结果,但正如@TallTed 正确指出的那样,这实际上不是由查询强制执行的,所以这不是什么你可以依赖。所以我之前断言这是端点中的错误是错误的。

让我们稍微探讨一下。

我使用的数据:

@prefix : <http://example.org/> .

:a-gene a :Gene .
:b-gene a :Gene .
:c-gene a :Gene .
:d-gene a :Gene .

:strain1 a :Strain .
:strain2 a :Strain .

:strain1 :hasGene :a-gene .
:strain1 :hasGene :b-gene .
:strain1 :hasGene :d-gene .


:strain2 :hasGene :b-gene .
:strain2 :hasGene :d-gene .

如果我们看一下最简单的查询形式,我们想要返回所有 ?s 和所有 ?g ,其中可选地有一个 :hasGene 关系,我们希望它们按顺序排列。您最初的查询基本上是这样的:

PREFIX : <http://example.org/>
select ?s ?g
where { ?g a :Gene.
        ?s a :Strain.
        optional { ?s ?hasGene ?g } .
}
order by ?s

现在,这个查询在我的 Sesame 商店(以及您的端点)中返回:

?s                              ?g
<http://example.org/strain1>    <http://example.org/a-gene>
<http://example.org/strain1>    <http://example.org/b-gene>
<http://example.org/strain1>    <http://example.org/c-gene>
<http://example.org/strain1>    <http://example.org/d-gene>
<http://example.org/strain2>    <http://example.org/a-gene>
<http://example.org/strain2>    <http://example.org/b-gene>
<http://example.org/strain2>    <http://example.org/c-gene>
<http://example.org/strain2>    <http://example.org/d-gene>

看起来不错吧?全部按字母数字顺序排列。但重要的是要认识到此处 ?g 列的顺序是巧合。如果引擎返回了这个:

?s                              ?g
<http://example.org/strain1>    <http://example.org/c-gene>
<http://example.org/strain1>    <http://example.org/b-gene>
<http://example.org/strain1>    <http://example.org/a-gene>
<http://example.org/strain1>    <http://example.org/d-gene>
<http://example.org/strain2>    <http://example.org/b-gene>
<http://example.org/strain2>    <http://example.org/a-gene>
<http://example.org/strain2>    <http://example.org/c-gene>
<http://example.org/strain2>    <http://example.org/d-gene>

...它也应该是一个有效的结果——毕竟,我们的查询没有任何地方说 ?g 应该被排序。

然而,解决方案很简单:对 ?s?g 都进行排序。由于在我们特定的 SPARQL 端点中,正确的顺序已经“巧合地”返回,即使没有这个,我们可以通过一个小技巧来验证它是否有效:使用 DESC 运算符恢复顺序。

查询:

PREFIX : <http://example.org/>
SELECT ?s ?g
WHERE { ?g a :Gene.
        ?s a :Strain.
        OPTIONAL { ?s ?hasGene ?g } .
}
ORDER BY ?s DESC(?g)

结果:

?s                              ?g
<http://example.org/strain1>    <http://example.org/d-gene>
<http://example.org/strain1>    <http://example.org/c-gene>
<http://example.org/strain1>    <http://example.org/b-gene>
<http://example.org/strain1>    <http://example.org/a-gene>
<http://example.org/strain2>    <http://example.org/d-gene>
<http://example.org/strain2>    <http://example.org/c-gene>
<http://example.org/strain2>    <http://example.org/b-gene>
<http://example.org/strain2>    <http://example.org/a-gene>

您可以看到 ?g 列现在实际上是按字母倒序排列的(这当然与您想要的相反,但是只需省略 DESC< 即可轻松纠正 稍后查询的一部分 - 关键是我们通过这种方式验证了它是 我们的查询 进行排序,而不是我们使用的任何端点)。

虽然它仍然不能完全解决二进制字符串中的排序问题。由于在您的原始查询中 BIND 发生在 排序之前(因为绑定(bind)是图形模式的一部分,它在结果排序发生之前得到全面评估), ORDER BY 子句对此没有影响。也就是说,如果我们简单地执行此查询:

PREFIX : <http://example.org/>
SELECT ?s (GROUP_CONCAT(?result ; SEPARATOR="") as ?binary)
WHERE { ?g a :Gene.
        ?s a :Strain.
        OPTIONAL { ?s ?hasGene ?g } .
        BIND((IF(BOUND(?hasGene), "1","0")) AS ?result). 
}
GROUP BY ?s 
ORDER BY ?s DESC(?g)

我们仍然得到这个结果:

?s  ?binary
<http://example.org/strain1>    "1101"
<http://example.org/strain2>    "0101"

换句话说,我们的二进制字符串仍然没有反转,这是应该的。

解决方案是引入一个进一步的子查询,它将所需的结果传递给它的外部查询,然后连接这个有序的结果以创建二进制字符串,如下所示:

PREFIX : <http://example.org/>
SELECT ?s (GROUP_CONCAT(?result ; SEPARATOR="") as ?binary)
WHERE { 
  { SELECT ?s ?hasGene  
    WHERE { ?g a :Gene.
            ?s a :Strain.
            OPTIONAL {?s ?hasGene ?g.}.
    }
    ORDER BY ?s DESC(?g)
  }
  BIND((IF(BOUND(?hasGene), "1","0")) AS ?result). 
}
GROUP BY ?s

结果是:

?s  ?binary
<http://example.org/strain1>    "1011"
<http://example.org/strain2>    "1010"

如您所见,查询现在强制执行正确的(倒置的)二进制字符串。然后我们需要将整个野兽输入到您想要的 CONSTRUCT 查询中,最后我们需要取出二进制字符串的反转。

然后完整的查询变成这样:

查询 2:

PREFIX : <http://example.org/>
CONSTRUCT {?s :hasBinary ?binary }
WHERE {
  SELECT ?s (GROUP_CONCAT(?result ; SEPARATOR="") as ?binary)
  WHERE { 
    { SELECT ?s ?hasGene  
      WHERE { ?g a :Gene.
              ?s a :Strain.
              OPTIONAL {?s ?hasGene ?g.}.
      }
      ORDER BY ?s ?g
    }
    BIND((IF(BOUND(?hasGene), "1","0")) AS ?result). 
  }
  GROUP BY ?s
}

结果:

<http://example.org/strain1> <http://example.org/hasBinary> "1101" .
<http://example.org/strain2> <http://example.org/hasBinary> "0101" .

关于aggregate - Sparql group_concat - 停止排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34796235/

相关文章:

python - Pandas 数据帧按时间戳聚合到 x 分钟箱中

sql - 使用聚合函数来缩小查询范围

mysql - 如何按月或年聚合日期 Mysql

sparql - 查询获取某个实体类的数量属性

javascript - 使用 Python 从网站下载 Javascript 文件

Java Bioinformatics - 获取字符串中多个特定单词的所有索引

mysql - 如何在 Mysql 中对 json_arrayagg() 返回的数组进行排序?

java - 从 SPARQL 结果中的文字中删除数据类型

sparql - 如何从SPARQL联合查询(服务关键字)返回特定变量?

python - 将相似的模式合并为单一的共识模式