---
title: "Unsupervised Hierarchical Clustering of Single Cell Data"
output:
  pdf_document: 
    fig_caption: yes
---

## Introduction
  
This report describes the methods used to obtain the highly expressed genes used for unsupervised hierarchical clustering of the single cell data. Three different gene sets were chosen to cluster the non-diabetic, diabetic, and combined cohorts respectively. For each cohort, genes with log2(CPM) expression values greater than 10.5 in at least one sample were selected.

```{r, eval=FALSE, echo=TRUE, warning=FALSE}
# Load libraries
suppressPackageStartupMessages(library(Biobase))
suppressPackageStartupMessages(library(edgeR))
suppressPackageStartupMessages(library(xlsx))
library(Biobase)
library(edgeR)
library(xlsx)
set.seed(53079239)

# Load NonT2D single cell data
fname = "NonT2D"
setwd("/Users/lawlon/Documents/Final_RNA_Seq_3/Data/")
load("nonT2D.rdata")
p.anns <- featureData(cnts.eset)
probe.anns <- as(p.anns,"data.frame")
ND.anns <- pData(cnts.eset)
# Remove multiples and keep all other groups
ND.sel <- ND.anns[ND.anns$cell.type %in% c("INS", "PPY", "GCG", "SST", "COL1A1", "KRT19", "PRSS1", "none"),]

# Obtain counts
ND.counts <- exprs(cnts.eset)
# Calculate log2 cpm
cpms <- cpm(x = ND.counts)
data <- log2(cpms+1)
data <- data[, rownames(ND.sel)]

# Filter gene by those with max CPM value greater than 10 (high expressed genes)
r.max <- apply(data,1,max)
ND.data.sel <- data[r.max > 10.5,]

# Combine probe anns with selected fpkm values
p.res <- probe.anns[rownames(ND.data.sel),]
ND.data.sel.exp <- cbind(p.res,ND.data.sel)

# Write selected gene matrix to file
write.xlsx(ND.data.sel.exp, file = "Supplemental_Table_S4.xlsx", 
           sheetName = paste(fname, "genes_selected", sep = "_"))

# Select genes for T2D data set
fname = "T2D"
setwd("/Users/lawlon/Documents/Final_RNA_Seq_3/Data/")
load("T2D.rdata")
p.anns <- featureData(cnts.eset)
probe.anns <- as(p.anns,"data.frame")
T2D.anns <- pData(cnts.eset)
# Remove multiples and keep all other groups
T2D.sel <- T2D.anns[T2D.anns$cell.type %in% c("INS", "PPY", "GCG", "SST", "COL1A1", "KRT19", "PRSS1", "none"),]

# Obtain counts
T2D.counts <- exprs(cnts.eset)
# Calculate log2 cpm
T2D.cpms <- cpm(x = T2D.counts)
T2D.data <- log2(T2D.cpms+1)
T2D.data <- T2D.data[, rownames(T2D.sel)]

# Filter gene by those with max CPM value greater than 10 (high expressed genes)
r.max <- apply(T2D.data,1,max)
T2D.data.sel <- T2D.data[r.max > 10.5,]

# Combine probe anns with selected fpkm values
p.res <- probe.anns[rownames(T2D.data.sel),]
T2D.data.sel.exp <- cbind(p.res,T2D.data.sel)

# Write selected gene matrix to file
write.xlsx(T2D.data.sel.exp, file = "Supplemental_Table_S4.xlsx", 
           sheetName = paste(fname, "genes_selected", sep = "_"),
           append = TRUE)

# Select genes for combined NonT2D and T2D dataset
# combine expression datasets
fname = "NonT2D_and_T2D"
comb.data <- cbind(data, T2D.data)

# Filter gene by those with max CPM value greater than 10 (high expressed genes)
r.max <- apply(comb.data,1,max)
comb.data.sel <- comb.data[r.max > 10.5,]

# Combine probe anns with selected fpkm values
p.res <- probe.anns[rownames(comb.data.sel),]
comb.data.sel.exp <- cbind(p.res,comb.data.sel)

# Write selected gene matrix to file
write.xlsx(comb.data.sel.exp, file = "Supplemental_Table_S4.xlsx", 
           sheetName = paste(fname, "genes_selected", sep = "_"),
           append = TRUE)


```

## Session Information
```{r, eval=TRUE, echo=TRUE}
suppressPackageStartupMessages(library(Biobase))
suppressPackageStartupMessages(library(edgeR))
suppressPackageStartupMessages(library(xlsx))
library(Biobase)
library(edgeR)
library(xlsx)
sessionInfo()
```
