Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement working updated version of findAllChildTerms #581

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Freymaurer marked this conversation as resolved.
Show resolved Hide resolved
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})
Freymaurer marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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 @@

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 Expand Up @@ -231,14 +234,14 @@
///
getTermSuggestions : (int*string) -> Async<Term []>
/// (nOfReturnedResults*queryString*parentOntology). If parentOntology = "" then isNull -> Error.
getTermSuggestionsByParentTerm : (int*string*SwateObsolete.TermMinimal) -> Async<Term []>

Check warning on line 237 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 237 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 237 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated

Check warning on line 237 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated
getAllTermsByParentTerm : SwateObsolete.TermMinimal -> Async<Term []>

Check warning on line 238 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 238 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 238 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated

Check warning on line 238 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated
/// (nOfReturnedResults*queryString*parentOntology). If parentOntology = "" then isNull -> Error.
getTermSuggestionsByChildTerm : (int*string*SwateObsolete.TermMinimal) -> Async<Term []>

Check warning on line 240 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 240 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 240 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated

Check warning on line 240 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated
getAllTermsByChildTerm : SwateObsolete.TermMinimal -> Async<Term []>

Check warning on line 241 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 241 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 241 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated

Check warning on line 241 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated
getTermsForAdvancedSearch : (AdvancedSearchTypes.AdvancedSearchOptions) -> Async<Term []>
getUnitTermSuggestions : (int*string) -> Async<Term []>
getTermsByNames : SwateObsolete.TermSearchable [] -> Async<SwateObsolete.TermSearchable []>

Check warning on line 244 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 244 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

This construct is deprecated

Check warning on line 244 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated

Check warning on line 244 in src/Shared/Shared.fs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

This construct is deprecated

// Tree related requests
getTreeByAccession : string -> Async<TreeTypes.Tree>
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