Commit 3422fd64 authored by Lucy McNeill's avatar Lucy McNeill
Browse files

auto_crop and get_pachytene gives same results for any input resolution, i.e....

auto_crop and get_pachytene gives same results for any input resolution, i.e. images found at the end of the pipeline are all the same
parent 129ec6d3
No preview for this file type
......@@ -51,13 +51,14 @@
#' @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.
#' @param resize_l length for resized image
#' @examples demo_path = paste0(system.file("extdata",package = "synapsis"))
#' auto_crop_fast(demo_path, annotation = "on", max_cell_area = 30000,
#' min_cell_area = 7000)
#' @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, strand_amp = 2, path_out = img_path)
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, path_out = img_path, resize_l = 720)
{
file_list <- list.files(img_path)
dir.create(paste0(path_out,"/crops"))
......@@ -85,6 +86,8 @@ auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 700
print(file)
image <- readImage(file_dna)
img_orig <- channel(image, "grey")
img_orig_highres <- img_orig
img_orig <- resize(img_orig, w = resize_l, h = resize_l)
antibody1_store <- 1
}
if(grepl(paste0('*',channel1_string,'.',file_ext,'$'), img_file)){
......@@ -125,10 +128,10 @@ auto_crop_fast <- function(img_path, max_cell_area = 20000, min_cell_area = 700
### row of interest is the counter_final'th row of x_final
cell_count <- cell_count +1
if(third_channel=="on"){
crop_single_object_fast(retained,OOI_final,counter_final,img_orig,img_orig_foci,img_orig_DAPI,file_dna,file_foci,file_DAPI,cell_count, mean_pix, annotation, file_base, img_path, r_max, cx, cy,channel3_string,channel2_string,channel1_string,file_ext,third_channel,path_out)
crop_single_object_fast(retained,OOI_final,counter_final,img_orig,img_orig_foci,img_orig_DAPI,file_dna,file_foci,file_DAPI,cell_count, mean_pix, annotation, file_base, img_path, r_max, cx, cy,channel3_string,channel2_string,channel1_string,file_ext,third_channel,path_out, img_orig_highres, resize_l)
}
else{
crop_single_object_fast(retained,OOI_final,counter_final,img_orig,img_orig_foci,img_orig_foci,file_dna,file_foci,file_foci,cell_count, mean_pix, annotation, file_base, img_path, r_max, cx, cy,channel3_string,channel2_string,channel1_string,file_ext,third_channel,path_out)
crop_single_object_fast(retained,OOI_final,counter_final,img_orig,img_orig_foci,img_orig_foci,file_dna,file_foci,file_foci,cell_count, mean_pix, annotation, file_base, img_path, r_max, cx, cy,channel3_string,channel2_string,channel1_string,file_ext,third_channel,path_out, img_orig_highres, resize_l)
}
}
antibody1_store <- 0
......@@ -175,13 +178,13 @@ cat("out of",image_count,"images, we got",cell_count,"viable cells \n", sep = "
#' channel illuminating cell structures. Defaults to DAPI,
#' if third channel == "on".
#' @param third_channel Optional, defaults to "off".
#' @param img_orig_highres the original strand image with original resolution
#' @param file_ext file extension of your images e.g. tif jpeg or png.
#' @param resize_l length of square to resize original image to.
#' @return Crops around all candidates in both channels
#'
crop_single_object_fast <- function(retained, OOI_final,counter_final,img_orig,img_orig_foci,img_orig_DAPI="blank",file_dna,file_foci,file_DAPI = "blank",cell_count, mean_pix, annotation, file_base, img_path, r_max, cx, cy,channel3_string,channel2_string,channel1_string,file_ext,third_channel,path_out){
crop_single_object_fast <- function(retained, OOI_final,counter_final,img_orig,img_orig_foci,img_orig_DAPI="blank",file_dna,file_foci,file_DAPI = "blank",cell_count, mean_pix, annotation, file_base, img_path, r_max, cx, cy,channel3_string,channel2_string,channel1_string,file_ext,third_channel,path_out, img_orig_highres, resize_l){
tmp_img <- retained
## have a single object
### delete all other objects
......@@ -194,33 +197,40 @@ crop_single_object_fast <- function(retained, OOI_final,counter_final,img_orig,i
tmp_img <- as.numeric(tmp_img)*rmObjects(bwlabel(retained), counter_single, reenumerate = TRUE)
}
}
noise_gone <- bwlabel(tmp_img)*as.matrix(img_orig)
noise_gone_foci <- bwlabel(tmp_img)*as.matrix(img_orig_foci)
#### resize your images here?
dim_orig <- dim(img_orig_highres)
new_l <- as.integer(dim_orig[1])
print(dim_orig)
print(new_l)
noise_gone_highres <- bwlabel(resize(tmp_img, h = new_l, w = new_l))*as.matrix(img_orig_highres)
noise_gone_foci <- bwlabel(resize(tmp_img, h = new_l, w = new_l))*as.matrix(img_orig_foci)
if(third_channel == "on"){
noise_gone_DAPI <- bwlabel(tmp_img)*as.matrix(img_orig_DAPI)
noise_gone_DAPI <- bwlabel(resize(tmp_img, h = new_l, w = new_l))*as.matrix(img_orig_DAPI)
}
### use the features of tmp_img
### here we have the single object. Need to identify its centre value and radius..
crop_r <- floor(r_max[counter_final])
cx <- cx[counter_final]
cy <- cy[counter_final]
crop_r_highres <- floor(r_max[counter_final]*round( new_l/resize_l))
cx_highres <- cx[counter_final]*( new_l/resize_l)
cy_highres <- cy[counter_final]*( new_l/resize_l)
cat("\n the cropping radius, cx and cy are", crop_r_highres, cx_highres, cy_highres, sep = " ")
# might want to do this as a matrix
top_left_x <- floor(cx-crop_r)
top_left_y <- floor(cy-crop_r)
bottom_left_x <- floor(cx-crop_r)
bottom_left_y <- floor(cy+crop_r)
bottom_right_x <- floor(cx+crop_r)
bottom_right_y <- floor(cy+crop_r)
top_right_x <- floor(cx-crop_r)
top_right_y <- floor(cy+crop_r)
top_left_x_highres <- floor(cx_highres-crop_r_highres)
top_left_y_highres <- floor(cy_highres-crop_r_highres)
bottom_left_x_highres <- floor(cx_highres-crop_r_highres)
bottom_left_y_highres <- floor(cy_highres+crop_r_highres)
bottom_right_x_highres <- floor(cx_highres+crop_r_highres)
bottom_right_y_highres <- floor(cy_highres+crop_r_highres)
top_right_x_highres <- floor(cx_highres-crop_r_highres)
top_right_y_highres <- floor(cy_highres+crop_r_highres)
## crop image
ix <- bottom_left_x:bottom_right_x
iy <- top_left_y:bottom_left_y
ix <- bottom_left_x_highres:bottom_right_x_highres
iy <- top_left_y_highres:bottom_left_y_highres
####### end high res stuff
#########
### cropping finished
## cropping part
tryCatch({
img_path_out <- path_out
new_img <- noise_gone[ix, iy]
new_img <- noise_gone_highres[ix, iy]
## want all images to have the same mean (mean_pix)
orig_mean <- mean(new_img)
mean_factor <- mean_pix/orig_mean
......@@ -266,6 +276,7 @@ crop_single_object_fast <- function(retained, OOI_final,counter_final,img_orig,i
#str(e) # #prints structure of exception
print("couldn't crop it since cell is on the edge. Neglected the following mask of a cell candidate:")
display(tmp_img)
display(noise_gone_highres)
}
}
)
......
......@@ -46,6 +46,7 @@
#' have foci > 100 or so.
#' @param artificial_amp_factor Amplification of foci channel, for annotation only.
#' @param strand_amp multiplication of strand channel to make masks
#' @param min_foci minimum pixel area for a foci. Depends on your dpi etc. Defaults to 4
#' @examples demo_path = paste0(system.file("extdata",package = "synapsis"))
#' foci_counts <- count_foci(demo_path,offset_factor = 3, brush_size = 3,
#' brush_sigma = 3, annotation = "on",stage = "pachytene")
......@@ -53,7 +54,7 @@
#' @return foci count per cell
count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor = 2, brush_size = 3, brush_sigma = 3, foci_norm = 0.01, annotation = "off",channel2_string = "SYCP3", channel1_string = "MLH3",file_ext = "jpeg", KO_str = "--",WT_str = "++",KO_out = "-/-", WT_out = "+/+", watershed_stop = "off", watershed_radius = 1, watershed_tol = 0.05, crowded_foci = TRUE, artificial_amp_factor = 1, strand_amp = 2)
count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor = 2, brush_size = 3, brush_sigma = 3, foci_norm = 0.01, annotation = "off",channel2_string = "SYCP3", channel1_string = "MLH3",file_ext = "jpeg", KO_str = "--",WT_str = "++",KO_out = "-/-", WT_out = "+/+", watershed_stop = "off", watershed_radius = 1, watershed_tol = 0.05, crowded_foci = TRUE, artificial_amp_factor = 1, strand_amp = 2, min_foci =9)
{
cell_count <- 0
image_count <-0
......@@ -141,12 +142,18 @@ count_foci <- function(img_path, stage = "none", offset_px = 0.2, offset_factor
else{
coincident_foci <- watershed(bwlabel(foci_th*strands)*as.matrix(img_orig_foci),tolerance=watershed_tol, ext=watershed_radius)
}
coincident_df <- data.frame(computeFeatures.shape(coincident_foci))
if(annotation == "on"){
print(coincident_df)
}
coincident_df <- coincident_df[coincident_df$s.area > min_foci,]
### multiply strands by foci_label
if(annotation == "on"){
annotate_foci_counting(img_file,cell_count,new_img,img_orig_foci,artificial_amp_factor,foci_mask_crop,strands,coincident_foci)
}
overlap_no <- table(coincident_foci)
foci_per_cell <- length(overlap_no)
foci_per_cell <- nrow(coincident_df)
if(annotation=="on"){
cat("\n which counts this many foci:",foci_per_cell, sep = " ")
}
......
......@@ -26,6 +26,7 @@
#' @param WT_str string in filename corresponding to wildtype genotype. Defaults to ++.
#' @param KO_out string in output csv in genotype column, for knockout. Defaults to -/-.
#' @param WT_out string in output csv in genotype column, for knockout. Defaults to +/+.
#' @param resize_l length of resized square cell image.
#' @param artificial_amp_factor Amplification of foci channel, for RGB output files. Deaults to 3.
#' @param strand_amp multiplication of strand channel.
#' @examples demo_path = paste0(system.file("extdata",package = "synapsis"))
......@@ -35,7 +36,7 @@
#'
get_pachytene <- function(img_path, species_num = 20, offset = 0.2,ecc_thresh = 0.85, area_thresh = 0.06, annotation = "off", channel2_string = "SYCP3", channel1_string = "MLH3",file_ext = "jpeg", KO_str = "--",WT_str = "++",KO_out = "-/-", WT_out = "+/+", path_out = img_path, artificial_amp_factor=3, strand_amp = 2)
get_pachytene <- function(img_path, species_num = 20, offset = 0.2,ecc_thresh = 0.85, area_thresh = 0.06, annotation = "off", channel2_string = "SYCP3", channel1_string = "MLH3",file_ext = "jpeg", KO_str = "--",WT_str = "++",KO_out = "-/-", WT_out = "+/+", path_out = img_path, artificial_amp_factor=3, strand_amp = 2, resize_l = 120)
{
cell_count <- 0
image_count <-0
......@@ -68,7 +69,9 @@ get_pachytene <- function(img_path, species_num = 20, offset = 0.2,ecc_thresh =
file_base_dna <- file_base
image_count <- image_count +1
image <- readImage(file_dna)
img_orig_highres <- channel(image, "grey")
img_orig <- channel(strand_amp*image, "grey")
img_orig <- resize(img_orig, w = resize_l, h = resize_l)
antibody1_store <- 1
}
if(grepl(paste0('*',channel1_string,'.',file_ext,'$'), img_file)){
......@@ -92,7 +95,7 @@ get_pachytene <- function(img_path, species_num = 20, offset = 0.2,ecc_thresh =
num_strands <- computeFeatures.shape(strands)
num_strands <- data.frame(num_strands)
#### segment the strands
if (nrow(num_strands)<max_obj && nrow(num_strands)>5){
if (nrow(num_strands)<max_obj && nrow(num_strands)>3){
cell_count <- cell_count + 1
### identified a good image. count foci
### data frame stuff
......@@ -128,20 +131,20 @@ get_pachytene <- function(img_path, species_num = 20, offset = 0.2,ecc_thresh =
df_cells <- rbind(df_cells,t(c(img_file,cell_count,genotype,px_mask, px_total,px_fraction, mean_ecc,mean_ratio,skew,sd_bright_px,stage_classification)))
pachytene_count <- pachytene_count + 1
file_dna <- tools::file_path_sans_ext(file_base_dna)
filename_crop <- paste0(img_path_out,"/pachytene/", file_dna,".jpeg")
writeImage(img_orig, filename_crop)
filename_crop <- paste0(img_path_out,"/pachytene/", file_dna,'.',file_ext)
writeImage(img_orig_highres, filename_crop)
if(annotation == "on"){
print("decided the following is pachytene")
display(img_orig)
display(img_orig_highres)
}
file_foci <- tools::file_path_sans_ext(file_base_foci)
filename_crop_foci <- paste0(img_path_out,"/pachytene/", file_foci,".jpeg")
filename_crop_foci <- paste0(img_path_out,"/pachytene/", file_foci,'.',file_ext)
writeImage(img_orig_foci, filename_crop_foci)
### add RGB channel
ch1 <-channel(img_orig,"grey")
ch1 <-channel(img_orig_highres,"grey")
ch2 <- channel(img_orig_foci,"grey")
RGB_img <- rgbImage(ch1,ch2,0*ch1)
filename_crop_RGB <- paste0(img_path_out,"/pachytene-RGB/", file_dna,".jpeg")
filename_crop_RGB <- paste0(img_path_out,"/pachytene-RGB/", file_dna,'.',file_ext)
writeImage(RGB_img, filename_crop_RGB)
}
}
......
......@@ -24,7 +24,8 @@ auto_crop_fast(
third_channel = "off",
cell_aspect_ratio = 2,
strand_amp = 2,
path_out = img_path
path_out = img_path,
resize_l = 720
)
}
\arguments{
......@@ -78,6 +79,8 @@ illuminating foci. Defaults to MLH3}
\item{strand_amp}{multiplication of strand channel for get_blobs function.}
\item{path_out, }{user specified output path. Defaults to img_path}
\item{resize_l}{length for resized image}
}
\value{
cropped SC and foci channels around single cells, regardless of stage
......
......@@ -25,7 +25,8 @@ count_foci(
watershed_tol = 0.05,
crowded_foci = TRUE,
artificial_amp_factor = 1,
strand_amp = 2
strand_amp = 2,
min_foci = 9
)
}
\arguments{
......@@ -82,6 +83,8 @@ have foci > 100 or so.}
\item{artificial_amp_factor}{Amplification of foci channel, for annotation only.}
\item{strand_amp}{multiplication of strand channel to make masks}
\item{min_foci}{minimum pixel area for a foci. Depends on your dpi etc. Defaults to 4}
}
\value{
foci count per cell
......
......@@ -27,7 +27,9 @@ crop_single_object_fast(
channel1_string,
file_ext,
third_channel,
path_out
path_out,
img_orig_highres,
resize_l
)
}
\arguments{
......@@ -85,6 +87,10 @@ illuminating foci. Defaults to MLH3}
\item{third_channel}{Optional, defaults to "off".}
\item{path_out, }{user specified output path. Defaults to img_path}
\item{img_orig_highres}{the original strand image with original resolution}
\item{resize_l}{length of square to resize original image to.}
}
\value{
Crops around all candidates in both channels
......
......@@ -20,7 +20,8 @@ get_pachytene(
WT_out = "+/+",
path_out = img_path,
artificial_amp_factor = 3,
strand_amp = 2
strand_amp = 2,
resize_l = 120
)
}
\arguments{
......@@ -55,6 +56,8 @@ get_pachytene(
\item{artificial_amp_factor}{Amplification of foci channel, for RGB output files. Deaults to 3.}
\item{strand_amp}{multiplication of strand channel.}
\item{resize_l}{length of resized square cell image.}
}
\value{
Pairs of foci and SC channel crops for pachytene
......
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