Skip to content

Commit

Permalink
Merge pull request #506 from phenoscape/minor-cleanups
Browse files Browse the repository at this point in the history
Minor cleanup and warnings fixes.
  • Loading branch information
balhoff authored Dec 1, 2021
2 parents b99f146 + a4aaaa3 commit 0a6508f
Show file tree
Hide file tree
Showing 27 changed files with 168 additions and 231 deletions.
18 changes: 4 additions & 14 deletions src/main/scala/org/phenoscape/kb/AnatomicalEntity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.MediaTypes
import org.apache.jena.query.{QueryFactory, QuerySolution}
import org.phenoscape.kb.KBVocab.{rdfsSubClassOf, _}
import org.phenoscape.kb.Similarity.rdfsSubClassOf
import org.phenoscape.owl.Vocab._
import org.phenoscape.scowl._
import org.phenoscape.sparql.SPARQLInterpolation._
import org.phenoscape.sparql.SPARQLInterpolationOWL._
import org.phenoscape.kb.util.SPARQLInterpolatorOWLAPI._
import org.phenoscape.kb.util.SPARQLInterpolatorBlazegraph._
import org.phenoscape.owl.{NamedRestrictionGenerator, Vocab}
import org.semanticweb.owlapi.model.IRI
import spray.json.DefaultJsonProtocol._
Expand Down Expand Up @@ -71,25 +70,21 @@ object AnatomicalEntity {
}

// Output a boolean matrix as CSV
def matrixRendererFromMapOfMaps[A](dependencyMatrix: DependencyMatrix[A]) = {
def matrixRendererFromMapOfMaps[A](dependencyMatrix: DependencyMatrix[A]): String = {

val mapOfMaps = dependencyMatrix.map
val orderedKeys = dependencyMatrix.orderedKeys
val headers = s",${orderedKeys.mkString(",")}" //print column headers

val matrix = for (x <- orderedKeys) yield {
val row = s"$x"
val values = for (y <- orderedKeys) yield mapOfMaps(x)(y) match {
case true => 1
case false => 0
}
val values = for (y <- orderedKeys) yield (if (mapOfMaps(x)(y)) 1 else 0)
s"$row,${values.mkString(",")}"
}
s"$headers\n${matrix.mkString("\n")}\n"
}

def presenceAbsenceDependencyMatrix(iris: List[IRI]): Future[DependencyMatrix[IRI]] = {
import org.phenoscape.kb.util.Util.TraversableOps
import org.phenoscape.kb.util.Util.MapOps
val query = queryImpliesPresenceOfMulti(iris)
for {
Expand Down Expand Up @@ -123,21 +118,16 @@ object AnatomicalEntity {
"""

private def queryImpliesPresenceOfMulti(terms: Iterable[IRI]): QueryText = {
import scalaz._
import Scalaz._
val valuesList = terms.map(t => sparql" $t ").fold(sparql"")(_ + _)
sparql"""
SELECT DISTINCT ?x ?y
FROM $KBClosureGraph
FROM $KBMainGraph
FROM $KBRedundantRelationGraph
WHERE {
VALUES ?x { $valuesList }
VALUES ?x { $valuesList }
VALUES ?y { $valuesList }
?x $rdfsSubClassOf | $IMPLIES_PRESENCE_OF ?y .
FILTER(?x != ?y)
}
"""
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/phenoscape/kb/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.phenoscape.owlet.SPARQLComposer._
import org.phenoscape.sparql.FromQuerySolution
import org.semanticweb.owlapi.model.IRI

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.language.postfixOps
Expand Down
110 changes: 63 additions & 47 deletions src/main/scala/org/phenoscape/kb/CharacterDescription.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import org.phenoscape.owlet.SPARQLComposer._
import org.semanticweb.owlapi.model.{IRI, OWLClassExpression}
import spray.json.DefaultJsonProtocol._
import spray.json._
import org.phenoscape.kb.KBVocab.KBMainGraph

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import scala.concurrent.Future
import org.phenoscape.sparql.SPARQLInterpolation._
import org.phenoscape.sparql.SPARQLInterpolationOWL._

object CharacterDescription {

Expand Down Expand Up @@ -58,12 +61,16 @@ object CharacterDescription {
App.executeSPARQLQuery(buildCharacterDescriptionQuery(iri), fromQuerySolution(iri)).map(_.headOption)

def annotatedCharacterDescriptionWithAnnotation(iri: IRI): Future[Option[AnnotatedCharacterDescription]] = {
val query = select_distinct('state, 'description, 'matrix, 'matrix_label) where bgp(
t('state, describes_phenotype, iri),
t('state, dcDescription, 'description),
t('matrix, has_character / may_have_state_value, 'state),
t('matrix, rdfsLabel, 'matrix_label)
)
val query =
sparql"""
SELECT DISTINCT ?state ?description ?matrix ?matrix_label
WHERE {
?state $describes_phenotype $iri .
?state $dcDescription ?description .
?matrix $has_character/$may_have_state_value ?state .
?matrix $rdfsLabel ?matrix_label .
}
""".toQuery
val result = App.executeSPARQLQuery(
query,
result =>
Expand All @@ -84,59 +91,68 @@ object CharacterDescription {
result.flatMap(Future.sequence(_)).map(_.headOption)
}

def buildBasicQuery(entity: OWLClassExpression = owlThing,
taxon: OWLClassExpression = owlThing,
publications: Iterable[IRI] = Nil): Query = {
val entityPatterns =
if (entity == owlThing) Nil
def buildWhereClause(entity: OWLClassExpression = owlThing,
taxon: OWLClassExpression = owlThing,
publications: Iterable[IRI] = Nil): QueryText = {
val entityPattern =
if (entity == owlThing) sparql""
else sparql"""
?phenotype $ps_entity_term|$ps_related_entity_term ?entity .
?entity $rdfsSubClassOf ${entity.asOMN}
"""
val taxonPattern =
if (taxon == owlThing) sparql""
else
t('phenotype, ps_entity_term | ps_related_entity_term, 'entity) :: t('entity,
rdfsSubClassOf,
entity.asOMN) :: Nil
val taxonPatterns =
if (taxon == owlThing) Nil
else
t('taxon, exhibits_state, 'state) :: t('taxon, rdfsSubClassOf, taxon.asOMN) :: Nil
val filters =
if (publications.isEmpty) Nil
else
new ElementFilter(
new E_OneOf(new ExprVar('matrix),
new ExprList(publications.map(new NodeValueNode(_)).toBuffer[Expr].asJava))) :: Nil
select_distinct() from "http://kb.phenoscape.org/" where (bgp(
t('state, dcDescription, 'state_desc) ::
t('state, describes_phenotype, 'phenotype) ::
t('matrix, has_character / may_have_state_value, 'state) ::
t('matrix, rdfsLabel, 'matrix_label) ::
entityPatterns ++
taxonPatterns: _*
) ::
filters: _*)
sparql"""
?taxon $exhibits_state ?state .
?taxon $rdfsSubClassOf ${taxon.asOMN} .
"""
val publicationsFilter =
if (publications.isEmpty) sparql""
else {
val pubsList = publications.map(p => sparql"$p").reduceOption((l, r) => sparql"$l, $r").getOrElse(sparql"")
sparql"""
FILTER(?matrix IN ($pubsList))
"""
}
sparql"""
WHERE {
?state $dcDescription ?state_desc .
?state $describes_phenotype ?phenotype .
?matrix $has_character/$may_have_state_value ?state .
?matrix $rdfsLabel ?matrix_label .
$entityPattern
$taxonPattern
$publicationsFilter
}
"""
}

def buildQuery(entity: OWLClassExpression = owlThing,
taxon: OWLClassExpression = owlThing,
publications: Iterable[IRI] = Nil,
limit: Int = 20,
offset: Int = 0): Query = {
val query = buildBasicQuery(entity, taxon, publications)
query.addResultVar('state)
query.addResultVar('state_desc)
query.addResultVar('matrix)
query.addResultVar('matrix_label)
query.setOffset(offset)
query.setLimit(limit)
query.addOrderBy('state_desc)
query.addOrderBy('state)
query
val whereClause = buildWhereClause(entity, taxon, publications)
sparql"""
SELECT DISTINCT ?state ?state_desc ?matrix ?matrix_label
FROM $KBMainGraph
$whereClause
ORDER BY ?state_desc ?state
LIMIT $limit
OFFSET $offset
""".toQuery
}

def buildTotalQuery(entity: OWLClassExpression = owlThing,
taxon: OWLClassExpression = owlThing,
publications: Iterable[IRI] = Nil): Query = {
val query = buildBasicQuery(entity, taxon, publications)
query.getProject.add(Var.alloc("count"), query.allocAggregate(new AggCountVarDistinct(new ExprVar("state"))))
query
val whereClause = buildWhereClause(entity, taxon, publications)
sparql"""
SELECT (COUNT(DISTINCT(?state) AS ?count)
FROM $KBMainGraph
$whereClause
""".toQuery
}

def buildVariationProfileTotalQuery(taxa: Seq[IRI]): Query = {
Expand Down
43 changes: 18 additions & 25 deletions src/main/scala/org/phenoscape/kb/EQForGene.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
package org.phenoscape.kb

import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.language.postfixOps

import akka.util.Timeout
import org.apache.jena.query.Query
import org.phenoscape.owl.NamedRestrictionGenerator
import org.phenoscape.owl.Vocab._
import org.phenoscape.kb.KBVocab._
import org.phenoscape.owlet.SPARQLComposer._
import org.phenoscape.scowl._

import org.phenoscape.owl.Vocab._
import org.phenoscape.sparql.SPARQLInterpolation._
import org.phenoscape.kb.util.SPARQLInterpolatorOWLAPI._
import org.phenoscape.sparql.SPARQLInterpolationOWL._
import org.phenoscape.sparql.SPARQLInterpolation.QueryText

import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.model.IRI

import com.google.common.collect.HashMultiset

import akka.util.Timeout
import spray.json._
import org.phenoscape.sparql.SPARQLInterpolationOWL._
import spray.json.DefaultJsonProtocol._
import spray.json._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.language.postfixOps

object EQForGene {

Expand Down Expand Up @@ -56,10 +44,15 @@ object EQForGene {
App.executeSPARQLQuery(annotationsQuery(geneID), _.getResource("association").getURI)

def annotationsQuery(geneIRI: IRI): Query =
select_distinct('association) from "http://kb.phenoscape.org/" where bgp(
t('association, rdfType, association),
t('association, associationHasPredicate, has_phenotype),
t('association, associationHasSubject, geneIRI))
sparql"""
SELECT DISTINCT ?association
FROM $KBMainGraph
WHERE {
?association $rdfType $association .
?association $associationHasPredicate $has_phenotype .
?association $associationHasSubject $geneIRI .
}
""".toQuery

def qualitiesForAnnotation(annotationID: String): Future[Iterable[String]] =
App.executeSPARQLQuery(annotationSuperQualityQuery(annotationID), _.getResource("quality").getURI)
Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/org/phenoscape/kb/Gene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.phenoscape.kb

import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.MediaTypes
import org.apache.jena.graph.NodeFactory
import org.apache.jena.query.{Query, QuerySolution}
import org.apache.jena.rdf.model.{Model, Property, Resource, ResourceFactory}
import org.apache.jena.sparql.core.Var
Expand All @@ -25,10 +24,10 @@ import spray.json.DefaultJsonProtocol._
import spray.json._
import org.phenoscape.sparql.SPARQLInterpolation.{QueryText, _}
import org.phenoscape.sparql.SPARQLInterpolationOWL._
import org.phenoscape.kb.util.SPARQLInterpolatorOWLAPI._
import org.phenoscape.kb.util.SPARQLInterpolatorBlazegraph._
import org.phenoscape.owl.Vocab.{AnnotatedPhenotype, GeneExpression, associated_with_gene, associated_with_taxon, dcSource, has_part, in_taxon, inheres_in, occurs_in, part_of, rdfType, towards}

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import scala.concurrent.Future
import scala.language.implicitConversions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import akka.http.scaladsl.model.MediaTypes
import akka.util.Timeout
import org.apache.jena.query.{Query, QuerySolution}
import org.apache.jena.sparql.core.Var
import org.apache.jena.sparql.expr.{E_NotOneOf, Expr, ExprList, ExprVar}
import org.apache.jena.sparql.expr.aggregate.AggCountDistinct
import org.apache.jena.sparql.expr.nodevalue.NodeValueNode
import org.apache.jena.sparql.expr.{E_NotOneOf, Expr, ExprList, ExprVar}
import org.apache.jena.sparql.syntax.{ElementFilter, ElementSubQuery}
import org.phenoscape.kb.JSONResultItem.JSONResultItemsMarshaller
import org.phenoscape.kb.KBVocab.{rdfsLabel, rdfsSubClassOf, _}
import org.phenoscape.kb.Main.system.dispatcher
import org.phenoscape.kb.JSONResultItem.JSONResultItemsMarshaller
import org.phenoscape.owl.Vocab._
import org.phenoscape.owlet.OwletManchesterSyntaxDataType.SerializableClassExpression
import org.phenoscape.owlet.SPARQLComposer._
Expand All @@ -20,9 +20,9 @@ import org.semanticweb.owlapi.model.{IRI, OWLClassExpression}
import spray.json.DefaultJsonProtocol._
import spray.json._

import scala.collection.JavaConverters._
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.jdk.CollectionConverters._
import scala.language.postfixOps

case class GeneExpressionAnnotation(gene: MinimalTerm, location: MinimalTerm, source: Option[IRI])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import org.apache.jena.sparql.expr._
import org.apache.jena.sparql.expr.aggregate.AggCountDistinct
import org.apache.jena.sparql.expr.nodevalue.NodeValueNode
import org.apache.jena.sparql.syntax.{ElementFilter, ElementSubQuery}
import org.phenoscape.kb.JSONResultItem.JSONResultItemsMarshaller
import org.phenoscape.kb.KBVocab.{rdfsLabel, rdfsSubClassOf, _}
import org.phenoscape.kb.Main.system.dispatcher
import org.phenoscape.kb.JSONResultItem.JSONResultItemsMarshaller
import org.phenoscape.owl.Vocab._
import org.phenoscape.owlet.OwletManchesterSyntaxDataType.SerializableClassExpression
import org.phenoscape.owlet.SPARQLComposer._
Expand All @@ -20,8 +20,8 @@ import org.semanticweb.owlapi.model.{IRI, OWLClassExpression}
import spray.json.DefaultJsonProtocol._
import spray.json._

import scala.collection.JavaConverters._
import scala.concurrent.Future
import scala.jdk.CollectionConverters._
import scala.language.postfixOps

case class GenePhenotypeAnnotation(gene: MinimalTerm, phenotype: MinimalTerm, source: Option[IRI])
Expand Down
13 changes: 3 additions & 10 deletions src/main/scala/org/phenoscape/kb/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ import org.semanticweb.owlapi.model.IRI
import org.phenoscape.kb.KBVocab.{rdfsLabel, rdfsSubClassOf, _}
import org.phenoscape.owl.Vocab.rdfType
import org.phenoscape.kb.Main.system.dispatcher
import org.phenoscape.owl.NamedRestrictionGenerator
import org.phenoscape.owlet.SPARQLComposer._
import org.phenoscape.sparql.SPARQLInterpolation._
import org.phenoscape.sparql.SPARQLInterpolation.QueryText
import org.phenoscape.kb.util.SPARQLInterpolatorOWLAPI._
import org.phenoscape.scowl
import org.phenoscape.sparql.SPARQLInterpolationOWL._

import scala.concurrent.Future
import scala.language.postfixOps
import java.net.{URLDecoder, URLEncoder}

object Graph {

Expand Down Expand Up @@ -148,15 +142,14 @@ object Graph {
val termSubsumerSeq = for {
pairs <- futurePairs
} yield {
val termSubsumerPairs = pairs.map(p => (p._1 -> (p._2, p._3)))
termSubsumerPairs.map { case (term, (relation, subsumer)) =>
val termSubsumerPairs = pairs.map(p => p._1 -> (p._2, p._3))
termSubsumerPairs.flatMap { case (term, (relation, subsumer)) =>
val virtualTermIRI = relation match {
case "http://www.w3.org/2000/01/rdf-schema#subClassOf" => IRI.create(subsumer)
case _ => RelationalTerm(IRI.create(relation), IRI.create(subsumer)).iri

}
Map(IRI.create(term) -> virtualTermIRI)
}.flatten
}
}
termSubsumerSeq
}
Expand Down
Loading

0 comments on commit 0a6508f

Please sign in to comment.