Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified MakeDiploidFSTMat() function #19

Open
akijarl opened this issue May 29, 2019 · 1 comment
Open

Modified MakeDiploidFSTMat() function #19

akijarl opened this issue May 29, 2019 · 1 comment

Comments

@akijarl
Copy link

akijarl commented May 29, 2019

I noticed that the MakeDiploidFSTMat() function does not proceed in cases where individual alleles being considered are not represented by both homozygotes and a heterozygote in the population (i.e. there are not occurrences of 0, 1, and 2 in the snp matrix).

Modified the function by allowing it to proceed as long as any combination of 0, 1, 2, or 9 exists in the matrix, as long as it only contains entries from among those values:

MakeDiploidFSTMat<-function(SNPmat,locusNames,popNames){
    locusname <- unlist(locusNames)
    popname <- unlist(popNames)
    snplevs <- levels(as.factor(unlist(SNPmat)))
    if(any(!(snplevs%in%c(0,1,2,9)))==TRUE) {
      print("Error: Your snp matrix has a character other than 0,1,2 or 9")
      break
    }
    if (dim(SNPmat)[1] != length(popname)) {
      print("Error: your population names do not match your SNP matrix")
      break
    }
    if (dim(SNPmat)[2] != length(locusname)) {
      print("Error:  your locus names do not match your SNP matrix")
      break
    }
    writeLines("Calculating FSTs, may take a few minutes...")
    nloci <- length(locusname)
    FSTmat <- matrix(NA, nrow = nloci, ncol = 8)
    for (i in 1:nloci) {
      FSTmat[i, ] = unlist(getFSTs_diploids(popname, SNPmat[,i]))
      if (i%%10000 == 0) {
        print(paste(i, "done of", nloci))
      }
    }
    outTemp = as.data.frame(FSTmat)
    outTemp = cbind(locusname, outTemp)
    colnames(outTemp) = c("LocusName", "He", "FST", "T1", "T2", 
                          "FSTNoCorr", "T1NoCorr", "T2NoCorr", "meanAlleleFreq")
    return(outTemp)
  }

P.S.
In order to run this modified function need to redefine the function getFSTs_diploids() in the global environment:

getFSTs_diploids = function(popNameList, SNPDataColumn){  
  #eliminating the missing data for this locus
  popnames=unlist(as.character(popNameList))
  popNameTemp=popnames[which(SNPDataColumn!=9)]
  snpDataTemp=SNPDataColumn[SNPDataColumn!=9]
  
  HetCounts <- tapply(snpDataTemp, list(popNameTemp,snpDataTemp), length)
  HetCounts[is.na(HetCounts)] = 0
  
  #Case: all individuals are genetically identical at this locus
  if(dim(HetCounts)[2]==1){
    return (list(He=NA,FST=NA, T1=NA, T2=NA,FSTNoCorr=NA, T1NoCorr=NA, T2NoCorr=NA,meanAlleleFreq = NA))
  }
  
  if(dim(HetCounts)[2]==2){
    if(paste(colnames(HetCounts),collapse="")=="01"){HetCounts=cbind(HetCounts,"2"=0)}
    if(paste(colnames(HetCounts),collapse="")=="12"){HetCounts=cbind("0"=0,HetCounts)} 
    if(paste(colnames(HetCounts),collapse="")=="02"){HetCounts=cbind(HetCounts[,1],"1"=0, HetCounts[,2])}
  }
  
  out = WC_FST_Diploids_2Alleles(HetCounts)	
  return(out)
}
@whitlock
Copy link
Owner

whitlock commented Aug 19, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants