Skip to content

Commit

Permalink
adding deferredValues and fetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhayhurst committed Oct 24, 2024
1 parent 7ea43a7 commit 71789db
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 40 deletions.
31 changes: 11 additions & 20 deletions app/models/Backend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,21 @@ class Backend @Inject() (implicit
esRetriever.getByIds(targetIndexName, ids, fromJsValue[GeneOntologyTerm])
}

def getL2GPredictions(id: String,
pagination: Option[Pagination]
): Future[IndexedSeq[L2GPredictions]] = {
def getL2GPredictions(ids: Seq[String]): Future[IndexedSeq[L2GPredictions]] = {
val indexName = getIndexOrDefault("l2g_predictions")
val pag = pagination.getOrElse(Pagination.mkDefault)

esRetriever
.getByIndexedTermsMust(indexName,
Map("studyLocusId.keyword" -> Seq(id)),
pag,
fromJsValue[L2GPredictions],
sortByField = ElasticRetriever.sortBy("score", SortOrder.Desc)
.getByIndexedTermsMust(
indexName,
Map("studyLocusId.keyword" -> ids),
Pagination(Pagination.indexDefault, Pagination.sizeMax),
fromJsValue[L2GPredictions],
sortByField = ElasticRetriever.sortBy("score", SortOrder.Desc)
)
.map(_._1)
}

def getVariants(ids: Seq[String]): Future[IndexedSeq[VariantIndex]] = {
val indexName = getIndexOrDefault("variant_index")

//esRetriever.getByIds(indexName, ids, fromJsValue[VariantIndex])
esRetriever
.getByIndexedTermsMust(indexName,
Map("variantId.keyword" -> ids),
Expand All @@ -166,20 +161,16 @@ class Backend @Inject() (implicit
.map(_._1)
}

def getBiosample(id: String): Future[Option[Biosample]] = {
def getBiosamples(ids: Seq[String]): Future[IndexedSeq[Biosample]] = {
val indexName = getIndexOrDefault("biosample", Some("biosample"))
esRetriever
.getByIndexedTermsMust(
indexName,
Map("biosampleId.keyword" -> Seq(id)),
Map("biosampleId.keyword" -> ids),
Pagination.mkDefault,
fromJsValue[Biosample]
)
.map {
case (Seq(), _) => None
case (hits, aggs) =>
Some(hits.head)
}
.map(_._1)
}

def getStudies(queryArgs: StudyQueryArgs,
Expand Down Expand Up @@ -243,7 +234,7 @@ class Backend @Inject() (implicit
queryArgs: CredibleSetQueryArgs,
pagination: Option[Pagination]
): Future[IndexedSeq[JsValue]] = {
val pag = pagination.getOrElse(Pagination.mkDefault)
val pag = pagination.getOrElse(Pagination(Pagination.indexDefault, Pagination.sizeMax))
val indexName = getIndexOrDefault("credible_set")
val termsQuery = Map(
"studyLocusId.keyword" -> queryArgs.ids,
Expand Down
18 changes: 9 additions & 9 deletions app/models/entities/CredibleSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import models.Backend
import models.gql.StudyTypeEnum
import models.gql.Arguments.StudyType
import models.entities.GwasIndex.{gwasImp, gwasWithoutCredSetsImp}
import models.gql.Fetchers.{gwasFetcher, targetsFetcher, variantFetcher}
import models.gql.Fetchers.{gwasFetcher, l2gFetcher, targetsFetcher, variantFetcher}
import models.gql.Objects.{logger, targetImp, variantIndexImp, colocalisationImp, l2gPredictionsImp}
import play.api.Logging
import play.api.libs.json.{JsValue, Json, OFormat, OWrites}
Expand All @@ -16,7 +16,8 @@ import sangria.schema.{
ObjectType,
OptionType,
StringType,
fields
fields,
DeferredValue
}
import models.gql.Arguments.{studyTypes, pageArg}

Expand Down Expand Up @@ -89,7 +90,7 @@ object CredibleSet extends Logging {
"target",
OptionType(targetImp),
Some("Target"),
resolve = r => targetsFetcher.deferOpt(r.value.geneId)
resolve = r => DeferredValue(targetsFetcher.deferOpt(r.value.geneId))
)
)
)
Expand All @@ -105,7 +106,7 @@ object CredibleSet extends Logging {
resolve = r => {
val variantId = (r.value.variantId)
logger.debug(s"Finding variant index: $variantId")
variantFetcher.deferOpt(variantId)
DeferredValue(variantFetcher.deferOpt(variantId))
}
)
)
Expand All @@ -129,17 +130,16 @@ object CredibleSet extends Logging {
resolve = js => {
val id = (js.value \ "variantId").asOpt[String]
logger.debug(s"Finding variant for id: $id")
variantFetcher.deferOpt(id)
DeferredValue(variantFetcher.deferOpt(id))
}
),
Field(
"l2Gpredictions",
OptionType(ListType(l2gPredictionsImp)),
OptionType(l2gPredictionsImp),
description = None,
arguments = pageArg :: Nil,
resolve = js => {
val id = (js.value \ "studyLocusId").as[String]
js.ctx.getL2GPredictions(id, js.arg(pageArg))
DeferredValue(l2gFetcher.deferOpt(id))
}
),
Field(
Expand Down Expand Up @@ -310,7 +310,7 @@ object CredibleSet extends Logging {
resolve = js => {
val studyId = (js.value \ "studyId").asOpt[String]
logger.debug(s"Finding gwas study: $studyId")
gwasFetcher.deferOpt(studyId)
DeferredValue(gwasFetcher.deferOpt(studyId))
}
)
val credibleSetImp: ObjectType[Backend, JsValue] = ObjectType(
Expand Down
16 changes: 9 additions & 7 deletions app/models/entities/GwasIndex.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package models.entities

import models.Backend
import models.gql.Fetchers.{diseasesFetcher, targetsFetcher}
import models.gql.Fetchers.{biosamplesFetcher, diseasesFetcher, targetsFetcher}
import play.api.Logging
import play.api.libs.json.{JsValue, Json, OFormat}
import models.entities.CredibleSet.credibleSetImp
Expand All @@ -14,7 +14,8 @@ import sangria.schema.{
ObjectType,
OptionType,
StringType,
fields
fields,
DeferredValue
}
import models.entities.CredibleSet.credibleSetWithoutStudyImp
import models.gql.StudyTypeEnum
Expand Down Expand Up @@ -44,6 +45,7 @@ object GwasIndex extends Logging {
deriveObjectType[Backend, LdPopulationStructure]()
implicit val sampleImp: ObjectType[Backend, Sample] = deriveObjectType[Backend, Sample]()
implicit val sumStatQCImp: ObjectType[Backend, SumStatQC] = deriveObjectType[Backend, SumStatQC]()
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
val gwasFields: Seq[Field[Backend, JsValue]] = Seq(
Field(
"studyId",
Expand Down Expand Up @@ -76,19 +78,19 @@ object GwasIndex extends Logging {
resolve = js => {
val geneId = (js.value \ "geneId").asOpt[String]
logger.debug(s"Finding target: $geneId")
targetsFetcher.deferOpt(geneId)
DeferredValue(targetsFetcher.deferOpt(geneId))
}
),
Field(
"biosample",
OptionType(biosampleImp),
Some("Biosample"),
Some("biosample"),
resolve = js => {
val biosampleId = (js.value \ "biosampleFromSourceId").asOpt[String].getOrElse("")
if (biosampleId.isEmpty) {
None
} else {
js.ctx.getBiosample(biosampleId)
js.ctx.getBiosamples(Seq(biosampleId)).map(_.headOption)
}
}
),
Expand Down Expand Up @@ -129,7 +131,7 @@ object GwasIndex extends Logging {
resolve = js => {
val ids = (js.value \ "traitFromSourceMappedIds").asOpt[Seq[String]].getOrElse(Seq.empty)
logger.debug(s"Finding diseases for ids: $ids")
diseasesFetcher.deferSeqOpt(ids)
DeferredValue(diseasesFetcher.deferSeqOpt(ids))
}
),
Field(
Expand Down Expand Up @@ -159,7 +161,7 @@ object GwasIndex extends Logging {
.asOpt[Seq[String]]
.getOrElse(Seq.empty)
logger.debug(s"Finding diseases for ids: $ids")
diseasesFetcher.deferSeqOpt(ids)
DeferredValue(diseasesFetcher.deferSeqOpt(ids))
}
),
Field(
Expand Down
44 changes: 44 additions & 0 deletions app/models/gql/Fetchers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package models.gql

import models.Helpers.fromJsValue
import models.entities.{
Biosample,
CredibleSet,
Disease,
Drug,
Expressions,
GeneOntologyTerm,
GwasIndex,
HPO,
Indications,
L2GPredictions,
OtarProjects,
Reactome,
Target,
Expand Down Expand Up @@ -77,6 +80,16 @@ object Fetchers extends Logging {
}
)

implicit val biosampleHasId: HasId[Biosample, String] = HasId[Biosample, String](_.biosampleId)
val biosamplesFetcherCache = FetcherCache.simple
val biosamplesFetcher: Fetcher[Backend, Biosample, Biosample, String] = Fetcher(
config =
FetcherConfig.maxBatchSize(entities.Configuration.batchSize).caching(biosamplesFetcherCache),
fetch = (ctx: Backend, ids: Seq[String]) => {
ctx.getBiosamples(ids)
}
)

//hpo fetcher
implicit val hpoHasId: HasId[HPO, String] = HasId[HPO, String](_.id)

Expand Down Expand Up @@ -128,6 +141,30 @@ object Fetchers extends Logging {
}
)

val credibleSetFetcherCache = FetcherCache.simple
val credibleSetFetcher: Fetcher[Backend, JsValue, JsValue, String] = {
implicit val credibleSetFetcherId: HasId[JsValue, String] =
HasId[JsValue, String](js => (js \ "studyLocusId").as[String])
Fetcher(
config = FetcherConfig
.maxBatchSize(entities.Configuration.batchSize)
.caching(credibleSetFetcherCache),
fetch = (ctx: Backend, ids: Seq[String]) => {
ctx.getCredibleSets(entities.CredibleSetQueryArgs(ids = ids), None)
}
)
}

implicit val l2gFetcherId: HasId[L2GPredictions, String] =
HasId[L2GPredictions, String](_.studyLocusId)
val l2gFetcherCache = FetcherCache.simple
val l2gFetcher: Fetcher[Backend, L2GPredictions, L2GPredictions, String] = Fetcher(
config = FetcherConfig.maxBatchSize(entities.Configuration.batchSize).caching(l2gFetcherCache),
fetch = (ctx: Backend, ids: Seq[String]) => {
ctx.getL2GPredictions(ids)
}
)

val gwasFetcherCache = FetcherCache.simple
val gwasFetcher: Fetcher[Backend, JsValue, JsValue, String] = {
implicit val gwasFetcherId: HasId[JsValue, String] =
Expand Down Expand Up @@ -160,6 +197,13 @@ object Fetchers extends Logging {
def resetCache(): Unit = {
logger.info("Clearing all GraphQL caches.")
val fetchers: List[SimpleFetcherCache] = List(
biosamplesFetcherCache,
credibleSetFetcherCache,
gwasFetcherCache,
hpoFetcherCache,
goFetcherCache,
variantFetcherCache,
l2gFetcherCache,
targetsFetcherCache,
drugsFetcherCache,
diseasesFetcherCache,
Expand Down
8 changes: 4 additions & 4 deletions app/models/gql/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ object Objects extends Logging {
"target",
OptionType(targetImp),
Some("Target"),
resolve = r => targetsFetcher.deferOpt(r.value.targetId)
resolve = r => DeferredValue(targetsFetcher.deferOpt(r.value.targetId))
)
)
)
Expand All @@ -1301,7 +1301,7 @@ object Objects extends Logging {
Field("target",
OptionType(targetImp),
Some("Target"),
resolve = r => targetsFetcher.deferOpt(r.value.targetId)
resolve = r => DeferredValue(targetsFetcher.deferOpt(r.value.targetId))
)
),
ReplaceField(
Expand Down Expand Up @@ -1333,7 +1333,7 @@ object Objects extends Logging {
"target",
OptionType(targetImp),
Some("Target"),
resolve = r => targetsFetcher.deferOpt(r.value.geneId)
resolve = r => DeferredValue(targetsFetcher.deferOpt(r.value.geneId))
)
)
)
Expand Down Expand Up @@ -1369,7 +1369,7 @@ object Objects extends Logging {
val soId = (r.value.mostSevereConsequenceId)
.replace("_", ":")
logger.debug(s"Finding variant functional consequence: $soId")
soTermsFetcher.deferOpt(soId)
DeferredValue(soTermsFetcher.deferOpt(soId))
}
)
),
Expand Down

0 comments on commit 71789db

Please sign in to comment.