Skip to content

Instantly share code, notes, and snippets.

@anpefi
Created February 8, 2016 06:55
Show Gist options
  • Save anpefi/10d61508a0355e416482 to your computer and use it in GitHub Desktop.
Save anpefi/10d61508a0355e416482 to your computer and use it in GitHub Desktop.
This script obtains the distribution if Sannon's Index of diversity for all the AFLP loci
# Assuming you have your aflp matrix data with samples in rows and
# loci in columns. The file is a csv file with the format of msap
#Read data
data <- read.csv("aflp.csv", header=TRUE)
#convert to matrix
matN <- as.matrix(data[,4:length(data[1,])])
#Function for sannon index
shannon <-function(x){
#Calculate Shannon's Diversity Index given a list of binomial alleles
t <- table(x)
if (length(t)<2) return(NaN)
P=t[1]/sum(t)
Q=t[2]/sum(t)
S=-(P*log(P)+Q*log(Q))
return(S)
}
#Apply the funtion to your data
Sannon.I <- apply(matM, 2, shannon)
summary(Sannon.I)
hist(Sannon.I)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment