Mercurial > repos > recetox > test_recetox_aplcms_unsupervised
comparison main.R @ 0:be51059c2384 draft default tip
"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/recetox_aplcms commit 65d42862f9265e8ba3783368ac0bddb154e3a427-dirty"
| author | recetox |
|---|---|
| date | Fri, 18 Jun 2021 16:36:23 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:be51059c2384 |
|---|---|
| 1 library(recetox.aplcms) | |
| 2 | |
| 3 save_extracted_features <- function(df, filename) { | |
| 4 df <- as.data.frame(df) | |
| 5 columns <- c("mz", "pos", "sd1", "sd2", "area") | |
| 6 arrow::write_parquet(df[columns], filename) | |
| 7 } | |
| 8 | |
| 9 save_feature_sample_table <- function(df, filename) { | |
| 10 columns <- c("feature", "mz", "rt", "sample", "sample_rt", "sample_intensity") | |
| 11 arrow::write_parquet(df[columns], filename) | |
| 12 } | |
| 13 | |
| 14 known_table_columns <- function() { | |
| 15 c("chemical_formula", "HMDB_ID", "KEGG_compound_ID", "mass", "ion.type", | |
| 16 "m.z", "Number_profiles_processed", "Percent_found", "mz_min", "mz_max", | |
| 17 "RT_mean", "RT_sd", "RT_min", "RT_max", "int_mean(log)", "int_sd(log)", | |
| 18 "int_min(log)", "int_max(log)") | |
| 19 } | |
| 20 | |
| 21 save_known_table <- function(df, filename) { | |
| 22 columns <- known_table_columns() | |
| 23 arrow::write_parquet(df[columns], filename) | |
| 24 } | |
| 25 | |
| 26 read_known_table <- function(filename) { | |
| 27 arrow::read_parquet(filename, col_select = known_table_columns()) | |
| 28 } | |
| 29 | |
| 30 save_pairing <- function(df, filename) { | |
| 31 write.table(df, filename, row.names = FALSE, col.names = c("new", "old")) | |
| 32 } | |
| 33 | |
| 34 save_all_extracted_features <- function(dfs, filenames) { | |
| 35 filenames <- tools::file_path_sans_ext(basename(filenames)) | |
| 36 filenames <- paste0(filenames, ".parquet") | |
| 37 filenames <- file.path("extracted", filenames) | |
| 38 dir.create("extracted") | |
| 39 mapply(save_extracted_features, dfs, filenames) | |
| 40 } | |
| 41 | |
| 42 save_all_corrected_features <- function(dfs, filenames) { | |
| 43 filenames <- tools::file_path_sans_ext(basename(filenames)) | |
| 44 filenames <- paste0(filenames, ".parquet") | |
| 45 filenames <- file.path("corrected", filenames) | |
| 46 dir.create("corrected") | |
| 47 mapply(save_extracted_features, dfs, filenames) | |
| 48 } | |
| 49 | |
| 50 unsupervised_main <- function(sample_files, aligned_file, recovered_file, ...) { | |
| 51 sample_files <- sort_samples_by_acquisition_number(sample_files) | |
| 52 | |
| 53 res <- unsupervised(filenames = sample_files, ...) | |
| 54 | |
| 55 save_all_extracted_features(res$extracted_features, sample_files) | |
| 56 save_all_corrected_features(res$corrected_features, sample_files) | |
| 57 | |
| 58 save_feature_sample_table(res$aligned_feature_sample_table, aligned_file) | |
| 59 save_feature_sample_table(res$recovered_feature_sample_table, recovered_file) | |
| 60 } | |
| 61 | |
| 62 hybrid_main <- function(sample_files, known_table_file, updated_known_table_file, pairing_file, aligned_file, recovered_file, ...) { | |
| 63 sample_files <- sort_samples_by_acquisition_number(sample_files) | |
| 64 | |
| 65 known <- read_known_table(known_table_file) | |
| 66 res <- hybrid(filenames = sample_files, known_table = known, ...) | |
| 67 | |
| 68 save_known_table(res$updated_known_table, updated_known_table_file) | |
| 69 save_pairing(res$features_known_table_pairing, pairing_file) | |
| 70 | |
| 71 save_all_extracted_features(res$extracted_features, sample_files) | |
| 72 save_all_corrected_features(res$corrected_features, sample_files) | |
| 73 | |
| 74 save_feature_sample_table(res$aligned_feature_sample_table, aligned_file) | |
| 75 save_feature_sample_table(res$recovered_feature_sample_table, recovered_file) | |
| 76 } |
