Commit 2cc7dfec authored by Lucy McNeill's avatar Lucy McNeill
Browse files

auto_crop_fast outputs a folder called crops-RGB with strand and foci channel

parent 2a5797c8
No preview for this file type
......@@ -48,6 +48,7 @@
#' channel illuminating cell structures. Defaults to DAPI, if
#' third channel == "on".
#' @param third_channel Optional, defaults to "off".
#' @param strand_amp multiplication of strand channel for get_blobs function.
#' @param file_ext file extension of your images e.g. tif jpeg or png.
#' @examples demo_path = paste0(system.file("extdata",package = "synapsis"))
#' auto_crop_fast(demo_path, annotation = "on", max_cell_area = 30000,
......@@ -55,10 +56,11 @@
#' @author Lucy McNeill
#' @return cropped SC and foci channels around single cells, regardless of stage
auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 7000, mean_pix = 0.08, annotation = "off", blob_factor = 15, bg_blob_factor = 10, offset = 0.2, final_blob_amp = 10, test_amount = 0,brush_size_blob = 51, sigma_blob = 15, channel3_string = "DAPI", channel2_string = "SYCP3", channel1_string = "MLH3", file_ext = "jpeg", third_channel = "off",cell_aspect_ratio = 2)
auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 7000, mean_pix = 0.08, annotation = "off", blob_factor = 15, bg_blob_factor = 10, offset = 0.2, final_blob_amp = 10, test_amount = 0,brush_size_blob = 51, sigma_blob = 15, channel3_string = "DAPI", channel2_string = "SYCP3", channel1_string = "MLH3", file_ext = "jpeg", third_channel = "off",cell_aspect_ratio = 2, strand_amp = 2)
{
file_list <- list.files(img_path)
dir.create(paste0(img_path,"/crops"))
dir.create(paste0(img_path,"/crops-RGB"))
cell_count <- 0
image_count <-0
antibody1_store <- 0
......@@ -81,7 +83,7 @@ auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 700
file_dna <- img_file
print(file)
image <- readImage(file_dna)
img_orig <- channel(2*image, "grey")
img_orig <- channel(image, "grey")
antibody1_store <- 1
}
if(grepl(paste0('*',channel1_string,'.',file_ext,'$'), img_file)){
......@@ -99,7 +101,7 @@ auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 700
break
}
#### blur the image with get_blobs. call it on img_orig, optional offset
blob_th <- get_blobs(img_orig,blob_factor, bg_blob_factor, offset,final_blob_amp,brush_size_blob, sigma_blob)
blob_th <- get_blobs(strand_amp*img_orig,blob_factor, bg_blob_factor, offset,final_blob_amp,brush_size_blob, sigma_blob)
blob_label <- bwlabel(blob_th)
blob_label <- channel(blob_label, "gray")
candidate <- bwlabel(blob_label)
......@@ -229,6 +231,12 @@ crop_single_object_fast <- function(retained, OOI_final,counter_final,img_orig,i
file_foci <- gsub(file_stub,'', file_foci)
filename_crop_foci <- paste0(img_path,"/crops/", file_dna,"-crop-",cell_count,file_stub)
writeImage(new_img_foci, filename_crop_foci)
### add RGB channel
ch1 <-channel(new_img,"grey")
ch2 <- channel(new_img_foci,"grey")
RGB_img <- rgbImage(ch1,ch2,0*ch1)
filename_crop_RGB <- paste0(img_path,"/crops-RGB/", file_dna,"-crop-",cell_count,file_stub)
writeImage(RGB_img, filename_crop_RGB)
if(third_channel == "on"){
new_img_DAPI <- noise_gone_DAPI[ix, iy]
file_stub <- paste0('-',channel3_string,'.',file_ext)
......
......@@ -142,12 +142,12 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
}
### multiply strands by foci_label
if(annotation == "on"){
cat("at file",img_file, sep = " ")
cat("cell counter is", cell_count, sep= " ")
cat("\n at file",img_file, sep = " ")
cat("\n cell counter is", cell_count, sep= " ")
cat("\n original images")
plot(0.5*new_img)
plot(new_img)
plot(img_orig_foci)
ch1 <-channel(0.5*new_img,"grey")
ch1 <-channel(new_img,"grey")
ch2 <- channel(artificial_amp_factor*foci_mask_crop,"grey")
bluered <- rgbImage(ch1, ch2, 0*ch1)
cat("\n displaying resulting foci count plots. Overlay two channels:")
......@@ -160,7 +160,7 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
overlap_no <- table(coincident_foci)
foci_per_cell <- length(overlap_no)
if(annotation=="on"){
cat("which counts this many foci:",foci_per_cell, sep = " ")
cat("\n which counts this many foci:",foci_per_cell, sep = " ")
}
image_mat <- as.matrix(foci_mask_crop)
image_mat <- image_mat[image_mat > 1e-06]
......@@ -177,7 +177,7 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
### number of foci NOT on an SC
alone_foci <- nrow(foci_candidates) - foci_per_cell
if(annotation == "on"){
cat("number of alone foci",alone_foci, sep = " ")
cat("\n number of alone foci",alone_foci, sep = " ")
if(alone_foci < 0){
cat("\n this one had a negative lone foci amount. Suspect overcounting of foci in this one:")
plot(rgbImage(strands,foci_label,0*foci_label))
......@@ -186,7 +186,7 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
}
percent_px <- sum(overlap)/sum(foci_areas)
if(annotation == "on"){
cat("percentage of foci channel coincident:", percent_px*100, sep = " ")
cat("\n percentage of foci channel coincident:", percent_px*100, sep = " ")
}
tryCatch({
### data frame stuff
......
......@@ -22,7 +22,8 @@ auto_crop_fast(
channel1_string = "MLH3",
file_ext = "jpeg",
third_channel = "off",
cell_aspect_ratio = 2
cell_aspect_ratio = 2,
strand_amp = 2
)
}
\arguments{
......@@ -72,6 +73,8 @@ illuminating foci. Defaults to MLH3}
\item{third_channel}{Optional, defaults to "off".}
\item{cell_aspect_ratio}{Maximum aspect ratio of blob to be defined as a cell}
\item{strand_amp}{multiplication of strand channel for get_blobs function.}
}
\value{
cropped SC and foci channels around single cells, regardless of stage
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment