Search code examples
pythongenetics

Quick thought regarding .split()


So I need to make a punnett square using python. A punnett square is basically a simple method of determining visible and sometimes non-visible traits. My code so far takes the two parent's genetic makeup and finds all the different combinations of A's and G's. The only problem I am having at this point is that when I print, the letters aren't in the correct order. For example: for each "child's" possible genetic makeup, there are two A's (either capital or lowercase) and two G's ( also either upper or lowercase). I have done a fair amount of research and the only other question/answer on SOF that was somewhat related to mine was not clear and didn't work. My code is as follows.

import itertools
import string

#genepool_1 refers to parent 1's alleles
#genepool_2 refers to parent 2's alleles
#outerlayerf1 refers to both parent genetic contributions to the first generation of offspring
#f1 refers to the punnett square of the first generation of offspring

#parent 1
genepool_1 = ['Aa','Gg']
parent_1 = sorted(list(itertools.product(*genepool_1)))

#parent 2
genepool_2 = ['aa','GG']
parent_2 = sorted(list(itertools.product(*genepool_2)))


#F1 or Parent 1/2 Offspring
outerlayerf1 = [parent_1,parent_2]
f1___________ = list(itertools.product(*outerlayerf1))
f1__________ = str(f1___________)
f1_________ = f1__________.replace('[','')
f1________ = f1_________.replace(']','')
f1_______ = f1________.replace("'",'')
f1______ = f1_______.replace(' ','')
f1_____ = f1______.replace(')),((', ') (')
f1____ = f1_____.replace('((', '(')
f1___ = f1____.replace('))',')')
f1__ = f1___.replace('),(','')
f1_ = f1__.replace(',','')
print f1_

And it prints out

(AGaG) (AGaG) (AGaG) (AGaG) (AgaG) (AgaG) (AgaG) (AgaG) (aGaG) (aGaG) (aGaG) (aGaG) (agaG) (agaG) (agaG) (agaG)

When it should print

(AaGG) (AaGG) (AaGG) (AaGG) (AagG) (AagG) (AagG) (AagG) (aaGG) (aaGG) (aaGG) (aaGG) (aagG) (aagG) (aagG) (aagG)

(I am aware that each option is printed 4 times. It needs to be this way to have the most accurate probabilities)

Thanks so much,

Eli


Solution

  • I am horribly confused by everything related to this question, but I think I got it to work with this, so at least I hope that helps:

    f1 = [
         thingies[0][0] + thingies[1][0] + thingies[0][1] + thingies[1][1]
         for thingies in zip(parent_1, parent_2)
    ] * 4
    print(f1)