Commit 81001d37 authored by Lucy McNeill's avatar Lucy McNeill
Browse files

added option to specify an output path path_out in auto_crop_fast,...

added option to specify an output path path_out in auto_crop_fast, get_pachytene in case user wants output crops in a different place to the original image files
parent 94d29d1c
No preview for this file type
......@@ -21,6 +21,7 @@
#' @importFrom utils str
#' @export auto_crop_fast
#' @param img_path, path containing image data to analyse
#' @param path_out, user specified output path. Defaults to img_path
#' @param max_cell_area, Maximum pixel area of a cell candidate
#' @param min_cell_area, Minimum pixel area of a cell candidate
#' @param mean_pix, Mean pixel intensity of cell crop (in SYCP3 channel)
......@@ -56,11 +57,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, strand_amp = 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, path_out = img_path)
{
file_list <- list.files(img_path)
dir.create(paste0(img_path,"/crops"))
dir.create(paste0(img_path,"/crops-RGB"))
dir.create(paste0(path_out,"/crops"))
dir.create(paste0(path_out,"/crops-RGB"))
cell_count <- 0
image_count <-0
antibody1_store <- 0
......@@ -124,10 +125,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)
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)
}
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)
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)
}
}
antibody1_store <- 0
......@@ -165,6 +166,7 @@ cat("out of",image_count,"images, we got",cell_count,"viable cells \n", sep = "
#' @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 path_out, user specified output path. Defaults to img_path
#' @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
......@@ -179,7 +181,7 @@ cat("out of",image_count,"images, we got",cell_count,"viable cells \n", sep = "
#' @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){
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){
tmp_img <- retained
## have a single object
### delete all other objects
......@@ -217,6 +219,7 @@ crop_single_object_fast <- function(retained, OOI_final,counter_final,img_orig,i
### cropping finished
## cropping part
tryCatch({
img_path_out <- path_out
new_img <- noise_gone[ix, iy]
## want all images to have the same mean (mean_pix)
orig_mean <- mean(new_img)
......@@ -224,24 +227,24 @@ crop_single_object_fast <- function(retained, OOI_final,counter_final,img_orig,i
new_img <- new_img*mean_factor
file_stub <- paste0('-',channel2_string,'.',file_ext)
file_dna <- gsub(file_stub,'', file_base)
filename_crop <- paste0(img_path,"/crops/", file_dna,"-crop-",cell_count,file_stub)
filename_crop <- paste0(img_path_out,"/crops/", file_dna,"-crop-",cell_count,file_stub)
writeImage(new_img, filename_crop)
new_img_foci <- noise_gone_foci[ix, iy]
file_stub <- paste0('-',channel1_string,'.',file_ext)
file_foci <- gsub(file_stub,'', file_foci)
filename_crop_foci <- paste0(img_path,"/crops/", file_dna,"-crop-",cell_count,file_stub)
filename_crop_foci <- paste0(img_path_out,"/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)
filename_crop_RGB <- paste0(img_path_out,"/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)
file_DAPI <- gsub(file_stub,'', file_DAPI)
filename_crop_DAPI <- paste0(img_path,"/crops/", file_dna,"-crop-",cell_count,file_stub)
filename_crop_DAPI <- paste0(img_path_out,"/crops/", file_dna,"-crop-",cell_count,file_stub)
writeImage(new_img_DAPI, filename_crop_DAPI)
}
......
......@@ -13,7 +13,7 @@
#' The file, cell number, foci count etc. are output as a data frame.
#'
#' @export count_foci
#' @param img_path, path containing image data to analyse
#' @param img_path, path containing crops to analyse
#' @param stage, meiosis stage of interest. Currently count_foci determines
#' this with thresholding/ object properties in the dna channel. But will be
#' classified using ML model in future versions.
......
......@@ -12,7 +12,8 @@
#' for mouse cells which can be very well spread / separated.
#'
#' @export get_pachytene
#' @param img_path, path containing image data to analyse
#' @param img_path, path containing crops analyse
#' @param path_out, user specified output path. Defaults to img_path
#' @param species_num, number of chromosomes in the species
#' @param offset, Pixel value offset used in therholding for the dna (SYCP3) channel
#' @param ecc_thresh, The minimum average eccentricity of all objects in mask determined by computefeatures, for a cell to be pachytene.
......@@ -32,7 +33,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 = "+/+")
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)
{
cell_count <- 0
image_count <-0
......@@ -44,8 +45,17 @@ get_pachytene <- function(img_path, species_num = 20, offset = 0.2,ecc_thresh =
df_cells <- data.frame(matrix(ncol = length(df_cols), nrow = 0))
colnames(df_cells) <- df_cols
img_path_new <- paste0(img_path,"/crops")
dir.create(paste0(img_path_new,"/pachytene"))
dir.create(paste0(img_path_new,"/pachytene-RGB"))
if(img_path == path_out){
img_path_out <- img_path_new
dir.create(paste0(img_path_new,"/pachytene"))
dir.create(paste0(img_path_new,"/pachytene-RGB"))
}
else{
img_path_out <- path_out
dir.create(paste0(path_out,"/pachytene"))
dir.create(paste0(path_out,"/pachytene-RGB"))
}
file_list <- list.files(img_path_new)
for (img_file in file_list){
file_base <- img_file
......@@ -116,20 +126,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_new,"/pachytene/", file_dna,".jpeg")
filename_crop <- paste0(img_path_out,"/pachytene/", file_dna,".jpeg")
writeImage(img_orig, filename_crop)
if(annotation == "on"){
print("decided the following is pachytene")
display(img_orig)
}
file_foci <- tools::file_path_sans_ext(file_base_foci)
filename_crop_foci <- paste0(img_path_new,"/pachytene/", file_foci,".jpeg")
filename_crop_foci <- paste0(img_path_out,"/pachytene/", file_foci,".jpeg")
writeImage(img_orig_foci, filename_crop_foci)
### add RGB channel
ch1 <-channel(img_orig,"grey")
ch2 <- channel(img_orig_foci,"grey")
RGB_img <- rgbImage(ch1,ch2,0*ch1)
filename_crop_RGB <- paste0(img_path_new,"/pachytene-RGB/", file_dna,".jpeg")
filename_crop_RGB <- paste0(img_path_out,"/pachytene-RGB/", file_dna,".jpeg")
writeImage(RGB_img, filename_crop_RGB)
}
}
......
......@@ -27,7 +27,7 @@
#' distances of each along)
#'
#' @export
#' @param img_path, path containing image data to analyse
#' @param img_path, path containing crops to analyse
#' @param stage, meiosis stage of interest. Currently count_foci determines
#' this with thresholding/ object properties in the dna channel. But will be
#' classified using ML model in future versions.
......
......@@ -23,7 +23,8 @@ auto_crop_fast(
file_ext = "jpeg",
third_channel = "off",
cell_aspect_ratio = 2,
strand_amp = 2
strand_amp = 2,
path_out = img_path
)
}
\arguments{
......@@ -75,6 +76,8 @@ illuminating foci. Defaults to MLH3}
\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.}
\item{path_out, }{user specified output path. Defaults to img_path}
}
\value{
cropped SC and foci channels around single cells, regardless of stage
......
......@@ -28,7 +28,7 @@ count_foci(
)
}
\arguments{
\item{img_path, }{path containing image data to analyse}
\item{img_path, }{path containing crops to analyse}
\item{stage, }{meiosis stage of interest. Currently count_foci determines
this with thresholding/ object properties in the dna channel. But will be
......
......@@ -26,7 +26,8 @@ crop_single_object_fast(
channel2_string,
channel1_string,
file_ext,
third_channel
third_channel,
path_out
)
}
\arguments{
......@@ -82,6 +83,8 @@ illuminating foci. Defaults to MLH3}
\item{file_ext}{file extension of your images e.g. tif jpeg or png.}
\item{third_channel}{Optional, defaults to "off".}
\item{path_out, }{user specified output path. Defaults to img_path}
}
\value{
Crops around all candidates in both channels
......
......@@ -17,11 +17,12 @@ get_pachytene(
KO_str = "--",
WT_str = "++",
KO_out = "-/-",
WT_out = "+/+"
WT_out = "+/+",
path_out = img_path
)
}
\arguments{
\item{img_path, }{path containing image data to analyse}
\item{img_path, }{path containing crops analyse}
\item{species_num, }{number of chromosomes in the species}
......@@ -46,6 +47,8 @@ get_pachytene(
\item{KO_out}{string in output csv in genotype column, for knockout. Defaults to -/-.}
\item{WT_out}{string in output csv in genotype column, for knockout. Defaults to +/+.}
\item{path_out, }{user specified output path. Defaults to img_path}
}
\value{
Pairs of foci and SC channel crops for pachytene
......
......@@ -32,7 +32,7 @@ measure_distances_general(
)
}
\arguments{
\item{img_path, }{path containing image data to analyse}
\item{img_path, }{path containing crops to analyse}
\item{offset_px, }{Pixel value offset used in thresholding of dna channel}
......
Markdown is supported
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