Skip to content

Commit

Permalink
Merge pull request #581 from nfdi4plants/Debug_Excel_FixFindAllChildT…
Browse files Browse the repository at this point in the history
…erms

implement working updated version of findAllChildTerms
  • Loading branch information
Freymaurer authored Nov 27, 2024
2 parents dfef06d + 6e37d7c commit 9a18456
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/Client/OfficeInterop/OfficeInterop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open GlobalBindings

open Shared
open Database
open DTO
open DTOs.TermQuery

open OfficeInterop
open OfficeInterop.ExcelUtil
Expand Down Expand Up @@ -1635,7 +1635,7 @@ let validateSelectedAndNeighbouringBuildingBlocks () =
/// <param name="names"></param>
let searchTermInDatabase name =
promise {
let term = TermQuery.create(name, searchMode=Database.FullTextSearch.Exact)
let term = TermQueryDto.create(name, searchMode=Database.FullTextSearch.Exact)
let! results = Async.StartAsPromise(Api.ontology.searchTerm term)
let result = Array.tryHead results
return result
Expand All @@ -1650,7 +1650,7 @@ let searchTermsInDatabase names =
let terms =
names
|> List.map (fun name ->
TermQuery.create(name, searchMode=Database.FullTextSearch.Exact)
TermQueryDto.create(name, searchMode=Database.FullTextSearch.Exact)
)
|> Array.ofSeq
let! result = Async.StartAsPromise(Api.ontology.searchTerms terms)
Expand Down
16 changes: 9 additions & 7 deletions src/Client/SharedComponents/TermSearchInput.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ open Browser.Types
open ARCtrl
open Shared
open Shared.Database
open DTO
open Shared.DTOs.TermQuery
open Shared.DTOs.ParentTermQuery
open Fable.Core.JsInterop

module TermSearchAux =
Expand All @@ -30,22 +31,23 @@ module TermSearchAux =

let searchByName(query: string, setResults: Term [] -> unit) =
async {
let query = TermQuery.create(query, 10)
let query = TermQueryDto.create(query, 10)
let! terms = Api.ontology.searchTerm query
setResults terms
}

let searchByParent(query: string, parentTAN: string, setResults: Term [] -> unit) =
async {
let query = TermQuery.create(query, 50, parentTAN)
let query = TermQueryDto.create(query, 50, parentTAN)
let! terms = Api.ontology.searchTerm query
setResults terms
}

let searchAllByParent(parentTAN: string, setResults: Term [] -> unit) =
let findAllChildTerms(parentTAN: string, setResults: Term [] -> unit) =
async {
let! terms = Api.api.getAllTermsByParentTerm <| Shared.SwateObsolete.TermMinimal.create "" parentTAN
setResults terms
let query = ParentTermQueryDto.create(parentTAN, 50)
let! terms = Api.ontology.findAllChildTerms query
setResults terms.results
}

let allByParentSearch (
Expand All @@ -61,7 +63,7 @@ module TermSearchAux =
async {
ClickOutsideHandler.AddListener(SelectAreaID, fun e -> stopSearch())
}
searchAllByParent(parent.TermAccessionShort, fun terms -> setSearchTreeState {Results = terms; SearchIs = SearchIs.Done})
findAllChildTerms(parent.TermAccessionShort, fun terms -> setSearchTreeState {Results = terms; SearchIs = SearchIs.Done})
]
|> Async.Parallel
|> Async.Ignore
Expand Down
31 changes: 23 additions & 8 deletions src/Server/Api/IOntologyAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module API.IOntologyAPI
open Shared
open Database

open DTO
open DTOs.TermQuery
open DTOs.ParentTermQuery
open Fable.Remoting.Server
open Fable.Remoting.Giraffe
open ARCtrl
Expand All @@ -26,7 +27,7 @@ module Helper =
module V3 =
open ARCtrl.Helper.Regex.ActivePatterns

let searchSingleTerm credentials (content: TermQuery) =
let searchSingleTerm credentials (content: TermQueryDto) =
async {
let dbSearchRes =
match content.query.Trim() with
Expand All @@ -46,12 +47,20 @@ module Helper =
return dbSearchRes
}

let searchChildTerms credentials (content: ParentTermQueryDto) =
async {
let dbSearchRes =
Term.Term(credentials).findAllChildTerms(content.parentTermId, ?limit=content.limit) |> Array.ofSeq

return dbSearchRes
}

open Helper

[<RequireQualifiedAccess>]
module V3 =

let ontologyApi (credentials : Helper.Neo4JCredentials) : IOntologyAPIv3 =
let ontologyApi (credentials: Helper.Neo4JCredentials) : IOntologyAPIv3 =
{
//Development
getTestNumber =
Expand All @@ -68,7 +77,7 @@ module V3 =
Helper.V3.searchSingleTerm credentials query
]
let! results = asyncQueries |> Async.Parallel
let zipped = Array.map2 (fun a b -> TermQueryResults.create(a,b)) queries results
let zipped = Array.map2 (fun a b -> TermQueryDtoResults.create(a,b)) queries results
return zipped
}
getTermById = fun id ->
Expand All @@ -80,6 +89,12 @@ module V3 =
| 0 -> None
| _ -> failwith $"Found multiple terms with the same accession: {id}" // must be multiples as negative cannot exist for length
}
findAllChildTerms = fun content ->
async {
let! results = Helper.V3.searchChildTerms credentials content
let zipped = ParentTermQueryDtoResults.create(content, results)
return zipped
}
}

let createIOntologyApi credentials =
Expand All @@ -97,7 +112,7 @@ open Shared.SwateObsolete.Regex
module V1 =

/// <summary>Deprecated</summary>
let ontologyApi (credentials : Helper.Neo4JCredentials) : IOntologyAPIv1 =
let ontologyApi (credentials: Helper.Neo4JCredentials) : IOntologyAPIv1 =
/// We use sorensen dice to avoid scoring mutliple occassions of the same word (Issue https://github.com/nfdi4plants/Swate/issues/247)
let sorensenDiceSortTerms (searchStr:string) (terms: Term []) =
terms |> SorensenDice.sortBySimilarity searchStr (fun term -> term.Name)
Expand Down Expand Up @@ -193,7 +208,7 @@ module V1 =
return filteredResult
}

getUnitTermSuggestions = fun (max:int,typedSoFar:string) ->
getUnitTermSuggestions = fun (max:int, typedSoFar:string) ->
async {
let dbSearchRes =
match typedSoFar with
Expand All @@ -207,7 +222,7 @@ module V1 =
return res
}

getTermsByNames = fun (queryArr) ->
getTermsByNames = fun queryArr ->
async {
// check if search string is empty. This case should delete TAN- and TSR- values in table
let filteredQueries = queryArr |> Array.filter (fun x -> x.Term.Name <> "" || x.Term.TermAccession <> "")
Expand All @@ -224,7 +239,7 @@ module V1 =
Term.TermQuery.getByName(searchTerm.Term.Name, searchType=FullTextSearch.Exact)
)
let result =
Helper.Neo4j.runQueries(queries,credentials)
Helper.Neo4j.runQueries(queries, credentials)
|> Array.map2 (fun termSearchable dbResults ->
// replicate if..elif..else conditions from 'queries'
if termSearchable.Term.TermAccession <> "" then
Expand Down
44 changes: 42 additions & 2 deletions src/Server/Database/Term.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Database.Term

open Neo4j.Driver
open Shared.DTO
open Shared.Database
open Shared.SwateObsolete
open Helper
Expand Down Expand Up @@ -103,7 +102,7 @@ type Term(?credentials:Neo4JCredentials, ?session:IAsyncSession) =
let limit = defaultArg limit 5
let searchNameQuery = Queries.NameQueryFullText ("node", limit=true)
let searchTreeQuery =
"""MATCH (node:Term)
"""MATCH (node:Term)
WHERE node.accession IN $AccessionList
MATCH (endNode:Term {accession: $Parent})
MATCH (node)
Expand Down Expand Up @@ -185,6 +184,47 @@ type Term(?credentials:Neo4JCredentials, ?session:IAsyncSession) =
printfn "%s" exn.Message
[||]

/// <summary>
/// Find all child terms of a given parent
/// </summary>
/// <param name="parentId"></param>
/// <param name="limit"></param>
member this.findAllChildTerms(parentId: string, ?limit: int) =
let limit = defaultArg limit 5

let query =
"""MATCH (child)-[:is_a*]->(:Term {accession: $Parent})
RETURN child.accession, child.name, child.definition, child.is_obsolete
LIMIT $Limit"""

let config = Action<TransactionConfigBuilder>(fun (config : TransactionConfigBuilder) -> config.WithTimeout(TimeSpan.FromSeconds(0.5)) |> ignore)
use session = if session.IsSome then session.Value else Neo4j.establishConnection(credentials.Value)
let main =
task {
let! tree_query =
let parameters = System.Collections.Generic.Dictionary<string,obj>([
KeyValuePair("Parent", box parentId);
KeyValuePair("Limit", box limit)
])
session.RunAsync(query, parameters, config)

let! tree_records = tree_query.ToListAsync()
let tree_results =
[|
for record in tree_records do
yield Term.asTerm("child") record
|]
return tree_results
}
try
main.Result
with
| exn ->
printfn "%s" exn.Message
[||]

//Neo4j.runQuery(query, param, (Term.asTerm("child")), ?session=session, ?credentials=credentials)

/// This function will allow for raw apache lucene input. It is possible to search either term name or description or both.
/// The function will error if both term name and term description are None.
member this.getByAdvancedTermSearch(advancedSearchOptions:Shared.AdvancedSearchTypes.AdvancedSearchOptions) =
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Database/TreeSearch.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Database.TreeSearch
open System
open Neo4j.Driver

open Shared.DTO
open Shared.DTOs
open Shared.Database
open Shared.Database.TreeTypes

Expand Down
21 changes: 21 additions & 0 deletions src/Shared/DTOs/ParentTermQueryDto.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Shared.DTOs.ParentTermQuery

open Shared

type ParentTermQueryDto = {
parentTermId: string
limit: int option
} with
static member create(parentTermId, ?limit) = {
parentTermId = parentTermId
limit = limit
}

type ParentTermQueryDtoResults = {
query: ParentTermQueryDto
results: Database.Term []
} with
static member create(query, results) = {
query = query
results = results
}
17 changes: 7 additions & 10 deletions src/Shared/DTO.fs → src/Shared/DTOs/TermQueryDto.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module Shared.DTO
module Shared.DTOs.TermQuery

open ARCtrl
open Shared

open Database

type TermQuery = {
type TermQueryDto = {
query: string
limit: int option
parentTermId: string option
Expand All @@ -19,12 +17,11 @@ type TermQuery = {
searchMode = searchMode
}

type TermQueryResults = {
query: TermQuery
results: Term []
type TermQueryDtoResults = {
query: TermQueryDto
results: Database.Term []
} with
static member create(query, results) = {
query = query
results = results
}

}
13 changes: 8 additions & 5 deletions src/Shared/Shared.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace Shared
open System
open Shared
open Database
open DTO
open DTOs.TermQuery
open DTOs.ParentTermQuery

[<AutoOpen>]
module Regex =
Expand Down Expand Up @@ -46,10 +47,12 @@ module SorensenDice =

type IOntologyAPIv3 = {
// Development
getTestNumber : unit -> Async<int>
searchTerm: TermQuery -> Async<Term []>
searchTerms: TermQuery[] -> Async<TermQueryResults[]>
getTermById: string -> Async<Term option>
getTestNumber : unit -> Async<int>
searchTerm : TermQueryDto -> Async<Term []>
searchTerms : TermQueryDto[] -> Async<TermQueryDtoResults[]>
getTermById : string -> Async<Term option>
findAllChildTerms : ParentTermQueryDto -> Async<ParentTermQueryDtoResults>

}

/// Development api
Expand Down
3 changes: 2 additions & 1 deletion src/Shared/Shared.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Database.fs" />
<Compile Include="DTOs\ParentTermQueryDto.fs" />
<Compile Include="DTOs\TermQueryDto.fs" />
<Compile Include="Extensions.fs" />
<Compile Include="StaticTermCollection.fs" />
<Compile Include="URLs.fs" />
<Compile Include="DTO.fs" />
<Compile Include="ARCtrl.Helper.fs" />
<Compile Include="AdvancedSearchTypes.fs" />
<Compile Include="Shared.fs" />
Expand Down

0 comments on commit 9a18456

Please sign in to comment.