From 9404f2554d93b5e6ad314340d58cd9b984648147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20M=C3=A4klin?= Date: Mon, 11 Apr 2022 13:55:05 +0300 Subject: [PATCH 1/5] Exit fastspar_bootstrap if `--prefix` contains a directory that can't be accessed. --- src/bootstrap.cpp | 5 +++++ src/common.cpp | 13 +++++++++++++ src/common.h | 1 + 3 files changed, 19 insertions(+) diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 9874af2..0df9897 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -94,6 +94,11 @@ int main(int argc, char **argv) { OtuTable otu_table; otu_table.load_otu_file(bootstrap_options.otu_filename); + // If bootstrap_prefix contains a directory, check that it exists + if (bootstrap_options.bootstrap_prefix.find('/') != std::string::npos) { + directory_exists(bootstrap_options.bootstrap_prefix); + } + // Generate bootstraps get_and_write_bootstraps(otu_table, bootstrap_options.bootstrap_number, bootstrap_options.bootstrap_prefix, bootstrap_options.threads, bootstrap_options.seed); diff --git a/src/common.cpp b/src/common.cpp index 5abf3f2..7e902e4 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,5 +1,6 @@ #include "common.h" +#include std::vector PERMITTED_OTU_HEADERS = {"#OTU ID", "#OTU_ID", "OTU ID", "OTU_ID", "OTU_id", "OTU id"}; @@ -162,3 +163,15 @@ float float_from_optarg(const char *optarg) { } return std::atof(string_float.c_str()); } + +void directory_exists(const std::string &optarg) { + std::string outfile_dir(optarg); + outfile_dir.erase(outfile_dir.rfind("/"), outfile_dir.size()); + DIR* dir = opendir(outfile_dir.c_str()); + if (dir) { + closedir(dir); + } else { + fprintf(stderr, "Directory does not seem to exist: %s/\n", outfile_dir.c_str()); + exit(1); + } +} diff --git a/src/common.h b/src/common.h index ac665f9..db52ddb 100644 --- a/src/common.h +++ b/src/common.h @@ -49,5 +49,6 @@ int int_from_optarg(const char *optarg); // Convert character to float (for commandline argument parsing) float float_from_optarg(const char *optarg); +void directory_exists(const std::string &optarg); #endif From 6bff3e342904251de54ada6c09cb05aec76e2a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20M=C3=A4klin?= Date: Tue, 12 Apr 2022 10:27:52 +0300 Subject: [PATCH 2/5] Exit fastspar if --correlation or --covariance output to a directory that doesn't exist. --- src/fastspar.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/fastspar.cpp b/src/fastspar.cpp index 272bb3d..d1ca866 100644 --- a/src/fastspar.cpp +++ b/src/fastspar.cpp @@ -47,6 +47,14 @@ int main(int argc, char **argv) { FastSpar fastspar(&otu_table, fastspar_options.iterations, fastspar_options.exclude_iterations, fastspar_options.threshold, fastspar_options.threads, fastspar_options.seed); + // Check that the output files are accessible if not in current directory + if (fastspar_options.correlation_filename.find('/') != std::string::npos) { + directory_exists(fastspar_options.correlation_filename); + } + if (fastspar_options.covariance_filename.find('/') != std::string::npos) { + directory_exists(fastspar_options.covariance_filename); + } + // Run FastSpar iterations fprintf(stdout, "Running SparCC iterations\n"); fastspar.infer_correlation_and_covariance(); From be7c7ff0d2d73b238476ee1229b077345d0c5a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20M=C3=A4klin?= Date: Tue, 12 Apr 2022 10:33:34 +0300 Subject: [PATCH 3/5] Exit fastspar_pvalues if --outfile is in a directory that does not exist. --- src/pvalue.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/pvalue.cpp b/src/pvalue.cpp index ca9d6b3..1d6f118 100644 --- a/src/pvalue.cpp +++ b/src/pvalue.cpp @@ -237,6 +237,11 @@ int main(int argc, char **argv) { exit(0); } + // Check that the output files are accessible if not in current directory + if (pval_options.out_filename.find('/') != std::string::npos) { + directory_exists(pval_options.out_filename); + } + // Read in otu tables (used to calculate total possible permutations) printf("Reading in OTU count table\n"); OtuTable otu_table; From 0642967943a8e3b3f39c068dfe39a4fa6ae91081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20M=C3=A4klin?= Date: Tue, 12 Apr 2022 10:35:31 +0300 Subject: [PATCH 4/5] Exit fastspar_pvalues if the input prefix is in an inaccessible folder. --- src/pvalue.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/pvalue.cpp b/src/pvalue.cpp index 1d6f118..028e15a 100644 --- a/src/pvalue.cpp +++ b/src/pvalue.cpp @@ -229,6 +229,11 @@ int main(int argc, char **argv) { // Get commandline arguments PvalOptions pval_options = get_commandline_arguments(argc, argv); + // Check that the input prefix is accessible if in a folder + if (pval_options.bootstrap_prefix.find('/') != std::string::npos) { + directory_exists(pval_options.bootstrap_prefix); + } + // Collect bootstrap correlation file paths and then make sure we have found the correct number std::vector bs_cor_paths = get_bootstrap_correlation_paths(pval_options.bootstrap_prefix); if (bs_cor_paths.size() != pval_options.permutations) { From a0ef28a70c2b445a5fd0dd05de0d9a7f91057468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20M=C3=A4klin?= Date: Tue, 12 Apr 2022 10:40:01 +0300 Subject: [PATCH 5/5] Use preprocessor directive to remove include and directory_exists() implementation if compiling on windows. --- src/common.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/common.cpp b/src/common.cpp index 7e902e4..269343f 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,6 +1,8 @@ #include "common.h" +#ifndef _WIN32 // dirent.h is not available on windows #include +#endif std::vector PERMITTED_OTU_HEADERS = {"#OTU ID", "#OTU_ID", "OTU ID", "OTU_ID", "OTU_id", "OTU id"}; @@ -165,6 +167,7 @@ float float_from_optarg(const char *optarg) { } void directory_exists(const std::string &optarg) { +#ifndef _WIN32 // implementation doesn't work on windows std::string outfile_dir(optarg); outfile_dir.erase(outfile_dir.rfind("/"), outfile_dir.size()); DIR* dir = opendir(outfile_dir.c_str()); @@ -174,4 +177,5 @@ void directory_exists(const std::string &optarg) { fprintf(stderr, "Directory does not seem to exist: %s/\n", outfile_dir.c_str()); exit(1); } +#endif }