## **Data quality control (QC)**

### **Correlation between samples:**

Here we show scatterplots comparing expression levels for all genes between the different samples, for i) all controls, ii) all treatment samples and iii) for all samples together.

These plots will only be produced when the total number of samples to compare within a group is less than or equal to 10.

```{r corr_plot_control, echo = FALSE, fig.height=12, fig.width=12, eval=length(index_control_cols) <= 10, results='asis', warning = FALSE}

cat("#### **Correlation between control samples:**
Replicates within the same group tend to have Pearson correlation coefficients >= 0.96. Lower values may indicate problems with the samples.
")

PerformanceAnalytics::chart.Correlation(raw_filter[index_control_cols], histogram=TRUE, pch=19, log="xy")

```

```{r corr_plot_treatment, echo = FALSE, fig.height=12, fig.width=12, eval=length(index_treatmn_cols) <= 10, results='asis', warning = FALSE}

cat("#### **Correlation between treatment samples:**
Replicates within the same group tend to have Pearson correlation coefficients >= 0.96. Lower values may indicate problems with the samples.
")

PerformanceAnalytics::chart.Correlation(raw_filter[index_treatmn_cols], histogram=TRUE, pch=19, log="xy")


```


```{r corr_plot_all , echo = FALSE, fig.height=12, fig.width=12, eval=length(index_control_cols) + length(index_treatmn_cols) <= 10, results='asis', warning = FALSE}


cat("#### **Correlation between samples: All vs all replicates**

Correlation coefficients tend to be slightly higher between replicates from the same group than between replicates from different groups. If this is not the case, it may indicate mislabelling or other potential issues.

")

PerformanceAnalytics::chart.Correlation(raw_filter, histogram=TRUE, pch=19, log="xy")

```

### **Heatmap and clustering showing correlation between replicates**
**BROWN: higher correlation; YELLOW: lower**

```{r heatmap_corr, eval=!"externalDEA" %in% names(all_counts_for_plotting), echo = FALSE, fig.height=12, fig.width=12}
col<- colorRampPalette(c("#FFFFCC", "#FD8D3C", "#800026"))(20)
res <- cor(raw_filter)
gplots::heatmap.2(x = res, col = col, symm = TRUE, margins = rep(max(nchar(colnames(res))), 2), scale = "none", trace = "none")
```

```{r config_PCA, echo = FALSE, results = "asis", warning = TRUE, message = TRUE}
pca_data <- PCA_res$all_genes$pca_data
dim_to_keep <- PCA_res$all_genes$dim_to_keep
dim_data <- PCA_res$all_genes$dim_data
dim_data_merged <- PCA_res$all_genes$dim_data_merged

```

```{r child=file.path(template_folder,"facto_miner.Rmd")}

```



### **Visualizing normalization results**

These boxplots show the distributions of count data before and after normalization (shown for normalization method `r names(all_data_normalized)[1]`):

#### Representation of cpm unfiltered data:

```{r density_distribution_cpm, echo = FALSE, warning=FALSE}
cpm_no_zeros <- cpm_table[! rowSums(cpm_table) == 0, ]
log2cpm <- log2(cpm_no_zeros + 1)
cpm_long <- reshape2::melt(log2cpm, measure.vars=colnames(log2cpm))

ggplot2::ggplot(cpm_long, ggplot2::aes(x = value, colour=Var2)) + ggplot2::geom_density() +
ggplot2::xlab(expression(log[2](cpm + 1))) +
ggplot2::geom_vline(xintercept = log2( opt$reads + 1))

```

#### Before normalization:

```{r density_distribution_raw, echo = FALSE, warning=FALSE}
log2raw_counts <- log2(raw_filter + 1)
raw_long <- reshape2::melt(log2raw_counts, measure.vars=colnames(log2raw_counts))

ggplot2::ggplot(raw_long, ggplot2::aes(x = value, colour=variable)) + ggplot2::geom_density() +
ggplot2::xlab(expression(log[2](count + 1)))

```

#### After normalization:

```{r density_distribution_norm, echo = FALSE, warning=FALSE}

log2norm_counts <- log2(all_data_normalized[["default"]] + 1)
norm_long <- reshape2::melt(log2norm_counts, measure.vars=colnames(log2norm_counts))

ggplot2::ggplot(norm_long, ggplot2::aes(x = value, colour=variable)) + ggplot2::geom_density() +
ggplot2::xlab(expression(log[2](count + 1)))

```

### **Count metrics by sample ranks**

```{r coverage_plot_head, echo = FALSE, warning = FALSE, results='asis'}
	if (!is.null(library_sizes)) {
		cat('#### **Sample rank versus total reads in raw FASTQ**')
	}else{
		cat('#### **Sample rank versus total counts**')
	}
```

```{r coverage_plot, echo = FALSE, warning = FALSE}
    if (!is.null(library_sizes)) {
        xtitle="Sample Rank (total reads)"
    } else {
        xtitle="Sample Rank (total counts)"
    }
    ggplot2::ggplot(coverage_df, ggplot2::aes(x = sample_rank, y = total_counts)) +
        ggplot2::geom_point(size = 3) +
        cowplot::theme_cowplot() +
        cowplot::background_grid() +
        ggplot2::labs(title = "Read Counts", x=xtitle, y = "Reads Counted") +
        ggplot2::ylim(c(0,NA)) +
        ggplot2::scale_color_brewer(palette="Dark2")
```
```{r coverage_plot_text, echo = FALSE, warning = FALSE, results='asis'}
	if (!is.null(library_sizes)) {
		cat('\n\n Sample rank is the position a sample holds after sorting by total reads')
	}else{
		cat('\n\n Sample rank is the position a sample holds after sorting by total counts')
	}
```

```{r coverage_plot_frac_head, eval = !is.null(coverage_df$counted_frac),  echo = FALSE, warning = FALSE, results='asis'}
	cat('#### **Fraction counts read sorted by rank**')
```

```{r coverage_plot_frac, eval = !is.null(coverage_df$counted_frac),  echo = FALSE, warning = FALSE}
    ggplot2::ggplot(coverage_df, ggplot2::aes(x = sample_rank, y = counted_frac)) +
        ggplot2::geom_point(size = 3) +
        cowplot::theme_cowplot() +
        cowplot::background_grid() +
        ggplot2::labs(title = "Read Counts", x="Sample Rank (total reads)", y = "% counts read") +
        ggplot2::ylim(c(0,NA)) +
        ggplot2::scale_color_brewer(palette="Dark2")
```

```{r coverage_plot_frac_text, eval = !is.null(coverage_df$counted_frac),  echo = FALSE, warning = FALSE, results='asis'}
	cat('Counted fraction is the percentage of reads that map to a gene (only available if total reads provided)')
```

### **Statistics of expressed genes**

```{r exp_genes_plot, echo = FALSE, warning = FALSE}
		colors <- c("Unique" = "blue", "Union" = "red", "Intersection" = "green")
		ggplot2::ggplot(data = exp_genes_df, ggplot2::aes(x = count_rank)) +
		    ggplot2::geom_line(ggplot2::aes(y = expressed_genes, color = "Unique")) +
		    ggplot2::geom_line(ggplot2::aes(y = union_expressed_genes, color = "Union")) +
		    ggplot2::geom_line(ggplot2::aes(y = inters_expressed_genes, color = "Intersection")) +
		    ggplot2::labs(x = "Sample rank",
		                  y = "Number of genes",
		                  color = "Legend") +
		    ggplot2::expand_limits(y=0) +
		    ggplot2::scale_color_manual(values = colors)
```

Samples are ranked by total expressed genes. Union of expressed genes represents the cumulative total expressed genes (sum of
all genes expressed in any sample up to current sample, expected to increase with sample rank). Intersection of expressed genes
represents the cumulative intersection of expressed genes (sum of genes expressed in all samples up to current sample, expected
to decrease with sample rank).

#### **Mean count distribution by filter**

```{r mean_counts_plot, echo = FALSE, warning = FALSE}
    ggplot2::ggplot(mean_counts_df, ggplot2::aes(x = counts, col = filter)) +
	ggplot2::geom_density(ggplot2::aes(y=..count..)) +
	ggplot2::scale_x_log10() +
	ggplot2::labs(x = "Mean counts per gene", y = "Frequency") +
    ggplot2::guides(col = ggplot2::guide_legend(title = NULL)) +
    ggplot2::scale_color_brewer(palette = "Paired") +
    cowplot::theme_cowplot() +
    ggplot2::theme(legend.position = "top",
          legend.justification="center",
          legend.background = ggplot2::element_rect(color = NA))
```

This plot represents the mean counts distribution per gene, classified by filters

### Gene counts variance distribution

Variance of gene counts across samples are represented. Genes with lower variance than selected threshold (dashed grey line) were filtered out. 

```{r variance_dist, echo = FALSE, eval=!grepl("F", opt$modules), warning = FALSE, message = FALSE}

gene_count_var <- data.frame(variance =var_filter[["variance_dis"]])

ggplot2::ggplot(gene_count_var, ggplot2::aes(x = variance))+
ggplot2::geom_density() + 
ggplot2::geom_vline(xintercept = var_filter[["thr"]], linetype="dashed", 
                color = "grey")
```

```{r all_norm_head, echo=FALSE, results='asis'}

cat("### **Samples differences by all counts normalized**:
		
All counts were normalizated by default (see options below) algorithm. This count were scaled by log10 and plotted in a heatmap.")

```

```{r heatmap_all_norm_plot), echo = FALSE, fig.height=12, fig.width=12}
	
		deseq_normalized_data <- all_data_normalized[['default']]
	if (nrow(all_data_normalized[['default']]) <= 50000) { 
		deseq_normalized_data_mtx <- log10(data.matrix(deseq_normalized_data) + 1)

		plot <- gplots::heatmap.2(deseq_normalized_data_mtx, col = col, labRow = FALSE, dendrogram = "column", trace = "none", srtCol=45,  adjCol = c(1,1))
	} else {
		cat("\n\nThis plot can not be generated because more than 50 000 genes have passed the filters and it can cause memory problems.\n\n")
	}

```

```{r top_n_expression, results='asis', echo=FALSE, warning=FALSE, message=FALSE}
	cat("\n\n### **Percentages of reads per sample mapping to the most highly expressed genes**\n\n")
	make_top_n_expression_table(count_data=raw_filter, n=5)

```



