-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathggrasp.R
executable file
·444 lines (385 loc) · 13.4 KB
/
ggrasp.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env Rscript
###############################################################################
# #
# Copyright (c) 2017 J. Craig Venter Institute. #
# All rights reserved. #
# #
###############################################################################
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###############################################################################
###############################################################################
library(getopt); #reading in parameters
library(ggplot2);
library(ggrasp);
library(ape)
library(methods);
#copied from sample_medioids.R
params=c(
"input", "i", 1, "character",
"type", "t", 2, "character",
"output", "o", 1, "character",
"method", "m", 2, "character",
"offset", "d", 2, "numeric", #optional conversion of similarity to distance matrix using offset provided
"verbose", "v", 2, "character",
"layout", "l", 2, "character",
"names", "n", 2, "character",
"clusters", "c", 2, "character",
"ranks", "r", 2, "character",
"threshold", "h", 2, "character",
"keep", "k", 2, "numeric",
"zscore", "z", 2, "character",
"minlambda", "x", 2, "character",
"metainfo", "a", 2, "character",
"start","s", 2, "numeric",
"end","e", 2, "numeric",
"package","p", 2, "character",
"writetable", "0", "2", "numeric",
"writepseudo", "1", "2", "numeric",
"writetree", "2", "2", "numeric",
"writetrimtree", "3", "2", "numeric",
"writeitol", "4", "2", "numeric",
"plothist", "5", "2", "numeric",
"plotgmm", "6", "2", "numeric",
"plottree", "7", "2", "numeric",
"plotclust", "8", "2", "numeric",
"plottrim", "9", "2", "numeric",
"plottype", "y", "2", "character"
)
opt=getopt(spec=matrix(params, ncol=4, byrow=TRUE))
script_name=unlist(strsplit(commandArgs(FALSE)[4],"=")[1])[2]
usage = paste (
"\nUsage:\n\n", script_name, "\n\n",
"\n# Input Variables\n",
"\t-i, --input <input file name for tab delimited distance matrix with row and column headers, newick file, or aligned multiple fasta file>\n",
"\t-t, --type <type of the tree format to makes. Default is 'complete'>\n",
"\t-d, --offset <optional offset to convert a similarity matrix to a distance matrix>\n",
"\t-m, --method <optional method to use for hclust such as complete, single or average or nj to perform neighbor-joining>\n",
"\t-v, --verbose <optional verbal threshold: 1 = no output, 2 = text only, 3 = graphics in pdf. Default = 3>\n",
"\t-n, --names <optional name file to label the exterior of the tree. Multiple files can be included with a comma between>\n",
"\t-r, --ranks <optional input file with ranks of the genes for medoid selection. Ranks should be from 1 (best) to n (worst) in two columns with the genome ID in the first>\n",
"\n# Clustering Variables\n",
"\t-c, --clusters <optional number of clusters to generate>\n",
"\t-h, --threshold <optional threshold to use to cluster the genomes>\n",
"\t-k, --keep <optional flag to keep all reference clusters as sub-and show subclusters>\n",
"\t-z, --zscore <optional value to combine all gaussian mixtures within this z-score. Default = 1. Use 0 to turn off>\n",
"\t-a, --metadata <optional input file containing metadata for the genomes. It should be tab-delineated>\n",
"\t-x, --minlambda <optional value used to discard a Gaussian distribution if it has less than this percentage of the total distribution. Default is 0.005>\n",
"\t-s, --start <optional value with the minimum number of gmm to examine. Default is 2>\n",
"\t-e, --end <optional value with the maximum number of gmm to examine. Default is 10>\n",
"\t-p, --packages <optional value with the package to use for the Gaussian mixture model estimate. Default is bgmm.\n",
"\n# Output Variables\n",
"\t-o, --output <output file name for html table, pdf plots, cluster and txt files>\n",
"\t-l, --layout <optional phylogeny layout such as rectangular, circular, radial and slanted. Default = circular>\n",
"\t-0, --writetable <optional flag used to save the cluster table>\n",
"\t-1, --writepseudo <optional flag used to save the pseudo-fasta list>\n",
"\t-2, --writetree <optional flag used to save the cluster table>\n",
"\t-3, --writetree <optional flag used to save the cluster table>\n",
"\t-4, --writetree <optional flag used to save the cluster table>\n",
"\t-5, --plothist <optional flag used to save the histogram with the plot>\n",
"\t-6, --plotgmm <optional flag used to save the histogram with the Gaussian distribution overlayed>\n",
"\t-7, --plottree <optional flag used to save the full tree plot>\n",
"\t-8, --plotclust <optional flag used to save the full tree plot with the clusters highlighted>\n",
"\t-9, --plottrim <optional flag used to save the trimmed tree with only the medoids>\n",
"\t-y, --plottype <optional flag used to provide the plot format>\n",
"This script will select sample genome medoids by selecting a threshold using Gaussian Mixture Models.\n",
"\n",
"\n");
if(!length(opt$input))
{
cat(usage)
cat(length(opt))
q(status=-1)
}
if (!length(opt$output))
{
cat("No output file name inputed. Using default \"output\"\n\n");
OutputFileName = "Output";
}else
{
OutputFileName <- opt$output;
}
meta.info = "";
if (!is.null(opt$metainfo))
{
if (file.exists(opt$metainfo))
{
meta.info <- opt$metainfo;
}else
{
cat(paste("\nCannot find metainfo file ", opt$metainfo, ". Skipping this step\n\n", sep=""));
}
}
medioid_run_str = "";
dist_con <- opt$offset;
in.file = opt$input;
in.format = "";
if (!(is.null(opt$type)))
{
in.format = opt$type;
if (!(tolower(opt$type) %in% c("matrix", "tree", "fasta")))
{
cat(paste("Format ", in.format, " is not recognized. Allowing program to select...\n", sep=""));
in.format = "";
}
}
verbosity = 3;
verb_lev_str = c("No text or PDF", "Text Only", "Both Text and PDF Graphics")
z.limit.input = 1.0;
min.lambda = 0.005;
if (!is.null(opt$z.remove))
{
z.limit.input = as.numeric(opt$zscore);
}
if (!is.null(opt$min.lambda))
{
min.lambda = as.numeric(opt$minlambda);
}
if (!is.null(opt$verbosity)) {
if (is.integer(as.integer(opt$verbosity)))
{
verbosity = as.integer(opt$verbosity);
if (verbosity >0 & verbosity < 4)
{
cat("\nSetting output verbosity to", verb_lev_str[verbosity], "\n\n")
}else
{
varbosity = 3;
cat("\nIncorrected Verbosity setting (1,2,3 accepted.) Output verbosity remains at", verb_lev_str[verbosity], "\n\n")
}
}else
{
cat("\nIncorrected Verbosity setting (1,2,3 accepted.) Output verbosity remains at", verb_lev_str[verbosity], "\n\n")
}
}
offset.val = 0
if (!is.null(opt$offset)) {
if (verbosity > 1) {
cat("\nConverting similarity matrix to distance matrix using threshold: ", opt$offset, "\n\n"); }
offset.val = opt$offset
}
accept_hclust <- c("complete", "average", "single", "nj")
hclust_m = "complete"
if (!is.null(opt$method))
{
if (opt$method %in% accept_hclust)
{
hclust_m <- opt$method;
if (verbosity > 1)
{
cat("\nUsing", hclust_m,"to make phylogeny\n\n");
}
}else
{
cat("\nSuggested unacceptible phylogenetic method...\n\nQuiting..\n");
q()
}
}
RankNameFile = "";
if (!is.null(opt$ranks))
{
RankNameFile = opt$ranks;
}
AnnotFile = meta.info;
if (!is.null(opt$offset)) {
gg.1 <- ggrasp.load(file=in.file, file.format=in.format, offset=as.numeric(offset.val), tree.method=hclust_m, rank.file=RankNameFile)#, annotation=meta.info)
}else
{
gg.1 <- ggrasp.load(file=in.file, file.format=in.format, tree.method=hclust_m, rank.file=RankNameFile) #, annotation=meta.info)
}
if(is.null(opt$input_newick_file))
{
write(gg.1@phy, file=paste(OutputFileName, "tree", "nwk", sep="."))
}
layout = "circular";
if (!is.null(opt$layout)) {
layout = opt$layout;
if (verbosity > 1) {
cat("\nSetting phylogeny plot layout to: ", layout, "\n\n"); }
}
if (!is.null(opt$names))
{
name_file <- opt$names;
name_file = InputNameFile;
if (verbosity > 1)
{
if (grepl(",", name_file))
{
cat("\nAdding files ", name_file, " to label exterior of tree\n\n")
}else
{
cat("\nAdding file ", name_file, " to label exterior of tree\n\n")
}
}
}
accept.package = c("bgmm","mixtools")
run.type.in = "bgmm"
if(!is.null(opt$package))
{
if (!(opt$package %in% accept.package))
{
cat(paste(opt$package, " is not found. Default bgmm is being used...\n\n", sep=""));
}else
{
run.type.in = opt$package;
}
}
g.start = 2;
g.end = 10;
if (!is.null(opt$start))
{
g.start = opt$start;
}
if (!is.null(opt$end))
{
g.end = opt$end;
}
z.score = 1;
if (!is.null(opt$zscore))
{
z.score = opt$zscore;
}
if(is.null(opt$clusters) && is.null(opt$threshold))
{
gg.2 <- ggrasp.cluster(gg.1, gmm.start=g.start, gmm.max=g.end, z.limit=z.score, run.type=run.type.in)
if (verbosity > 1)
{
cat(paste("\n", as.character(length(gg.2@gmm$mu))," Gaussian Mixture Models identified...\n\n", sep=""));
cat(paste("\nMedoid Threshold set at ", as.character(gg.2@h),"\n\nClustering the Genomes with threshold\n\n", sep=""));
}
}else
{
if (is.null(opt$threshold))
{
if (verbosity > 1)
{
cat(paste("Getting the top ", opt$cluster, " clades by walking down the tree..\n\n"));
}
clusters = as.numeric(opt$cluster)
gg.2 <- ggrasp.cluster(gg.1, num.clusters = clusters)
if (verbosity > 1)
{
cat(paste("\nMedoid Threshold set at ", as.character(gg.2@h),"\n\nClustering the Genomes with threshold\n\n", sep=""));
}
}else
{
thrsh = as.numeric(opt$threshold);
gg.2 <- ggrasp.cluster(gg.1, threshold = thrsh)
if (verbosity > 1)
{
cat(paste(as.character(max(gg.2@cluster)), " clusters made using threshold of " , thrsh,"\n\n", sep=""));
}
}
}
plottype = "pdf"
if (!is.null(opt$plottype))
{
opt$plottype = tolower(opt$plottype);
if (!(opt$plottype %in% c("pdf", "png")))
{
cat(paste(opt$plottype, " is not recognized. Default value is being used"));
}
else
{
plottype = opt$plottype;
}
cat(plottype)
}
if(!is.null(opt$writetable))
{
if (opt$writetable == TRUE)
{
cat("Here\n");
opt$writetable = 0;
}
file.name <- paste(OutputFileName, "table", "txt", sep=".");
ggrasp.write(x = gg.2, type = "table", rank.level = opt$writetable, file=file.name);
}
if(!is.null(opt$writetree))
{
if (opt$writetree == TRUE)
{
opt$writetree = 0;
}
file.name <- paste(OutputFileName, "tree", "txt", sep=".");
ggrasp.write(x = gg.1, type = "tree", file=file.name);
}
if(!is.null(opt$writetrimtree))
{
if (opt$writetrimtree == TRUE)
{
opt$writetrimtree = 0;
}
file.name <- paste(OutputFileName, "tree", "trim", "txt", sep=".");
ggrasp.write(x = gg.2, type = "trimmed", rank.level = opt$writetrimtree, file=file.name);
}
if(!is.null(opt$writeitol))
{
if (opt$writeitol == TRUE)
{
opt$writeitol = 0;
}
file.name <- paste(OutputFileName, "itol", "txt", sep=".");
ggrasp.write(gg.2, "itol", file=file.name);
}
if(!is.null(opt$writepseudo))
{
if (opt$writepseudo == TRUE)
{
opt$writepseudo = 0;
}
file.name <- paste(OutputFileName, "pfasta", "txt", sep=".");
ggrasp.write(gg.2, type = "list", rank.level = opt$writetable, file= file.name);
}
file.name <- paste(OutputFileName, "medoids", "txt", sep=".");
write(gg.2@medoids, file.name);
if(!is.null(opt$plothist))
{
if (opt$plothist == TRUE)
{
opt$plothist = 0;
}
cat(paste("Plotting Histogram\n\n"));
file.name <- paste(OutputFileName, "hist", plottype, sep=".");
ggsave(plot(gg.2, "histogram"), file = file.name, device=plottype);
}
if(!is.null(opt$plottree))
{
cat(paste("Plotting Tree\n\n"));
file.name <- paste(OutputFileName, "tree", plottype, sep=".");
ggsave(plot(gg.1, "tree"), file = file.name, device=plottype);
}
if(!is.null(opt$plotclust))
{
cat(paste("Plotting Cluster Tree\n\n"));
file.name <- paste(OutputFileName, "clusttree", plottype, sep=".");
ggsave(plot(gg.2, "tree"), file = file.name, device=plottype);
}
if(!is.null(opt$plotgmm))
{
cat(paste("Plotting GMM\n\n"));
file.name <- paste(OutputFileName, "gmm", plottype, sep=".");
ggsave(plot(gg.2, "gmm"), file = file.name, device=plottype);
}
if(!is.null(opt$plottrim))
{
cat(paste("Plotting Trimmed Tree\n\n"));
file.name <- paste(OutputFileName, "trimmed", plottype, sep=".");
ggsave(plot(gg.2, "trimmed"), file = file.name, device=plottype);
}
if (verbosity > 1)
{
cat(paste("Finished\n\n\n"));
}