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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
# File 'lib/iroki/main/main.rb', line 179
def self.main(color_branches: nil,
color_taxa_names: nil,
exact: nil,
remove_bootstraps_below: nil,
color_map_f: nil,
biom_f: nil,
single_color: nil,
name_map_f: nil,
auto_color: nil,
display_auto_color_options: nil,
newick_f: nil,
out_f: nil,
default_color: "black",
min_lumin: 35,
max_lumin: 70)
args = method(__method__).parameters.map { |arg| arg[1] }
AbortIf::logger.info "Args " + args.map { |arg| "#{arg} = #{eval(arg.to_s).inspect}" }.join(', ')
if display_auto_color_options
STDERR.puts "\n Choices for --auto-color ..."
STDERR.print " - kelly: up to 19 high contrast colors\n\n#{Iroki::Color::Palette::KELLY_TEXT}\n\n"
exit
end
auto_color_options = ["kelly"]
abort_if !auto_color.nil? && !auto_color_options.include?(auto_color),
"'#{auto_color}' is not a valid option. Try iroki --help for help."
case auto_color
when nil
auto_color_hash = nil
when "kelly"
auto_color_hash = Iroki::Color::Palette::KELLY
end
abort_if single_color && biom_f.nil?,
"--single-color was passed but no biom file was given"
abort_if (biom_f || color_map_f) && color_branches.nil? && color_taxa_names.nil?,
"No coloring options selected."
newick = check_file newick_f, :newick
abort_unless valid_newick?(newick_f),
"The Newick file doesn't look like a real Newick file"
abort_if out_f.nil?,
"--outfile is a required arg. Try iroki --help for help."
if color_branches || color_taxa_names
abort_if biom_f.nil? && color_map_f.nil?,
"Color options were provided, but no biom file " +
"or color map file was provided"
end
color_f = nil
if !biom_f && (color_taxa_names || color_branches)
color_f = check_file color_map_f, :color_map_f
end
check = color_map_f &&
!color_taxa_names &&
!color_branches
abort_if check,
"A color map file was provided without specifying " +
"any coloring options"
abort_if(newick_f &&
color_map_f.nil? &&
biom_f.nil? &&
name_map_f.nil?,
"Newick file was given but no other files were given")
abort_if biom_f && !exact,
"Regex matching cannot be used with a biom file. " +
"Pass the --exact flag"
str = File.read newick
newick = Bio::Newick.new str, parser: :iroki
if newick &&
newick.entry_overrun &&
!newick.entry_overrun.chomp.empty?
AbortIf.logger.warn {
"Extra info (#{newick.entry_overrun}) in newick file " +
"(#{newick_f}) will be ignored. " +
"Does your newick file contain multiple trees?"
}
end
tree = newick.tree
iroki_to_name = Iroki::Tree.change_names tree
if name_map_f
name_map = parse_name_map name_map_f
else
name_map = nil
end
if name_map_f && color_map_f.nil? && biom_f.nil?
AbortIf.logger.info "Only renaming was requested."
end
if biom_f
samples, counts, is_single_group = Biom.open(biom_f, "rt").parse
if is_single_group
biom_patterns = SingleGroupGradient.new(samples,
counts,
single_color,
min_lumin,
max_lumin).patterns
else
abort_if single_color,
"--single-color is not compatible with a two " +
"group biom file. Please remove that option."
g1_counts = counts.map(&:first)
g2_counts = counts.map(&:last)
biom_patterns = TwoGroupGradient.new(samples,
g1_counts,
g2_counts,
min_lumin,
max_lumin).patterns
end
end
if color_map_f && biom_f
color_map_patterns = parse_color_map_iroki color_map_f,
iroki_to_name,
exact_matching: exact,
auto_color: auto_color_hash
name_to_iroki = iroki_to_name.invert
biom_patterns_iroki_name = {}
biom_patterns.each do |name, val|
biom_patterns_iroki_name[name_to_iroki[name]] = val
end
patterns = biom_patterns_iroki_name.merge(color_map_patterns) do |key, oldval, newval|
new_label = if !newval[:label].nil? && !newval[:label].empty?
newval[:label]
else
oldval[:label]
end
new_branch = if !newval[:branch].nil? && !newval[:branch].empty?
newval[:branch]
else
oldval[:branch]
end
{ label: new_label, branch: new_branch }
end
elsif color_f
patterns = parse_color_map_iroki color_f,
iroki_to_name,
exact_matching: exact,
auto_color: auto_color_hash
elsif biom_f
patterns = biom_patterns
name_to_iroki = iroki_to_name.invert
h = {}
patterns.each do |name, val|
h[name_to_iroki[name]] = val
end
patterns = h
else
patterns = nil
end
if default_color && default_color != "black"
abort_if !default_color.hex? && !Iroki::Color::COLORS[default_color],
"--default-color must be a hex code (don't forget the '#' or a valid color. Got '#{default_color}'."
Iroki::Color::default_color_tag = { label: Iroki::Color.get_tag(default_color),
branch: Iroki::Color.get_tag(default_color), }
end
nil_val = { label: "", branch: "" }
leaves_with_names = tree.leaves.reject { |leaf| leaf.name.nil? }
if color_taxa_names
leaves = leaves_with_names.map do |n|
name = n.name
if (color = add_color_to_leaf_branch(patterns, name, exact, iroki_to_name))
name.single_quote + color[:label]
else
name.single_quote
end
end
else
leaves = leaves_with_names.map { |n| n.name.single_quote }
end
if color_branches
total = tree.nodes.count
n = 0
tree.collect_node! do |node|
n += 1
$stderr.printf "Node: %d of %d\r", n, total
color_nodes patterns, tree, node, exact, iroki_to_name
end
end
$stderr.puts
tre_str = tree.newick(indent: false)
silly_iroki_to_name = {}
iroki_to_name.each do |iname, oldname|
if name_map && name_map.has_key?(oldname)
silly_iroki_to_name[iname] = name_map[oldname]
else
silly_iroki_to_name[iname] = oldname
end
end
nexus = "#NEXUS
begin taxa;
dimensions ntax=#{leaves.count};
taxlabels
#{leaves.join("\n").gsub(/iroki[0-9]+iroki/, silly_iroki_to_name)}
;
end;
begin trees;
tree tree_1 = [&R] #{Iroki::Tree.gsub_iroki_newick_string tre_str, silly_iroki_to_name}
end;
#{FIG}"
File.open(out_f, "w") { |f| f.puts nexus }
nexus
end
|