Skip to content

Commit

Permalink
Merge pull request #9 from CSBiology/feature-minimalParser-#5
Browse files Browse the repository at this point in the history
#5 Minimal parser
  • Loading branch information
omaus authored Jul 28, 2023
2 parents a97d033 + e9c6c34 commit 63512c8
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
22 changes: 22 additions & 0 deletions playground.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#r "FsOboParser.dll"

#r "nuget: IsaDotNet"
//#r "nuget: FsOboParser"

open FsOboParser

Expand All @@ -10,8 +11,29 @@ open System.IO

let testPath = Path.Combine(__SOURCE_DIRECTORY__, "./../../nfdi4plants/arc-validate/ErrorClassOntology.obo")

//OboEntries.fromFile true testPath
let testOntology = OboOntology.fromFile true testPath

let testTerms = [
OboTerm.Create("test:000", Name = "test0")
OboTerm.Create("test:001", Name = "test1a", IsA = ["test:000"])
OboTerm.Create("test:002", Name = "test2", IsA = ["test:001"])
OboTerm.Create("test:003", Name = "test1b", IsA = ["test:000"])
]

let testOntology = OboOntology.create testTerms []

testOntology.GetChildOntologyAnnotations(testTerms.Head.Id)
testOntology.GetChildOntologyAnnotations(testTerms.Head.Id, Depth = 1)
testOntology.GetChildOntologyAnnotations(testTerms.Head.Id, Depth = 2)

let performanceTerms = List.init 7000000 (fun i -> OboTerm.Create($"lol:{i}"))
let performanceOboOntology = OboOntology.create performanceTerms []
OboOntology.toFile @"C:\Repos\CSBiology\FsOboParser\performanceOntology.obo" performanceOboOntology

let x = OboOntology.fromFile false @"C:\Repos\CSBiology\FsOboParser\performanceOntology.obo"


//let fileLines = File.ReadAllLines testPath

//OboTerm.fromLines true ((fileLines |> Seq.ofArray).GetEnumerator()) 0
Expand Down
3 changes: 2 additions & 1 deletion src/FsOboParser/FastOboGraph.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace FsOboParser

//open OboTerm

//open type OboTerm


////########################################
Expand Down
4 changes: 3 additions & 1 deletion src/FsOboParser/FsOboParser.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<Compile Include="TermSynonym.fs" />
<Compile Include="OboTerm.fs" />
<Compile Include="OboTypeDef.fs" />
<Compile Include="OboEntry.fs" />
<Compile Include="OboEntries.fs" />
<Compile Include="OboOntology.fs" />
<Compile Include="FastOboGraph.fs" />
</ItemGroup>
Expand All @@ -35,7 +37,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="ISADotNet" Version="0.6.1" />
</ItemGroup>

Expand Down
30 changes: 30 additions & 0 deletions src/FsOboParser/OboEntries.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace FsOboParser


/// 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 =

match en.MoveNext() with
| true ->
match (en.Current |> DBXref.trimComment) with
| "[Term]" ->
let lineNumber, parsedTerm = OboTerm.fromLines verbose en lineNumber "" "" false [] "" "" [] [] [] [] [] [] [] [] false [] [] [] false "" ""
loop en (Term parsedTerm :: entries) lineNumber
| "[Typedef]" ->
let lineNumber, parsedTypeDef = OboTypeDef.fromLines verbose en lineNumber "" "" "" "" [] [] false false false false false false false
loop en (TypeDef parsedTypeDef :: entries) lineNumber
| _ -> loop en entries (lineNumber + 1)
| false -> entries

loop en [] 1

/// Reads an OBO file and returns a list of OboEntries.
let fromFile verbose filepath =
System.IO.File.ReadAllLines filepath
|> fromLines verbose
7 changes: 7 additions & 0 deletions src/FsOboParser/OboEntry.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FsOboParser


/// Model of raw OboEntries, divided into Terms (as `OboTerm`s) and TypeDefs (as `OboTypeDef`s).
type OboEntry =
| Term of OboTerm
| TypeDef of OboTypeDef
19 changes: 17 additions & 2 deletions src/FsOboParser/OboOntology.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ type OboOntology =
| true ->
match (en.Current |> trimComment) with
| "[Term]" ->
let lineNumber,parsedTerm = (OboTerm.fromLines verbose en lineNumber "" "" false [] "" "" [] [] [] [] [] [] [] [] false [] [] [] false "" "")
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)
let lineNumber,parsedTypeDef = OboTypeDef.fromLines verbose en lineNumber "" "" "" "" [] [] false false false false false false false
loop en terms (parsedTypeDef :: typedefs) lineNumber
| _ -> loop en terms typedefs (lineNumber + 1)
| false -> OboOntology.create (List.rev terms) (List.rev typedefs)
Expand All @@ -48,6 +48,21 @@ type OboOntology =
System.IO.File.ReadAllLines path
|> OboOntology.fromLines verbose

/// Takes a list of OboEntries and returns the OboOntology based on it.
static member fromOboEntries entries =

let rec loop terms typedefs entries =
match entries with
| h :: t ->
match h with
| Term term -> loop (term :: terms) typedefs t
| TypeDef typedef -> loop terms (typedef :: typedefs) t
| [] -> terms, typedefs

let terms, typedefs = loop [] [] entries

OboOntology.create terms typedefs

/// Writes an OBO Ontology to term and type def stanzas in line form.
static member toLines (oboOntology : OboOntology) =
seq {
Expand Down
2 changes: 1 addition & 1 deletion src/FsOboParser/OboTypeDef.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open DBXref
open System


/// Models the relationship between OBO Terms
/// Models the relationship between OBO Terms.
type OboTypeDef =
{
///The unique id of the current term.
Expand Down

0 comments on commit 63512c8

Please sign in to comment.