Commit 94ba5475 authored by Lucy McNeill's avatar Lucy McNeill
Browse files

user can input a maximum aspect ratio for a blob area to be defined as a cell...

user can input a maximum aspect ratio for a blob area to be defined as a cell (number >1 ) as blob_aspect_ratio in auto_crop functions
parent fe6b2030
No preview for this file type
......@@ -18,11 +18,12 @@
#' @param test_amount, Optional number of first N images you want to run function on. For troubleshooting/testing/variable calibration purposes.
#' @param brush_size_blob, Brush size for smudging the dna channel to make blobs
#' @param sigma_blob, Sigma in Gaussian brush for smudging the dna channel to make blobs
#' @param cell_aspect_ratio Maximum aspect ratio of blob to be defined as a cell
#' @return cropped SC and foci channels around single cells, regardless of stage
auto_crop <- 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)
auto_crop <- 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, cell_aspect_ratio = 2)
{
file_list <- list.files(img_path)
dir.create(paste0(img_path,"/crops"))
......@@ -81,7 +82,7 @@ auto_crop <- function(img_path, max_cell_area = 20000, min_cell_area = 7000, me
## function: remove things that aren't cells
retained <- keep_cells(candidate, max_cell_area, min_cell_area)
retained <- keep_cells(candidate, max_cell_area, min_cell_area,cell_aspect_ratio)
### crop over each cell
## function: crop around every single viable cell
......@@ -177,9 +178,10 @@ get_blobs <- function(img_orig, blob_factor, bg_blob_factor, offset,final_blob_a
#' @param candidate Mask of individual cell candidates
#' @param max_cell_area, Maximum pixel area of a cell candidate
#' @param min_cell_area, Minimum pixel area of a cell candidate
#' @param cell_aspect_ratio Maximum aspect ratio of blob to be defined as a cell
#' @return Mask of cell candidates which meet size criteria
keep_cells <- function(candidate, max_cell_area, min_cell_area){
keep_cells <- function(candidate, max_cell_area, min_cell_area, cell_aspect_ratio){
# delete everything that's too small
colorimg<- colorLabels(candidate, normalize = TRUE)
......@@ -199,7 +201,7 @@ keep_cells <- function(candidate, max_cell_area, min_cell_area){
retained <- as.numeric(retained)*rmObjects(candidate, counter, reenumerate = TRUE)
}
## if statement checking that it's not too long i.e. not at edge.
if(semi_maj/semi_min > 2 & is.na(semi_maj/semi_min)==FALSE){
if(semi_maj/semi_min > cell_aspect_ratio & is.na(semi_maj/semi_min)==FALSE){
retained <- as.numeric(retained)*rmObjects(candidate, counter, reenumerate = TRUE)
}
......
......@@ -2,7 +2,7 @@
#'
#' crop an image around each viable cell candidate.
#' @importFrom stats median sd
#' @importFrom EBImage bwlabel channel colorLabels computeFeatures computeFeatures.basic computeFeatures.moment computeFeatures.shape computeFeatures.haralick display filter2 makeBrush readImage rgbImage rmObjects rotate writeImage
#' @importFrom EBImage bwlabel channel colorLabels computeFeatures computeFeatures.basic computeFeatures.moment computeFeatures.shape display filter2 makeBrush readImage rgbImage rmObjects rotate writeImage
#' @importFrom graphics text
#' @importFrom utils str
#' @export auto_crop_fast
......@@ -17,18 +17,13 @@
#' @param final_blob_amp, Contrast factor to multiply smoothed/smudged image. Used in thresholding to make blob mask.
#' @param test_amount, Optional number of first N images you want to run function on. For troubleshooting/testing/variable calibration purposes.
#' @param brush_size_blob, Brush size for smudging the dna channel to make blobs
#' @param cell_aspect_ratio Maximum aspect ratio of blob to be defined as a cell
#' @param sigma_blob, Sigma in Gaussian brush for smudging the dna channel to make blobs
#' @param channel1_string String appended to the files showing the channel illuminating foci. Defaults to MLH3
#' @param channel2_string String appended to the files showing the channel illuminating synaptonemal complexes. Defaults to SYCP3
#' @param channel3_string Defaults to DAPI (if third channel is on)
#' @param file_ext file extension of your images e.g. tiff jpeg or png.
#' @param third_channel Whether there is a third channel e.g. DAPI stain. On or off. Defaults to off.
#'
#' @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")
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)
{
file_list <- list.files(img_path)
dir.create(paste0(img_path,"/crops"))
......@@ -61,7 +56,7 @@ auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 700
if(grepl(paste0('*',channel2_string,'.',file_ext,'$'), file)){
file_dna = file
print(file)
image <- readImage(file_dna)
img_orig <- channel(2*image, "grey")
antibody1_store <- 1
......@@ -90,7 +85,7 @@ auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 700
blob_label <- channel(blob_label, "gray")
candidate <- bwlabel(blob_label)
## function: remove things that aren't cells
retained <- keep_cells(candidate, max_cell_area, min_cell_area)
retained <- keep_cells(candidate, max_cell_area, min_cell_area,cell_aspect_ratio)
### crop over each cell
## function: crop around every single viable cell
### crop foci channel here
......@@ -156,11 +151,6 @@ print("viable cells")
#' @param annotation, Choice to output pipeline choices (recommended to knit)
#' @param file_base, filename base common to all three channels i.e. without -MLH3.jpeg etc.
#' @param img_path, path containing image data to analyse
#' @param channel1_string String appended to the files showing the channel illuminating foci. Defaults to MLH3
#' @param channel2_string String appended to the files showing the channel illuminating synaptonemal complexes. Defaults to SYCP3
#' @param channel3_string Defaults to DAPI (if third channel is on)
#' @param file_ext file extension of your images e.g. tiff jpeg or png.
#' @param third_channel Whether there is a third channel e.g. DAPI stain. On or off. Defaults to off.
#'
......
......@@ -36,6 +36,7 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
}
file_list <- list.files(img_path_new)
print(file_list)
df_cols <- c("filename","cell_no","genotype","stage","foci_count", "sd_foci","mean_foci","median_foci","mean_px","median_px", "percent_on","sd_px","lone_foci")
df_cells <- data.frame(matrix(ncol = length(df_cols), nrow = 0))
......@@ -77,7 +78,12 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
disc = disc / sum(disc)
localBackground = filter2(new_img, disc)
offset = offset_px
thresh_crop = (new_img - localBackground > offset)
if(stage == "pachytene"){
thresh_crop = (new_img - localBackground > offset)
}
else{
thresh_crop = new_img > offset
}
strands <- bwlabel(thresh_crop)
color_img_strands<- colorLabels(strands, normalize = TRUE)
num_strands <- computeFeatures.shape(strands)
......@@ -90,14 +96,20 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
#### normalise the foci image
offset = offset_factor*bg
foci_th = foci_mask_crop > bg + offset
### smooth it
img_tmp_contrast = foci_mask_crop
w = makeBrush(size = brush_size, shape = 'gaussian', sigma = brush_sigma)
#w = makeBrush(size = 1, shape = 'gaussian', sigma = 3)
img_flo = filter2(img_tmp_contrast, w)
## smooth foci channel
foci_th = img_flo > bg + offset
if(stage != "pachytene"){
foci_th = foci_mask_crop > bg + offset
}
else{
### smooth it
img_tmp_contrast = foci_mask_crop
w = makeBrush(size = brush_size, shape = 'gaussian', sigma = brush_sigma)
#w = makeBrush(size = 1, shape = 'gaussian', sigma = 3)
img_flo = filter2(img_tmp_contrast, w)
## smooth foci channel
foci_th = img_flo > bg + offset
}
foci_label = bwlabel(foci_th)
foci_label <- channel(foci_label, "grey")
num_strands <- computeFeatures.shape(strands)
......@@ -121,8 +133,6 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
display(colorLabels(coincident_foci))
print("two channels, only coincident foci")
display(rgbImage(strands,coincident_foci,coincident_foci))
}
overlap_no = table(coincident_foci)
......@@ -181,7 +191,7 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
error = function(e) {
#what should be done in case of exception?
str(e) # #prints structure of exception
print("couldn't crop it")
print("something went wrong while making the data frame")
}
)
}
......
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