From 7889ed598130a2c0325e14cdf21e8b8d957280e7 Mon Sep 17 00:00:00 2001 From: hechth Date: Tue, 23 Jul 2024 12:22:37 +0200 Subject: [PATCH] started refactoring --- DESCRIPTION | 2 +- R/adjust.time.R | 8 +++--- tests/testthat/test-adjust-time.R | 42 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 80196d4..3b1effc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,4 +17,4 @@ NeedsCompilation: no Suggests: dataCompareR, testthat (>= 3.0.0), microbenchmark Config/testthat/edition: 3 -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 diff --git a/R/adjust.time.R b/R/adjust.time.R index 9a9a0bb..11d0a74 100644 --- a/R/adjust.time.R +++ b/R/adjust.time.R @@ -6,9 +6,8 @@ NULL compute_comb <- function(template_features, features) { combined <- dplyr::bind_rows( template_features, - dplyr::bind_cols(features |> dplyr::select(c(mz, rt, cluster)), sample_id = features$sample_id) - ) - combined <- combined |> dplyr::arrange_at("mz") + features |> dplyr::select(c(mz, rt, cluster, sample_id)) + ) |> dplyr::arrange_at(c("cluster","mz")) return(combined) } @@ -38,6 +37,7 @@ compute_template_adjusted_rt <- function(combined, sel, j) { #' @export compute_corrected_features <- function(features, delta_rt, avg_time) { features <- features[order(features$rt, features$mz), ] + corrected <- features$rt original <- features$rt to_correct <- original[original >= min(delta_rt) & @@ -90,7 +90,7 @@ correct_time <- function(this.feature, template_features) { orig.features <- this.feature template <- unique(template_features$sample_id)[1] j <- unique(this.feature$sample_id)[1] - + if (j != template) { this.comb <- compute_comb(template_features, this.feature) sel <- compute_sel(this.comb) diff --git a/tests/testthat/test-adjust-time.R b/tests/testthat/test-adjust-time.R index a57910f..fdd65ab 100644 --- a/tests/testthat/test-adjust-time.R +++ b/tests/testthat/test-adjust-time.R @@ -41,3 +41,45 @@ patrick::with_parameters_test_that( ) ) ) + +test_that("compute_sel", { + testdata <- file.path("..", "testdata") + template_features <- file.path(testdata, "template", "RCX_shortened.parquet") + template_features <- arrow::read_parquet(template_features) + + clustered_table <- read_parquet_files( + c("RCX_06_shortened"), + "clusters", + "_extracted_clusters.parquet" + )[[1]] + + all_features <- compute_template_adjusted_rt( + compute_comb(template_features, clustered_table), + compute_sel(combined), + "RCX_07_shortened" + ) + + subsets <- template_features |> + dplyr::bind_rows(clustered_table |> dplyr::select(c(mz, rt, cluster, sample_id))) |> + dplyr::arrange_at(c("cluster","mz")) |> + dplyr::group_by(cluster) |> + mutate(count = n_distinct(sample_id)) |> + filter(count == 2) |> + add_count() |> + filter(n == 2) |> + ungroup() |> + group_by(sample_id) |> + group_split() + + all_features_new <- cbind(subsets[[1]]$rt, subsets[[2]]$rt) + all_features_new_order <- order(all_features_new[,2]) + all_features_new_arranged <- all_features_new[all_features_new_order,] + + this.feature <- compute_corrected_features( + clustered_table, + all_features_new_arranged[, 2], + all_features_new_arranged[, 1] - all_features_new_arranged[, 2] + ) + + browser() +}) \ No newline at end of file