@@ -7,188 +7,39 @@ open ARCtrl
77open ARCtrl.Process
88open System.IO
99
10- module Value =
11-
12- let encoder ( options : ConverterOptions ) ( value : Value ) =
13- match value with
14- | Value.Float f ->
15- Encode.float f
16- | Value.Int i ->
17- Encode.int i
18- | Value.Name s ->
19- Encode.string s
20- | Value.Ontology s ->
21- OntologyAnnotation.encoder options s
22-
23- let decoder ( options : ConverterOptions ) : Decoder < Value > =
24- {
25-
26- new Decoder< Value> with
27- member this.Decode ( s , json ) =
28- match Decode.int.Decode( s, json) with
29- | Ok i -> Ok ( Value.Int i)
30- | Error _ ->
31- match Decode.float.Decode( s, json) with
32- | Ok f -> Ok ( Value.Float f)
33- | Error _ ->
34- match OntologyAnnotation.decoder( options). Decode( s, json) with
35- | Ok f -> Ok ( Value.Ontology f)
36- | Error _ ->
37- match Decode.string.Decode( s, json) with
38- | Ok s -> Ok ( Value.Name s)
39- | Error e -> Error e}
40-
41-
42-
43- let fromJsonString ( s : string ) =
44- Decode.fromJsonString ( decoder ( ConverterOptions())) s
45-
46- let fromJsonldString ( s : string ) =
47- Decode.fromJsonString ( decoder ( ConverterOptions( IsJsonLD= true ))) s
48-
49- let toJsonString ( v : Value ) =
50- encoder ( ConverterOptions()) v
51- |> Encode.toJsonString 2
52-
53- //let fromFile (path : string) =
54- // File.ReadAllText path
55- // |> fromString
56-
57- //let toFile (path : string) (v:Value) =
58- // File.WriteAllText(path,toString v)
5910
6011module Factor =
61-
62- let genID ( f : Factor ) : string =
63- match f.Name with
64- | Some n -> " #Factor_" + n.Replace( " " , " _" )
65- | None -> " #EmptyFactor"
66-
67- let encoder ( options : ConverterOptions ) ( oa : Factor ) =
68- [
69- if options.SetID then
70- " @id" , Encode.string ( oa |> genID)
71- if options.IsJsonLD then
72- " @type" , ( Encode.list [ Encode.string " Factor" ])
73- Encode.tryInclude " factorName" Encode.string ( oa.Name)
74- if options.IsJsonLD then
75- if oa.FactorType.IsSome then
76- Encode.tryInclude " annotationValue" Encode.string ( oa.Name)
77- Encode.tryInclude " termSource" Encode.string ( oa.FactorType.Value.TermSourceREF)
78- Encode.tryInclude " termAccession" Encode.string ( oa.FactorType.Value.TermAccessionNumber)
79- else
80- Encode.tryInclude " factorType" ( OntologyAnnotation.encoder options) ( oa.FactorType)
81- Encode.tryIncludeSeq " comments" ( Comment.encoder options) ( oa.Comments |> Option.defaultValue ( ResizeArray()))
82- if options.IsJsonLD then
83- " @context" , ROCrateContext.Factor.context_ jsonvalue
84- ]
85- |> Encode.choose
86- |> Encode.object
87-
88- let decoder ( options : ConverterOptions ) : Decoder < Factor > =
89- Decode.object ( fun get ->
90- {
91- Name = get.Optional.Field " factorName" Decode.string
92- FactorType = get.Optional.Field " factorType" ( OntologyAnnotation.decoder options)
93- Comments = get.Optional.Field " comments" ( Decode.array ( Comment.decoder options))
94- }
95- )
96-
97- let fromJsonString ( s : string ) =
98- GDecode.fromJsonString ( decoder ( ConverterOptions())) s
99- let fromJsonldString ( s : string ) =
100- GDecode.fromJsonString ( decoder ( ConverterOptions( IsJsonLD= true ))) s
10112
102- let toJsonString ( f : Factor ) =
103- encoder ( ConverterOptions()) f
104- |> Encode.toJsonString 2
13+ module ISAJson =
14+
15+ let encoder ( value : Factor ) =
16+ [
17+ Encode.tryInclude " factorName" Encode.string value.Name
18+ Encode.tryInclude " factorType" OntologyAnnotation.ISAJson.encoder value.FactorType
19+ Encode.tryIncludeSeq " comments" Comment.ISAJson.encoder ( value.Comments |> Option.defaultValue ( ResizeArray()))
20+ ]
21+ |> Encode.choose
22+ |> Encode.object
23+
24+ let decoder : Decoder < Factor > =
25+ Decode.object ( fun get ->
26+ {
27+ Name = get.Optional.Field " factorName" Decode.string
28+ FactorType = get.Optional.Field " factorType" ( OntologyAnnotation.ISAJson.decoder)
29+ Comments = get.Optional.Field " comments" ( Decode.resizeArray ( Comment.ISAJson.decoder))
30+ }
31+ )
32+
33+
34+ [<AutoOpen>]
35+ module FactorExtensions =
10536
106- /// exports in json-ld format
107- let toJsonldString ( f : Factor ) =
108- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) f
109- |> Encode.toJsonString 2
110-
111- let toJsonldStringWithContext ( a : Factor ) =
112- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) a
113- |> Encode.toJsonString 2
114-
115- //let fromFile (path : string) =
116- // File.ReadAllText path
117- // |> fromString
118-
119- //let toFile (path : string) (f:Factor) =
120- // File.WriteAllText(path,toString f)
121-
122-
123- module FactorValue =
124-
125- let genID ( fv : FactorValue ) : string =
126- match fv.ID with
127- | Some id -> URI.toString id
128- | None -> " #EmptyFactorValue"
129-
130- let encoder ( options : ConverterOptions ) ( oa : FactorValue ) =
131- [
132- if options.SetID then
133- " @id" , Encode.string ( oa |> genID)
134- else
135- Encode.tryInclude " @id" Encode.string ( oa.ID)
136- if options.IsJsonLD then
137- " @type" , ( Encode.list [ Encode.string " FactorValue" ])
138- " additionalType" , Encode.string " FactorValue"
139- if options.IsJsonLD then
140- if oa.Category.IsSome then
141- Encode.tryInclude " categoryName" Encode.string ( oa.Category.Value.Name)
142- if oa.Category.IsSome && oa.Category.Value.FactorType.IsSome then
143- Encode.tryInclude " category" Encode.string ( oa.Category.Value.FactorType.Value.Name)
144- if oa.Category.IsSome && oa.Category.Value.FactorType.IsSome then
145- Encode.tryInclude " categoryCode" Encode.string ( oa.Category.Value.FactorType.Value.TermAccessionNumber)
146- if oa.Value.IsSome then " value" , Encode.string ( oa.ValueText)
147- if oa.Value.IsSome && oa.Value.Value.IsAnOntology then
148- Encode.tryInclude " valueCode" Encode.string ( oa.Value.Value.AsOntology()) .TermAccessionNumber
149- if oa.Unit.IsSome then Encode.tryInclude " unit" Encode.string ( oa.Unit.Value.Name)
150- if oa.Unit.IsSome then Encode.tryInclude " unitCode" Encode.string ( oa.Unit.Value.TermAccessionNumber)
151- else
152- Encode.tryInclude " category" ( Factor.encoder options) ( oa.Category)
153- Encode.tryInclude " value" ( Value.encoder options) ( oa.Value)
154- Encode.tryInclude " unit" ( OntologyAnnotation.encoder options) ( oa.Unit)
155- if options.IsJsonLD then
156- " @context" , ROCrateContext.FactorValue.context_ jsonvalue
157- ]
158- |> Encode.choose
159- |> Encode.object
160-
161- let decoder ( options : ConverterOptions ) : Decoder < FactorValue > =
162- Decode.object ( fun get ->
163- {
164- ID = get.Optional.Field " @id" GDecode.uri
165- Category = get.Optional.Field " category" ( Factor.decoder options)
166- Value = get.Optional.Field " value" ( Value.decoder options)
167- Unit = get.Optional.Field " unit" ( OntologyAnnotation.decoder options)
168- }
169- )
170-
171- let fromJsonString ( s : string ) =
172- GDecode.fromJsonString ( decoder ( ConverterOptions())) s
173- let fromJsonldString ( s : string ) =
174- GDecode.fromJsonString ( decoder ( ConverterOptions( IsJsonLD= true ))) s
175-
176- let toJsonString ( f : FactorValue ) =
177- encoder ( ConverterOptions()) f
178- |> Encode.toJsonString 2
179-
180- /// exports in json-ld format
181- let toJsonldString ( f : FactorValue ) =
182- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) f
183- |> Encode.toJsonString 2
184-
185- let toJsonldStringWithContext ( a : FactorValue ) =
186- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) a
187- |> Encode.toJsonString 2
37+ type Factor with
18838
189- //let fromFile (path : string) =
190- // File.ReadAllText path
191- // |> fromString
39+ static member fromISAJsonString ( s : string ) =
40+ Decode.fromJsonString Factor.ISAJson.decoder s
19241
193- //let toFile (path : string) (f:FactorValue) =
194- // File.WriteAllText(path,toString f)
42+ static member toISAJsonString (? spaces ) =
43+ fun ( f : Factor ) ->
44+ Factor.ISAJson.encoder f
45+ |> Encode.toJsonString ( Option.defaultValue 2 spaces)
0 commit comments