Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hechth committed Jul 26, 2024
1 parent 5b0d831 commit 9fa9225
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description: This is a customized fork of the original work from Tianwei Yu.
It takes the adaptive processing of LC/MS metabolomics data further
with focus on high resolution MS for both LC and GC applications.
Depends: R (>= 3.50), MASS, mzR, splines, doParallel, foreach,
snow, dplyr, tidyr, stringr, tibble, tools, arrow
snow, dplyr, tidyr, stringr, tibble, tools, arrow, plyr
biocViews: Technology, MassSpectrometry
License: GPL-2
LazyLoad: yes
Expand Down
31 changes: 21 additions & 10 deletions R/feature.align.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ create_metadata <- function(sample_grouped, sample_names) {
#' @export
create_intensity_row <- function(sample_grouped) {
sample_grouped %>%
group_by(sample_id) %>%
summarise(intensity = sum(area)) %>%
pivot_wider(names_from = "sample_id", values_from = "intensity")
dplyr::group_by(sample_id) %>%
dplyr::summarise(intensity = sum(area)) %>%
tidyr::pivot_wider(names_from = "sample_id", values_from = "intensity")
}

#' Compute median RT for each sample
Expand All @@ -41,9 +41,9 @@ create_intensity_row <- function(sample_grouped) {
#' @export
create_rt_row <- function(sample_grouped) {
sample_grouped %>%
group_by(sample_id) %>%
summarise(rt = median(rt)) %>%
pivot_wider(names_from = "sample_id", values_from = "rt")
dplyr::group_by(sample_id) %>%
dplyr::summarise(rt = median(rt)) %>%
tidyr::pivot_wider(names_from = "sample_id", values_from = "rt")
}

#' Create a list containing 3 tibbles: metadata, intensities and RTs.
Expand Down Expand Up @@ -161,10 +161,21 @@ comb <- function(x, ...) {
#' @return Cleaned tibble.
#' @export
clean_data_matrix <- function(x, sample_names) {
x %>%
x <- x %>%
replace(is.na(.), 0) %>%
dplyr::relocate(sample_names) %>%
as_tibble()
dplyr::relocate(sample_names) |>
add_feature_ids()
return(x)
}

#' Add `id` column to a dataframe
#' @param x A dataframe
#' @return The same dataframe but with an additional `id` column
#' in first place which contains the rownames.
#' @export
add_feature_ids <- function(x) {
x$id <- as.numeric(rownames(x))
return(tibble::as_tibble(x |> dplyr::relocate(id)))
}

#' Align peaks from spectra into a feature table.
Expand Down Expand Up @@ -217,7 +228,7 @@ create_aligned_feature_table <- function(features_table,

aligned_features$intensity <- clean_data_matrix(aligned_features$intensity, sample_names)
aligned_features$rt <- clean_data_matrix(aligned_features$rt, sample_names)
aligned_features$metadata <- as_tibble(aligned_features$metadata)
aligned_features$metadata <- add_feature_ids(aligned_features$metadata)

return(aligned_features)
}
7 changes: 4 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ register_functions_to_cluster <- function(cluster) {
'prof.to.features',
'load.lcms',
'adaptive.bin',
'add_feature_ids',
'find.turn.point',
'msExtrema',
'find_local_maxima',
Expand Down Expand Up @@ -113,9 +114,9 @@ load_aligned_features <- function(metadata_file, intensities_file, rt_file, tol_
tolerances <- arrow::read_parquet(tol_file)

result <- list()
result$metadata <- as_tibble(metadata) |> select(-id)
result$intensity <- as_tibble(intensities) |> select(-id)
result$rt <- as_tibble(rt) |> select(-id)
result$metadata <- as_tibble(metadata)
result$intensity <- as_tibble(intensities)
result$rt <- as_tibble(rt)
result$mz_tol_relative <- tolerances$mz_tolerance
result$rt_tol_relative <- tolerances$rt_tolerance
return(result)
Expand Down
1 change: 1 addition & 0 deletions conda/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ dependencies:
- r-tidyr
- r-stringr
- r-tibble
- r-plyr

6 changes: 6 additions & 0 deletions tests/testthat/test-feature-align.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
update_expected <- function(actual) {
arrow::write_parquet(actual$metadata, file.path("..", "testdata", "aligned", "metadata_table.parquet"))
arrow::write_parquet(actual$intensity, file.path("..", "testdata", "aligned", "intensity_table.parquet"))
arrow::write_parquet(actual$rt, file.path("..", "testdata", "aligned", "rt_table.parquet"))
}

patrick::with_parameters_test_that(
"feature.align test",
{
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-hybrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ patrick::with_parameters_test_that("basic hybrid test", {
actual <- as_tibble(result$recovered_feature_sample_table)
keys <- c("mz", "rt", "sample", "sample_rt", "sample_intensity")

# arrow::write_parquet(actual, file.path(testdata, "hybrid", paste0(.test_name, "_recovered_feature_sample_table.parquet")))
expected <- arrow::read_parquet(
file.path(testdata, "hybrid", paste0(.test_name, "_recovered_feature_sample_table.parquet"))
)
Expand Down

0 comments on commit 9fa9225

Please sign in to comment.