💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.5 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "entity/systems/proteome.hpp"
  20. #include "entity/components/proteome.hpp"
  21. #include "genetics/sequence.hpp"
  22. #include "genetics/standard-code.hpp"
  23. namespace entity {
  24. namespace system {
  25. proteome::proteome(entity::registry& registry):
  26. updatable(registry)
  27. {
  28. registry.on_construct<entity::component::genome>().connect<&proteome::on_genome_construct>(this);
  29. registry.on_replace<entity::component::genome>().connect<&proteome::on_genome_replace>(this);
  30. }
  31. void proteome::update(double t, double dt)
  32. {}
  33. void proteome::on_genome_construct(entity::registry& registry, entity::id entity_id, entity::component::genome& genome)
  34. {
  35. on_genome_replace(registry, entity_id, genome);
  36. }
  37. void proteome::on_genome_replace(entity::registry& registry, entity::id entity_id, entity::component::genome& genome)
  38. {
  39. // Allocate a proteome component
  40. entity::component::proteome proteome_component;
  41. // For each chromosome in the genome
  42. for (const std::string& chromosome: genome.chromosomes)
  43. {
  44. // Find the first ORF in the chromosome
  45. auto orf = genetics::sequence::find_orf(chromosome.begin(), chromosome.end(), genetics::standard_code);
  46. // While the ORF is valid
  47. while (orf.start != chromosome.end())
  48. {
  49. // Translate the base sequence into an amino acid sequence (protein)
  50. std::string protein;
  51. genetics::sequence::translate(orf.start, orf.stop, std::back_inserter(protein), genetics::standard_code);
  52. // Append protein to the proteome
  53. proteome_component.proteins.push_back(protein);
  54. // Find the next ORF
  55. orf = genetics::sequence::find_orf(orf.stop, chromosome.end(), genetics::standard_code);
  56. }
  57. }
  58. // Assign or replace the entity's proteome component
  59. registry.assign_or_replace<entity::component::proteome>(entity_id, proteome_component);
  60. }
  61. } // namespace system
  62. } // namespace entity