From fc0e9daa530dd0e22d35e2796d6805d989d7f745 Mon Sep 17 00:00:00 2001 From: Ethan Bass Date: Sat, 16 Nov 2024 22:49:53 -0500 Subject: [PATCH] Delete .Rhistory --- .Rhistory | 512 ------------------------------------------------------ 1 file changed, 512 deletions(-) delete mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index 6df726e..0000000 --- a/.Rhistory +++ /dev/null @@ -1,512 +0,0 @@ -call. = FALSE -) -} -R.format <- match.arg(R.format, c("matrix", "data.frame")) -format.in <- match.arg(format.in, c("chemstation.uv", "masshunter.dad")) -exists <- dir.exists(paths) | file.exists(paths) -if (mean(exists) == 0){ -stop("Cannot locate files. None of the supplied paths exist.") -} -if (export){ -if (is.null(path.out)){ -ans <- readline(".........Export directory not specified. -.........Export files to current working directory (y/n)? ") -if (ans %in% c("y","Y")){ -path.out <- getwd() -} else{ -stop("Must specify directory to export files.") -} -} -path.out <- gsub("/$","", path.out) -if (!dir.exists(path.out)){ -stop(paste0("The export directory '", path.out, "' does not exist.")) -} -} -if (is.null(dat)){ -dat<-list()} -# choose converter -if (format.in == "masshunter.dad"){ -pattern <- ifelse(is.null(pattern),".sp", pattern) -converter <- ifelse(parser=="aston", sp_converter, entab_reader) -} else if (format.in=="chemstation.uv"){ -pattern <- ifelse(is.null(pattern),".uv", pattern) -converter <- ifelse(parser=="aston", trace_converter, entab_reader) -} else{ -converter <- ifelse(parser=="aston", trace_converter, entab_reader) -} -if (find_files){ -files <- unlist(lapply(paths, function(path){ -files <- list.files(path = path, pattern = pattern, -full.names = TRUE, recursive = TRUE) -if (length(files)==0){ -if (!dir.exists(path)){ -warning(paste0("The directory '", basename(path), "' does not exist.")) -} else{ -warning(paste0("No files matching the pattern '", pattern, -"' were found in '", basename(path), "'")) -} -} -files -})) -} else{ -files <- paths -match <- grep(pattern, files) -if (length(match)==0){ -warning("The provided files do not match the expected file extension. -Please confirm that the specified format ('format.in') is correct.", -immediate. = TRUE) -} else if (length(match) < length(files)){ -warning(paste("Some of the files do not have the expected file extension:", -files[match]), immediate. = TRUE) -} -} -file_names <- strsplit(files, "/") -file_names <- gsub("\\.[Dd]", "", -sapply(file_names, function(n) n[grep("\\.[Dd]", n)])) -data <- lapply(X=files, function(f){ -df <- converter(f) -}) -if (R.format == "matrix"){ -data <- lapply(data, FUN=as.matrix)} -names(data) <- file_names -if (export){ -sapply(seq_along(data), function(i){ -write.csv(data[[i]], file = paste0(paste(path.out,names(data)[i], sep="/"),".CSV")) -}) -} -dat <- append(dat,data) -dat -} -#' -#' Converts a single chromatogram from MassHunter \code{.sp} format to R \code{data.frame}. -#' -#' Uses the [Aston](https://github.com/bovee/aston) file parser. -#' -#' @name sp_converter -#' @param file path to file -#' @return A data.frame object (retention time x wavelength). -#' @import reticulate -#' @export sp_converter -sp_converter <- function(file){ -df <- trace_file$agilent_uv$AgilentDAD(file) -pd$DataFrame(df$data$values, columns=df$data$columns, -index=df$data$index) -} -#' -#' Converts a single chromatogram from ChemStation \code{.uv} format to R \code{data.frame}. -#' -#' Uses the [Aston](https://github.com/bovee/aston) file parser. -#' -#' @name uv_converter -#' @param file path to file -#' @return A data.frame object (retention time x trace). -#' @import reticulate -#' @export uv_converter -uv_converter <- function(file){ -trace_file <- reticulate::import("aston.tracefile") -pd <- reticulate::import("pandas") -df <- trace_file$TraceFile(file) -df <- pd$DataFrame(df$data$values, columns=df$data$columns, -index=df$data$index) -# multiply by empirical correction value -apply(df,2,function(xx)xx*0.9536743164062551070259132757200859487056732177734375) -} -#' Aston TraceFile Converter -#' -#' Uses Aston parser to figure out file-type and convert to R \code{data.frame}. -#' @name trace_converter -#' @title generic converter for other types of files -#' @param file path to file -#' @return A data.frame object (retention time x trace). -#' @import reticulate -#' @noRd -trace_converter <- function(file){ -trace_file <- reticulate::import("aston.tracefile") -pd <- reticulate::import("pandas") -df <- trace_file$TraceFile(file) -pd$DataFrame(df$data$values, columns=df$data$columns, -index=df$data$index) -} -parser <- match.arg (parser, c("aston","entab")) -if (parser == "entab" & !requireNamespace("entab", quietly = TRUE)) { -stop( -"The entab R package must be installed to use entab parsers.", -call. = FALSE -) -} -R.format <- match.arg(R.format, c("matrix", "data.frame")) -format.in <- match.arg(format.in, c("chemstation.uv", "masshunter.dad")) -exists <- dir.exists(paths) | file.exists(paths) -if (mean(exists) == 0){ -stop("Cannot locate files. None of the supplied paths exist.") -} -if (export){ -if (is.null(path.out)){ -ans <- readline(".........Export directory not specified. -.........Export files to current working directory (y/n)? ") -if (ans %in% c("y","Y")){ -path.out <- getwd() -} else{ -stop("Must specify directory to export files.") -} -} -path.out <- gsub("/$","", path.out) -if (!dir.exists(path.out)){ -stop(paste0("The export directory '", path.out, "' does not exist.")) -} -} -if (is.null(dat)){ -dat<-list()} -# choose converter -if (format.in == "masshunter.dad"){ -pattern <- ifelse(is.null(pattern),".sp", pattern) -converter <- ifelse(parser=="aston", sp_converter, entab_reader) -} else if (format.in=="chemstation.uv"){ -pattern <- ifelse(is.null(pattern),".uv", pattern) -converter <- ifelse(parser=="aston", trace_converter, entab_reader) -} else{ -converter <- ifelse(parser=="aston", trace_converter, entab_reader) -} -if (find_files){ -files <- unlist(lapply(paths, function(path){ -files <- list.files(path = path, pattern = pattern, -full.names = TRUE, recursive = TRUE) -if (length(files)==0){ -if (!dir.exists(path)){ -warning(paste0("The directory '", basename(path), "' does not exist.")) -} else{ -warning(paste0("No files matching the pattern '", pattern, -"' were found in '", basename(path), "'")) -} -} -files -})) -} else{ -files <- paths -match <- grep(pattern, files) -if (length(match)==0){ -warning("The provided files do not match the expected file extension. -Please confirm that the specified format ('format.in') is correct.", -immediate. = TRUE) -} else if (length(match) < length(files)){ -warning(paste("Some of the files do not have the expected file extension:", -files[match]), immediate. = TRUE) -} -} -file_names <- strsplit(files, "/") -file_names <- gsub("\\.[Dd]", "", -sapply(file_names, function(n) n[grep("\\.[Dd]", n)])) -data <- lapply(X=files, function(f){ -df <- converter(f) -}) -files -converter -converter(f) -files -converter(files[1]) -file<-files[1] -r <- entab::Reader(file) -r -df <- as.data.frame(r) -library(entab) -df <- as.data.frame(r) -df -entab::as.data.frame -detach(entab) -detach("entab") -detach(package:entab) -r <- entab::Reader(file) -df <- entab::as.data.frame(r) -df -if (format.out=="wide"){ -df <- data.frame(pivot_wider(df, id_cols = "time", -names_from = "wavelength", values_from = "intensity"))} -meta <- r$metadata() -source("~/R_packages/chromConverter/R/read_chroms.R", echo=TRUE) -chr <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv",parser="entab") -library(tidyr) -devtools::document() -chr <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv",parser="entab") -chr -chr <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="aston") -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[,100],type='l') -matplot(chr[,50],type='l') -matplot(chr[[1]][,50],type='l') -matplot(chr2[[1]][,50],type='l') -matplot(chr[[1]][,50],type='l') -matplot(chr2[[1]][,50],type='l',add=T) -chr -matplot(chr2[[1]][,"250.0"],type='l',add=T) -chr2 -matplot(chr2[[1]][,"X250"],type='l',add=T) -matplot(chr[[1]][,"250.0"],type='l') -matplot(chr2[[1]][,"X250"],type='l',add=T) -matplot(chr2[[1]][,"X230"],type='l',add=T) -matplot(chr[[1]][,"230.0"],type='l') -matplot(chr2[[1]][,"X230"],type='l',add=T) -matplot(chr2[[1]][,"X210"],type='l',add=T) -matplot(chr[[1]][,"210.0"],type='l') -matplot(chr2[[1]][,"X210"],type='l',add=T) -#' -#' Converts a single chromatogram from ChemStation \code{.uv} format to R \code{data.frame}. -#' -#' Uses the [Aston](https://github.com/bovee/aston) file parser. -#' -#' @name uv_converter -#' @param file path to file -#' @return A data.frame object (retention time x trace). -#' @import reticulate -#' @export uv_converter -uv_converter <- function(file, correction=TRUE){ -trace_file <- reticulate::import("aston.tracefile") -pd <- reticulate::import("pandas") -df <- trace_file$TraceFile(file) -df <- pd$DataFrame(df$data$values, columns=df$data$columns, -index=df$data$index) -# multiply by empirical correction value -if (correction) -apply(df,2,function(xx)xx*0.9536743164062551070259132757200859487056732177734375) -} -ath -path -uv_converter(path) -source("~/R_packages/chromConverter/R/read_chroms.R", echo=TRUE) -chr <- uv_converter(path,correction=F) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[[1]][,"250.0"],type='l') -matplot(chr2[[1]][,"X250"],type='l',add=T) -chr2 -matplot(chr[[1]][,"250.0"],type='l') -chr[[1]] -chr -chr -chr <- uv_converter(path, correction=F) -chr -chr <- uv_converter(path, correction=T) -chr -source("~/R_packages/chromConverter/R/read_chroms.R", echo=TRUE) -chr <- uv_converter(path, correction=F) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[[1]][,"250.0"],type='l') -chr -matplot(chr[[1]][,"250.0"],type='l') -matplot(chr[,"250.0"],type='l') -matplot(chr2[[1]][,"X250"],type='l',add=T) -chr <- uv_converter(path, correction=F) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[,"250.0"],type='l') -matplot(chr2[[1]][,"X250"],type='l',add=T) -chr <- uv_converter(path, correction=T) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[,"250.0"],type='l') -matplot(chr2[[1]][,"X250"],type='l',add=T) -chr <- uv_converter(path, correction=T) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[,"250.0"],type='l') -matplot(chr2[[1]][,"X250"],type='l',add=T) -matplot(chr[,"230.0"],type='l') -matplot(chr2[[1]][,"X230"],type='l',add=T) -chr <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="aston") -# chr <- uv_converter(path, correction=T) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[,"230.0"],type='l') -matplot(chr2[[1]][,"X230"],type='l',add=T) -matplot(chr[[1]][,"230.0"],type='l') -matplot(chr2[[1]][,"X230"],type='l',add=T) -source("~/R_packages/chromConverter/R/read_chroms.R", echo=TRUE) -chr <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="aston") -# chr <- uv_converter(path, correction=T) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[[1]][,"230.0"],type='l') -matplot(chr2[[1]][,"X230"],type='l',add=T) -chr <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="aston") -# chr <- uv_converter(path, correction=T) -chr2 <- read_chroms(path, find_files = FALSE, format.in = "chemstation.uv", -parser="entab") -matplot(chr[[1]][,"230.0"],type='l') -matplot(chr2[[1]][,"X230"],type='l',add=T) -install.packages('entab', repos='https://ethanbass.github.io/drat/') -devtools::check() -devtools::document() -devtools::check() -devtools::check() -devtools::check() -devtools::check() -devtools::check() -x<-read_lines(f) -start <- grep("/Users/ethanbass/Downloads/HPLC-example-files 2/05_20170526_MME_AA_STD-Mix2_ED_1.txt",1) -?read_lines -x<-read_lines(f) -start <- grep("/Users/ethanbass/Downloads/HPLC-example-files 2/05_20170526_MME_AA_STD-Mix2_ED_1.txt",1) -library(readr) -f<-/Users/ethanbass/Downloads/HPLC-example-files 2/05_20170526_MME_AA_STD-Mix2_ED_1.txt -f<-"/Users/ethanbass/Downloads/HPLC-example-files 2/05_20170526_MME_AA_STD-Mix2_ED_1.txt" -x<-read_lines(f) -start <- grep("/Users/ethanbass/Downloads/HPLC-example-files 2/05_20170526_MME_AA_STD-Mix2_ED_1.txt",1) -start -start <- grep("Chromatogram Data:",1) -start -start <- grep("Chromatogram Data:",x) -start -start <- tail(grep("Chromatogram Data:",x),1) -start -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start+4, show_col_types = F) -x -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start+1, show_col_types = F) -x -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F) -x -read_chromera <- function(paths, suffix="txt", dat=NULL, R.format = c("matrix","data.frame","tibble")){ -R.format <- match.arg(R.format, c("matrix","data.frame","tibble")) -dne <- which(!sapply(paths, dir.exists)) -if (length(dne)>0){ -for (i in 1:length(dne)){ -warning(paste0("path not found: '", paths[dne[i]], "'")) -}} -if (is.null(dat)){ -dat<-list() -} -for (path in paths){ -files <- list.files(path=path, pattern = suffix, full.names = T) -file_names <- gsub(pattern = paste0(".",suffix), x = basename(files), replacement = "") -mydata <- lapply(X=files, function(f){ -x<-read_lines(f) -start <- tail(grep("Chromatogram Data:",x),1) -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F) -}) -mydata <- lapply(mydata, FUN=as.matrix) -names(mydata) <- file_names -dat <- append(dat,mydata) -} -rm <- which(sapply(dat,dim)[1,]==0) -if (length(rm) > 0){ -dat <- dat[-rm] -warning(paste("The following chromatograms were found to be empty and automatically removed:", toString(rm,sep=","))) -} -if (R.format == "tibble"){ -dat -} else if (R.format == "data.frame"){ -lapply(dat, function(x){ -x <- as.data.frame(x) -rownames(x) <- x[,1] -x[,2, drop=F] -}) -} else if (R.format == "matrix"){ -lapply(dat, function(x){ -x <- as.matrix(x) -rownames(x) <- x[,1] -x[,2, drop=F] -}) -} -} -read_chromera(f) -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/05_20170526_MME_AA_STD-Mix2_ED_1.txt") -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/", suffix="txt") -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/", suffix="txt", R.format="tibble") -x<-read_lines(f) -start <- tail(grep("Chromatogram Data:",x),1) -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F) -x -x$`Time (min)` -x$`Step (s)` -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/", suffix="txt", R.format="tibble") -x -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F,col_types = "numeric") -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F, col_types = "numeric") -?read_tsv -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F, col_types = n) -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F, col_types = "n") -x -x$`Step (s)` -x$`Time (min)` -x$`Value (nC)` -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F, col_types = "d") -x -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F, col_types = "n") -x -x$`Time (min)` -x$`Step (s)` -sapply(x,as.numeric) -x -x$`Value (nC)` -x$`Time (min)` -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F) -x -sapply(x, function(x)gsub(",",".",x)) -sapply(x, function(x) as.numeric(gsub(",",".",x))) -read_chromera <- function(paths, suffix="txt", dat=NULL, R.format = c("matrix","data.frame","tibble")){ -R.format <- match.arg(R.format, c("matrix","data.frame","tibble")) -dne <- which(!sapply(paths, dir.exists)) -if (length(dne)>0){ -for (i in 1:length(dne)){ -warning(paste0("path not found: '", paths[dne[i]], "'")) -}} -if (is.null(dat)){ -dat<-list() -} -for (path in paths){ -files <- list.files(path=path, pattern = suffix, full.names = T) -file_names <- gsub(pattern = paste0(".",suffix), x = basename(files), replacement = "") -mydata <- lapply(X=files, function(f){ -x<-read_lines(f) -start <- tail(grep("Chromatogram Data:",x),1) -# start<-tail(grep("\\[(.*?)\\]",x),1) -x <- read_tsv(f, skip = start, show_col_types = F) -x<-sapply(x, function(x) as.numeric(gsub(",",".",x))) -}) -mydata <- lapply(mydata, FUN=as.matrix) -names(mydata) <- file_names -dat <- append(dat,mydata) -} -rm <- which(sapply(dat,dim)[1,]==0) -if (length(rm) > 0){ -dat <- dat[-rm] -warning(paste("The following chromatograms were found to be empty and automatically removed:", toString(rm,sep=","))) -} -if (R.format == "tibble"){ -dat -} else if (R.format == "data.frame"){ -lapply(dat, function(x){ -x <- as.data.frame(x) -rownames(x) <- x[,1] -x[,2, drop=F] -}) -} else if (R.format == "matrix"){ -lapply(dat, function(x){ -x <- as.matrix(x) -rownames(x) <- x[,1] -x[,2, drop=F] -}) -} -} -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/") -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/", R.format = "tibble") -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/", R.format = "data.frame") -source("~/R_packages/chromConverter/R/read_chromera.R", echo=TRUE) -read_chromera("/Users/ethanbass/Downloads/HPLC-example-files 2/") -source("~/R_packages/chromConverter/R/read_chromera.R", echo=TRUE)