Skip to content

Commit

Permalink
add ontology search functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Jul 19, 2022
1 parent 7385792 commit 11ef6b8
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 136 deletions.
115 changes: 95 additions & 20 deletions src/ISADotNet.QueryModel/Obo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ open ISADotNet

/// Functions for par
module Obo =

let trimComment (line : string) =
line.Split('!').[0].Trim()

open System

//Dbxref definitions take the following form:
Expand Down Expand Up @@ -336,7 +340,7 @@ module Obo =
propertyValues builtIn createdBy creationDate =

if en.MoveNext() then
let split = en.Current.Split([|": "|], System.StringSplitOptions.None)
let split = (en.Current |> trimComment).Split([|": "|], System.StringSplitOptions.None)
match split.[0] with
| "id" ->
let v = split.[1..] |> String.concat ": "
Expand Down Expand Up @@ -689,7 +693,7 @@ module Obo =
is_anti_symmetric is_transitive is_metadata_tag is_class_level =

if en.MoveNext() then
let split = en.Current.Split([|": "|], System.StringSplitOptions.None)
let split = (en.Current |> trimComment).Split([|": "|], System.StringSplitOptions.None)
match split.[0] with
| "id" ->
let v = split.[1..] |> String.concat ": "
Expand Down Expand Up @@ -815,7 +819,7 @@ module Obo =

match en.MoveNext() with
| true ->
match en.Current with
match (en.Current |> trimComment) with
| "[Term]" -> let lineNumber,parsedTerm = (OboTerm.fromLines verbose en lineNumber "" "" false [] "" "" [] [] [] [] [] [] [] [] false [] [] [] false "" "")
loop en (parsedTerm :: terms) typedefs lineNumber
| "[Typedef]" -> let lineNumber,parsedTypeDef = (OboTypeDef.fromLines verbose en lineNumber "" "" "" "" [] [] false false false false false false false)
Expand Down Expand Up @@ -875,9 +879,35 @@ module Obo =
if t.Id = id then Some (OboTerm.toOntologyAnnotation t) else None
)

member this.GetEquivalentOntologyAnnotations(term : ISADotNet.OntologyAnnotation) =
let rec loop (equivalents : ISADotNet.OntologyAnnotation list) (lastLoop : ISADotNet.OntologyAnnotation list) =
member this.TryGetTermByName(name : string) =
this.Terms
|> List.tryFind (fun t ->
t.Name = name
)

member this.GetTermByName(name : string) =
this.Terms
|> List.find (fun t ->
t.Name = name
)

member this.TryGetOntologyAnnotationByName(name : string) =
this.Terms
|> List.tryPick (fun t ->
if t.Name = name then Some (OboTerm.toOntologyAnnotation t) else None
)

member this.GetOntologyAnnotationByName(name : string) =
this.Terms
|> List.pick (fun t ->
if t.Name = name then Some (OboTerm.toOntologyAnnotation t) else None
)

member this.GetEquivalentOntologyAnnotations(term : ISADotNet.OntologyAnnotation, ?Depth : int) =

let rec loop depth (equivalents : ISADotNet.OntologyAnnotation list) (lastLoop : ISADotNet.OntologyAnnotation list) =
if equivalents.Length = lastLoop.Length then equivalents
elif Depth.IsSome && Depth.Value < depth then equivalents
else
let newEquivalents =
equivalents
Expand All @@ -896,17 +926,23 @@ module Obo =
| None ->
[]
)
loop newEquivalents equivalents
loop [term] []
loop (depth + 1) (equivalents @ newEquivalents |> List.distinct) equivalents
loop 1 [term] []
|> List.filter ((<>) term)

member this.GetEquivalentOntologyAnnotations(termId : string) =
OntologyAnnotation.fromAnnotationId termId
|> this.GetEquivalentOntologyAnnotations

member this.GetParentOntologyAnnotations(term : ISADotNet.OntologyAnnotation) =
let rec loop (equivalents : ISADotNet.OntologyAnnotation list) (lastLoop : ISADotNet.OntologyAnnotation list) =
member this.GetEquivalentOntologyAnnotations(termId : string, ?Depth) =
match Depth with
| Some d ->
OntologyAnnotation.fromAnnotationId termId
|> fun oa -> this.GetEquivalentOntologyAnnotations(oa, d)
| None ->
OntologyAnnotation.fromAnnotationId termId
|> this.GetEquivalentOntologyAnnotations

member this.GetParentOntologyAnnotations(term : ISADotNet.OntologyAnnotation, ?Depth) =
let rec loop depth (equivalents : ISADotNet.OntologyAnnotation list) (lastLoop : ISADotNet.OntologyAnnotation list) =
if equivalents.Length = lastLoop.Length then equivalents
elif Depth.IsSome && Depth.Value < depth then equivalents
else
let newEquivalents =
equivalents
Expand All @@ -924,13 +960,52 @@ module Obo =
| None ->
[]
)
loop newEquivalents equivalents
loop [term] []
loop (depth + 1) (equivalents @ newEquivalents |> List.distinct) equivalents
loop 1 [term] []
|> List.filter ((<>) term)

member this.GetParentOntologyAnnotations(termId : string, ?Depth) =
match Depth with
| Some d ->
OntologyAnnotation.fromAnnotationId termId
|> fun oa -> this.GetParentOntologyAnnotations(oa, d)
| None ->
OntologyAnnotation.fromAnnotationId termId
|> this.GetParentOntologyAnnotations

member this.GetChildOntologyAnnotations(term : ISADotNet.OntologyAnnotation, ?Depth) =
let rec loop depth (equivalents : ISADotNet.OntologyAnnotation list) (lastLoop : ISADotNet.OntologyAnnotation list) =
if equivalents.Length = lastLoop.Length then equivalents
elif Depth.IsSome && Depth.Value < depth then equivalents
else
let newEquivalents =
equivalents
|> List.collect (fun t ->
this.Terms
|> List.choose (fun pt ->
let isChild =
pt.IsA
|> List.exists (fun isA -> t.AnnotationID = isA)
if isChild then
Some (OboTerm.toOntologyAnnotation(pt))
else
None

)
)
loop (depth + 1) (equivalents @ newEquivalents |> List.distinct) equivalents
loop 1 [term] []
|> List.filter ((<>) term)

member this.GetParentOntologyAnnotations(termId : string) =
OntologyAnnotation.fromAnnotationId termId
|> this.GetParentOntologyAnnotations
member this.GetChildOntologyAnnotations(termId : string, ?Depth) =
match Depth with
| Some d ->
OntologyAnnotation.fromAnnotationId termId
|> fun oa -> this.GetChildOntologyAnnotations(oa, d)
| None ->
OntologyAnnotation.fromAnnotationId termId
|> this.GetChildOntologyAnnotations


type OboTermDef =
{
Expand All @@ -946,7 +1021,7 @@ module Obo =
//parseTermDef
static member fromLines (en:Collections.Generic.IEnumerator<string>) id name isTransitive isCyclic =
if en.MoveNext() then
let split = en.Current.Split([|": "|], System.StringSplitOptions.None)
let split = (en.Current |> trimComment).Split([|": "|], System.StringSplitOptions.None)
match split.[0] with
| "id" -> OboTermDef.fromLines en (split.[1..] |> String.concat ": ") name isTransitive isCyclic
| "name" -> OboTermDef.fromLines en id (split.[1..] |> String.concat ": ") isTransitive isCyclic
Expand All @@ -971,7 +1046,7 @@ module Obo =
seq {
match en.MoveNext() with
| true ->
match en.Current with
match (en.Current |> trimComment) with
| "[Term]" -> let lineNumber,parsedTerm = (OboTerm.fromLines verbose en lineNumber "" "" false [] "" "" [] [] [] [] [] [] [] [] false [] [] [] false "" "")
yield parsedTerm
yield! loop en lineNumber
Expand Down
33 changes: 27 additions & 6 deletions src/ISADotNet.QueryModel/OntologyAnnotation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,43 @@ module OntologyAnnotation =
member this.ToTerm() =
OntologyAnnotation.toTerm(this)

static member findTerm(nameOrId : string) =
Term.Search(nameOrId, 1).[0]

static member findTerm(nameOrId : string,ont : Obo.OboOntology) =
match ont.TryGetOntologyAnnotation nameOrId with
| Some oa ->
oa
| None ->
match ont.TryGetOntologyAnnotationByName nameOrId with
| Some oa -> oa
| None -> failwithf "could not find Ontology term %s in given ontology" nameOrId

static member isChildTerm(parent : OntologyAnnotation,child : OntologyAnnotation,ont : Obo.OboOntology) =
Term.SearchByParent(child.NameText, 1, parent |> OntologyAnnotation.toTerm)
|> Array.isEmpty
|> not
ont.GetParentOntologyAnnotations(child)
|> List.contains parent

member this.IsChildTermOf(parent : OntologyAnnotation) =
OntologyAnnotation.isChildTerm(parent,this)


static member isChildTerm (parent : OntologyAnnotation,child : OntologyAnnotation) =
Term.SearchByParent(child.NameText, 1, parent |> OntologyAnnotation.toTerm)
|> Array.isEmpty
|> not

//member this.IsChildTermOf(parent : OntologyAnnotation) =
// OntologyAnnotation.isChildTerm parent this
member this.IsChildTermOf(parent : OntologyAnnotation, ont : Obo.OboOntology) =
OntologyAnnotation.isChildTerm(parent,this,ont)

static member isEquivalentTo(term : OntologyAnnotation,targetTerm : OntologyAnnotation,ont : Obo.OboOntology) =
ont.GetEquivalentOntologyAnnotations(term)
|> List.contains targetTerm

member this.IsEquivalentTo(targetTerm : OntologyAnnotation, ont : Obo.OboOntology) =
OntologyAnnotation.isEquivalentTo(targetTerm,this,ont)

static member getAs (term : OntologyAnnotation, targetOntology : string, ont : Obo.OboOntology) =
ont.GetEquivalentOntologyAnnotations(term)
|> List.find (fun t -> t.TermSourceREFString = targetOntology)

member this.GetAs(targetOntology : string, ont : Obo.OboOntology) =
OntologyAnnotation.getAs(this,targetOntology,ont)
109 changes: 0 additions & 109 deletions src/ISADotNet.XLSX/AssayFile/SwateTable.fs

This file was deleted.

1 change: 0 additions & 1 deletion src/ISADotNet.XLSX/ISADotNet.XLSX.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<Compile Include="AssayFile\AnnotationNode.fs" />
<Compile Include="AssayFile\AnnotationTable.fs" />
<Compile Include="AssayFile\MetaData.fs" />
<Compile Include="AssayFile\SwateTable.fs" />
<Compile Include="AssayFile\Assay.fs" />
<Compile Include="StudyFile\MetaData.fs" />
<Compile Include="StudyFile\Study.fs" />
Expand Down

0 comments on commit 11ef6b8

Please sign in to comment.