Skip to content

Commit

Permalink
Rework CvTerm and CvUnit as record, use unified naming for their fiel…
Browse files Browse the repository at this point in the history
…ds (Accession, Value, RefUri)
  • Loading branch information
kMutagene committed Aug 16, 2023
1 parent c6e2072 commit 2293e3f
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 151 deletions.
6 changes: 3 additions & 3 deletions src/ARCTokenization/Address.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ open ControlledVocabulary

module Address =

let column : CvTerm = "http://purl.obolibrary.org/obo/NCIT_C43379","Column","NCIT"
let column = CvTerm.create(accession = "http://purl.obolibrary.org/obo/NCIT_C43379", value = "Column", ref = "NCIT")

let row : CvTerm = "http://purl.obolibrary.org/obo/NCIT_C43378","Row","NCIT"
let row = CvTerm.create(accession = "http://purl.obolibrary.org/obo/NCIT_C43378", value = "Row", ref = "NCIT")

let worksheet : CvTerm = "http://purl.obolibrary.org/obo/NCIT_C73541","Worksheet","NCIT"
let worksheet = CvTerm.create(accession = "http://purl.obolibrary.org/obo/NCIT_C73541", value = "Worksheet", ref = "NCIT")

//"isa_investigation!A1"

Expand Down
10 changes: 5 additions & 5 deletions src/ARCTokenization/AnnotationTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ module AnnotationTable =
let cvPars =
List.map4 (
fun (vl : FsCell) unt tan tsr ->
let valTerm = CvUnit(tan.Value, vl.Value, tsr.Value)
let valTerm = CvUnit.create(accession = tan.Value, value = vl.Value, ref = tsr.Value)
CvParam(d.Value, a.Value, c.Value, WithCvUnitAccession (unt.Value, valTerm))
) dataCellsVal dataCellsUnt dataCellsTan dataCellsTsr
yield! cvPars
Expand All @@ -166,7 +166,7 @@ module AnnotationTable =
(dataCellsVal, dataCellsTsr, dataCellsTan)
|||> List.map3 (
fun vl tsr tan ->
let valTerm = CvTerm(tan.Value, vl.Value, tsr.Value)
let valTerm = CvTerm.create(accession = tan.Value, value = vl.Value, ref = tsr.Value)
CvParam(c.Value, a.Value, b.Value, CvValue valTerm)
)
yield! cvPars
Expand All @@ -182,7 +182,7 @@ module AnnotationTable =
(dataCellsVal, dataCellsTsr)
||> List.map2 (
fun vl tsr ->
let valTerm = CvTerm("(n/a)", vl.Value, tsr.Value)
let valTerm = CvTerm.create (accession = "(n/a)", value = vl.Value, ref = tsr.Value)
CvParam("n/a", a.Value, b.Value, CvValue valTerm)
)
yield! cvPars
Expand All @@ -195,7 +195,7 @@ module AnnotationTable =
(dataCellsTsr, dataCellsTan)
||> List.map2 (
fun tsr tan ->
let valTerm = CvTerm(tan.Value, "n/a", tsr.Value)
let valTerm = CvTerm.create(accession = tan.Value, value = "n/a", ref = tsr.Value)
CvParam(b.Value, "(n/a)", a.Value, CvValue valTerm)
)
yield! cvPars
Expand Down Expand Up @@ -291,7 +291,7 @@ module AnnotationTable =
let getNodeType (cvPar : #IParamBase) =
//let castedCvPar = cvPar :?> CvParam<string> // debatable approach
//let v = Param.getCvName castedCvPar
let v = CvBase.getCvName cvPar
let v = CvBase.getCvValue cvPar
match v with
| "Source Name" -> Source
| "Sample Name"
Expand Down
2 changes: 1 addition & 1 deletion src/ARCTokenization/MetadataSheet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module MetadataSheet =

let (|Term|_|) (terms : CvTerm list) (key : string) : CvTerm Option =
terms
|> List.tryFind (fun (term) -> CvTerm.getName term = key)
|> List.tryFind (fun (term) -> term.Value = key)

let (|UnMatchable|) (key : string) : string =
key
Expand Down
8 changes: 4 additions & 4 deletions src/ARCTokenization/Terms.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module InvestigationMetadata =

let cvTerms =
ontology.Terms
|> List.map (fun t -> CvTerm(t.Id,t.Name,"INVMSO"))
|> List.map (fun t -> CvTerm.create(accession = t.Id, value = t.Name, ref = "INVMSO"))

module StudyMetadata =

Expand All @@ -34,7 +34,7 @@ module StudyMetadata =

let cvTerms =
ontology.Terms
|> List.map (fun t -> CvTerm(t.Id,t.Name,"STDMSO"))
|> List.map (fun t -> CvTerm.create(accession = t.Id, value = t.Name, ref ="STDMSO"))

module AssayMetadata =

Expand All @@ -44,8 +44,8 @@ module AssayMetadata =

let cvTerms =
ontology.Terms
|> List.map (fun t -> CvTerm(t.Id,t.Name,"ASSMSO"))
|> List.map (fun t -> CvTerm.create(accession = t.Id, value = t.Name, ref ="ASSMSO"))

module StructuralTerms =

let metadataSectionKey = CvTerm("AGMO:00000001","Metadata Section Key","AGMO")
let metadataSectionKey = CvTerm.create(accession = "AGMO:00000001", value = "Metadata Section Key", ref = "AGMO")
12 changes: 6 additions & 6 deletions src/ControlledVocabulary/CvAttributeCollection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CvAttributeCollection(attributes : IDictionary<string,IParam>) =
new(attributes) =
let dict =
attributes
|> Seq.map (fun cvp -> (cvp :> ICvBase).Name, cvp)
|> Seq.map (fun cvp -> (cvp :> ICvBase).Value, cvp)
|> Dictionary.ofSeq
CvAttributeCollection(dict)

Expand All @@ -22,12 +22,12 @@ type CvAttributeCollection(attributes : IDictionary<string,IParam>) =

/// Add an IParam as an attribute. Fails, if an attribute with the same key already exists
member this.AddAttribute (param : IParam) =
Dictionary.addInPlace (param |> CvBase.getCvName) param this
Dictionary.addInPlace (param |> CvBase.getCvValue) param this
|> ignore

/// Add an IParam as an attribute. Does not fail, if an attribute with the same key already exists
member this.TryAddAttribute (param : IParam) =
let key = param |> CvBase.getCvName
let key = param |> CvBase.getCvValue
if this.ContainsAttribute key then false
else
this.AddAttribute param
Expand All @@ -39,7 +39,7 @@ type CvAttributeCollection(attributes : IDictionary<string,IParam>) =

/// Retrieves an IParam attribute by its term, if it exists, else returns None
member this.TryGetAttribute (term : CvTerm) =
Dictionary.tryFind (CvTerm.getName term) this
Dictionary.tryFind term.Value this
|> Option.bind (fun param ->
if CvBase.equalsTerm term param then
Some param
Expand All @@ -51,15 +51,15 @@ type CvAttributeCollection(attributes : IDictionary<string,IParam>) =

/// Retrieves an IParam attribute by its term, if it exists, else fails
member this.GetAttribute (term : CvTerm) =
Dictionary.item (CvTerm.getName term) this
Dictionary.item term.Value this

/// Returns true, if an attribute with the given name exists in the collection
member this.ContainsAttribute (name : string) =
Dictionary.containsKey name this

/// Returns true, if an attribute with the given term exists in the collection
member this.ContainsAttribute (term : CvTerm) =
match Dictionary.tryFind (CvTerm.getName term) this with
match Dictionary.tryFind term.Value this with
| Some param when CvBase.equalsTerm term param -> true
| _ -> false

Expand Down
26 changes: 13 additions & 13 deletions src/ControlledVocabulary/CvContainer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ module internal Dictionary =
/// Represents a collection of structured properties, annotated with a controlled vocabulary term.
type CvContainer (
cvAccession : string,
cvName : string,
cvRefUri : string,
attributes : IDictionary<string,IParam>,
properties : IDictionary<string,seq<ICvBase>>
cvValue : string,
cvRef : string,
attributes : IDictionary<string,IParam>,
properties : IDictionary<string,seq<ICvBase>>
) =

inherit CvAttributeCollection(attributes)

interface ICvBase with
member this.ID = cvAccession
member this.Name = cvName
member this.RefUri = cvRefUri
member this.Accession = cvAccession
member this.Value = cvValue
member this.RefUri = cvRef
member this.HasAttributes
with get() = this.Attributes |> Seq.isEmpty |> not

Expand All @@ -37,8 +37,8 @@ type CvContainer (
CvContainer(cvAccession, cvName, cvRefUri, Seq.empty)


new ((id,name,ref) : CvTerm, attributes : IDictionary<string,IParam>) =
CvContainer(id,name,ref, attributes, Dictionary<string, ICvBase seq>())
new (term : CvTerm, attributes : IDictionary<string,IParam>) =
CvContainer(term.Accession, term.Value, term.RefUri, attributes, Dictionary<string, ICvBase seq>())
new (term : CvTerm,attributes : seq<IParam>) =
let dict = CvAttributeCollection(attributes)
CvContainer(term, dict)
Expand Down Expand Up @@ -307,7 +307,7 @@ type CvContainer (
///
/// If a value with the same key already exist in the container, appends the new child
static member addSingle (value : ICvBase) (cvContainer : CvContainer) =
Dictionary.addOrAppendInPlace value.Name value cvContainer.Properties
Dictionary.addOrAppendInPlace value.Value value cvContainer.Properties


/// Sets children as a property of the CvContainer.
Expand All @@ -319,15 +319,15 @@ type CvContainer (
static member setMany (values : seq<ICvBase>) (cvContainer : CvContainer) =
let propertyName =
values
|> Seq.countBy (fun v -> v.Name)
|> Seq.countBy (fun v -> v.Value)
|> Seq.exactlyOne
|> fst
Dictionary.addOrUpdateInPlace propertyName values cvContainer.Properties
|> ignore

/// Sets a single child as a property of the CvContainer, accessible by its name.
static member setSingle (value : ICvBase) (cvContainer : CvContainer) =
Dictionary.addOrUpdateInPlace value.Name (Seq.singleton value) cvContainer.Properties
Dictionary.addOrUpdateInPlace value.Value (Seq.singleton value) cvContainer.Properties

override this.ToString() =
$"CvContainer: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).ID}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tProperties: {this.Properties.Keys |> Seq.toList}"
$"CvContainer: {(this :> ICvBase).Value}\n\tID: {(this :> ICvBase).Accession}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tProperties: {this.Properties.Keys |> Seq.toList}"
12 changes: 6 additions & 6 deletions src/ControlledVocabulary/CvObject.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
open System.Collections.Generic

/// Represents a generic object, annotated with a controlled vocabulary term
type CvObject<'T>(cvAccession : string, cvName : string, cvRefUri : string, object : 'T, attributes : Dictionary<string,IParam>) =
type CvObject<'T>(cvAccession : string, cvValue : string, cvRef : string, object : 'T, attributes : Dictionary<string,IParam>) =

inherit CvAttributeCollection(attributes)

interface ICvBase with
member this.ID = cvAccession
member this.Name = cvName
member this.RefUri = cvRefUri
member this.Accession = cvAccession
member this.Value = cvValue
member this.RefUri = cvRef
member this.HasAttributes
with get() = this.Attributes |> Seq.isEmpty |> not

new ((id,name,ref) : CvTerm,object : 'T, attributes) = CvObject (id,name,ref,object,attributes)
new (term: CvTerm, object : 'T, attributes) = CvObject (term.Accession, term.Value, term.RefUri, object, attributes)

member this.Object = object

override this.ToString() =
$"Name: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).ID}\n\tRefUri: {(this :> ICvBase).RefUri}"
$"Name: {(this :> ICvBase).Value}\n\tID: {(this :> ICvBase).Accession}\n\tRefUri: {(this :> ICvBase).RefUri}"
26 changes: 13 additions & 13 deletions src/ControlledVocabulary/CvParam.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ open FSharpAux
///
/// Attributes can be used to further describe the CvParam
[<StructuredFormatDisplay("{DisplayText}")>]
type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValue : ParamValue, attributes : IDictionary<string,IParam>) =
type CvParam(cvAccession : string, cvValue : string, cvRef : string, paramValue : ParamValue, attributes : IDictionary<string,IParam>) =

inherit CvAttributeCollection(attributes)

interface IParam with
member this.ID = cvAccession
member this.Name = cvName
member this.RefUri = cvRefUri
member this.Value = paramValue
member this.WithValue(v : ParamValue) = CvParam(cvAccession,cvName,cvRefUri,v,attributes)
member this.Accession = cvAccession
member this.Value = cvValue
member this.RefUri = cvRef
member this.Value = paramValue
member this.WithValue(v : ParamValue) = CvParam(cvAccession,cvValue,cvRef,v,attributes)
member this.HasAttributes
with get() = this.Attributes |> Seq.isEmpty |> not

Expand All @@ -31,8 +31,8 @@ type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValu
new (id,name,ref,v : IConvertible) =
CvParam (id,name,ref,ParamValue.Value v)

new ((id,name,ref) : CvTerm,pv,attributes : seq<IParam>) =
CvParam (id,name,ref,pv,attributes)
new (term : CvTerm, pv, attributes : seq<IParam>) =
CvParam (term.Accession, term.Value, term.RefUri, pv, attributes)
new (cvTerm,pv : ParamValue) =
CvParam (cvTerm,pv,Seq.empty)
new (cvTerm,v : IConvertible) =
Expand Down Expand Up @@ -87,11 +87,11 @@ type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValu
static member tryMapValue (f : ParamValue -> ParamValue option) (param : CvParam) =
Param.tryMapValue f param

static member tryAddName (name : string) (param : CvParam) =
Param.tryAddName name param
static member tryAddValue (value : string) (param : CvParam) =
Param.tryAddValue value param

static member tryAddAnnotationID (id : string) (param : CvParam) =
Param.tryAddAnnotationID id param
static member tryAddAccession (id : string) (param : CvParam) =
Param.tryAddAccession id param

static member tryAddReference (ref : string) (param : CvParam) =
Param.tryAddReference ref param
Expand All @@ -104,7 +104,7 @@ type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValu


override this.ToString() =
$"CvParam: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).ID}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tValue: {(this :> IParamBase).Value}\n\tAttributes: {this.Keys |> Seq.toList}"
$"CvParam: {(this :> ICvBase).Value}\n\tID: {(this :> ICvBase).Accession}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tValue: {(this :> IParamBase).Value}\n\tAttributes: {this.Keys |> Seq.toList}"

member this.DisplayText =
this.ToString()
48 changes: 21 additions & 27 deletions src/ControlledVocabulary/CvTerm.fs
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
namespace ControlledVocabulary

/// Represents a term from a controlled vocabulary (Cv)
/// in the form of: id|accession * name|value * refUri
/// in the form of: id|accession ; name|value ; refUri
// ?Maybe [<Struct>]
//[<Struct>]
type CvTerm = string * string * string
type CvTerm = {
Accession: string
Value: string
RefUri: string
} with
static member create(
accession: string,
value: string,
ref : string
) =
{Accession = accession; Value = value; RefUri = ref}

module CvTerm =

/// gets the name of the CvTerm
let getName (cvTerm : CvTerm) =
match cvTerm with
| id, name, refUri -> name

/// gets the name of the CvTerm
let getId (cvTerm : CvTerm) =
match cvTerm with
| id, name, refUri -> id
static member create(
value: string
) =
CvTerm.create(
value = value,
accession = "",
ref = ""
)

/// gets the source reference of the CvTerm
let getRef (cvTerm : CvTerm) =
match cvTerm with
| id, name, refUri -> refUri

/// creates a CvTerm from name
let fromName (name : string) : CvTerm=
"", name, ""

/// creates a CvTerm from a term triplet
let create id name ref : CvTerm =
id,name,ref

/// Represents a unit term from the unit ontology
/// in the form of: id|accession * name * refUri
// ?Maybe [<Struct>]
type CvUnit = string * string * string
type CvUnit = CvTerm
Loading

0 comments on commit 2293e3f

Please sign in to comment.