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..269343f 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,5 +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"}; @@ -162,3 +165,17 @@ float float_from_optarg(const char *optarg) { } return std::atof(string_float.c_str()); } + +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()); + if (dir) { + closedir(dir); + } else { + fprintf(stderr, "Directory does not seem to exist: %s/\n", outfile_dir.c_str()); + exit(1); + } +#endif +} 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 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(); diff --git a/src/pvalue.cpp b/src/pvalue.cpp index ca9d6b3..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) { @@ -237,6 +242,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;