complex.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef API_COMPLEX_H
  12. #define API_COMPLEX_H
  13. #include "generated/gen_complex.h"
  14. #include "api/api_common.h"
  15. #include "api/api_utils.h"
  16. #include "api/compartment_utils.h"
  17. #include "bng/bngl_names.h"
  18. namespace BNG {
  19. class BNGData;
  20. class Cplx;
  21. }
  22. namespace MCell {
  23. namespace API {
  24. class Species;
  25. // WARNING: do not set compartment_name through attribute, use set_compartment_name
  26. class Complex: public GenComplex {
  27. public:
  28. COMPLEX_CTOR()
  29. static std::shared_ptr<API::Complex> construct_empty() {
  30. // to avoid Complex object semantic check, we need to insert a dummy name
  31. // when creating the object
  32. auto res_cplx_inst = std::make_shared<API::Complex>(STR_UNSET);
  33. res_cplx_inst->name = STR_UNSET;
  34. return res_cplx_inst;
  35. }
  36. static std::shared_ptr<API::Complex> construct_from_bng_cplx(
  37. const BNG::BNGData& bng_data,
  38. const BNG::Cplx& bng_inst);
  39. static std::shared_ptr<API::Complex> construct_from_bng_cplx_w_orientation(
  40. const BNG::BNGData& bng_data,
  41. const BNG::Cplx& bng_inst,
  42. const Orientation orientation);
  43. void set_name(const std::string& name_) override {
  44. BaseDataClass::set_name(name_);
  45. // rerun initialization because the name is parsed as a BNGL string
  46. elementary_molecules.clear();
  47. postprocess_in_ctor();
  48. }
  49. void postprocess_in_ctor() override;
  50. void check_semantics() const override {
  51. if (is_species_object()) {
  52. // all semantic checks will be done in Species
  53. return;
  54. }
  55. if (compartment_name == BNG::DEFAULT_COMPARTMENT_NAME) {
  56. throw ValueError("Compartment name '" + compartment_name + "' is reserved, please use a different name.");
  57. }
  58. GenComplex::check_semantics();
  59. }
  60. // using shorter printout when all_details is false
  61. std::string to_str(const bool all_details=false, const std::string ind="") const override;
  62. bool __eq__(const Complex& other) const override;
  63. std::string to_bngl_str() const override {
  64. return to_bngl_str_w_custom_orientation();
  65. }
  66. std::shared_ptr<Species> as_species() override;
  67. // do not export elementary_molecules (they are represented by name)
  68. bool skip_vectors_export() const override {
  69. return true;
  70. }
  71. // export into a single line
  72. bool export_as_string_without_newlines() const override {
  73. return true;
  74. }
  75. std::string export_to_python(std::ostream& out, PythonExportContext& ctx) {
  76. // we must set name for export if it was not set
  77. if (!is_set(name)) {
  78. name = to_bngl_str_w_custom_orientation();
  79. }
  80. return GenComplex::export_to_python(out, ctx);
  81. }
  82. // complexes can be only either surf or vol, there is no other option
  83. // WARNING: information on whether this is a surface or volume complex
  84. // may not be available all the time, e.g. when complex is constructed with m.Complex('A')
  85. bool is_vol() const {
  86. return !is_surf();
  87. }
  88. // WARNING: same as above
  89. bool is_surf() const;
  90. std::string to_bngl_str_w_custom_orientation(const bool include_mcell_orientation = false) const;
  91. // not really const, sets mutable members that serve as cache
  92. const std::string& get_canonical_name() const;
  93. // WARNING: information on whether this is a surface or volume complex - used when determining the
  94. // compartment may not be available all the time, e.g. when complex is constructed with m.Complex('A')
  95. const std::string& get_primary_compartment_name() const;
  96. void set_compartment_name(const std::string& new_compartment_name) {
  97. compartment_name = new_compartment_name;
  98. set_unset_compartments_of_elementary_molecules();
  99. }
  100. private:
  101. void set_unset_compartments_of_elementary_molecules();
  102. bool is_species_object() const;
  103. // set when __eq__ is called, valid if cached_data_are_uptodate is true
  104. mutable std::string canonical_name;
  105. };
  106. } // namespace API
  107. } // namespace MCell
  108. #endif // API_COMPLEX_H