如何与R执行双对配对。请解决一些代码问题

时间:2019-04-15 14:24:45

标签: r genetic

我正在从事植物工作,您可能知道,我们必须做很多杂交来提高品种。一种杂交称为“双对交配”,我想实现双对交配(这意味着初始种群的每个亲本都精确地以2个不同的杂交方式发生),以便为将来创造出无血统的血统。育种计算值。

这只是逻辑问题,因为我没有练习足够的R。我认为您可以在不了解所有内容的情况下帮助我。

下面的部分显示了我被卡住的地方。       #选择配对

  pairs <- data.frame()
  for (i in 1:2) { 
  pairs <- rbind(pairs,(data.frame(dam=sample(pdams, npairs, replace=TRUE), sire=sample(psires, npairs, replace=TRUE))))
  }
**

这是我的整个剧本:

##################################
####### PEDIGREE FUNCTION ########
##################################

# function to create a pedigree with dispersal
# inputs:
# nids = list of number of individuals per generation
# ngenerations = number of generations to simulate
# epm = rate of extra-pair mating (defaults to NULL, no extra-pair)
# missing = probability that one parent is missing in the pedigree
# nonb = proportion of each generation that is non-breeding
# gridsize = length of one size of (square) spatial grid 
# dispmean = mean dispersal distance (lognormal)
# dispvar = variance in dispersal distance (lognormal)

pedfun<-function(nids, ngenerations, epm=NULL, missing=NULL, nonb=0.4, 
             gridsize=50, dispmean, dispsd){

# get list of individuals and their generations
gener<-1:ngenerations

genern <- rep(1:ngenerations, times = nids)
ID <- 1:sum(nids)

# runs on generation-by-generation basis
for(i in 1:ngenerations){

  id<-ID[which(genern==i)]
  dam<-rep(NA, nids[i])
  sire<-rep(NA, nids[i])

  Xloc<-rep(NA, nids[i])
  Yloc<-rep(NA, nids[i])

  # randomly allocates sex (0 = male, 1 = female)
  sex<-sample(c(0,1), length(id), replace=TRUE)

  # for first generation, no dams or sires are known 
  # so remain NA

if(i==1){

  # for first generation
  # spatial locations sampled at random for X and Y coordinates
  Xloc<-sample(1:gridsize, length(id), replace=TRUE)
  Yloc<-sample(1:gridsize, length(id), replace=TRUE)

  # combine into single data frame
  pedigree<-data.frame(id=id, dam=dam, sire=sire, 
                       generation=i, sex=sex,
                       Xloc=Xloc, Yloc=Yloc, disp_dist=NA,
                       fall=0)

}else if(i>1){

  # for all generations after first
  # list of all possible dams and sires
  # from previous generation
  pdams<-pedigree$id[which(pedigree$generation==(i-1) &
                             pedigree$sex==1)]
  psires<-pedigree$id[which(pedigree$generation==(i-1) &
                              pedigree$sex==0)]

  # determine number of pairs
  # depending on how many males and females
  # and the proportion of the population that is non-breeding
  npairs<-min(length(pdams), length(psires)) - 
    round(min(length(pdams), length(psires))*nonb)

     # selects breeding males and females
  pdams<-pedigree$id[which(pedigree$generation==(i-1) & 
                             pedigree$sex==1 & pedigree$fall==0)]
  psires<-pedigree$id[which(pedigree$generation==(i-1) & 
                              pedigree$sex==0 & pedigree$fall==0)]

  if(length(pdams)<npairs | length(psires)<npairs){
    npairs<-min(length(pdams), length(psires))
  }

  #selection of pairs

  pairs <- data.frame()
  for (i in 1:2) { 
  pairs <- rbind(pairs,(data.frame(dam=sample(pdams, npairs,         replace=TRUE), sire=sample(psires, npairs, replace=TRUE))))
  }
**

  # gives each offspring their parental pair
  pairid<-as.numeric(sample(rownames(pairs), 
                            length(id), replace=TRUE))

  # gives each offspring their sex
  sex<-sample(c(0,1), length(id), replace=TRUE)

  # put into dataframe format
  addped<-data.frame(id=id, 
                     dam=pairs$dam[pairid], 
                     sire=pairs$sire[pairid],
                     generation=i, 
                     sex=sex)

谢谢!

0 个答案:

没有答案
相关问题