Skip to content
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
191 changes: 190 additions & 1 deletion src/Ontology.NET/OBO/OboOntology.fs

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions src/Ontology.NET/OBO/OboTerm.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
namespace Ontology.NET.OBO


open System
open System.Text.RegularExpressions

open DBXref
open TermSynonym

open System

open ControlledVocabulary
open FSharpAux

Expand Down Expand Up @@ -337,7 +338,7 @@
propertyValues builtIn createdBy creationDate

| "xref" | "xref_analog" | "xref_unk" ->
let v = (split.[1..] |> String.concat ": ") |> parseDBXref

Check warning on line 341 in src/Ontology.NET/OBO/OboTerm.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This construct is deprecated. Use `DBXref.ofString` instead

Check warning on line 341 in src/Ontology.NET/OBO/OboTerm.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This construct is deprecated. Use `DBXref.ofString` instead
OboTerm.fromLines verbose en (lineNumber + 1)
id name isAnonymous altIds definition comment subsets synonyms (v::xrefs) isA
intersectionOf unionOf disjointFrom relationships isObsolete replacedby consider
Expand Down Expand Up @@ -517,10 +518,21 @@

/// Takes a relationship and returns a tuple consisting of the name of the relationship and the ID of the OboTerm it matches.
static member deconstructRelationship relationship =
let pattern = System.Text.RegularExpressions.Regex @"^(?<relName>.+?) (?<id>[^ ]+:\d+)(?: .*)?$"
let pattern = Regex @"^(?<relName>.+?) (?<id>[^ ]+:\d+)(?: .*)?$"
let regexMatch = pattern.Match relationship
regexMatch.Groups["relName"].Value, regexMatch.Groups["id"].Value

/// <summary>
/// Takes the ID of a target term and the name of a relationship and constructs the relationship from them.
/// </summary>
/// <param name="targetTermId">The ID of the term that the relationship targets.</param>
/// <param name="relationshipName">The name of the relationship, e.g. "part_of".</param>
static member constructRelationship targetTermId relationshipName =
let whiteSpacePattern = Regex(@"\s")
if whiteSpacePattern.Match(relationshipName).Success then
raise (System.ArgumentException($"relationshipName {relationshipName} must not contain white spaces.", relationshipName))
$"{targetTermId} {relationshipName}"

/// Returns the OboTerm's relationships as a triple consisting of the term's ID, the name of the relationship, and the related term's ID.
member this.GetRelatedTermIds() =
this.Relationships
Expand Down
285 changes: 225 additions & 60 deletions src/Ontology.NET/Ontology.fs

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions src/Ontology.NET/OntologyRelation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ type RelationType =
| Term of CvTerm
| Custom of string

with
/// <summary>
/// Creates a RelationType from a given string.
/// </summary>
/// <param name="str">The string that is used to create a RelationType.</param>
static member fromString (str : string) =
match str.ToLower() with
| "is_a"
| "is a"
| "isa" -> IsA
| "xref"
| "x_ref"
| "x ref" -> Xref
| _ -> Custom str

/// <summary>
/// Returns the RelationType as a string.
/// </summary>
override this.ToString() : string =
match this with
| IsA -> "is_a"
| Xref -> "xref"
| Custom r -> r
| Term cvt -> cvt.Name

/// <summary>
/// Returns a RelationType as a string.
/// </summary>
static member toString (rt : RelationType) =
rt.ToString()


/// Model for a generic ontological relation. Consists of fields RelationType that inhabits the type of the relation, and Target for the targeted CvTerm of the relation.
type OntologyRelation = {
Expand Down
40 changes: 20 additions & 20 deletions tests/Ontology.NET.Tests/OBO/OboOntology.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ module OboOntologyTests =
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 formatVersionActual = testFile1 |> Option.map (fun o -> o.FormatVersion)
let dataVersionActual = testFile1 |> Option.map (fun o -> o.DataVersion) |> Option.flatten
let ontologyActual = testFile1 |> Option.map (fun o -> o.Ontology) |> Option.flatten
let dateActual = testFile1 |> Option.map (fun o -> o.Date) |> Option.flatten
let savedByActual = testFile1 |> Option.map (fun o -> o.SavedBy) |> Option.flatten
let autoGeneratedByActual = testFile1 |> Option.map (fun o -> o.AutoGeneratedBy) |> Option.flatten
let subsetdefsActual = testFile1 |> Option.map (fun o -> o.Subsetdefs)
let importsActual = testFile1 |> Option.map (fun o -> o.Imports)
let synonymtypedefsActual = testFile1 |> Option.map (fun o -> o.Synonymtypedefs)
let idSpacesActual = testFile1 |> Option.map (fun o -> o.Idspaces)
let defaultRelationshipIdPrefixActual = testFile1 |> Option.map (fun o -> o.DefaultRelationshipIdPrefix) |> Option.flatten
let idMappingsActual = testFile1 |> Option.map (fun o -> o.IdMappings)
let remarksActual = testFile1 |> Option.map (fun o -> o.Remarks)
let treatXrefsAsEquivalentsActual = testFile1 |> Option.map (fun o -> o.TreatXrefsAsEquivalents)
let treatXrefsAsGenusDifferentiasActual = testFile1 |> Option.map (fun o -> o.TreatXrefsAsGenusDifferentias)
let treatXrefsAsRelationshipsActual = testFile1 |> Option.map (fun o -> o.TreatXrefsAsRelationships)
let treatXrefsAsIsAsActual = testFile1 |> Option.map (fun o -> o.TreatXrefsAsIsAs)
let relaxUniqueIdentifierAssumptionForNamespacesActual = testFile1 |> Option.map (fun o -> o.RelaxUniqueIdentifierAssumptionForNamespaces)
let relaxUniqueLabelAssumptionForNamespacesActual = testFile1 |> Option.map (fun o -> o.RelaxUniqueLabelAssumptionForNamespaces)
let formatVersionExpected = "0.0.1" |> Some
let dataVersionExpected = "0.0.1" |> Some
let ontologyExpected = "CL" |> Some
Expand Down Expand Up @@ -129,7 +129,7 @@ module OboOntologyTests =
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"
Expect.isNone (testFile2 |> Option.map (fun o -> o.Date) |> 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"
Expand Down
10 changes: 10 additions & 0 deletions tests/Ontology.NET.Tests/OBO/OboTerm.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ module OboTermTests =
Expect.equal actual.Name expected.Name "Names are different"
Expect.equal actual.Accession expected.Accession "TANs are different"
]

testList "constructRelationship" [
testCase "returns correct relationship string" <| fun _ ->
let actual = OboTerm.constructRelationship "part_of" "TGMA:0000002"
let expected = "part_of TGMA:0000002"
Expect.equal actual expected "relationship strings differ"

testCase "throws when expected" <| fun _ ->
Expect.throws (fun _ -> OboTerm.constructRelationship "TGMA:0000002" "part of" |> ignore) "Did not throw though expected"
]
]
3 changes: 1 addition & 2 deletions tests/Ontology.NET.Tests/Ontology.NET.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
<Compile Include="OBO\OboOntology.Tests.fs" />
<Compile Include="OBO\OboTypeProvider.Tests.fs" />
<Compile Include="ReferenceObjects.fs" />
<Compile Include="RelationType.Tests.fs" />
<Compile Include="Ontology.Tests.fs" />
<Compile Include="Main.fs" />
</ItemGroup>

<ItemGroup />

</Project>
Loading
Loading