bng_analyzer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #include <iostream>
  12. #include <cassert>
  13. #include <string>
  14. #ifndef _MSC_VER
  15. #include <getopt.h>
  16. #else
  17. #include "win_getopt/win_getopt.h"
  18. #endif
  19. #include "nfsim_species_unifier.h"
  20. using namespace std;
  21. /* Command-line arguments structure:
  22. * long arg name
  23. * has argument
  24. * pointer to flag (should always be 0)
  25. * short argument letter
  26. */
  27. static const option long_options[] = {
  28. { "help", 0, 0, 'h' },
  29. { "version", 0, 0, 'v' },
  30. { "dat", 0, 0, 'd' },
  31. { "output", 1, 0, 'o' },
  32. { nullptr, 0, 0, 0 }
  33. };
  34. void print_usage(const char* argv0) {
  35. cout <<
  36. " [-d] INPUT_FILE [-o OUTPUT_FILE]\n"
  37. " -d Optional argument that specifies that the input is a .dat ascii file \n"
  38. " for MCell visualization, NFSim .species file otherwise\n";
  39. }
  40. void print_version(const char* argv0) {
  41. cout << "TODO\n";
  42. }
  43. const int ARG_PARSE_ERROR = 1;
  44. const int ARG_PARSE_QUIT = 0;
  45. const int ARG_PARSE_OK = -1;
  46. // returns ARG_PARSE_OK if execution should continue
  47. // ARG_PARSE_END to end with exit code 0,
  48. // ARG_PARSE_ERROR to end with exit code 1,
  49. int process_args(
  50. const int argc, char* argv[],
  51. string& input_file,
  52. string& output_file,
  53. bool& is_dat
  54. ) {
  55. input_file = "";
  56. output_file = "";
  57. is_dat = false;
  58. assert(argc > 0);
  59. while (1) {
  60. // get the next argument
  61. int c = getopt_long_only(argc, argv, "hvo:", long_options, nullptr);
  62. if (c == -1)
  63. break;
  64. switch (c) {
  65. case 'h':
  66. print_usage(argv[0]);
  67. return ARG_PARSE_QUIT;
  68. case 'v':
  69. print_version(argv[0]);
  70. return ARG_PARSE_QUIT;
  71. case 'o':
  72. output_file = optarg;
  73. break;
  74. case 'd':
  75. is_dat = true;
  76. break;
  77. default:
  78. cerr << "Invalid arguments.\n";
  79. return ARG_PARSE_ERROR;
  80. }
  81. }
  82. if (optind < argc) {
  83. if (argc - optind > 1) {
  84. cerr << "Only one input file can be specified.\n";
  85. return ARG_PARSE_ERROR;
  86. }
  87. input_file = argv[optind];
  88. }
  89. else {
  90. cerr << "Input file was not specified.\n";
  91. return ARG_PARSE_ERROR;
  92. }
  93. return ARG_PARSE_OK;
  94. }
  95. int main(const int argc, char* argv[]) {
  96. string input_file;
  97. string output_file;
  98. bool is_dat;
  99. int arg_process_res = process_args(argc, argv, input_file, output_file, is_dat);
  100. if (arg_process_res != ARG_PARSE_OK) {
  101. return arg_process_res;
  102. }
  103. MCell::NFSimSpeciesUnifier nfsim_unifier;
  104. bool ok;
  105. ok = nfsim_unifier.read_input_file(input_file, is_dat);
  106. if (!ok) {
  107. cerr << "There was an error while reading " << input_file << ".\n";
  108. return 1;
  109. }
  110. ok = nfsim_unifier.print_unified_species(output_file);
  111. return ok ? 0 : 1;
  112. }