<commit_before>package org.phenoscape.kb

import scala.concurrent.Future
import org.phenoscape.owl.Vocab._
import org.phenoscape.kb.KBVocab.rdfsSubClassOf
import org.phenoscape.owlet.SPARQLComposer._
import org.phenoscape.scowl.OWL._
import org.semanticweb.owlapi.model.IRI
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary.RDFS_LABEL
import com.hp.hpl.jena.graph.NodeFactory
import com.hp.hpl.jena.query.Query
import com.hp.hpl.jena.query.QuerySolution
import com.hp.hpl.jena.sparql.expr.E_IsIRI
import com.hp.hpl.jena.sparql.expr.ExprVar
import com.hp.hpl.jena.sparql.syntax.ElementFilter
import com.hp.hpl.jena.vocabulary.RDFS
import com.hp.hpl.jena.vocabulary.OWL2
import spray.http._
import spray.httpx._
import spray.httpx.SprayJsonSupport._
import spray.httpx.marshalling._
import spray.json.DefaultJsonProtocol._
import spray.json._
import com.hp.hpl.jena.sparql.expr.ExprList
import com.hp.hpl.jena.sparql.expr.E_OneOf
import scala.collection.JavaConversions._
import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueNode
import org.phenoscape.kb.KBVocab._
import org.phenoscape.kb.KBVocab.rdfsLabel
import org.phenoscape.kb.Main.system.dispatcher
import org.semanticweb.owlapi.model.OWLClassExpression
import org.phenoscape.owlet.OwletManchesterSyntaxDataType.SerializableClassExpression
import com.hp.hpl.jena.graph.Node
import org.semanticweb.owlapi.apibinding.OWLManager
import com.hp.hpl.jena.sparql.expr.aggregate.AggCountVarDistinct
import com.hp.hpl.jena.sparql.core.Var
import TaxonEQAnnotation.ps_entity_term
import TaxonEQAnnotation.ps_quality_term
import TaxonEQAnnotation.ps_related_entity_term

object CharacterDescription {

  def search(text: String, limit: Int): Future[Seq[CharacterDescription]] = {
    App.executeSPARQLQuery(buildSearchQuery(text, limit), CharacterDescription(_))
  }

  def query(entity: OWLClassExpression = owlThing, taxon: OWLClassExpression = owlThing, publications: Iterable[IRI] = Nil, limit: Int = 20, offset: Int = 0): Future[Seq[CharacterDescription]] = for {
    query <- App.expandWithOwlet(buildQuery(entity, taxon, publications, limit, offset))
    descriptions <- App.executeSPARQLQuery(query, CharacterDescription(_))
  } yield {
    descriptions
  }

  def queryTotal(entity: OWLClassExpression = owlThing, taxon: OWLClassExpression = owlThing, publications: Iterable[IRI] = Nil): Future[ResultCount] = for {
    query <- App.expandWithOwlet(buildTotalQuery(entity, taxon, publications))
    result <- App.executeSPARQLQuery(query)
  } yield {
    ResultCount(result)
  }

  def queryVariationProfile(taxa: Seq[IRI], limit: Int = 20, offset: Int = 0): Future[Seq[CharacterDescriptionAnnotation]] =
    App.executeSPARQLQuery(buildVariationProfileQuery(taxa, limit, offset), CharacterDescriptionAnnotation(_))

  def queryVariationProfileTotal(taxa: Seq[IRI]): Future[Int] =
    App.executeSPARQLQuery(buildVariationProfileTotalQuery(taxa)).map(ResultCount.count)

  def withIRI(iri: IRI): Future[Option[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, 'phenotype) where (
      bgp(
        t('state, describes_phenotype, 'phenotype),
        t('state, dcDescription, 'description),
        t('matrix, has_character / may_have_state_value, 'state),
        t('matrix, rdfsLabel, 'matrix_label)))
    val result = App.executeSPARQLQuery(query, result => {
      Term.computedLabel(IRI.create(result.getResource("phenotype").getURI)).map { phenotype =>
        AnnotatedCharacterDescription(
          CharacterDescription(
            IRI.create(result.getResource("state").getURI),
            result.getLiteral("description").getLexicalForm,<commit_msg>Fixed bug in annotated description service. It was not restricting query to input phenotype URI.<commit_after>package org.phenoscape.kb

import scala.concurrent.Future
import org.phenoscape.owl.Vocab._
import org.phenoscape.kb.KBVocab.rdfsSubClassOf
import org.phenoscape.owlet.SPARQLComposer._
import org.phenoscape.scowl.OWL._
import org.semanticweb.owlapi.model.IRI
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary.RDFS_LABEL
import com.hp.hpl.jena.graph.NodeFactory
import com.hp.hpl.jena.query.Query
import com.hp.hpl.jena.query.QuerySolution
import com.hp.hpl.jena.sparql.expr.E_IsIRI
import com.hp.hpl.jena.sparql.expr.ExprVar
import com.hp.hpl.jena.sparql.syntax.ElementFilter
import com.hp.hpl.jena.vocabulary.RDFS
import com.hp.hpl.jena.vocabulary.OWL2
import spray.http._
import spray.httpx._
import spray.httpx.SprayJsonSupport._
import spray.httpx.marshalling._
import spray.json.DefaultJsonProtocol._
import spray.json._
import com.hp.hpl.jena.sparql.expr.ExprList
import com.hp.hpl.jena.sparql.expr.E_OneOf
import scala.collection.JavaConversions._
import com.hp.hpl.jena.sparql.expr.nodevalue.NodeValueNode
import org.phenoscape.kb.KBVocab._
import org.phenoscape.kb.KBVocab.rdfsLabel
import org.phenoscape.kb.Main.system.dispatcher
import org.semanticweb.owlapi.model.OWLClassExpression
import org.phenoscape.owlet.OwletManchesterSyntaxDataType.SerializableClassExpression
import com.hp.hpl.jena.graph.Node
import org.semanticweb.owlapi.apibinding.OWLManager
import com.hp.hpl.jena.sparql.expr.aggregate.AggCountVarDistinct
import com.hp.hpl.jena.sparql.core.Var
import TaxonEQAnnotation.ps_entity_term
import TaxonEQAnnotation.ps_quality_term
import TaxonEQAnnotation.ps_related_entity_term

object CharacterDescription {

  def search(text: String, limit: Int): Future[Seq[CharacterDescription]] = {
    App.executeSPARQLQuery(buildSearchQuery(text, limit), CharacterDescription(_))
  }

  def query(entity: OWLClassExpression = owlThing, taxon: OWLClassExpression = owlThing, publications: Iterable[IRI] = Nil, limit: Int = 20, offset: Int = 0): Future[Seq[CharacterDescription]] = for {
    query <- App.expandWithOwlet(buildQuery(entity, taxon, publications, limit, offset))
    descriptions <- App.executeSPARQLQuery(query, CharacterDescription(_))
  } yield {
    descriptions
  }

  def queryTotal(entity: OWLClassExpression = owlThing, taxon: OWLClassExpression = owlThing, publications: Iterable[IRI] = Nil): Future[ResultCount] = for {
    query <- App.expandWithOwlet(buildTotalQuery(entity, taxon, publications))
    result <- App.executeSPARQLQuery(query)
  } yield {
    ResultCount(result)
  }

  def queryVariationProfile(taxa: Seq[IRI], limit: Int = 20, offset: Int = 0): Future[Seq[CharacterDescriptionAnnotation]] =
    App.executeSPARQLQuery(buildVariationProfileQuery(taxa, limit, offset), CharacterDescriptionAnnotation(_))

  def queryVariationProfileTotal(taxa: Seq[IRI]): Future[Int] =
    App.executeSPARQLQuery(buildVariationProfileTotalQuery(taxa)).map(ResultCount.count)

  def withIRI(iri: IRI): Future[Option[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 result = App.executeSPARQLQuery(query, result => {
      println("Result: " + result)
      Term.computedLabel(iri).map { phenotype =>
        println("Phenotype: " + phenotype)
        AnnotatedCharacterDescription(
          CharacterDescription(
            IRI.create(result.getResource("state").getURI),
            result.getLiteral("description").getLexicalForm,
{'fraction_non_alphanumeric': 0.10844444444444444, 'fraction_numerical': 0.0017777777777777779, 'mean_word_length': 5.514474772539288, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '675893', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2546, 'n_words': 410}