diff --git a/src/Ontology.NET/Ontology.fs b/src/Ontology.NET/Ontology.fs index 0746744..52525aa 100644 --- a/src/Ontology.NET/Ontology.fs +++ b/src/Ontology.NET/Ontology.fs @@ -201,6 +201,20 @@ type Ontology() = onto + /// + /// Returns the Ontology as a collection of Triplets. + /// + /// A collection of Triplets in the form of SourceTerm * Relation * TargetTerm. + member this.ToTriplets() = + FGraph.toSeq this + |> Seq.collect ( + fun (nk1,nd1,nk2,nd2,es) -> + es + |> Seq.map ( + fun e -> nd1, e, nd2 + ) + ) + // basic functionality: @@ -1306,4 +1320,11 @@ type Ontology() = /// A sequence of term IDs representing all transitively is_a source-related terms and their xref-related terms that can be reached within the given depth. /// A source relation is an incoming relation. E.g. "Term A -> Term B", Term A is the source-related term to Term B. If "->" is an is_a relation, Term A is the subclass of Term B. static member getSubClassesWithDepthWithXrefsTransitively termID depth (onto : Ontology) = - onto.GetSubClassesWithDepthWithXrefsTransitively(termID, depth) \ No newline at end of file + onto.GetSubClassesWithDepthWithXrefsTransitively(termID, depth) + + /// + /// Returns the given Ontology as a collection of Triplets. + /// + /// A collection of Triplets in the form of SourceTerm * Relation * TargetTerm. + static member toTriplets (onto : Ontology) = + onto.ToTriplets() \ No newline at end of file diff --git a/tests/Ontology.NET.Tests/Ontology.Tests.fs b/tests/Ontology.NET.Tests/Ontology.Tests.fs index 5e7fa0a..128de2c 100644 --- a/tests/Ontology.NET.Tests/Ontology.Tests.fs +++ b/tests/Ontology.NET.Tests/Ontology.Tests.fs @@ -358,4 +358,37 @@ module OntologyTests = Expect.equal actual.Ontology expected.Ontology "ontology (i.e., ontology name) differs" ] + testList "ToTriplets" [ + testCase "returns correct triplets" <| fun _ -> + let actual = ReferenceObjects.testOnto1.ToTriplets() |> Seq.toList + let expected = [ + ({ Accession = "test:01" + Name = "Frosch" + RefUri = "test" }, Xref, { Accession = "test:02" + Name = "Kröte" + RefUri = "test" }); + ({ Accession = "test:01" + Name = "Frosch" + RefUri = "test" }, IsA, { Accession = "test:04" + Name = "Tier" + RefUri = "test" }); + ({ Accession = "test:02" + Name = "Kröte" + RefUri = "test" }, IsA, { Accession = "test:04" + Name = "Tier" + RefUri = "test" }); + ({ Accession = "test:03" + Name = "Quakendes Geschöpf" + RefUri = "test" }, Xref, { Accession = "test:02" + Name = "Kröte" + RefUri = "test" }); + ({ Accession = "test:03" + Name = "Quakendes Geschöpf" + RefUri = "test" }, IsA, { Accession = "test:04" + Name = "Tier" + RefUri = "test" }) + ] + Expect.sequenceEqual actual expected "Triplets differ" + ] + ] \ No newline at end of file