I Accidentally Wrote A 😹Cat-Genetics 🧬 Algorithm While Following My Python Beginner Course

I Accidentally Wrote A 😹Cat-Genetics 🧬 Algorithm While Following My Python Beginner Course

Instructions unclear printing kittens instead...

Okay, hear me out. It was an accident. You probably remember how I told you to find meaningful exercises to you and I decided to learn Objects and Classes with cats instead of customer databases. So about that, it might have escalated.

As a background - I did work with cat genetics in the past. I educated people, made classes, and was a mentor to some when it came to that specific topic, and even later when I was not in that field anymore, it would stay one of my biggest interests and I could read news and studies about new mutations and new breeding developments about new colors for hours.

The Initial Idea

When I did my Objects and Classes exercise, I wondered if I could make them interact with each other. I did have Lists, Tuples, and For-Loops in my lessons before, so I knew a certain degree of interactivity was possible. I thought "Wouldn't it be fun if I had 2 cats with certain genetic traits and then I just reshuffle those into a new cat. That would be a fun exercise, right?"

I looked at how I could combine those, but I was really unsure what to look for because all in all, I am really just a beginner in this field. I JUST learned about the existence of Classes. I was a little frustrated because I was stuck and couldn't go forward. One thing I usually do then is trying to work on something else and then later come back with a fresh mind.

I Realized I Bit A Too Big Of A Piece From That Cake

So instead of worrying about how to recombine the parents, I wrote a list of which traits I want the parents to have that can be inherited by the kittens. And while I was writing them down I felt bad cause my initial plan was, that each parent had one trait and the kitten would pick one of them. But this is genetically incorrect because recessive and dominant traits exist and depending on how they recombine on the kitten, certain traits show or don't. On top parents could be homozygous or heterozygous which in fact would again have an influence on the kitten. Oh no.

Why Cats Are Fairly Complicated

Each trait contains two parts. One comes from mom, one from dad. Some traits need to come from both parents to be seen (recessive) but some just need to come from one parent and as long as they are there, they will show (dominant). If an animal is homozygous it means both traits it has, are the same. It does not "hide" a recessive side. If an animal is heterozygous it means that it either has only one dominant side that makes a trait show or it has a recessive side that's inactive unless it's recombined with another of its kind on its offspring.

A simple example would be the long fur on a cat. It's recessive and that means if you mate a long-haired cat with a short-haired, all kittens would be short-haired but would have saved the information of the longhaired parent in their DNA. If you mate those with each other, you would get long- and short-haired cats because there is a chance of those hidden and inactive sides recombining.

the-lucky-neko-JYhw8TtoxTg-unsplash.jpg Photo by The Lucky Neko on Unsplash

Red Or Black?

And then there are colors! Cat colors are basically kind of a Boolean. There are only 2 base cat colors. Black and Orange (red) and are saved on the X-Chromosome. Since girls have two, they can have black and red, and male cats have only one and therefore they can only have one of them at the same time.

HOW SHOULD I MAKE A CODE FOR THAT, WHAT?

One Deep Breath - If Nature Can Do It, I Can Do It! I Guess...

I already learned that no matter how complicated the rules are, you can put them into code. It just may take a bit. If nature can follow these rules, then I can write them down. So I wrote down some pseudo code that would wrap up everything I wanted to implement.

Shorthair > Longhair
Black = Orange
Striped > Solid
F = XX, M =XY
Xb = black X-chromosome
Xr = red X-chromosome
Striped + XbXr = Torbie
Xb+Xr = Tortie
Tortie, Torbie = Female

This ruleset would not really define how traits are inherited but make it easier for me to take a cat with traits and print out it looks.

The First Mockup

After my first evening, I came up with this.

E_KuzuIXIAE9ciI.png

I made a Class and defined its traits and then "created" example cats myself as I did with the previous exercise.

E_KurenXsAI_jZa.png

Following my defined rules, the code then gave me this.

E_Ku8vxXsAAcluY.png

The Code Knows How A Cat Looks!

Interpreting the traits of a cat and putting them out, following a ruleset is one thing. Making a simulation that would combine two cats and then realistically reshuffle genes to create new ones, was something completely different.

Back To The Whiteboard

The next day I decided that I had to kind of start over. Especially to have the red-black problem solved. With the help of coldino who was mentoring me through the whole process, we first created a code that was similar to the kitten code before. One cat has two genetic sides from mother and father. We randomize which code a kitten will get by setting it up like this. For simplicity reasons, we treated gender as another kind of trait.

def mate(mother: Cat, father: Cat):
    agouti_pair = inherit_gene((mother.mother.agouti, mother.father.agouti),
                               (father.mother.agouti, father.father.agouti))
    longfur_pair = inherit_gene((mother.mother.long_fur, mother.father.long_fur),
                                (father.mother.long_fur, father.father.long_fur))
    red_pair = inherit_gene((mother.mother.red, mother.father.red),
                            (father.mother.red, father.father.red))

    sex = pick_random((Sex.X, Sex.Y))

That would then create a kitten with the randomized code that the initial function could readout and interpret.

Wait, It Works?

The big magic happened when this code

for i in range(4):
    kitten = mate(cat_a, cat_b)
    print(kitten.phenotype)

gave us this output

['male', 'striped', 'red', 'shorthair']
['female', 'striped', 'red', 'shorthair']
['female', 'tortie', 'shorthair']
['female', 'striped', 'black', 'shorthair']

The program understood! I probably printed another 40 cats just to see if it was right. I changed the genetics of the parent-cats over and over again. It still worked and I was amazed.

I Want More

I clearly surpassed my initial goal to understand how Classes and Objects work. But even better - I was hooked to push this program further - supporting more genes and having a visual output. Now I have to go back to my beginner's course, to progress with my red-line content. But this success got me so excited, that I want to learn more to do even better things.

Photo by The Lucky Neko on Unsplash

 
Share this