Mercurial > repos > iuc > scater_filter
annotate scater-plot-dist-scatter.R @ 1:b7ea9f09c02f 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:27:39 -0400 |
parents | e6ca62ac65c6 |
children | 7a365ec81b52 |
rev | line source |
---|---|
0
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
1 #!/usr/bin/env Rscript |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
2 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
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 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
4 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
5 # Load optparse we need to check inputs |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
6 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
7 library(optparse) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
8 library(workflowscriptscommon) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
9 library(LoomExperiment) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
10 library(scater) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
11 library(ggpubr) |
1
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
12 library(scales) |
0
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
13 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
14 # parse options |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
15 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
16 option_list = list( |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
17 make_option( |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
18 c("-i", "--input-loom"), |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
19 action = "store", |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
20 default = NA, |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
21 type = 'character', |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
22 help = "A SingleCellExperiment object file in Loom format." |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
23 ), |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
24 make_option( |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
25 c("-o", "--output-plot-file"), |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
26 action = "store", |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
27 default = NA, |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
28 type = 'character', |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
29 help = "Path of the PDF output file to save plot to." |
1
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
30 ), |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
31 make_option( |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
32 c("-l", "--log-scale"), |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
33 action="store_true", |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
34 default=FALSE, |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
35 type = 'logical', |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
36 help = "Plot on log scale (recommended for large datasets)." |
0
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
37 ) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
38 ) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
39 |
1
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
40 opt <- wsc_parse_args(option_list, mandatory = c('input_loom', 'output_plot_file', 'log_scale')) |
0
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
41 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
42 # Check parameter values |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
43 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
44 if ( ! file.exists(opt$input_loom)){ |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
45 stop((paste('File', opt$input_loom, 'does not exist'))) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
46 } |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
47 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
48 # Input from Loom format |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
49 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
50 scle <- import(opt$input_loom, format='loom', type='SingleCellLoomExperiment') |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
51 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
52 #do the scatter plot of reads vs genes |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
53 total_counts <- scle$total_counts |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
54 total_features <- scle$total_features_by_counts |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
55 count_feats <- cbind(total_counts, total_features) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
56 cf_dm <- as.data.frame(count_feats) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
57 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
58 # Calculate binwidths for reads and features plots. Use 20 bins |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
59 read_bins <- max(total_counts / 1e6) / 20 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
60 feat_bins <- max(total_features) / 20 |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
61 |
1
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
62 # Make the plots |
0
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
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)") + |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
64 ylab("Feature count") + ggtitle("Scatterplot of reads vs features") |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
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") |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
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") |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
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)) |
e6ca62ac65c6
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 5fdcafccb6c645d301db040dfeed693d7b6b4278
iuc
parents:
diff
changeset
|
68 |
1
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
69 if (! opt$log_scale){ |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
70 final_plot <- ggarrange(plot1, plot2, plot, plot3, ncol=2, nrow=2) |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
71 ggsave(opt$output_plot_file, final_plot, device="pdf") |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
72 } else { |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
73 plot_log_both <- plot + scale_x_continuous(trans = 'log10') + scale_y_continuous(trans = 'log10') |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
74 plot1_log <- plot1 + scale_y_continuous(trans = 'log10') |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
75 plot2_log <- plot2 + scale_y_continuous(trans = 'log10') |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
76 plot3_log <- plot3 + scale_y_log10(labels=number) |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
77 final_plot_log <- ggarrange(plot1_log, plot2_log, plot_log_both, plot3_log, ncol=2, nrow=2) |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
78 ggsave(opt$output_plot_file, final_plot_log, device="pdf") |
b7ea9f09c02f
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/scater commit 61f3899168453092fd25691cf31871a3a350fd3b"
iuc
parents:
0
diff
changeset
|
79 } |