From 676c56e008626532b84050470c4640114260752d Mon Sep 17 00:00:00 2001 From: qzhang503 Date: Sun, 3 Dec 2023 07:57:27 -0600 Subject: [PATCH] 2023-12-03 --- R/ms2frames.R | 8 +++++--- R/msmsmatches2.R | 5 +++-- R/utils_os.R | 38 +++++++++++++++++++------------------- man/delete_files.Rd | 4 +++- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/R/ms2frames.R b/R/ms2frames.R index c9105f3..cff3fec 100644 --- a/R/ms2frames.R +++ b/R/ms2frames.R @@ -96,6 +96,7 @@ hpair_mgths <- function (ms1_offset = 0, notch = NULL, mgfs, n_modules, else { mgfs <- make_dia_mgfs(mgfs = mgfs, mgf_path = mgf_path, quant = quant, min_mass = min_mass, ppm_ms1_bin = ppm_ms1_bin) + mgfs <- mgfs[names(mgfs) != "-Inf"] # unknown precursors } # to chunks: each chunk has multiple frames: each frame multiple precursors @@ -265,13 +266,14 @@ make_dia_mgfs <- function (mgfs, mgf_path, quant = "none", min_mass = 200L, data_ms2 <- data_ms2[seqs, ] mgfs <- dplyr::bind_cols(datalist, dataflat, data_ms2) - # rm(list = c("datalist", "dataflat", "scans", "data_ms2", - # "col_nms", "cols_list", "cols_flat", "cols_ms2")) - ord <- order(mgfs$ms1_mass) mgfs <- mgfs[ord, ] ms1_bins <- ceiling(log(mgfs$ms1_mass/min_mass)/log(1+ppm_ms1_bin/1e6)) + # ms1_bins can be -Inf or NA since + # (1) ms1_moverz can be NA because not yet determined by the current algorithm + # (2) ms1_mass can also be NA due to ms1_charge being NA + # (3) ms1_charge can be zero since undetermined mgfs <- split(mgfs, ms1_bins) } diff --git a/R/msmsmatches2.R b/R/msmsmatches2.R index 0e88976..d5d655c 100644 --- a/R/msmsmatches2.R +++ b/R/msmsmatches2.R @@ -98,12 +98,13 @@ ms2match <- function (mgf_path, aa_masses_all, out_path, .path_bin, delete_files( out_path, - ignores = c(mgf_path, "\\.[Rr]$", "\\.(mgf|MGF)$", "\\.(mzML|mzml)$", + ignores = c("\\.[Rr]$", "\\.(mgf|MGF)$", "\\.(mzML|mzml)$", "\\.xlsx$", "\\.xls$", "\\.csv$", "\\.txt$", "\\.pars$", "^mgf$", "^mgfs$", "^mzML$", "^mzMLs$", "Calls", "^PSM$", "^Peptide$", "^Protein$", "fraction_scheme.rda", "label_scheme.rda", - "label_scheme_full.rda")) + "label_scheme_full.rda"), + paths_excluded = mgf_path) # pairs expts and theos files_a <- list.files(mgf_path, pattern = "^expttheo_", full.names = TRUE) diff --git a/R/utils_os.R b/R/utils_os.R index 54814e6..0b18621 100644 --- a/R/utils_os.R +++ b/R/utils_os.R @@ -557,8 +557,9 @@ match_calltime <- function (path = "~/mzion/.MSearches/Cache/Calls", #' #' @param path A file path. #' @param ignores The file extensions to be ignored. +#' @param paths_excluded The path to be ignored. #' @param ... Arguments for file.remove. -delete_files <- function (path, ignores = NULL, ...) +delete_files <- function (path, ignores = NULL, paths_excluded = NULL, ...) { # Hadley Ch.19 Quasiquotation, note 97 @@ -577,27 +578,26 @@ delete_files <- function (path, ignores = NULL, ...) nms <- do.call(list.files, args) - if (!is.null(ignores)) { - nms <- local({ - dirs <- list.dirs(path, full.names = FALSE, recursive = recursive) - dirs <- dirs[dirs != ""] + if (length(nms) && length(paths_excluded)) { + ignore_case <- if (Sys.info()['sysname'] == "Windows") TRUE else FALSE + + for (i in seq_along(paths_excluded)) + nms <- nms[!grepl(paths_excluded[[i]], nms, ignore.case = ignore_case)] + } - idxes_kept <- purrr::map_lgl(dirs, function (x) any(grepl(x, ignores))) - - nms_kept <- list.files(path = file.path(path, dirs[idxes_kept]), - recursive = TRUE, full.names = TRUE) - - nms[! nms %in% nms_kept] - }) + if (!is.null(ignores)) { + dirs <- list.dirs(path, full.names = FALSE, recursive = recursive) + dirs <- dirs[dirs != ""] + oks <- purrr::map_lgl(dirs, function (x) any(grepl(x, ignores))) + nms_oks <- list.files(path = file.path(path, dirs[oks]),recursive = TRUE, + full.names = TRUE) + nms <- nms[!nms %in% nms_oks] - nms <- local({ - exts <- gsub("^.*(\\.[^.]*)$", "\\1", nms) - idxes_kept <- purrr::map_lgl(exts, function (x) any(grepl(x, ignores))) - - nms[!idxes_kept] - }) + exts <- gsub("^.*(\\.[^.]*)$", "\\1", nms) + oks <- purrr::map_lgl(exts, function (x) any(grepl(x, ignores))) + nms <- nms[!oks] } - + if (length(nms)) suppressMessages(file.remove(file.path(nms))) diff --git a/man/delete_files.Rd b/man/delete_files.Rd index 8bcb081..18ff54e 100644 --- a/man/delete_files.Rd +++ b/man/delete_files.Rd @@ -4,13 +4,15 @@ \alias{delete_files} \title{Deletes files under a directory.} \usage{ -delete_files(path, ignores = NULL, ...) +delete_files(path, ignores = NULL, paths_excluded = NULL, ...) } \arguments{ \item{path}{A file path.} \item{ignores}{The file extensions to be ignored.} +\item{paths_excluded}{The path to be ignored.} + \item{...}{Arguments for file.remove.} } \description{