Skip to content

Commit

Permalink
Merge pull request #35 from CSBiology/feature-DocumentHeaderTags-#33
Browse files Browse the repository at this point in the history
Add document header tags
  • Loading branch information
omaus authored May 27, 2024
2 parents b4c655a + 7244745 commit fdac911
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 23 deletions.
190 changes: 190 additions & 0 deletions playground.fsx

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/OBO.NET/OboEntries.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace OBO.NET


open System


/// Functions for working with OboEntries.
module OboEntries =

/// Reads a collection of strings and parses them into a list of OboEntries.
let fromLines verbose (input : seq<string>) =

let en = input.GetEnumerator()
let rec loop (en : System.Collections.Generic.IEnumerator<string>) entries lineNumber =

let rec loop (en : Collections.Generic.IEnumerator<string>) entries lineNumber =

match en.MoveNext() with
| true ->
Expand All @@ -26,5 +30,5 @@ module OboEntries =

/// Reads an OBO file and returns a list of OboEntries.
let fromFile verbose filepath =
System.IO.File.ReadAllLines filepath
IO.File.ReadAllLines filepath
|> fromLines verbose
181 changes: 162 additions & 19 deletions src/OBO.NET/OboOntology.fs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions tests/OBO.NET.Tests/OBO.NET.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
</PropertyGroup>

<ItemGroup>
<None Include="References\IncorrectHeaderTags.obo" />
<None Include="References\DuplicateHeaderTags.obo" />
<None Include="references\CorrectHeaderTags.obo" />
<Compile Include="TermSynonym.Tests.fs" />
<Compile Include="OboTerm.Tests.fs" />
<Compile Include="OboOntology.Tests.fs" />
Expand All @@ -29,4 +32,6 @@
<PackageReference Update="FSharp.Core" Version="7.0.401" />
</ItemGroup>

<ItemGroup />

</Project>
95 changes: 93 additions & 2 deletions tests/OBO.NET.Tests/OboOntology.Tests.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
namespace OBO.NET.Tests

open Expecto
open OBO.NET

open Expecto

open System
open System.IO


module OboOntologyTests =

[<Tests>]
let oboOntologyTest =
testList "OboOntology" [

let testTerm1 =
OboTerm.Create(
"id:1",
Expand Down Expand Up @@ -39,19 +44,103 @@ module OboOntologyTests =
Name = "testTerm5",
Synonyms = [TermSynonym.parseSynonym None 0 "\"testTerm1\" EXACT []"; TermSynonym.parseSynonym None 1 "\"testTerm2\" BROAD []"; TermSynonym.parseSynonym None 2 "\"testTerm0\" NARROW []"]
)
let testOntology = OboOntology.create [testTerm1; testTerm2; testTerm3; testTerm4; testTerm5] []

let testFile1Path = Path.Combine(__SOURCE_DIRECTORY__, "References", "CorrectHeaderTags.obo")
let testFile2Path = Path.Combine(__SOURCE_DIRECTORY__, "References", "IncorrectHeaderTags.obo")
let testFile3Path = Path.Combine(__SOURCE_DIRECTORY__, "References", "DuplicateHeaderTags.obo")
let testFile1 = try OboOntology.fromFile false testFile1Path |> Some with _ -> None
let testFile2 = try OboOntology.fromFile false testFile2Path |> Some with _ -> None
let testFile3 = try OboOntology.fromFile false testFile3Path |> Some with _ -> None

testList "fromFile" [
testCase "can read files" <| fun _ ->
Expect.isSome testFile1 $"Could not read testFile1: {testFile1Path}"
Expect.isSome testFile2 $"Could not read testFile2: {testFile2Path}"
Expect.isSome testFile3 $"Could not read testFile3: {testFile3Path}"
testCase "reads correct headers correctly" <| fun _ ->
let formatVersionActual = Option.map (fun o -> o.FormatVersion) testFile1
let dataVersionActual = Option.map (fun o -> o.DataVersion) testFile1 |> Option.flatten
let ontologyActual = Option.map (fun o -> o.Ontology) testFile1 |> Option.flatten
let dateActual = Option.map (fun o -> o.Date) testFile1 |> Option.flatten
let savedByActual = Option.map (fun o -> o.SavedBy) testFile1 |> Option.flatten
let autoGeneratedByActual = Option.map (fun o -> o.AutoGeneratedBy) testFile1 |> Option.flatten
let subsetdefsActual = Option.map (fun o -> o.Subsetdefs) testFile1
let importsActual = Option.map (fun o -> o.Imports) testFile1
let synonymtypedefsActual = Option.map (fun o -> o.Synonymtypedefs) testFile1
let idSpacesActual = Option.map (fun o -> o.Idspaces) testFile1
let defaultRelationshipIdPrefixActual = Option.map (fun o -> o.DefaultRelationshipIdPrefix) testFile1 |> Option.flatten
let idMappingsActual = Option.map (fun o -> o.IdMappings) testFile1
let remarksActual = Option.map (fun o -> o.Remarks) testFile1
let treatXrefsAsEquivalentsActual = Option.map (fun o -> o.TreatXrefsAsEquivalents) testFile1
let treatXrefsAsGenusDifferentiasActual = Option.map (fun o -> o.TreatXrefsAsGenusDifferentias) testFile1
let treatXrefsAsRelationshipsActual = Option.map (fun o -> o.TreatXrefsAsRelationships) testFile1
let treatXrefsAsIsAsActual = Option.map (fun o -> o.TreatXrefsAsIsAs) testFile1
let relaxUniqueIdentifierAssumptionForNamespacesActual = Option.map (fun o -> o.RelaxUniqueIdentifierAssumptionForNamespaces) testFile1
let relaxUniqueLabelAssumptionForNamespacesActual = Option.map (fun o -> o.RelaxUniqueLabelAssumptionForNamespaces) testFile1
let formatVersionExpected = "0.0.1" |> Some
let dataVersionExpected = "0.0.1" |> Some
let ontologyExpected = "CL" |> Some
let dateExpected = DateTime(1970, 1, 1, 0, 0, 0) |> Some
let savedByExpected = "Oliver Maus" |> Some
let autoGeneratedByExpected = "TalkGPT" |> Some
let subsetdefsExpected = ["GO_SLIM \"GO Slim\""; "GO_BASIC \"GO Basic\""] |> Some
let importsExpected = ["http://purl.obolibrary.org/obo/go.owl"; "http://purl.obolibrary.org/obo/cl.owl"] |> Some
let synonymtypedefsExpected = ["UK_SPELLING \"British spelling\" EXACT"; "US_SPELLING \"American spelling\" EXACT"] |> Some
let idspacesExpected = ["GO urn:lsid:bioontology.org:GO: \"gene ontology terms\""; "GO urn:lsid:bioontology.org:GO: \"gene ontology types\""] |> Some
let defaultRelationshipIdPrefixExpected = "OBO_REL" |> Some
let idMappingsExpected = ["part_of OBO_REL:part_of"; "has_a OBO_REL:has_a"] |> Some
let remarksExpected = ["test1"; "test2"] |> Some
let treatXrefsAsEquivalentExpected = ["CL"; "GO"] |> Some
let treatXrefsAsGenusDifferentiaExpected = ["CL part_of NCBITaxon:7955"; "CL part_of NCBITaxon:7956"] |> Some
let treatXrefsAsRelationshipExpected = ["MA homologous_to"; "MA analogous_to"] |> Some
let treatXrefsAsIsAExpected = ["CL"; "GO"] |> Some
let relaxUniqueIdentifierAssumptionForNamespaceExpected = ["my_combined_ontology"; "my_combined_ontology2"] |> Some
let relaxUniqueLabelAssumptionForNamespaceExpected = ["my_combined_ontology"; "my_combined_ontology2"] |> Some
Expect.equal formatVersionActual formatVersionExpected "format-version is not identical"
Expect.equal dataVersionActual dataVersionExpected "data-version is not identical"
Expect.equal ontologyActual ontologyExpected "ontology is not identical"
Expect.equal dateActual dateExpected "date is not identical"
Expect.equal savedByActual savedByExpected "saved-by is not identical"
Expect.equal autoGeneratedByActual autoGeneratedByExpected "auto-generated-by is not identical"
Expect.equal subsetdefsActual subsetdefsExpected "subsetdefs is not identical"
Expect.equal importsActual importsExpected "imports are not identical"
Expect.equal synonymtypedefsActual synonymtypedefsExpected "synonymtypedefs are not identical"
Expect.equal idSpacesActual idspacesExpected "idspaces are not identical"
Expect.equal defaultRelationshipIdPrefixActual defaultRelationshipIdPrefixExpected "default-relationship-id-prefix is not identical"
Expect.equal idMappingsActual idMappingsExpected "id-mappings are not identical"
Expect.equal remarksActual remarksExpected "remarks are not identical"
Expect.equal treatXrefsAsEquivalentsActual treatXrefsAsEquivalentExpected "treat-xrefs-as-equivalents are not identical"
Expect.equal treatXrefsAsGenusDifferentiasActual treatXrefsAsGenusDifferentiaExpected "treat-xrefs-as-genus-differentia are not identical"
Expect.equal treatXrefsAsRelationshipsActual treatXrefsAsRelationshipExpected "treat-xrefs-as-relationships are not identical"
Expect.equal treatXrefsAsIsAsActual treatXrefsAsIsAExpected "treat-xrefs-as-is-a are not identical"
Expect.equal relaxUniqueIdentifierAssumptionForNamespacesActual relaxUniqueIdentifierAssumptionForNamespaceExpected "relax-unique-identifier-assumption-for-namespaces are not identical"
Expect.equal relaxUniqueLabelAssumptionForNamespacesActual relaxUniqueLabelAssumptionForNamespaceExpected "relax-unique-label-assumption-for-namespaces are not identical"
testCase "reads incorrect headers correctly" <| fun _ ->
Expect.isNone (Option.map (fun o -> o.Date) testFile2 |> Option.flatten) "Date should be missing but was still parsed"
testCase "reads Terms correctly" <| fun _ ->
let termsExpected = List.init 2 (fun i -> OboTerm.Create $"Test:000{i + 1}") |> Some
Expect.equal (Option.map (fun o -> o.Terms) testFile1) termsExpected "Terms did not match"
testCase "reads Typedefs correctly" <| fun _ ->
let typedefsExpected = List.init 2 (fun i -> OboTypeDef.Create($"Test:000{i + 3}", "", "")) |> Some
Expect.equal (Option.map (fun o -> o.TypeDefs) testFile1) typedefsExpected "Terms did not match"
]

let testOntology = OboOntology.Create([testTerm1; testTerm2; testTerm3; testTerm4; testTerm5], [], "")

testList "GetRelatedTerms" [
testCase "returns correct related terms" <| fun _ ->
let actual = testOntology.GetRelatedTerms(testTerm1)
let expected = [testTerm1, "related_to", Some testTerm2; testTerm1, "unrelated_to", Some testTerm3; testTerm1, "antirelated_to", None]
Expect.sequenceEqual actual expected "is not equal"
]

testList "GetIsAs" [
testCase "returns correct related terms" <| fun _ ->
let actual = testOntology.GetIsAs testTerm3
let expected = [testTerm3, Some testTerm1; testTerm3, Some testTerm2]
Expect.sequenceEqual actual expected "is not equal"
]

testList "GetRelations" [
testCase "returns correct TermRelations" <| fun _ ->
let actual = testOntology.GetRelations()
Expand All @@ -68,12 +157,14 @@ module OboOntologyTests =
]
Expect.sequenceEqual actual expected "is not equal"
]

testList "GetSynonyms" [
testCase "returns correct synonymous terms" <| fun _ ->
let actual = testOntology.GetSynonyms testTerm5
let expected = seq {Exact, testTerm5, testTerm1; Broad, testTerm5, testTerm2}
Expect.sequenceEqual actual expected "is not equal"
]

testList "TryGetSynonyms" [
testCase "returns correct synonymous terms" <| fun _ ->
let actual = testOntology.TryGetSynonyms testTerm5
Expand Down
47 changes: 47 additions & 0 deletions tests/OBO.NET.Tests/References/CorrectHeaderTags.obo
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
format-version: 0.0.1
data-version: 0.0.1
ontology: CL
date: 01:01:1970 00:00
saved-by: Oliver Maus
auto-generated-by: TalkGPT
subsetdef: GO_SLIM "GO Slim"
subsetdef: GO_BASIC "GO Basic"
import: http://purl.obolibrary.org/obo/go.owl
import: http://purl.obolibrary.org/obo/cl.owl
synonymtypedef: UK_SPELLING "British spelling" EXACT
synonymtypedef: US_SPELLING "American spelling" EXACT
idspace: GO urn:lsid:bioontology.org:GO: "gene ontology terms"
idspace: GO urn:lsid:bioontology.org:GO: "gene ontology types"
default-relationship-id-prefix: OBO_REL
id-mapping: part_of OBO_REL:part_of
id-mapping: has_a OBO_REL:has_a
remark: test1
remark: test2
treat-xrefs-as-equivalent: CL
treat-xrefs-as-equivalent: GO
treat-xrefs-as-genus-differentia: CL part_of NCBITaxon:7955
treat-xrefs-as-genus-differentia: CL part_of NCBITaxon:7956
treat-xrefs-as-relationship: MA homologous_to
treat-xrefs-as-relationship: MA analogous_to
treat-xrefs-as-is_a: CL
treat-xrefs-as-is_a: GO
relax-unique-identifier-assumption-for-namespace: my_combined_ontology
relax-unique-identifier-assumption-for-namespace: my_combined_ontology2
relax-unique-label-assumption-for-namespace: my_combined_ontology
relax-unique-label-assumption-for-namespace: my_combined_ontology2

[Term]
id: Test:0001

[Term]
id: Test:0002

[Typedef]
id: Test:0003
name:
range:

[Typedef]
id: Test:0004
name:
range:
28 changes: 28 additions & 0 deletions tests/OBO.NET.Tests/References/DuplicateHeaderTags.obo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
format-version: 0.0.1
format-version: 0.0.2
data-version: 0.0.1
data-version: 0.0.2
date: 01:01:1970 00:00
date: 01:01:1970 00:01
saved-by: Oliver Maus
saved-by: Loliver Laus
auto-generated-by: TalkGPT
auto-generated-by: SpeakGPT
default-relationship-id-prefix: OBO_REL
default-relationship-id-prefix: BABO_REL

[Term]
id: Test:0001

[Term]
id: Test:0002

[Typedef]
id: Test:0003
name:
range:

[Typedef]
id: Test:0004
name:
range:
17 changes: 17 additions & 0 deletions tests/OBO.NET.Tests/References/IncorrectHeaderTags.obo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
date: 32:14:1970 25:61

[Term]
id: Test:0001

[Term]
id: Test:0002

[Typedef]
id: Test:0003
name:
range:

[Typedef]
id: Test:0004
name:
range:

0 comments on commit fdac911

Please sign in to comment.