| 1 | #!/usr/local/bin/perl -w | ||
| 2 | ###################################################### | ||
| 3 | # nextool.pl | ||
| 4 | ###################################################### | ||
| 5 | # | ||
| 6 | # Thanks to Peter Yang and Chengzhi Liang for the original version | ||
| 7 | # | ||
| 8 | # $Id: nextool.pl,v 1.42 2012/02/07 21:49:27 astoltzfus Exp $ | ||
| 9 | |||
| 10 | # this script provides a set of functions for manipulating NEXUS files | ||
| 11 | # eg, select/exclude/rename OTUs, select character columns in OTUs | ||
| 12 | # select/exclude subtree, select/reroot tree | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use Pod::Usage; | ||
| 16 | use Carp; | ||
| 17 | use Data::Dumper; | ||
| 18 | $Data::Dumper::Maxdepth=3; | ||
| 19 | use Bio::NEXUS; | ||
| 20 | use Bio::NEXUS::Functions; | ||
| 21 | |||
| 22 | my $infile = shift @ARGV; | ||
| 23 | unless ($infile) {die "Usage: nextool.pl <infile> [outfile] [command [options]]\n";} | ||
| 24 | |||
| 25 | if ($infile eq "-v") { | ||
| 26 | die "nextool.pl \$Revision: 1.42 $\n"; | ||
| 27 | } | ||
| 28 | if ($infile eq "-h" || $infile eq '--help') { | ||
| 29 | pod2usage(-exitval => 0, -verbose => 1); | ||
| 30 | } | ||
| 31 | |||
| 32 | my $outfile = shift @ARGV; | ||
| 33 | my $command = shift @ARGV; | ||
| 34 | #if ($outfile eq "-") {$outfile = \*STDOUT;} | ||
| 35 | if ($outfile =~ /(^-$|^STDOUT$)/) {$outfile = \*STDOUT;} | ||
| 36 | if (! $outfile || ! $command || $command eq "rewrite") { | ||
| 37 | my $nexus = new Bio::NEXUS($infile, 1); | ||
| 38 | $nexus->write($outfile||"temp.nex", 1); | ||
| 39 | exit; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $nexus = new Bio::NEXUS($infile); | ||
| 43 | |||
| 44 | if ($command eq "rename_otus") { | ||
| 45 | $nexus = rename_otus($nexus, @ARGV); | ||
| 46 | $nexus->write($outfile); | ||
| 47 | }elsif ($command eq "select") { | ||
| 48 | my $by = shift @ARGV; | ||
| 49 | if ($by =~ /^block/i) { $nexus = selectbyblock($nexus, @ARGV); } | ||
| 50 | elsif ($by =~ /^otu/i) { $nexus = selectbyotu($nexus, 1, @ARGV); } | ||
| 51 | elsif ($by =~ /^subtree/i) { $nexus = selectbyinode($nexus, @ARGV); } | ||
| 52 | elsif ($by =~ /^tree/i) { $nexus = selectbytree($nexus, @ARGV); } | ||
| 53 | elsif ($by =~ /^column/i) { $nexus = selectbycolumn($nexus, 1, @ARGV); } | ||
| 54 | elsif ($by =~ /^set/i) { $nexus = selectbysets($nexus, 1, @ARGV); } | ||
| 55 | $nexus->write($outfile); | ||
| 56 | }elsif ($command eq "exclude") { | ||
| 57 | my $by = shift @ARGV; | ||
| 58 | if ($by =~ /block/i) { $nexus = exclude_blocks($nexus, @ARGV); } | ||
| 59 | elsif ($by =~ /otu/i) { $nexus = selectbyotu($nexus, 0, @ARGV); } | ||
| 60 | elsif ($by =~ /subtree/i) { $nexus = exclude_subtree($nexus, @ARGV); } | ||
| 61 | elsif ($by =~ /^column/i) { $nexus = selectbycolumn($nexus, 0, @ARGV); } | ||
| 62 | elsif ($by =~ /^set/i) { $nexus = selectbysets($nexus, 0, @ARGV); } | ||
| 63 | $nexus->write($outfile); | ||
| 64 | }elsif ($command eq "reroot") { | ||
| 65 | $nexus = reroottree($nexus, @ARGV); | ||
| 66 | $nexus->write($outfile); | ||
| 67 | } elsif ($command eq "addtree") { | ||
| 68 | &addtree($nexus,@ARGV); | ||
| 69 | $nexus->write($outfile); | ||
| 70 | } elsif ($command =~ /^makesets?$/) { | ||
| 71 | my $by = shift @ARGV; | ||
| 72 | if ($by =~ /(?:file|^f$)/i) { $nexus = setsbyfile($nexus, @ARGV); } | ||
| 73 | elsif ($by =~ /(?:inode|^i$)/i) { $nexus = setsbyinode($nexus, @ARGV); } | ||
| 74 | elsif ($by =~ /(?:otu|^o$)/i) { $nexus = setsbyotus($nexus, @ARGV); } | ||
| 75 | elsif ($by =~ /(?:clade|^c$)/i) { $nexus = setsbyclade($nexus, @ARGV); } | ||
| 76 | elsif ($by =~ /(?:union|^u$)/i) { $nexus = setsbyunion($nexus, @ARGV); } | ||
| 77 | elsif ($by =~ /(?:difference|^d$)/i) { $nexus = setsbydifference($nexus, @ARGV); } | ||
| 78 | elsif ($by =~ /(?:charstate|^cs$)/i) { $nexus = setsbycharstate($nexus, @ARGV); } | ||
| 79 | elsif ($by =~ /(?:cladeconsensus|^cc$)/i) { $nexus = setsbycladeconsensus($nexus, @ARGV); } | ||
| 80 | else {die "Expecting command that looks like 'makesets <bymode> <arguments>', where bymode is one of: file, inode, otu, clade, union, difference, charstate, cladeconsensus\n"}; | ||
| 81 | $nexus->write($outfile); | ||
| 82 | } elsif ($command =~ /^removesets?$/) { | ||
| 83 | $nexus = removesets($nexus, @ARGV); | ||
| 84 | $nexus->write($outfile); | ||
| 85 | } elsif ($command =~ /^renamesets?$/) { | ||
| 86 | $nexus = renamesets($nexus, @ARGV); | ||
| 87 | $nexus->write($outfile); | ||
| 88 | } elsif ($command =~ /^listsets?$/) { | ||
| 89 | file_overwrite_warning($infile, $outfile, $command, 0); | ||
| 90 | listsets($nexus, $outfile, @ARGV); | ||
| 91 | } elsif ($command =~ /^listsetnames?$/) { | ||
| 92 | file_overwrite_warning($infile, $outfile, $command, 0); | ||
| 93 | listsetnames($nexus, $outfile); | ||
| 94 | } else { | ||
| 95 | die "unknown command: $command\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | exit; | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | ######### SUBROUTINES ######################### | ||
| 103 | |||
| 104 | # rename OTUs | ||
| 105 | # | ||
| 106 | sub rename_otus { | ||
| 107 | my ($nexus, $transfile) = @_; | ||
| 108 | open(FILE, "<$transfile") or die "ERROR: Can\'t open $transfile\n"; | ||
| 109 | my $lines; | ||
| 110 | while ( $_ = <FILE> ) { | ||
| 111 | s/^#.*$//; | ||
| 112 | next if /^\s*$/; | ||
| 113 | $lines .= $_; | ||
| 114 | } | ||
| 115 | close( FILE ); | ||
| 116 | my %translation = @{ &_parse_nexus_words( $lines ) }; | ||
| 117 | return $nexus->rename_otus(\%translation); | ||
| 118 | } | ||
| 119 | |||
| 120 | # select a subset of blocks | ||
| 121 | sub selectbyblock { | ||
| 122 | my ($nexus, @blocks) = @_; | ||
| 123 | return $nexus->select_blocks(\@blocks); | ||
| 124 | } | ||
| 125 | |||
| 126 | # select subset of OTUs | ||
| 127 | sub selectbyotu { | ||
| 128 | my ($nexus, $mode, @args) = @_; | ||
| 129 | die "need otu names" unless (@args); | ||
| 130 | my @otus; | ||
| 131 | if ($args[0] eq '-f') { # input from file | ||
| 132 | my $file = $args[1]; | ||
| 133 | open(FILE, $file) or carp "File $file not found\n"; | ||
| 134 | my @lines = <FILE>; | ||
| 135 | @otus = split /\s+/, "@lines"; | ||
| 136 | close(FILE); | ||
| 137 | }else { # input from command line separated by space | ||
| 138 | my $list = join( " ", @args ); | ||
| 139 | $list = unquote( $list ); | ||
| 140 | $list =~ s/^\s+|\s+$//g; | ||
| 141 | @otus = split( /[,\s]\s*/, $list ); | ||
| 142 | } | ||
| 143 | if ( $mode == 1 ) { # select mode | ||
| 144 | return $nexus->select_otus(\@otus); | ||
| 145 | } | ||
| 146 | else { # exclude mode | ||
| 147 | return $nexus->exclude_otus(\@otus); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | # select a tree | ||
| 152 | sub selectbytree { | ||
| 153 | my ($nexus, $treename) = @_; | ||
| 154 | ($treename) or die "ERROR: Need to specify a tree to be selected\n"; | ||
| 155 | return $nexus->select_tree($treename); | ||
| 156 | } | ||
| 157 | |||
| 158 | # select a subtree | ||
| 159 | sub selectbyinode { | ||
| 160 | my ($nexus, $nodename, $treename) = @_; | ||
| 161 | $nodename or die "ERROR: Need to specify an internal node for subtree\n"; | ||
| 162 | |||
| 163 | return $nexus->select_subtree($nodename, $treename); | ||
| 164 | } | ||
| 165 | |||
| 166 | # use $command=1 for select and $command=0 for exclude | ||
| 167 | # arguments (column numbers) can be of form: | ||
| 168 | # 1) 1-3 4 5 6-10 or 1-3, 4 5, 6-10 | ||
| 169 | # 2) -f <file name> contains numbers in format as examplified in 1) | ||
| 170 | sub selectbycolumn { | ||
| 171 | my ($nexus, $command, @args) = @_; | ||
| 172 | my $args = "@args"; | ||
| 173 | $args =~ s/title\s*=\s*(\w+)//i; | ||
| 174 | my $title = $1; | ||
| 175 | # my $block = $nexus->get_block("characters", $title); | ||
| 176 | |||
| 177 | die "need column numbers" unless $args; | ||
| 178 | |||
| 179 | my $columns; | ||
| 180 | if ($args =~ /-f (\S+)/) { # input from file | ||
| 181 | my $file = $1; | ||
| 182 | $columns = do{ local(@ARGV, $/) = $file; <>}; | ||
| 183 | $columns =~ s/\n/ /g; | ||
| 184 | }else { # input from command line separated by comma or space | ||
| 185 | $columns = $args; | ||
| 186 | } | ||
| 187 | |||
| 188 | my @columns = @{ &parse_number($columns) }; | ||
| 189 | if ($command) { | ||
| 190 | return $nexus->select_chars(\@columns, $title); | ||
| 191 | }else { | ||
| 192 | return $nexus->exclude_chars(\@columns, $title); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | sub selectbysets { | ||
| 197 | my ($nexus, $mode, @setnames) = @_; | ||
| 198 | my @otus; | ||
| 199 | die "Provide set names as arguments\n" unless (@setnames); | ||
| 200 | for my $setname (@setnames) { | ||
| 201 | push(@otus, @{ $nexus->get_block('sets')->get_taxset($setname) } ); | ||
| 202 | } | ||
| 203 | |||
| 204 | if ( $mode == 1 ) { # "select" mode | ||
| 205 | return $nexus->select_otus(\@otus); | ||
| 206 | } else { # "exclude" mode | ||
| 207 | return $nexus->exclude_otus(\@otus); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | # parse numbers in format "1-3, 4 6 8-10" | ||
| 212 | sub parse_number { | ||
| 213 | my $s = unquote( shift ); | ||
| 214 | |||
| 215 | if (! $s =~ /^\s*(\d+(-\d+)?)([,\s]\s*\d+(-\d+)?)*\s*$/ ) { | ||
| 216 | die "Invalid number format. Use 1 or 1-3 or 1, 3, 5-8 or 1 3 5 6-10.\n"; | ||
| 217 | } | ||
| 218 | $s =~ s/^\s+|\s+$//g; | ||
| 219 | $s =~ s/,?\s+/,/g; # use ',' as separator | ||
| 220 | my @cols = split(/,/, $s); | ||
| 221 | |||
| 222 | my @arr; | ||
| 223 | foreach my $item (@cols) { | ||
| 224 | if ($item =~ /-/) { # eg 1-3 | ||
| 225 | $item =~ /([0-9]+)\s*-\s*([0-9]+)/; | ||
| 226 | for (my $i = $1; $i <= $2; $i++) { push ( @arr, $i-1 ); } | ||
| 227 | } elsif ($item =~ /^\d+$/) { # eg 4 | ||
| 228 | push ( @arr, $item-1 ); | ||
| 229 | } elsif ($item) { | ||
| 230 | die "non-number was used for column number\n"; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | @arr = sort {$a<=>$b} @arr; | ||
| 235 | return \@arr; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub unquote { | ||
| 239 | my $string = shift; | ||
| 240 | $string =~ s/^ *'(.*)' *$/$1/; | ||
| 241 | $string =~ s/^ *"(.*)" *$/$1/; | ||
| 242 | return( $string ); | ||
| 243 | } | ||
| 244 | |||
| 245 | sub exclude_blocks { | ||
| 246 | my ($nexus, @blocks) = @_; | ||
| 247 | return $nexus->exclude_blocks(\@blocks); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub exclude_subtree { | ||
| 251 | my ($nexus, $subtree) = @_; | ||
| 252 | return $nexus->exclude_subtree($subtree); | ||
| 253 | } | ||
| 254 | |||
| 255 | sub reroottree { | ||
| 256 | my ($nexus, $outgroup, $root_position, $treename) = @_; | ||
| 257 | if ( $nexus->get_block('trees')->get_tree('$root_position') ) { #in case no root position is supplied, but a tree name is | ||
| 258 | ($root_position, $treename) = (undef, $root_position); | ||
| 259 | } | ||
| 260 | return $nexus->reroot($outgroup, $root_position, $treename); | ||
| 261 | } | ||
| 262 | |||
| 263 | sub addtree { | ||
| 264 | my ($nexus,$treeFile,$treeName) = @_; | ||
| 265 | |||
| 266 | if ( !$treeName ) { ($treeName = (split("/",$treeFile))[-1]) =~ s/(.*)\..*/$1/; } | ||
| 267 | |||
| 268 | if ( -f $treeFile ) { | ||
| 269 | open(TF,"<$treeFile") || warn("ERROR: could not open $treeFile. No tree data will appear in NEXUS file."); | ||
| 270 | |||
| 271 | my ($treeString); | ||
| 272 | while(<TF>) { | ||
| 273 | s/\s*//go; | ||
| 274 | $treeString .= $_; | ||
| 275 | } | ||
| 276 | |||
| 277 | close(TF); | ||
| 278 | |||
| 279 | $treeString =~ s/(.*);$/$1/; #remove semicolon from end of $treeString, if there is one. | ||
| 280 | |||
| 281 | #$treeString must be formatted like this in order to work w/ Bio::NEXUS::TreesBlock::new - | ||
| 282 | # tree $treeName = $treeString | ||
| 283 | #the following manipulations ensure we end up w/ such a string to pass to create_block | ||
| 284 | $treeString =~ s/^tree(.*)$/$1/; | ||
| 285 | unless ( $treeString =~ m/^$treeName=/ ) { | ||
| 286 | $treeString = $treeName . " = " . $treeString; | ||
| 287 | } | ||
| 288 | $treeString = "tree " . $treeString; | ||
| 289 | |||
| 290 | print STDERR "tree string:\n$treeString\n"; | ||
| 291 | $nexus->add_block( $nexus->create_block('trees',$treeString, 1) ); | ||
| 292 | } else { | ||
| 293 | die("Tree file: \"$treeFile\" not found or could not be read"); | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | sub setsbyfile { | ||
| 298 | my ($nexus, @setfiles) = @_; | ||
| 299 | my $sets; | ||
| 300 | |||
| 301 | for my $setfile (@setfiles) { | ||
| 302 | open (SF, "<$setfile") || die ("Could not open set file $setfile.\n\n"); | ||
| 303 | while (<SF>) { | ||
| 304 | chomp; | ||
| 305 | push (@{$$sets{$setfile}}, $_); | ||
| 306 | } | ||
| 307 | close (SF); | ||
| 308 | } | ||
| 309 | &add_or_create_set ($nexus, $sets); | ||
| 310 | return $nexus; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub setsbyinode { | ||
| 314 | my ($nexus, @inodenames) = @_; | ||
| 315 | my $sets; | ||
| 316 | |||
| 317 | for my $inodename (@inodenames) { | ||
| 318 | unless ($nexus->get_block('trees')->get_tree()->find($inodename)) { | ||
| 319 | die "User-provided inode <$inodename> is not the name of an inode in this NEXUS file\nCommand aborted\n\n"; | ||
| 320 | } | ||
| 321 | my $subtree = $nexus->select_subtree($inodename); | ||
| 322 | my $otus = $subtree->get_otus(); | ||
| 323 | $$sets{$inodename} = $otus; | ||
| 324 | } | ||
| 325 | &add_or_create_set ($nexus, $sets); | ||
| 326 | return $nexus; | ||
| 327 | } | ||
| 328 | |||
| 329 | sub setsbyclade { | ||
| 330 | my ($nexus, @clade_elements) = @_; | ||
| 331 | my ($sets, $treename);# $treename is currently a dummy variable, but we should implement it | ||
| 332 | |||
| 333 | unless (validate_otus($nexus, [@clade_elements])) {die "User-provided OTUs failed validation against TAXA Block\n\n"} | ||
| 334 | |||
| 335 | while (scalar(@clade_elements) > 0) { | ||
| 336 | my ($otu1, $otu2) = splice(@clade_elements, 0, 2); | ||
| 337 | unless (defined $otu2) {die "You have supplied an odd number of OTU's. This mode requires pairs.\n\n"} | ||
| 338 | $otu1 = $nexus->get_block('trees')->get_tree($treename)->find($otu1); | ||
| 339 | $otu2 = $nexus->get_block('trees')->get_tree($treename)->find($otu2); | ||
| 340 | my $inode_name = $otu1 -> mrca($otu2, $treename) -> name(); | ||
| 341 | my $otus = $nexus->select_subtree($inode_name)->get_otus(); | ||
| 342 | $$sets{$inode_name} = $otus; | ||
| 343 | } | ||
| 344 | |||
| 345 | &add_or_create_set ($nexus, $sets); | ||
| 346 | return $nexus; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub setsbyotus { | ||
| 350 | my ($nexus, @otuargs) = @_; | ||
| 351 | my $otuargs = join(" ", @otuargs); | ||
| 352 | my $sets; | ||
| 353 | |||
| 354 | my @setsandelements = $otuargs =~ /\s*([^=]+)\s*=\s*\[\s*([^\]]+)\s*\]/g; | ||
| 355 | |||
| 356 | while (@setsandelements) { | ||
| 357 | $$sets{shift(@setsandelements)} = [split(/\s/, splice(@setsandelements, 1, 1))]; | ||
| 358 | } | ||
| 359 | |||
| 360 | my $validation = 1; | ||
| 361 | for my $users_otus (values %$sets) { | ||
| 362 | $validation = validate_otus($nexus, $users_otus); | ||
| 363 | } | ||
| 364 | |||
| 365 | unless ($validation == 1) {die "User-defined sets failed validation against TAXA Block\n\n"} | ||
| 366 | |||
| 367 | &add_or_create_set ($nexus, $sets); | ||
| 368 | return $nexus; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub setsbyunion { | ||
| 372 | my ($nexus, @setargs) = @_; | ||
| 373 | my $setargs = join(" ", @setargs); | ||
| 374 | my $sets = $nexus -> get_block('sets') -> get_taxsets(); | ||
| 375 | |||
| 376 | my @setsandelements = $setargs =~ /\s*(\b[^=]+\b)\s*=\s*\[\s*([^\]]+)\s*\]/g; | ||
| 377 | while (@setsandelements) { | ||
| 378 | my ($newsetname, @subsets) = (shift(@setsandelements), split(/(?:\s+|\s*\+\s*)/, shift(@setsandelements))); | ||
| 379 | my @otus; | ||
| 380 | for my $subset (@subsets) { | ||
| 381 | push(@otus, @{$nexus -> get_block('sets') -> get_taxset($subset)}); | ||
| 382 | } | ||
| 383 | $$sets{$newsetname} = [@otus]; | ||
| 384 | } | ||
| 385 | |||
| 386 | &add_or_create_set ($nexus, $sets); | ||
| 387 | return $nexus; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub setsbydifference { | ||
| 391 | my ($nexus, @setargs) = @_; | ||
| 392 | my $setargs = join(" ", @setargs); | ||
| 393 | my $sets = $nexus -> get_block('sets') -> get_taxsets(); | ||
| 394 | # print Dumper ($sets); | ||
| 395 | |||
| 396 | @setargs = $setargs =~ /\s*([^=\s]+)\s*=\s*\[\s*([^-\s\]]+)\s*-\s*([^-\s\]]+)\s*\]/g; | ||
| 397 | my (@difference_set_names, @minuend_set_names, @subtrahend_set_names); | ||
| 398 | while (@setargs) { | ||
| 399 | push(@difference_set_names, shift (@setargs)); | ||
| 400 | push(@minuend_set_names, shift (@setargs)); | ||
| 401 | push(@subtrahend_set_names, shift (@setargs)); | ||
| 402 | } | ||
| 403 | |||
| 404 | for (my $i = 0; $i < @difference_set_names; $i++) { | ||
| 405 | $$sets{$difference_set_names[$i]} = [@{$$sets{$minuend_set_names[$i]}}]; | ||
| 406 | for (my $j = 0; $j < @{$$sets{ $difference_set_names[$i] }}; $j++ ) { | ||
| 407 | for my $subtrahendOTU ( @{$$sets{ $subtrahend_set_names[$i] }} ) { | ||
| 408 | if ( ${$$sets{ $difference_set_names[$i] }}[$j] eq $subtrahendOTU) { | ||
| 409 | splice ( @{$$sets{ $difference_set_names[$i] }}, $j, 1); | ||
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | &add_or_create_set ($nexus, $sets); | ||
| 416 | return $nexus; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub setsbycharstate { | ||
| 420 | my ($nexus, @char_args) = @_; | ||
| 421 | my ($proposed_sets, $title) = &parse_charstate_args(@char_args); | ||
| 422 | my ($sets, @column_labels); | ||
| 423 | my $block = $nexus->get_block("characters", $title); | ||
| 424 | |||
| 425 | my $character_labels = $block->get_charlabels; | ||
| 426 | my $seq_length = $block->get_nchar; | ||
| 427 | my %char_matrix = %{$block->get_otuset->get_otu_sequences}; | ||
| 428 | if ($character_labels && @$character_labels) { | ||
| 429 | @column_labels = @$character_labels; | ||
| 430 | } elsif ($seq_length) { | ||
| 431 | for (1 .. $seq_length) {push @column_labels, $_;} | ||
| 432 | } | ||
| 433 | my %column_indices; | ||
| 434 | for (my $i = 0; $i < @column_labels; $i++) { | ||
| 435 | $column_indices{$column_labels[$i]} = $i; | ||
| 436 | } | ||
| 437 | foreach my $proposed_setname (keys %{$proposed_sets}){ | ||
| 438 | while (@{$$proposed_sets{$proposed_setname}} > 0 ) { | ||
| 439 | my ($column_label, $query_value) = splice(@{$$proposed_sets{$proposed_setname}}, 0, 2); | ||
| 440 | foreach my $otu (keys %char_matrix) { | ||
| 441 | my $locus_value = substr($char_matrix{$otu},$column_indices{$column_label},1); | ||
| 442 | if ($query_value =~ m/$locus_value/) { | ||
| 443 | push (@{$$sets{$proposed_setname}}, $otu); | ||
| 444 | } | ||
| 445 | } | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | &add_or_create_set ($nexus, $sets); | ||
| 450 | return $nexus; | ||
| 451 | } | ||
| 452 | |||
| 453 | sub parse_charstate_args { | ||
| 454 | my $char_args = join (" ", @_); | ||
| 455 | $char_args =~ m/title\s*=\s*(\w+)/i; | ||
| 456 | my $title = $1; # store the captured title name, if one was provided | ||
| 457 | my $num_title_matches = $char_args =~ s/title\s*=\s*\w+//g; # make sure the user only referred to one title (to prevent ambiguity) | ||
| 458 | if ($num_title_matches > 1 ) {die ("Too many 'title' arguments--please reference only one Characters Block\n\n")} | ||
| 459 | # the following line matches each set as defined by the user and stores it in the @proposed_sets array | ||
| 460 | # basically, it looks for bracket pairs that look like foo[bar], | ||
| 461 | # which may or may not be preceded by a set name declaration ('set_name = ') | ||
| 462 | my @proposed_sets = $char_args =~ m/(?:[-\w]+\s*=\s*)?\s*[-\w]+\s*\[[^\]]+\]\s*/g; | ||
| 463 | my %proposed_sets; | ||
| 464 | for my $proposed_set (@proposed_sets) { # for each of the matches made above | ||
| 465 | my $set_name; | ||
| 466 | if ($proposed_set =~ m/^\s*([^\s\[\]]+)\s*=/) { # if a name declaration was matched | ||
| 467 | $set_name = $1; # use that as the set name | ||
| 468 | $proposed_set =~ s/^\s*[^\s\[\]]+\s*=\s*//; # then, get rid of that portion of the array element | ||
| 469 | } else { # otherwise, make up a unique set name by joining the column label and locus value(s) | ||
| 470 | $set_name = join("", ($proposed_set =~ m/[^"\s\[\]]/g)); | ||
| 471 | } | ||
| 472 | my ($column_label, $locus_value); | ||
| 473 | while ($proposed_set =~ m/[^"\s]/) { # as long as $proposed_set contains more than spaces and quotes | ||
| 474 | ($column_label) = $proposed_set =~ m/[^"\s\[]+/; # match the column label | ||
| 475 | $proposed_set =~ m/\[([^\]]+)\]/; # and match the locus value(s) | ||
| 476 | $locus_value = $1; | ||
| 477 | $locus_value =~ s/[\s,]//g; # get rid of any spaces or commas the user may have separated the locus values with | ||
| 478 | push (@{$proposed_sets{$set_name}}, $column_label, $locus_value); # push the column label and locus value(s) onto this sets array in the set hash | ||
| 479 | $proposed_set =~ s/^[^\]]*\]//; # destroy the evidence | ||
| 480 | } | ||
| 481 | } | ||
| 482 | return (\%proposed_sets, $title); | ||
| 483 | } | ||
| 484 | |||
| 485 | sub setsbycladeconsensus { | ||
| 486 | my ($nexus, @args) = @_; | ||
| 487 | my $consensus_sets = {}; | ||
| 488 | my $treename; | ||
| 489 | my $args = join(' ', @args); | ||
| 490 | $args =~ m/title\s*=\s*(\w+)/i; | ||
| 491 | my $title = $1; | ||
| 492 | if ($title) {$args =~ s/\s*title\s*=\s*\w+\s*//i;} | ||
| 493 | if ($args =~ /(\w+)/) {$treename = $1;} | ||
| 494 | my $char_block = $nexus->get_block("characters", $title); | ||
| 495 | my %otunames_sequences = %{$char_block->get_otuset->get_otu_sequences}; | ||
| 496 | my $tree = $nexus->get_block('trees')->get_tree($treename)->clone(); | ||
| 497 | my $rootnode = $tree->get_rootnode(); | ||
| 498 | &set_consensus_seqs($rootnode, \%otunames_sequences); | ||
| 499 | ($rootnode, $consensus_sets) = &simplify_tree($rootnode, $consensus_sets); | ||
| 500 | $tree->set_name("nonredundant" . $tree->name()); | ||
| 501 | $nexus->get_block('trees')->add_tree($tree); | ||
| 502 | &add_or_create_set ($nexus, $consensus_sets); | ||
| 503 | return $nexus; | ||
| 504 | } | ||
| 505 | |||
| 506 | sub set_consensus_seqs { | ||
| 507 | my ($node, $otus_seqs) = @_; | ||
| 508 | my @children = @{ $node->children() }; | ||
| 509 | for my $child (@children) { | ||
| 510 | if ( $child->is_otu() ) { | ||
| 511 | $child->set_seq(${$otus_seqs}{$child->name()}); | ||
| 512 | if ( ! $node->get_seq() || ( $node->get_seq() eq $child->get_seq() ) ) { | ||
| 513 | $node->set_seq($child->get_seq()); | ||
| 514 | } else { | ||
| 515 | $node->set_seq('NO_CONSENSUS'); | ||
| 516 | } | ||
| 517 | } else { | ||
| 518 | &set_consensus_seqs($child, $otus_seqs); | ||
| 519 | if ( ! $node->get_seq() || ( $node->get_seq() eq $child->get_seq() ) ) { | ||
| 520 | $node->set_seq($child->get_seq()); | ||
| 521 | } else { | ||
| 522 | $node->set_seq('NO_CONSENSUS'); | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | } | ||
| 527 | |||
| 528 | sub simplify_tree { | ||
| 529 | my ($node, $consensus_sets) = @_; | ||
| 530 | my $setname; | ||
| 531 | if ($node->get_seq() ne 'NO_CONSENSUS' && ! $node->is_otu() ) { | ||
| 532 | my @otu_descendents = @{ $node->get_otus() }; | ||
| 533 | $setname = $otu_descendents[0]->name() . "_GROUP"; | ||
| 534 | ${ $consensus_sets }{$setname} = []; | ||
| 535 | for my $descendent (@otu_descendents) { | ||
| 536 | push(@{${$consensus_sets }{$setname}}, $descendent->name()); | ||
| 537 | } | ||
| 538 | my $newlength = $node->distance($otu_descendents[0]) + $node->length(); | ||
| 539 | my $parent = $node->get_parent(); | ||
| 540 | my $siblings = $node->get_siblings(); | ||
| 541 | $parent->set_children($siblings); | ||
| 542 | $parent->adopt($otu_descendents[0],0); | ||
| 543 | $otu_descendents[0]->set_length($newlength); | ||
| 544 | } else { | ||
| 545 | for my $child (@{ $node->children() }) { | ||
| 546 | simplify_tree($child, $consensus_sets); | ||
| 547 | } | ||
| 548 | } | ||
| 549 | return ($node, $consensus_sets); | ||
| 550 | } | ||
| 551 | |||
| 552 | sub validate_otus { #Verifies that user-specified OTU names are taxlabels in the TAXA block | ||
| 553 | my ($nexus, $users_otus) = @_; | ||
| 554 | my $validation = 1; | ||
| 555 | for my $users_otu (@$users_otus) { | ||
| 556 | unless (defined ($nexus->get_block('taxa')->is_taxon($users_otu, 1))) { | ||
| 557 | $validation = 0; | ||
| 558 | } | ||
| 559 | } | ||
| 560 | return $validation; | ||
| 561 | } | ||
| 562 | |||
| 563 | sub add_or_create_set { | ||
| 564 | my ($nexus, $sets) = @_; | ||
| 565 | my $setsblock; | ||
| 566 | if ($nexus -> get_block('Sets')) { | ||
| 567 | $nexus -> get_block('Sets') -> add_taxsets($sets); | ||
| 568 | } else { | ||
| 569 | $setsblock = Bio::NEXUS::SetsBlock -> new('Sets'); | ||
| 570 | $setsblock -> set_taxsets($sets); | ||
| 571 | $nexus -> add_block($setsblock); | ||
| 572 | } | ||
| 573 | return $nexus; | ||
| 574 | } | ||
| 575 | |||
| 576 | sub removesets { | ||
| 577 | my ($nexus, @usersets) = @_; | ||
| 578 | my $failure = 0; | ||
| 579 | my $sets_to_remove; | ||
| 580 | my $setsblock = get_setsblock_if_setsblock($nexus); | ||
| 581 | my $taxsets = $setsblock->get_taxsets(); | ||
| 582 | unless (scalar(keys %$taxsets) > 0) {die "The SETS Block is already empty\n\n";} | ||
| 583 | |||
| 584 | if ($usersets[0] =~ /^-p$/i) { | ||
| 585 | shift @usersets; | ||
| 586 | for my $userset (@usersets) { | ||
| 587 | my $match = 0; | ||
| 588 | TAXSET: for my $taxset (keys %$taxsets) { | ||
| 589 | if ($taxset =~ /^$userset$/) { | ||
| 590 | push(@$sets_to_remove, $taxset); | ||
| 591 | $match = 1; | ||
| 592 | next TAXSET; | ||
| 593 | } | ||
| 594 | } | ||
| 595 | if ($match == 0) { | ||
| 596 | warn "<$userset> does not match any sets in this NEXUS file\n"; | ||
| 597 | $failure = 1; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | } else { | ||
| 601 | for (my $i=0; $i < scalar(@usersets); $i++) { | ||
| 602 | if ($$taxsets{$usersets[$i]}) { | ||
| 603 | push (@$sets_to_remove, $usersets[$i]); | ||
| 604 | } else { | ||
| 605 | warn "<$usersets[$i]> is not the name of a set in this NEXUS file.\n"; | ||
| 606 | $failure = 1; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | } | ||
| 610 | die "Command aborted\n\n" if $failure == 1; | ||
| 611 | $nexus->get_block('sets')->delete_taxsets(@$sets_to_remove); | ||
| 612 | return $nexus; | ||
| 613 | } | ||
| 614 | |||
| 615 | sub renamesets { | ||
| 616 | my ($nexus, @old_and_new) = @_; | ||
| 617 | my $setsblock = get_setsblock_if_setsblock($nexus); | ||
| 618 | $setsblock->rename_taxsets(@old_and_new); | ||
| 619 | return $nexus; | ||
| 620 | } | ||
| 621 | |||
| 622 | sub listsets { | ||
| 623 | my ($nexus, $outfile, @sets_to_list) = @_; | ||
| 624 | my $setsblock = get_setsblock_if_setsblock($nexus); | ||
| 625 | if (@sets_to_list > 0) { | ||
| 626 | my $fh = open_filehandle($outfile); | ||
| 627 | for (@sets_to_list) { | ||
| 628 | my @otus = @{ $setsblock->get_taxset($_) }; | ||
| 629 | if (@otus == 0) { | ||
| 630 | warn("$_ = [] Possible error: Set is empty or does not exist\n\n"); | ||
| 631 | } elsif (@otus > 0) { | ||
| 632 | print $fh "$_ = [@otus]\n\n"; | ||
| 633 | } | ||
| 634 | } | ||
| 635 | } else { | ||
| 636 | $setsblock->print_all_taxsets($outfile); | ||
| 637 | } | ||
| 638 | } | ||
| 639 | |||
| 640 | sub listsetnames { | ||
| 641 | my ($nexus, $outfile) = @_; | ||
| 642 | my $setsblock = get_setsblock_if_setsblock($nexus); | ||
| 643 | my @setnames = @{ $setsblock->get_taxset_names() }; | ||
| 644 | my $fh = open_filehandle($outfile); | ||
| 645 | print $fh "@setnames\n"; | ||
| 646 | } | ||
| 647 | |||
| 648 | sub open_filehandle { | ||
| 649 | my ($fh) = @_; | ||
| 650 | if ($fh eq "-" || $fh eq \*STDOUT) {return \*STDOUT} | ||
| 651 | else {open(FH, ">$fh") || die "Could not open $outfile for writing\n\n"} | ||
| 652 | return \*FH; | ||
| 653 | } | ||
| 654 | |||
| 655 | sub get_setsblock_if_setsblock { | ||
| 656 | my ($nexus) = @_; | ||
| 657 | my $setsblock; | ||
| 658 | unless ($setsblock = $nexus->get_block('sets')){die "This NEXUS file does not contain a SETS Block\n\n"} | ||
| 659 | return $setsblock; | ||
| 660 | } | ||
| 661 | |||
| 662 | sub file_overwrite_warning { | ||
| 663 | my ($infile, $outfile, $command, $silentmode) = @_; | ||
| 664 | if ($silentmode == 1) {return 1} | ||
| 665 | if ($infile eq $outfile) { | ||
| 666 | warn("Do you really want to overwrite NEXUS file $infile with the $command output?\n"); | ||
| 667 | if (<STDIN> =~ /y/) { | ||
| 668 | return 1; | ||
| 669 | } else { die("Command aborted\n\n")} | ||
| 670 | } else { return 1;} | ||
| 671 | } | ||
| 672 | |||
| 673 | ################# POD Documentation ################## | ||
| 674 | |||
| 675 | __END__ |