Skip to content

Commit

Permalink
Test more things in each query using shacl
Browse files Browse the repository at this point in the history
  • Loading branch information
JervenBolleman committed May 21, 2024
1 parent 93d2632 commit 24e1577
Show file tree
Hide file tree
Showing 50 changed files with 148 additions and 64 deletions.
2 changes: 2 additions & 0 deletions glyconnect/1.ttl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
prefix ex: <https://glyconnect.expasy.org/.well-known/sparql-examples>
prefix up: <http://purl.uniprot.org/core/>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix schema: <https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>

ex:1
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes ex: ;
schema:target <https://glyconnect.expasy.org/sparql> ;
rdfs:comment """Get all Glyconnect proteins their Uniprot reference isoform""" ;
sh:select """SELECT distinct ?glycoprotein ?isoform
where {
Expand Down
2 changes: 2 additions & 0 deletions glyconnect/2.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ prefix ex: <https://glyconnect.expasy.org/.well-known/sparql-examples>
prefix up: <http://purl.uniprot.org/core/>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix schema: <https://schema.org/>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>

ex:2
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes ex: ;
schema:target <https://glyconnect.expasy.org/sparql> ;
rdfs:comment """Get all Glyconnect glycan with their graphical SNFG representation""" ;
sh:select """SELECT distinct ?structure ?image
where {
Expand Down
2 changes: 2 additions & 0 deletions glyconnect/3.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ prefix ex: <https://glyconnect.expasy.org/sparql>
prefix up: <http://purl.uniprot.org/core/>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix schema: <https://schema.org/>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>

ex:3
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes ex: ;
schema:target <https://glyconnect.expasy.org/sparql> ;
rdfs:comment """Select all glycosylations (glycan, protein, position) with their reference articles""" ;
sh:select """SELECT distinct ?glycoprotein ?isoform ?position ?structure ?image ?citation ?pmid
where {
Expand Down
2 changes: 2 additions & 0 deletions glyconnect/4.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ prefix ex: <https://glyconnect.expasy.org/.well-known/sparql-examples>
prefix up: <http://purl.uniprot.org/core/>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix schema: <https://schema.org/>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>

ex:4
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes ex: ;
schema:target <https://glyconnect.expasy.org/sparql> ;
rdfs:comment """Select all glycosylations (glycan, protein, position) with their associated disease where the name start with 'cancer'""" ;
sh:select """SELECT distinct ?glycoprotein ?isoform ?position ?structure ?disease ?diseasename
where {
Expand Down
2 changes: 2 additions & 0 deletions glyconnect/5.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ prefix up: <http://purl.uniprot.org/core/>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix schema: <https://schema.org/>

ex:5
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
schema:target <https://glyconnect.expasy.org/sparql> ;
sh:prefixes ex: ;
rdfs:comment """Get all Glyconnect reference papers""" ;
sh:select """SELECT distinct ?citation ?pmid
Expand Down
3 changes: 3 additions & 0 deletions metanetx/1.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ prefix ex: <https://sparql.metanetx.org/.well-known/sparql-examples/>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix schema: <https://schema.org/>

ex:1
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes ex: ;
schema:target <https://rdf.metanetx.org/sparql/> ;
rdfs:comment """Retrieve the MNXref metabolite with name *N,N-dimethyl-beta-alanine*, together with molecular information.""" ;
sh:select """SELECT ?metabolite ?label ?source ?formula ?charge ?inchi ?inchikey ?smiles
WHERE {
Expand Down
24 changes: 17 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-bom</artifactId>
<version>${rdf4j.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand All @@ -54,43 +61,46 @@
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-turtle</artifactId>
<version>${rdf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-model</artifactId>
<version>${rdf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-model-api</artifactId>
<version>${rdf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-model-vocabulary</artifactId>
<version>${rdf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-sail-model</artifactId>
<version>${rdf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-sail-memory</artifactId>
<version>${rdf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-repository-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-repository-sail</artifactId>
<version>${rdf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-shacl</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,58 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.stream.Stream;

import org.eclipse.rdf4j.common.exception.ValidationException;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
import org.eclipse.rdf4j.model.vocabulary.SHACL;
import org.eclipse.rdf4j.query.MalformedQueryException;
import org.eclipse.rdf4j.query.parser.QueryParser;
import org.eclipse.rdf4j.query.parser.sparql.SPARQLParserFactory;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.helpers.StatementCollector;
import org.eclipse.rdf4j.rio.turtle.TurtleWriter;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.eclipse.rdf4j.sail.shacl.ShaclSail;

public class CreateTestWithRDF4jMethods {
private static final String shaclOneIdOneQuery = """
PREFIX sh:<http://www.w3.org/ns/shacl#>
PREFIX schema:<https://schema.org/>
[] sh:targetClass sh:SPARQLExecutable ;
sh:property [
sh:path [ sh:alternativePath ( sh:select sh:ask sh:describe sh:construct ) ] ;
sh:maxCount 1 ;
sh:minCount 1
] , [
sh:path rdfs:comment ;
sh:maxCount 1 ;
sh:minCount 1 ] , [
sh:path schema:target;
sh:minCount 1 ].
""";

static void testQueryValid(Path p, String projectPrefixes) {
assertTrue(Files.exists(p));
Expand All @@ -40,9 +70,8 @@ static void testQueryValid(Path p, String projectPrefixes) {
}
assertFalse(model.isEmpty());
QueryParser parser = new SPARQLParserFactory().getParser();
Stream.of("ask", "select", "concat", "describe")
.map(s -> model.getStatements(null,
SimpleValueFactory.getInstance().createIRI(SHACL.NAMESPACE, s), null))
Stream.of("ask", "select", "concat", "describe").map(
s -> model.getStatements(null, SimpleValueFactory.getInstance().createIRI(SHACL.NAMESPACE, s), null))
.map(Iterable::iterator).forEach(i -> {
while (i.hasNext()) {
Value obj = i.next().getObject();
Expand All @@ -60,4 +89,67 @@ static void testQueryValid(Path p, String projectPrefixes) {

}

/**
* Use shacl to test all the turtle files contain an rdfs:comment and one query.
* Also makes a test that all example IRIs are unique.
*
* @param paths
*/
static void testShaclContsraints(Stream<Path> paths) {
MemoryStore memoryStore = new MemoryStore();
try {
memoryStore.init();

ShaclSail shaclSail = new ShaclSail(memoryStore);
Repository repo = new SailRepository(shaclSail);
try (RepositoryConnection connection = repo.getConnection();
StringReader sr = new StringReader(shaclOneIdOneQuery)) {
// add shapes
connection.begin();
connection.add(sr, RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
connection.commit();
} catch (RDFParseException | RepositoryException | IOException e) {
fail(e);
}
try (RepositoryConnection connection = repo.getConnection()) {
paths.forEach(p -> testValidatingAFile(connection, p));
}
repo.shutDown();
shaclSail.shutDown();
} finally {
memoryStore.shutDown();
}
}

private static void testValidatingAFile(RepositoryConnection connection, Path p) {
assertTrue(Files.exists(p));
connection.begin();
try {
IRI iri = connection.getValueFactory().createIRI(p.toUri().toString());
connection.add(p.toFile(), iri);
connection.commit();
} catch (RDFParseException | RepositoryException | IOException e) {
if (e.getCause() instanceof ValidationException ve) {
String report = validationReportAsString(ve);
fail(p.toUri() + " failed " + ve + '\n'+ report, e.getCause());
}
fail(e);
}
assertFalse(connection.size() == 0);
}

private static String validationReportAsString(ValidationException ve) {
Model vem = ve.validationReportAsModel();
var boas = new ByteArrayOutputStream();
TurtleWriter tw = new TurtleWriter(boas);
tw.startRDF();
Iterator<Statement> iterator = vem.iterator();
while(iterator.hasNext()) {
tw.handleStatement(iterator.next());
}
tw.endRDF();
String report = boas.toString();
return report;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RiotException;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;

Expand Down Expand Up @@ -51,11 +52,21 @@ public Stream<DynamicTest> testAllWithRDF4j() throws URISyntaxException, IOExcep
return testAll(tester);
}

@Test
public void testUniqueIdentifierPerQueries() throws URISyntaxException, IOException {
CreateTestWithRDF4jMethods.testShaclContsraints(Files.walk(getBasePath(), 5).filter(this::isTurtleButNotPrefixFile));
}

@TestFactory
public Stream<DynamicTest> testPrefixDeclarations() throws URISyntaxException, IOException {
Path basePath = getBasePath();
return Files.walk(basePath, 5).filter(this::isTurtleAndPrefixFile).flatMap(this::testPrefixes);
}

private Path getBasePath() throws URISyntaxException {
URL baseDir = getClass().getResource("/");
Path basePath = Paths.get(baseDir.toURI());
return Files.walk(basePath, 5).filter(this::isTurtleAndPrefixFile).flatMap(this::testPrefixes);
return basePath;
}

private Stream<DynamicTest> testAll(BiFunction<Path, String, Executable> tester)
Expand Down
1 change: 0 additions & 1 deletion uniprot/10.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix faldo:<http://biohackathon.org/resource/faldo#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix schema:<http://schema.org/>
ex:10
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
1 change: 0 additions & 1 deletion uniprot/11.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix faldo:<http://biohackathon.org/resource/faldo#>
prefix schema:<http://schema.org/>
ex:11
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
1 change: 1 addition & 0 deletions uniprot/15.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
ex:15
a sh:SPARQLExecutable, sh:SPARQLExecutable ;
schema:target <https://sparql.uniprot.org/sparql/> ;
rdfs:comment """Select all triples that relate to the EMBL CDS entry AA089367.1: """;
sh:describe """DESCRIBE <http://purl.uniprot.org/embl-cds/AAO89367.1>
""" .
1 change: 0 additions & 1 deletion uniprot/21.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix skos:<http://www.w3.org/2004/02/skos/core#>
prefix schema:<http://schema.org/>
ex:21
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
1 change: 0 additions & 1 deletion uniprot/27.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ prefix sh: <http://www.w3.org/ns/shacl#>
prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix schema:<http://schema.org/>
ex:27
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
2 changes: 1 addition & 1 deletion uniprot/28.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ prefix sh: <http://www.w3.org/ns/shacl#>
prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix schema:<http://schema.org/>

ex:28
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
1 change: 0 additions & 1 deletion uniprot/29.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix allie: <http://allie.dbcls.jp/>
prefix schema:<http://schema.org/>
ex:29
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
1 change: 0 additions & 1 deletion uniprot/30.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix taxon:<http://purl.uniprot.org/taxonomy/>
prefix schema:<http://schema.org/>
ex:30
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
1 change: 0 additions & 1 deletion uniprot/31.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ prefix schema:<https://schema.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix taxon:<http://purl.uniprot.org/taxonomy/>
prefix schema:<http://schema.org/>
ex:31
a sh:SPARQLSelectExecutable, sh:SPARQLExecutable ;
sh:prefixes _:sparql_examples_prefixes ;
Expand Down
Loading

0 comments on commit 24e1577

Please sign in to comment.