Simulating “Animal Crossing: New Horizons” Mendelian Flower Genetics in Mathematica​
​Pedro Cabral – Researcher, Data Scientist.

Introduction

In the video game Animal Crossing: New Horizons, by Nintendo, one interesting game mechanic is the ability of planting and breeding many species flowers. The game uses simple Mendelian genetics and many results can be obtained with the Punnet square and simple statistics. In this article, I define many useful functions for representing the flower genes, computationally breeding two flowers, and plotting and charting the Punnet square.

Understanding the Genes

Before starting the definition of the functions and breeding of the flowers, lets understand how exactly the genes present in the flowers work.
“Rr”​
Controls whether a flower is red based or not. Red based flowers are red, black, pink, and orange . Non-red are yellow, white, and purple.
​Note: Not all flowers with the “R” gene will be red-based, since other genes can cover it, but a flower with the genes “rr” will never be red-based.
◼
  • “RR” or “Rr”  Red-based.
  • ◼
  • “rr”  Non-red.
  • “Yy”​
    Controls whether a flower has any yellow in it (yellow or orange).
    ◼
  • “YY” or “Yy”  Yellow or Orange.
  • ◼
  • “yy”  Non-yellow.
  • “Ww”​
    Controls whether a white flower will be white or show its underlying color: either purple or blue depending on the species.
    ​Note: White flowers are fairly recessive to most colors, so this gene will only have an effect if the white isn’t getting covered by reds, yellows, or other colors.
    ◼
  • “WW” or “Ww”  White.
  • ◼
  • “ww”  Purple or blue.
  • “Ss”​
    Controls which shade the red-based flower will be. Pink is the lightest shade, red is the middle, and black is the darkest. This gene has no effect on non-red flowers.
    ◼
  • “SS”  Pink.
  • ◼
  • “Ss”  Red.
  • ◼
  • “ss”  Black.
  • Functions and Definitions

    FlowerGene

    Splits the gene into partitions of two. Useful on operations involving breeding and Punnet square.
    In[]:=
    FlowerGene::invgene="Flower gene must contain 8 alleles.";
    In[]:=
    FlowerGene[gene_String]:=Module[{str=StringReplace[gene,"-"""],part},​​(*Checkifthere's8alleles.*)​​If[!(StringLength[str]8),​​Return[Message[FlowerGene::invgene]];​​];​​(*Partitionintogroupsoftwo.*)​​part=StringPartition[str,2];​​(*Returnthesplittedalleles.*)​​Return[StringSplit[#,""]&/@part];​​];
    In[]:=
    (*Exampleusage:*)​​FlowerGene["RR-yy-WW-Ss"]
    Out[]=
    {{R,R},{y,y},{W,W},{S,s}}

    FlowerColor

    Returns the color for a given gene. This approach of determining a specific flower color is ad-hoc.
    In[]:=
    FlowerColor[gene_]:=Modulestr=StringReplace[gene,"-"""],flowergenes=
    FlowerGenes
    ,​​Return[flowergenes〚str〛];​​;
    In[]:=
    (*Exampleusage:*)​​FlowerColor["RR-yy-WW-Ss"]
    Out[]=
    Red

    RandomFlowerGene

    Generates a random flower gene.
    In[]:=
    RandomFlowerGene[]:=Module[{},​​RandomChoice[{"RR","Rr","rr"}]<>RandomChoice[{"YY","Yy","yy"}]<>RandomChoice[{"WW","Ww","ww"}]<>RandomChoice[{"SS","Ss","ss"}]​​];
    In[]:=
    (*Exampleusage:*)​​RandomFlowerGene[]
    Out[]=
    RRyyWWSs

    FlowerIcon

    Returns an icon for a given color .
    In[]:=
    FlowerIcon::invcolor="`` is not a valid flower color.";
    In[]:=
    FlowerIcon[color_String]:=Modulecolors=AssociationThread{"red","white","orange","yellow","purple","black","pink","blue"},ImageResize[#,{28}]&/@
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,​​(*Checkifthecolorisavalidcolor*)​​If[!KeyExistsQ[colors,ToLowerCase[StringTrim[color]]],​​Return[Message[FlowerIcon::invcolor,color]];​​];​​(*Returnthecorrespondingcolor.*)​​Return[colors〚ToLowerCase[StringTrim[color]]〛];​​;
    In[]:=
    (*Exampleusage:*)​​FlowerIcon["Red"]
    Out[]=

    OrderGenes

    Order the genes into the correct order.
    In[]:=
    OrderGenes["r","R"]="Rr";​​OrderGenes["y","Y"]="Yy";​​OrderGenes["w","W"]="Ww";​​OrderGenes["s","S"]="Ss";
    In[]:=
    (*Foreverythingelse,leaveasis.*)​​OrderGenes[a_,b_]:=a<>b;
    In[]:=
    (*Exampleusage:*)​​OrderGenes["r","R"]
    Out[]=
    Rr

    PunnetSquare

    Creates a Punnet square for two genes.
    In[]:=
    PunnetSquare[geneA_List,geneB_List]:=Module[{cross},​​(*Crossallthealleles.*)​​cross=Table[OrderGenes[rA,rB]<>OrderGenes[yA,yB]<>OrderGenes[wA,wB]<>OrderGenes[sA,sB],​​{rA,geneA〚1〛},​​{rB,geneB〚1〛},​​{yA,geneA〚2〛},​​{yB,geneB〚2〛},​​{wA,geneA〚3〛},​​{wB,geneB〚3〛},​​{sA,geneA〚4〛},​​{sB,geneB〚4〛}​​];​​(*Returnthearrayofcolors.*)​​Return[ArrayReshape[Flatten[cross],{16,16}]];​​];
    In[]:=
    (*Exampleusage:*)​​Short[PunnetSquare[FlowerGene["RR-yy-WW-Ss"],FlowerGene["rr-YY-WW-ss"]],2]

    UniqueGenes

    Gets all the unique genes of a Punnet square.

    PunnetSquareCount

    Counts the occurrences on a Punnet square.

    PunnetSquarePercent

    PunnetSquareChart

    PunnetSquareColors

    Breeding of “Seed” Flowers (Parental Generation)

    “Seed” flowers are the Parental Generation flowers. In this case, only three different genes represents the P Generation. In this section, we will compute the breeding of three different parental flowers with distinct genes:
    ◼
  • Parental Red  RR-yy-WW-Ss
  • ◼
  • Parental Yellow  rr-YY-WW-ss
  • ◼
  • Parental White  rr-yy-Ww-ss
  • Breeding of Parental Red and Parental Yellow

    Breeding of Parental Red and Parental White

    Breeding of Parental Yellow and Parental White

    Now, let’s compute the Filial 1 Generation.
    Tip: You can hover your mouse over a flower to know its color, gene, and probability.

    Breeding the Rarest Flower: Blue

    The rarest flower is the Hybrid Blue, only one speciment exists, with the gene “RR-YY-ww-ss”.