---
title: "R Notebook - Identification of tRNA-derived small RNA (tsRNA) responsive to the tumor suppressor, RUNX1, in breast cancer"

output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. To execute a chunk, click the *Run* button within the chunk or by place your cursor inside the chunk and press *Cmd+Shift+Enter*. 

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file). 

# Load required R packages and record version number

```{r}
library("rgl")
library("limma")

R_Ver <- R.version
rgl_Ver <- packageVersion("rgl")
limma_Ver <- packageVersion("limma")

print("R version")
R_Ver
print("rgl package version")
rgl_Ver
print("limma package version")
limma_Ver
```


# Analysis of experimental MCF10A_shNS, shRunx1-C1, shRunx1-C4, MCF10CA1a_EV, MCF10CA1a_R1_OE cell lines

```{r}
## Scripted by Nicholas H. Farina based on Dario Venezario script and limma user manual
## University of Vermont Cancer Center

setwd("~/working/tsRNA_BCa_cells")

#===========================#
#### 1. Read array files ####
#===========================#

targets <- readTargets("targets_experimental.txt")

## Set up a filter so that any spot with a flag of -99 or less gets zero weight.

f <- function(x) as.numeric(x$Flags > -99)

## Read in the data.

RG <- read.maimages(targets, source="genepix", wt.fun=f, columns=list(E="F635 Mean", Eb="B635"))

## Identify different types of probes from the entries appearing in the gene list

spottypes <- readSpotTypes(file = "SpotTypes.txt", sep = "\t", check.names = FALSE)

## It is typically used as an argument to the controlStatus() function to set the status of each spot on the array, for example

RG$genes$Status <- controlStatus(spottypes, RG)

#==================================================#
#### 2. BG correct and normalize accross arrays ####
#==================================================#

## The following command implements a type of adaptive background correction. This is optional but recommended for GenePix data.

RGbkgd <- backgroundCorrect(RG, method="normexp", offset=50)

## Print-tip loess normalization:
MA <- normalizeBetweenArrays(RGbkgd, method = "quantile")

## Generate and export table of normalized data

NormData_names <- c(as.data.frame(MA$E), as.data.frame(MA$genes))
write.csv(NormData_names, file = "experimental_tsRNA_bkgr cor_quant inter norm.csv")

NormData <- as.matrix(MA$E)

#==========================#
#### 3. Quality control ####
#==========================#

## Generate MA plots for 1 array (each array is a different column)
plotMD(RGbkgd, column = 1)

## Generate MA plots for all arrays in a 3 x 2 layout (files written to working directory as PNG)
plotMA3by2(RGbkgd, prefix="MA_experimental")

```


# Analysis of parental MCF10A, MCF10AT1, MCF10CA1a, MCF7, and MDA-MB-231 cell lines

```{r}
## Scripted by Nicholas H. Farina based on Dario Venezario script and limma user manual
## University of Vermont Cancer Center

setwd("~/working/tsRNA_BCa_cells")

#===========================#
#### 1. Read array files ####
#===========================#

targets <- readTargets("targets_parental.txt")

## Set up a filter so that any spot with a flag of -99 or less gets zero weight.

f <- function(x) as.numeric(x$Flags > -99)

## Read in the data.

RG <- read.maimages(targets, source="genepix", wt.fun=f, columns=list(E="F635 Mean", Eb="B635"))

## Identify different types of probes from the entries appearing in the gene list

pottypes <- readSpotTypes(file = "SpotTypes.txt", sep = "\t", check.names = FALSE)

## It is typically used as an argument to the controlStatus() function to set the status of each spot on the array, for example

RG$genes$Status <- controlStatus(spottypes, RG)

#==================================================#
#### 2. BG correct and normalize accross arrays ####
#==================================================#

## The following command implements a type of adaptive background correction. This is optional but recommended for GenePix data.

RGbkgd <- backgroundCorrect(RG, method="normexp", offset=50)

## Print-tip loess normalization:
MA <- normalizeBetweenArrays(RGbkgd, method = "quantile") # NF Script for tsRNA arrays

## Generate and export table of normalized data

NormData_names <- c(as.data.frame(MA$E), as.data.frame(MA$genes))
write.csv(NormData_names, file = "parental_tsRNA_bkgr cor_quant inter norm.csv")

NormData <- as.matrix(MA$E)

#==========================#
#### 3. Quality control ####
#==========================#

## Generate MA plots for 1 array (each array is a different column)
plotMD(RGbkgd, column = 1)

## Generate MA plots for all arrays in a 3 x 2 layout (files written to working directory as PNG)
plotMA3by2(RGbkgd, prefix = "MA_parental")

```



# Create 3D scatter plot of PCA in Figure 1A

```{r}
## Scripted by Nicholas H. Farina
## University of Vermont Cancer Center

#### Read in file and group by condition ####

setwd("~/Creative Cloud Files/Manuscripts and Grants/Manuscripts/Submitted/tsRNA and Runx1/Data")

PCA <- read.csv("PCA_experimental_tsRNA.csv", header = TRUE, sep = ",")

## This is here just in case each there is a desire to modify the look of each group

# MCF10A_shNS_CTRL <- filter(PCA, Group == "MCF10A_shNS_CTRL")
# MCF10A_shR1.C1 <- filter(PCA, Group == "MCF10A_shRunx1-C1")
# MCF10A_shR1.C4 <- filter(PCA, Group == "MCF10A_shRunx1-C4")
# MCF10CA1a_EV_CTRL <- filter(PCA, Group == "MCF10CA1a_EV_CTRL")
# MCF10CA1a_R1_OE <- filter(PCA, Group == "MCF10CA1a_R1_OE")


#### Color each cell type ####
PCAplot <- with(PCA, spheres3d(PCA.1, PCA.2, PCA.3, col=c('red','blue','gold','white','springgreen')[as.numeric(Group)], radius = 0.1))

#### Set aspect, size, and view ####
aspect3d(1,1,1)
par3d(windowRect = c(30,30,1000,1000))
view3d(theta = 0, phi = -5, fov = 50)

#### add axes and gridlines to 3D axis planes (x, x+, y, y+, z, z+) ####
box3d()
grid3d('x', col = "gray", lwd = 1, lty = 1, n = 6)
grid3d('y', col = "gray", lwd = 1, lty = 1, n = 6)
grid3d('z', col = "gray", lwd = 1, lty = 1, n = 7)
axis3d('x-+', nticks = 6)
axis3d('y-+', nticks = 7)

#### export to file - keep commented out unless wanting to write a new file ####
# rgl.postscript("PCA_experimental_tsRNA_3D_phi-5.svg", fmt = "svg", drawText = T)

```


