---
title: "The Code That Never Was"
output: html_document
date: "2023-12-14"
---

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

## The Code That Never Was

This is code that did not make the "final cut" and would've been nice, but ultimately not neccessary to the analyses.

## 06-deseq2

### Making an extra Heatmap for sig DEGs

I wanted to create a heatmap that showed sig. DEGs by sample, not neccessary, but was would've been cool. Scrapped it for the sake of time. The following code goes after the whole of the \`06-deseq2.Rmd\` script:

\~\~\~\~

commenting out this section so I can knit and folks can see the outputs

```{r}
# make a heatmap using only our significant DEGs

# #import data
# DEGCounts <- read.table("../output/1213-DEG.tab", header=TRUE, quote="\"")
# head(DEGCounts)
# 
# #select subset of just gene and sample columns
# DEGHeatmap <- DEGCounts[-c(2:7)]
# head(DEGHeatmap)
# 
# # establish that the first column is row names 
# # rownames(DEGHeatmap) <- DEGHeatmap$gene
# 
# #get rid of the redundant row names column that will be created.
# # DEGHeatmap <- DEGHeatmap[,-1]
# 
# 
# ### stuck here
# ### Error in seq.default(-m, m, length.out = n + 1) :'from' must be a finite number
# 
# #make heatmap
# pheatmap(DEGHeatmap, scale = "row",show_rownames = TRUE, treeheight_col = 1, fontsize_col = 10, fontsize_row = 3)



```

Chunk above is giving me trouble. Here's what I want:

1.  a data frame containing rows = genes, cols = samples, values = mean exp but ONLY for things where p \<= 0.05
2.  a heatmap that then shows the genes that are *significantly deferentially* expressed

```{r}

# read in sig.DEGs
DEGCounts <- read.table("../output/1213-DEG.tab", header=TRUE, quote="\"")
head(DEGCounts)

DEGCountNames <-rownames(DEGCounts)
DEGCounts <- add_column(DEGCounts, DEGCountNames, .before = DEGCounts$baseMean )

varGenes <- order( rowVars (x = assay(rld), useNames = FALSE ), decreasing=TRUE )
varGenes <-assay(rld)[topVarGenes,]

varNames <- rownames(varGenes)
varGenes <- as.data.frame(varGenes) 
varGenes <- add_column(varGenes, varNames, .before = varNames$`M-C-193`)

merge(varGenes, DEGCounts, by.y = "DEGCountNames", by.x = "top25names")

```

## extra abandoned chunks 

[github issue](https://github.com/RobertsLab/resources/issues/1880) from when I was struggling (again) with DESeq2 and getting a heatmap

```{r}
#Regularized log transformation
rld <- rlog( deseq2.dds, blind = FALSE )

#Get 25 top varying genes
topVarGenes <- head( order( rowVars (x = assay(rld), useNames = FALSE ), decreasing=TRUE ), 25)
```

```{r}
# log2_changes <- deseq2.res[, "log2FoldChange"]

# Filter for significant genes (adjusted p-value < 0.05)
significant_results <- subset(deseq2.res, padj < 0.05)

# Sort results by absolute log2 fold change
significant_results <- significant_results[order(abs(significant_results$baseMean), decreasing = TRUE), ]

# Get top 25 significant genes
top_25_genes <- head(significant_results, 25)

# Extract log2 fold change values for top 25 genes
top_25_genes_ids <- rownames(top_25_genes)
top_25_genes_log2fc <- top_25_genes$log2FoldChange

# Create a matrix containing log2 fold change values of top 25 genes
top_25_genes_matrix <- count_matrix[top_25_genes_ids, ]

# Plot heatmap for log2 fold change of top 25 genes
pheatmap(top_25_genes_matrix, 
         cluster_rows = FALSE, 
         cluster_cols = FALSE,
         main = "Heatmap of Log2 Fold Change for Top 25 DEGs",
         #color = colorRampPalette(c("blue", "yellow", "red"))(100),  # Choose a color palette
         breaks = seq(-4, 4, length.out = 101),  # Adjust the breaks for better color resolution
         fontsize = 8)  # Adjust fontsize as needed
```

```{r}
# make heatmap of top 25 differentially expressed genes
# get top 25 counts
top25Counts<-assay(rld)[topVarGenes,]
head(top25Counts)

# make the heatmap. Cluster_cols = FALSE neccessary to get the M-T and M-C columns next to eachother (defult leaves em randomized)
pheatmap(top25Counts,scale = "row",show_rownames = TRUE, treeheight_col = 1, fontsize_col = 12, fontsize_row = 5, cluster_cols = FALSE, cluster_rows = FALSE)

deg <- read.csv("../output/1213-DEG.tab", row.names = 1, sep = "")
deg <- deg[order(deg[, 1], decreasing = TRUE), ]
deg25 <- deg[1:25, ]



pheatmap(top25Counts,scale = "row",show_rownames = TRUE, treeheight_col = 1, fontsize_col = 12, fontsize_row = 5, cluster_cols = FALSE, cluster_rows = FALSE)

```
