<commit_before>---
output:
    knitrBootstrap::bootstrap_document:
        theme: readable
        highlight: zenburn
        theme.chooser: TRUE
        highlight.chooser: TRUE
    html_document:
        toc: true
        highlight: zenburn

---
```{r custom-code}
source("~/repos/myRfunctions/transactions.R")
dn <- "slack_mirna-mrna/"
path_files = "~/repos/pipelines/slack_cel_network/network"
```


```{r setup, echo=FALSE, eval=FALSE}
knitr::opts_chunk$set(tidy=TRUE, highlight=TRUE, dev="png",
                      cache=FALSE, highlight=TRUE, autodep=TRUE, warning=FALSE, error=FALSE,
                      eval=TRUE,
                      message=FALSE, prompt=TRUE, comment='', fig.cap='', bootstrap.show.code=FALSE)
render_2_drop("~/repos/pipelines/slack_cel_network/network/interaction.rmd",dn)

```
# Introduction

The main idea is to find miRNA that correlated to genes that have been found deregulated in RNAseq data. 
Currently using targetscanworm for the miRNA-gene pairs matrix.
miRNA-mRNA interactions with (negative) correlation < -.7 and FDR<0.1 are considered.

# Load data


```{r load}
suppressMessages(library(DESeq2,quietly=TRUE))
library(ggplot2)
library(dplyr)
library(tidyr)

mirna<-read.table("~/repos/pipelines/slack_cel_network/reports/new_smallrna/log_matrix_perfect.txt",header=T,row.names=1)
row.names(mirna)<-sub(".ref.0.0.0.0","",row.names(mirna))


mrna<-read.table("~/repos/pipelines/slack_cel_network/reports/rlog_mat.txt",header=T,row.names=1)
names(mrna) = c("N2_1_Day0","N2_1_Day5","N2_2_Day0","N2_2_Day5","N2_3_Day0","N2_3_Day5",     "mir71_1_Day0", "mir71_1_Day5","mir71_2_Day0","mir71_2_Day5","mir71_3_Day0","mir71_3_Day5")
#tf<-read.table("tf.list2",sep=" ")
#chea<-read.table("chea-database",header=F,sep=",")

n2_labels = c("N2_1_Day0","N2_1_Day5","N2_2_Day0","N2_2_Day5","N2_3_Day0","N2_3_Day5")
ko_labels = c("mir71_1_Day0","mir71_1_Day5","mir71_2_Day0","mir71_2_Day5","mir71_3_Day0","mir71_3_Day5")

```


Load target~miRNA (targetscanWorm) table and deregulated genes (from RNAseq data)
```{r load-targets}
gene_map = read.table("~/orch/scratch/celegans_network/celegans_network_raw/data/gene_map.dat",as.is=c(TRUE,TRUE))
ago = gene_map %>%filter(grepl("ago",V3))
target<-read.table("~/orch/scratch/celegans_network/celegans_network_raw/data/targets.csv",as.is=c(TRUE,TRUE))
target_tf<-read.table("~/orch/scratch/celegans_network/celegans_network_raw/data/aging_master_table.txt",as.is=c(TRUE,TRUE))
```


# Methods
Create function to detect negative correlation and get pvalue.
Very basic method: pearson correlation.
```{r cor-functions}
getcor_gene<-function(g,logmrna="",logmirna=""){
  #print(g)
  mir<-target[target[,2]==g,1]
  mir<-intersect(mir,row.names(logmirna))
  if (length(mir)>0){
  gexp=as.numeric(logmrna[g,])
  #print(mir)
  corlist<-do.call("rbind",(lapply(mir,function(m){
    mexp<-unlist(logmirna[m,])
    #print(mexp)
    #print(gexp)
    c<-cor.test(gexp,mexp,method="pearson")
    return(data.frame(rho=c$estimate,pvalue=c$p.value))
    })))
  #print(corlist)
  return(cbind(data.frame(g=g,mir=mir), corlist))
  }else{
    return(data.frame(g=g,mir="mir",rho=NA,pvalue=NA))
  }
}


getcor_mirna<-function(mir,logmrna="",logmirna=""){
  #print(g)
  genes<-target[target[,1]==mir,2]
  genes<-intersect(genes,row.names(logmrna))
  if (length(genes)>0){
  mexp=as.numeric(logmirna[mir,])
  #print(mir)
  corlist<-do.call("rbind",(lapply(genes,function(g){
    gexp<-unlist(logmrna[g,])
    #print(mexp)
    #print(gexp)
    c<-cor.test(gexp,mexp,method="pearson")
    return(data.frame(rho=c$estimate,pvalue=c$p.value))
    })))
  #print(corlist)
  return(cbind(data.frame(g=genes,mir=mir), corlist))
  }else{
    return(data.frame(g="genes",mir=mir,rho=NA,pvalue=NA))
  }
}
#testing 
#getcor("WBGene00000012",mrna[, n2_labels],mirna[, n2_labels])
```


```{r cor-wrapper}
getTargets = function(de,genes,micros,fn_cor){
    res<-do.call("rbind",lapply(de,fn_cor,logmrna=genes,logmirna=micros))
    res = res %>% filter(!is.na(rho) & rho < 0) %>% mutate(adj=p.adjust(pvalue,method="BH"))
    #hist(res$pvalue)
    res[order(res$rho,res$pvalue),]
}
```

apply function to data


```{r n2-cor, cache=FALSE}
n2_de<-read.table("~/repos/pipelines/slack_cel_network/reports/n2_de_genes.txt",as.is=TRUE)
n2_deg_mirna<-read.table("~/repos/pipelines/slack_cel_network/reports/new_smallrna/mirna_n2_de_genes_perfect.txt",header=T,row.names=1)
row.names(n2_deg_mirna)<-sub(".ref.0.0.0.0","",row.names(n2_deg_mirna))
n2_res = getTargets(as.character(n2_de$id), mrna[, n2_labels], mirna[, n2_labels],getcor_gene)
n2_mir_res = getTargets(as.character(row.names(n2_deg_mirna)), mrna[, n2_labels], mirna[, n2_labels],getcor_mirna)
n2_both_res = getTargets(as.character(row.names(n2_deg_mirna)), mrna[n2_de$id, n2_labels], mirna[, n2_labels],getcor_mirna)
n2_res_sign = n2_res %>% filter(adj<0.1 & rho< -.7)
n2_mir_res_sign = n2_mir_res %>% filter(adj<0.1 & rho< -.7)
n2_both_res_sign = n2_both_res %>% filter(adj<0.1 & rho< -.7)

ko_de<-read.table("~/repos/pipelines/slack_cel_network/reports/ko_de_genes.txt",as.is=TRUE)
ko_deg_mirna<-read.table("~/repos/pipelines/slack_cel_network/reports/new_smallrna/mirna_ko_de_genes_perfect.txt",header=T,row.names=1)
row.names(ko_deg_mirna)<-sub(".ref.0.0.0.0","",row.names(ko_deg_mirna))
ko_res = getTargets(as.character(ko_de$id), mrna[, ko_labels], mirna[, ko_labels],getcor_gene)
ko_mir_res = getTargets(as.character(row.names(ko_deg_mirna)), mrna[, n2_labels], mirna[, n2_labels],getcor_mirna)
ko_both_res = getTargets(as.character(row.names(ko_deg_mirna)), mrna[ko_de$id, ko_labels], mirna[, ko_labels],getcor_mirna)
ko_res_sign = ko_res %>% filter(adj<0.1 & rho< -.7)
ko_mir_res_sign = ko_mir_res %>% filter(adj<0.1 & rho< -.7)
ko_both_res_sign = ko_both_res %>% filter(adj<0.1 & rho< -.7)


```


# Results 

```{r interaction-tables}
fn_n2 = paste0(path_files,"/n2_interaction.txt")
write.table(n2_both_res_sign, fn_n2)
fn_ko = paste0(path_files,"/ko_interaction.txt")
write.table(ko_both_res_sign, fn_ko)
```

[n2 interactions](`r get_report_links(fn_n2)`)
<commit_msg>fix correct groups<commit_after>---
output:
    knitrBootstrap::bootstrap_document:
        theme: readable
        highlight: zenburn
        theme.chooser: TRUE
        highlight.chooser: TRUE
    html_document:
        toc: true
        highlight: zenburn

---
```{r custom-code}
source("~/repos/myRfunctions/transactions.R")
dn <- "slack_mirna-mrna/"
path_files = "~/repos/pipelines/slack_cel_network/network"
```


```{r setup, echo=FALSE, eval=FALSE}
knitr::opts_chunk$set(tidy=TRUE, highlight=TRUE, dev="png",
                      cache=FALSE, highlight=TRUE, autodep=TRUE, warning=FALSE, error=FALSE,
                      eval=TRUE,
                      message=FALSE, prompt=TRUE, comment='', fig.cap='', bootstrap.show.code=FALSE)
render_2_drop("~/repos/pipelines/slack_cel_network/network/interaction.rmd",dn)

```
# Introduction

The main idea is to find miRNA that correlated to genes that have been found deregulated in RNAseq data. 
Currently using targetscanworm for the miRNA-gene pairs matrix.
miRNA-mRNA interactions with (negative) correlation < -.7 and FDR<0.1 are considered.

# Load data


```{r load}
suppressMessages(library(DESeq2,quietly=TRUE))
library(ggplot2)
library(dplyr)
library(tidyr)

mirna<-read.table("~/repos/pipelines/slack_cel_network/reports/new_smallrna/log_matrix_perfect.txt",header=T,row.names=1)
row.names(mirna)<-sub(".ref.0.0.0.0","",row.names(mirna))


mrna<-read.table("~/repos/pipelines/slack_cel_network/reports/rlog_mat.txt",header=T,row.names=1)
names(mrna) = c("N2_1_Day0","N2_1_Day5","N2_2_Day0","N2_2_Day5","N2_3_Day0","N2_3_Day5",     "mir71_1_Day0", "mir71_1_Day5","mir71_2_Day0","mir71_2_Day5","mir71_3_Day0","mir71_3_Day5")
#tf<-read.table("tf.list2",sep=" ")
#chea<-read.table("chea-database",header=F,sep=",")

n2_labels = c("N2_1_Day0","N2_1_Day5","N2_2_Day0","N2_2_Day5","N2_3_Day0","N2_3_Day5")
ko_labels = c("mir71_1_Day0","mir71_1_Day5","mir71_2_Day0","mir71_2_Day5","mir71_3_Day0","mir71_3_Day5")

```


Load target~miRNA (targetscanWorm) table and deregulated genes (from RNAseq data)
```{r load-targets}
gene_map = read.table("~/orch/scratch/celegans_network/celegans_network_raw/data/gene_map.dat",as.is=c(TRUE,TRUE))
ago = gene_map %>%filter(grepl("ago",V3))
target<-read.table("~/orch/scratch/celegans_network/celegans_network_raw/data/targets.csv",as.is=c(TRUE,TRUE))
target_tf<-read.table("~/orch/scratch/celegans_network/celegans_network_raw/data/aging_master_table.txt",as.is=c(TRUE,TRUE))
```


# Methods
Create function to detect negative correlation and get pvalue.
Very basic method: pearson correlation.
```{r cor-functions}
getcor_gene<-function(g,logmrna="",logmirna=""){
  #print(g)
  mir<-target[target[,2]==g,1]
  mir<-intersect(mir,row.names(logmirna))
  if (length(mir)>0){
  gexp=as.numeric(logmrna[g,])
  #print(mir)
  corlist<-do.call("rbind",(lapply(mir,function(m){
    mexp<-unlist(logmirna[m,])
    #print(mexp)
    #print(gexp)
    c<-cor.test(gexp,mexp,method="pearson")
    return(data.frame(rho=c$estimate,pvalue=c$p.value))
    })))
  #print(corlist)
  return(cbind(data.frame(g=g,mir=mir), corlist))
  }else{
    return(data.frame(g=g,mir="mir",rho=NA,pvalue=NA))
  }
}


getcor_mirna<-function(mir,logmrna="",logmirna=""){
  #print(g)
  genes<-target[target[,1]==mir,2]
  genes<-intersect(genes,row.names(logmrna))
  if (length(genes)>0){
  mexp=as.numeric(logmirna[mir,])
  #print(mir)
  corlist<-do.call("rbind",(lapply(genes,function(g){
    gexp<-unlist(logmrna[g,])
    #print(mexp)
    #print(gexp)
    c<-cor.test(gexp,mexp,method="pearson")
    return(data.frame(rho=c$estimate,pvalue=c$p.value))
    })))
  #print(corlist)
  return(cbind(data.frame(g=genes,mir=mir), corlist))
  }else{
    return(data.frame(g="genes",mir=mir,rho=NA,pvalue=NA))
  }
}
#testing 
#getcor("WBGene00000012",mrna[, n2_labels],mirna[, n2_labels])
```


```{r cor-wrapper}
getTargets = function(de,genes,micros,fn_cor){
    res<-do.call("rbind",lapply(de,fn_cor,logmrna=genes,logmirna=micros))
    res = res %>% filter(!is.na(rho) & rho < 0) %>% mutate(adj=p.adjust(pvalue,method="BH"))
    #hist(res$pvalue)
    res[order(res$rho,res$pvalue),]
}
```

apply function to data


```{r n2-cor, cache=FALSE}
n2_de<-read.table("~/repos/pipelines/slack_cel_network/reports/n2_de_genes.txt",as.is=TRUE)
n2_deg_mirna<-read.table("~/repos/pipelines/slack_cel_network/reports/new_smallrna/mirna_n2_de_genes_perfect.txt",header=T,row.names=1)
row.names(n2_deg_mirna)<-sub(".ref.0.0.0.0","",row.names(n2_deg_mirna))
n2_res = getTargets(as.character(n2_de$id), mrna[, n2_labels], mirna[, n2_labels],getcor_gene)
n2_mir_res = getTargets(as.character(row.names(n2_deg_mirna)), mrna[, n2_labels], mirna[, n2_labels],getcor_mirna)
n2_both_res = getTargets(as.character(row.names(n2_deg_mirna)), mrna[n2_de$id, n2_labels], mirna[, n2_labels],getcor_mirna)
n2_res_sign = n2_res %>% filter(adj<0.1 & rho< -.7)
n2_mir_res_sign = n2_mir_res %>% filter(adj<0.1 & rho< -.7)
n2_both_res_sign = n2_both_res %>% filter(adj<0.1 & rho< -.7)

ko_de<-read.table("~/repos/pipelines/slack_cel_network/reports/ko_de_genes.txt",as.is=TRUE)
ko_deg_mirna<-read.table("~/repos/pipelines/slack_cel_network/reports/new_smallrna/mirna_ko_de_genes_perfect.txt",header=T,row.names=1)
row.names(ko_deg_mirna)<-sub(".ref.0.0.0.0","",row.names(ko_deg_mirna))
ko_res = getTargets(as.character(ko_de$id), mrna[, ko_labels], mirna[, ko_labels],getcor_gene)
ko_mir_res = getTargets(as.character(row.names(ko_deg_mirna)), mrna[, ko_labels], mirna[, ko_labels],getcor_mirna)
ko_both_res = getTargets(as.character(row.names(ko_deg_mirna)), mrna[ko_de$id, ko_labels], mirna[, ko_labels],getcor_mirna)
ko_res_sign = ko_res %>% filter(adj<0.1 & rho< -.7)
ko_mir_res_sign = ko_mir_res %>% filter(adj<0.1 & rho< -.7)
ko_both_res_sign = ko_both_res %>% filter(adj<0.1 & rho< -.7)


```


# Results 

```{r interaction-tables}
fn_n2 = paste0(path_files,"/n2_interaction.txt")
write.table(n2_both_res_sign, fn_n2)
fn_ko = paste0(path_files,"/ko_interaction.txt")
write.table(ko_both_res_sign, fn_ko)
```

[n2 interactions](`r get_report_links(fn_n2)`)

{'fraction_non_alphanumeric': 0.1838664583504646, 'fraction_numerical': 0.0294383685552175, 'mean_word_length': 6.158328428487345, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7237195', 'n_tokens_mistral': 5427, 'n_tokens_neox': 5054, 'n_words': 572}