1
1
namespace ARCtrl.Json
2
2
3
3
open Thoth.Json .Core
4
-
5
4
open ARCtrl
6
5
open System.IO
7
6
@@ -19,156 +18,119 @@ module AnnotationValue =
19
18
Decode.map string Decode.float
20
19
Decode.string
21
20
]
22
-
23
- module OntologySourceReference =
24
-
25
- let genID ( o : OntologySourceReference ) =
26
- match o.File with
27
- | Some f -> f
28
- | None ->
29
- match o.Name with
30
- | Some n -> " #OntologySourceRef_" + n.Replace( " " , " _" )
31
- | None -> " #DummyOntologySourceRef"
32
-
33
- let encoder ( options : ConverterOptions ) ( osr : OntologySourceReference ) =
34
- [
35
- if options.SetID then
36
- " @id" , Encode.string ( osr |> genID)
37
- if options.IsJsonLD then
38
- " @type" , Encode.string " OntologySourceReference"
39
- Encode.tryInclude " description" Encode.string ( osr.Description)
40
- Encode.tryInclude " file" Encode.string ( osr.File)
41
- Encode.tryInclude " name" Encode.string ( osr.Name)
42
- Encode.tryInclude " version" Encode.string ( osr.Version)
43
- Encode.tryIncludeSeq " comments" ( Comment.encoder options) ( osr.Comments)
44
- if options.IsJsonLD then
45
- " @context" , ROCrateContext.OntologySourceReference.context_ jsonvalue
46
- ]
47
- |> Encode.choose
48
- |> Encode.object
49
-
50
- let decoder ( options : ConverterOptions ) : Decoder < OntologySourceReference > =
51
- Decode.object ( fun get ->
52
- OntologySourceReference(
53
- ?description = get.Optional.Field " description" Decode.uri,
54
- ?file = get.Optional.Field " file" Decode.string,
55
- ?name = get.Optional.Field " name" Decode.string,
56
- ?version = get.Optional.Field " version" Decode.string,
57
- ?comments = get.Optional.Field " comments" ( Decode.resizeArray ( Comment.decoder options))
58
- )
59
- )
60
-
61
- let fromJsonString ( s : string ) =
62
- Decode.fromJsonString ( decoder ( ConverterOptions())) s
63
-
64
- let fromJsonldString ( s : string ) =
65
- Decode.fromJsonString ( decoder ( ConverterOptions( IsJsonLD= true ))) s
66
-
67
- let toJsonString ( oa : OntologySourceReference ) =
68
- encoder ( ConverterOptions()) oa
69
- |> Encode.toJsonString 2
70
-
71
- /// exports in json-ld format
72
- let toJsonldString ( oa : OntologySourceReference ) =
73
- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) oa
74
- |> Encode.toJsonString 2
75
-
76
- let toJsonldStringWithContext ( a : OntologySourceReference ) =
77
- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) a
78
- |> Encode.toJsonString 2
79
-
80
- // let fromFile (path : string) =
81
- // File.ReadAllText path
82
- // |> fromString
83
-
84
- //let toFile (path : string) (osr:OntologySourceReference) =
85
- // File.WriteAllText(path,toString osr)
21
+
86
22
87
23
module OntologyAnnotation =
88
-
89
- let genID ( o : OntologyAnnotation ) : string =
90
- match o.TermAccessionNumber with
91
- | Some ta -> URI.toString ta
92
- | None -> match o.TermSourceREF with
93
- | Some r -> " #" + r.Replace( " " , " _" )
94
- | None -> match o.Name with
95
- | Some n -> " #UserTerm_" + n .Replace( " " , " _" )
96
- | None -> " #DummyOntologyAnnotation"
97
-
98
- let encoder ( options : ConverterOptions ) ( oa : OntologyAnnotation ) =
99
- let commentEncoder = if options.IsJsonLD then Comment.encoderDisambiguatingDescription else Comment.encoder options
24
+
25
+ let encoder ( oa : OntologyAnnotation ) =
100
26
[
101
- if options.SetID then
102
- " @id" , Encode.string ( oa |> genID)
103
- if options.IsJsonLD then
104
- " @type" , Encode.string " OntologyAnnotation"
105
27
Encode.tryInclude " annotationValue" Encode.string ( oa.Name)
106
28
Encode.tryInclude " termSource" Encode.string ( oa.TermSourceREF)
107
29
Encode.tryInclude " termAccession" Encode.string ( oa.TermAccessionNumber)
108
- Encode.tryIncludeSeq " comments" commentEncoder ( oa.Comments)
109
- if options.IsJsonLD then
110
- " @context" , ROCrateContext.OntologyAnnotation.context_ jsonvalue
30
+ Encode.tryIncludeSeq " comments" Comment.encoder ( oa.Comments)
111
31
]
112
32
|> Encode.choose
113
33
|> Encode.object
114
34
115
35
116
- let decoder ( options : ConverterOptions ) : Decoder < OntologyAnnotation > =
36
+ let decoder : Decoder < OntologyAnnotation > =
117
37
Decode.object ( fun get ->
118
38
OntologyAnnotation.create(
119
- ?name = get.Optional.Field " annotationValue" ( AnnotationValue.decoder options ),
39
+ ?name = get.Optional.Field " annotationValue" ( AnnotationValue.decoder),
120
40
?tsr = get.Optional.Field " termSource" Decode.string,
121
41
?tan = get.Optional.Field " termAccession" Decode.string,
122
- ?comments = get.Optional.Field " comments" ( Decode.resizeArray ( Comment.decoder options ) )
42
+ ?comments = get.Optional.Field " comments" ( Decode.resizeArray Comment.decoder)
123
43
)
124
44
)
125
45
126
-
127
- let compressedEncoder ( stringTable : StringTableMap ) ( options : ConverterOptions ) ( oa : OntologyAnnotation ) =
46
+ let compressedEncoder ( stringTable : StringTableMap ) ( oa : OntologyAnnotation ) =
128
47
[
129
- if options.SetID then " @id" , Encode.string ( oa |> genID)
130
- if options.IsJsonLD then " @type" , Encode.string " OntologyAnnotation"
131
48
Encode.tryInclude " a" ( StringTable.encodeString stringTable) ( oa.Name)
132
49
Encode.tryInclude " ts" ( StringTable.encodeString stringTable) ( oa.TermSourceREF)
133
50
Encode.tryInclude " ta" ( StringTable.encodeString stringTable) ( oa.TermAccessionNumber)
134
- Encode.tryIncludeSeq " comments" ( Comment.encoder options ) ( oa.Comments)
51
+ Encode.tryIncludeSeq " comments" Comment.encoder ( oa.Comments)
135
52
]
136
53
|> Encode.choose
137
54
|> Encode.object
138
55
139
56
140
- let compressedDecoder ( stringTable : StringTableArray ) ( options : ConverterOptions ) : Decoder < OntologyAnnotation > =
57
+ let compressedDecoder ( stringTable : StringTableArray ) : Decoder < OntologyAnnotation > =
141
58
Decode.object ( fun get ->
142
59
OntologyAnnotation(
143
60
?name = get.Optional.Field " a" ( StringTable.decodeString stringTable),
144
61
?tsr = get.Optional.Field " ts" ( StringTable.decodeString stringTable),
145
62
?tan = get.Optional.Field " ta" ( StringTable.decodeString stringTable),
146
- ?comments = get.Optional.Field " comments" ( Decode.resizeArray ( Comment.decoder options ) )
63
+ ?comments = get.Optional.Field " comments" ( Decode.resizeArray Comment.decoder)
147
64
)
148
65
)
149
66
150
- let fromJsonString ( s : string ) =
151
- Decode.fromJsonString ( decoder ( ConverterOptions())) s
67
+ module ROCrate =
68
+
69
+ let genID ( o : OntologyAnnotation ) : string =
70
+ match o.TermAccessionNumber with
71
+ | Some ta -> URI.toString ta
72
+ | None ->
73
+ match o.TermSourceREF with
74
+ | Some r -> " #" + r.Replace( " " , " _" )
75
+ | None ->
76
+ match o.Name with
77
+ | Some n -> " #UserTerm_" + n .Replace( " " , " _" )
78
+ | None -> " #DummyOntologyAnnotation"
79
+
80
+ let encoder ( oa : OntologyAnnotation ) =
81
+ [
82
+ " @id" , Encode.string ( oa |> genID)
83
+ " @type" , Encode.string " OntologyAnnotation"
84
+ Encode.tryInclude " annotationValue" Encode.string ( oa.Name)
85
+ Encode.tryInclude " termSource" Encode.string ( oa.TermSourceREF)
86
+ Encode.tryInclude " termAccession" Encode.string ( oa.TermAccessionNumber)
87
+ Encode.tryIncludeSeq " comments" Comment.ROCrate.encoderDisambiguatingDescription ( oa.Comments)
88
+ " @context" , ROCrateContext.OntologyAnnotation.context_ jsonvalue
89
+ ]
90
+ |> Encode.choose
91
+ |> Encode.object
92
+
93
+ let decoder : Decoder < OntologyAnnotation > =
94
+ Decode.object ( fun get ->
95
+ OntologyAnnotation.create(
96
+ ?name = get.Optional.Field " annotationValue" AnnotationValue.decoder,
97
+ ?tsr = get.Optional.Field " termSource" Decode.string,
98
+ ?tan = get.Optional.Field " termAccession" Decode.string,
99
+ ?comments = get.Optional.Field " comments" ( Decode.resizeArray Comment.decoder)
100
+ )
101
+ )
102
+
103
+ module ISAJson =
152
104
153
- let fromJsonldString ( s : string ) =
154
- Decode.fromJsonString ( decoder ( ConverterOptions( IsJsonLD= true ))) s
155
-
156
- let toJsonString ( oa : OntologyAnnotation ) =
157
- encoder ( ConverterOptions()) oa
158
- |> Encode.toJsonString 2
159
-
160
- /// exports in json-ld format
161
- let toJsonldString ( oa : OntologyAnnotation ) =
162
- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) oa
163
- |> Encode.toJsonString 2
164
-
165
- let toJsonldStringWithContext ( a : OntologyAnnotation ) =
166
- encoder ( ConverterOptions( SetID= true , IsJsonLD= true )) a
167
- |> Encode.toJsonString 2
168
-
169
- //let fromFile (path : string) =
170
- // File.ReadAllText path
171
- // |> fromString
172
-
173
- //let toFile (path : string) (oa:OntologyAnnotation) =
174
- // File.WriteAllText(path,toString oa)
105
+ let encoder = encoder
106
+ let decoder = decoder
107
+
108
+ [<AutoOpen>]
109
+ module OntologyAnnotationExtensions =
110
+
111
+ type OntologyAnnotation with
112
+
113
+ static member fromJsonString ( s : string ) =
114
+ Decode.fromJsonString OntologyAnnotation.decoder s
115
+
116
+ static member toJsonString (? spaces ) =
117
+ fun ( obj : OntologyAnnotation ) ->
118
+ OntologyAnnotation.encoder obj
119
+ |> Encode.toJsonString ( defaultArg spaces 2 )
120
+
121
+ static member fromROCrateJsonString ( s : string ) =
122
+ Decode.fromJsonString OntologyAnnotation.ROCrate.decoder s
123
+
124
+ /// exports in json-ld format
125
+ static member toROCrateJsonString (? spaces ) =
126
+ fun ( obj : OntologyAnnotation ) ->
127
+ OntologyAnnotation.ROCrate.encoder obj
128
+ |> Encode.toJsonString ( defaultArg spaces 2 )
129
+
130
+ static member toISAJsonString (? spaces ) =
131
+ fun ( obj : OntologyAnnotation ) ->
132
+ OntologyAnnotation.ISAJson.encoder obj
133
+ |> Encode.toJsonString ( defaultArg spaces 2 )
134
+
135
+ static member fromISAJsonString ( s : string ) =
136
+ Decode.fromJsonString OntologyAnnotation.ISAJson.decoder s
0 commit comments