annotate scater-plot-dist-scatter.R @ 0:a30f4bfe8f01 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
author iuc
date Tue, 03 Sep 2019 14:30:21 -0400
parents
children 2b09ca1c5e41
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
1 #!/usr/bin/env Rscript
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
2
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
3 # Plot the distribution of read counts and feature counts, side by side, then a scatter plot of read counts vs feature counts below
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
4
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
5 # Load optparse we need to check inputs
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
6
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
7 library(optparse)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
8 library(workflowscriptscommon)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
9 library(LoomExperiment)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
10 library(scater)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
11 library(ggpubr)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
12 library(scales)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
13
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
14 # parse options
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
15
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
16 option_list = list(
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
17 make_option(
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
18 c("-i", "--input-loom"),
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
19 action = "store",
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
20 default = NA,
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
21 type = 'character',
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
22 help = "A SingleCellExperiment object file in Loom format."
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
23 ),
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
24 make_option(
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
25 c("-o", "--output-plot-file"),
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
26 action = "store",
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
27 default = NA,
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
28 type = 'character',
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
29 help = "Path of the PDF output file to save plot to."
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
30 ),
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
31 make_option(
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
32 c("-l", "--log-scale"),
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
33 action="store_true",
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
34 default=FALSE,
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
35 type = 'logical',
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
36 help = "Plot on log scale (recommended for large datasets)."
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
37 )
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
38 )
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
39
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
40 opt <- wsc_parse_args(option_list, mandatory = c('input_loom', 'output_plot_file', 'log_scale'))
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
41
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
42 # Check parameter values
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
43
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
44 if ( ! file.exists(opt$input_loom)){
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
45 stop((paste('File', opt$input_loom, 'does not exist')))
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
46 }
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
47
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
48 # Input from Loom format
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
49
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
50 scle <- import(opt$input_loom, format='loom', type='SingleCellLoomExperiment')
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
51
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
52 #do the scatter plot of reads vs genes
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
53 total_counts <- scle$total_counts
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
54 total_features <- scle$total_features_by_counts
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
55 count_feats <- cbind(total_counts, total_features)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
56 cf_dm <- as.data.frame(count_feats)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
57
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
58 # Calculate binwidths for reads and features plots. Use 20 bins
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
59 read_bins <- max(total_counts / 1e6) / 20
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
60 feat_bins <- max(total_features) / 20
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
61
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
62 # Make the plots
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
63 plot <- ggplot(cf_dm, aes(x=total_counts / 1e6, y=total_features)) + geom_point(shape=1) + geom_smooth() + xlab("Read count (millions)") +
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
64 ylab("Feature count") + ggtitle("Scatterplot of reads vs features")
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
65 plot1 <- qplot(total_counts / 1e6, geom="histogram", binwidth = read_bins, ylab="Number of cells", xlab = "Read counts (millions)", fill=I("darkseagreen3")) + ggtitle("Read counts per cell")
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
66 plot2 <- qplot(total_features, geom="histogram", binwidth = feat_bins, ylab="Number of cells", xlab = "Feature counts", fill=I("darkseagreen3")) + ggtitle("Feature counts per cell")
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
67 plot3 <- plotColData(scle, y="pct_counts_MT", x="total_features_by_counts") + ggtitle("% MT genes") + geom_point(shape=1) + theme(text = element_text(size=15)) + theme(plot.title = element_text(size=15))
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
68
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
69 if (! opt$log_scale){
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
70 final_plot <- ggarrange(plot1, plot2, plot, plot3, ncol=2, nrow=2)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
71 ggsave(opt$output_plot_file, final_plot, device="pdf")
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
72 } else {
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
73 plot_log_both <- plot + scale_x_continuous(trans = 'log10') + scale_y_continuous(trans = 'log10')
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
74 plot1_log <- plot1 + scale_y_continuous(trans = 'log10')
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
75 plot2_log <- plot2 + scale_y_continuous(trans = 'log10')
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
76 plot3_log <- plot3 + scale_y_log10(labels=number)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
77 final_plot_log <- ggarrange(plot1_log, plot2_log, plot_log_both, plot3_log, ncol=2, nrow=2)
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
78 ggsave(opt$output_plot_file, final_plot_log, device="pdf")
a30f4bfe8f01 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
diff changeset
79 }