Advertisement
kjblack

Three Genders

Feb 2nd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.32 KB | None | 0 0
  1.  
  2. determineGenderYDom <- function(chromosone1,chromosone2)
  3. {
  4.     ## Function to determine the gender of a child based on the
  5.     ## child's two chromosones.
  6.     ##
  7.     ## This assumes that Y is dominant with respect to both X and Z
  8.     ##                   X is dominant with respect to Z
  9.    
  10.     if((chromosone1=="Y") | (chromosone2=="Y"))
  11.     {
  12.         return("M")
  13.     }
  14.     else if ((chromosone1=="X") | (chromosone2=="X"))
  15.     {
  16.         return("F")
  17.     }
  18.     return("V")
  19. }
  20.  
  21. determineGenderZDom <- function(chromosone1,chromosone2)
  22. {
  23.     ## Function to determine the gender of a child based on the
  24.     ## child's two chromosones.
  25.     ##
  26.     ## This assumes that Z is dominant with respect to both X and Y
  27.     ##                   Y is dominant with respect to X
  28.    
  29.     if((chromosone1=="Z") | (chromosone2=="Z"))
  30.     {
  31.         return("V")
  32.     }
  33.     else if ((chromosone1=="Y") | (chromosone2=="Y"))
  34.     {
  35.         return("M")
  36.     }
  37.     return("F")
  38. }
  39.  
  40.  
  41.  
  42. ## Change the right hand of this assignment to change the way the
  43. ## script calculates dominance. The right hand side should be one of
  44. ## the functions above.
  45. determineGender <- determineGenderZDom
  46.  
  47. addGenders <- function(leftChromosone1, leftChromosone2,    # chromosones of parent 1
  48.                        rightChromosone1,rightChromosone2)   # chromosones of parent 2
  49. {
  50.     ## Function to determine the chromosones and genders of all possible
  51.     ## children based on the chromosones of both parents.
  52.     if(determineGender(leftChromosone1,leftChromosone2) !=
  53.        determineGender(rightChromosone1,rightChromosone2))
  54.     {
  55.         ## The parents are of two different genders. Assume that they
  56.         ## can have offspring. Return all possible permutations and
  57.         ## the gender for each permutation.
  58.         return(data.frame(
  59.             chromosone1=c(leftChromosone1,leftChromosone2,leftChromosone1,leftChromosone2),
  60.             chromosone2=c(rightChromosone1,rightChromosone2,rightChromosone2,rightChromosone1),
  61.             gender=c(determineGender(leftChromosone1,rightChromosone1),
  62.                      determineGender(leftChromosone2,rightChromosone2),
  63.                      determineGender(leftChromosone1,rightChromosone2),
  64.                      determineGender(leftChromosone2,rightChromosone1))))
  65.     }
  66.     return(NA)
  67. }
  68.  
  69.  
  70. ## Define the possible chromosones
  71. chromosones = c("X","Y","Z")
  72.  
  73. ## Define an empty data frame that will contain all the possible children.
  74. gender = data.frame(chromosone1=character(0),chromosone1=character(0),gender=character(0))
  75.  
  76.  
  77. ## Loop through all possible permutations for parent 1.
  78. for (leftChromosone1 in chromosones)
  79. {
  80.     for (leftChromosone2 in chromosones)
  81.     {
  82.         if((leftChromosone1!="Y") || (leftChromosone2!="Y"))
  83.         {
  84.             ## Assume up front that there are no YY males. This
  85.             ## assumes no males give birth.
  86.  
  87.             ## Now loop through all possible permutations for parent 2.
  88.             for (rightChromosone1 in chromosones)
  89.             {
  90.                 for (rightChromosone2 in chromosones)
  91.                 {
  92.                     if((rightChromosone1!="Y") || (rightChromosone2!="Y"))
  93.                     {
  94.                         ## Assume up front that there are no YY males. This
  95.                         ## assumes no males give birth.
  96.  
  97.                         babies <- addGenders(leftChromosone1, leftChromosone2,
  98.                                              rightChromosone1,rightChromosone2)
  99.                         if(length(babies)>1)
  100.                         {
  101.                             gender <- rbind(gender,babies)
  102.                         }
  103.                        
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.     }
  109. }
  110. ## At this point we have a data frame which has the permutations for
  111. ## children assuming that males cannot give birth. The data frame also
  112. ## has the gender of each child, and it assumes that all combinations
  113. ## are equally likely.
  114.  
  115.  
  116. if (length(gender$gender[(gender$chromosone1 == "Y") & (gender$chromosone2 == "Y")])>0)
  117. {
  118.     ## uh-oh.... it is possible to get YY males with this
  119.     ## ordering. Need to go back and add those possibilities
  120.  
  121.     for (rightChromosone1 in chromosones)
  122.     {
  123.         for (rightChromosone2 in chromosones)
  124.         {
  125.  
  126.             ## Add the possibilities when parent 1 is a YY diploid.
  127.             babies <- addGenders("Y","Y",rightChromosone1,rightChromosone2)
  128.             if(length(babies)>1)
  129.             {
  130.                 gender <- rbind(gender,babies)
  131.             }
  132.  
  133.             if((rightChromosone1!="Y") || (rightChromosone2!="Y"))
  134.             {
  135.                 ## If the chromosones are not both Y then gotta add
  136.                 ## them when parent 2 is both.
  137.                 babies <- addGenders(rightChromosone1,rightChromosone2,"Y","Y")
  138.                 if(length(babies)>1)
  139.                 {
  140.                     gender <- rbind(gender,babies)
  141.                 }
  142.                
  143.             }
  144.         }
  145.     }
  146.  
  147.    
  148. }
  149.  
  150. gender$chromosone1 = as.factor(gender$chromosone1) # convert everything to factors
  151. gender$chromosone2 = as.factor(gender$chromosone2)
  152. gender$gender      = as.factor(gender$gender)
  153.  
  154. print(table(gender$gender))
  155. plot(gender$gender)
  156.  
  157. children <- table(gender$chromosone1,gender$chromosone2)
  158. print(children)
  159. mosaicplot(children)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement