alberthrocks

Gene Probability Python Code, with spreadsheet creator

Mar 20th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. import random
  2. import xlwt
  3. import sys
  4.  
  5. ## {{{ http://code.activestate.com/recipes/577058/ (r2)
  6. def query_yes_no(question, default="yes"):
  7.     """Ask a yes/no question via raw_input() and return their answer.
  8.    
  9.    "question" is a string that is presented to the user.
  10.    "default" is the presumed answer if the user just hits <Enter>.
  11.        It must be "yes" (the default), "no" or None (meaning
  12.        an answer is required of the user).
  13.  
  14.    The "answer" return value is one of "yes" or "no".
  15.    """
  16.     valid = {"yes":"yes",   "y":"yes",  "ye":"yes",
  17.              "no":"no",     "n":"no"}
  18.     if default == None:
  19.         prompt = " [y/n] "
  20.     elif default == "yes":
  21.         prompt = " [Y/n] "
  22.     elif default == "no":
  23.         prompt = " [y/N] "
  24.     else:
  25.         raise ValueError("invalid default answer: '%s'" % default)
  26.  
  27.     while 1:
  28.         sys.stdout.write(question + prompt)
  29.         choice = raw_input().lower()
  30.         if default is not None and choice == '':
  31.             return default
  32.         elif choice in valid.keys():
  33.             return valid[choice]
  34.         else:
  35.             sys.stdout.write("Please respond with 'yes' or 'no' "\
  36.                              "(or 'y' or 'n').\n")
  37. ## end of http://code.activestate.com/recipes/577058/ }}}
  38.  
  39.  
  40. genotype = [0,0]
  41. homoDom=0;
  42. heteroDom=0;
  43. homoRec=0;
  44. dom=0;
  45. rec=0;
  46. bigtable=[]
  47. print "Genotype\t| Genotype name\t\t| Phenotype"
  48. print "====================================================="
  49. for x in range(0,100):
  50.     genotype[0]=int(random.randrange(0,100)/50)
  51.     genotype[1]=int(random.randrange(0,100)/50)
  52.     if genotype[0] == 1:
  53.             if genotype[1] == 1:
  54.                 print "RR \t\t| homozygous dominant\t| normal skin"
  55.                 bigtable.append(["RR", "homozygous dominant", "normal skin"])
  56.                 homoDom+=1
  57.             else:
  58.                 print "Rr \t\t| heterozygous dominant\t| normal skin"
  59.                 bigtable.append(["Rr", "heterozygous dominant", "normal skin"])
  60.                 heteroDom+=1
  61.     else:
  62.         if genotype[1] == 1:
  63.             print "rR\t\t| heterozygous dominant\t| normal skin"
  64.             bigtable.append(["rR", "heterozygous dominant", "normal skin"])
  65.             heteroDom+=1
  66.         else:
  67.             print "rr\t\t| homozygous recessive\t| albino"
  68.             bigtable.append(["rr", "homozygous recessive", "normal skin"])
  69.             homoRec+=1
  70. print "Statistics:"
  71. print "RR: "+str(homoDom)+"/100 Rr or rR: "+str(heteroDom)+"/100 rr: "+str(homoRec)+"/100."
  72. rec=homoRec
  73. dom=homoDom+heteroDom
  74. print str(dom)+"/100 were dominant. "+str(rec)+"/100 were recessive."
  75. print "Look over the results above. \n"
  76. resp=query_yes_no("Create an Excel worksheet? Press ENTER or y to create one, otherwise n to simply exit.")
  77. if resp == 'yes':
  78.     print "============================================================="
  79.     print "Creating Excel worksheet..."
  80.     print "============================================================="
  81.     wbk = xlwt.Workbook()
  82.     sheet = wbk.add_sheet('Trait Lab Results')
  83.    
  84.     # Create a bold font to use
  85.     boldstyle = xlwt.XFStyle()
  86.      
  87.     # Create a font to use with the style
  88.     font = xlwt.Font()
  89.     font.bold = True
  90.      
  91.     # Set the style's font to this new one you set up
  92.     boldstyle.font = font
  93.    
  94.     # Create the header!
  95.     # Starts at zero, goes by row,column
  96.     sheet.write(0,0,'Genotype',boldstyle)
  97.     sheet.write(0,1,'Genotype Name',boldstyle)
  98.     sheet.write(0,2,'Phenotype',boldstyle)
  99.    
  100.     rowi=1
  101.     # Import the data!
  102.     for result in bigtable:
  103.         sheet.write(rowi, 0, result[0])
  104.         sheet.write(rowi, 1, result[1])
  105.         sheet.write(rowi, 2, result[2])
  106.         rowi+=1
  107.     try:
  108.         wbk.save('TraitLabGenotypesPhenotypesResults.xls')
  109.         print "Done! The Excel spreadsheet is called TraitLabGenotypesPhenotypesResults.xls."
  110.     except IOError:
  111.         print "Error: Excel spreadsheet couldn't be created! :("
Advertisement
Add Comment
Please, Sign In to add comment