| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: format-tree.pl | ||
| 3 | # ABSTRACT: Format (and annotate) trees for printing | ||
| 4 | # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be> | ||
| 5 | |||
| 6 | use Modern::Perl '2011'; | ||
| 7 | use autodie; | ||
| 8 | |||
| 9 | use Getopt::Euclid qw(:vars); | ||
| 10 | use List::AllUtils qw(pairfirst); | ||
| 11 | use Smart::Comments; | ||
| 12 | use Try::Tiny; | ||
| 13 | |||
| 14 | use Bio::MUST::Core; | ||
| 15 | use Bio::MUST::Core::Utils qw(:filenames secure_outfile); | ||
| 16 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 17 | use aliased 'Bio::MUST::Core::IdMapper'; | ||
| 18 | use aliased 'Bio::MUST::Core::SeqId'; | ||
| 19 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 20 | use aliased 'Bio::MUST::Core::Tree'; | ||
| 21 | |||
| 22 | # TODO: implement numbered taxonomic levels as in fetch-tax.pl | ||
| 23 | # check for conditionally required arguments | ||
| 24 | die <<'EOT' if !$ARGV_annotate && ($ARGV_collapse || $ARGV_colorize); | ||
| 25 | Missing required arguments: | ||
| 26 | --annotate=<level> | ||
| 27 | EOT | ||
| 28 | |||
| 29 | die <<'EOT' if !$ARGV_taxdir && ($ARGV_root_on_taxon || $ARGV_annotate || $ARGV_auto_final_ids); | ||
| 30 | Missing required arguments: | ||
| 31 | --taxdir=<dir> | ||
| 32 | EOT | ||
| 33 | |||
| 34 | # optionally read global org-mapper | ||
| 35 | my $org_mapper; | ||
| 36 | if ($ARGV_org_mapper) { | ||
| 37 | ### Mapping organisms from: $ARGV_org_mapper | ||
| 38 | $org_mapper = IdMapper->load($ARGV_org_mapper); | ||
| 39 | } | ||
| 40 | |||
| 41 | # optionally build taxonomy object | ||
| 42 | my $tax; | ||
| 43 | if ($ARGV_taxdir) { | ||
| 44 | ### Annotating trees using: $ARGV_taxdir | ||
| 45 | $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir ); | ||
| 46 | } | ||
| 47 | |||
| 48 | # setup potential rooting strategy | ||
| 49 | my $filter; | ||
| 50 | my ($component, $target) = pairfirst { $b } | ||
| 51 | map { $_ => $ARGV{"--root-on-$_"} } qw(taxon genus species family tag); | ||
| 52 | if ($component) { | ||
| 53 | ### Rooting strategy: "$component = $target" | ||
| 54 | $filter = $component eq 'taxon' ? $tax->tax_filter( ["+$target"] ) | ||
| 55 | : SeqId->${\ ($component . '_filter') }( ["+$target"] ) | ||
| 56 | ; | ||
| 57 | } | ||
| 58 | |||
| 59 | # setup collapsing and group naming | ||
| 60 | my $annotate_key; | ||
| 61 | my $collapse_key; | ||
| 62 | if ($ARGV_collapse && ($ARGV_collapse =~ m/label|color/xms)) { | ||
| 63 | $annotate_key = 'taxon_label' if $ARGV_annotate eq 'missing'; | ||
| 64 | $collapse_key = 'taxon_label' if $ARGV_collapse eq 'label'; | ||
| 65 | $collapse_key = '!color' if $ARGV_collapse eq 'color'; | ||
| 66 | $ARGV_collapse = 'no rank'; | ||
| 67 | } | ||
| 68 | $ARGV_annotate = 'no rank' if $ARGV_annotate && $ARGV_annotate eq 'missing'; | ||
| 69 | my %opts = (name => $ARGV_annotate); | ||
| 70 | $opts{ collapse} = $ARGV_collapse if $ARGV_collapse; | ||
| 71 | |||
| 72 | TREE: | ||
| 73 | for my $infile (@ARGV_infiles) { | ||
| 74 | |||
| 75 | ### Processing: $infile | ||
| 76 | my $tree; | ||
| 77 | try { $tree = Tree->load($infile) } | ||
| 78 | catch { warn "Warning: cannot load '$infile' as a Tree; skipping!\n" }; | ||
| 79 | next TREE unless $tree; | ||
| 80 | |||
| 81 | $infile =~ s/$_//xms for @ARGV_in_strip; | ||
| 82 | |||
| 83 | if ($ARGV_map_ids) { | ||
| 84 | my $idmfile = change_suffix($infile, '.idm'); | ||
| 85 | my $idm = IdMapper->load($idmfile); | ||
| 86 | ### Restoring seq ids from: $idmfile | ||
| 87 | |||
| 88 | if ($org_mapper) { # TODO: move into module for testing? | ||
| 89 | # build preliminary mapper from infile idm and global org_mapper | ||
| 90 | my $idl = IdList->new( ids => [ $idm->all_long_ids ] ); | ||
| 91 | my $pre_idm = $idl->org_mapper_from_abbr_ids($org_mapper); | ||
| 92 | # update infile mapper to replace abbr_ids by longer_ids | ||
| 93 | $idm = IdMapper->new( | ||
| 94 | long_ids => [ | ||
| 95 | map { $pre_idm->long_id_for($_) } $idm->all_long_ids | ||
| 96 | ], | ||
| 97 | abbr_ids => [ $idm->all_abbr_ids ], | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | $tree->restore_ids($idm); | ||
| 101 | } | ||
| 102 | |||
| 103 | if ($ARGV_from_consense) { | ||
| 104 | my $bl = $ARGV_arb ? 1 : undef; # treeplot wants branch lengths | ||
| 105 | $tree->switch_branch_lengths_and_labels_for_entities($bl); | ||
| 106 | } | ||
| 107 | |||
| 108 | if ($ARGV_ultrametrize) { | ||
| 109 | $tree->tree->chronompl; | ||
| 110 | } | ||
| 111 | |||
| 112 | if ($filter) { | ||
| 113 | ### Rooting on: $target | ||
| 114 | $tree->root_tree($filter, -1, 1); | ||
| 115 | # Note: rerooting must occur before propagating taxonomy to nodes | ||
| 116 | } | ||
| 117 | |||
| 118 | if ($ARGV_ladderize) { | ||
| 119 | $tree->tree->ladderize($ARGV_ladderize eq 'asc'); | ||
| 120 | } | ||
| 121 | |||
| 122 | if ($ARGV_annotate) { | ||
| 123 | $tax->attach_taxonomies_to_terminals($tree); | ||
| 124 | $tax->attach_taxonomies_to_internals($tree); | ||
| 125 | $tax->attach_taxa_to_entities($tree, \%opts); | ||
| 126 | |||
| 127 | if ($ARGV_colorize) { | ||
| 128 | ### Coloring tree using: $ARGV_colorize | ||
| 129 | my $scheme = $tax->load_color_scheme($ARGV_colorize); | ||
| 130 | $scheme->attach_colors_to_entities($tree); | ||
| 131 | } | ||
| 132 | |||
| 133 | if ($ARGV_collapse) { | ||
| 134 | $tree->collapse_subtrees($collapse_key); | ||
| 135 | } | ||
| 136 | |||
| 137 | # iTOL output | ||
| 138 | if ($ARGV_itol) { | ||
| 139 | $tree->store_itol_datasets( | ||
| 140 | insert_suffix($infile, $ARGV_out_suffix), $annotate_key | ||
| 141 | ); | ||
| 142 | } | ||
| 143 | |||
| 144 | # TRE or ARB output | ||
| 145 | unless ($ARGV_figtree || $ARGV_itol) { | ||
| 146 | $tree->switch_attributes_and_labels_for_internals('taxon'); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | if ($ARGV_map_final_ids) { | ||
| 151 | my $idmfile = change_suffix($infile, '.final-idm'); | ||
| 152 | my $idm = IdMapper->load($idmfile); | ||
| 153 | ### Final seq ids taken from: $idmfile | ||
| 154 | $tree->restore_ids($idm); | ||
| 155 | } | ||
| 156 | |||
| 157 | elsif ($ARGV_auto_final_ids) { | ||
| 158 | ### Building final ids using NCBI Taxonomy | ||
| 159 | my $idm = $tax->tax_mapper($tree, { append_acc => 1 } ); | ||
| 160 | $tree->restore_ids($idm); | ||
| 161 | } | ||
| 162 | |||
| 163 | # TODO: add PDF export | ||
| 164 | |||
| 165 | my $args; | ||
| 166 | |||
| 167 | my $outfile = change_suffix($infile, '.tre'); | ||
| 168 | my $store_method = 'store'; | ||
| 169 | if ($ARGV_arb) { | ||
| 170 | $outfile = change_suffix($infile, '.arb'); | ||
| 171 | $store_method = 'store_arb'; | ||
| 172 | $args = { alifile => change_suffix($infile, '.ali') }; | ||
| 173 | } elsif ($ARGV_figtree) { | ||
| 174 | $outfile = change_suffix($infile, '.nex'); | ||
| 175 | $store_method = 'store_figtree'; | ||
| 176 | } | ||
| 177 | $outfile = secure_outfile($outfile, $ARGV_out_suffix); | ||
| 178 | ### Output tree in: $outfile | ||
| 179 | $tree->$store_method($outfile, $args); | ||
| 180 | |||
| 181 | if ($ARGV_grp) { | ||
| 182 | my $grpfile = change_suffix($infile, '.grp'); | ||
| 183 | $grpfile = secure_outfile($grpfile, $ARGV_out_suffix); | ||
| 184 | $tree->store_grp($grpfile); | ||
| 185 | my $nbsfile = change_suffix($infile, '.nbs'); | ||
| 186 | $nbsfile = secure_outfile($nbsfile, $ARGV_out_suffix); | ||
| 187 | $tree->store_nbs($nbsfile); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | # TODO: include iqtree2mapper? | ||
| 192 | |||
| 193 | __END__ |