Skip to content

Commit

Permalink
Add DBXref functionality and expand type
Browse files Browse the repository at this point in the history
  • Loading branch information
omaus committed Jun 4, 2024
1 parent 2e583a5 commit 50645d7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
47 changes: 27 additions & 20 deletions src/OBO.NET/DBXref.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,34 @@ open ControlledVocabulary
open System


/// Representation of dbxrefs.
type DBXref = {
/// Representation of DBXrefs.
type DBXref =
{
Name : string
Description : string
Modifiers : string
}

/// Parses a given string to a DBXref
static member ofString (v : string) =
let xrefRegex = Text.RegularExpressions.Regex("""(?<xrefName>^([^"{])*)(\s?)(?<xrefDescription>\"(.*?)\")?(\s?)(?<xrefModifiers>\{(.*?)}$)?""")
let matches = xrefRegex.Match(v.Trim()).Groups
{
Name = matches.Item("xrefName") .Value |> String.trim
Description = matches.Item("xrefDescription") .Value |> String.trim
Modifiers = matches.Item("xrefModifiers") .Value |> String.trim
}

/// Returns the corresponding CvTerm of the DBXref with empty name.
member this.ToCvTerm() = {
Name = ""
Accession = this.Name
RefUri =
String.split ':' this.Name
|> Array.head
|> String.trim
}


/// Functions for working with DBXrefs.
module DBXref =
Expand Down Expand Up @@ -45,24 +66,10 @@ module DBXref =
let trimComment (line : string) =
line.Split('!').[0].Trim()

let private xrefRegex =
Text.RegularExpressions.Regex("""(?<xrefName>^([^"{])*)(\s?)(?<xrefDescription>\"(.*?)\")?(\s?)(?<xrefModifiers>\{(.*?)}$)?""")

[<Obsolete "Use `DBXref.ofString` instead">]
let parseDBXref (v : string) =
let matches = xrefRegex.Match(v.Trim()).Groups
{
Name = matches.Item("xrefName").Value
Description = matches.Item("xrefDescription").Value
Modifiers = matches.Item("xrefModifiers").Value
}
DBXref.ofString v

/// Creates a CvTerm (with an empty name) of a given DBXref.
let toCvTerm dbxref : CvTerm =
{
Name = ""
Accession = dbxref.Name
RefUri =
String.split ':' dbxref.Name
|> Array.head
|> String.trim
}
let toCvTerm (dbxref : DBXref) =
dbxref.ToCvTerm()
35 changes: 35 additions & 0 deletions tests/OBO.NET.Tests/DBXref.Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace OBO.NET.Tests


open OBO.NET

open Expecto
open ControlledVocabulary


module DBXref =

[<Tests>]
let dbxrefTests =
testList "DBXref" [

let testDBXref = DBXref.ofString """test:1 "testDesc" {testMod}"""

testList "ofString" [
testCase "returns correct DBXref" <| fun _ ->
let expected = {Name = "test:1"; Description = "\"testDesc\""; Modifiers = "{testMod}"}
Expect.equal testDBXref.Name expected.Name "Name does not match"
Expect.equal testDBXref.Description expected.Description "Description does not match"
Expect.equal testDBXref.Modifiers expected.Modifiers "Modifiers do not match"
]

testList "ToCvTerm" [
testCase "returns correct CvTerm" <| fun _ ->
let actual = testDBXref.ToCvTerm()
let expected = CvTerm.create("test:1", "", "test")
Expect.equal actual.Accession expected.Accession "TANs are not equal"
Expect.equal actual.RefUri expected.RefUri "TSRs are not equal"
Expect.equal actual.Name expected.Name "Names are not equal"
]

]
3 changes: 1 addition & 2 deletions tests/OBO.NET.Tests/OBO.NET.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<None Include="References\IncorrectHeaderTags.obo" />
<None Include="References\DuplicateHeaderTags.obo" />
<None Include="references\CorrectHeaderTags.obo" />
<Compile Include="DBXref.Tests.fs" />
<Compile Include="TermSynonym.Tests.fs" />
<Compile Include="OboTerm.Tests.fs" />
<Compile Include="OboOntology.Tests.fs" />
Expand All @@ -32,6 +33,4 @@
<PackageReference Update="FSharp.Core" Version="7.0.401" />
</ItemGroup>

<ItemGroup />

</Project>

0 comments on commit 50645d7

Please sign in to comment.