-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCompositeCell.fs
208 lines (178 loc) · 8.31 KB
/
CompositeCell.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
namespace ARCtrl
open Fable.Core
[<AttachMembers>]
[<RequireQualifiedAccess>]
type CompositeCell =
/// ISA-TAB term columns as ontology annotation.
///
/// https://isa-specs.readthedocs.io/en/latest/isatab.html#ontology-annotations
| Term of OntologyAnnotation
/// Single columns like Input, Output, ProtocolREF, .. .
| FreeText of string
/// ISA-TAB unit columns, consisting of value and unit as ontology annotation.
///
/// https://isa-specs.readthedocs.io/en/latest/isatab.html#unit
| Unitized of string*OntologyAnnotation
| Data of Data
member this.isUnitized = match this with | Unitized _ -> true | _ -> false
member this.isTerm = match this with | Term _ -> true | _ -> false
member this.isFreeText = match this with | FreeText _ -> true | _ -> false
member this.isData = match this with | Data _ -> true | _ -> false
/// <summary>
/// This returns the default empty cell from an existing CompositeCell.
/// </summary>
member this.GetEmptyCell() =
match this with
| CompositeCell.Term _ -> CompositeCell.emptyTerm
| CompositeCell.Unitized _ -> CompositeCell.emptyUnitized
| CompositeCell.FreeText _ -> CompositeCell.emptyFreeText
| CompositeCell.Data _ -> CompositeCell.emptyData
/// <summary>
/// This function returns an array of all values as string
///
/// ```fsharp
/// match this with
/// | FreeText s -> [|s|]
/// | Term oa -> [| oa.NameText; defaultArg oa.TermSourceREF ""; defaultArg oa.TermAccessionNumber ""|]
/// | Unitized (v,oa) -> [| v; oa.NameText; defaultArg oa.TermSourceREF ""; defaultArg oa.TermAccessionNumber ""|]
/// ```
/// </summary>
member this.GetContent() =
match this with
| FreeText s -> [|s|]
| Term oa -> [| oa.NameText; defaultArg oa.TermSourceREF ""; defaultArg oa.TermAccessionNumber ""|]
| Unitized (v,oa) -> [| v; oa.NameText; defaultArg oa.TermSourceREF ""; defaultArg oa.TermAccessionNumber ""|]
| Data d -> [| defaultArg d.Name ""; defaultArg d.Format ""; defaultArg d.SelectorFormat ""|]
/// FreeText string will be converted to unit term name.
///
/// Term will be converted to unit term.
member this.ToUnitizedCell() =
match this with
| Unitized _ -> this
| FreeText text -> CompositeCell.Unitized ("", OntologyAnnotation.create(text))
| Term term -> CompositeCell.Unitized ("", term)
| Data d -> CompositeCell.Unitized ("", OntologyAnnotation.create(d.NameText))
/// FreeText string will be converted to term name.
///
/// Unit term will be converted to term and unit value is dropped.
member this.ToTermCell() =
match this with
| Term _ -> this
| Unitized (_,unit) -> CompositeCell.Term unit
| FreeText text -> CompositeCell.Term(OntologyAnnotation.create(text))
| Data d -> CompositeCell.Term(OntologyAnnotation(d.NameText))
/// Will always keep `OntologyAnnotation.NameText` from Term or Unit.
member this.ToFreeTextCell() =
match this with
| FreeText _ -> this
| Term term -> FreeText(term.NameText)
| Unitized (_,unit) -> FreeText(unit.NameText)
| Data d -> FreeText (d.NameText)
member this.ToDataCell() =
match this with
| Unitized (_, unit) -> CompositeCell.createDataFromString unit.NameText
| FreeText txt -> CompositeCell.createDataFromString txt
| Term term -> CompositeCell.createDataFromString term.NameText
| Data _ -> this
// Suggest this syntax for easy "of-something" access
member this.AsUnitized =
match this with
| Unitized (v,u) -> v,u
| _ -> failwith "Not a Unitized cell."
member this.AsTerm =
match this with
| Term c -> c
| _ -> failwith "Not a Term Cell."
member this.AsFreeText =
match this with
| FreeText c -> c
| _ -> failwith "Not a FreeText Cell."
member this.AsData =
match this with
| Data d -> d
| _ -> failwith "Not a Data Cell."
// TODO: i would really love to have an overload here accepting string input
static member createTerm (oa:OntologyAnnotation) = Term oa
static member createTermFromString (?name: string, ?tsr: string, ?tan: string) =
Term <| OntologyAnnotation.create(?name = name, ?tsr = tsr, ?tan = tan)
static member createUnitized (value: string, ?oa:OntologyAnnotation) = Unitized (value, Option.defaultValue (OntologyAnnotation()) oa)
static member createUnitizedFromString (value: string, ?name: string, ?tsr: string, ?tan: string) =
Unitized <| (value, OntologyAnnotation.create(?name = name, ?tsr = tsr, ?tan = tan))
static member createFreeText (value: string) = FreeText value
static member createData (d:Data) = Data d
static member createDataFromString (value : string, ?format : string, ?selectorFormat : string) =
Data(Data.create(Name = value, ?Format = format, ?SelectorFormat = selectorFormat))
static member emptyTerm = Term (OntologyAnnotation())
static member emptyFreeText = FreeText ""
static member emptyUnitized = Unitized ("", OntologyAnnotation())
static member emptyData = Data(Data.create())
/// <summary>
/// Updates current CompositeCell with information from OntologyAnnotation.
///
/// For `Term`, OntologyAnnotation (oa) is fully set. For `Unitized`, oa is set as unit while value is untouched.
/// For `FreeText` oa.NameText is set.
/// </summary>
/// <param name="oa"></param>
member this.UpdateWithOA (oa:OntologyAnnotation) =
match this with
| CompositeCell.Term _ -> CompositeCell.createTerm oa
| CompositeCell.Unitized (v,_) -> CompositeCell.createUnitized (v,oa)
| CompositeCell.FreeText _ -> CompositeCell.createFreeText oa.NameText
| CompositeCell.Data d ->
d.Name <- Some oa.NameText
CompositeCell.Data d
/// <summary>
/// Updates current CompositeCell with information from OntologyAnnotation.
///
/// For `Term`, OntologyAnnotation (oa) is fully set. For `Unitized`, oa is set as unit while value is untouched.
/// For `FreeText` oa.NameText is set.
/// </summary>
/// <param name="oa"></param>
/// <param name="cell"></param>
static member updateWithOA (oa:OntologyAnnotation) (cell: CompositeCell) =
cell.UpdateWithOA oa
override this.ToString() =
match this with
| Term oa -> $"{oa.NameText}"
| FreeText s -> s
| Unitized (v,oa) -> $"{v} {oa.NameText}"
| Data d -> $"{d.NameText}"
member this.Copy() =
match this with
| Term oa -> Term (oa.Copy())
| FreeText s -> FreeText s
| Unitized (v,oa) -> Unitized (v, oa.Copy())
| Data d -> Data (d.Copy())
member this.ValidateAgainstHeader(header : CompositeHeader, ?raiseException) =
let raiseExeption = Option.defaultValue false raiseException
let cell = this
match header, cell with
// no cell values will be handled later and is no error case
| isData when header.IsDataColumn && (cell.isData || cell.isFreeText) ->
true
| isData when header.IsDataColumn ->
if raiseExeption then
let msg = $"Invalid combination of header `{header}` and cell `{cell}`, Data header should have either Data or Freetext cells"
failwith msg
false
| isTerm when header.IsTermColumn && (cell.isTerm || cell.isUnitized) ->
true
| isNotTerm when (not header.IsTermColumn) && cell.isFreeText ->
true
| h, c ->
if raiseExeption then
let msg = $"Invalid combination of header `{h}` and cell `{cell}`"
failwith msg
// Maybe still return `msg` somehow if `raiseExeption` is false?
false
#if FABLE_COMPILER
//[<CompiledName("Term")>]
static member term (oa:OntologyAnnotation) = CompositeCell.Term(oa)
//[<CompiledName("FreeText")>]
static member freeText (s:string) = CompositeCell.FreeText(s)
//[<CompiledName("Unitized")>]
static member unitized (v:string, oa:OntologyAnnotation) = CompositeCell.Unitized(v, oa)
//[<CompiledName("Data")>]
static member data (d:Data) = CompositeCell.Data(d)
#else
#endif