---  
title: "ExpHunterSuite: corMiT crosvalidation report"
author: "SysBioLab"
output: 
  html_document:
    toc: true
    toc_float: true
    df_print: paged
    fig_width: 12
---

<style type='text/css'>
    body .main-container {
        max-width: 90%;

    }

    .plot_real_size {
      overflow: scroll;
      max-height: 600px;
    }

    .plot_real_size img{
      max-width: none;
      max-height: none;
    }

    embed {
   
  }   
</style>
# **CoRmiT: Crossvalidation**

## Rank of the test dataset for the best strategies in train 

```{r config, echo = FALSE, warning =FALSE, message=FALSE, results = 'asis'}

mirna_names <- translate_miRNA_ids(unique(full_ranking$miRNA))
```

```{r ranking_dist, echo = FALSE, warning =FALSE, message=FALSE, results = 'asis', eval = FALSE} 

cross_rank$significant <- ifelse(cross_rank$miRNA %in% best_miRNA_strategies$miRNA, "significant", "not significant")

cross_rank$miRNA_n <- mirna_names[match(cross_rank$miRNA, mirna_names$ACCESSION), "NAME"]   

ggplot2::ggplot(cross_rank, ggplot2::aes(x = miRNA_n, y = test_rank, fill = significant)) + 

ggplot2::geom_boxplot()+
 ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 315,hjust=0,vjust=0)) +
 ggplot2::labs(y= "Rank position of test dataset")


```

## Rank of the best strategy

Ranking values of the best strategy in corMiT but using random sections of databases

```{r ranking_dist_train, echo = FALSE, warning =FALSE, message=FALSE, results = 'asis'} 

train_rank_dist$miRNA_n <- mirna_names[match(train_rank_dist$miRNA, mirna_names$ACCESSION), "NAME"]   


p_meds <- plyr::ddply(train_rank_dist, plyr::.(miRNA_n), summarise, med = median(OR_train_rank))

ggplot2::ggplot(train_rank_dist, ggplot2::aes(x = miRNA_n, y = OR_train_rank)) + 
ggplot2::geom_boxplot()+
ggplot2::geom_text(data = p_meds, ggplot2::aes(x = miRNA_n, y = med, label = med), 
              size = 3, vjust = -1.5)+


 ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 60,hjust=1,vjust=1, size = 18),
                 axis.text.y = ggplot2::element_text(size = 18), 
                 axis.title.y = ggplot2::element_text(size = 15, face = "bold")) +
 ggplot2::labs(y= "Ranking position in 75% of DB", x = NULL)+
 ggplot2::scale_y_continuous(limits = c(0, 25))



```

```{r ranking_tp_corr, echo = FALSE, warning =FALSE, message=FALSE, results = 'asis', eval = FALSE} 
	
	rank_tp <- cross_rank %>% group_by(miRNA) %>% summarise(median_rank = median(test_rank))
	rank_tp$TP <- best_miRNA_strategies[match(rank_tp$miRNA, best_miRNA_strategies$miRNA), "TP"]
	ggplot2::ggplot(rank_tp, ggplot2::aes(x = TP, y = median_rank)) +ggplot2::geom_point()+
	ggplot2::geom_label(label = miRNA) 
```

```{r ranking_scatter, echo = FALSE, warning =TRUE, message=TRUE, results = 'asis'} 

plot_objects <- list()
full_ranking <- full_ranking[full_ranking$miRNA %in% best_miRNA_strategies$miRNA,]
full_ranking$miRNA_n <- mirna_names[match(full_ranking$miRNA, mirna_names$ACCESSION), "NAME"] 
miRNA_cor_results$miRNA_cont_tables$miRNA_n  <- mirna_names[match(miRNA_cor_results$miRNA_cont_tables$miRNA, mirna_names$ACCESSION), "NAME"] 
train_rank_dist2 <- data.frame()
for (miRNA in unique(full_ranking$miRNA_n)) {
   miRNA_ct <- miRNA_cor_results$miRNA_cont_tables[miRNA_cor_results$miRNA_cont_tables$miRNA_n == miRNA,]
    miRNA_ct <- miRNA_ct[miRNA_ct$db_group == "multimir",]
    miRNA_ct$full_db_rank <- rank(-miRNA_ct$Odds_ratio, ties.method ="min")
    
    miRNA_rank <- full_ranking[full_ranking$miRNA_n == miRNA,]
    full_and_sample_eq <- match(paste0(miRNA_rank$strategy, miRNA_rank$corr_cutoff),
                                paste0(miRNA_ct$strategy, miRNA_ct$corr_cutoff))
    miRNA_rank$full_db_rank <- miRNA_ct$full_db_rank[full_and_sample_eq]
    miRNA_rank$Pvalue <- miRNA_ct$Pvalue[full_and_sample_eq]

    # miRNA_rank <- miRNA_rank[miRNA_rank$Pvalue <= 0.05,]
    
    top_miRNA_rank <- miRNA_rank[miRNA_rank$full_db_rank == 1 & !is.na(miRNA_rank$full_db_rank),]
   
    train_rank_dist2 <- rbind(train_rank_dist2,top_miRNA_rank)
    pp <- ggplot2::ggplot(miRNA_rank, ggplot2::aes(y = OR_train_rank, x = full_db_rank))+
    ggplot2::geom_jitter(size = 0.5,width = 0.2, height = 0.2, alpha = 0.3) + 
    ggplot2::geom_boxplot(data = top_miRNA_rank, mapping= ggplot2::aes(y = OR_train_rank, x = 0))+
    ggplot2::scale_y_reverse()+
    ggplot2::scale_x_reverse()+
    ggplot2::ggtitle(miRNA)+
    ggplot2::labs(y = NULL, x = NULL)+
    ggplot2::geom_vline(xintercept  = 1.5, linetype="dashed")
        plot_objects[[miRNA]] <- pp
}
# save(train_rank_dist, file = "/mnt/scratch/users/bio_267_uma/josecordoba/NGS_projects/LaforaRNAseq/analysis/target_wf_pearson/ctrl_vs_mut_deff/coRmiT.R_0000/test2.Rdata")
gridExtra::grid.arrange(grobs = plot_objects, ncol = 3, nrow = ceiling(length(plot_objects)/3),
  left = "Ranking position in 75% of DB", bottom = "Ranking position in 100% of DB")
```


## Rank of the best strategy 2

Ranking values of the best strategy in corMiT but using random sections of databases

```{r ranking_dist_train2, echo = FALSE, warning =FALSE, message=FALSE, results = 'asis'} 

train_rank_dist2$miRNA_n <- mirna_names[match(train_rank_dist2$miRNA, mirna_names$ACCESSION), "NAME"]   

ggplot2::ggplot(train_rank_dist2, ggplot2::aes(x = miRNA_n, y = OR_train_rank)) + 
ggplot2::geom_boxplot()+


 ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 60,hjust=1,vjust=1, size = 18),
                 axis.text.y = ggplot2::element_text(size = 18), 
                 axis.title.y = ggplot2::element_text(size = 15, face = "bold")) +
 ggplot2::labs(y= "Ranking position in 75% of DB", x = NULL)+
 ggplot2::scale_y_continuous(limits = c(0, 25))



```