% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% SIGNAL DENSITIES
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<% signalDensities <- function(fit,  fields=c("CT", "betaN", "betaT", "rho", "c1", "c2", "betaTN", "rhoN", "c1N", "c2N"), ...) { %>
<%
# Argument 'fields':
fields <- match.arg(fields, several.ok=TRUE);
%>

\clearpage
\section{Signal densities}
\label{sec:SignalDensities}
<%
require("ggplot2") || throw("Package not loaded: ggplot2");
data <- getLocusData(fit, fields="full");
unknown <- setdiff(fields, names(data));
if (length(unknown) > 0L) {
  throw("Unknown locus data fields: ", paste(sQuote(unknown), collapse=", "));
}
params <- fit$params;

chromosomes <- getChromosomes(fit);
nbrOfChromosomes <- length(chromosomes);
chromosomesH <- seqToHumanReadable(chromosomes);
chromosomesH <- if (nbrOfChromosomes == 1) {
  sprintf("Chr %s", chromosomesH);
} else {
  sprintf("Chrs %s", chromosomesH);
}
chrsTags <- if (nbrOfChromosomes == 1) {
  sprintf("chr%02d", chromosomes[1]);
} else {
  sprintf("chrs%02d-%02d", min(chromosomes), max(chromosomes));
}
%>
<% if (nrow(data) >= 2L) { %>
\begin{figure}[htbp]
 \begin{center}
<% for (ff in fields) { %>
<%
  symbol <- symbolList[[ff]];
  panelTitle <- switch(ff, CT="\\TCN", betaN="\\BAFN", betaT="\\BAFT", betaTN="\\BAFTN", "NNN");
  lim <- switch(ff, CT=ClimX, c1=ClimX, c2=ClimX, c1N=ClimX, c2N=ClimX, BlimX);
%>
\raisebox{4ex}{<%=symbol%>}
<% for (by in c("all", "muN")) { %>
<%
  tags <- c(chrsTags, by, ff);
%>
    \resizebox{0.33\textwidth}{!}{%
      \includegraphics{<%={
        toPNG(name=sampleNameEscDots, tags=tags, width=640, aspectRatio=0.3, {
          gg <- ggplot(data, aes_string(x=ff, y="..count.."));

          if (by == "all") {
            if (ff == "CT" && any(!data$isSNP, na.rm=TRUE)) {
              gg <- gg + aes(group=type, colour=type);
            }

            h <- NULL;
            if (ff == "rho") {
              h <- params$deltaAB;
              label <- expression(Delta[AB]);
              col <- "orange";
            } else if (ff == "c1") {
              h <- params$deltaLowC1;
              label <- expression(Delta[LOH]);
              col <- "blue";
            }

            if (!is.null(h)) {
              gg <- gg + geom_vline(xintercept=h, colour=col, size=1.5);
            }
          } else if (by == "muN") {
            gg <- gg + aes_string(group="muNx", colour="muNx");
            gg <- gg + scale_color_manual(values=muNCols);
          }
 
          # See https://github.com/hadley/ggplot2/wiki/Legend-Attributes
          gg <- gg + theme(legend.position=c(0.97, 0.75),
                          legend.justification = 1,
                          legend.direction="vertical",
                          legend.title=element_text(size=0),
                          legend.text=element_text(size=16));
 
          gg <- gg + geom_density(size=2, na.rm=TRUE);
          gg <- gg + xlab(NULL);
          gg <- gg + ylab(NULL);
          gg <- gg + xlim(lim);
          gg <- gg + theme(axis.text.x=xnbrOpts, axis.text.y=ynbrOpts);
          gg <- gg + theme(axis.title.x=xlabOpts, axis.title.y=ylabOpts);
          suppressWarnings({
            print(gg);
          });
        }, force=FALSE);
      }%>}%
    }%
<% } # for (by ...) %>
\\ %
<% } # for (ff ...) %>
 \end{center}
 \caption{
  Density estimates of \emph{locus-level} signals on <%=chromosomesH%>
  with and without TumorBoost normalization.
  The signals in the left and right panels are without and with stratification on genotype calls (AA and BB in gray and AB in black).
   If there are no allelic imbalances in the tumor, then the density of 
   the tumor should be similar to a normal sample, that is, three bands
   of signals corresponding to the three genotype groups.
 }
 \label{fig:SignalDensity}
\end{figure}
<% } else { %>
NOTE: No density plots produced for <%= chromosomesH %> because there are fewer than two loci.
<% } ## if (nrow(data) >= 2L) %>
<% } # signalDensities() %>
