library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggrepel)
## Loading required package: ggplot2
library(ggplot2)

Importing and Understanding Data Frames

Read in all necessary files from the yeast transcriptomics data.

# Read in the merged label data frame created in this week's Input and Merge assignment
mergedLabels <- read.csv("mergedYeast.csv", header = TRUE)

# Read in remaining files as single files
expression <- read.csv("SC_expression.csv", header = TRUE)

conditions <- read.csv("conditions_annotation.csv", header = TRUE)

View newly imported data frames.

View(mergedLabels)
View(conditions)
View(expression)

Filter condition Data Frame and Apply to expression Data Frame

Filter the condition data frame for ‘ethanol.’

# Filter for 'ethanol'
ethConditions <- conditions[grepl("ethanol",conditions$secondary),] 

# Drop the last column ('additional_information') since it contains no data
ethConditions <- subset(ethConditions, select = -additional_information)

Filter the expression data frame to the filtered condition data frame (treatment).

# Rename the 'X' column in the expression data frame to 'gene'
expression <- expression %>% rename(gene = X)

# Extract ID column from ethConditions data frame since it is a foreign key in the expression data frame
treatment <- ethConditions$ID

# Inspect the names of the values from the extracted data
treatment
## [1] "IFFABC" "IFFABB" "IFFABF" "IFFABQ" "IFFABI" "IFFABS" "IFFABN" "IFFAFR"
# Filter out the data that appears in ethConditionsID in the expression data frame to help ensure a smooth join
filteredExpression <- expression %>%
  select(gene, all_of(treatment))

# View newly filtered data frame
View(filteredExpression)

Join Filtered expression data frame with labels Data Frame

Join filteredExpression and mergedLabels by ‘gene.’

# Determining what join needs used
mergedLabels %>% 
  filter(is.na(gene))
## [1] gene       validation BP         CC         MF        
## <0 rows> (or 0-length row.names)
mergedLabels %>%
  summarise(unique_count = n_distinct(gene))
##   unique_count
## 1         6071
filteredExpression %>% 
  filter(is.na(gene))
## [1] gene   IFFABC IFFABB IFFABF IFFABQ IFFABI IFFABS IFFABN IFFAFR
## <0 rows> (or 0-length row.names)
filteredExpression %>%
  summarise(unique_count = n_distinct(gene))
##   unique_count
## 1         6071
# Choosing inner_join to join because this is an even join, where all 'gene' values likely match with no NA data (6071 unique observations in each data frame) 
joinedDF <- inner_join(mergedLabels, filteredExpression, by = "gene")

# View new joined data frame
View(joinedDF)

Use pivot_longer to Re-Organize Data Frame

Pivot ‘treatment’ values longer.

joinedDF_longer <- joinedDF %>%
  pivot_longer(
    cols = all_of(treatment),
    names_to = "treatment",
    values_to = "count")

Make a Tibble of Pivoted Data

Make a tibble of mean and median counts for all treatment.

# Make the tibble
longerTibble <- joinedDF_longer %>%
  group_by(treatment) %>%
  summarize(
    mean_count = mean(count, na.rm = TRUE),
    median_count = median(count, na.rm = TRUE),
    n = n()
  ) %>%
  as_tibble()

#View tibble
View(longerTibble)

Visualize Distribution of Counts for Treatments

Create a violin plot of the data.

# Filtering out extreme outliers (mean_count x 2 = 328, just like in assignment page example) before plotting 
summary(joinedDF_longer$count)
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
##      0.00     25.83     50.41    164.72    100.14 142672.97
# joined_filtered <- joinedDF_longer %>% 
 # filter(count > 328)

# Since there are no outliers, data can be plotted as default
# ggplot(joined_filtered, aes(x = treatment, y = count)) + 
  # geom_boxplot() +
  # labs(
   # title = "Counts of Gene Expression by Treatment",
   # x = "Treatment",
   # y = "Count"
#  )

# After seeing the range of data in the first plot, resetting the filter to be more stringent and then replot
joined_filtered <- joinedDF_longer %>% 
  filter(count < 3000)

ggplot(joined_filtered, aes(x = treatment, y = count,)) + 
  geom_violin() +
  labs(
    title = "Counts of Gene Expression by Treatment",
    x = "Treatment",
    y = "Count"
  )