Skip to content

Commit 2cb9db8

Browse files
committed
rework ARCtrl.Core types to improve transpiled javascript
1 parent b282dc3 commit 2cb9db8

14 files changed

+371
-168
lines changed

src/Core/ARCtrl.Core.fsproj

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Compile Include="Helper\HashCodes.fs" />
1414
<Compile Include="Helper\Update.fs" />
1515
<Compile Include="Helper\Printer.fs" />
16+
<Compile Include="Helper\SemVer.fs" />
1617
<Compile Include="URI.fs" />
1718
<Compile Include="Comment.fs" />
1819
<Compile Include="CommentList.fs" />
@@ -52,6 +53,7 @@
5253
<Compile Include="Table\ArcTable.fs" />
5354
<Compile Include="Table\ArcTables.fs" />
5455
<Compile Include="ArcTypes.fs" />
56+
<Compile Include="Template.fs" />
5557
<Compile Include="Conversion.fs" />
5658
<Compile Include="IdentifierSetters.fs" />
5759
</ItemGroup>

src/Core/ArcTypes.fs

+97-84
Large diffs are not rendered by default.

src/Core/CommentList.fs

+13-16
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,50 @@
44
open ARCtrl.Helper
55

66
/// If a comment with the given key exists in the [], return its value, else return None
7-
let tryItem (key: string) (comments : Comment []) =
7+
let tryItem (key: string) (comments : ResizeArray<Comment>) =
88
comments
9-
|> Array.tryPick (fun c ->
9+
|> Seq.tryPick (fun c ->
1010
match c.Name with
1111
| Some n when n = key -> c.Value
1212
| _ -> None
1313
)
1414

1515
/// Returns true, if the key exists in the []
16-
let containsKey (key: string) (comments : Comment []) =
16+
let containsKey (key: string) (comments : ResizeArray<Comment>) =
1717
comments
18-
|> Array.exists (fun c ->
18+
|> Seq.exists (fun c ->
1919
match c.Name with
2020
| Some n when n = key -> true
2121
| _ -> false
2222
)
2323

2424
/// If a comment with the given key exists in the [], return its value
25-
let item (key: string) (comments : Comment []) =
25+
let item (key: string) (comments : ResizeArray<Comment>) =
2626
(tryItem key comments).Value
2727

2828
/// Create a map of comment keys to comment values
29-
let toMap (comments : Comment []) =
29+
let toMap (comments : ResizeArray<Comment>) =
3030
comments
31-
|> Array.choose (fun c ->
31+
|> ResizeArray.choose (fun c ->
3232
match c.Name with
3333
| Some n -> Some (n,c.Value)
3434
| _ -> None
3535
)
36-
|> Map.ofArray
36+
|> Map.ofSeq
3737

38-
/// Adds the given comment to the comment []
39-
let add (comment : Comment) (comments : Comment []) =
40-
Array.append comments [|comment|]
4138

4239
/// Add the given comment to the comment [] if it doesnt exist, else replace it
43-
let set (comment : Comment) (comments : Comment []) =
40+
let set (comment : Comment) (comments : ResizeArray<Comment>) =
4441
if containsKey comment.Name.Value comments then
4542
comments
46-
|> Array.map (fun c -> if c.Name = comment.Name then comment else c)
43+
|> ResizeArray.map (fun c -> if c.Name = comment.Name then comment else c)
4744
else
48-
Array.append comments [|comment|]
45+
ResizeArray.appendSingleton comment comments
4946

5047
/// Returns a new comment [] where comments with the given key are filtered out
51-
let dropByKey (key: string) (comments : Comment []) =
48+
let dropByKey (key: string) (comments : ResizeArray<Comment>) =
5249
comments
53-
|> Array.filter (fun c ->
50+
|> ResizeArray.filter (fun c ->
5451
match c.Name with
5552
| Some n when n = key -> false
5653
| _ -> true

src/Core/Conversion.fs

+20-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Person =
1010
let orcidKey = "ORCID"
1111

1212
let setOrcidFromComments (person : Person) =
13+
let person = person.Copy()
1314
let isOrcidComment (c : Comment) =
1415
c.Name.IsSome && (c.Name.Value.ToUpper().EndsWith(orcidKey))
1516
let orcid,comments =
@@ -23,13 +24,16 @@ module Person =
2324
orcid, comments
2425
person.ORCID <- orcid
2526
person.Comments <- comments
27+
person
2628

2729
let setCommentFromORCID (person : Person) =
30+
let person = person.Copy()
2831
match person.ORCID with
2932
| Some orcid ->
30-
let comment = Comment.create (Name = orcidKey, Value = orcid)
33+
let comment = Comment.create (name = orcidKey, value = orcid)
3134
person.Comments.Add comment
3235
| None -> ()
36+
person
3337

3438
/// Functions for transforming base level ARC Table and ISA Json Objects
3539
module JsonTypes =
@@ -170,8 +174,8 @@ module JsonTypes =
170174
match name with
171175
| Regex.ActivePatterns.Regex pattern r ->
172176
let oa = (r.Groups.Item "ontology").Value |> OntologyAnnotation.fromTermAnnotation
173-
let v = (r.Groups.Item "value").Value |> Value.fromString
174-
{oa with Name = (Some v.Text)}
177+
let v = (r.Groups.Item "value").Value
178+
OntologyAnnotation.create(name = v, ?tan = oa.TermAccessionNumber, ?tsr = oa.TermSourceREF)
175179
| _ ->
176180
OntologyAnnotation.create(name = name)
177181

@@ -185,7 +189,9 @@ module ProcessParsing =
185189
let tryComponentGetter (generalI : int) (valueI : int) (valueHeader : CompositeHeader) =
186190
match valueHeader with
187191
| CompositeHeader.Component oa ->
188-
let cat = CompositeHeader.Component (oa.SetColumnIndex valueI)
192+
let newOA = oa.Copy()
193+
newOA.SetColumnIndex valueI
194+
let cat = CompositeHeader.Component newOA
189195
fun (matrix : System.Collections.Generic.Dictionary<(int * int),CompositeCell>) i ->
190196
JsonTypes.composeComponent cat matrix.[generalI,i]
191197
|> Some
@@ -195,7 +201,9 @@ module ProcessParsing =
195201
let tryParameterGetter (generalI : int) (valueI : int) (valueHeader : CompositeHeader) =
196202
match valueHeader with
197203
| CompositeHeader.Parameter oa ->
198-
let cat = CompositeHeader.Parameter (oa.SetColumnIndex valueI)
204+
let cat =
205+
OntologyAnnotation.setColumnIndex valueI oa
206+
|> CompositeHeader.Parameter
199207
fun (matrix : System.Collections.Generic.Dictionary<(int * int),CompositeCell>) i ->
200208
JsonTypes.composeParameterValue cat matrix.[generalI,i]
201209
|> Some
@@ -204,7 +212,9 @@ module ProcessParsing =
204212
let tryFactorGetter (generalI : int) (valueI : int) (valueHeader : CompositeHeader) =
205213
match valueHeader with
206214
| CompositeHeader.Factor oa ->
207-
let cat = CompositeHeader.Factor (oa.SetColumnIndex valueI)
215+
let cat =
216+
OntologyAnnotation.setColumnIndex valueI oa
217+
|> CompositeHeader.Factor
208218
fun (matrix : System.Collections.Generic.Dictionary<(int * int),CompositeCell>) i ->
209219
JsonTypes.composeFactorValue cat matrix.[generalI,i]
210220
|> Some
@@ -213,7 +223,9 @@ module ProcessParsing =
213223
let tryCharacteristicGetter (generalI : int) (valueI : int) (valueHeader : CompositeHeader) =
214224
match valueHeader with
215225
| CompositeHeader.Characteristic oa ->
216-
let cat = CompositeHeader.Characteristic (oa.SetColumnIndex valueI)
226+
let cat =
227+
OntologyAnnotation.setColumnIndex valueI oa
228+
|> CompositeHeader.Characteristic
217229
fun (matrix : System.Collections.Generic.Dictionary<(int * int),CompositeCell>) i ->
218230
JsonTypes.composeCharacteristicValue cat matrix.[generalI,i]
219231
|> Some
@@ -628,7 +640,7 @@ type ArcTable with
628640
|> Seq.fold (fun (p : Protocol) h ->
629641
match h with
630642
| CompositeHeader.ProtocolType ->
631-
Protocol.setProtocolType p OntologyAnnotation.empty
643+
Protocol.setProtocolType p (OntologyAnnotation())
632644
| CompositeHeader.ProtocolVersion -> Protocol.setVersion p ""
633645
| CompositeHeader.ProtocolUri -> Protocol.setUri p ""
634646
| CompositeHeader.ProtocolDescription -> Protocol.setDescription p ""

src/Core/Helper/Collections.fs

+18-1
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,21 @@ module ResizeArray =
141141
b
142142

143143
let isEmpty (a : ResizeArray<_>) =
144-
a.Count = 0
144+
a.Count = 0
145+
146+
/// Immutable append
147+
let append (a : ResizeArray<_>) (b : ResizeArray<_>) =
148+
let c = ResizeArray<_>()
149+
for i in a do
150+
c.Add(i)
151+
for i in b do
152+
c.Add(i)
153+
c
154+
155+
/// append a single element
156+
let appendSingleton (b : 'T) (a : ResizeArray<_>) =
157+
let c = ResizeArray<_>()
158+
for i in a do
159+
c.Add(i)
160+
c.Add(b)
161+
c

src/Core/Helper/SemVer.fs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module ARCtrl.Helper.SemVer
2+
3+
open Regex.ActivePatterns
4+
5+
module SemVerAux =
6+
7+
let Pattern =
8+
@"^(?<major>\d+)" +
9+
@"(\.(?<minor>\d+))?" +
10+
@"(\.(?<patch>\d+))?" +
11+
@"(-(?<pre>[0-9A-Za-z-\.]+))?" +
12+
@"(\+(?<build>[0-9A-Za-z-\.]+))?$"
13+
14+
15+
type SemVer = {
16+
Major: int
17+
Minor: int
18+
Patch: int
19+
PreRelease: string option
20+
Metadata: string option
21+
} with
22+
static member make major minor patch pre meta = {
23+
Major = major
24+
Minor = minor
25+
Patch = patch
26+
PreRelease = pre
27+
Metadata = meta
28+
}
29+
30+
static member create(major: int, minor: int, patch: int, ?pre: string, ?meta: string) =
31+
{
32+
Major = major
33+
Minor = minor
34+
Patch = patch
35+
PreRelease = pre
36+
Metadata = meta
37+
}
38+
39+
static member tryOfString (str: string) =
40+
match str with
41+
| Regex (SemVerAux.Pattern) m ->
42+
let g = m.Groups
43+
let major = int g.["major"].Value
44+
let minor = int g.["minor"].Value
45+
let patch = int g.["patch"].Value
46+
let pre =
47+
match g.["pre"].Success with
48+
| true -> Some g.["pre"].Value
49+
| false -> None
50+
let meta =
51+
match g.["build"].Success with
52+
| true -> Some g.["build"].Value
53+
| false -> None
54+
Some <| SemVer.create(major, minor, patch, ?pre=pre, ?meta=meta)
55+
| _ -> None
56+
57+
member this.AsString() : string =
58+
let sb = System.Text.StringBuilder()
59+
sb.Append(sprintf "%i.%i.%i" this.Major this.Minor this.Patch) |> ignore
60+
if this.PreRelease.IsSome then
61+
sb.Append(sprintf "-%s" this.PreRelease.Value) |> ignore
62+
if this.Metadata.IsSome then
63+
sb.Append(sprintf "+%s" this.Metadata.Value) |> ignore
64+
sb.ToString()

src/Core/OntologyAnnotation.fs

+16-18
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ type OntologyAnnotation(?name,?tsr,?tan, ?comments) =
4141
static member create(?name,?tsr,?tan,?comments) : OntologyAnnotation =
4242
OntologyAnnotation.make name tsr tan (defaultArg comments <| ResizeArray())
4343

44-
static member empty = OntologyAnnotation.create()
45-
4644
member this.TANInfo =
4745
match this.TermAccessionNumber with
4846
| Some v ->
@@ -92,22 +90,22 @@ type OntologyAnnotation(?name,?tsr,?tan, ?comments) =
9290
///
9391
/// `asOntobeePurlUrl`: option to return term accession in Ontobee purl-url format (`http://purl.obolibrary.org/obo/MS_1000121`)
9492
/// </summary>
95-
// TODO: Not sure if still needed ~Kevin F. 2024.03.20
96-
//static member toString (oa : OntologyAnnotation, ?asOntobeePurlUrlIfShort: bool) =
97-
// let asOntobeePurlUrlIfShort = Option.defaultValue false asOntobeePurlUrlIfShort
98-
// {|
99-
// TermName = oa.Name |> Option.defaultValue ""
100-
// TermSourceREF = oa.TermSourceREF |> Option.defaultValue ""
101-
// TermAccessionNumber =
102-
// if asOntobeePurlUrlIfShort then
103-
// let url = oa.TermAccessionAndOntobeeUrlIfShort
104-
// if url = "" then
105-
// oa.TermAccessionNumber |> Option.defaultValue ""
106-
// else
107-
// url
108-
// else
109-
// oa.TermAccessionNumber |> Option.defaultValue ""
110-
// |}
93+
///// TODO: Not sure if still needed ~Kevin F. 2024.03.20
94+
static member toString (oa : OntologyAnnotation, ?asOntobeePurlUrlIfShort: bool) =
95+
let asOntobeePurlUrlIfShort = Option.defaultValue false asOntobeePurlUrlIfShort
96+
{|
97+
TermName = oa.Name |> Option.defaultValue ""
98+
TermSourceREF = oa.TermSourceREF |> Option.defaultValue ""
99+
TermAccessionNumber =
100+
if asOntobeePurlUrlIfShort then
101+
let url = oa.TermAccessionAndOntobeeUrlIfShort
102+
if url = "" then
103+
oa.TermAccessionNumber |> Option.defaultValue ""
104+
else
105+
url
106+
else
107+
oa.TermAccessionNumber |> Option.defaultValue ""
108+
|}
111109

112110
interface IISAPrintable with
113111
member this.Print() =

0 commit comments

Comments
 (0)