Skip to content

Instantly share code, notes, and snippets.

@mizlan
Created July 18, 2021 17:09
Show Gist options
  • Save mizlan/78728954b4cf16f6e29b9e22af76bba1 to your computer and use it in GitHub Desktop.
Save mizlan/78728954b4cf16f6e29b9e22af76bba1 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# parse iqtree .state output
# perl asr_seq.pl foo.state output_file
use strict;
use warnings;
use feature 'say';
my $input_file = $ARGV[0]
or die "usage: $0 <input file> <output file>\n";
my $output_file = $ARGV[1]
or die "usage: $0 <input file> <output file>\n";
$input_file =~ /\.state$/ or die "not a .state file?";
my %node_positions = ();
my %nodes = ();
open my $ifh, '<', $input_file or die "Cannot open $input_file: $!";
open my $ofh, '>', $output_file or die "Cannot open $output_file: $!";
while (my $line = <$ifh>) {
chomp $line;
my @cols = split /\s+/, $line;
if ($line =~ /^Node\s+Site/) {
for my $i (0 .. $#cols) {
my $col = $cols[$i];
$node_positions{"$1"} = $i if $col =~ /p_(.)/;
}
} elsif ($line =~ /^Node(\d+)/) {
my $node = $1;
my $site = $cols[1];
my $state = $cols[2];
# my $prob = $cols[$node_positions{$state}];
$nodes{$node + 0} .= $state;
die "Assumption of increasing sites violated, contact me" if $site != length($nodes{$node});
}
}
for my $node (sort {$a <=> $b} keys %nodes) {
say $ofh $node, ": ", $nodes{$node};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment