#install.packages("tidyverse")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
OT_StickleGene <- read_csv("/stor/work/Bio321G_RY_Spring2024/Exercises/OT_StickleGene.csv")
## Rows: 76 Columns: 6901
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr    (4): sample_id, population, sex, turb_combined
## dbl (6897): ENSGACG00000000009, ENSGACG00000000013, ENSGACG00000000014, ENSG...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#View(OT_StickleGene)
TS_StickleGene <- read_csv("/stor/work/Bio321G_RY_Spring2024/Exercises/TS_StickleGene.csv")
## Rows: 58 Columns: 5923
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr    (4): sample_id, population, sex, turb_combined
## dbl (5919): ENSGACG00000000009, ENSGACG00000000016, ENSGACG00000000024, ENSG...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#View(TS_StickleGene)
OT_genes = ncol(select(OT_StickleGene, contains("ENS")))
print(OT_genes)
## [1] 6897
TS_genes = ncol(select(TS_StickleGene, contains("ENS")))
print(TS_genes)
## [1] 5919
OT_sample = nrow(select(OT_StickleGene))
print(OT_sample)
## [1] 76
TS_sample = nrow(select(TS_StickleGene))
print(TS_sample)
## [1] 58
OT_StickleGene <- OT_StickleGene %>%
  mutate(origin = case_when(
    population %in% c("Pristi", "Galta", "LittlaLon", "Lon", "Hops", "Thanga") ~ "N_America",
    population %in% c("Frosta", "Blauta") ~ "Europe",
  ))
TS_StickleGene <- TS_StickleGene %>%
  mutate(origin = case_when(
    population %in% c("Pristi", "Galta", "LittlaLon", "Lon", "Hops", "Thanga") ~ "N_America",
    population %in% c("Frosta", "Blauta") ~ "Europe",
  ))
TS_Sum <- TS_StickleGene %>%
  group_by(population, sex) %>%
  summarise_if(is.numeric, mean, na.rn = TRUE)
view(TS_Sum)
OT_Sum <- OT_StickleGene %>%
  group_by(population, sex) %>%
  summarise_if(is.numeric, mean, na.rn = TRUE)
view(OT_Sum)
OT_meta <- OT_StickleGene %>%
  select(-contains("ENS"))
view(OT_meta)

TS_meta <- TS_StickleGene %>%
  select(-contains("ENS"))
view(TS_meta)
targetgene_OT <- OT_Sum %>%
  select(population, sex, `ENSGACG00000017645`)
view(targetgene_OT)
targetgene_TS <- TS_Sum %>%
  select(population, sex, `ENSGACG00000017645`)
view(targetgene_TS)
ggplot(OT_StickleGene, aes(x = population, y = `ENSGACG00000017645`, fill = sex)) +
  geom_boxplot()

ggplot(TS_StickleGene, aes(x = population, y = `ENSGACG00000017645`, fill = sex)) +
  geom_boxplot()

Target_Summarized_TS<- TS_StickleGene %>%
  group_by(population, sex) %>%
  summarise(mean_expression = mean(`ENSGACG00000017645`, na.rm = TRUE), .groups = 'drop')

Target_Summarized_OT<- OT_StickleGene %>%
  group_by(population, sex) %>%
  summarise(mean_expression = mean(`ENSGACG00000017645`, na.rm = TRUE), .groups = 'drop')

Target_TS_Wider <- Target_Summarized_TS %>%
  pivot_wider(names_from = sex, values_from = mean_expression)
view(Target_Summarized_TS)
view(Target_TS_Wider)

Target_OT_Wider <- Target_Summarized_OT %>%
  pivot_wider(names_from = sex, values_from = mean_expression)
view(Target_Summarized_OT)
view(Target_OT_Wider)
ggplot(Target_TS_Wider, aes(x = M, y = F, color = population)) +
  geom_point(size = 3) +
  labs(x = "Male Expression", y = "Female Expression", title = "Expression of ENSGACG00000017645 by Sex and Population for TS") +
  theme_minimal()

ggplot(Target_OT_Wider, aes(x = M, y = F, color = population)) +
  geom_point(size = 3) +
  labs(x = "Male Expression", y = "Female Expression", title = "Expression of ENSGACG00000017645 by Sex and Population for OT") +
  theme_minimal()