Skip to content

Commit

Permalink
fix additionalProperties being lost in RO-Crate write read
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Nov 6, 2024
1 parent 44c28f3 commit 17d6e64
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Json/Process/Material.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Material =
[
"@id", Encode.string (oa |> genID) |> Some
"@type", (Encode.list [Encode.string "Material"]) |> Some
"additionalType", Encode.string "Material" |> Some
Encode.tryInclude "name" Encode.string oa.Name
Encode.tryInclude "type" MaterialType.ROCrate.encoder oa.MaterialType
Encode.tryIncludeListOpt "characteristics" MaterialAttributeValue.ROCrate.encoder oa.Characteristics
Expand All @@ -31,7 +32,10 @@ module Material =

let decoder : Decoder<Material> =
let rec decode() =
Decode.object (fun get ->
Decode.object (fun get ->
match get.Optional.Field "additionalType" Decode.uri with
| Some "Material" | None -> ()
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
{
ID = get.Optional.Field "@id" Decode.uri
Name = get.Optional.Field "name" Decode.string
Expand Down
2 changes: 1 addition & 1 deletion src/Json/Process/ProcessInput.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module ProcessInput =

let decoder : Decoder<ProcessInput> =
Decode.oneOf [
Decode.map ProcessInput.Source Source.ROCrate.decoder
Decode.map ProcessInput.Sample Sample.ROCrate.decoder
Decode.map ProcessInput.Source Source.ROCrate.decoder
Decode.map ProcessInput.Data Data.ROCrate.decoder
Decode.map ProcessInput.Material Material.ROCrate.decoder
]
Expand Down
4 changes: 4 additions & 0 deletions src/Json/Process/Sample.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Sample =
[
"@id", Encode.string (oa |> genID) |> Some
"@type", (Encode.list [ Encode.string "Sample"]) |> Some
"additionalType", Encode.string "Sample" |> Some
Encode.tryInclude "name" Encode.string (oa.Name)
Encode.tryIncludeList "additionalProperties" id additionalProperties
"@context", ROCrateContext.Sample.context_jsonvalue |> Some
Expand Down Expand Up @@ -64,6 +65,9 @@ module Sample =
additionalProperties
|> List.choose snd
|> Helper.Option.fromValueWithDefault []
match get.Optional.Field "additionalType" Decode.uri with
| Some "Sample" | None -> ()
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
{
ID = get.Optional.Field "@id" Decode.uri
Name = get.Optional.Field "name" Decode.string
Expand Down
5 changes: 4 additions & 1 deletion src/Json/Process/Source.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Source =
[
"@id", Encode.string (oa |> genID) |> Some
"@type", (Encode.list [ Encode.string "Source"]) |> Some
"additionalType", Encode.string "Source" |> Some
Encode.tryInclude "name" Encode.string (oa.Name)
Encode.tryIncludeListOpt "characteristics" MaterialAttributeValue.ROCrate.encoder (oa.Characteristics)
"@context", ROCrateContext.Source.context_jsonvalue |> Some
Expand All @@ -29,7 +30,9 @@ module Source =

let rec decoder : Decoder<Source> =
Decode.object (fun get ->

match get.Optional.Field "additionalType" Decode.uri with
| Some "Source" | None -> ()
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
{
ID = get.Optional.Field "@id" Decode.uri
Name = get.Optional.Field "name" Decode.string
Expand Down
4 changes: 2 additions & 2 deletions src/Json/context/rocrate/isa_assay_context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Assay =
otherMaterials: string
samples: string
characteristicCategories: string
processSequences: string
processSequence: string
unitCategories: string

comments: string
Expand All @@ -38,7 +38,7 @@ module Assay =
"technologyPlatform", Encode.string "sdo:measurementMethod"
"dataFiles", Encode.string "sdo:hasPart"
"performers", Encode.string "sdo:creator"
"processSequences", Encode.string "sdo:about"
"processSequence", Encode.string "sdo:about"

"comments", Encode.string "sdo:comment"
"filename", Encode.string "sdo:url"
Expand Down
33 changes: 32 additions & 1 deletion tests/Json/Process/ProcessInput.Tests.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Tests.Process.ProcessInput
module Tests.Process.ProcessInput

open ARCtrl
open ARCtrl.Process
Expand All @@ -22,7 +22,23 @@ let private tests_source =
let actual = o_out
Expect.stringEqual actual expected "Written processInput does not match read process input"
)
testCase "LD_WriteReadWithCharacteristics" (fun () ->
let charaHeader = Process.MaterialAttribute.create(CharacteristicType = OntologyAnnotation.create("MyAnnotation","NCIT","http://purl.obolibrary.org/obo/NCIT_C42781"))
let charaValue = OntologyAnnotation.create("MyAnnotationValue","NCIT","http://purl.obolibrary.org/obo/NCIT_C42782")
let chara = Process.MaterialAttributeValue.create(Category = charaHeader, Value = Value.Ontology charaValue)
let source = Source.create(Name = "#sample/sample-P-0.1-aliquot7", Characteristics = [chara])
let o_out = ProcessInput.ROCrate.encoder (ProcessInput.Source source) |> Encode.toJsonString 0
let inputPI = Decode.fromJsonString ProcessInput.ROCrate.decoder o_out
let inputSourceOpt = ProcessInput.trySource inputPI
let inputSource = Expect.wantSome inputSourceOpt "Input is not a sample"
Expect.equal inputSource.Name source.Name "Sample name did not match"
let characteristics = Expect.wantSome inputSource.Characteristics "No characteristics found"
Expect.hasLength characteristics 1 "Sample characteristics length did not match"
let inputChara = characteristics.[0]
Expect.equal inputChara chara "Sample characteristic did not match"
)
]

let private tests_material =
testList "Material" [
testCase "ReaderSuccess" (fun () ->
Expand Down Expand Up @@ -75,6 +91,21 @@ let private tests_sample =
let actual = o_out
Expect.stringEqual actual expected "Written processInput does not match read process input"
)
testCase "LD_WriteReadWithCharacteristics" (fun () ->
let charaHeader = Process.MaterialAttribute.create(CharacteristicType = OntologyAnnotation.create("MyAnnotation","NCIT","http://purl.obolibrary.org/obo/NCIT_C42781"))
let charaValue = OntologyAnnotation.create("MyAnnotationValue","NCIT","http://purl.obolibrary.org/obo/NCIT_C42782")
let chara = Process.MaterialAttributeValue.create(Category = charaHeader, Value = Value.Ontology charaValue)
let sample = Sample.create(Name = "#sample/sample-P-0.1-aliquot7", Characteristics = [chara])
let o_out = ProcessInput.ROCrate.encoder (ProcessInput.Sample sample) |> Encode.toJsonString 0
let inputPI = Decode.fromJsonString ProcessInput.ROCrate.decoder o_out
let inputSampleOpt = ProcessInput.trySample inputPI
let inputSample = Expect.wantSome inputSampleOpt "Input is not a sample"
Expect.equal inputSample.Name sample.Name "Sample name did not match"
let characteristics = Expect.wantSome inputSample.Characteristics "No characteristics found"
Expect.hasLength characteristics 1 "Sample characteristics length did not match"
let inputChara = characteristics.[0]
Expect.equal inputChara chara "Sample characteristic did not match"
)
]


Expand Down

0 comments on commit 17d6e64

Please sign in to comment.