\FloatBarrier

\section{Testing for null alleles}


<<echo=FALSE, results='hide', message=FALSE>>=
nullalleles<-null.all(cats)  
obs.ho<-nullalleles$homozygotes$observed
boot.ho<-nullalleles$homozygotes$bootstrap
tot.obs.ho<-nullalleles$homozygotes$overall$observed
tot.dist.ho<-nullalleles$homozygotes$overall$distribution
@

\subsection{Comparison of the observed number of homozygotes vs. expected - overall}
Boxplots show the bootstrap distribution of the expected number of homozygotes for each allele with the boxes showing the 25th (lower edge), 50th (solid line), and 75th (upper edge) percentiles of the distribution and the whiskers showing 1.5 * the inter-quartile range. Solid black dots indicate outliers while red dots indicate the observed number of homozygotes for the allele. If the red dot is above the end of the whisker it suggests that there is an excess of homozygotes for that locus, 

If your results suggest that there is an excess of homozygotes for one (or more) of your loci, you might have a null allele. On the other hand, it could also be incomplete mixing across populations. Try rerunning your analysis by subpopulation or region and see if that resolves the excess of homozygotes. If it doesn't, then you should consider taking more robust measures to check for null alleles. See for instance Girard's (2011) paper on a robust statistical method to detect null alleles.  
\newline
\newline The probability of the observed number of homozygotes for each locus is available in the results object using null.all\$homozygotes\$overall
<<null_all_overall_ho, echo=FALSE, fig.width=7, fig.height=4, fig.path=fig.path, dev=dev, dpi=dpi, fig.cap="Number of observed homozygotes at each locus (red dots) compared to the distribution of the expected number of homozygotes assuming HWE (boxplots)">>=
tot.dist.ho<-reshape2::melt(tot.dist.ho)
toh <- data.frame(xx=tot.obs.ho)
print(ggplot(data=tot.dist.ho, aes(x=factor(Var2),y=value))+geom_boxplot(fill="grey")+theme_classic()+xlab("Locus")+ylab("Number of homozygotes")+scale_x_discrete(labels=names(cats@loc.n.all))+geom_point(data=toh, aes(x=1:length(xx),y=xx),colour="red",size=3))
@
\FloatBarrier

\subsection{Comparison of the observed number of homozygotes vs. expected - by locus}
Boxplots show the bootstrap distribution of the expected number of homozygotes for each allele with the boxes showing the 25th (lower edge), 50th (solid line), and 75th (upper edge) percentiles of the distribution and the whiskers showing 1.5 * the inter-quartile range. Solid black dots indicate outliers while red dots indicate the observed number of homozygotes for the allele. If the red dot is above the end of the whisker it suggests that there is an excess of homozygotes for that allele. 
\newline
\newline The probability of the observed number of homozygotes for each allele is available in the results object using null.all\$homozygotes\$probability.obs

\noindent
<<null_all, echo=FALSE, fig.width=7, fig.height=4, fig.path=fig.path, dev=dev, dpi=dpi>>=
for (i in 1:length(obs.ho)) {
  boot.dist<-reshape2::melt(boot.ho[[i]])
  obs.val<-as.matrix(obs.ho[[i]],ncol=1)
  rownames(obs.val)<-seq(1:length(obs.ho[[i]]))
  obs.val<-reshape2::melt(t(obs.val))
  print(
    ggplot(data=boot.dist,aes(as.factor(Var2),value)) +geom_boxplot(fill="lightgrey")+theme_classic()+geom_point(data=obs.val,aes(as.factor(Var2),value, colour="red"),size=3)+ylab("Number of homozygotes")+xlab("Allele")+ scale_x_discrete(labels=unname(cats@all.names[[i]]))+ggtitle(locNames(cats)[i])+ theme(legend.position="none")
  )
}  
@

<<echo=FALSE,results='asis'>>=
tab <-nullalleles$homozygotes$probability.obs
cap_first<-"Probability of the observed number of homozygotes"
cap_cont<-"Probability of the observed number of homozygotes (continued)"


breaktab(tab, cap_first, cap_cont)

#breaktab <- function(tab, cap_first, cap_cont)
#{
#num_tables<-ceiling(dim(tab)[2]/8)
#for(i in 1:num_tables){
#  if(i==1) {
#    tempcap<-cap_first
#  } else if (i>1) {
#    tempcap<-cap_cont
#  }
#  firstcol<-1+(8*(i-1))
#  lastcol<-i*8
#  if(lastcol>dim(tab)[2]) lastcol<-dim(tab)[2]
#  tempdata2<-as.matrix(tab[,firstcol:lastcol])
#  if((lastcol-firstcol)==0) colnames(tempdata2)<-colnames(tab)[dim(tab)[2]]
#  tab2<-xtable(tempdata2,cap=tempcap, digits=3)
#  print(tab2,include.rownames=TRUE, ,add.to.row=list(list(seq(1,nrow(tab2),2)),"\\rowcolor[gray]{0.9} ")) 
#}
#}
write.csv(nullalleles$homozygotes$probability.obs, paste(cats@other$filename,"-null_allele_probability.csv", sep=""), row.names=TRUE)
@

\FloatBarrier

\subsection{Frequency of null alleles}
\subsubsection{Determined using Chakraborty et al. (1992)}
<<echo=FALSE,results='hide'>>=
method1.boot<-nullalleles$null.allele.freq$bootstrap$method1
colnames(method1.boot)<-locNames(cats)
method1.boot<-reshape2::melt(method1.boot)
method1.obs<-reshape2::melt(nullalleles$null.allele.freq$summary1[1,])
method1.obs$names <- rownames(method1.obs)
@

<<null_all_Chakraborty, echo=FALSE, fig.width=7, fig.height=4, fig.path=fig.path, dev=dev, dpi=dpi>>=

   ggplot(data=method1.boot,aes(Var2,value)) +geom_boxplot(fill="grey")+theme_classic()+geom_point(data=method1.obs,aes(names,value, colour="red"),size=3)+ylab("Frequency of null alleles")+xlab("Locus")+ scale_x_discrete(labels=unname(locNames(cats)))+ggtitle("Null allele frequency - Chakraborty et al. (1992)")+ theme(legend.position="none")+geom_hline(yintercept=0,colour="lightgrey")
@

<<echo=FALSE,results='hide'>>=
method1_summary_cap<-"Summary of null allele frequencies by locus for Chakraborty et al. (1992)"
method1_table<-xtable(nullalleles$null.allele.freq$summary1, cap=method1_summary_cap, digits=3)
print(method1_table,include.rownames=TRUE)
write.csv(nullalleles$null.allele.freq$summary2, paste(cats@other$filename,"-null_allele_freq_Chakraborty.csv", sep=""), row.names=TRUE)
@


\subsubsection{Determined using Brookfield (1996)}
<<echo=FALSE,results='hide'>>=
method2.boot<-nullalleles$null.allele.freq$bootstrap$method2
colnames(method2.boot)<-locNames(cats)
method2.boot<-reshape2::melt(method2.boot)
method2.obs<-reshape2::melt(nullalleles$null.allele.freq$summary2[1,])
method2.obs$names <- rownames(method2.obs)
@

<<null_all_Brookfield, echo=FALSE, fig.width=7, fig.height=4, fig.path=fig.path, dev=dev, dpi=dpi>>=
ggplot(data=method2.boot,aes(Var2,value)) +geom_boxplot(fill="grey")+theme_classic()+geom_point(data=method2.obs,aes(names,colour="red",size=3))+ylab("Frequency of null alleles")+xlab("Locus")+ scale_x_discrete(labels=unname(locNames(cats)))+ggtitle("Null allele frequency - Brookfield (1996)")+ theme(legend.position="none")+geom_hline(yintercept=0,colour="lightgrey")
@


<<echo=FALSE,results='asis'>>=
method2_summary_cap<-"Summary of null allele frequencies by locus for Brookfield (1996)"
method2_table<-xtable(nullalleles$null.allele.freq$summary2, cap=method2_summary_cap, digits=3)
print(method2_table,include.rownames=TRUE)
write.csv(nullalleles$null.allele.freq$summary2, paste(cats@other$filename,"-null_allele_freq_Brookfield.csv", sep=""), row.names=TRUE)
@

\FloatBarrier
<<echo=FALSE, results='hide'>>=
allresults$null.all<-list(nullalleles)
@
\FloatBarrier

