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


```{r setup, include=FALSE}

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

source("Expression_master_lib_June2025.R")
test = F
```

## import dataset

```{r data import}

# count_table

Alltable_onl = read.csv('datalocalandonline_cds_counts_2025_final.tsv', h=T, sep = "\t", row.names=1) #15796   125
#rownames(Alltable_onl) = Alltable_onl$Row.names

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)
# colnames(Alltable_onl) = gsub("onl5_cds_SRR17216323_Peromyscus_maniculatus", "onl_cds_SRR17216323_Peromyscus_maniculatus", colnames(Alltable_onl))
# colnames(Alltable_onl) = gsub("onl5_cds_SRR17216333_Peromyscus_maniculatus", "onl_cds_SRR17216333_Peromyscus_maniculatus", colnames(Alltable_onl))
# colnames(Alltable_onl) = gsub("onl5_cds_SRR17216352_Peromyscus_maniculatus", "onl_cds_SRR17216352_Peromyscus_maniculatus", colnames(Alltable_onl))

# coldata

coldata <- read.csv("coldata_July2025.tsv", sep = "\t")

coldata$cond_season <- factor( coldata$cond_season, levels = c("mesic","arid"))

coldata$read_length_class <- "from_50_to_80"
coldata$read_length_class[coldata$average_read_length > 80 & coldata$average_read_length < 102] <- "from_80_to_100"
coldata$read_length_class[coldata$average_read_length > 120] <- "above_120"
  
coldata$RIN_class <- NA
coldata$RIN_class[coldata$RIN_RNA_local < 7] <- "below_7"
coldata$RIN_class[coldata$RIN_RNA_local > 7 & coldata$RIN_RNA_local < 8] <- "from_7_to_8"
coldata$RIN_class[coldata$RIN_RNA_local > 8 & coldata$RIN_RNA_local < 9] <- "from_8_to_9"
coldata$RIN_class[coldata$RIN_RNA_local > 9 ] <- "above 9"
  
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"),]


```

# 1- Set ANCIENT

# Dataset expression 2025

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

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

```

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

make_exp_analyses_all(data_expr_anc_season)

```

# Set RECENT

# Dataset expression 2025, SET RECENT TRANSITION

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

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

```

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

make_exp_analyses_all(data_expr_rec_season)

```


# Set Murinae

# Dataset expression 2025, SET MURIDAE INTRA-FAMILY

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

data_expr_mur_season = list(var1_design = NULL, var2_design = "cond_season", Alltable = Alltable_onl, coldata = coldata_mur, output_dir=output_dir, pcs_to_plot = c(1,2), dataset = "murinae", nCPU = 14, test = test)

```

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

make_exp_analyses_all(data_expr_mur_season)

```


# Dataset expression 2025, SET TOTAL 

## Change in lib: total_group_batch

```{r}
output_dir="dataset_tot_final/"
system(paste0("mkdir -p " , 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, pcs_to_plot = c(1,5), dataset = "total", nCPU = 14, test = test)


```

```{r, fig.path = output_dir}
make_exp_analyses_all(data_expr_tot_fam_season)
```

