Skip to content

Commit

Permalink
new working version
Browse files Browse the repository at this point in the history
  • Loading branch information
buddekai committed Oct 8, 2021
1 parent 8fbfa54 commit b54ac6f
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 801 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Custom files/directories
/tests
readCzi.Rproj
/examples/*metadata.txt
/examples/output
/examples/output_compare

# History files
.Rhistory
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: readCzi
Type: Package
Title: Read czi image files, convert them to tifs, and save metadata
Version: 0.1.6
Version: 0.1.7
Author: c(person("Kai", "Budde", email = "[email protected]",
role = c("aut", "cre"))
Maintainer: Kai Budde <[email protected]>
Expand Down
95 changes: 49 additions & 46 deletions R/readCzi.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ readCzi <- function(input_file = NULL) {

# Load image directly from czi
image_loaded <- zis$imread(input_file)
czi_class <- zis$CziFile(input_file)

# Load metadata
df_metadata <- readCziMetadata(input_file = input_file,
save_metadata = FALSE)
metadata <- czi_class$metadata(czi_class)

# Save bit depth of the image --------------------------------------------
czi_class <- zis$CziFile(input_file)

bit_depth <- czi_class$dtype
if(bit_depth == "uint16"){
bit_depth <- 16
Expand Down Expand Up @@ -135,57 +134,61 @@ readCzi <- function(input_file = NULL) {

}

# Check if there is/are specific emission wavelengths (of channels used)
# Decide which channel is red/green/blue
if(grepl(pattern = "EmissionWavelength",
x = metadata,
ignore.case = TRUE)){

if(number_of_channels == 3){
wavelengths <- gsub(
pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
sep=""),
replacement = "\\1,\\2,\\3",
x = metadata)
}else if(number_of_channels == 2){
wavelengths <- gsub(
pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
sep=""),
replacement = "\\1,\\2",
x = metadata)
}else if(number_of_channels == 1){
wavelengths <- gsub(
pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
sep=""),
replacement = "\\1",
x = metadata)
}

wavelengths <- as.numeric(strsplit(wavelengths, split = ",")[[1]])
# if(number_of_channels == 3){
# wavelengths <- gsub(
# pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# ".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# ".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# sep=""),
# replacement = "\\1,\\2,\\3",
# x = metadata)
# }else if(number_of_channels == 2){
# wavelengths <- gsub(
# pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# ".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# sep=""),
# replacement = "\\1,\\2",
# x = metadata)
# }else if(number_of_channels == 1){
# wavelengths <- gsub(
# pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# sep=""),
# replacement = "\\1",
# x = metadata)
# }

# wavelengths <- as.numeric(strsplit(wavelengths, split = ",")[[1]])

# Upper and lower limits of wavelengths to determine the colors
# red: > 600nm
# green: >= 500nm and <= 600nm
# blue: < 500nm

upper_limit_blue <- 500
lower_limit_red <- 600

test_red <- any(wavelengths > lower_limit_red)
red_id <- ifelse(test = test_red, yes = which(wavelengths > lower_limit_red), no = 0)
if(length(red_id) > 1){print("More than one red channel!?")}

test_green <- any( (upper_limit_blue) <= wavelengths & (wavelengths <= lower_limit_red))
green_id <- ifelse(test = test_green, yes = which((upper_limit_blue <= wavelengths) & (wavelengths <= lower_limit_red)), no = 0)
if(length(green_id) > 1){print("More than one green channel!?")}

test_blue <- any(wavelengths < upper_limit_blue)
blue_id <- ifelse(test = test_blue, yes = which(upper_limit_blue > wavelengths), no = 0)
if(length(blue_id) > 1){print("More than one blue channel!?")}

rgb_layers <- c(red_id, green_id, blue_id)
# upper_limit_blue <- 450#500
# lower_limit_red <- 550#600
#
# test_red <- any(wavelengths > lower_limit_red)
# red_id <- ifelse(test = test_red, yes = which(wavelengths > lower_limit_red), no = 0)
# if(length(red_id) > 1){print("More than one red channel!?")}
#
# test_green <- any( (upper_limit_blue) <= wavelengths & (wavelengths <= lower_limit_red))
# green_id <- ifelse(test = test_green, yes = which((upper_limit_blue <= wavelengths) & (wavelengths <= lower_limit_red)), no = 0)
# if(length(green_id) > 1){print("More than one green channel!?")}
#
# test_blue <- any(wavelengths < upper_limit_blue)
# blue_id <- ifelse(test = test_blue, yes = which(upper_limit_blue > wavelengths), no = 0)
# if(length(blue_id) > 1){print("More than one blue channel!?")}
#
# rgb_layers <- c(red_id, green_id, blue_id)

rgb_layers <- c(df_metadata$red_channel,
df_metadata$green_channel,
df_metadata$blue_channel)

color_axis <- "C"

Expand Down Expand Up @@ -230,13 +233,13 @@ readCzi <- function(input_file = NULL) {
}

if(length(dim(copy_image_loaded)) == 8){
if(rgb_layers[1] != 0){
if(!is.na(rgb_layers[1]) && rgb_layers[1] != 0){
image_loaded[,,1,] <- copy_image_loaded[,,rgb_layers[1],,,,,]
}
if(rgb_layers[2] != 0){
if(!is.na(rgb_layers[2]) && rgb_layers[2] != 0){
image_loaded[,,2,] <- copy_image_loaded[,,rgb_layers[2],,,,,]
}
if(rgb_layers[3] != 0){
if(!is.na(rgb_layers[3]) && rgb_layers[3] != 0){
image_loaded[,,3,] <- copy_image_loaded[,,rgb_layers[3],,,,,]
}

Expand Down
162 changes: 59 additions & 103 deletions R/readCziMetadata_Apotome.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ readCziMetadata_Apotome <- function(metadata = metadata,
illumination_wavelengths[i] <- as.numeric(unlist(channel_information[[1]]$IlluminationWavelength$SinglePeak))
}
if(grepl(pattern = "LightSourceSettings", x = channel_information, ignore.case = TRUE)){
light_source_intensitys[i] <- unlist(channel_information[[1]]$LightSourcesSettings$LightSourceSettings$Intensity)
light_source_intensitys[i] <- as.numeric(gsub(pattern = "\\%", replacement = "", x = light_source_intensitys[i]))
dump_light_source_intensitys <- unlist(channel_information[[1]]$LightSourcesSettings$LightSourceSettings$Intensity)
dump_light_source_intensitys <- gsub(pattern = "\\%", replacement = "", x = dump_light_source_intensitys)
dump_light_source_intensitys <- gsub(pattern = " ", replacement = "", x = dump_light_source_intensitys)
light_source_intensitys[i] <- as.numeric(dump_light_source_intensitys)
rm(dump_light_source_intensitys)
}

}
Expand Down Expand Up @@ -103,107 +106,6 @@ readCziMetadata_Apotome <- function(metadata = metadata,
contour_size_y <- NA
}

# # Get information of Apotome (LED widefield) #############################
#
# # Get information depending on the number of channels
#
# if(number_of_channels == 3){
#
# # LED widefield
# excitation_wavelengths <- gsub(
# pattern = paste(".+<ExcitationWavelength>(.+)</ExcitationWavelength>.+",
# ".+<ExcitationWavelength>(.+)</ExcitationWavelength>.+",
# ".+<ExcitationWavelength>(.+)</ExcitationWavelength>.+",
# sep=""),
# replacement = "\\1,\\2,\\3",
# x = metadata)
#
# emission_wavelengths <- gsub(
# pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# ".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# ".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# sep=""),
# replacement = "\\1,\\2,\\3",
# x = metadata)
#
# illumination_wavelengths <- gsub(
# pattern = paste(
# ".+<IlluminationWavelength>.+<SinglePeak>(.+)</SinglePeak>.+</IlluminationWavelength>.+",
# ".+<IlluminationWavelength>.+<SinglePeak>(.+)</SinglePeak>.+</IlluminationWavelength>.+",
# ".+<IlluminationWavelength>.+<SinglePeak>(.+)</SinglePeak>.+</IlluminationWavelength>.+",
# sep=""),
# replacement = "\\1,\\2,\\3",
# x = metadata)
#
#
# light_source_intensitys <- gsub(
# pattern = paste(".+<Intensity>(.+)</Intensity>.+",
# ".+<Intensity>(.+)</Intensity>.+",
# ".+<Intensity>(.+)</Intensity>.+",
# sep=""),
# replacement = "\\1, \\2, \\3", x = metadata)
#
# }else if(number_of_channels == 2){
#
# # LED widefield
# excitation_wavelengths <- gsub(
# pattern = paste(".+<ExcitationWavelength>(.+)</ExcitationWavelength>.+",
# ".+<ExcitationWavelength>(.+)</ExcitationWavelength>.+",
# sep=""),
# replacement = "\\1,\\2",
# x = metadata)
#
# emission_wavelengths <- gsub(
# pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# ".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# sep=""),
# replacement = "\\1,\\2",
# x = metadata)
#
# illumination_wavelengths <- gsub(
# pattern = paste(
# ".+<IlluminationWavelength>.+<SinglePeak>(.+)</SinglePeak>.+</IlluminationWavelength>.+",
# ".+<IlluminationWavelength>.+<SinglePeak>(.+)</SinglePeak>.+</IlluminationWavelength>.+",
# sep=""),
# replacement = "\\1,\\2",
# x = metadata)
#
#
# light_source_intensitys <- gsub(
# pattern = paste(".+<Intensity>(.+)</Intensity>.+",
# ".+<Intensity>(.+)</Intensity>.+",
# sep=""),
# replacement = "\\1, \\2", x = metadata)
#
# }else if(number_of_channels == 1){
# # LED widefield
# excitation_wavelengths <- gsub(
# pattern = paste(".+<ExcitationWavelength>(.+)</ExcitationWavelength>.+",
# sep=""),
# replacement = "\\1",
# x = metadata)
#
# emission_wavelengths <- gsub(
# pattern = paste(".+<EmissionWavelength>(.+)</EmissionWavelength>.+",
# sep=""),
# replacement = "\\1",
# x = metadata)
#
# illumination_wavelengths <- gsub(
# pattern = paste(
# ".+<IlluminationWavelength>.+<SinglePeak>(.+)</SinglePeak>.+</IlluminationWavelength>.+",
# sep=""),
# replacement = "\\1",
# x = metadata)
#
#
# light_source_intensitys <- gsub(
# pattern = paste(".+<Intensity>(.+)</Intensity>.+",
# sep=""),
# replacement = "\\1", x = metadata)
#
# }

# Sorting the channels information regarding wavelength ------------------
channel_order <- order(illumination_wavelengths)

Expand All @@ -226,6 +128,57 @@ readCziMetadata_Apotome <- function(metadata = metadata,
}
}

# Finding the color of each channel and the corresponding chan number ----

# Upper and lower limits of emission wavelengths to determine the colors
red_limit <- 600 #>600nmnm
# green: #>= 500nm and <= 600nm
blue_limit <- 500 #< 500nm

if(number_of_channels == 3){
# 1: blue, 2: green, 3: red
channel_color <- channel_order
}else{

channel_color <- rep(NA, 3)

for(i in 1:number_of_channels){

if(!is.na(emission_wavelengths[i])){
if(emission_wavelengths[i] < blue_limit){
# Finding the blue channel
channel_color[1] <- channel_order[i]

}else if(emission_wavelengths[i] > red_limit){
# Finding the red channel
channel_color[3] <- channel_order[i]

}else{
# Finding the green channel
channel_color[2] <- channel_order[i]

}
}else{
if(illumination_wavelengths[i] < (blue_limit-50)){
# Finding the blue channel
channel_color[1] <- channel_order[i]

}else if(illumination_wavelengths[i] > (red_limit-50)){
# Finding the red channel
channel_color[3] <- channel_order[i]

}else{
# Finding the green channel
channel_color[2] <- channel_order[i]

}
}


}

}

# Put information into a data frame
df_metadata <- data.frame(
"fileName" = NA,
Expand All @@ -242,6 +195,9 @@ readCziMetadata_Apotome <- function(metadata = metadata,
"scaling_x" = NA,
"scaling_y" = NA,
"scaling_z" = NA,
"blue_channel" = channel_color[1],
"green_channel" = channel_color[2],
"red_channel" = channel_color[3],
"scene_name" = scene_name,
"scene_center_position_x" = scene_center_position_x,
"scene_center_position_y" = scene_center_position_y,
Expand Down
Loading

0 comments on commit b54ac6f

Please sign in to comment.