Skip to content

Commit 4e1d1aa

Browse files
committed
v0.5.2 improvements to reshape_peaktable
* Added `metadata` argument to `reshape_peaktable` for filtering metadata fields. * Added option to for renaming peaks via `reshape_peaktable` by providing a named list.
1 parent 3a25c55 commit 4e1d1aa

File tree

10 files changed

+57
-35
lines changed

10 files changed

+57
-35
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: chromatographR
33
Title: Chromatographic Data Analysis Toolset
4-
Version: 0.5.1
4+
Version: 0.5.2
55
Authors@R: c(
66
person("Ethan", "Bass", , "[email protected]", role = c("aut", "cre"),
77
comment = c(ORCID = "0000-0002-6175-6739")),

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# chromatographR 0.5.2
2+
3+
* Added `metadata` argument to `reshape_peaktable` for filtering metadata fields.
4+
* Added option to for renaming peaks via `reshape_peaktable` by providing a named list.
5+
16
# chromatographR 0.5.1
27

38
* In `plot_chroms`, `show_legend` now defaults to FALSE to prevent overloading of the plot.

R/correct_rt.R

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,18 @@ correct_rt <- function(chrom_list, lambdas, models = NULL, reference = 'best',
191191
})
192192
})
193193
# fix times
194-
old_ts <- c(rep(NA,short), get_times(chrom_list_og, index = reference))
194+
old_ts <- c(rep(NA, short), get_times(chrom_list_og, index = reference))
195195
times <- suppressWarnings(stats::approx(x = jset[,reference],
196196
y = old_ts, 1:jmax)$y)
197197
idx_start <- which.min(times)
198198
if (idx_start > 1){
199-
beg <- sort(seq(from = times[idx_start]-res, by = -res, length.out = idx_start-1),
200-
decreasing = FALSE)
199+
beg <- sort(seq(from = times[idx_start] - res, by = -res,
200+
length.out = idx_start - 1), decreasing = FALSE)
201201
} else beg <- NULL
202202
idx_end <- which.max(times)
203203
if (idx_end < length(times)){
204-
end <- seq(from = times[idx_end]+res, length.out = length(times) - idx_end, by = res)
204+
end <- seq(from = times[idx_end] + res,
205+
length.out = length(times) - idx_end, by = res)
205206
} else end <- NULL
206207
new.times <- c(beg, times[!is.na(times)], end)
207208
result <- mapply(function(x,idx){
@@ -248,16 +249,15 @@ correct_rt <- function(chrom_list, lambdas, models = NULL, reference = 'best',
248249
#' @export correct_peaks
249250
correct_peaks <- function(peak_list, mod_list){
250251
mapply(function(samp, mod){
251-
lapply(samp,
252-
function(profile){
253-
if (nrow(profile) > 0) {
252+
lapply(samp, function(profile){
253+
if (nrow(profile) > 0){
254254
cbind(profile,
255255
rt.cor = c(predict(mod, profile[,1], what = "time")))
256256
} else {
257257
cbind(profile, rt.cor = rep(0, 0))
258258
}
259-
})},
260-
peak_list, mod_list, SIMPLIFY = FALSE)
259+
}
260+
)}, peak_list, mod_list, SIMPLIFY = FALSE)
261261
}
262262

263263
#' Plot PTW alignments
@@ -278,29 +278,33 @@ plot.ptw_list <- function(x, lambdas, legend = TRUE, ...){
278278
ts <- as.numeric(colnames(x[[1]]$sample))
279279

280280
if (missing(lambdas)){
281-
lambdas<-all.lambdas
281+
lambdas <- all.lambdas
282282
}
283283
if (any(!(lambdas %in% all.lambdas))){
284284
stop("Lambdas not found. Please check argument and try again")
285285
}
286286

287287
lambda.idx <- which(lambdas %in% all.lambdas)
288-
288+
# plot warped samples
289289
plot.new()
290290
plot.window(xlim=c(head(ts,1), tail(ts,1)),
291-
ylim=c(0, max(sapply(x, function(xx) xx$warped.sample), na.rm=TRUE)*1.2))
291+
ylim=c(0, max(sapply(x, function(xx) xx$warped.sample), na.rm = TRUE)*1.2))
292292
for (i in seq_along(x)){
293-
matplot(ts, t(x[[i]]$warped.sample[lambda.idx,, drop=FALSE]), type='l',add=TRUE)
293+
matplot(ts, t(x[[i]]$warped.sample[lambda.idx, , drop = FALSE]),
294+
type = 'l', add = TRUE)
294295
}
295-
if (legend)
296-
legend("topright", legend="ptw", bty = "n")
297-
296+
if (legend){
297+
legend("topright", legend = "ptw", bty = "n")
298+
}
299+
# plot reference
298300
plot.new()
299-
plot.window(xlim=c(head(ts,1),tail(ts,1)),
300-
ylim=c(0, max(x[[i]]$reference, na.rm = TRUE)*1.2))
301+
plot.window(xlim = c(head(ts,1), tail(ts,1)),
302+
ylim = c(0, max(x[[i]]$reference, na.rm = TRUE)*1.2))
301303
for (i in seq_along(x)){
302-
matplot(ts, t(x[[i]]$sample[lambda.idx,, drop=FALSE]), type='l', add=TRUE)
304+
matplot(ts, t(x[[i]]$sample[lambda.idx, , drop = FALSE]),
305+
type = 'l', add = TRUE)
306+
}
307+
if (legend){
308+
legend("topright", legend = "queries", bty = "n")
303309
}
304-
if (legend)
305-
legend("topright", legend="queries", bty = "n")
306310
}

R/filter_peaks.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ filter_peaktable <- function(peak_table, rts, min_rt, max_rt, min_value, lambda,
139139
} else (idx.lambda <- seq_along(peak_table$tab))
140140
idx <- Reduce(intersect, list(idx.rt, idx.val, idx.lambda))
141141
peak_table$tab <- peak_table$tab[,idx, drop = FALSE]
142-
peak_table$pk_meta <- peak_table$pk_meta[,idx, drop = FALSE]
142+
peak_table$pk_meta <- peak_table$pk_meta[, idx, drop = FALSE]
143143
if (inherits(peak_table$ref_spectra, c("data.frame", "matrix"))){
144-
peak_table$ref_spectra <- peak_table$ref_spectra[,idx, drop = FALSE]
144+
peak_table$ref_spectra <- peak_table$ref_spectra[, idx, drop = FALSE]
145145
}
146146
peak_table
147147
}

R/get_peaktable.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
#' get_peaktable(pks, response = "area")
9090
#' @seealso \code{\link{attach_ref_spectra}} \code{\link{attach_metadata}}
9191
#' @export get_peaktable
92-
92+
9393
get_peaktable <- function(peak_list, chrom_list, response = c("area", "height"),
9494
use.cor = FALSE, hmax = 0.2, plot_it = FALSE,
9595
ask = plot_it, clust = c("rt","sp.rt"),

R/reshape_chroms.R

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,26 @@ reshape_chrom <- function(x, lambdas = NULL, rts = NULL){
6464
#' Reshapes peak table from wide to long format
6565
#' @name reshape_peaktable
6666
#' @param x A \code{peak_table} object.
67-
#' @param peaks A character vector specifying peaks to include.
67+
#' @param peaks A character vector specifying the peaks to include. If the
68+
#' character vector is named, the names of the vector elements will be used in
69+
#' place of the original peak names.
70+
#' @param metadata A character vector specifying the metadata fields to include.
6871
#' @return A data.frame containing the information for the specified peaks in
6972
#' long format.
7073
#' @author Ethan Bass
7174
#' @export
7275

73-
reshape_peaktable <- function(x, peaks){
76+
reshape_peaktable <- function(x, peaks, metadata){
7477
if (!missing(peaks)){
75-
x$tab <- x$tab[,which(colnames(x$tab) %in% peaks), drop=FALSE]
78+
x$tab <- x$tab[,which(colnames(x$tab) %in% peaks), drop = FALSE]
79+
if (!is.null(names(peaks))){
80+
colnames(x$tab) <- names(peaks)
81+
}
82+
}
83+
if (!missing(metadata)){
84+
x$sample_meta <- x$sample_meta[,which(colnames(x$sample_meta) %in% metadata), drop = FALSE]
7685
}
77-
xx <- reshape(as.data.frame(chr=rownames(x$tab), x$tab), direction = "long",
86+
xx <- reshape(as.data.frame(chr = rownames(x$tab), x$tab), direction = "long",
7887
varying = list(1:ncol(x$tab)), v.names = x$args[["response"]],
7988
times = colnames(x$tab), timevar = "peak",
8089
idvar = "sample", ids = rownames(x$tab))

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ Also see the [contributing.md](https://github.com/ethanbass/chromatographR/blob/
5757

5858
If you use chromatographR in published work, please cite it as follows:
5959

60-
Bass, E. (2023). chromatographR: chromatographic data analysis toolset (version 0.5.1).
60+
Bass, E. (2023). chromatographR: chromatographic data analysis toolset (version 0.5.2).
6161
http://doi.org/10.5281/zenodo.6944334

inst/CITATION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ citEntry(
55
title = "chromatographR: chromatographic data analysis toolset",
66
author = "Ethan Bass",
77
year = "2023",
8-
note = "version 0.5.1",
8+
note = "version 0.5.2",
99
url = "https://ethanbass.github.io/chromatographR/",
1010
doi = "10.5281/zenodo.6944334",
1111
textVersion = paste("Bass, E. (2023).",
12-
"chromatographR: chromatographic data analysis toolset (version 0.5.1).",
12+
"chromatographR: chromatographic data analysis toolset (version 0.5.2).",
1313
"http://doi.org/10.5281/zenodo.6944334"
1414
)
1515
)

man/reshape_peaktable.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-utility-functions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ test_that("check_for_pkg functions as expected",{
9797

9898
test_that("reshape_peaktable works as expected",{
9999
data(pk_tab)
100-
pktab_long<-reshape_peaktable(pk_tab)
100+
pktab_long <- reshape_peaktable(pk_tab)
101101
expect_equal(ncol(pktab_long),4)
102102
expect_equal(nrow(pktab_long), nrow(pk_tab)*ncol(pk_tab))
103103
})

0 commit comments

Comments
 (0)