Skip to content

Commit

Permalink
new version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Russel committed Jan 18, 2018
1 parent 2826e53 commit 359e9ce
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,6 +1,6 @@
Package: RCon3D
Title: Analysis of Confocal Images of Microbial Biofilms
Version: 1.2.1
Version: 1.2.2
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 3D aggregates.
Depends: R (>= 3.2.5)
Expand Down
12 changes: 7 additions & 5 deletions R/clumps.R
Expand Up @@ -13,7 +13,7 @@
#' @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
#' @param coords Logical. Return coordinates of the centroids of each aggregate. This can be somewhat time-consuming if there are many aggregates
#' @keywords array image aggregate
#' @return A list with two parts. First part is a dataframe with ID, size of aggregates in pixels, size of aggregates in microns if pwidth and zstep are provided, coordinates if coords is TRUE, if coords is TRUE also a variable, Edge, indicating how many pixels of each aggregate is on the edge of the image, 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
#' @return A list with two parts. First part is a dataframe with ID, size of aggregates in pixels, size of aggregates in microns if pwidth and zstep are provided, coordinates if coords is TRUE, if coords is TRUE also variables, Edge.x, Edge.y and Edge.z, indicating how many pixels of each aggregate is on the edge of the image, 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

Expand All @@ -31,7 +31,7 @@ clumps <- function(imgs,channel,kern.neighbour=c(3,3,3),type.neighbour="box",ker
# For each replica
for(k in 1:length(ch_files)) {

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

# Load
ch_t <- readRDS(ch_files[k])
Expand Down Expand Up @@ -73,11 +73,13 @@ clumps <- function(imgs,channel,kern.neighbour=c(3,3,3),type.neighbour="box",ker
edge.y <- c(1,dim(ch_agg)[2])
edge.z <- c(1,dim(ch_agg)[3])

touch <- sapply(coordsl, function(ac) sum(sum(sapply(ac[,1], function(x) x %in% edge.x)),
touch <- t(sapply(coordsl, function(ac) c(sum(sapply(ac[,1], function(x) x %in% edge.x)),
sum(sapply(ac[,2], function(x) x %in% edge.y)),
sum(sapply(ac[,3], function(x) x %in% edge.z))))
sum(sapply(ac[,3], function(x) x %in% edge.z)))))

afr$Edge <- touch
colnames(touch) <- c("Edge.x","Edge.y","Edge.y")

afr <- cbind(afr, touch)
}

results[[k]] <- afr
Expand Down
13 changes: 11 additions & 2 deletions R/findIMG.R
Expand Up @@ -2,15 +2,24 @@
#'
#' Find arrays already created by the \code{loadIMG} or \code{tiff_to_array} functions
#' @param path The path of the _Array.R files
#' @param channels Optional. Channel names to look for in the image files
#' @keywords tif array image
#' @return The paths for the array files
#' @export

findIMG <- function(path){
findIMG <- function(path, channels = NULL){

# Find the files
files <- list.files(path, "_Array.R", full.names = T)


if(!is.null(channels)){
files.new <- c()
for(i in seq_along(channels)){
files.new <- c(files.new,files[grep(channels[i],files)])
}
files <- files.new
}

return(files)

}
34 changes: 16 additions & 18 deletions R/merge_channels.R
@@ -1,48 +1,46 @@
#' Create RDS for a combination of channels
#'
#' @param path The path of the _Array.R files
#' @param path The path of where to create the _Array.R files
#' @param imgs The paths for the images to merge
#' @param channels Character vector with names of the channels. Channel names should be in the names of the .tif files
#' @param method How to merge. Either "union" (presence is coded if at least one channel is present), "intersect" (presence is coded only if all channels are present), or "subtract" (presence is coded as those in the first channel but not in the second channel).
#' @keywords array image
#' @return Creates arrays as RDS files in the specified path, and outputs the paths for these files
#' @return Creates arrays as RDS files in the specified path, and outputs the paths for image files containing the name of the method and channels
#' @export

merge_channels <- function(path, channels, method){
merge_channels <- function(path, imgs, channels, method){

setwd(path)

# Find the files
files <- list.files(path, "_Array.R", full.names = T)
files <- imgs
files.l <- lapply(channels, function(x) files[grep(x, files)])
if(length(grep("RCon3D", files.l)) > 0){
files.l <- lapply(1:length(channels), function(x) files.l[[x]][-grep("RCon3D", files.l[[x]])])
}


# For each image
for(k in 1:length(files.l[[1]])){

imgs <- lapply(files.l, function(x) readRDS(x[[k]]))
imags <- lapply(files.l, function(x) readRDS(x[[k]]))

if(method == "union"){
img <- do.call("+",imgs)
img <- do.call("+",imags)
img[img != 0] <- 1
saveRDS(img, file = gsub(channels[1],paste0("RCon3D.union.",channels[1],".",channels[2]),files.l[[1]][k]))
files <- list.files(path, "RCon3D.union.", full.names = T)
saveRDS(img, file = gsub(channels[1],paste0("RCon3D.",channels[1],".",channels[2],".union"),files.l[[1]][k]))
files <- list.files(path, paste0("RCon3D.",channels[1],".",channels[2],".union"), full.names = T)
}

if(method == "intersect"){
img <- do.call("+",imgs)
img <- do.call("+",imags)
img[img != length(channels)] <- 0
img[img == length(channels)] <- 1
saveRDS(img, file = gsub(channels[1],paste0("RCon3D.intersect.",channels[1],".",channels[2]),files.l[[1]][k]))
files <- list.files(path, "RCon3D.intersect.", full.names = T)
saveRDS(img, file = gsub(channels[1],paste0("RCon3D.",channels[1],".",channels[2],".intersect"),files.l[[1]][k]))
files <- list.files(path, paste0("RCon3D.",channels[1],".",channels[2],".intersect"), full.names = T)
}

if(method == "subtract"){
img <- imgs[[1]] - imgs[[2]]
img <- imags[[1]] - imags[[2]]
img[img == -1] <- 0
saveRDS(img, file = gsub(channels[1],paste0("RCon3D.subtract.",channels[1],".",channels[2]),files.l[[1]][k]))
files <- list.files(path, "RCon3D.subtract.", full.names = T)
saveRDS(img, file = gsub(channels[1],paste0("RCon3D.",channels[1],".",channels[2],".subtract"),files.l[[1]][k]))
files <- list.files(path, paste0("RCon3D.",channels[1],".",channels[2],".subtract"), full.names = T)
}

}
Expand Down
2 changes: 1 addition & 1 deletion man/clumps.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/findIMG.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions man/merge_channels.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 359e9ce

Please sign in to comment.