Skip to content

Commit

Permalink
New package version!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Russel committed Mar 4, 2017
1 parent 7b368f7 commit 6ce96c0
Show file tree
Hide file tree
Showing 27 changed files with 915 additions and 914 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Package: RCon3D
Title: Analysis of Confocal Images of Microbial Biofilms
Version: 0.0.0.9000
Version: 1.0.0.9000
Authors@R: person("Jakob", "Russel", email = "russel2620@gmail.com", role = c("aut", "cre"))
Description: Various 3D analyses of confocal images. E.g. Quantification, co-aggregation and identifying aggregates.
Description: Various 3D analyses of confocal images. E.g. Quantification, co-aggregation and identifying 3D aggregates.
Depends: R (>= 3.2.5)
Imports:
foreach,
doParallel,
mmand,
tiff
Suggests:
tiff,
rootSolve
License: MIT
Encoding: UTF-8
Expand Down
24 changes: 12 additions & 12 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Generated by roxygen2: do not edit by hand

export(Agg)
export(CreateNulls)
export(CreateRandom)
export(CrossCor)
export(CrossCor.default)
export(CrossRatio)
export(CrossRatio.default)
export(ELayers)
export(LayerSplit)
export(LayerStd)
export(Quantify)
export(clumps)
export(co_agg)
export(co_agg.default)
export(create_nulls)
export(create_random)
export(cross_ratio)
export(cross_ratio.default)
export(extract_layers)
export(findIMG)
export(layer_split)
export(layer_stand)
export(loadIMG)
export(tiffToArray)
export(quant)
export(tiff_to_array)
import(doParallel)
import(foreach)
import(mmand)
Expand Down
190 changes: 95 additions & 95 deletions R/Agg.R → R/clumps.R
Original file line number Diff line number Diff line change
@@ -1,95 +1,95 @@
#' Find 3D aggregates
#'
#' Function to group adjacent pixels in aggregates
#' @param imgs The paths of array files; i.e. output from loadIMG or findIMG functions.
#' @param channel Name of the channel to find aggregates in. Should be in the names of the array files
#' @param kern.neighbour Numeric vector indicating range of neighbouring pixels to aggregate in the x,y,z directions. Has to be odd intergers. c(1,1,1) means no aggregating.
#' @param kern.smooth Optional. Numeric vector indicating range of median smoothing in the x,y,z directions. Has to be odd intergers. c(1,1,1) means no smoothing.
#' @param layers Optional. Should the function only look in a subset of layers. A list with lists of layers to use for each image. Can also be the output from ELayers
#' @param pwidth Optional. Width of pixels in microns to calculate aggregate size in microns instead of pixels
#' @param zstep Optional. z-step in microns to calculate aggregate size in microns instead of pixels
#' @param naming Optional. Add metadata to the output dataframe by looking through names of array files. Should be a list of character vectors, each list element will be added as a variable. Example: naming=list(Time=c("T0","T1","T2")). The function inserts a variable called Time, and then looks through the names of the array files and inserts characters mathcing either T0, T1 or T2
#' @keywords array image aggregate
#' @return A list with two parts. First part is a dataframe with ID and size of aggregates and name of image, second part is a list of the arrays in which pixels are NA if empty or given a number indicating the aggregate ID
#' @import mmand
#' @export

Agg <- function(imgs,channel,kern.neighbour=c(3,3,3),kern.smooth=NULL,layers=NULL,pwidth=NULL,zstep=NULL,naming=NULL) {

# Load image
ch_files <- imgs[grep(channel, imgs)]

# Lists for results
arrays <- list()
results <- list()

# For each replica
for(k in 1:length(ch_files)) {

message(paste("Running replica",k))

# Load
ch_t <- readRDS(ch_files[k])
side <- dim(ch_t)[1]
h <- dim(ch_t)[3]

# Subset layers
if(!is.null(layers[[k]])){
ch_t <- ch_t[,,layers[[k]]]
}

# Extend parameter
ep <- max(c(kern.neighbour,kern.smooth))

# Extend
ch_new <- array(NA, dim=c(side+(2*ep), side+(2*ep), h+(2*ep)))
ch_new[(ep+1):(side+ep),(ep+1):(side+ep),(ep+1):(h+ep)] <- ch_t

# Smooth
if(!is.null(kern.smooth)) {
if(kern.smooth[1] %% 1 == 0 & kern.smooth[1] %% 2 != 0 &
kern.smooth[2] %% 1 == 0 & kern.smooth[2] %% 2 != 0 &
kern.smooth[3] %% 1 == 0 & kern.smooth[3] %% 2 != 0) {
kern.s <- kernelArray(array(1,dim=kern.smooth))
ch_new <- medianFilter(ch_new,kern.s)
ch_new[ch_new > 0] <- 1 } else stop("Kernel smooth has to be odd integers in all directions")
}

# Find aggregates
kern.n <- kernelArray(array(1,dim=kern.neighbour))
ch_agg <- components(ch_new,kern.n)

# Crop
ch_fin <- ch_agg[(ep+1):(side+ep),(ep+1):(side+ep),(ep+1):(h+ep)]

afr <- as.data.frame(table(ch_fin))
colnames(afr) <- c("ID","Size")
if(!is.null(pwidth) & !is.null(zstep)) afr$Size.micron <- afr$Size * pwidth^2 * zstep
afr$Img <- sub(paste0("_.*"),"",sub(".*/", "", ch_files[k]))
results[[k]] <- afr
arrays[[k]] <- ch_fin
}

afrx <- do.call(rbind,results)

if(!is.null(naming)){
afrxn <- afrx
for(i in 1:length(naming)){
name.temp <- naming[[i]]
afrxn <- cbind(afrxn,NA)
for(j in 1:length(name.temp)){
afrxn[grep(name.temp[j],afrxn$Img),ncol(afrx)+i] <- name.temp[j]
}
}

colnames(afrxn) <- c(colnames(afrx),names(naming))

} else afrxn <- afrx

All <- list(Aggregates=afrxn,Arrays=arrays)

return(All)
}



#' Find 3D aggregates/clumps
#'
#' Function to group adjacent pixels in aggregates/clumps
#' @param imgs The paths of array files; i.e. output from \code{loadIMG} or \code{findIMG} functions.
#' @param channel Name of the channel to find aggregates in. Should be in the names of the array files
#' @param kern.neighbour Numeric vector indicating range of neighbouring pixels to aggregate in the x,y,z directions. Has to be odd intergers. c(1,1,1) means no aggregating.
#' @param kern.smooth Optional. Numeric vector indicating range of median smoothing in the x,y,z directions. Has to be odd intergers. c(1,1,1) means no smoothing.
#' @param layers Optional. Should the function only look in a subset of layers. A list with lists of layers to use for each image. Can also be the output from \code{extract_layers}
#' @param pwidth Optional. Width of pixels in microns to calculate aggregate size in microns instead of pixels
#' @param zstep Optional. z-step in microns to calculate aggregate size in microns instead of pixels
#' @param naming Optional. Add metadata to the output dataframe by looking through names of array files. Should be a list of character vectors, each list element will be added as a variable. Example: naming=list(Time=c("T0","T1","T2")). The function inserts a variable called Time, and then looks through the names of the array files and inserts characters mathcing either T0, T1 or T2
#' @keywords array image aggregate
#' @return A list with two parts. First part is a dataframe with ID and size of aggregates and name of image, second part is a list of the arrays in which pixels are NA if empty or given a number indicating the aggregate ID
#' @import mmand
#' @export

clumps <- function(imgs,channel,kern.neighbour=c(3,3,3),kern.smooth=NULL,layers=NULL,pwidth=NULL,zstep=NULL,naming=NULL) {

# Load image
ch_files <- imgs[grep(channel, imgs)]

# Lists for results
arrays <- list()
results <- list()

# For each replica
for(k in 1:length(ch_files)) {

message(paste("Running replica",k))

# Load
ch_t <- readRDS(ch_files[k])
side <- dim(ch_t)[1]
h <- dim(ch_t)[3]

# Subset layers
if(!is.null(layers[[k]])){
ch_t <- ch_t[,,layers[[k]]]
}

# Extend parameter
ep <- max(c(kern.neighbour,kern.smooth))

# Extend
ch_new <- array(NA, dim=c(side+(2*ep), side+(2*ep), h+(2*ep)))
ch_new[(ep+1):(side+ep),(ep+1):(side+ep),(ep+1):(h+ep)] <- ch_t

# Smooth
if(!is.null(kern.smooth)) {
if(kern.smooth[1] %% 1 == 0 & kern.smooth[1] %% 2 != 0 &
kern.smooth[2] %% 1 == 0 & kern.smooth[2] %% 2 != 0 &
kern.smooth[3] %% 1 == 0 & kern.smooth[3] %% 2 != 0) {
kern.s <- kernelArray(array(1,dim=kern.smooth))
ch_new <- medianFilter(ch_new,kern.s)
ch_new[ch_new > 0] <- 1 } else stop("Kernel smooth has to be odd integers in all directions")
}

# Find aggregates
kern.n <- kernelArray(array(1,dim=kern.neighbour))
ch_agg <- components(ch_new,kern.n)

# Crop
ch_fin <- ch_agg[(ep+1):(side+ep),(ep+1):(side+ep),(ep+1):(h+ep)]

afr <- as.data.frame(table(ch_fin))
colnames(afr) <- c("ID","Size")
if(!is.null(pwidth) & !is.null(zstep)) afr$Size.micron <- afr$Size * pwidth^2 * zstep
afr$Img <- sub(paste0("_.*"),"",sub(".*/", "", ch_files[k]))
results[[k]] <- afr
arrays[[k]] <- ch_fin
}

afrx <- do.call(rbind,results)

if(!is.null(naming)){
afrxn <- afrx
for(i in 1:length(naming)){
name.temp <- naming[[i]]
afrxn <- cbind(afrxn,NA)
for(j in 1:length(name.temp)){
afrxn[grep(name.temp[j],afrxn$Img),ncol(afrx)+i] <- name.temp[j]
}
}

colnames(afrxn) <- c(colnames(afrx),names(naming))

} else afrxn <- afrx

All <- list(Aggregates=afrxn,Arrays=arrays)

return(All)
}



Loading

0 comments on commit 6ce96c0

Please sign in to comment.