Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Equidea\Engine;
- // Base phenotypes
- define('COLOR_BLACK', 1);
- define('COLOR_SEALBROWN', 2);
- define('COLOR_BAY', 3);
- define('COLOR_WILDBAY', 4);
- define('COLOR_CHESTNUT', 5);
- define('COLOR_FLAXEN_CHESTNUT', 6);
- // Single cream phenotypes
- define('COLOR_SMOKY_BLACK', 7);
- define('COLOR_SMOKY_BROWN', 8);
- define('COLOR_BUCKSKIN', 9);
- define('COLOR_WILD_BUCKSKIN', 10);
- define('COLOR_PALOMINO', 11);
- // Double cream phenotypes
- define('COLOR_SMOKY_CREAM', 12);
- define('COLOR_SEALBROWN_CREAM', 13);
- define('COLOR_PERLINO', 14);
- define('COLOR_CREMELLO', 15);
- /**
- * An object representation of a full horse color genome.
- *
- * A genome contains many different locuses, which are the places where genetic
- * information about color mutations and manipulation of molecules such as
- * eumelanin and pheomelanin are stored.
- *
- * @see https://en.wikipedia.org/wiki/Equine_coat_color_genetics
- *
- * @author Lisa Saalfrank <lisa.saalfrank@web.de>
- * @copyright 2020 Lisa Saalfrank
- * @package Equidea\Engine
- * @version 0.1.0
- */
- class ColorDna
- {
- public const GENE_CHECK_HOMOZYGOUS = true;
- public const GENE_CHECK_HETEROZYGOUS = false;
- // Agouti
- public const AGOUTI_INACTIVE = 'a';
- public const AGOUTI_SEALBROWN = 'at';
- public const AGOUTI_BAY = 'A';
- public const AGOUTI_WILDBAY = 'A+';
- // Extension
- public const EXTENSION_INACTIVE = 'e';
- public const EXTENSION_ACTIVE = 'E';
- // Flaxen
- public const FLAXEN_INACTIVE = 'F';
- public const FLAXEN_ACTIVE = 'f';
- // Cream
- public const CREAM_INACTIVE = 'cr'
- public const CREAM_ACTIVE = 'Cr';
- protected array $agouti = [
- self::AGOUTI_INACTIVE,
- self::AGOUTI_INACTIVE
- ];
- protected array $extension = [
- self::EXTENSION_INACTIVE,
- self::EXTENSION_INACTIVE
- ];
- protected array $flaxen = [
- self::FLAXEN_INACTIVE,
- self::FLAXEN_INACTIVE
- ];
- protected array $cream = [
- self::CREAM_INACTIVE,
- self::CREAM_INACTIVE
- ];
- public function getAgouti() : array {
- return $this->agouti;
- }
- public function getExtension() : array {
- return $this->extension;
- }
- public function getFlaxen() : array {
- return $this->flaxen;
- }
- public function getCream() : array {
- return $this->cream;
- }
- public function agoutiHas(string $gene) : bool {
- return in_array($gene, $this->agouti);
- }
- public function extensionHas(string $gene) : bool {
- return in_array($gene, $this->extension);
- }
- public function flaxenHas(string $gene) : bool {
- return in_array($gene, $this->flaxen);
- }
- public function creamHas(string $gene, bool $type = false) : bool
- {
- if ($type == true) {
- return $this->cream[0] == $gene && $this->cream[1] == $gene;
- }
- return in_array($gene, $this->cream);
- }
- public function setAllAlleles(array $dna) : void
- {
- $this->agouti = $dna['agouti'];
- $this->extension = $dna['extension'];
- $this->flaxen = $dna['flaxen'];
- $this->cream = $dna['cream'];
- }
- public function getAllAlleles() : array
- {
- return [
- 'agouti' => $this->agouti,
- 'extension' => $this->extension,
- 'flaxen' => $this->flaxen,
- 'cream' => $this->cream
- ];
- }
- public function importJson(array $jsonDna) : void
- {
- $this->agouti = json_decode($jsonDna['agouti']);
- $this->extension = json_decode($jsonDna['extension']);
- $this->flaxen = json_decode($jsonDna['flaxen']);
- $this->cream = json_decode($jsonDna['cream']);
- }
- public function exportJson() : array
- {
- return [
- 'agouti' => json_encode($this->agouti),
- 'extension' => json_encode($this->extension),
- 'flaxen' => json_encode($this->flaxen),
- 'cream' => json_encode($this->cream)
- ];
- }
- }
- // Color inheritance
- function inheritColorDna(ColorDna $mother, ColorDna $father) : ColorDna
- {
- $randomGene = function(array $genes) : string {
- return $genes[array_rand($genes)];
- };
- $motherAlleles = $mother->getAllAlleles();
- $fatherAlleles = $father->getAllAlleles();
- $childAlleles = [];
- foreach (array_keys($motherAlleles) as $allele)
- {
- $childAlleles[$allele] = [
- $randomGene($motherAlleles[$allele]),
- $randomGene($fatherAlleles[$allele])
- ];
- }
- $child = new ColorDna();
- $child->setAllAlleles($childAlleles);
- return $child;
- }
- // Color calculation
- function findBasePhenotype(ColorDna $dna) : int
- {
- if ($dna->extensionHas(ColorDna::EXTENSION_ACTIVE))
- {
- if ($dna->agoutiHas(ColorDna::AGOUTI_WILDBAY)) {
- return COLOR_WILDBAY;
- }
- if ($dna->agoutiHas(ColorDna::AGOUTI_BAY)) {
- return COLOR_BAY;
- }
- if ($dna->agoutiHas(ColorDna::AGOUTI_SEALBROWN)) {
- return COLOR_SEALBROWN;
- }
- return COLOR_BLACK;
- } else
- {
- if (!$dna->flaxenHas(ColorDna::FLAXEN_INACTIVE)) {
- return COLOR_FLAXEN_CHESTNUT;
- }
- return COLOR_CHESTNUT;
- }
- }
- function hasDoubleCreamPhenotype(ColorDna $dna) : bool
- {
- return $dna->creamHas(
- ColorDna::CREAM_ACTIVE,
- ColorDna::GENE_CHECK_HOMOZYGOUS
- );
- }
- function findSingleCreamPhenotype(int $base) : int
- {
- switch ($base)
- {
- case COLOR_BLACK: return COLOR_SMOKY_BLACK;
- case COLOR_SEALBROWN: return COLOR_SMOKY_BROWN;
- case COLOR_BAY: return COLOR_BUCKSKIN;
- case COLOR_WILDBAY: return COLOR_WILD_BUCKSKIN;
- case COLOR_CHESTNUT:
- case COLOR_FLAXEN_CHESTNUT: return COLOR_PALOMINO;
- }
- }
- function findDoubleCreamPhenotype(int $base) : int
- {
- switch ($base)
- {
- case COLOR_BLACK: return COLOR_SMOKY_CREAM;
- case COLOR_SEALBROWN: return COLOR_SEALBROWN_CREAM;
- case COLOR_BAY:
- case COLOR_WILDBAY: return COLOR_PERLINO;
- case COLOR_CHESTNUT:
- case COLOR_FLAXEN_CHESTNUT: return COLOR_CREMELLO;
- }
- }
- function findBirthPhenotype(ColorDna $dna) : int
- {
- $base = findBasePhenotype($dna);
- if ($dna->creamHas(ColorDna::CREAM_ACTIVE))
- {
- if (hasDoubleCreamPhenotype($dna)) {
- return findDoubleCreamPhenotype($base);
- }
- return findSingleCreamPhenotype($base);
- }
- return $base;
- }
- function hasGreyPhenotype(ColorDna $dna) : bool
- {
- return !$dna->greyHas(
- ColorDna::GREY_INACTIVE,
- ColorDna::GENE_CHECK_HOMOZYGOUS
- );
- }
- function findIntermediateGreyPhenotype(int $base) : int
- {
- switch ($base)
- {
- case COLOR_BLACK: return COLOR_BLACK_GREY;
- case COLOR_SEALBROWN: return COLOR_SEALBROWN_GREY;
- case COLOR_BAY: return COLOR_BAY_GREY;
- case COLOR_WILDBAY: return COLOR_WILDBAY_GREY;
- case COLOR_CHESTNUT: return COLOR_FLAXEN_CHESTNUT_GREY;
- case COLOR_FLAXEN_CHESTNUT: return COLOR_FLAXEN_CHESTNUT_GREY;
- case COLOR_SMOKY_BLACK: return COLOR_SMOKY_BLACK_GREY;
- case COLOR_SMOKY_BROWN: return COLOR_SMOKY_BROWN_GREY;
- case COLOR_BUCKSKIN: return COLOR_BUCKSKIN_GREY;
- case COLOR_WILD_BUCKSKIN: return COLOR_WILD_BUCKSKIN_GREY;
- case COLOR_PALOMINO: return COLOR_PALOMINO_GREY;
- }
- }
- function findFinalGreyPhenotype(ColorDna $dna) : int
- {
- if ($dna->greyHas(ColorDna::GREY_NORMAL))
- {
- $isNormalHomozygous = $dna->greyHas(
- ColorDna::GREY_NORMAL,
- ColorDna::GENE_CHECK_HOMOZYGOUS
- );
- if ($isNormalHomozygous == true) {
- return COLOR_SOLID_GREY;
- }
- return COLOR_FLEABITTEN_GREY;
- }
- return COLOR_DAPPLE_GREY;
- }
Add Comment
Please, Sign In to add comment