Bruvo.distance <- function(genotype1, genotype2, maxl=9, usatnt=2, missing=-9) { require(combinat) # for permn if(identical(genotype1, genotype2)) {dist <- 0} else { if((length(genotype1)>maxl & length(genotype2)>maxl)| genotype1[1]==missing|genotype2[1]==missing){ dist <- NA} else { if(length(genotype1) >= length(genotype2)) { genotypeL <- genotype1/usatnt; genotypeS <- genotype2/usatnt} else { genotypeL <- genotype2/usatnt; genotypeS <- genotype1/usatnt} # if genotypes are identical, just return a distance of zero without doing the rest # of the calculation # if genotypes are both longer than 9, skip this calculation because it will take hours # whichever genotype has more alleles, make this genotypeL (long) and the other # genotypeS (short) # convert alleles into repeat counts by dividing by usatnt kl <- length(genotypeL) # sets the ploidy level for this genotype comparison ks <- length(genotypeS) # number of alleles in the shorter genotype allele.distances <- array(0 , c(kl,ks)) # Create an empty matrix to contain the raw distances between alleles for(n in 1:kl) { for(m in 1:ks) {allele.distances[n,m] <- genotypeL[n] - genotypeS[m]}} # fills the array with the differences in allele repeat count geometric.distances <- array(1 - 2^-abs(allele.distances) , c(kl,ks)) # geometric transformation based on mutation probabilities #Next, find the minimum distance sum among all permutations column <- 1:ks # an index of all columns (genotypeS alleles) row <- 1:kl # an index of all rows (genotypeL alleles) combinations <- combn(row, ks, FUN = NULL, simplify=FALSE) # all combinations of alleles in genotypeL that can be matched to non-virtual # alleles in genotypeS permutations <- permn(ks) # all possible orders that alleles within these combinations can go in mindist <- Inf # this variable will store the minimum sum encountered so far. for(i in 1:length(combinations)) { # the loop to go through every possible sum of compatible allele comparisons rowcomb <- combinations[[i]] # choose one combination of rows for this round for(l in 1:length(permutations)){ # go through all orders of this combinations of rows sum <- 0 # this is si, the sum of allele comparisons for(j in 1:ks){ sum <- sum + geometric.distances[rowcomb[permutations[[l]][j]],column[j]]} # the loop to calculate the sum for this permutation if(sum < mindist) {mindist <- sum} # is this the minimum sum found so far? }} dist <- (mindist+kl-ks)/kl # add 1 for each infinite virtual allele, then divide by the ploidy }} dist } read.GeneMapper<-function(infiles, loci){ if(length(infiles)!=length(loci)){ print("infiles and loci must be vectors of the same length") NA }else{ genotypedata<-list() length(genotypedata)<-length(loci) names(genotypedata)<-loci for(i in 1:length(loci)){ locusdata<-read.table(infiles[i],sep="\t",header=TRUE) genotypedata[[i]]<-list() length(genotypedata[[i]])<-length(locusdata$Sample.Name) names(genotypedata[[i]])<-as.character(locusdata$Sample.Name) alleleindex<-grep("Allele",names(locusdata),value=FALSE) for(j in 1:length(locusdata$Sample.Name)){ untrimmedgenotype<-locusdata[j,alleleindex] genotypedata[[i]][[j]]<-untrimmedgenotype[!is.na(untrimmedgenotype)] } } genotypedata } } distance.matrix.1locus<-function(gendata, distmetric=Bruvo.distance, progress=TRUE, ...){ count<-length(gendata) # how many genotypes there are distances<-matrix(nrow=count, ncol=count, dimnames=list(names(gendata),names(gendata))) for(m in 1:count){for(n in m:count){ thisdistance<-distmetric(gendata[[m]],gendata[[n]], ...) distances[m,n]<-thisdistance distances[n,m]<-thisdistance if(progress){print(c(names(gendata)[[m]],names(gendata)[[n]]))} }} distances } mean.distance.matrix <- function(gendata, samples, loci, all.distances=FALSE, usatnts=NULL, ...){ # create an array containing all distances by locus and sample loci.matrices<-array(dim=c(length(loci),length(samples),length(samples)), dimnames=list(loci,samples,samples)) for(i in loci){ if(is.null(usatnts)){ loci.matrices[i,,]<-distance.matrix.1locus(gendata[[i]][samples],...) } else { loci.matrices[i,,]<-distance.matrix.1locus(gendata[[i]][samples], usatnt=usatnts[i],...) } } # calculate the mean matrix across all loci mean.matrix<-matrix(nrow=length(samples),ncol=length(samples), dimnames=list(samples,samples)) for(j in samples){ for(k in samples){ mean.matrix[j,k]<-mean(loci.matrices[,j,k][!is.na(loci.matrices[,j,k])]) } } # return the mean matrix and possibly the array as well if(all.distances){ list(loci.matrices, mean.matrix) } else { mean.matrix } }