SPARQL

SPARQL SCRIPTS

ONTODerm SPARQL Library

1. Download sparqllib.php Library
2. Cut and paste the following code into a file and name it ontodermlib.php
3. Add Version number of ONTODerm from here http://knoodl.com/ui/index/vocab/groups/ONTODerm in brackets.
eg. Version 2 used below
private $odep = "http://knoodl.com/ui/groups/ONTODerm/vocab/ONTODerm(2)/sparql/";
4. A sample term lookup code is below.
5. A back link will be highly appreciated if you use this code.
6. This is part of DermKnowledgeBASE and ONTODerm projects.
7. Please read disclaimer before you use this.


ontodermlib.php
<?php
require_once( "sparqllib.php" );

class 
ONTODerm{

//Settings
private $odns "http://gulfdoctor.net/ontoderm/ontoderm.owl#";
//Add Version number of ONTODerm from here http://knoodl.com/ui/index/vocab/groups/ONTODerm in brackets. eg. Version 2 used below
//private $odep = "http://knoodl.com/ui/groups/ONTODerm/vocab/ONTODerm(2)/sparql/";
private $odep "http://knoodl.com/ui/groups/ONTODerm/vocab/ONTODerm(2)/";

// SPARQL Limit
private $limit 200;

//properties
private $sparql;
private 
$offset;
private 
$db;
private 
$results;
private 
$rows_fetched;

public function 
connect(){
    
$this->db sparql_connect($this->odep);
    if( !
$this->db ) { print $this->db->errno() . ": " $this->db->error(). "\n"; exit; }
    
$this->db->ns"foaf","http://xmlns.com/foaf/0.1/" );
    
$this->offset=0;
}

public function 
set_sparql($query){
    
$this->sparql $query;
}

public function 
reset(){
    
$this->offset =0;
}

public function 
get_offset(){
    return 
$this->offset;
}

private function 
search(){
    
$result $this->db->query($this->sparql); 
    if( !
$result ) { print $this->db->errno() . ": " $this->db->error(). "\n"; exit; }else{$this->results=$result;}
    
$this->rows_fetched $result->num_rows$result );

}

public function 
num_rows(){
    return 
$this->rows_fetched;
}

public function 
next(){
    if(
$this->limit == $this->rows_fetched){$this->offset += $this->limit; }
}

public function 
previous(){
    if(
$this->offset >= $this->limit){$this->offset -= $this->limit; }
}

public function 
get_classesAndProperties(){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s ?p ?o. } LIMIT ".$this->limit." OFFSET ".$this->offset;
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;
}

public function 
get_labels(){
    
$this->sparql "SELECT DISTINCT ?o WHERE { ?s <http://www.w3.org/2000/01/rdf-schema#label> ?o. } LIMIT ".$this->limit." OFFSET ".$this->offset;
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
array_push($output$row['o']);
    }
    return 
$output;

}

public function 
label2class($label){
    
$this->sparql "SELECT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. ?s ?p '".$label."'.} LIMIT 1";
    
$this->search();
    
$row $this->results->fetch_array();
    
$start strpos($row['s'], '#');
    return 
substr($row['s'], $start+1);
}

public function 
string2class($str){
    
$class=ucwords ($str);
    
$class=preg_replace('/[\s]+/','',$class);
    return 
$class;
}

public function 
get_classes(){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. ?s ?p ?o. } LIMIT ".$this->limit." OFFSET ".$this->offset;
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
get_properties(){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#ObjectProperty>. ?s ?p ?o. } LIMIT ".$this->limit." OFFSET ".$this->offset;
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
equivalent_class($class){
    
$this->sparql "SELECT DISTINCT ?s WHERE { { ?s <http://www.w3.org/2002/07/owl#equivalentClass> <".$this->odns.$class."> } UNION { <".$this->odns.$class."> <http://www.w3.org/2002/07/owl#equivalentClass> ?s }}";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
equivalent_property($property){
    
$this->sparql "SELECT DISTINCT ?s WHERE { { ?s <http://www.w3.org/2002/07/owl#equivalentProperty> <".$this->odns.$property."> } UNION { <".$this->odns.$property."> <http://www.w3.org/2002/07/owl#equivalentProperty> ?s }}";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
disjoint($class){
    
$this->sparql "SELECT DISTINCT ?s WHERE { { ?s <http://www.w3.org/2002/07/owl#disjointWith> <".$this->odns.$class."> } UNION { <".$this->odns.$class."> <http://www.w3.org/2002/07/owl#disjointWith> ?s }}";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
inverse($property){
    
$this->sparql "SELECT DISTINCT ?s WHERE { { ?s <http://www.w3.org/2002/07/owl#inverseOf> <".$this->odns.$property."> } UNION { <".$this->odns.$property."> <http://www.w3.org/2002/07/owl#inverseOf> ?s }}";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
superclasses(){
    
$this->sparql "SELECT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. OPTIONAL { ?s <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?sc} FILTER(!bound(?sc) && isURI(?s))}";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
superclass($class){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. <".$this->odns.$class."> <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s. }";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
superproperty($property){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#ObjectProperty>. <".$this->odns.$property."> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> ?s. }";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
domain($property){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. <".$this->odns.$property."> <http://www.w3.org/2000/01/rdf-schema#domain> ?s. }";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
range($property){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. <".$this->odns.$property."> <http://www.w3.org/2000/01/rdf-schema#range> ?s. }";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
subclasses($class){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. ?s <http://www.w3.org/2000/01/rdf-schema#subClassOf> <".$this->odns.$class.">. }";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
subproperties($property){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#ObjectProperty>. ?s <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <".$this->odns.$class.">. }";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
siblings($class){
    
$this->sparql "SELECT DISTINCT ?s WHERE { ?s a <http://www.w3.org/2002/07/owl#Class>. <".$this->odns.$class."> <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?sib. ?s <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?sib.}";
    
$this->search();
    
$output = array();
    while( 
$row $this->results->fetch_array() ){
        
$start strpos($row['s'], '#');
        if(
$start>0){array_push($outputsubstr($row['s'], $start+1));}
    }
    return 
$output;

}

public function 
get_label($class){
    
$this->sparql "SELECT ?s WHERE { <".$this->odns.$class."> <http://www.w3.org/2000/01/rdf-schema#label> ?s.} LIMIT 1";
    
$this->search();
    
$row $this->results->fetch_array();
    return 
$row['s'];

}

public function 
ask($class){
    
$this->sparql "SELECT ?s WHERE { <".$this->odns.$class."> a ?s.} LIMIT 1";
    
$this->search();
    
$row $this->results->fetch_array();
    return 
$row['s'];
}

public function 
mainclass($class){
    
$this->sparql "SELECT * WHERE{
<"
.$this->odns.$class."> <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s0.
OPTIONAL {?s0 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s1}
OPTIONAL {?s1 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s2} 
OPTIONAL {?s2 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s3} 
OPTIONAL {?s3 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s4} 
OPTIONAL {?s4 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s5} 
OPTIONAL {?s5 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s6} 
OPTIONAL {?s6 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s7} 
OPTIONAL {?s7 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s8} 
OPTIONAL {?s8 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s9} 
OPTIONAL {?s9 <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?s10} 
    }"
;
    
//$this->sparql = "SELECT ?s WHERE { <".$this->odns.$class."> a ?s.} LIMIT 1";
    
$this->search();
    
$row $this->results->fetch_array();
    for(
$x=0;$x<10;$x++){    
        
$index="s$x";
        if(
$row[$index]=="" && $x>0){
            
$y=$x-1;
            
$index="s$y";
            
$start strpos($row[$index], '#');
            return 
substr($row[$index], $start+1);
        }

    }
    return 
"null";
}

}
?>

odlookup.php
<?php
require_once( "sparqllib.php" );
require_once( 
"ontodermlib.php" );

$term $_REQUEST['term'];

if(
$term==""){
?>
    <p></p>
<p><h1>Dermatology Term Lookup</h1></p>
<p>This is the new version of Dermatology Term Lookup (DTL). The previous version was based on the perl library RDF::Core. This version uses SPARQL to query ONTODerm SPARQL Endpoint at http://knoodl.com/ui/groups/ONTODerm/vocab/ONTODerm/sparql</p>

<p>ONTODerm is hosted by knoodl  the first Distributed Information Management System (DIMS).  If you have domain knowledge in dermatology and wish to get involved in ONTODerm development, join the ONTODerm community <a href='http://knoodl.com/ui/groups/ONTODerm'>http://knoodl.com/ui/groups/ONTODerm</a> or contact <img src='http://gulfdoctor.net/button/email.php?text=ontoderm*gulfdoctor.net'></p>
    <form name="termlookup" action="odlookup.php" method="POST">Term: <input type="text"
        name="term" size="50" maxlength="50" /> <input type="submit" name="termlookuo"
        value="Submit" formmethod="post" />Example: Bacterial
      Cellulitis<br />
    </form>
    <p><br />
    </p>


<?php
}else{

echo 
"<p><b><h1>$term</h1></b></p><p></p>";
$term strtolower($term);

$od = new ONTODerm();
$od->connect();
$term=$od->string2class($term);

echo 
"<p><b>SuperClass</b></p>";
$classes $od->superclass($term);
foreach (
$classes as $class){
    echo 
"<a href='odlookup.php?term=$class'>$class</a><br>";
}

echo 
"<p><b>SubClass</b></p>";
$classes $od->subclasses($term);
foreach (
$classes as $class){
    echo 
"<a href='odlookup.php?term=$class'>$class</a><br>";
}

echo 
"<p><b>Sibling</b></p>";
$classes $od->siblings($term);
foreach (
$classes as $class){
    echo 
"<a href='odlookup.php?term=$class'>$class</a><br>";
}

echo 
"<p><b>Equivalent</b></p>";
$classes $od->equivalent_class($term);
foreach (
$classes as $class){
    echo 
"<a href='odlookup.php?term=$class'>$class</a><br>";
}

echo 
"<p><b>Disjoint</b></p>";
$classes $od->disjoint($term);
foreach (
$classes as $class){
    echo 
"<a href='odlookup.php?term=$class'>$class</a><br>";
}

}
?>


|


This is an experimental application for healthcare professionals. The information presented here is not intended to diagnose, treat, cure or prevent any disease. Read disclaimer.

SkinHelpDesk.com - Evidence based skincare free

About Me

I have varied research interests ranging from eHealth, Health Information Exchange, Clinical Trials and Research, Contact Dermatitis, Bioinformatics, and Cosmetic Dermatology. I have work experience in Canada as an eHealth analyst, and in Dubai and India as a Specialist Dermatologist.

Address

Bell Raj Eapen
Hamilton, ON
Canada