#!/bin/gawk -f # Produces a staggered representation of the Newick tree # Cam Webb BEGIN{ RS = "\x04"; ORS = ""; depth = -1 } { tree = $0; } END{ close(ARGV[1]); gsub(/[\t\n\ ]/,"",tree); # clear the file print "" > ARGV[1]; i = 1; while (i <= length(tree)) { if (substr(tree, i, 1) ~ /\(/) { depth++; print "\n" >> ARGV[1]; for (x = 0; x < depth; x++) {print " " >> ARGV[1]}; print "(" >> ARGV[1]; if (substr(tree, i+1, 1) !~ /\(/) { print "\n" >> ARGV[1]; for (x = 0; x <= depth; x++) {print " " >> ARGV[1]}; } i++; } else if (substr(tree, i, 1) ~ /\)/) { print "\n" >> ARGV[1]; for (x = 0; x < depth; x++) {print " " >> ARGV[1]}; print ")" >> ARGV[1]; depth--; i++; } else if ((substr(tree, i, 1) ~ /,/) && (substr(tree, i+1, 1) ~ /[A-Za-z]/)) { print ",\n" >> ARGV[1]; for (x = 0; x <= depth; x++) {print " " >> ARGV[1]}; i++; } else { print (substr(tree, i, 1)) >> ARGV[1]; i++; } } }