<%--------------------------------------------------------------------
  This is an RSP LaTeX template for the report system of PSCBS.
--------------------------------------------------------------------%>
<%@meta author="Henrik Bengtsson"%>
<%@meta date="2013-03-21"%>

<%--------------------------------------------------------------------
Include RSP templates
--------------------------------------------------------------------%>
<%@include file="includes/reportHeader.tex.rsp"%>

<%
library("PSCBS");
library("R.devices");
library("R.cache");
getChecksum <- R.cache::getChecksum;
stopifnot(exists("rspArgs", mode="list"));
%>


<%--------------------------------------------------------------------
Report configuration
--------------------------------------------------------------------%>
<%@logical SIGNAL_DENSITIES="${PSCBS::reports/signalDensities}" default="TRUE"%>
<%@logical LEVEL_DENSITIES="${PSCBS::reports/levelDensities}" default="TRUE"%>
<%@logical C1C2="${PSCBS::reports/pscnSegmentationTransitions}" default="FALSE"%>

<%-- To implement --%>
<%@logical PER_GENOTYPE="${PSCBS::reports/perGenotype}" default="TRUE"%>


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LATEX STARTUP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[twoside,12pt]{article}
\usepackage{fancyvrb} % Custom verbose environments
\usepackage{lastpage} % Total number of pages
\usepackage{xspace}
\usepackage{subfigure}  % \subfigure[<title>]{}
\usepackage[round]{natbib}

% No paragraph indentation
\setlength{\parindent}{0cm}

\addtolength{\oddsidemargin}{-0.4in}
\addtolength{\evensidemargin}{-1.02in}
\addtolength{\textwidth}{1.5in}
\addtolength{\topmargin}{-1.0in}
\addtolength{\textheight}{1.5in}

\renewcommand{\topfraction}{1.00}   % max fraction of floats at top
\renewcommand{\bottomfraction}{1.0} % max fraction of floats at bottom
\renewcommand{\textfraction}{0.00}

<%@include file="includes/reportSetupGraphics.tex.rsp"%>
<%@include file="includes/reportSetupMacros.tex.rsp"%>


<% pairedPSCBSReport <- function(fit, sampleName=sampleName(fit), dataSet=NULL, studyName=NULL, chromosomes=NULL, Clim=c(0,4), Blim=c(0,1), reportPerChromosome=TRUE, ...) { %>

<%@if name="SIGNAL_DENSITIES"%>
 <%@include file="includes/signalDensities.tex.rsp"%>
<%@endif%>
<%@include file="includes/summaryOfAnnotationAndGenotypeCalls.tex.rsp"%>
<%@include file="includes/pscnSegmentationTracks.tex.rsp"%>
<%@if name="LEVEL_DENSITIES"%>
 <%@include file="includes/levelDensities.tex.rsp"%>
<%@endif%>
<%@include file="includes/pscnSegmentationTransitions.tex.rsp"%>

<%
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Validate arguments
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Argument 'fit':
fit <- Arguments$getInstanceOf(fit, "PairedPSCBS");
fitClass <- class(fit)[1L];
if (inherits(fit, "NonPairedPSCBS")) {
  fitClassLbl <- "Non-paired PSCBS";
} else if (inherits(fit, "PairedPSCBS")) {
  fitClassLbl <- "Paired PSCBS";
} else if (inherits(fit, "CBS")) {
  fitClassLbl <- "CBS";
} else {
  fitClassLbl <- fitClass;
}

# Argument 'sampleName':
stopifnot(is.character(sampleName));
sampleName <- Arguments$getCharacter(sampleName);
sampleNameEscDots <- gsub(".", "_", sampleName, fixed=TRUE);

# Argument 'dataSet':
if (!is.null(dataSet)) {
  dataSet <- Arguments$getCharacter(dataSet);
} else {
  dataSet <- "?";
}
dataSetLbl <- sprintf("Data set: %s\\\\", toLatex(dataSet));

# Argument 'studyName':
if (is.null(studyName)) studyName <- fitClassLbl;
studyName <- Arguments$getCharacter(studyName);
studyLbl <- sprintf("Study: %s\\\\", toLatex(studyName));

# Argument 'chromosomes':
if (!is.null(chromosomes)) {
  fit <- extractChromosomes(fit, chromosomes);
}
%>

<%reportHeaderSetup(fit, sampleName=sampleName, dataSet=dataSet, studyName=studyName) %>
<%reportHeaderUpdate(fit)%>

<%
ClimX <- Clim + c(-1,1)*diff(Clim)*0.08;
BlimX <- Blim + c(-1,1)*diff(Blim)*0.08;
%>

\begin{document}
\title{<%=fitClassLbl%> Report:\\<%=studyLbl%><%=dataSetLbl%>Sample: <%=toLatex(sampleName)%>}
\author{Report template by <%@meta name="author"%>}
\maketitle
\thispagestyle{fancy}

%%\tableofcontents

%>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DATA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% METHODS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
\label{sec:Introduction}
This is a quality control (QC) report based on non-paired tumor SNP microarray data.

\subsection{Method}
\label{sec:Method}
We use a non-paired PSCBS segmentation method to partion the genome into segments such that all signals in a particular segment are likely to originate from the same underlying parent-specific copy-number state.  This method is adopted from the Paired PSCBS segmentation method~\citep{OlshenA_etal_2011} with two main differences.
First, heterozygous SNPs are "called" based on tumor allele B fractions ("tumor BAFs"), which are poor proxies for the corresponding normal ones.  This is a major disadvantage compared to having a matched normal.
Second, the tumor BAFs cannot be normalized using the TumorBoost method~\citep{BengtssonH_etal_2010}.  


<%--
\subsection{Post-segmentation pruning}
\label{sec:PostSegmentationPruning}
<%
fitP <- pruneByHClust(fit, h=0.25);
#print(fitP);
%>
--%>

\clearpage


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RESULTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% WHOLE-GENOME RESULTS
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
\section{Whole-Genome Results}
\label{ch:WholeGenomeResults}

<%=summaryOfAnnotationAndGenotypeCalls(fit)%>
<%@if name="SIGNAL_DENSITIES"%>
<%=signalDensities(fit, fields=c("CT", "betaT", "rho", "c1", "c2"))%>
<%@endif%>
<%=pscnSegmentationTracks(fit)%>
<%@if name="LEVEL_DENSITIES"%>
<%=levelDensities(fit, fields=c("tcn", "dh", "c1", "c2"))%>
<%@endif%>
<%@if name="C1C2"%>
<%=pscnSegmentationTransitions(fit)%>
<%@endif%>


% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% PER CHROMOSOME SEGMENTATION
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
<% if (reportPerChromosome && nbrOfChromosomes(fit) > 1) { %>
<% for (chr in getChromosomes(fit)) { %>
<%
fitT <- extractChromosome(fit, chromosome=chr);
%>
\clearpage
\section{Chromosome <%=chr%>}
\label{sec:Chromosome<%=chr%>Results}
<%reportHeaderUpdate(fit)%>
<%=summaryOfAnnotationAndGenotypeCalls(fitT)%>
<%@if name="SIGNAL_DENSITIES"%>
<%=signalDensities(fitT, fields=c("CT", "betaT", "rho", "c1", "c2"))%>
<%@endif%>
<%=pscnSegmentationTracks(fitT)%>
<%---
<%@if name="LEVEL_DENSITIES"%>
<%=levelDensities(fitT, fields=c("tcn", "dh", "c1", "c2"))%>
<%@endif%>
---%>
<%@if name="C1C2"%>
<%=pscnSegmentationTransitions(fitT)%>
<%@endif%>
<% } # for (chr ...) %>
<% } # if (reportPerChromosome && nbrOfChromosomes(fit) > 1) %>


<%--
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% PRUNED: WHOLE-GENOME SEGMENTATION
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
\subsection{Hierarchical pruning}
\label{sec:HierarchicalPruning}

% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% PRUNED: PER CHROMOSOME SEGMENTATION
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
--%>


\clearpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% REFERENCES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
<%@include file="includes/references.tex.rsp"%>


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% APPENDIX
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\appendix
\section{Appendix}
<%@include file="includes/sessionInfo.tex.rsp"%>

\end{document}

<% } # pairedPSCBSReport() %>

<%---
<%
message(paste(capture.output({ str(rspArgs) }), collapse="\n"));
message(paste(capture.output({ ll() }), collapse="\n"));
%>
---%>
<%=do.call("pairedPSCBSReport", args=rspArgs)%>


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HISTORY:
# 2012-11-03
# o Replaced deprecated ggplot2 functions.
# 2012-09-16
# o Added easy report configuration at the very top.
# 2012-05-30
# o Removed ggplot2 warnings on missing values.
# 2012-02-28
# o Now it is possible to turn off usage of the alpha channel in
#   plots, e.g. setOption("PSCBS::report/useAlphaChannel", FALSE).
#   This is useful for if the alpha channel is not supported.
% 2012-02-27
% o First successful run with real data.
% o Now all of the report uses a PairedPSCBS object.
% o Now making more use of templates.
% o Now passing a data frame to segmentByPairedCBS().
% 2011-09-30
% o Created.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
