From 485d778ed49f99b1ae7994d1a1e764ea08091e95 Mon Sep 17 00:00:00 2001 From: HLWeil Date: Wed, 26 Jun 2024 12:52:06 +0200 Subject: [PATCH] make process conversion more robust against different outputs --- src/Core/Conversion.fs | 25 ++++++++++++--- tests/Core/ArcJsonConversion.Tests.fs | 45 ++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/Core/Conversion.fs b/src/Core/Conversion.fs index 1bdb83e2..8160b878 100644 --- a/src/Core/Conversion.fs +++ b/src/Core/Conversion.fs @@ -100,6 +100,10 @@ module JsonTypes = let c = MaterialAttribute.create(CharacteristicType = header.ToTerm()) MaterialAttributeValue.create(Category = c,?Value = v,?Unit = u) + let composeFreetextMaterialName (headerFT : string) (name : string) = + $"{headerFT}={name}" + + /// Convert a CompositeHeader and Cell tuple to a ISA ProcessInput let composeProcessInput (header : CompositeHeader) (value : CompositeCell) : ProcessInput = match header with @@ -107,6 +111,8 @@ module JsonTypes = | CompositeHeader.Input IOType.Sample -> ProcessInput.createSample(value.ToString()) | CompositeHeader.Input IOType.Material -> ProcessInput.createMaterial(value.ToString()) | CompositeHeader.Input IOType.Data -> ProcessInput.createRawData(value.ToString()) + | CompositeHeader.Input (IOType.FreeText ft) -> + ProcessInput.createMaterial(composeFreetextMaterialName ft (value.ToString())) | _ -> failwithf "Could not parse input header %O" header @@ -114,9 +120,12 @@ module JsonTypes = /// Convert a CompositeHeader and Cell tuple to a ISA ProcessOutput let composeProcessOutput (header : CompositeHeader) (value : CompositeCell) : ProcessOutput = match header with - | CompositeHeader.Output IOType.Sample -> ProcessOutput.createSample(value.ToString()) + | CompositeHeader.Output IOType.Source + | CompositeHeader.Output IOType.Sample -> ProcessOutput.createSample(value.ToString()) | CompositeHeader.Output IOType.Material -> ProcessOutput.createMaterial(value.ToString()) | CompositeHeader.Output IOType.Data -> ProcessOutput.createRawData(value.ToString()) + | CompositeHeader.Output (IOType.FreeText ft) -> + ProcessOutput.createMaterial(composeFreetextMaterialName ft (value.ToString())) | _ -> failwithf "Could not parse output header %O" header @@ -407,8 +416,11 @@ module ProcessParsing = ProcessInput.createSample(input.Name, characteristics = chars) ] else - input - |> ProcessInput.setCharacteristicValues chars + if chars.Length > 0 then + input + |> ProcessInput.setCharacteristicValues chars + else + input |> List.singleton | None -> fun (matrix : System.Collections.Generic.Dictionary<(int * int),CompositeCell>) i -> @@ -429,8 +441,11 @@ module ProcessParsing = ProcessOutput.createSample(output.Name, factors = factors) ] else - output - |> ProcessOutput.setFactorValues factors + if factors.Length > 0 then + output + |> ProcessOutput.setFactorValues factors + else + output |> List.singleton | None -> fun (matrix : System.Collections.Generic.Dictionary<(int * int),CompositeCell>) i -> diff --git a/tests/Core/ArcJsonConversion.Tests.fs b/tests/Core/ArcJsonConversion.Tests.fs index 4c337fb4..a57f8fc8 100644 --- a/tests/Core/ArcJsonConversion.Tests.fs +++ b/tests/Core/ArcJsonConversion.Tests.fs @@ -42,6 +42,17 @@ module Helper = t.AddColumns(columns) t + let singleRowOutputSource = + /// Input [Source] --> Source_0 .. Source_4 + let columns = + [| + CompositeColumn.create(CompositeHeader.Input IOType.Source, createCells_FreeText "Source" 1) + CompositeColumn.create(CompositeHeader.Output IOType.Source, createCells_FreeText "Sample" 1) + |] + let t = ArcTable.init(tableName1) + t.AddColumns(columns) + t + let singleRowMixedValues = /// Input [Source] --> Source_0 .. Source_4 let columns = @@ -137,15 +148,19 @@ let private tests_ArcTableProcess = let expectedParam = ProtocolParameter.create(ParameterName = oa_species) let expectedValue = Value.Ontology oa_chlamy let expectedPPV = ProcessParameterValue.create(Category = expectedParam, Value = expectedValue) + let expectedInput = Source.create(Name = "Source_0") |> ProcessInput.Source + let expectedOutput = Sample.create(Name = "Sample_0") |> ProcessOutput.Sample Expect.equal processes.Length 1 "Should have 1 process" let p = processes.[0] - Expect.isSome p.ParameterValues "Process should have parameter values" - Expect.equal p.ParameterValues.Value.Length 1 "Process should have 1 parameter values" - Expect.equal p.ParameterValues.Value.[0] expectedPPV "Param value does not match" - Expect.isSome p.Inputs "Process should have inputs" - Expect.equal p.Inputs.Value.Length 1 "Process should have 1 input" - Expect.isSome p.Outputs "Process should have outputs" - Expect.equal p.Outputs.Value.Length 1 "Process should have 1 output" + let paramValues = Expect.wantSome p.ParameterValues "Process should have parameter values" + Expect.equal paramValues.Length 1 "Process should have 1 parameter values" + Expect.equal paramValues.[0] expectedPPV "Param value does not match" + let inputs = Expect.wantSome p.Inputs "Process should have inputs" + Expect.equal inputs.Length 1 "Process should have 1 input" + Expect.equal inputs.[0] expectedInput "Input value does not match" + let outputs = Expect.wantSome p.Outputs "Process should have outputs" + Expect.equal outputs.Length 1 "Process should have 1 output" + Expect.equal outputs.[0] expectedOutput "Output value does not match" Expect.isSome p.Name "Process should have name" Expect.equal p.Name.Value tableName1 "Process name should match table name" ) @@ -156,6 +171,22 @@ let private tests_ArcTableProcess = let expectedTable = t Expect.arcTableEqual table expectedTable "Table should be equal" ) + testCase "SingleRowOutputSource GetProcesses" (fun () -> + let t = singleRowOutputSource.Copy() + let processes = t.GetProcesses() + let expectedInput = Source.create(Name = "Source_0") |> ProcessInput.Source + let expectedOutput = Sample.create(Name = "Sample_0") |> ProcessOutput.Sample + Expect.equal processes.Length 1 "Should have 1 process" + let p = processes.[0] + let inputs = Expect.wantSome p.Inputs "Process should have inputs" + Expect.equal inputs.Length 1 "Process should have 1 input" + Expect.equal inputs.[0] expectedInput "Input value does not match" + let outputs = Expect.wantSome p.Outputs "Process should have outputs" + Expect.equal outputs.Length 1 "Process should have 1 output" + Expect.equal outputs.[0] expectedOutput "Output value does not match" + let name = Expect.wantSome p.Name "Process should have name" + Expect.equal name tableName1 "Process name should match table name" + ) testCase "SingleRowMixedValues GetProcesses" (fun () -> let t = singleRowMixedValues.Copy()