gen_species.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_species.h"
  15. #include "api/species.h"
  16. #include "api/complex.h"
  17. #include "api/elementary_molecule.h"
  18. #include "api/species.h"
  19. namespace MCell {
  20. namespace API {
  21. void GenSpecies::check_semantics() const {
  22. }
  23. void GenSpecies::set_initialized() {
  24. vec_set_initialized(elementary_molecules);
  25. initialized = true;
  26. }
  27. void GenSpecies::set_all_attributes_as_default_or_unset() {
  28. class_name = "Species";
  29. name = STR_UNSET;
  30. diffusion_constant_2d = FLT_UNSET;
  31. diffusion_constant_3d = FLT_UNSET;
  32. custom_time_step = FLT_UNSET;
  33. custom_space_step = FLT_UNSET;
  34. target_only = false;
  35. name = STR_UNSET;
  36. elementary_molecules = std::vector<std::shared_ptr<ElementaryMolecule>>();
  37. orientation = Orientation::DEFAULT;
  38. compartment_name = STR_UNSET;
  39. }
  40. std::shared_ptr<Species> GenSpecies::copy_species() const {
  41. std::shared_ptr<Species> res = std::make_shared<Species>(DefaultCtorArgType());
  42. res->class_name = class_name;
  43. res->name = name;
  44. res->diffusion_constant_2d = diffusion_constant_2d;
  45. res->diffusion_constant_3d = diffusion_constant_3d;
  46. res->custom_time_step = custom_time_step;
  47. res->custom_space_step = custom_space_step;
  48. res->target_only = target_only;
  49. res->name = name;
  50. res->elementary_molecules = elementary_molecules;
  51. res->orientation = orientation;
  52. res->compartment_name = compartment_name;
  53. return res;
  54. }
  55. std::shared_ptr<Species> GenSpecies::deepcopy_species(py::dict) const {
  56. std::shared_ptr<Species> res = std::make_shared<Species>(DefaultCtorArgType());
  57. res->class_name = class_name;
  58. res->name = name;
  59. res->diffusion_constant_2d = diffusion_constant_2d;
  60. res->diffusion_constant_3d = diffusion_constant_3d;
  61. res->custom_time_step = custom_time_step;
  62. res->custom_space_step = custom_space_step;
  63. res->target_only = target_only;
  64. res->name = name;
  65. for (const auto& item: elementary_molecules) {
  66. res->elementary_molecules.push_back((is_set(item)) ? item->deepcopy_elementary_molecule() : nullptr);
  67. }
  68. res->orientation = orientation;
  69. res->compartment_name = compartment_name;
  70. return res;
  71. }
  72. bool GenSpecies::__eq__(const Species& other) const {
  73. return
  74. name == other.name &&
  75. diffusion_constant_2d == other.diffusion_constant_2d &&
  76. diffusion_constant_3d == other.diffusion_constant_3d &&
  77. custom_time_step == other.custom_time_step &&
  78. custom_space_step == other.custom_space_step &&
  79. target_only == other.target_only &&
  80. name == other.name &&
  81. vec_ptr_eq(elementary_molecules, other.elementary_molecules) &&
  82. orientation == other.orientation &&
  83. compartment_name == other.compartment_name;
  84. }
  85. bool GenSpecies::eq_nonarray_attributes(const Species& other, const bool ignore_name) const {
  86. return
  87. (ignore_name || name == other.name) &&
  88. diffusion_constant_2d == other.diffusion_constant_2d &&
  89. diffusion_constant_3d == other.diffusion_constant_3d &&
  90. custom_time_step == other.custom_time_step &&
  91. custom_space_step == other.custom_space_step &&
  92. target_only == other.target_only &&
  93. (ignore_name || name == other.name) &&
  94. true /*elementary_molecules*/ &&
  95. orientation == other.orientation &&
  96. compartment_name == other.compartment_name;
  97. }
  98. std::string GenSpecies::to_str(const bool all_details, const std::string ind) const {
  99. std::stringstream ss;
  100. ss << get_object_name() << ": " <<
  101. "name=" << name << ", " <<
  102. "diffusion_constant_2d=" << diffusion_constant_2d << ", " <<
  103. "diffusion_constant_3d=" << diffusion_constant_3d << ", " <<
  104. "custom_time_step=" << custom_time_step << ", " <<
  105. "custom_space_step=" << custom_space_step << ", " <<
  106. "target_only=" << target_only << ", " <<
  107. "name=" << name << ", " <<
  108. "\n" << ind + " " << "elementary_molecules=" << vec_ptr_to_str(elementary_molecules, all_details, ind + " ") << ", " << "\n" << ind + " " <<
  109. "orientation=" << orientation << ", " <<
  110. "compartment_name=" << compartment_name;
  111. return ss.str();
  112. }
  113. py::class_<Species> define_pybinding_Species(py::module& m) {
  114. return py::class_<Species, Complex, std::shared_ptr<Species>>(m, "Species", "There are three ways how to use this class:\n1) definition of simple species - in this case 'name' is \na single identifier and at least 'diffusion_constant_2d' or \n'diffusion_constant_3d' must be provided.\nExample: m.Species('A', diffusion_constant_3d=1e-6). \nSuch a definition must be added to subsystem or model so that \nduring model initialization this species is transformed to MCell \nrepresentation and an ElementaryMoleculeType 'A' with a given \ndiffusion constant is created as well.\n2) full definition of complex species - in this case the \ninherited attribute 'elementary_molecules' from Complex\nis used as a definition of the complex and this gives information \non diffusion constants of the used elementary molecules.\nExample: m.Species(elementary_molecules=[ei1, ei2]). \nSuch a definition must be added to subsystem or model. \n3) declaration of species - in this case only 'name' in the form of \nan BNGL string is provided. The complex instance specified by the name \nmust be fully qualified (i.e. all components are present and those \ncomponents that have a state have their state set).\nNo information on diffusion constants and other properties of \nused elementary molecules is provided, it must be provided elsewhere.\nExample: m.Species('A(b!1).B(a!1)').\nThis is a common form of usage when reaction rules are provided in a BNGL file.\nSuch declaration does no need to be added to subsystem or model.\nThis form is used as argument in cases where a fully qualified instance \nmust be provided such as in molecule releases.\n \n")
  115. .def(
  116. py::init<
  117. const std::string&,
  118. const double,
  119. const double,
  120. const double,
  121. const double,
  122. const bool,
  123. const std::vector<std::shared_ptr<ElementaryMolecule>>,
  124. const Orientation,
  125. const std::string&
  126. >(),
  127. py::arg("name") = STR_UNSET,
  128. py::arg("diffusion_constant_2d") = FLT_UNSET,
  129. py::arg("diffusion_constant_3d") = FLT_UNSET,
  130. py::arg("custom_time_step") = FLT_UNSET,
  131. py::arg("custom_space_step") = FLT_UNSET,
  132. py::arg("target_only") = false,
  133. py::arg("elementary_molecules") = std::vector<std::shared_ptr<ElementaryMolecule>>(),
  134. py::arg("orientation") = Orientation::DEFAULT,
  135. py::arg("compartment_name") = STR_UNSET
  136. )
  137. .def("check_semantics", &Species::check_semantics)
  138. .def("__copy__", &Species::copy_species)
  139. .def("__deepcopy__", &Species::deepcopy_species, py::arg("memo"))
  140. .def("__str__", &Species::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  141. .def("__eq__", &Species::__eq__, py::arg("other"))
  142. .def("inst", &Species::inst, py::arg("orientation") = Orientation::DEFAULT, py::arg("compartment_name") = STR_UNSET, "Creates a copy of a Complex from this Species with specified orientation and compartment name. \n\n- orientation: Maximum one of orientation or compartment_name can be set, not both.\n\n- compartment_name: Maximum one of orientation or compartment_name can be set, not both.\n\n")
  143. .def("dump", &Species::dump)
  144. .def_property("name", &Species::get_name, &Species::set_name, "Name of the species in the BNGL format. \nOne must either specify name or elementary_molecules (inherited from Complex). \nThis argument name is parsed during model initialization. \n")
  145. .def_property("diffusion_constant_2d", &Species::get_diffusion_constant_2d, &Species::set_diffusion_constant_2d, "This molecule is constrained to surface with diffusion constant D. \nD can be zero, in which case the molecule doesn’t move. \nThe units of D are cm^2/s.\n")
  146. .def_property("diffusion_constant_3d", &Species::get_diffusion_constant_3d, &Species::set_diffusion_constant_3d, "This molecule diffuses in space with diffusion constant D. \nD can be zero, in which case the molecule doesn’t move. \nThe units of D are cm^2/s.\n \n")
  147. .def_property("custom_time_step", &Species::get_custom_time_step, &Species::set_custom_time_step, "Optional setting of a custom time step for this specific species. \nA molecule of this species should take timesteps of length custom_time_step (in seconds). \nUse either this or custom_time_step.\n")
  148. .def_property("custom_space_step", &Species::get_custom_space_step, &Species::set_custom_space_step, "Optional setting of a custom space step for this specific species. \nA molecule of this species should take steps of average length custom_space_step (in microns). \nUse either this or custom_time_step.\n \n")
  149. .def_property("target_only", &Species::get_target_only, &Species::set_target_only, "A molecule of this species will not initiate reactions when it runs into other molecules. This\nsetting can speed up simulations when applied to a molecule at high concentrations \nthat reacts with a molecule at low concentrations (it is more efficient for\nthe low-concentration molecule to trigger the reactions). This directive does\nnot affect unimolecular reactions.\n")
  150. ;
  151. }
  152. std::string GenSpecies::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  153. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  154. return ctx.get_exported_name(this);
  155. }
  156. std::string exported_name = std::string("species") + "_" + (is_set(name) ? fix_id(name) : std::to_string(ctx.postinc_counter("species")));
  157. if (!export_even_if_already_exported()) {
  158. ctx.add_exported(this, exported_name);
  159. }
  160. bool str_export = export_as_string_without_newlines();
  161. std::string nl = "";
  162. std::string ind = " ";
  163. std::stringstream ss;
  164. if (!str_export) {
  165. nl = "\n";
  166. ind = " ";
  167. ss << exported_name << " = ";
  168. }
  169. ss << "m.Species(" << nl;
  170. if (name != STR_UNSET) {
  171. ss << ind << "name = " << "'" << name << "'" << "," << nl;
  172. }
  173. if (elementary_molecules != std::vector<std::shared_ptr<ElementaryMolecule>>() && !skip_vectors_export()) {
  174. ss << ind << "elementary_molecules = " << export_vec_elementary_molecules(out, ctx, exported_name) << "," << nl;
  175. }
  176. if (orientation != Orientation::DEFAULT) {
  177. ss << ind << "orientation = " << orientation << "," << nl;
  178. }
  179. if (compartment_name != STR_UNSET) {
  180. ss << ind << "compartment_name = " << "'" << compartment_name << "'" << "," << nl;
  181. }
  182. if (diffusion_constant_2d != FLT_UNSET) {
  183. ss << ind << "diffusion_constant_2d = " << f_to_str(diffusion_constant_2d) << "," << nl;
  184. }
  185. if (diffusion_constant_3d != FLT_UNSET) {
  186. ss << ind << "diffusion_constant_3d = " << f_to_str(diffusion_constant_3d) << "," << nl;
  187. }
  188. if (custom_time_step != FLT_UNSET) {
  189. ss << ind << "custom_time_step = " << f_to_str(custom_time_step) << "," << nl;
  190. }
  191. if (custom_space_step != FLT_UNSET) {
  192. ss << ind << "custom_space_step = " << f_to_str(custom_space_step) << "," << nl;
  193. }
  194. if (target_only != false) {
  195. ss << ind << "target_only = " << target_only << "," << nl;
  196. }
  197. ss << ")" << nl << nl;
  198. if (!str_export) {
  199. out << ss.str();
  200. return exported_name;
  201. }
  202. else {
  203. return ss.str();
  204. }
  205. }
  206. std::string GenSpecies::export_vec_elementary_molecules(std::ostream& out, PythonExportContext& ctx, const std::string& parent_name) {
  207. // does not print the array itself to 'out' and returns the whole list
  208. std::stringstream ss;
  209. ss << "[";
  210. for (size_t i = 0; i < elementary_molecules.size(); i++) {
  211. const auto& item = elementary_molecules[i];
  212. if (i == 0) {
  213. ss << " ";
  214. }
  215. else if (i % 16 == 0) {
  216. ss << "\n ";
  217. }
  218. if (!item->skip_python_export()) {
  219. std::string name = item->export_to_python(out, ctx);
  220. ss << name << ", ";
  221. }
  222. }
  223. ss << "]";
  224. return ss.str();
  225. }
  226. } // namespace API
  227. } // namespace MCell