<commit_before>package org.phenoscape.kb

import java.net.URI
import java.text.SimpleDateFormat
import java.util.Calendar

import org.apache.jena.query.{Query, QuerySolution}
import org.apache.jena.rdf.model.{Property, ResourceFactory}
import org.apache.jena.sparql.core.Var
import org.apache.jena.sparql.expr.aggregate.{AggCountDistinct, AggCountVarDistinct}
import org.apache.jena.sparql.expr.nodevalue.NodeValueNode
import org.apache.jena.sparql.expr.{E_OneOf, Expr, ExprList, ExprVar}
import org.apache.jena.sparql.syntax.{ElementFilter, ElementSubQuery}
import org.apache.log4j.Logger
import org.obo.datamodel.impl.OBOClassImpl
import org.phenoscape.io.NeXMLUtil
import org.phenoscape.kb.KBVocab.{rdfsLabel, rdfsSubClassOf}
import org.phenoscape.kb.Main.system.dispatcher
import org.phenoscape.model.MultipleState.MODE
import org.phenoscape.model.{Character, DataSet, MultipleState, State, Taxon => MatrixTaxon}
import org.phenoscape.owl.NamedRestrictionGenerator
import org.phenoscape.owl.Vocab._
import org.phenoscape.owlet.OwletManchesterSyntaxDataType.SerializableClassExpression
import org.phenoscape.owlet.SPARQLComposer._
import org.phenoscape.scowl._
import org.semanticweb.owlapi.model.{IRI, OWLClassExpression, OWLEntity}

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.concurrent.Future
import scala.language.{implicitConversions, postfixOps}

object PresenceAbsenceOfStructure {

  val implies_presence_of_some = NamedRestrictionGenerator.getClassRelationIRI(IMPLIES_PRESENCE_OF.getIRI)

  implicit def owlEntityToJenaProperty(prop: OWLEntity): Property = ResourceFactory.createProperty(prop.getIRI.toString)

  def statesEntailingAbsence(taxon: IRI, entity: IRI, limit: Int = 20, offset: Int = 0): Future[Seq[AnnotatedCharacterDescription]] =
    App.executeSPARQLQuery(buildAbsenceStatesQuery(taxon, entity, limit, offset), AnnotatedCharacterDescription.fromQuerySolution).flatMap(Future.sequence(_))

  def statesEntailingAbsenceTotal(taxon: IRI, entity: IRI): Future[Int] =
    App.executeSPARQLQuery(buildAbsenceStatesQueryTotal(taxon, entity)).map(ResultCount.count)

  def statesEntailingPresence(taxon: IRI, entity: IRI, limit: Int = 20, offset: Int = 0): Future[Seq[AnnotatedCharacterDescription]] =
    App.executeSPARQLQuery(buildPresenceStatesQuery(taxon, entity, limit, offset), AnnotatedCharacterDescription.fromQuerySolution).flatMap(Future.sequence(_))

  def statesEntailingPresenceTotal(taxon: IRI, entity: IRI): Future[Int] =
    App.executeSPARQLQuery(buildPresenceStatesQueryTotal(taxon, entity)).map(ResultCount.count)

  def taxaExhibitingPresence(entity: IRI, taxonFilter: Option[IRI], limit: Int = 20, offset: Int = 0): Future[Seq[Taxon]] =
    App.executeSPARQLQuery(buildExhibitingPresenceQuery(entity, taxonFilter, limit, offset), resultToTaxon)

  def taxaExhibitingPresenceTotal(entity: IRI, taxonFilter: Option[IRI]): Future[Int] =
    App.executeSPARQLQuery(buildExhibitingPresenceTotalQuery(entity, taxonFilter)).map(ResultCount.count)

  def taxaExhibitingAbsence(entity: IRI, taxonFilter: Option[IRI], limit: Int = 20, offset: Int = 0): Future[Seq[Taxon]] =
    App.executeSPARQLQuery(buildExhibitingAbsenceQuery(entity, taxonFilter, limit, offset), resultToTaxon)

  def taxaExhibitingAbsenceTotal(entity: IRI, taxonFilter: Option[IRI]): Future[Int] =
    App.executeSPARQLQuery(buildExhibitingAbsenceTotalQuery(entity, taxonFilter)).map(ResultCount.count)

  def presenceAbsenceMatrix(mainEntityClass: OWLClassExpression, taxonClass: OWLClassExpression, variableOnly: Boolean, includeParts: Boolean): Future[DataSet] = {
    val entityClass = if (includeParts) mainEntityClass or (part_of some mainEntityClass) else mainEntityClass
    for {
      query <- App.expandWithOwlet(buildMatrixQuery(entityClass, taxonClass))
      model <- App.executeSPARQLConstructQuery(query)
    } yield {
      val dataset = new DataSet()
      KB.buildDate.map {
        date => dataset.setCurators("The author of this ontotrace matrix is Phenoscape KB built on " + date.toString)
      }
      val characters: mutable.Map[String, Character] = mutable.Map()
      val states: mutable.Map[String, State] = mutable.Map()
      val taxa: mutable.Map[String, MatrixTaxon] = mutable.Map()
      val presencesAndAbsences = {
        val allPresences = model.listStatements(null, has_presence_of, null).asScala.toSet<commit_msg>Add build date future to for comprehension.<commit_after>package org.phenoscape.kb

import java.net.URI
import java.text.SimpleDateFormat
import java.util.Calendar

import org.apache.jena.query.{Query, QuerySolution}
import org.apache.jena.rdf.model.{Property, ResourceFactory}
import org.apache.jena.sparql.core.Var
import org.apache.jena.sparql.expr.aggregate.{AggCountDistinct, AggCountVarDistinct}
import org.apache.jena.sparql.expr.nodevalue.NodeValueNode
import org.apache.jena.sparql.expr.{E_OneOf, Expr, ExprList, ExprVar}
import org.apache.jena.sparql.syntax.{ElementFilter, ElementSubQuery}
import org.apache.log4j.Logger
import org.obo.datamodel.impl.OBOClassImpl
import org.phenoscape.io.NeXMLUtil
import org.phenoscape.kb.KBVocab.{rdfsLabel, rdfsSubClassOf}
import org.phenoscape.kb.Main.system.dispatcher
import org.phenoscape.model.MultipleState.MODE
import org.phenoscape.model.{Character, DataSet, MultipleState, State, Taxon => MatrixTaxon}
import org.phenoscape.owl.NamedRestrictionGenerator
import org.phenoscape.owl.Vocab._
import org.phenoscape.owlet.OwletManchesterSyntaxDataType.SerializableClassExpression
import org.phenoscape.owlet.SPARQLComposer._
import org.phenoscape.scowl._
import org.semanticweb.owlapi.model.{IRI, OWLClassExpression, OWLEntity}

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.concurrent.Future
import scala.language.{implicitConversions, postfixOps}

object PresenceAbsenceOfStructure {

  val implies_presence_of_some = NamedRestrictionGenerator.getClassRelationIRI(IMPLIES_PRESENCE_OF.getIRI)

  implicit def owlEntityToJenaProperty(prop: OWLEntity): Property = ResourceFactory.createProperty(prop.getIRI.toString)

  def statesEntailingAbsence(taxon: IRI, entity: IRI, limit: Int = 20, offset: Int = 0): Future[Seq[AnnotatedCharacterDescription]] =
    App.executeSPARQLQuery(buildAbsenceStatesQuery(taxon, entity, limit, offset), AnnotatedCharacterDescription.fromQuerySolution).flatMap(Future.sequence(_))

  def statesEntailingAbsenceTotal(taxon: IRI, entity: IRI): Future[Int] =
    App.executeSPARQLQuery(buildAbsenceStatesQueryTotal(taxon, entity)).map(ResultCount.count)

  def statesEntailingPresence(taxon: IRI, entity: IRI, limit: Int = 20, offset: Int = 0): Future[Seq[AnnotatedCharacterDescription]] =
    App.executeSPARQLQuery(buildPresenceStatesQuery(taxon, entity, limit, offset), AnnotatedCharacterDescription.fromQuerySolution).flatMap(Future.sequence(_))

  def statesEntailingPresenceTotal(taxon: IRI, entity: IRI): Future[Int] =
    App.executeSPARQLQuery(buildPresenceStatesQueryTotal(taxon, entity)).map(ResultCount.count)

  def taxaExhibitingPresence(entity: IRI, taxonFilter: Option[IRI], limit: Int = 20, offset: Int = 0): Future[Seq[Taxon]] =
    App.executeSPARQLQuery(buildExhibitingPresenceQuery(entity, taxonFilter, limit, offset), resultToTaxon)

  def taxaExhibitingPresenceTotal(entity: IRI, taxonFilter: Option[IRI]): Future[Int] =
    App.executeSPARQLQuery(buildExhibitingPresenceTotalQuery(entity, taxonFilter)).map(ResultCount.count)

  def taxaExhibitingAbsence(entity: IRI, taxonFilter: Option[IRI], limit: Int = 20, offset: Int = 0): Future[Seq[Taxon]] =
    App.executeSPARQLQuery(buildExhibitingAbsenceQuery(entity, taxonFilter, limit, offset), resultToTaxon)

  def taxaExhibitingAbsenceTotal(entity: IRI, taxonFilter: Option[IRI]): Future[Int] =
    App.executeSPARQLQuery(buildExhibitingAbsenceTotalQuery(entity, taxonFilter)).map(ResultCount.count)

  def presenceAbsenceMatrix(mainEntityClass: OWLClassExpression, taxonClass: OWLClassExpression, variableOnly: Boolean, includeParts: Boolean): Future[DataSet] = {
    val entityClass = if (includeParts) mainEntityClass or (part_of some mainEntityClass) else mainEntityClass
    val buildDateFut = KB.buildDate
    for {
      query <- App.expandWithOwlet(buildMatrixQuery(entityClass, taxonClass))
      model <- App.executeSPARQLConstructQuery(query)
      buildDate <- buildDateFut
    } yield {
      val dataset = new DataSet()
      dataset.setCurators("Ontotrace matrix generated from Phenoscape KB built on " + buildDate.toString)
      val characters: mutable.Map[String, Character] = mutable.Map()
      val states: mutable.Map[String, State] = mutable.Map()
      val taxa: mutable.Map[String, MatrixTaxon] = mutable.Map()
      val presencesAndAbsences = {
        val allPresences = model.listStatements(null, has_presence_of, null).asScala.toSet
{'fraction_non_alphanumeric': 0.10046332918973895, 'fraction_numerical': 0.002938185105661657, 'mean_word_length': 6.531914893617022, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, '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': '5662051', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2795, 'n_words': 507}