<commit_before>package org.phenoscape.kb.queries

import org.phenoscape.kb.AnatomicalEntity
import org.phenoscape.kb.KBVocab.{rdfsSubClassOf, _}
import org.phenoscape.kb.Main.system.dispatcher
import org.phenoscape.kb.util.BlazegraphNamedSubquery
import org.phenoscape.kb.util.SPARQLInterpolatorOWLAPI._
import org.phenoscape.sparql.SPARQLInterpolationOWL._
import org.phenoscape.owl.NamedRestrictionGenerator
import org.phenoscape.owl.Vocab._
import org.phenoscape.scowl._
import org.phenoscape.sparql.SPARQLInterpolation.{QueryText, _}
import org.semanticweb.owlapi.model.IRI
import scalaz.Scalaz._
import scalaz._

import scala.concurrent.Future
import scala.language.postfixOps

object GeneAffectingPhenotype {

  def buildQuery(entity: Option[IRI],
                 quality: Option[IRI],
                 includeParts: Boolean,
                 includeHistoricalHomologs: Boolean,
                 includeSerialHomologs: Boolean,
                 countOnly: Boolean,
                 limit: Int,
                 offset: Int): Future[String] =
    for {
      (whereClause, subqueries) <-
        constructWhereClause(entity, quality, includeParts, includeHistoricalHomologs, includeSerialHomologs)
    } yield {
      val unifiedQueries = BlazegraphNamedSubquery.unifyQueries(subqueries)
      val namedQueriesBlock =
        if (unifiedQueries.nonEmpty) unifiedQueries.map(_.namedQuery).reduce(_ + _) else sparql""
      val paging = if (limit > 0) sparql"LIMIT $limit OFFSET $offset" else sparql""
      val query =
        if (countOnly)
          sparql"""
      SELECT (COUNT(*) AS ?count)
      FROM $KBMainGraph
      FROM $KBClosureGraph
      $namedQueriesBlock
      WHERE {
        SELECT DISTINCT ?gene ?gene_label ?taxon ?taxon_label
        $whereClause
      }
      """
        else
          sparql"""
      SELECT DISTINCT ?gene ?gene_label ?taxon ?taxon_label
      FROM $KBMainGraph
      FROM $KBClosureGraph
      $namedQueriesBlock
      $whereClause
      ORDER BY LCASE(?gene_label) ?gene
      $paging
      """
      BlazegraphNamedSubquery.updateReferencesFor(unifiedQueries, query.text)
    }

  private def constructWhereClause(
    entity: Option[IRI],
    quality: Option[IRI],
    includeParts: Boolean,
    includeHistoricalHomologs: Boolean,
    includeSerialHomologs: Boolean): Future[(QueryText, Set[BlazegraphNamedSubquery])] = {
    val validHomologyRelation =
      (if (includeHistoricalHomologs) Set(homologous_to.getIRI) else Set.empty) ++ (if (includeSerialHomologs)<commit_msg>Include relation graph in query<commit_after>package org.phenoscape.kb.queries

import org.phenoscape.kb.AnatomicalEntity
import org.phenoscape.kb.KBVocab.{rdfsSubClassOf, _}
import org.phenoscape.kb.Main.system.dispatcher
import org.phenoscape.kb.util.BlazegraphNamedSubquery
import org.phenoscape.kb.util.SPARQLInterpolatorOWLAPI._
import org.phenoscape.sparql.SPARQLInterpolationOWL._
import org.phenoscape.owl.NamedRestrictionGenerator
import org.phenoscape.owl.Vocab._
import org.phenoscape.scowl._
import org.phenoscape.sparql.SPARQLInterpolation.{QueryText, _}
import org.semanticweb.owlapi.model.IRI
import scalaz.Scalaz._
import scalaz._

import scala.concurrent.Future
import scala.language.postfixOps

object GeneAffectingPhenotype {

  def buildQuery(entity: Option[IRI],
                 quality: Option[IRI],
                 includeParts: Boolean,
                 includeHistoricalHomologs: Boolean,
                 includeSerialHomologs: Boolean,
                 countOnly: Boolean,
                 limit: Int,
                 offset: Int): Future[String] =
    for {
      (whereClause, subqueries) <-
        constructWhereClause(entity, quality, includeParts, includeHistoricalHomologs, includeSerialHomologs)
    } yield {
      val unifiedQueries = BlazegraphNamedSubquery.unifyQueries(subqueries)
      val namedQueriesBlock =
        if (unifiedQueries.nonEmpty) unifiedQueries.map(_.namedQuery).reduce(_ + _) else sparql""
      val paging = if (limit > 0) sparql"LIMIT $limit OFFSET $offset" else sparql""
      val query =
        if (countOnly)
          sparql"""
      SELECT (COUNT(*) AS ?count)
      FROM $KBMainGraph
      FROM $KBClosureGraph
      FROM $KBRedundantRelationGraph
      $namedQueriesBlock
      WHERE {
        SELECT DISTINCT ?gene ?gene_label ?taxon ?taxon_label
        $whereClause
      }
      """
        else
          sparql"""
      SELECT DISTINCT ?gene ?gene_label ?taxon ?taxon_label
      FROM $KBMainGraph
      FROM $KBClosureGraph
      FROM $KBRedundantRelationGraph
      $namedQueriesBlock
      $whereClause
      ORDER BY LCASE(?gene_label) ?gene
      $paging
      """
      BlazegraphNamedSubquery.updateReferencesFor(unifiedQueries, query.text)
    }

  private def constructWhereClause(
    entity: Option[IRI],
    quality: Option[IRI],
    includeParts: Boolean,
    includeHistoricalHomologs: Boolean,
    includeSerialHomologs: Boolean): Future[(QueryText, Set[BlazegraphNamedSubquery])] = {
    val validHomologyRelation =
      (if (includeHistoricalHomologs) Set(homologous_to.getIRI) else Set.empty) ++ (if (includeSerialHomologs)
{'fraction_non_alphanumeric': 0.08547008547008547, 'fraction_numerical': 0.0003885003885003885, 'mean_word_length': 3.223954060705496, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '534117', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1601, 'n_words': 305}