---
title: "WGCNA_master_run"
output:
  html_document: default
  pdf_document: default
---


```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE, message = FALSE, cache = F)
print(getwd())

source("2-WGCNA_master_lib_June2025.R")

```

## import dataset

```{r data import}

# count_table

Alltable_onl = read.csv('datalocalandonline_cds_counts_2025_final.tsv', h=T, sep = "\t", row.names=1)

# Old table only to compar
#Alltable_onl = read.csv('../datalocalandonline_cds_counts_2022.tsv', h=T, sep = "\t", row.names=1)



colnames(Alltable_onl) = gsub("loc_cds_SHPC43_Mastomys_kollmanspergeri", "loc_cds_SHPC43_Mastomys_kollmannspergeri", colnames(Alltable_onl))
colnames(Alltable_onl) = gsub("loc_cds_SHPC74_Mastomys_kollmanspergeri", "loc_cds_SHPC74_Mastomys_kollmannspergeri", colnames(Alltable_onl))
colnames(Alltable_onl) = gsub("loc_cds_SHPC75_Mastomys_kollmanspergeri", "loc_cds_SHPC75_Mastomys_kollmannspergeri", colnames(Alltable_onl))
colnames(Alltable_onl) = gsub("loc_cds_SHPC76_Mastomys_kollmanspergeri", "loc_cds_SHPC76_Mastomys_kollmannspergeri", colnames(Alltable_onl))
colnames(Alltable_onl) = gsub("SHPC46_Arvicanthis_niloticus_lmo", "SHPC46_Mastomys_natalensis", colnames(Alltable_onl)) #change Ani_lmo to Mna even if not using in expression)

# coldata

coldata <- read.csv("coldata_with_bio17_July2025.tsv", sep = "\t") # 110 samples

# Subset_coldata
coldata_tot <- coldata[which(coldata$total_set == "yes"),]
coldata_mur <- coldata[which(coldata$muridae_set == "yes"),]
coldata_rec <- coldata[which(coldata$recent_set == "yes"),]
coldata_anc <- coldata[which(coldata$ancient_set == "yes"),]

```

## prepare tree

```{r data import}


```



# 1- Set ANCIENT

# Dataset expression 2025

```{r, eval = FALSE}
output_dir="dataset_ancient_WGCNA_q50_power20_0910/"
system(paste0("mkdir -p " , output_dir))

```

```{r, fig.path = output_dir, eval = FALSE}

data_expr_anc_season = list(var1_design = "ancient_group_batch", var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_anc, output_dir=output_dir, dataset = "ancient", nCPU = 6)

optimize_param_FUN(data_expr_anc_season)

```

```{r, fig.path = output_dir, eval = FALSE}

data_expr_anc_season_2 = list(var1_design = "ancient_group_batch", var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_anc, output_dir=output_dir, dataset = "ancient", nCPU = 6, picked_power = 20)

wgcna_all_FUN(data_expr_anc_season_2)

```

# Set RECENT

# Dataset expression 2025, SET RECENT TRANSITION

```{r, eval = FALSE}
output_dir="dataset_recent_WGCNA_q50_power8_0910/"
system(paste0("mkdir -p " , output_dir))


```

```{r, fig.path = output_dir, eval = FALSE}

data_expr_rec_season = list(var1_design = "recent_group_batch", var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_rec, output_dir=output_dir, dataset = "recent", nCPU = 6)

optimize_param_FUN(data_expr_rec_season)

```

```{r, fig.path = output_dir, eval = FALSE}

data_expr_rec_season_2 = list(var1_design = "recent_group_batch", var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_rec, output_dir=output_dir, dataset = "recent", nCPU = 6, picked_power = 8)

# v2 = 12, final = 7
wgcna_all_FUN(data_expr_rec_season_2)

```

# Set Murinae

# Dataset expression 2025, SET MURIDAE INTRA-FAMILY

```{r, eval = FALSE}
output_dir="dataset_muridae_WGCNA_q50_power20_1010_with_2022counts/"
system(paste0("mkdir -p " , output_dir))

```

```{r, fig.path = output_dir, eval = FALSE}

data_expr_mur_season = list(var1_design = NULL, var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_mur, output_dir=output_dir, dataset = "murinae", nCPU = 6)

optimize_param_FUN(data_expr_mur_season)

```

```{r, fig.path = output_dir, eval = FALSE}

data_expr_mur_season_2 = list(var1_design = NULL, var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_mur, output_dir=output_dir, dataset = "murinae", nCPU = 6, picked_power = 20)

wgcna_all_FUN(data_expr_mur_season_2)

```


# Dataset expression 2025, SET TOTAL 

## Change in lib: total_group_batch
```{r}
output_dir="dataset_tot_WGCNA_q50_power5_0910/"
system(paste0("mkdir -p " , output_dir))


```

```{r, fig.path = output_dir}

data_expr_tot_fam_season = list(var1_design = "total_group_batch", var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_tot, output_dir=output_dir, dataset = "total", nCPU = 6)

optimize_param_FUN(data_expr_tot_fam_season)
```

```{r, fig.path = output_dir, eval = FALSE}

data_expr_tot_fam_season_2 = list(var1_design = "total_group_batch", var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_tot, output_dir=output_dir, dataset = "total", nCPU = 6, picked_power = 5)

#power v2=7, final =5
wgcna_all_FUN(data_expr_tot_fam_season_2)

```

