Skip to content
Snippets Groups Projects
Commit 3d731935 authored by Jean-Karim Heriche's avatar Jean-Karim Heriche
Browse files

Change handling of relations between Analysis objects and Datafile objects....

Change handling of relations between Analysis objects and Datafile objects. Add SQL to populate Screen_has_Plate and Screen_has_Analysis tables.
parent 53aaf1b8
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,51 @@ AnalysisHandle <- R6Class(
return(analysis)
} else { return(NULL) }
},
get_by_data = function(datafile, data_as_input = FALSE) {
if(data_as_input) {
# Analyses that consumed this data
query <- "SELECT A.* FROM Analysis AS A, Analysis_has_Data AS AhD
WHERE AhD.Parent_ID = ? AND AhD.Analysis_ID = A.ID"
data <- self$dbConnection$get_data(query, list(screen$ID))
if(length(data$ID)>0) {
analyses <- list()
df.list <- split(data, data$ID)
for(df in df.list) {
a <- Analysis$new(dbConnection = self$dbConnection,
ID = df$ID,
name = df$name,
version = df$version,
description = df$description,
outcome = df$outcome,
status = df$status,
code = df$code)
analyses <- c(analyses, a)
}
return(analyses)
} else { return(NULL) }
} else {
# Analysis that produced this data.
# There should be only one.
query <- "SELECT A.* FROM Analysis AS A, Analysis_has_Data AS AhD
WHERE AhD.Child_ID = ? AND AhD.Analysis_ID = A.ID"
df <- self$dbConnection$get_data(query, list(datafile$ID))
if(length(df$ID) == 1) {
analysis <- Analysis$new(dbConnection = self$dbConnection,
ID = df$ID,
name = df$name,
version = df$version,
description = df$description,
outcome = df$outcome,
status = df$status,
code = df$code)
return(analysis)
} else if(length(df$ID) == 0) {
return(NULL)
} else { # Shouldn't happen
stop("Datafile produced by more than one analysis")
}
}
},
get_all_by_screen = function(screen) {
query <- "SELECT A.* FROM Analysis AS A, Screen_has_Analysis AS ShA
WHERE ShA.Screen_ID = ? AND ShA.Analysis_ID = A.ID"
......@@ -46,7 +91,7 @@ AnalysisHandle <- R6Class(
get_all_by_plate = function(plate) {
query <- "SELECT A.* FROM Analysis AS A, Analysis_has_Data AS AhD, Plate_has_Data AS PhD
WHERE PhD.Plate_ID = ? AND
PhD.Datafile_ID = AhD.Datafile_ID AND
(PhD.Datafile_ID = AhD.Parent_ID OR PhD.Datafile_ID = AhD.Child_ID) AND
AhD.Analysis_ID = A.ID"
data <- self$dbConnection$get_data(query, list(plate$ID))
if(length(data$ID)>0) {
......@@ -78,7 +123,7 @@ AnalysisHandle <- R6Class(
},
store = function(analysis.list) {
statement <- "INSERT IGNORE INTO Analysis (ID, name, version, description, outcome, status, code)
VALUES (?, ?, ?, ?, ?, ?, ?)"
VALUES (?, ?, ?, ?, ?, ?, ?)"
for(analysis in analysis.list) {
result <- self$dbConnection$execute(statement, list(analysis$ID,
analysis$name,
......@@ -87,9 +132,25 @@ AnalysisHandle <- R6Class(
analysis$outcome,
analysis$status,
analysis$code))
if(length(analysis$datafiles)>0) {
if(length(analysis$input_datafiles)>0) {
dfh <- DatafileHandle$new(self$dbConnection)
result <- dfh$store(analysis$input_datafiles)
for(datafile in analysis$input_datafiles) {
st <- "INSERT IGNORE INTO Analysis_has_Data (Analysis_ID, Parent_ID)
VALUES (?, ?)"
r <- self$dbConnection$execute(st, list(plate$ID,
datafile$ID))
}
}
if(length(analysis$output_datafiles)>0) {
dfh <- DatafileHandle$new(self$dbConnection)
result <- dfh$store(analysis$datafiles)
result <- dfh$store(analysis$output_datafiles)
for(datafile in analysis$output_datafiles) {
st <- "INSERT IGNORE INTO Analysis_has_Data (Analysis_ID, Child_ID)
VALUES (?, ?)"
r <- self$dbConnection$execute(st, list(plate$ID,
datafile$ID))
}
}
}
}
......
......@@ -51,5 +51,41 @@ Datafile <- R6Class(
dfh <- DatafileHandle$new(self$dbConnection)
status <- dfh$store(list(self))
}
),
private = list(
analysis_object = NULL, # Analysis producing this datafile object
parent_list = NULL
),
active = list(
analysis = function(a) {
if(missing(a)) {
if(length(private$analysis_object) == 0) {
ah <- AnalysisHandle$new(self$dbConnection)
private$analysis_object <- ah$get_by_data(self)
}
return(private$analysis_object)
} else {
if(!("Analysis" %in% class(a))) {
stop("Datafile analysis must be an Analysis object.")
}
private$analysis_object <- a
}
},
parents = function(d) {
if(missing(d)) {
if(length(private$parent_list) == 0) {
dh <- DatafileHandle$new(self$dbConnection)
private$parent_list <- dh$get_parents(self)
}
return(private$parent_list)
} else {
for(item in d) {
if(!("Datafile" %in% class(item))) {
stop("Parent datafiles must be Datafile objects.")
}
}
private$parent_list <- d
}
}
)
)
\ No newline at end of file
......@@ -22,6 +22,55 @@ DatafileHandle <- R6Class(
type = df$type)
} else { return(NULL) }
},
get_parents = function(datafile) {
query <- "SELECT Parent_ID FROM Analysis_has_Data
WHERE Child_ID = ?"
data <- self$dbConnection$get_data(query, list(datafile$ID))
if(length(data$ID)>0) {
files <- list()
df.list <- split(data, data$ID)
for(df in df.list) {
datafile <- Datafile$new(dbConnection = self$dbConnection,
ID = df$ID,
filename = df$filename,
filepath = df$filepath,
format = df$format,
description = df$description,
date_created = df$date_created,
status = df$status,
type = df$type)
files <- c(files, datafile)
}
return(files)
} else { return(NULL) }
},
get_children = function(datafile, analysis = NULL) {
if(length(analysis) == 0 || !('Analysis' %in% class(analysis))) {
stop("get_children() method requires an Analysis object")
}
query <- "SELECT Child_ID FROM Analysis_has_Data
WHERE Parent_ID = ? AND Analysis_ID = ?"
data <- self$dbConnection$get_data(query, list(datafile$ID, analysis$ID))
if(length(data$ID)>0) {
files <- list()
df.list <- split(data, data$ID)
for(df in df.list) {
datafile <- Datafile$new(dbConnection = self$dbConnection,
ID = df$ID,
filename = df$filename,
filepath = df$filepath,
format = df$format,
description = df$description,
date_created = df$date_created,
status = df$status,
type = df$type)
datafile$analysis <- analysis
files <- c(files, datafile)
}
return(files)
} else { return(NULL) }
},
get_all_by_well = function(well) {
query <- "SELECT D.* FROM Datafile AS D, Well_has_Data AS WhD
WHERE WhD.Plate_ID = ? AND WhD.Well_position = ? AND
......@@ -48,8 +97,8 @@ DatafileHandle <- R6Class(
get_all_by_plate = function(plate) {
query <- "SELECT D.* FROM Datafile AS D, Plate_has_Data AS PhD
WHERE PhD.Plate_ID = ? AND D.ID = PhD.Datafile_ID"
df <- self$dbConnection$get_data(query, list(plate$ID))
if(length(df$ID)>0) {
data <- self$dbConnection$get_data(query, list(plate$ID))
if(length(data$ID)>0) {
files <- list()
df.list <- split(data, data$ID)
for(df in df.list) {
......@@ -70,13 +119,16 @@ DatafileHandle <- R6Class(
get_all_by_analysis = function(analysis, category = NULL) {
if(is.null(type)) {
query <- "SELECT D.* FROM Datafile AS D, Analysis_has_Data AS AhD
WHERE AhD.Analysis_ID = ? AND D.ID = AhD.Datafile_ID"
} else if(category %in% c('input', 'output')){
WHERE AhD.Analysis_ID = ? AND (D.ID = AhD.Parent_ID OR D.ID = AhD.Child_ID)"
} else if(category == 'input'){
query <- "SELECT D.* FROM Datafile AS D, Analysis_has_Data AS AhD
WHERE AhD.Analysis_ID = ? AND AhD.category = ? AND D.ID = AhD.Datafile_ID"
WHERE AhD.Analysis_ID = ? AND D.ID = AhD.Parent_ID"
} else if(category == 'output'){
query <- "SELECT D.* FROM Datafile AS D, Analysis_has_Data AS AhD
WHERE AhD.Analysis_ID = ? AND D.ID = AhD.Child_ID"
}
df <- self$dbConnection$get_data(query, list(analysis$ID))
if(length(df$ID)>0) {
data <- self$dbConnection$get_data(query, list(analysis$ID))
if(length(data$ID)>0) {
files <- list()
df.list <- split(data, data$ID)
for(df in df.list) {
......@@ -107,7 +159,7 @@ DatafileHandle <- R6Class(
store = function(datafile.list) {
statement <- "INSERT IGNORE INTO Datafile (ID, filepath, filename, description, format,
type, date_created, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
for(datafile in datafile.list) {
result <- self$dbConnection$execute(statement, list(datafile$ID,
datafile$filepath,
......@@ -117,6 +169,29 @@ DatafileHandle <- R6Class(
datafile$type,
datafile$date_created,
datafile$status))
if(length(datafile$parents)>0) {
rslt <- self$store(datafile$parents)
for(parent in datafile$parents) {
if(length(datafile$analysis) == 1) {
st <- "INSERT IGNORE INTO Analysis_has_Data (Child_ID, Parent_ID, Analysis_ID)
VALUES (?, ?, ?)"
r <- self$dbConnection$execute(st, list(datafile$ID,
parent$ID,
datafile$analysis$ID))
} else {
stop("Datafile must have an Analysis object")
}
}
}
if(length(datafile$children)>0) {
rslt <- self$store(datafile$children)
for(child in datafile$children) {
st <- "INSERT IGNORE INTO Analysis_has_Data (Analysis_ID, Child_ID)
VALUES (?, ?)"
r <- self$dbConnection$execute(st, list(datafile$ID,
child$ID))
}
}
}
}
)
......
......@@ -55,7 +55,7 @@ PlateHandle <- R6Class(
},
store = function(plate.list) {
statement <- "INSERT IGNORE INTO Plate (ID, nrows, ncols, name, type, date_prepared, date_used)
VALUES (?, ?, ?, ?, ?, ?, ?)"
VALUES (?, ?, ?, ?, ?, ?, ?)"
for(plate in plate.list) {
result <- self$dbConnection$execute(statement, list(plate$ID,
plate$nrows,
......@@ -73,7 +73,7 @@ PlateHandle <- R6Class(
result <- dfh$store(plate$datafiles)
for(datafile in plate$datafiles) {
st <- "INSERT IGNORE INTO Plate_has_Data (Plate_ID, Datafile_ID)
VALUES (?, ?)"
VALUES (?, ?)"
r <- self$dbConnection$execute(st, list(plate$ID,
datafile$ID))
}
......@@ -83,7 +83,7 @@ PlateHandle <- R6Class(
result <- qch$store(plate$qc)
for(qc in plate$qc) {
st <- "INSERT IGNORE INTO Plate_has_QC (Plate_ID, QC_ID)
VALUES (?, ?)"
VALUES (?, ?)"
r <- self$dbConnection$execute(st, list(plate$ID,
qc$ID))
}
......
......@@ -57,7 +57,7 @@ ScreenHandle <- R6Class(
},
store = function(screen.list) {
statement <- "INSERT IGNORE INTO Screen (ID, name, description, status, date_started, date_ended)
VALUES (?, ?, ?, ?, ?, ?)"
VALUES (?, ?, ?, ?, ?, ?)"
for(screen in screen.list) {
result <- self$dbConnection$execute(statement, list(screen$ID,
screen$name,
......@@ -68,10 +68,20 @@ ScreenHandle <- R6Class(
if(length(screen$plates)>0) {
plh <- PlateHandle$new(self$dbConnection)
result <- plh$store(screen$plates)
for(plate in screen$plates) {
st <- "INSERT IGNORE INTO Screen_has_Plate (Screen_ID, Plate_ID)
VALUES (?, ?)"
r <- self$dbConnection$execute(st, list(screen$ID, plate$ID))
}
}
if(length(screen$analyses)>0) {
ah <- AnalysisHandle$new(self$dbConnection)
result <- ah$store(screen$analyses)
for(analysis in screen$analyses) {
st <- "INSERT IGNORE INTO Screen_has_Analysis (Screen_ID, Analysis_ID)
VALUES (?, ?)"
r <- self$dbConnection$execute(st, list(screen$ID, analysis$ID))
}
}
}
}
......
......@@ -34,7 +34,7 @@ WellHandle <- R6Class(
},
store = function(well.list) {
statement <- "INSERT IGNORE INTO Well (Plate_ID, position, label, temperature)
VALUES (?, ?, ?, ?)"
VALUES (?, ?, ?, ?)"
for(well in well.list) {
result <- self$dbConnection$execute(statement, list(well$plate_ID,
well$position,
......@@ -45,7 +45,7 @@ WellHandle <- R6Class(
result <- dfh$store(well$datafiles)
for(datafile in well$datafiles) {
st <- "INSERT IGNORE INTO Well_has_Data (Plate_ID, Well_position, Datafile_ID)
VALUES (?, ?, ?)"
VALUES (?, ?, ?)"
r <- self$dbConnection$execute(st, list(well$plate_ID,
well$position,
datafile$ID))
......@@ -56,7 +56,7 @@ WellHandle <- R6Class(
result <- qch$store(well$qc)
for(qc in well$qc) {
st <- "INSERT IGNORE INTO Well_has_QC (Plate_ID, Well_position, QC_ID)
VALUES (?, ?, ?)"
VALUES (?, ?, ?)"
r <- self$dbConnection$execute(st, list(well$plate_ID,
well$position,
qc$ID))
......@@ -68,7 +68,7 @@ WellHandle <- R6Class(
for(compound in well$compounds) {
st <- "INSERT IGNORE INTO Well_has_Compound (Plate_ID, Well_position,
Compound_ID, dose, unit)
VALUES (?, ?, ?, ?, ?)"
VALUES (?, ?, ?, ?, ?)"
r <- self$dbConnection$execute(st, list(well$plate_ID,
well$position,
compound$ID,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment