Table of Contents |
---|
...
Open RStudio (you can type it in the Windows search bar)
Create a new R script: ‘File’ → “New File” → “R script”
Save this script where your samples folders are (‘File’ → ‘Save’). These should be on your H or W drive. Save the script file as ‘scrnaseq.R’
In the following sections you will be copying and running the R code into your scrnaseq.R script.
Cell Ranger (and nfcore/scrnaseq) generates a default folder and file output structure. There will be a main folder that contains all the sample subfolders (NOTE: this is where you must save your R script). Each sample folder will have an ‘outs’ subfolder. This ‘outs’ folder contains a ‘filtered_feature_bc_matrix’ folder, which contains the files that Seurat uses in its analysis.
...
You can manually set your working directory in RStudio by selecting ‘Session' -> 'Set working directory' -> 'Choose directory'. Choose the same directory as you saved your scrnaseq.R script, previous section. This will output the setwd(...)
command with your working directory into the console window (bottom left panel). Copy this command to replace the default setwd(...)
line in your R script.
...
Copy and paste the following code into the R script you just created, then run the code (highlight all the code in your R script, then press the run button).
...
Code Block |
---|
#### 2b. Set your working directory ####
# Change the below to the directory that contains your sample folders (you may have to browse H or W drive to find this)
# **USER INPUT**
setwd("H:/sam_dando/dataset1/count")
# You can see the sample subdirectories by:
list.dirs(full.names = F, recursive = F)
# You should see directories that are names after your samples.
# If you don't see this, browse through your H or W drives to find the correct path for your sample directories.
|
...
This will install all the required packages and dependencies and may take 30 minutes or more to complete. It may prompt you occasionally to update packages - select 'a' for all if/when this occurs.
Code Block |
---|
#### 2c. Installing required packages ####
# This section only needs to be run once on a computer.
# One the packages are installed, they need to be loaded every time they will be used (next section)
# Create vector of required package names
bioconductor_packages <- c("clusterProfiler", "pathview", "AnnotationHub", "org.Mm.eg.db")
cran_packages <- c("Seurat", "patchwork", "ggplot2", "tidyverse", "viridis", "plyr", "readxl", "scales")
# Compares installed packages to above packages and returns a vector of missing packages
new_packages <- bioconductor_packages[!(bioconductor_packages %in% installed.packages()[,"Package"])]
new_cran_packages <- cran_packages[!(cran_packages %in% installed.packages()[,"Package"])]
# Install missing bioconductor packages
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install(new_packages)
# Install missing cran packages
if (length(new_cran_packages)) install.packages(new_cran_packages, repos = "http://cran.us.r-project.org")
# Update all installed packages to the latest version
update.packages(bioconductor_packages, ask = FALSE)
update.packages(cran_packages, ask = FALSE, repos = "http://cran.us.r-project.org")
|
...
2d. Loading packages
Code Block |
---|
#### 2d. Loading required packages ####
# This section needs to be run every time
# Load packages
bioconductor_packages <- c("clusterProfiler", "pathview", "AnnotationHub", "org.Mm.eg.db")
cran_packages <- c("Seurat", "patchwork", "ggplot2", "tidyverse", "viridis", "plyr", "readxl", "scales")
lapply(cran_packages, require, character.only = TRUE)
lapply(bioconductor_packages, require, character.only = TRUE) |
...
2e. Select a sample to work with and import the data into R
Code Block |
---|
#### 2e. Choose a sample to work with and import the data for that sample into R #### # Give the sample name here that you want to work with. ## **USER INPUT** sample <- "Cerebellum" # To see the available samples (choose a sample name from this list): list.dirs(full.names = F, recursive = F) # Use Seurat's 'Read10X()' function to read in the full sample database. Cell Ranger creates 3 main database files that need to be combined into a single Seurat object. # Note: these datasets can be very large and take several minutes to import into R. They alsomat can use a lot of memory, so make sure your computer is up to the job (i.e. has at least 16GB of RAM) mat <- Read10X(data.dir = sample) # Have a look at the top 10 rows and columns to see if the data has been imported correctly<- Read10X(data.dir = paste0(sample, "/outs/filtered_feature_bc_matrix")) # Have a look at the top 10 rows and columns to see if the data has been imported correctly. You should see gene IDs as rows and barcodes (i.e. cells) as columns as.matrix(mat[1:10, 1:10]) # Now convert this to a Seurat object. Again, this may take several minutes to load and use a lot of memory mat2 <- CreateSeuratObject(counts = mat, project = sample) # You can see a summary of the data by simply running the Seurat object name mat2 # Set a colour palette that can contrast multiple clusters when you plot them. # You can change these colours as you like. # You can see what R colours are available here: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf c25 <- c("dodgerblue2", "#E31A1C", "green4", "#6A3D9A", "#FF7F00", "black", "gold1", "skyblue2", "#FB9A99", "palegreen2", "#CAB2D6", "#FDBF6F", "gray70", "khaki2", "maroon", "orchid1", "deeppink1", "blue1", "steelblue4", "darkturquoise", "green1", "yellow4", "yellow3", "darkorange4", "brown") |
2f. Idenitify markers in cells
Code Block |
---|
#### 2f. Idenitify markers in cells ####
# Create a vector called 'markers' that contains each of the markers you want to examine. These should be gene symbols. Replace the gene symbols below with your target markers.
## **USER INPUT**
markers <- c("P2ry12", "Tmem119", "Itgam")
# IMPORTANT: Note that the gene symbols have to exactly match the gene symbols in your dataset (including capitalisation). Gene symbols are more like 'common names' and can vary between databases. Your main gene identifiers are Ensembl IDs and we need to find the gene symbols that match these Ensembl IDs. For example, P2ry12 is also called ADPG-R, BDPLT8, HORK3 and various other gene symbols, depending on the database it's listed in. In the Ensemble database it's listed as P2ry12 (not P2RY12, remember, case matters) and matches Ensembl ID ENSMUSG00000036353.
# For this reason it's advisable to first search the Ensembl website for your markers of interest and for your organism, to ensure you are providing gene symbols that match the Ensembl IDs. https://asia.ensembl.org/Mus_musculus/Info/Index
# You can see if the markers you provided are present:
sum(row.names(mat) %in% markers)
# If you input 3 markers and the output from the above code = 3, then all are present. If the result is 2 then 2 of the 3 markers you provided are found in your data, etc.
## USER INPUT
# You can see if an individual marker is present like so (substitute for a marker of choice):
sum(row.names(mat) == "P2ry12")
# Outputs 1 if the marker is present, 0 if it isn't
# Pull out just the read counts for your defined markers
y <- mat[row.names(mat) %in% markers, ]
# Now we can count the number of cells containing zero transcripts for each of the examined markers. This enables an examination of the number of cells that have zero expression for these markers and therefore the number of cells that can be considered non-target cells.
# First count all cells
# Then make a loop to cycle through all markers (defined in previously created 'markers' vector)
a <- length(colnames(y))
for (i in 1:length(markers)) {
a <- c(a, sum(y[i,] == 0))
}
# Do a sum of the columns
y2 <- colSums(y)
# See if any zeros. If so, these cells are not target cells (as determined by absence of any target cell markers)
count <- c(a, sum(y2 == 0))
# Name the vector elements
names(count) <- c("Total_cells", markers, "All_zero")
# Generate the table
as.data.frame(count)
# The above table shows the total number of cells for your sample, the number of cells which had 0 expression for each marker, and the number of cell that had zero expression for all of the markers you provided.
|