Advertisement
Equidea

Untitled

Mar 9th, 2020
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Equidea\Engine\Horse;
  4.  
  5. /**
  6.  * An object representation of a full horse color genome.
  7.  *
  8.  * A genome contains many different locuses, which are the places for genetic information
  9.  * about color mutations and manipulation of molecules such as eumelanin and pheomelanin.
  10.  *
  11.  * It provides methods for most common operations and supports JSON type array input
  12.  * for allele locuses.
  13.  *
  14.  * @see https://en.wikipedia.org/wiki/Equine_coat_color_genetics
  15.  *
  16.  * @author      Lisa Saalfrank <lisa.saalfrank@web.de>
  17.  * @copyright   2020 Lisa Saalfrank
  18.  * @package     Equidea\Engine
  19.  * @version     0.1.0
  20.  */
  21. class ColorGenome {
  22.  
  23.     public const AGOUTI_INACTIVE = 'a';
  24.     public const AGOUTI_SEALBROWN = 'At';
  25.     public const AGOUTI_BAY = 'A';
  26.     public const AGOUTI_WILDBAY = 'A+';
  27.  
  28.     public const EXTENSION_INACTIVE = 'e';
  29.     public const EXTENSION_ACTIVE = 'E';
  30.  
  31.     public const FLAXEN_INACTIVE = 'F';
  32.     public const FLAXEN_ACTIVE = 'f';
  33.  
  34.     protected array $agouti = [
  35.         self::AGOUTI_INACTIVE,
  36.         self::AGOUTI_INACTIVE
  37.     ];
  38.  
  39.     protected array $extension = [
  40.        self::EXTENSION_INACTIVE,
  41.        self::EXTENSION_INACTIVE
  42.     ];
  43.  
  44.     protected array $flaxen = [
  45.         self::FLAXEN_INACTIVE,
  46.         self::FLAXEN_INACTIVE
  47.     ];
  48.  
  49.     /**
  50.      * Returns the allele on the agouti locus that controls the restriction of eumelanin
  51.      * in the horses coat.
  52.      *
  53.      * There are for different variants of the gene:
  54.      * - 'a' which results in black horses
  55.      * - 'At' which results in seal brown horses
  56.      * - 'A' which results in normal type bay horses
  57.      * - 'A+' which results in wild type bay horses
  58.      *
  59.      * @see https://en.wikipedia.org/wiki/Equine_coat_color_genetics#Agouti_phenotypes
  60.      *
  61.      * @return  array   The agouti locus
  62.      */
  63.     public function getAgouti() : array {
  64.         return $this->agouti;
  65.     }
  66.  
  67.     /**
  68.      * Returns the allele on the extension locus that controls whether a horses hair should
  69.      * be black or red.
  70.      *
  71.      * There are two different variants of the gene:
  72.      * - 'e' which will produce chestnut horses
  73.      * - 'E' which produces black, seal brown, bay or wild bay horses
  74.      *
  75.      * @see https://en.wikipedia.org/wiki/Equine_coat_color_genetics#Extension_phenotypes
  76.      *
  77.      * @return  array   The extension locus
  78.      */
  79.     public function getExtension() : array {
  80.         return $this->extension;
  81.     }
  82.  
  83.     /**
  84.      * Returns the allele on the flaxen locus that only affects chestnut based colors.
  85.      * It controls whether the mane and tale are red or blond.
  86.      *
  87.      * There are two different variants of the gene:
  88.      * - 'f' which causes the hair to turn blond, making the color flaxen chestnut
  89.      * - 'F' Which causes th hair to turn red, making it a normal chestnut
  90.      *
  91.      * @see https://en.wikipedia.org/wiki/Flaxen_gene
  92.      *
  93.      * @return  array   The flaxen locus
  94.      */
  95.     public function getFlaxen() : array {
  96.         return $this->flaxen;
  97.     }
  98.  
  99.     /**
  100.      * Checks whether either one or both of the two genes of the allele on the agouti locus
  101.      * match the given single gene.
  102.      *
  103.      * @param   string  $gene   The gene to search for
  104.      *
  105.      * @return  bool    The result of the search
  106.      */
  107.     public function agoutiHas(string $gene) : bool {
  108.         return in_array($gene, $this->agouti);
  109.     }
  110.  
  111.     /**
  112.      * Checks whether either one or both of the two genes of the allele on the extension
  113.      * locus match the given single gene.
  114.      *
  115.      * @param   string  $gene   The gene to search for
  116.      *
  117.      * @return  bool    The result of the search
  118.      */
  119.     public function extensionHas(string $gene) : bool {
  120.         return in_array($gene, $this->extension);
  121.     }
  122.  
  123.     /**
  124.      * Checks whether either one or both of the two genes of the allele on the flaxen locus
  125.      * match the given single gene.
  126.      *
  127.      * @param   string  $gene   The gene to search for
  128.      *
  129.      * @return  bool    The result of the search
  130.      */
  131.     public function flaxenHas(string $gene) : bool {
  132.         return in_array($gene, $this->flaxen);
  133.     }
  134.  
  135.     /**
  136.      * Fills all locuses with their allele pairs. Array must be a multi-dimensional
  137.      * associative array with the keys for agouti, extension and flaxen.
  138.      * The genes of an allele must be provided as an indexed array.
  139.      *
  140.      * Example:
  141.      * [
  142.      *  'agouti' => ['a', 'a'],
  143.      *  'extension' => ['e', 'e'],
  144.      *  'flaxen' => ['f', 'f']
  145.      * ]
  146.      *
  147.      * @param   array   $dna    All color alleles for each locus of a genome
  148.      *
  149.      * @return  void
  150.      */
  151.     public function setAllAlleles(array $dna) : void
  152.     {
  153.         $this->agouti = $dna['agouti'];
  154.         $this->extension = $dna['extension'];
  155.         $this->flaxen = $dna['flaxen'];
  156.     }
  157.  
  158.     /**
  159.      * Retrieves all locuses with their allele pairs as a multi-dimensional
  160.      * associative array with the keys for agouti, extension and flaxen.
  161.      * The genes of an allele are provided as an indexed array.
  162.      *
  163.      * Example:
  164.      * [
  165.      *  'agouti' => ['a', 'a'],
  166.      *  'extension' => ['e', 'e'],
  167.      *  'flaxen' => ['f', 'f']
  168.      * ]
  169.      *
  170.      * @return  array   All color alleles for each locus of a genome
  171.      */
  172.     public function getAllAlleles() : array
  173.     {
  174.         return [
  175.             'agouti' => $this->agouti,
  176.             'extension' => $this->extension,
  177.             'flaxen' => $this->flaxen
  178.         ];
  179.     }
  180.  
  181.     /**
  182.      * Receives an associative array of JSON-encoded allele pairs for each locus.
  183.      * Decodes the JSON import to indexed arrays and stores them in the class. Must
  184.      * contain the locuses agouti, extension and flaxen.
  185.      *
  186.      * Example:
  187.      * [
  188.      *  'agouti' => '["a","a"]',
  189.      *  'extension' => '["e","e"]',
  190.      *  'flaxen' => '["f","f"]'
  191.      * ]
  192.      *
  193.      * @param   array   $jsonDna    All color alleles encoded as JSON
  194.      *
  195.      * @return  void
  196.      */
  197.     public function importJson(array $jsonDna) : void
  198.     {
  199.         $this->agouti = json_decode($jsonDna['agouti']);
  200.         $this->extension = json_decode($jsonDna['extension']);
  201.         $this->flaxen = json_decode($jsonDna['flaxen']);
  202.     }
  203.  
  204.     /**
  205.      * Returns an associative array of JSON-encoded allele pairs for each locus, which
  206.      * are the locuses agouti, extension and flaxen. Note that only the alleles are
  207.      * JSON encoded but not the associations with locuses.
  208.      *
  209.      * Example:
  210.      * [
  211.      *  'agouti' => '["a","a"]',
  212.      *  'extension' => '["e","e"]',
  213.      *  'flaxen' => '["f","f"]'
  214.      * ]
  215.      *
  216.      * @return  All color alleles encoded as JSON
  217.      */
  218.     public function exportJson() : array
  219.     {
  220.         return [
  221.             'agouti' => json_encode($this->agouti),
  222.             'extension' => json_encode($this->extension),
  223.             'flaxen' => json_encode($this->flaxen)
  224.         ];
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement