Skip to content

Commit

Permalink
Fixes for data cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrug committed Sep 25, 2023
1 parent 71f28da commit 3f93fa6
Show file tree
Hide file tree
Showing 17 changed files with 464 additions and 68 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: LEEF.2.measurement.flowcytometer
Type: Package
Title: What the Package Does (Title Case)
Version: 0.9.5
Version: 0.9.7
Authors@R: c(
person(given = "Rainer M.", family = "Krug", email = "[email protected]", role = c("aut", "cre")),
person("SNF Project 310030_188431", role = "fnd")
Expand Down
9 changes: 7 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Generated by roxygen2: do not edit by hand

export(add_new_data)
export(calculate_gates)
export(calculate_gates_H)
export(dens)
export(dens_H)
export(extract_traits)
export(extractor_flowcytometer)
export(extractor_flowcytometer_gating)
export(extractor_flowcytometer_density)
export(extractor_flowcytometer_preparation)
export(extractor_flowcytometer_traits)
export(gating)
export(extractor_traits)
export(load_parameter)
export(par_template)
export(pre_processor_flowcytometer)
Expand Down
16 changes: 8 additions & 8 deletions R/dens.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dens <- function(
# #----- HAVING A LOOK AT THE GATING -----#

# i <- 1
# flowViz::xyplot(`FL3-A` ~ `FL1-A`, data = fsa[[i]], filter = bacteria_gate)
# flowViz::xyplot(`FL3-A` ~ `FL1-A`, data = fsa[[i]], filter = gates$bacteria$bacteria_gate)
# # fsa[[i]]
# flowViz::densityplot(~ `FL1-A`, data = fsa[[i]], filter = rg_LNA)
# # fsa[[i]]
Expand All @@ -42,7 +42,7 @@ dens <- function(

# applying filter to whole flowSet

result <- flowCore::filter(fsa, bacteria_gate)
result <- flowCore::filter(fsa, gates$bacteria$bacteria_gate)

# extract absolute counts
l <- lapply(result, flowCore::summary)
Expand All @@ -63,12 +63,12 @@ dens <- function(
flow.data <- flow.data[order(flow.data$date, flow.data$sample_letter, flow.data$sample_number), ]

# subset data based on gate for bacteria
subset.bacteria <- flowCore::Subset(fsa, bacteria_gate)
subset.bacteria <- flowCore::Subset(fsa, gates$bacteria$bacteria_gate)

# applying filter to bacteria to get the three bacteria populations
LNA <- flowCore::filter(subset.bacteria, rg_LNA)
MNA <- flowCore::filter(subset.bacteria, rg_MNA)
HNA <- flowCore::filter(subset.bacteria, rg_HNA)
LNA <- flowCore::filter(subset.bacteria, gates$bacteria$rg_LNA)
MNA <- flowCore::filter(subset.bacteria, gates$bacteria$rg_MNA)
HNA <- flowCore::filter(subset.bacteria, gates$bacteria$rg_HNA)

# extract absolute counts
l_LNA <- lapply(LNA, flowCore::summary)
Expand Down Expand Up @@ -104,8 +104,8 @@ dens <- function(

# get the algae
algae <- flowCore::filter(
flowCore::Subset(fsa, !bacteria_gate),
algae_gate
flowCore::Subset(fsa, !gates$bacteria$bacteria_gate),
gates$algae$algae_gate
)

# extract absolute counts
Expand Down
8 changes: 4 additions & 4 deletions R/dens_H.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dens_H <- function(
# #----- HAVING A LOOK AT THE GATING -----#

# i <- 1
# flowViz::xyplot(`FL3-A` ~ `FL1-A`, data = fsa[[i]], filter = bacteria_gate)
# flowViz::xyplot(`FL3-A` ~ `FL1-A`, data = fsa[[i]], filter = gates$bacteria$bacteria_gate)
# # fsa[[i]]
# flowViz::densityplot(~ `FL1-A`, data = fsa[[i]], filter = rg_LNA)
# # fsa[[i]]
Expand All @@ -42,7 +42,7 @@ dens_H <- function(

# applying filter to whole flowSet

result <- flowCore::filter(fsa, bacteria_gate)
result <- flowCore::filter(fsa, gates$bacteria$gates$bacteria$bacteria_gate)

# extract absolute counts
l <- lapply(result, flowCore::summary)
Expand All @@ -63,7 +63,7 @@ dens_H <- function(
flow.data <- flow.data[order(flow.data$date, flow.data$sample_letter, flow.data$sample_number), ]

# subset data based on gate for bacteria
subset.bacteria <- flowCore::Subset(fsa, bacteria_gate)
subset.bacteria <- flowCore::Subset(fsa, gates$bacteria$bacteria_gate)

# applying filter to bacteria to get the three bacteria populations
LNA <- flowCore::filter(subset.bacteria, rg_LNA)
Expand Down Expand Up @@ -104,7 +104,7 @@ dens_H <- function(

# get the algae
algae <- flowCore::filter(
flowCore::Subset(fsa, !bacteria_gate),
flowCore::Subset(fsa, !gates$bacteria$bacteria_gate),
algae_gate
)

Expand Down
190 changes: 190 additions & 0 deletions R/extract_traits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#' Extract flowcytometer traits
#'
#' NOT USED IN PIPLELINE!!!!
#' This function is extracting data to be added to the database
#' (and therefore make accessible for further analysis and forecasting)
#' from \code{.fcs} files.
#'
#' @param input directory from which to read the data
#' @param metadata_flowcytometer the content of the file
#' \code{metadata_flowcytometer.csv} which will be linked into the traits
#' @param min_FSC.A numeric. If \code{!NULL}, \code{FSA.A <= min_FSC.A} will be fitered out by using
#' a rectangular filter
#' \code{flowCore::rectangleGate(filterId="filter_out_0", "FSC-A" = c(min_FSC.A, +Inf))}
#' @param use_H if \code{TRUE}, gating will be done using \code{height},
#' otherwie \code{area}
#' @param timestamp timestamp. Default: read from \code{sample_metadata.yml}
#' @param fsa_p1 if \code{NULL}, \code{fsa_p1} will be read in, otherwise the
#' \code{fsa}
#' @param fsa_p2 if \code{NULL}, \code{fsa_p2} will be read in, otherwise the
#' \code{fsa}
#' @param gates_coordinates if \code{NULL}, \code{gates_coordinates} will be
#' read in, otherwise the \code{gates_coordinates}
#' @param wellid_keyword the kwyword which is used to identify the well ID.
#' Usually "$WELLID" (default), but for the EAWAG Flowcytometer it is "$SMNO".
#'
#' @return invisibly \code{TRUE} when completed successful
#'
#' @importFrom flowCore read.flowSet pData phenoData exprs logTransform truncateTransform transform
#' @importFrom flowCore rectangleGate polygonGate Subset
#' @importFrom yaml read_yaml
#' @importFrom stats setNames
#' @importFrom utils read.csv write.csv
#' @importFrom plyr join
#' @importFrom tidyr pivot_longer
#' @importFrom magrittr %>%
#' @import loggit
#' @export
#'
extract_traits <- function(
input = NULL,
particles = c("bacteria", "LNA", "MNA", "HNA", "algae"),
metadata_flowcytometer,
min_FSC.A = NULL,
use_H = FALSE,
timestamp = yaml::read_yaml(file.path(input, "sample_metadata.yml"))$timestamp,
fsa_p1 = NULL,
fsa_p2 = NULL,
gates_coordinates = NULL,
wellid_keyword = "$WELLID") {
message(" reading data ...")

if (is.null(fsa_p1)) {
fsa_p1 <- readRDS(file = file.path(input, paste0("flowcytometer_fsa_ungated.p_1.rds")))
}

if (is.null(fsa_p2)) {
fsa_p2 <- readRDS(file = file.path(input, paste0("flowcytometer_fsa_ungated.p_1.rds")))
}

if (!is.null(min_FSC.A)) {
g0 <- flowCore::rectangleGate(filterId = "filter_out_0", "FSC-A" = c(min_FSC.A, +Inf))
fsa_p1 <- flowCore::Subset(fsa_p1, g0)
fsa_p2 <- flowCore::Subset(fsa_p2, g0)
}

if (is.null(gates_coordinates)) {
gates_coordinates <- utils::read.csv(file.path(input, "gates_coordinates.csv"))
}

if (use_H) {
gates <- calculate_gates_H(gates_coordinates = gates_coordinates)
} else {
gates <- calculate_gates(gates_coordinates = gates_coordinates)
}



# function to gate each plate ---------------------------------------------

trait_plate <- function(plate_id,
fsa,
gates = gates,
wellid_keyword = wellid_keyword,
timestamp = timestamp) {
# extraction function -----------------------------------------------------

extr_traits <- function(pop,
wellid_keyword = wellid_keyword,
timestamp = timestamp) {
traits <- flowCore::fsApply(
pop,
function(p) {
result <- list()
result$sample <- unlist(flowCore::keyword(p, wellid_keyword))
result$plate <- plate_id
x <- exprs(p)
if (nrow(x) > 0) {
result <- suppressWarnings(
data.frame(
result,
x
)
)
} else {
result <- NULL
}
return(result)
}
)

traits <- do.call(rbind, traits)
traits$timestamp <- timestamp
return(traits)
}

traits <- list()
# The Bacteria ------------------------------------------------------------

bacteria_pop <- Subset(fsa, gates$bacteria$bacteria_gate)

# LNA_pop <- Subset(bacteria_pop, gates$bacteria$rg_LNA)
# MNA_pop <- Subset(bacteria_pop, gates$bacteria$rg_MNA)
# HNA_pop <- Subset(bacteria_pop, gates$bacteria$rg_HNA)

traits$bacteria <- extr_traits(bacteria_pop, wellid_keyword = wellid_keyword, timestamp = timestamp)

# The Algae ---------------------------------------------------------------

algae_pop <- Subset(fsa, gates$algae$algae_gate)

traits$algae <- extr_traits(bacteria_pop, wellid_keyword = wellid_keyword, timestamp = timestamp)

return(traits)
}


# Do the trait extraction for all plates --------------------------------------------

p1 <- trait_plate(
plate_id = "p_1",
fsa = fsa_p1,
gates = gates,
timestamp = timestamp,
wellid_keyword = wellid_keyword
)
p2 <- trait_plate(
plate_id = "p_2",
fsa = fsa_p2,
gates = gates,
timestamp = timestamp,
wellid_keyword = wellid_keyword
)

traits <- list()

traits$bacteria <- rbind(
p1$bacteria,
p2$bacteria
)
traits$algae <- rbind(
p1$algae,
p2$algae
)

traits$bacteria <- merge(
traits$bacteria,
metadata_flowcytometer,
by.x = c("sample", "plate"),
by.y = c("sample", "plate"),
all.x = TRUE,
all.y = FALSE
)
traits$algae <- merge(
traits$algae,
metadata_flowcytometer,
by.x = c("sample", "plate"),
by.y = c("sample", "plate"),
all.x = TRUE,
all.y = FALSE
)



# Finalize ----------------------------------------------------------------

message(" done")
message("########################################################")

return(traits)
}
58 changes: 37 additions & 21 deletions R/extractor_flowcytometer_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,45 @@ extractor_flowcytometer_density <- function(
#############################################################
#############################################################

if (is.null(gates_coordinates)) {
gates_coordinates <- utils::read.csv(file.path(input, "flowcytometer", "gates_coordinates.csv"))
}

if (is.null(fsa)) {
fsa <- readRDS(file.path(output, "flowcytometer", "flowcytometer_fsa_ungated.rds"))
}

if (is.null(flow.data)) {
flow.data <- utils::read.csv(file.path(output, "flowcytometer", "flowcytometer_ungated.csv"))
}

#############################################################
#############################################################

# function to gate each plate ---------------------------------------------

gate_plate <- function(plate,
input,
output,
use_H) {
gate_plate <- function(
plate,
input,
output,
use_H,
gates_coordinates,
fsa = NULL,
flow.data = NULL) {
if (is.null(gates_coordinates)) {
gates_coordinates <- utils::read.csv(file.path(input, "flowcytometer", "gates_coordinates.csv"))
}

if (is.null(fsa)) {
fsa <- readRDS(file.path(output, "flowcytometer", paste0("flowcytometer_fsa_ungated.", plate, ".rds")))
}

if (is.null(flow.data)) {
flow.data <- readRDS(file.path(output, "flowcytometer", paste0("flowcytometer_ungated.", plate, ".rds")))
}

if (use_H) {
gated <- dens_H(
gates_coordinates = utils::read.csv(file.path(input, "flowcytometer", "gates_coordinates.csv")),
fsa = file.path(file.path(output, "flowcytometer"), paste0("flowcytometer_fsa_ungated.", plate, ".rds")),
flow.data = file.path(file.path(output, "flowcytometer"), paste0("flowcytometer_ungated.", plate, ".rds"))
gates_coordinates = gates_coordinates,
fsa = fsa,
flow.data = flow.data
)
} else {
gated <- dens(
gates_coordinates = utils::read.csv(file.path(input, "flowcytometer", "gates_coordinates.csv")),
fsa = file.path(file.path(output, "flowcytometer"), paste0("flowcytometer_fsa_ungated.", plate, ".rds")),
flow.data = file.path(file.path(output, "flowcytometer"), paste0("flowcytometer_ungated.", plate, ".rds"))
gates_coordinates = gates_coordinates,
fsa = fsa,
flow.data = flow.data
)
}
# SAVE --------------------------------------------------------------------
Expand Down Expand Up @@ -133,7 +140,11 @@ extractor_flowcytometer_density <- function(
plates,
gate_plate,
input = input,
output = output
output = output,
use_H = use_H,
gates_coordinates,
fsa = fsa,
flow.data = flow.data
)

# Finalise ----------------------------------------------------------------
Expand Down Expand Up @@ -172,7 +183,12 @@ extractor_flowcytometer_density <- function(
message("########################################################")

if (dens_back) {
return(flow.data)
density <- NULL
for (plate in plates) {
fdp <- readRDS(file.path(add_path, paste0("flowcytometer_density.", plate, ".rds")))
density <- rbind(density, fdp)
}
return(density)
} else {
invisible(TRUE)
}
Expand Down
Loading

0 comments on commit 3f93fa6

Please sign in to comment.