gen_elementary_molecule.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2021 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 <sstream>
  12. #include "api/pybind11_stl_include.h"
  13. #include "api/python_export_utils.h"
  14. #include "gen_elementary_molecule.h"
  15. #include "api/elementary_molecule.h"
  16. #include "api/component.h"
  17. #include "api/elementary_molecule_type.h"
  18. namespace MCell {
  19. namespace API {
  20. void GenElementaryMolecule::check_semantics() const {
  21. if (!is_set(elementary_molecule_type)) {
  22. throw ValueError("Parameter 'elementary_molecule_type' must be set.");
  23. }
  24. }
  25. void GenElementaryMolecule::set_initialized() {
  26. if (is_set(elementary_molecule_type)) {
  27. elementary_molecule_type->set_initialized();
  28. }
  29. vec_set_initialized(components);
  30. initialized = true;
  31. }
  32. void GenElementaryMolecule::set_all_attributes_as_default_or_unset() {
  33. class_name = "ElementaryMolecule";
  34. elementary_molecule_type = nullptr;
  35. components = std::vector<std::shared_ptr<Component>>();
  36. compartment_name = STR_UNSET;
  37. }
  38. std::shared_ptr<ElementaryMolecule> GenElementaryMolecule::copy_elementary_molecule() const {
  39. std::shared_ptr<ElementaryMolecule> res = std::make_shared<ElementaryMolecule>(DefaultCtorArgType());
  40. res->class_name = class_name;
  41. res->elementary_molecule_type = elementary_molecule_type;
  42. res->components = components;
  43. res->compartment_name = compartment_name;
  44. return res;
  45. }
  46. std::shared_ptr<ElementaryMolecule> GenElementaryMolecule::deepcopy_elementary_molecule(py::dict) const {
  47. std::shared_ptr<ElementaryMolecule> res = std::make_shared<ElementaryMolecule>(DefaultCtorArgType());
  48. res->class_name = class_name;
  49. res->elementary_molecule_type = is_set(elementary_molecule_type) ? elementary_molecule_type->deepcopy_elementary_molecule_type() : nullptr;
  50. for (const auto& item: components) {
  51. res->components.push_back((is_set(item)) ? item->deepcopy_component() : nullptr);
  52. }
  53. res->compartment_name = compartment_name;
  54. return res;
  55. }
  56. bool GenElementaryMolecule::__eq__(const ElementaryMolecule& other) const {
  57. return
  58. (
  59. (is_set(elementary_molecule_type)) ?
  60. (is_set(other.elementary_molecule_type) ?
  61. (elementary_molecule_type->__eq__(*other.elementary_molecule_type)) :
  62. false
  63. ) :
  64. (is_set(other.elementary_molecule_type) ?
  65. false :
  66. true
  67. )
  68. ) &&
  69. vec_ptr_eq(components, other.components) &&
  70. compartment_name == other.compartment_name;
  71. }
  72. bool GenElementaryMolecule::eq_nonarray_attributes(const ElementaryMolecule& other, const bool ignore_name) const {
  73. return
  74. (
  75. (is_set(elementary_molecule_type)) ?
  76. (is_set(other.elementary_molecule_type) ?
  77. (elementary_molecule_type->__eq__(*other.elementary_molecule_type)) :
  78. false
  79. ) :
  80. (is_set(other.elementary_molecule_type) ?
  81. false :
  82. true
  83. )
  84. ) &&
  85. true /*components*/ &&
  86. compartment_name == other.compartment_name;
  87. }
  88. std::string GenElementaryMolecule::to_str(const bool all_details, const std::string ind) const {
  89. std::stringstream ss;
  90. ss << get_object_name() << ": " <<
  91. "\n" << ind + " " << "elementary_molecule_type=" << "(" << ((elementary_molecule_type != nullptr) ? elementary_molecule_type->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  92. "components=" << vec_ptr_to_str(components, all_details, ind + " ") << ", " << "\n" << ind + " " <<
  93. "compartment_name=" << compartment_name;
  94. return ss.str();
  95. }
  96. py::class_<ElementaryMolecule> define_pybinding_ElementaryMolecule(py::module& m) {
  97. return py::class_<ElementaryMolecule, std::shared_ptr<ElementaryMolecule>>(m, "ElementaryMolecule", "Instance of an elementary molecule type. A BNGL complex is composed of elementary molecules.")
  98. .def(
  99. py::init<
  100. std::shared_ptr<ElementaryMoleculeType>,
  101. const std::vector<std::shared_ptr<Component>>,
  102. const std::string&
  103. >(),
  104. py::arg("elementary_molecule_type"),
  105. py::arg("components") = std::vector<std::shared_ptr<Component>>(),
  106. py::arg("compartment_name") = STR_UNSET
  107. )
  108. .def("check_semantics", &ElementaryMolecule::check_semantics)
  109. .def("__copy__", &ElementaryMolecule::copy_elementary_molecule)
  110. .def("__deepcopy__", &ElementaryMolecule::deepcopy_elementary_molecule, py::arg("memo"))
  111. .def("__str__", &ElementaryMolecule::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  112. .def("__eq__", &ElementaryMolecule::__eq__, py::arg("other"))
  113. .def("to_bngl_str", &ElementaryMolecule::to_bngl_str, py::arg("with_compartment") = true, "Creates a string that corresponds to its BNGL representation\n- with_compartment: Include compartment name in the returned BNGL string.\n\n")
  114. .def("dump", &ElementaryMolecule::dump)
  115. .def_property("elementary_molecule_type", &ElementaryMolecule::get_elementary_molecule_type, &ElementaryMolecule::set_elementary_molecule_type, "Reference to the type of this elementary molecule.")
  116. .def_property("components", &ElementaryMolecule::get_components, &ElementaryMolecule::set_components, py::return_value_policy::reference, "List of component instances. Not all components need to be specified \nin case when this elementary molecule is used in a pattern.\n")
  117. .def_property("compartment_name", &ElementaryMolecule::get_compartment_name, &ElementaryMolecule::set_compartment_name, "Optional BNGL compartment name for this elemenrary molecule. If a 2D/surface compartment is specified, the elementary moelcule must be of surface type. If a 3D/volume compartment is specified, the elementary moelcule must be of volume type.")
  118. ;
  119. }
  120. std::string GenElementaryMolecule::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  121. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  122. return ctx.get_exported_name(this);
  123. }
  124. std::string exported_name = "elementary_molecule_" + std::to_string(ctx.postinc_counter("elementary_molecule"));
  125. if (!export_even_if_already_exported()) {
  126. ctx.add_exported(this, exported_name);
  127. }
  128. bool str_export = export_as_string_without_newlines();
  129. std::string nl = "";
  130. std::string ind = " ";
  131. std::stringstream ss;
  132. if (!str_export) {
  133. nl = "\n";
  134. ind = " ";
  135. ss << exported_name << " = ";
  136. }
  137. ss << "m.ElementaryMolecule(" << nl;
  138. ss << ind << "elementary_molecule_type = " << elementary_molecule_type->export_to_python(out, ctx) << "," << nl;
  139. if (components != std::vector<std::shared_ptr<Component>>() && !skip_vectors_export()) {
  140. ss << ind << "components = " << export_vec_components(out, ctx, exported_name) << "," << nl;
  141. }
  142. if (compartment_name != STR_UNSET) {
  143. ss << ind << "compartment_name = " << "'" << compartment_name << "'" << "," << nl;
  144. }
  145. ss << ")" << nl << nl;
  146. if (!str_export) {
  147. out << ss.str();
  148. return exported_name;
  149. }
  150. else {
  151. return ss.str();
  152. }
  153. }
  154. std::string GenElementaryMolecule::export_vec_components(std::ostream& out, PythonExportContext& ctx, const std::string& parent_name) {
  155. // does not print the array itself to 'out' and returns the whole list
  156. std::stringstream ss;
  157. ss << "[";
  158. for (size_t i = 0; i < components.size(); i++) {
  159. const auto& item = components[i];
  160. if (i == 0) {
  161. ss << " ";
  162. }
  163. else if (i % 16 == 0) {
  164. ss << "\n ";
  165. }
  166. if (!item->skip_python_export()) {
  167. std::string name = item->export_to_python(out, ctx);
  168. ss << name << ", ";
  169. }
  170. }
  171. ss << "]";
  172. return ss.str();
  173. }
  174. } // namespace API
  175. } // namespace MCell