Skip to content

Commit

Permalink
add rename contract and assay rename logic
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed May 29, 2024
1 parent 6ef030d commit 57f3a2e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/ARCtrl/ARC.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
member this.RemoveAssay(assayIdentifier: string) =
let isa =
match this.ISA with
| Some i -> i
| Some i when i.AssayIdentifiers |> Seq.contains assayIdentifier -> i
| Some _ -> failwith "ARC does not contain assay with given name"
| None -> failwith "Cannot remove assay from null ISA value."
let assay = isa.GetAssay(assayIdentifier)
let studies = assay.StudiesRegisteredIn
Expand All @@ -105,6 +106,25 @@ type ARC(?isa : ArcInvestigation, ?cwl : CWL.CWL, ?fs : FileSystem.FileSystem) =
]
|> ResizeArray

member this.RenameAssay(oldAssayIdentifier: string, newAssayIdentifier: string) =
let isa =
match this.ISA with
| Some i when i.AssayIdentifiers |> Seq.contains oldAssayIdentifier -> i
| Some _ -> failwith "ARC does not contain assay with given name"
| None -> failwith "Cannot rename assay in null ISA value."

isa.RenameAssay(oldAssayIdentifier,newAssayIdentifier)
let paths = this.FileSystem.Tree.ToFilePaths()
let oldAssayFolderPath = Path.getAssayFolderPath(oldAssayIdentifier)
let newAssayFolderPath = Path.getAssayFolderPath(newAssayIdentifier)
let renamedPaths = paths |> Array.map (fun p -> p.Replace(oldAssayFolderPath,newAssayFolderPath))
this.SetFilePaths(renamedPaths)
[
yield Contract.createRename(oldAssayFolderPath,newAssayFolderPath)
yield! this.GetUpdateContracts()
]
|> ResizeArray

member this.RemoveStudy(studyIdentifier: string) =
let isa =
match this.ISA with
Expand Down
16 changes: 16 additions & 0 deletions src/Contract/Contract.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Operation =
| [<CompiledName("DELETE")>] DELETE
| [<CompiledName("READ")>] READ
| [<CompiledName("EXECUTE")>] EXECUTE
| [<CompiledName("RENAME")>] RENAME

[<AttachMembers>]
type Contract =
Expand All @@ -79,12 +80,14 @@ type Contract =
[<NamedParams(fromIndex=2)>]
#endif
static member create(op, path, ?dtoType, ?dto) = {Operation= op; Path = path; DTOType = dtoType; DTO = dto}

/// <summary>Create a CREATE contract with all necessary information.</summary>
/// <param name="path">The path relative from ARC root, at which the new file should be created.</param>
/// <param name="dtoType">The file type.</param>
/// <param name="dto">The file data.</param>
/// <returns>Returns a CREATE contract.</returns>
static member createCreate(path, dtoType: DTOType, ?dto: DTO) = {Operation= Operation.CREATE; Path = path; DTOType = Some dtoType; DTO = dto}

/// <summary>Create a UPDATE contract with all necessary information.
///
/// Update contracts will overwrite in case of a string as DTO and will specifically update relevant changes only for spreadsheet files.
Expand All @@ -98,6 +101,7 @@ type Contract =
/// <param name="path">The path relative from ARC root, at which the file should be deleted.</param>
/// <returns>Returns a DELETE contract.</returns>
static member createDelete(path) = {Operation= Operation.DELETE; Path = path; DTOType = None; DTO = None}

/// <summary>Create a READ contract with all necessary information.
///
/// Created without DTO, any api user should update the READ contract with the io read result for further api use.
Expand All @@ -107,6 +111,7 @@ type Contract =
/// <param name="dtoType">The file type.</param>
/// <returns>Returns a READ contract.</returns>
static member createRead(path, dtoType: DTOType) = {Operation= Operation.READ; Path = path; DTOType = Some dtoType; DTO = None}

/// <summary>Create a EXECUTE contract with all necessary information.
///
/// This contract type is used to communicate cli tool execution.
Expand All @@ -117,3 +122,14 @@ type Contract =
static member createExecute(dto: CLITool, ?path: string) =
let path = Option.defaultValue "" path
{Operation= Operation.EXECUTE; Path = path; DTOType = Some DTOType.CLI; DTO = Some <| DTO.CLITool dto}

/// <summary>Create a RENAME contract with all necessary information.
///
/// This contract type is used to communicate file renaming.
///
/// **Note:** The path is the old path, the DTO is the new path.
/// </summary>
/// <param name="oldPath">The old path relative from ARC root.</param>
/// <param name="newPath">The new path relative from ARC root.</param>
/// <returns>Returns a RENAME contract.</returns>
static member createRename(oldPath, newPath) = {Operation= Operation.RENAME; Path = oldPath; DTOType = None; DTO = Some <| DTO.Text newPath}
26 changes: 26 additions & 0 deletions src/Core/ArcTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,32 @@ type ArcInvestigation(identifier : string, ?title : string, ?description : strin
newInv.RemoveAssay(assayIdentifier)
newInv

/// <summary>
/// Renames an assay in the whole investigation
/// </summary>
/// <param name="oldIdentifier">Identifier of the assay to be renamed</param>
/// <param name="newIdentifier">Identifier to which the assay should be renamed to</param>
member this.RenameAssay(oldIdentifier: string, newIdentifier: string) =
this.Assays
|> Seq.iter (fun a ->
if a.Identifier = oldIdentifier then
a.Identifier <- newIdentifier
)
this.Studies
|> Seq.iter (fun s ->
s.RegisteredAssayIdentifiers
|> Seq.iteri (fun i ai ->
if ai = oldIdentifier then
s.RegisteredAssayIdentifiers[i] <- newIdentifier
)
)

static member renameAssay(oldIdentifier: string, newIdentifier: string) =
fun (inv: ArcInvestigation) ->
let newInv = inv.Copy()
newInv.RenameAssay(oldIdentifier,newIdentifier)
newInv

// - Assay API - CRUD //
member this.SetAssayAt(index: int, assay: ArcAssay) =
ArcTypesAux.SanityChecks.validateUniqueAssayIdentifier assay.Identifier (this.Assays |> Seq.removeAt index |> Seq.map (fun a -> a.Identifier))
Expand Down
35 changes: 35 additions & 0 deletions tests/ARCtrl/ARCtrl.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -685,12 +685,47 @@ let private tests_RemoveAssay = testList "RemoveAssay" [
Expect.equal actual.[3].Operation UPDATE "study 2 contract cmd"
]

let tests_RenameAssay = testList "RenameAssay" [
testCase "not existing" <| fun _ ->
let i = ArcInvestigation("MyInvestigation")
i.InitAssay("OtherAssayName") |> ignore
let arc = ARC(isa = i)

let assayMoveF =
fun () -> arc.RenameAssay("MyOldAssay","MyNewAssay") |> ignore

Expect.throws assayMoveF "Should fail as arc does not contan assay with given name"
testCase "Basic" <| fun _ ->
let i = ArcInvestigation("MyInvestigation")
i.InitAssay("MyOldAssay") |> ignore
let arc = ARC(isa = i)
arc.GetWriteContracts() |> ignore
let contracts = arc.RenameAssay("MyOldAssay","MyNewAssay")
Expect.hasLength contracts 2 "Contract count is wrong"
let renameContract = contracts.[0]
Expect.equal renameContract.Operation Operation.RENAME "Rename contract operation"
Expect.equal "assays/MyOldAssay" renameContract.Path "Rename contract path"
let renameDTO = Expect.wantSome renameContract.DTO "Rename contract dto"
let expectedRenameDTO = DTO.Text "assays/MyNewAssay"
Expect.equal renameDTO expectedRenameDTO "Rename contract dto"
let updateContract = contracts.[1]
Expect.equal updateContract.Operation Operation.UPDATE "Update contract operation"
Expect.equal updateContract.Path "assays/MyNewAssay/isa.assay.xlsx" "Update contract path"
let updateDTO = Expect.wantSome updateContract.DTO "Update contract dto"
Expect.isTrue updateDTO.isSpreadsheet "Update contract dto"
let wb = updateDTO.AsSpreadsheet() :?> FsWorkbook
let updatedAssay = ARCtrl.Spreadsheet.ArcAssay.fromFsWorkbook wb
Expect.equal updatedAssay.Identifier "MyNewAssay" "Update contract Assay Identifier"
]


let main = testList "ARCtrl" [
tests_model
tests_updateFileSystem
tests_read_contracts
tests_writeContracts
tests_updateContracts
tests_RemoveAssay
tests_RenameAssay
payload_file_filters
]

0 comments on commit 57f3a2e

Please sign in to comment.