---
title: "37-plotting-miRNA-meth"
author: "Steven Roberts"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

```{bash}
head /Users/sr320/GitHub/ConTra/data/full-species-biomin/cleaned_peve/wgbs_counts_cleaned.csv
```

'Peve_00000077')",methylation_CpG_Porites_evermani_scaffold_2880_18065,mirna_Cluster_8147,0.40159893766018984,0.46547928344747846,0.5484496225931289,0.06388034578728863,0.08297033914565044,True,-0.8308501279304866,-0.4741015566965178,0.3567485712339688,negative,0.021137921882058208



'Peve_00000077')",methylation_CpG_Porites_evermani_scaffold_2880_18065,mirna_Cluster_6235,0.40159893766018984,0.44155194507803575,0.5179160414601047,0.03995300741784591,0.07636409638206898,True,-0.7770695032539743,-0.45647774091118826,0.320591762342786,negative,0.0313563215710303
methylation_mirna,"('OG_08920', 



'Peve_00007775')",methylation_CpG_Porites_evermani_scaffold_22_796859,mirna_Cluster_3676,0.4582881665650048,0.46203023380043606,0.5326939288630675,0.0037420672354312456,0.07066369506263148,True,0.7681097416621632,-0.08895084672406524,0.8570605883862285,positive,0.0351649339266733


'Peve_00037895')",methylation_CpG_Porites_evermani_scaffold_621_73376,mirna_Cluster_11615,0.4413585132806228,0.4752230866032875,0.6022089646716988,0.03386457332266468,0.12698587806841133,True,,,,NA,0.0031280131352001694
methylation_mirna,"('OG_06592', 


| 1 | ('OG_11813', 'Peve_00021937') | methylation_CpG_Porites_evermani_scaffold_3933_26651 | mirna_Cluster_1004 | 0.028 | 1.523 | False |



'Peve_00003282')",methylation_CpG_Porites_evermani_scaffold_2468_67964,mirna_Cluster_4968,0.49517256271521937,0.5084960749125962,0.6405817108341498,0.013323512197376819,0.13208563592155365,True,0.8594695256198828,0.32172617274470294,0.5377433528751798,positive,0.0016851843387992727
methylation_mirna,"('OG_04243', 'Peve_00003282')",methylation_CpG_Porites_evermani_scaffold_9_1086103,mirna_Cluster_4968,0.4949673182269946,0.5099578425794248,0.6510651968738449,0.014990524352430201,0.14110735429442012,True,0.8334351006638587,,,NA,0.001069190601504233
methylation_mirna,"('OG_04243', 'Peve_00003282')",methylation_CpG_Porites_evermani_scaffold_2042_42068,mirna_Cluster_4968,0.4874468046018934,0.5400480356262451,0.6189286749471542,0.05260123102435177,0.07888063932090905,True,0.7988298910216519,0.2830643510335664,0.5157655399880855,positive,0.014898023689596696
methylation_mirna,"('OG_04243', 'Peve_00003282')",methylation_CpG_Porites_evermani_scaffold_3494_26092,mirna_Cluster_4968,0.47922451226680995,0.5092387075022629,0.6797440829402398,0.0300141952354529,0.17050537543797695,True,0.9109498733150457,-0.17292843814343797,1.0838783114584838,positive,0.00024440069042364065
methylation_mirna,"('OG_04243', 'Peve_00003282')",methylation_CpG_Porites_evermani_scaffold_76_563332,mirna_Cluster_4968,0.4757088541307325,0.5001447419265209,0.6148700057162267,0.024435887795788425,0.11472526378970582,True,0.9378340640956038,0.4094715258653537,0.5283625382302501,positive,0.004149749902172872
```{r}
library(tidyverse)
library(ggplot2)

gene_id <- "Peve_00003282"
cpg_id  <- "CpG_Porites_evermani_scaffold_2468_67964"
mir_id  <- "Cluster_4968"

## -----------------------------
## GENE EXPRESSION (NO NAME COLLISION)
## -----------------------------
gene <- read_csv("/Users/sr320/GitHub/ConTra/data/full-species-biomin/cleaned_peve/gene_counts_cleaned.csv") %>%
  mutate(clean_gene = str_extract(gene_id, "Peve_\\d+")) %>%   # extract clean gene name
  filter(clean_gene == !!gene_id) %>%                          # compare to argument
  select(-gene_id, -clean_gene) %>%                            # keep data only
  unlist() %>% as.numeric()

## -----------------------------
## METHYLATION
## -----------------------------
meth <- read_csv("/Users/sr320/GitHub/ConTra/data/full-species-biomin/cleaned_peve/wgbs_counts_cleaned.csv") %>%
  filter(cpg_id == !!cpg_id) %>%
  select(-cpg_id) %>%
  unlist() %>% as.numeric()

## -----------------------------
## miRNA
## -----------------------------
mir <- read_csv("/Users/sr320/GitHub/ConTra/data/full-species-biomin/cleaned_peve/mirna_counts_cleaned.csv") %>%
  filter(mirna_id == !!mir_id) %>%
  select(-mirna_id) %>%
  unlist() %>% as.numeric()

## -----------------------------
## CHECK ALIGNMENT
## -----------------------------
lengths <- sapply(list(gene, meth, mir), length)
print(lengths)
stopifnot(all(lengths == lengths[1]))

## -----------------------------
## CONTEXT SPLITTING
## -----------------------------
miRNA_state <- ifelse(mir > median(mir, na.rm = TRUE),
                      "High miRNA", "Low miRNA")

df <- tibble(
  Expression = gene,
  Methylation = meth,
  miRNA = mir,
  miRNA_state = miRNA_state
)

## -----------------------------
## PLOT
## -----------------------------
ggplot(df, aes(x = Methylation, y = Expression, color = miRNA_state)) +
  geom_point(size = 3, alpha = 0.85) +
  geom_smooth(method = "lm", se = FALSE) +
  theme_bw(base_size = 14) +
  scale_color_manual(values = c("High miRNA" = "#D55E00",
                                "Low miRNA"  = "#0072B2")) +
  labs(
    title    = "miRNA levels modulate CpG methylation effects on gene expression",
    subtitle = paste("Gene:", gene_id, "| CpG:", cpg_id, "| miRNA:", mir_id),
    x = "CpG Methylation",
    y = "Gene Expression",
    color = "miRNA state"
  )
```
python code/plot_methylation_mirna_context.py \
    --results-dir output/context_dependent_analysis_TIMESTAMP \
    --context-dependent
    
    
    
