Mercurial > repos > artbio > cpm_tpm_rpk
comparison cpm_tpm_rpk.R @ 1:b74bab5157c4 draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/cpm_tpm_rpk commit d46436d5d73356c8803d6d97a110a2754e8a03fb
author | artbio |
---|---|
date | Tue, 05 Feb 2019 19:51:38 -0500 |
parents | 35d032c46a4e |
children | 563337e780ce |
comparison
equal
deleted
inserted
replaced
0:35d032c46a4e | 1:b74bab5157c4 |
---|---|
1 if (length(commandArgs(TRUE)) == 0) { | 1 if (length(commandArgs(TRUE)) == 0) { |
2 system("Rscript cpm_tpm_rpk.R -h", intern = F) | 2 system("Rscript cpm_tpm_rpk.R -h", intern = F) |
3 q("no") | 3 q("no") |
4 } | 4 } |
5 | 5 |
6 # Load necessary packages (install them if it's not the case) | 6 |
7 requiredPackages = c('optparse') | 7 # load packages that are provided in the conda env |
8 for (p in requiredPackages) { | 8 options( show.error.messages=F, |
9 if (!require(p, character.only = TRUE, quietly = T)) { | 9 error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } ) |
10 install.packages(p) | 10 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") |
11 } | 11 warnings() |
12 suppressPackageStartupMessages(suppressMessages(library(p, character.only = TRUE))) | 12 library(optparse) |
13 } | 13 library(ggplot2) |
14 library(reshape2) | |
15 library(Rtsne) | |
16 | |
14 | 17 |
15 | 18 |
16 #Arguments | 19 #Arguments |
17 option_list = list( | 20 option_list = list( |
18 make_option( | 21 make_option( |
66 make_option( | 69 make_option( |
67 c("-o", "--out"), | 70 c("-o", "--out"), |
68 default = "res.tab", | 71 default = "res.tab", |
69 type = 'character', | 72 type = 'character', |
70 help = "Output name [default : '%default' ]" | 73 help = "Output name [default : '%default' ]" |
74 ), | |
75 make_option( | |
76 "--tsne", | |
77 default = FALSE, | |
78 type = 'logical', | |
79 help = "performs T-SNE [default : '%default' ]" | |
80 ), | |
81 make_option( | |
82 "--seed", | |
83 default = 42, | |
84 type = 'integer', | |
85 help = "Seed value for reproducibility [default : '%default' ]" | |
86 ), | |
87 make_option( | |
88 "--perp", | |
89 default = 5.0, | |
90 type = 'numeric', | |
91 help = "perplexity [default : '%default' ]" | |
92 ), | |
93 make_option( | |
94 "--theta", | |
95 default = 1.0, | |
96 type = 'numeric', | |
97 help = "theta [default : '%default' ]" | |
98 ), | |
99 make_option( | |
100 c("-D", "--tsne_out"), | |
101 default = "tsne.pdf", | |
102 type = 'character', | |
103 help = "T-SNE pdf [default : '%default' ]" | |
71 ) | 104 ) |
72 ) | 105 ) |
73 | |
74 | 106 |
75 opt = parse_args(OptionParser(option_list = option_list), | 107 opt = parse_args(OptionParser(option_list = option_list), |
76 args = commandArgs(trailingOnly = TRUE)) | 108 args = commandArgs(trailingOnly = TRUE)) |
77 | 109 |
78 if (opt$data == "" & !(opt$help)) { | 110 if (opt$data == "" & !(opt$help)) { |
105 return(TPM) | 137 return(TPM) |
106 } | 138 } |
107 | 139 |
108 data = read.table( | 140 data = read.table( |
109 opt$data, | 141 opt$data, |
110 h = opt$colnames, | 142 header = opt$colnames, |
111 row.names = 1, | 143 row.names = 1, |
112 sep = opt$sep | 144 sep = opt$sep |
113 ) | 145 ) |
114 | 146 |
115 if (opt$type == "tpm" | opt$type == "rpk") { | 147 if (opt$type == "tpm" | opt$type == "rpk") { |
145 row.names = F, | 177 row.names = F, |
146 quote = F, | 178 quote = F, |
147 sep = "\t" | 179 sep = "\t" |
148 ) | 180 ) |
149 | 181 |
182 ## | |
183 if (opt$tsne == TRUE) { | |
184 df = cpm(data) | |
185 # filter and transpose df for tsne | |
186 df = df[rowSums(df) != 0,] # remove lines without information (with only 0 counts) | |
187 tdf = t(df) | |
188 tdf = log2(tdf + 1) | |
189 # make tsne and plot results | |
190 set.seed(opt$seed) ## Sets seed for reproducibility | |
191 # Run TSNE | |
192 tsne_out <- Rtsne(tdf, perplexity=opt$perp, theta=opt$theta) # | |
193 embedding <- as.data.frame(tsne_out$Y) | |
194 embedding$Class <- as.factor(sub("Class_", "", rownames(tdf))) | |
195 gg_legend = theme(legend.position="none") | |
196 ggplot(embedding, aes(x=V1, y=V2)) + | |
197 geom_point(size=1.25, color='red') + | |
198 geom_text(aes(label=Class),hjust=-0.2, vjust=-0.5, size=2.5, color='darkblue') + | |
199 gg_legend + | |
200 xlab("") + | |
201 ylab("") + | |
202 ggtitle('t-SNE of data (log2CPM transformed)') | |
203 ggsave(file=opt$tsne_out, device="pdf") | |
204 } | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 |