Skip to content

Merge FTIRtools into PlotFTIR #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ Authors@R: c(
person("His Majesty the King in Right of Canada, as represented by the Minister of Natural Resources",
role = 'cph')
)
Description: The goal of 'PlotFTIR' is to easily and quickly kick-start the production of journal-quality Fourier Transform Infra-Red (FTIR) spectral plots in R using 'ggplot2'. The produced plots can be published directly or further modified by 'ggplot2' functions.
L'objectif de 'PlotFTIR' est de démarrer facilement et rapidement la production des tracés spectraux de spectroscopie infrarouge à transformée de Fourier (IRTF) de qualité journal dans R à l'aide de 'ggplot2'. Les tracés produits peuvent être publiés directement ou modifiés davantage par les fonctions 'ggplot2'.
Description: The goal of 'PlotFTIR' is to easily and quickly kick-start the analysis and production of journal-quality Fourier Transform Infra-Red (FTIR) spectral plots in R using 'ggplot2'. The produced plots can be published directly or further modified by 'ggplot2' functions.
L'objectif de 'PlotFTIR' est de démarrer facilement et rapidement l'analyse et la production des tracés spectraux de spectroscopie infrarouge à transformée de Fourier (IRTF) de qualité journal dans R à l'aide de 'ggplot2'. Les tracés produits peuvent être publiés directement ou modifiés davantage par les fonctions 'ggplot2'.
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
Imports:
cli,
rlang,
scales
scales,
signal
RoxygenNote: 7.3.2
Depends:
R (>= 3.3)
Expand All @@ -33,7 +34,8 @@ Suggests:
ir,
ChemoSpec,
R.utils,
readJDX
readJDX,
baseline
Config/testthat/edition: 3
VignetteBuilder: knitr
BugReports: https://github.com/NRCan/PlotFTIR/issues
Expand Down
11 changes: 10 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@ export(add_band)
export(add_scalar_value)
export(add_wavenumber_marker)
export(average_spectra)
export(baseline_ftir)
export(check_ftir_data)
export(chemospec_to_plotftir)
export(compress_low_energy)
export(find_ftir_peaks)
export(fit_peak_df)
export(fit_peaks)
export(get_plot_sample_ids)
export(highlight_sample)
export(ir_to_plotftir)
export(move_plot_legend)
export(normalize_spectra)
export(plot_components)
export(plot_fit_ftir_peaks)
export(plot_fit_residuals)
export(plot_ftir)
export(plot_ftir_stacked)
export(plotftir_to_chemospec)
export(plotftir_to_ir)
export(read_ftir)
export(read_ftir_directory)
export(recalculate_baseline)
export(remove_continuum_ftir)
export(rename_plot_sample_ids)
export(save_plot)
export(shift_baseline)
export(smooth_ftir)
export(subtract_scalar_value)
export(transmittance_to_absorbance)
export(zoom_in_on_range)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# PlotFTIR (development version)

* Merged in deconvolution and advanced smoothing and baseline functions. (#19)

# PlotFTIR 1.2.0

* Patch update to expose `check_ftir_data` for downstream packages. (related to #19)
Expand Down
52 changes: 26 additions & 26 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ read_ftir_directory <- function(path, files, sample_names = NA, ...) {
)
}

if (!all(is.na(sample_names))) {
if (all(is.na(sample_names))) {
sample_names <- rep(NA, length(files))
} else {
if (length(sample_names) != length(files)) {
cli::cli_abort(c(
"Error in {.fn PlotFTIR::read_ftir_directory}: If providing {.arg sample_names} the same number of names as the number of {.arg files} must be provided.",
i = "You provided {length(sample_names)} {.arg sample_name{?s}} and {length(files)} {.arg file{?s}}"
))
}
} else {
sample_names <- rep(NA, length(files))
}

ftir <- data.frame()
Expand All @@ -219,11 +219,12 @@ read_ftir_directory <- function(path, files, sample_names = NA, ...) {
}
}
},
error = function(e)
error = function(e) {
cli::cli_warn(c(
"{e}",
i = "{.fn PlotFTIR::read_ftir_directory} will try to continue with the next file."
))
}
)
}
if (nrow(ftir) > 0) {
Expand Down Expand Up @@ -381,7 +382,10 @@ read_ftir_jdx <- function(path, file, sample_name = NA, ...) {
)
}
if (
!grepl("INFRARED", toupper(metadata[grepl("DATATYPE|DATA TYPE", metadata)]))
!grepl(
"INFRARED",
toupper(metadata[grepl("DATATYPE|DATA TYPE", metadata)])
)
) {
cli::cli_abort(
c(
Expand All @@ -392,23 +396,21 @@ read_ftir_jdx <- function(path, file, sample_name = NA, ...) {
}

intensity <- NA
if (any(grepl("absorbance", tolower(metadata)))) {
if (any(grepl("absorbance", tolower(metadata), fixed = TRUE))) {
intensity <- "absorbance"
} else if (any(grepl("transmittance", tolower(metadata)))) {
} else if (any(grepl("transmittance", tolower(metadata), fixed = TRUE))) {
intensity <- "transmittance"
}

ir <- jdx[[4]]
sample_name_jdx <- names(jdx[4])
if (is.na(sample_name)) {
sample_name <- sample_name_jdx
} else {
if (sample_name != sample_name_jdx) {
cli::cli_alert_warning(c(
'Note: provided sample name of "{sample_name}" does not match that contained in the .jdx file: "{sample_name_jdx}".',
i = "Will use the provided sample name."
))
}
} else if (sample_name != sample_name_jdx) {
cli::cli_alert_warning(c(
'Note: provided sample name of "{sample_name}" does not match that contained in the .jdx file: "{sample_name_jdx}".',
i = "Will use the provided sample name."
))
}

ftir_data <- data.frame(
Expand All @@ -418,7 +420,7 @@ read_ftir_jdx <- function(path, file, sample_name = NA, ...) {

if (!is.na(intensity)) {
if (intensity_type(ftir_data) != intensity) {
if (intensity == 'transmittance' & max(ftir_data$intensity < 1.2)) {
if (intensity == "transmittance" && max(ftir_data$intensity < 1.2)) {
# It's possible to do transmittance in 0..1 scale instead of percent.
# PlotFTIR works better with %Transmittance
ftir_data$intensity <- ftir_data$intensity * 100
Expand All @@ -435,7 +437,7 @@ read_ftir_jdx <- function(path, file, sample_name = NA, ...) {
intensity <- intensity_type(ftir_data)
}

if (intensity == 'absorbance') {
if (intensity == "absorbance") {
ftir_data$absorbance <- ftir_data$intensity
} else {
ftir_data$transmittance <- ftir_data$intensity
Expand Down Expand Up @@ -504,7 +506,7 @@ save_plot <- function(ftir_spectra_plot, filename, ...) {
))
}

if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::save_plt}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
Expand Down Expand Up @@ -554,7 +556,7 @@ ir_to_plotftir <- function(ir_data, what = NA) {

# Param checks

if (!("ir" %in% class(ir_data))) {
if (!(inherits(ir_data, "ir"))) {
cli::cli_abort(
"Error in {.fn PlotFTIR::ir_to_plotftir}. {.arg ir_data} must be of class {.cls ir}, produced by the {.pkg ir} package. You provided {.obj_type_friendly {ir_data}}."
)
Expand All @@ -564,7 +566,7 @@ ir_to_plotftir <- function(ir_data, what = NA) {
what <- seq_along(ir_data$spectra)
}

if (suppressWarnings(any(is.na(as.numeric(what))))) {
if (suppressWarnings(anyNA(as.numeric(what)))) {
if (all(what %in% ir_data$id_sample)) {
what <- which(what %in% ir_data$id_sample)
} else {
Expand Down Expand Up @@ -596,7 +598,7 @@ ir_to_df <- function(ir, what) {
}

# Param checks
if (!("ir" %in% class(ir))) {
if (!(inherits(ir, "ir"))) {
cli::cli_abort(
"Error in {.fn PlotFTIR::ir_to_df}. {.arg ir} must be of class {.cls ir}, produced by the {.pkg ir} package. You provided {.obj_type_friendly {ir}}."
)
Expand All @@ -620,12 +622,10 @@ ir_to_df <- function(ir, what) {

if (intensity == sample_intensity) {
ftir <- rbind(ftir, sampleir)
} else if (intensity == "absorbance") {
ftir <- rbind(ftir, transmittance_to_absorbance(sampleir))
} else {
if (intensity == "absorbance") {
ftir <- rbind(ftir, transmittance_to_absorbance(sampleir))
} else {
ftir <- rbind(ftir, absorbance_to_transmittance(sampleir))
}
ftir <- rbind(ftir, absorbance_to_transmittance(sampleir))
}
}

Expand Down Expand Up @@ -905,7 +905,7 @@ chemospec_to_plotftir <- function(csdata) {
}

# Param Checks
if (!("Spectra" %in% class(csdata))) {
if (!(inherits(csdata, "Spectra"))) {
cli::cli_abort(
"Error in {.fn PlotFTIR::chemospec_to_plotftir}. {.arg csdata} must be of class {.cls Spectra}, produced by the {.pkg ChemoSpec} package. You provided {.obj_type_friendly {csdata}}."
)
Expand Down
22 changes: 11 additions & 11 deletions R/manipulations.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ zoom_in_on_range <- function(ftir_spectra_plot, zoom_range = c(1000, 1900)) {
))
}

if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::zoom_in_on_range}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
}

if (!(length(zoom_range) == 2) || !all(is.numeric(zoom_range))) {
if (length(zoom_range) != 2 || !all(is.numeric(zoom_range))) {
cli::cli_abort(
"Error in {.fn PlotFTIR::zoom_in_on_range}. {.arg zoom_range} must be a numeric vector of length two."
)
Expand All @@ -69,7 +69,7 @@ zoom_in_on_range <- function(ftir_spectra_plot, zoom_range = c(1000, 1900)) {
}

if ("transmittance" %in% colnames(data)) {
if ('normal' %in% attr(ftir_spectra_plot, 'spectra_style')) {
if ("normal" %in% attr(ftir_spectra_plot, "spectra_style")) {
yrange <- c(0, 100)
} else {
yrange <- c(0, max(c(data$transmittance, 100), na.rm = TRUE))
Expand Down Expand Up @@ -160,7 +160,7 @@ compress_trans <- function(intercept = 2000, ratio = 5) {
i = "Did you accidentally put {.code -} on a new line?"
))
}
if (!ggplot2::is.ggplot(plot)) {
if (!ggplot2::is_ggplot(plot)) {
cli::cli_abort(
"You need to have a ggplot on the left side. You provided {.obj_type_friendly { plot }}."
)
Expand Down Expand Up @@ -228,7 +228,7 @@ compress_low_energy <- function(
))
}

if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::compress_low_energy}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
Expand Down Expand Up @@ -397,7 +397,7 @@ add_wavenumber_marker <- function(
text <- as.character(as.integer(wavenumber))
}

if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::add_wavenumber_marker}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
Expand Down Expand Up @@ -484,7 +484,7 @@ rename_plot_sample_ids <- function(ftir_spectra_plot, sample_ids) {
))
}

if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::rename_plot_sample_ids}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
Expand Down Expand Up @@ -608,7 +608,7 @@ move_plot_legend <- function(
i = "Install {.pkg ggplot2} with {.run install.packages('ggplot2')}"
))
}
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::move_plot_legend}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
Expand Down Expand Up @@ -708,7 +708,7 @@ highlight_sample <- function(ftir_spectra_plot, sample_ids, ...) {
))
}

if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::highlight_sample}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
Expand Down Expand Up @@ -803,7 +803,7 @@ add_band <- function(
))
}

if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
cli::cli_abort(
"Error in {.fn PlotFTIR::add_band}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
)
Expand All @@ -827,7 +827,7 @@ add_band <- function(
text <- ""
}

if (!(length(wavenumber_range) == 2) || !all(is.numeric(wavenumber_range))) {
if (length(wavenumber_range) != 2 || !all(is.numeric(wavenumber_range))) {
cli::cli_abort(
"Error in {.fn PlotFTIR::add_band}. {.arg wavenumber_range} must be a numeric vector of length two."
)
Expand Down
Loading