From 59cfdafb0b3e316c6a1dbc9034a1f38574ee00b5 Mon Sep 17 00:00:00 2001 From: Heinrich Lukas Weil Date: Fri, 5 Aug 2022 17:03:00 +0200 Subject: [PATCH] small improvements to Ontology Annotation and Component type --- src/ISADotnet/DataModel/Ontology.fs | 55 ++++++++++++++++++++++++++++- src/ISADotnet/DataModel/Protocol.fs | 2 +- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/ISADotnet/DataModel/Ontology.fs b/src/ISADotnet/DataModel/Ontology.fs index 80e4f53d..29f558e9 100644 --- a/src/ISADotnet/DataModel/Ontology.fs +++ b/src/ISADotnet/DataModel/Ontology.fs @@ -26,6 +26,7 @@ type AnnotationValue = | Int i -> string i | Float f -> string f +[] type OntologyAnnotation = { [] @@ -125,16 +126,45 @@ type OntologyAnnotation = else None + /// Tries to split a term id in form of `MS:1000121` into it's Term Accession Source `MS` and Term Accession Number `1000121`. + static member trySplitShortAnnotation (termAccessionPath : string) = + let r = Regex.Match(termAccessionPath,OntologyAnnotation.shortAnnotationRegex) + if r.Success then + Some (r.Groups.Item("ref").Value,r.Groups.Item("num").Value) + else + None + + /// Create a path in form of `http://purl.obolibrary.org/obo/MS_1000121`from it's Term Accession Source `MS` and Term Accession Number `1000121`. + static member createUriAnnotation (termSourceRef : string) (termAccessionNumber : string) = + let r = Regex.Match(termAccessionNumber,OntologyAnnotation.ontologyTermURIRegex) + let r2 = Regex.Match(termAccessionNumber,OntologyAnnotation.shortAnnotationRegex) + + if r.Success then + let termSourceRef = r.Groups.Item("ref").Value + let termAccessionNumber = r.Groups.Item("num").Value + $"http://purl.obolibrary.org/obo/{termSourceRef}_{termAccessionNumber}" + elif r2.Success then + let termSourceRef = r.Groups.Item("ref").Value + let termAccessionNumber = r.Groups.Item("num").Value + $"http://purl.obolibrary.org/obo/{termSourceRef}_{termAccessionNumber}" + else + $"http://purl.obolibrary.org/obo/{termSourceRef}_{termAccessionNumber}" + /// Creates an annotation of format `TermSourceRef:TermAccessionNumber` (e.g: `MS:1000690`) /// /// If termAccessionNumber is given in full URI form `http://purl.obolibrary.org/obo/MS_1000121`, takes last part of it. static member createShortAnnotation (termSourceRef : string) (termAccessionNumber : string) = let r = Regex.Match(termAccessionNumber,OntologyAnnotation.ontologyTermURIRegex) - + let r2 = Regex.Match(termAccessionNumber,OntologyAnnotation.shortAnnotationRegex) + if r.Success then let termSourceRef = r.Groups.Item("ref").Value let termAccessionNumber = r.Groups.Item("num").Value $"{termSourceRef}:{termAccessionNumber}" + elif r2.Success then + let termSourceRef = r.Groups.Item("ref").Value + let termAccessionNumber = r.Groups.Item("num").Value + $"{termSourceRef}:{termAccessionNumber}" else $"{termSourceRef}:{termAccessionNumber}" @@ -197,6 +227,17 @@ type OntologyAnnotation = else "" | None -> "" + member this.URLAnnotationString = + match this.TermAccessionNumber with + | Some t -> + match OntologyAnnotation.trySplitUri t with + | Some _ -> t + | None -> + let r = System.Text.RegularExpressions.Regex.Match(t,OntologyAnnotation.shortAnnotationRegex) + if r.Success then t + else "" + | None -> "" + /// Get a ISATab string entries from an ISAJson Ontology Annotation object (name,source,accession) static member toString (oa : OntologyAnnotation) = @@ -210,6 +251,18 @@ type OntologyAnnotation = member this.PrintCompact() = "OA " + this.NameText + override this.Equals other = + match other with + | :? OntologyAnnotation as oa -> (this :> System.IEquatable<_>).Equals oa + | _ -> false + + override this.GetHashCode () = (this.NameText+this.ShortAnnotationString).GetHashCode() + + interface System.IEquatable with + member this.Equals other = + other.ShortAnnotationString = this.ShortAnnotationString + && + other.NameText = this.NameText type OntologySourceReference = { diff --git a/src/ISADotnet/DataModel/Protocol.fs b/src/ISADotnet/DataModel/Protocol.fs index 1a48a830..fbadd5f8 100644 --- a/src/ISADotnet/DataModel/Protocol.fs +++ b/src/ISADotnet/DataModel/Protocol.fs @@ -127,7 +127,7 @@ type Component = | None, _ -> "" static member decomposeName (name : string) = - let pattern = """(?\S*) \((?[^(]*:[^)]*)\)""" + let pattern = """(?[^\(]+) \((?[^(]*:[^)]*)\)""" let unitPattern = """(?[\d\.]+) (?.+) \((?[^(]*:[^)]*)\)""" let r = System.Text.RegularExpressions.Regex.Match(name,pattern)