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.
...
Code Block |
---|
#### 2m(ii). Calculate the clusters #### # First you need to re-run the variable feature calculation, scaling, dimensionality reduction (PCA, t-SNE and UMAP). # This may take several minutes to run. mat3_filt <- FindVariableFeatures(mat3_filt, selection.method = "vst", nfeatures = nrow(mat3_filt)) all.genes <- rownames(mat3_filt) mat3_filt <- ScaleData(mat3_filt, features = all.genes) mat3_filt <- RunPCA(mat3_filt, dims = 1:3, verbose = F) mat3_filt <- RunUMAP(mat3_filt, dims = 1:3, verbose = F) mat3_filt <- RunTSNE(mat3_filt, dims = 1:3, verbose = F) # Then you generate a 'nearest neighbor' graph by calculating the neighborhood overlap (Jaccard index) # between every cell and identify clusters of cells based on shared nearest neighbor (SNN). mat3_filt <- FindNeighbors(mat3_filt, dims = 1:10) ## **USER INPUT** Enter a resolution based off the clustree plot mat3_filt <- FindClusters(mat3_filt, resolution = 0.6) # You can see how many cells there are per cluster like so: cellcount <- as.data.frame(table(mat3_filt@meta.data[4])) names(cellcount) <- c("Cluster", "Cell_count") cellcount |
...
Code Block |
---|
#### 2m(iii). Plot the results #### ## PCA Plot ## # Before filtration: p <- DimPlot(mat3, reduction = "pca", pt.size = 2, cols = c25) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # After filtration: p <- DimPlot(mat3_filt, reduction = "pca", pt.size = 2, cols = c25) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # Export as pdf and tiff tiff_exp <- paste0(sample, "_pca_filtered.tiff") ggsave(file = tiff_exp, dpi = 300, compression = "lzw", device = "tiff", plot = p, width = 20, height = 20, units = "cm") pdf_exp <- paste0(sample, "_pca_filtered.pdf") ggsave(file = pdf_exp, device = "pdf", plot = p, width = 20, height = 20, units = "cm") ## PCA: Plot one individual cluster ## # Here you can visualise a single cluster by colouring it a specific colour. # First define the colours, based on the cluster information in your seurat data. # You can change the background colours ("gray70") and the highlighted cluster colour ("red") to whatever you like. newcols <- rep("gray70", length(levels(mat3_filt))-1) newcols <- c(newcols, "red") # Enter which cluster you want to visualise in the `myclust` variable below. # This is based on the cluster numbers in the previous plot. ## **USER INPUT** myclust <- 3 # Plot the cluster, placing this cluster on top (the `order` parameter). p <- DimPlot(mat3_filt, reduction = "pca", pt.size = 2, cols = newcols, order = myclust) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # Export as a pdf and tiff tiff_exp <- paste0(sample, "_clust_", myclust, "_pca_filtered.tiff") ggsave(file = tiff_exp, dpi = 300, compression = "lzw", device = "tiff", plot = p, width = 20, height = 20, units = "cm") pdf_exp <- paste0(sample, "_clust_", myclust, "_pca_filtered.pdf") ggsave(file = pdf_exp, device = "pdf", plot = p, width = 20, height = 20, units = "cm") ## UMAP ## # Before filtration: p <- DimPlot(mat3, reduction = "umap", pt.size = 2, cols = c25) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=16), axis.text=element_text(size=14)) + labs(color="Cluster") p # After filtration: p <- DimPlot(mat3_filt, reduction = "umap", pt.size = 2, cols = c25) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # Export as a pdf and tiff tiff_exp <- paste0(sample, "_umap_filtered.tiff") ggsave(file = tiff_exp, dpi = 300, compression = "lzw", device = "tiff", plot = p, width = 20, height = 20, units = "cm") pdf_exp <- paste0(sample, "_umap_filtered.pdf") ggsave(file = pdf_exp, device = "pdf", plot = p, width = 20, height = 20, units = "cm") ## UMAP: Plot individual clusters ## # Select your colours newcols <- rep("gray70", length(levels(mat3_filt))-1) newcols <- c(newcols, "red") # Select your cluster and then plot it ## **USER INPUT** myclust <- 3 p <- DimPlot(mat3_filt, reduction = "umap", pt.size = 2, cols = newcols, order = myclust) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # Export as a pdf and tiff tiff_exp <- paste0(sample, "_clust_", myclust, "_umap_filtered.tiff") ggsave(file = tiff_exp, dpi = 300, compression = "lzw", device = "tiff", plot = p, width = 20, height = 20, units = "cm") pdf_exp <- paste0(sample, "_clust_", myclust, "_umap_filtered.pdf") ggsave(file = pdf_exp, device = "pdf", plot = p, width = 20, height = 20, units = "cm") ## t_SNE ## # Before filtration: p <- DimPlot(mat3, reduction = "tsne", pt.size = 2, cols = c25) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # After filtration: p <- DimPlot(mat3_filt, reduction = "tsne", pt.size = 2, cols = c25) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # Export as a pdf and tiff tiff_exp <- paste0(sample, "_tsne_filtered.tiff") ggsave(file = tiff_exp, dpi = 300, compression = "lzw", device = "tiff", plot = p, width = 20, height = 20, units = "cm") pdf_exp <- paste0(sample, "_tsne_filtered.pdf") ggsave(file = pdf_exp, device = "pdf", plot = p, width = 20, height = 20, units = "cm") ## t_SNE: Plot individual clusters ## # Select your colours newcols <- rep("gray70", length(levels(mat3_filt))-1) newcols <- c(newcols, "red") # Select your cluster and then plot it. myclust <- 3 p <- DimPlot(mat3_filt, reduction = "tsne", pt.size = 2, cols = newcols, order = myclust) + theme_bw() + theme(legend.title=element_text(size=14), legend.text = element_text(size = 14), axis.title=element_text(size=18), axis.text=element_text(size=14)) + labs(color="Cluster") p # Export as a pdf and tiff tiff_exp <- paste0(sample, "_clust_", myclust, "_tsne_filtered.tiff") ggsave(file = tiff_exp, dpi = 300, compression = "lzw", device = "tiff", plot = p, width = 20, height = 20, units = "cm") pdf_exp <- paste0(sample, "_clust_", myclust, "_tsne_filtered.pdf") ggsave(file = pdf_exp, device = "pdf", plot = p, width = 20, height = 20, units = "cm") ## Clusters - Heatmap of selected markers ## # Recall which markers you previously defined: markers # Before filtration: p <- DoHeatmap(mat3, features = markers, raster = T) + scale_fill_gradientn(colors = c("darkorange", "floralwhite", "dodgerblue4")) + theme(text = element_text(size = 16)) + labs(color = "Dose (mg)") p # After filtration: p <- DoHeatmap(mat3_filt, features = markers, raster = T) + scale_fill_gradientn(colors = c("darkorange", "floralwhite", "dodgerblue4")) + theme(text = element_text(size = 16)) + labs(color = "Dose (mg)") p # Export as a pdf and tiff tiff_exp <- paste0(sample, "_hmap_filtered.tiff") ggsave(file = tiff_exp, dpi = 300, compression = "lzw", device = "tiff", plot = p, width = 20, height = 20, units = "cm") pdf_exp <- paste0(sample, "_hmap_filtered.pdf") ggsave(file = pdf_exp, device = "pdf", plot = p, width = 20, height = 20, units = "cm") |