-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrajectory_analysis.R
119 lines (85 loc) · 3.8 KB
/
trajectory_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
install.packages("BiocManager")
BiocManager::install("Seurat")
library(Seurat)
library(optparse)
library(ggplot2)
# Set up argument parsing
option_list = list(
make_option(c("-d", "--data_path"), type = "character", default = NULL,
help = "Directory path to the Seurat data", metavar = "character"),
make_option(c("-o", "--output_dir"), type = "character", default = NULL,
help = "Directory path for output files", metavar = "character")
)
# Get the command line arguments
args <- commandArgs(trailingOnly = TRUE)
# Check if arguments are provided and update paths
if (length(args) >= 2) {
data_path <- args[1]
output_dir <- args[2]
}
# Print the arguments for debugging
print(paste("Data path:", data_path))
print(paste("Output directory:", output_dir))
# Load the data
seurat.data <- Read10X(data.dir = data_path)
# Create a Seurat object
seurat.obj <- CreateSeuratObject(counts = seurat.data, project = "SampleProject")
# Identify mitochondrial genes (adjust based on your dataset)
mito.genes <- grep("^MT-", rownames(seurat.obj), value = TRUE)
# Calculate the percentage of mitochondrial gene expression
seurat.obj[['percent.mt']] <- PercentageFeatureSet(seurat.obj, features = mito.genes)
# Basic filtering
seurat.obj <- subset(seurat.obj, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
# Normalization
seurat.obj <- NormalizeData(seurat.obj)
# Find highly variable features
seurat.obj <- FindVariableFeatures(seurat.obj, selection.method = "vst", nfeatures = 2000)
# Scale the data
seurat.obj <- ScaleData(seurat.obj)
# Perform PCA
seurat.obj <- RunPCA(seurat.obj, features = VariableFeatures(object = seurat.obj))
# Clustering
seurat.obj <- FindNeighbors(seurat.obj, dims = 1:10)
seurat.obj <- FindClusters(seurat.obj, resolution = 0.5)
# Perform t-SNE
seurat.obj <- RunTSNE(seurat.obj, dims = 1:10)
# Perform UMAP
seurat.obj <- RunUMAP(seurat.obj, dims = 1:10)
# Visualization and saving plots
# Plot t-SNE and save
tsne.plot <- DimPlot(seurat.obj, reduction = "tsne")
ggsave(file.path(output_dir, "tSNE_plot.pdf"), tsne.plot)
# Plot UMAP and save
umap.plot <- DimPlot(seurat.obj, reduction = "umap")
ggsave(file.path(output_dir, "UMAP_plot.pdf"), umap.plot)
# Get top 10 or 20 variable genes
top.genes <- head(VariableFeatures(seurat.obj), 20)
# Generate a Dot Plot for the top variable genes
dot.plot <- DotPlot(seurat.obj, features = top.genes) + RotatedAxis()
# Save the Dot Plot
ggsave(file.path(output_dir, "DotPlot.pdf"), plot = dot.plot, width = 10, height = 4)
# Generate a Violin Plot for each of the top variable genes
for (gene in top.genes) {
violin.plot <- VlnPlot(seurat.obj, features = gene, ncol = 1)
# Save the Violin Plot
ggsave(file.path(output_dir, paste0("ViolinPlot_", gene, ".pdf")), plot = violin.plot, width = 3, height = 4)
}
# Generate a Dot Plot for the top variable genes
dot.plot <- DotPlot(seurat.obj, features = top.genes) + RotatedAxis()
# Save the Dot Plot
ggsave(file.path(output_dir, "DotPlot.pdf"), plot = dot.plot, width = 10, height = 4)
# Plot PCA and save
pca.plot <- DimPlot(seurat.obj, reduction = "pca")
ggsave(file.path(output_dir, "PCA_plot.pdf"), pca.plot)
# Heatmap of top variable genes
heatmap.plot <- DoHeatmap(seurat.obj, features = top.genes)
ggsave(file.path(output_dir, "Heatmap.pdf"), heatmap.plot)
# Ridge plot for gene expression across clusters
ridge.plot <- RidgePlot(seurat.obj, features = top.genes, ncol = 2)
ggsave(file.path(output_dir, "RidgePlot.pdf"), ridge.plot)
# Feature plot for a specific gene
# Replace 'GeneName' with the gene you are interested in
feature.plot <- FeaturePlot(seurat.obj, features = top.genes)
ggsave(file.path(output_dir, "FeaturePlot_GeneName.pdf"), feature.plot)
# Optionally, you can also save the Seurat object for future use
saveRDS(seurat.obj, file.path(output_dir, "seurat_object.rds"))