Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update datamap handling in GetUpdateContracts function #450

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions src/ARCtrl/ARC.fs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
let filteredPaths = paths |> Array.filter (fun p -> p.StartsWith(assayFolderPath) |> not)
this.SetFilePaths(filteredPaths)
[
assay.ToDeleteContract()
isa.ToUpdateContract()
yield assay.ToDeleteContract()
yield isa.ToUpdateContract()
for s in studies do
s.ToUpdateContract()
yield! s.ToUpdateContract()
]
|> ResizeArray

Expand Down Expand Up @@ -323,7 +323,8 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
/// If datamapFile is set to true, a write contract for the datamap file will be included for each study and assay.
/// </summary>
/// <param name="datamapFile">Default: false.</param>
member this.GetWriteContracts () =
member this.GetWriteContracts (?datamapAsFile) =
let datamapAsFile = defaultArg datamapAsFile false
//let datamapFile = defaultArg datamapFile false
/// Map containing the DTOTypes and objects for the ISA objects.
let workbooks = System.Collections.Generic.Dictionary<string, DTOType*FsWorkbook>()
Expand All @@ -337,9 +338,9 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
s.StaticHash <- s.GetLightHashCode()
workbooks.Add (
Identifier.Study.fileNameFromIdentifier s.Identifier,
(DTOType.ISA_Study, ArcStudy.toFsWorkbook s)
(DTOType.ISA_Study, ArcStudy.toFsWorkbook(s,datamapSheet = not datamapAsFile))
)
if s.DataMap.IsSome (*&& datamapFile*) then
if s.DataMap.IsSome && datamapAsFile then
let dm = s.DataMap.Value
dm.StaticHash <- dm.GetHashCode()
workbooks.Add (
Expand All @@ -353,8 +354,9 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
a.StaticHash <- a.GetLightHashCode()
workbooks.Add (
Identifier.Assay.fileNameFromIdentifier a.Identifier,
(DTOType.ISA_Assay, ArcAssay.toFsWorkbook a))
if a.DataMap.IsSome (*&& datamapFile*) then
(DTOType.ISA_Assay, ArcAssay.toFsWorkbook(a, datamapSheet = not datamapAsFile))
)
if a.DataMap.IsSome && datamapAsFile then
let dm = a.DataMap.Value
dm.StaticHash <- dm.GetHashCode()
workbooks.Add (
Expand All @@ -381,7 +383,7 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
///
/// ISA contracts do contain the object data as spreadsheets, while the other contracts only contain the path.
/// </summary>
member this.GetUpdateContracts () =
member this.GetUpdateContracts (?datamapAsFile) =
// Map containing the DTOTypes and objects for the ISA objects.
match this.ISA with
| None -> // if no ISA is present, return write contracts
Expand All @@ -403,37 +405,19 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
for s in inv.Studies do
let hash = s.GetLightHashCode()
if s.StaticHash = 0 then
yield! s.ToCreateContract(WithFolder = true)
elif s.StaticHash <> hash then
yield s.ToUpdateContract()
yield! s.ToCreateContract(WithFolder = true,?datamapAsFile = datamapAsFile)
else
yield! s.ToUpdateContract(?datamapAsFile = datamapAsFile)
s.StaticHash <- hash

match s.DataMap with
| Some dm when dm.StaticHash = 0 ->
yield dm.ToCreateContractForStudy(s.Identifier)
dm.StaticHash <- dm.GetHashCode()
| Some dm when dm.StaticHash <> dm.GetHashCode() ->
yield dm.ToUpdateContractForStudy(s.Identifier)
dm.StaticHash <- dm.GetHashCode()
| _ -> ()

// Get Assay contracts
for a in inv.Assays do
let hash = a.GetLightHashCode()
if a.StaticHash = 0 then
yield! a.ToCreateContract(WithFolder = true)
elif a.StaticHash <> hash then
yield a.ToUpdateContract()
yield! a.ToCreateContract(WithFolder = true,?datamapAsFile = datamapAsFile)
else
yield! a.ToUpdateContract(?datamapAsFile = datamapAsFile)
a.StaticHash <- hash

match a.DataMap with
| Some dm when dm.StaticHash = 0 ->
yield dm.ToCreateContractForAssay(a.Identifier)
dm.StaticHash <- dm.GetHashCode()
| Some dm when dm.StaticHash <> dm.GetHashCode() ->
yield dm.ToUpdateContractForAssay(a.Identifier)
dm.StaticHash <- dm.GetHashCode()
| _ -> ()
|]


Expand Down
42 changes: 34 additions & 8 deletions src/Contract/ArcAssay.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,49 @@ module AssayContractExtensions =

type ArcAssay with

member this.ToCreateContract (?WithFolder) =
member this.ToCreateContract (?WithFolder, ?datamapAsFile) =
let withFolder = defaultArg WithFolder false
let dataMapAsFile = defaultArg datamapAsFile false
let path = Identifier.Assay.fileNameFromIdentifier this.Identifier
let c = Contract.createCreate(path, DTOType.ISA_Assay, DTO.Spreadsheet (this |> ArcAssay.toFsWorkbook))
let dto = DTO.Spreadsheet (ArcAssay.toFsWorkbook(this, datamapSheet = not dataMapAsFile))
let c = Contract.createCreate(path, DTOType.ISA_Assay, dto)
[|
if withFolder then
let folderFS = FileSystemTree.createAssaysFolder([|FileSystemTree.createAssayFolder this.Identifier|])
for p in folderFS.ToFilePaths(false) do
if p <> path && p <> "assays/.gitkeep" then Contract.createCreate(p, DTOType.PlainText)
c
if p <> path && p <> "assays/.gitkeep" then
yield Contract.createCreate(p, DTOType.PlainText)
match this.DataMap with
| Some dm ->
dm.StaticHash <- dm.GetHashCode()
if dataMapAsFile then
yield dm.ToCreateContractForAssay(this.Identifier)
| _ -> ()
yield c
|]


member this.ToUpdateContract () =
member this.ToUpdateContract (?datamapAsFile) =
let datamapAsFile = defaultArg datamapAsFile false
let path = Identifier.Assay.fileNameFromIdentifier this.Identifier
let c = Contract.createUpdate(path, DTOType.ISA_Assay, DTO.Spreadsheet (this |> ArcAssay.toFsWorkbook))
c
let hash = this.GetLightHashCode()
let datamapHasChanged =
match this.DataMap with
| Some dm ->
let hc = dm.GetHashCode() <> dm.StaticHash
dm.StaticHash <- dm.GetHashCode()
hc
| _ -> false
let createAssayContract() =
let dto = DTO.Spreadsheet (ArcAssay.toFsWorkbook(this, datamapSheet = not datamapAsFile))
Contract.createUpdate(path, DTOType.ISA_Assay, dto)
[|
if hash <> this.StaticHash || (datamapHasChanged && not datamapAsFile) then
createAssayContract()
if datamapHasChanged && datamapAsFile then
this.DataMap.Value.ToUpdateContractForAssay(this.Identifier)

|]

member this.ToDeleteContract () =
let path = getAssayFolderPath(this.Identifier)
Expand All @@ -49,7 +75,7 @@ module AssayContractExtensions =
static member toCreateContract (assay: ArcAssay,?WithFolder) : Contract [] =
assay.ToCreateContract(?WithFolder = WithFolder)

static member toUpdateContract (assay: ArcAssay) : Contract =
static member toUpdateContract (assay: ArcAssay) : Contract [] =
assay.ToUpdateContract()

static member tryFromReadContract (c:Contract) =
Expand Down
41 changes: 33 additions & 8 deletions src/Contract/ArcStudy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,48 @@ module StudyContractExtensions =

type ArcStudy with

member this.ToCreateContract (?WithFolder) =
member this.ToCreateContract (?WithFolder, ?datamapAsFile) =
let withFolder = defaultArg WithFolder false
let dataMapAsFile = defaultArg datamapAsFile false
let path = Identifier.Study.fileNameFromIdentifier this.Identifier
let c = Contract.createCreate(path, DTOType.ISA_Study, DTO.Spreadsheet (this |> ArcStudy.toFsWorkbook))
let dto = DTO.Spreadsheet (ArcStudy.toFsWorkbook(this, datamapSheet = not dataMapAsFile))
let c = Contract.createCreate(path, DTOType.ISA_Study, dto)

[|
if withFolder then
let folderFS = FileSystemTree.createStudiesFolder ([|FileSystemTree.createStudyFolder this.Identifier|])
for p in folderFS.ToFilePaths(false) do
if p <> path && p <> "studies/.gitkeep" then Contract.createCreate(p, DTOType.PlainText)
c
if p <> path && p <> "studies/.gitkeep" then yield Contract.createCreate(p, DTOType.PlainText)
match this.DataMap with
| Some dm ->
dm.StaticHash <- dm.GetHashCode()
if dataMapAsFile then
yield dm.ToCreateContractForStudy(this.Identifier)
| _ -> ()
yield c
|]

member this.ToUpdateContract () =
member this.ToUpdateContract (?datamapAsFile) =
let datamapAsFile = defaultArg datamapAsFile false
let path = Identifier.Study.fileNameFromIdentifier this.Identifier
let c = Contract.createUpdate(path, DTOType.ISA_Study, DTO.Spreadsheet (this |> ArcStudy.toFsWorkbook))
c
let hash = this.GetLightHashCode()
let datamapHasChanged =
match this.DataMap with
| Some dm ->
let hc = dm.GetHashCode() <> dm.StaticHash
dm.StaticHash <- dm.GetHashCode()
hc
| _ -> false
let createStudyContract() =
let dto = DTO.Spreadsheet (ArcStudy.toFsWorkbook(this, datamapSheet = not datamapAsFile))
Contract.createUpdate(path, DTOType.ISA_Study, dto)
[|
if hash <> this.StaticHash || (datamapHasChanged && not datamapAsFile) then
createStudyContract()
if datamapHasChanged && datamapAsFile then
this.DataMap.Value.ToUpdateContractForStudy(this.Identifier)

|]

member this.ToDeleteContract () =
let path = getStudyFolderPath(this.Identifier)
Expand All @@ -49,7 +74,7 @@ module StudyContractExtensions =
static member toCreateContract (study: ArcStudy, ?WithFolder) : Contract [] =
study.ToCreateContract(?WithFolder = WithFolder)

static member toUpdateContract (study: ArcStudy) : Contract =
static member toUpdateContract (study: ArcStudy) : Contract [] =
study.ToUpdateContract()

static member tryFromReadContract (c:Contract) =
Expand Down
26 changes: 21 additions & 5 deletions tests/ARCtrl/ARCtrl.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let private tests_read_contracts = testList "read_contracts" [
(Array.create 2 (CompositeCell.createFreeText UpdateAssayWithStudyProtocol.description))
"Description value was not taken correctly"
)
testCase "SimpleISA_WithDataset" (fun _ ->
testCase "SimpleISA_WithDatamap" (fun _ ->
let contracts = Array.append simpleISAContracts [|SimpleISA.Assay.proteomeDatamapContract|]

let arc = ARC()
Expand Down Expand Up @@ -226,13 +226,13 @@ let private tests_writeContracts = testList "write_contracts" [
Expect.exists contracts (fun c -> c.Path = "assays/MyAssay/isa.assay.xlsx" && c.DTOType.IsSome && c.DTOType.Value = Contract.DTOType.ISA_Assay) "assay file exisiting but has wrong DTO type"

)
testCase "assayWithDatamap" (fun _ ->
testCase "assayWithDatamap_AsFile" (fun _ ->
let inv = ArcInvestigation("MyInvestigation", "BestTitle")
let a = inv.InitAssay("MyAssay")
let dm = DataMap.init()
a.DataMap <- Some dm
let arc = ARC(isa = inv)
let contracts = arc.GetWriteContracts()
let contracts = arc.GetWriteContracts(datamapAsFile = true)
let contractPathsString = contracts |> Array.map (fun c -> c.Path) |> String.concat ", "
Expect.equal contracts.Length 10 $"Should contain more contracts as base folders but contained: {contractPathsString}"

Expand Down Expand Up @@ -463,7 +463,7 @@ let private tests_updateContracts = testList "update_contracts" [
let contracts = arc.GetUpdateContracts()
Expect.equal contracts.Length 0 "Should contain no contracts as there are no changes"
)
testCase "simpleISA_Datamap_Changed" (fun _ ->
testCase "simpleISA_Datamap_Changed_AsFile" (fun _ ->
let arc = ARC()
let readContracts = Array.append simpleISAContracts [|SimpleISA.Assay.proteomeDatamapContract|]
arc.SetISAFromContracts readContracts
Expand All @@ -472,10 +472,26 @@ let private tests_updateContracts = testList "update_contracts" [
let dm = Expect.wantSome (isa.GetAssay(SimpleISA.Assay.proteomeIdentifer).DataMap) "Assay should have datamap"
dm.GetDataContext(1).Name <- Some "Hello"

let contracts = arc.GetUpdateContracts()
let contracts = arc.GetUpdateContracts(datamapAsFile = true)
Expect.equal contracts.Length 1 $"Should contain only assay datamap change contract"
let expectedPath = Identifier.Assay.datamapFileNameFromIdentifier SimpleISA.Assay.proteomeIdentifer
Expect.equal contracts.[0].Path expectedPath "Should be the assay datamap file"
let nextContracts = arc.GetUpdateContracts(datamapAsFile = true)
Expect.equal nextContracts.Length 0 "Should contain no contracts as there are no changes"
)
testCase "simpleISA_Datamap_Changed_AsSheet" (fun _ ->
let arc = ARC()
let readContracts = Array.append simpleISAContracts [|SimpleISA.Assay.proteomeDatamapContract|]
arc.SetISAFromContracts readContracts
let isa = arc.ISA.Value

let dm = Expect.wantSome (isa.GetAssay(SimpleISA.Assay.proteomeIdentifer).DataMap) "Assay should have datamap"
dm.GetDataContext(1).Name <- Some "Hello"

let contracts = arc.GetUpdateContracts()
Expect.equal contracts.Length 1 $"Should contain only assay datamap change contract"
let expectedPath = Identifier.Assay.fileNameFromIdentifier SimpleISA.Assay.proteomeIdentifer
Expect.equal contracts.[0].Path expectedPath "Should be the assay file, as datamap is contained in assay"
let nextContracts = arc.GetUpdateContracts()
Expect.equal nextContracts.Length 0 "Should contain no contracts as there are no changes"
)
Expand Down
Loading