使用 SPARQL 从 turtle 文件中提取父节点的所有子节点

Extract all the child nodes for a parent node from turtle file using SPARQL

我在 turtle 文件中有以下概念。我想从以下概念中提取父节点 (DOID_4159) 及其所有子节点的首选标签和 ID。我已经编写了以下 SPARQL 查询来获取信息,但它不会提供所有子节点。

qres = g.query(
    """
    SELECT *
    WHERE {
    {?s skos:prefLabel ?prefLabel .
    ?s skos:broader  <http://purl.obolibrary.org/obo/DOID_4159>  . 
    }
    UNION
    {?s skos:prefLabel "skin cancer"@en .
    ?s skos:prefLabel ?prefLabel .
    }
    }
    """)
<http://purl.obolibrary.org/obo/DOID_8923>
      a       skos:Concept ;
      skos:altLabel "malignant scalp melanoma"@en , "malignant lip melanoma"@en , "malignant melanoma of skin of upper limb"@en , "cutaneous melanoma"@en , "malignant melanoma of ear and/or external auricular canal"@en , "malignant trunk melanoma"@en , "malignant melanoma of skin of trunk except scrotum"@en , "malignant lower limb melanoma"@en , "malignant melanoma of skin of lower limb"@en , "malignant upper limb melanoma"@en , "malignant ear melanoma"@en , "malignant neck melanoma"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:definition "A skin cancer that has_material_basis_in melanocytes." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;

<http://purl.obolibrary.org/obo/DOID_3451>
      a       skos:Concept ;
      skos:altLabel "carcinoma of skin"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:definition "A skin cancer that is located_in tissues of the skin and develops from epithelial cells." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "skin carcinoma"@en .
<http://purl.obolibrary.org/obo/DOID_6944>
      a       skos:Concept ;
      skos:altLabel "Seborrheic Keratosis of Vulva"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_6498> , <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "vulvar seborrheic keratosis"@en .
<http://purl.obolibrary.org/obo/DOID_4159>
      a       skos:Concept ;
      skos:altLabel "malignant neoplasm of skin"@en , "CA - skin cancer"@en , "melanoma and Non-melanoma skin cancer"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_37> ;
      skos:definition "An integumentary system cancer located_in the skin that is the uncontrolled growth of abnormal skin cells." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "skin cancer"@en .
<http://purl.obolibrary.org/obo/DOID_11684>
      a       skos:Concept ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_6498> ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "melanoacanthoma"@en .
Following is the desired output:
id        label
DOID_8923 skin melanoma
DOID_3451 skin carcinoma
DOID_6944 vulvar seborrheic keratosis
DOID_4159 skin cancer
DOID_6498 seborrheic keratosis
DOID_11684 melanoacanthoma

如果我想为父级 (DOID_4159) 及其所有关联节点获取此信息,我该如何实现?我是 SPARQL 的新手,非常感谢任何帮助。

这里我们遇到了一些问题

  • 您发布的.ttl 文件不正确。第一个实体的定义有错误,最后缺少 .。所以你必须更新定义:
<http://purl.obolibrary.org/obo/DOID_8923>
      a       skos:Concept ;
      skos:altLabel "malignant scalp melanoma"@en , "malignant lip melanoma"@en , "malignant melanoma of skin of upper limb"@en , "cutaneous melanoma"@en , "malignant melanoma of ear and/or external auricular canal"@en , "malignant trunk melanoma"@en , "malignant melanoma of skin of trunk except scrotum"@en , "malignant lower limb melanoma"@en , "malignant melanoma of skin of lower limb"@en , "malignant upper limb melanoma"@en , "malignant ear melanoma"@en , "malignant neck melanoma"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:definition "A skin cancer that has_material_basis_in melanocytes." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;

收件人:

<http://purl.obolibrary.org/obo/DOID_8923>
      a       skos:Concept ;
      skos:altLabel "malignant scalp melanoma"@en , "malignant lip melanoma"@en , "malignant melanoma of skin of upper limb"@en , "cutaneous melanoma"@en , "malignant melanoma of ear and/or external auricular canal"@en , "malignant trunk melanoma"@en , "malignant melanoma of skin of trunk except scrotum"@en , "malignant lower limb melanoma"@en , "malignant melanoma of skin of lower limb"@en , "malignant upper limb melanoma"@en , "malignant ear melanoma"@en , "malignant neck melanoma"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:definition "A skin cancer that has_material_basis_in melanocytes." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> .

最后的.,表示你的实体定义完成。而;用于需要添加其他关系的时候。

  • 记录 DOID_8923 没有 skos:prefLabel 因此,您无法从 DOID_8923 检索 prefLabel。您还需要将 skos:prefLabel 添加到此实体

  • 正如UninformedUser所指出的 您还必须在您的架构中指定 DOID_11684DOID_4159 之间的关系

在这里您可以找到您的 .ttl 文件更新和传递 SPARQL 查询

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

<http://purl.obolibrary.org/obo/DOID_8923>
      a       skos:Concept ;
      skos:altLabel "malignant scalp melanoma"@en , "malignant lip melanoma"@en , "malignant melanoma of skin of upper limb"@en , "cutaneous melanoma"@en , "malignant melanoma of ear and/or external auricular canal"@en , "malignant trunk melanoma"@en , "malignant melanoma of skin of trunk except scrotum"@en , "malignant lower limb melanoma"@en , "malignant melanoma of skin of lower limb"@en , "malignant upper limb melanoma"@en , "malignant ear melanoma"@en , "malignant neck melanoma"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:definition "A skin cancer that has_material_basis_in melanocytes." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> .

<http://purl.obolibrary.org/obo/DOID_3451>
      a       skos:Concept ;
      skos:altLabel "carcinoma of skin"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:definition "A skin cancer that is located_in tissues of the skin and develops from epithelial cells." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "skin carcinoma"@en .
<http://purl.obolibrary.org/obo/DOID_6944>
      a       skos:Concept ;
      skos:altLabel "Seborrheic Keratosis of Vulva"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_6498> , <http://purl.obolibrary.org/obo/DOID_4159> ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "vulvar seborrheic keratosis"@en .
<http://purl.obolibrary.org/obo/DOID_4159>
      a       skos:Concept ;
      skos:altLabel "malignant neoplasm of skin"@en , "CA - skin cancer"@en , "melanoma and Non-melanoma skin cancer"@en ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_37> ;
      skos:definition "An integumentary system cancer located_in the skin that is the uncontrolled growth of abnormal skin cells." ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "skin cancer"@en .
<http://purl.obolibrary.org/obo/DOID_11684>
      a       skos:Concept ;
      skos:broader <http://purl.obolibrary.org/obo/DOID_6498> ;
      skos:inScheme <https://localhost:8443/ontology/Applicanttest/APPLICANTTEST> ;
      skos:prefLabel "melanoacanthoma"@en .

SPARQL

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
prefix skos: <http://www.w3.org/2004/02/skos/core#> 

SELECT *

WHERE {?s skos:broader*  <http://purl.obolibrary.org/obo/DOID_4159>;
          
          skos:prefLabel ?prefLabel 
         }
   
s                                           prefLabel
<http://purl.obolibrary.org/obo/DOID_4159>  skin cancer
<http://purl.obolibrary.org/obo/DOID_3451>  skin carcinoma
<http://purl.obolibrary.org/obo/DOID_6944>  vulvar seborrheic keratosis