| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: split-rates-ali.pl | ||
| 3 | # ABSTRACT: Split ALI files into subsets of sites based on site-wise statistics | ||
| 4 | |||
| 5 | use Modern::Perl '2011'; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | use Getopt::Euclid qw(:vars); | ||
| 9 | use Smart::Comments; | ||
| 10 | |||
| 11 | use Bio::MUST::Core; | ||
| 12 | use Bio::MUST::Core::Utils qw(change_suffix secure_outfile); | ||
| 13 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 14 | use aliased 'Bio::MUST::Core::SeqMask'; | ||
| 15 | |||
| 16 | |||
| 17 | # TODO: generalize this to other executables through Utils? | ||
| 18 | my ($load, $store) = qw(load store); | ||
| 19 | my $suffix = '.ali'; | ||
| 20 | my %out_args; | ||
| 21 | if ($ARGV_phylip) { | ||
| 22 | ### Infiles (and outfiles) are in PHYLIP format | ||
| 23 | $load .= '_phylip'; | ||
| 24 | $store .= '_phylip'; | ||
| 25 | $suffix = '.phy'; | ||
| 26 | $out_args{chunk} = -1; | ||
| 27 | } | ||
| 28 | |||
| 29 | my $class = 'Bio::MUST::Core::SeqMask::Rates'; | ||
| 30 | my $delta = 'delta_rates'; | ||
| 31 | if ($ARGV_sitefreq) { | ||
| 32 | $class = 'Bio::MUST::Core::SeqMask::Pmsf'; | ||
| 33 | $delta = 'chi_square_stats'; | ||
| 34 | } | ||
| 35 | |||
| 36 | # optionally load other rates file for deltas | ||
| 37 | my $other_rates; | ||
| 38 | $other_rates = $class->load($ARGV_other_rates) if $ARGV_other_rates; | ||
| 39 | |||
| 40 | for my $infile (@ARGV_infiles) { | ||
| 41 | |||
| 42 | ### Processing: $infile | ||
| 43 | my $rates = $class->load($infile); | ||
| 44 | |||
| 45 | $infile =~ s/$_//xms for @ARGV_in_strip; | ||
| 46 | my $alifile = change_suffix($infile, $suffix); | ||
| 47 | my $ali = Ali->$load($alifile); | ||
| 48 | $ali->gapify_seqs if $ARGV_from_scafos; | ||
| 49 | ### Infile: "$alifile had " . $ali->width . ' sites' | ||
| 50 | |||
| 51 | # optionally delete constant sites | ||
| 52 | $ali->apply_mask( SeqMask->variable_mask($ali) ) if $ARGV_del_const; | ||
| 53 | |||
| 54 | # optionally use delta rates instead of raw rates if available | ||
| 55 | if ($other_rates) { | ||
| 56 | $rates = $rates->$delta($other_rates); | ||
| 57 | |||
| 58 | # optionally dump stats resulting from delta operation | ||
| 59 | if ($ARGV_dump_stats) { | ||
| 60 | my $outfile = secure_outfile( | ||
| 61 | change_suffix($infile, '.stats'), $ARGV_out_suffix | ||
| 62 | ); | ||
| 63 | ### Dumping site-wise stats to: $outfile | ||
| 64 | $rates->store($outfile); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | ### Computing masks from stats | ||
| 69 | my %args; | ||
| 70 | $args{percentile} = 1 if $ARGV_percentile; | ||
| 71 | $args{cumulative} = 1 if $ARGV_cumulative; | ||
| 72 | $args{descending} = 1 if $ARGV_descending; | ||
| 73 | my @masks = $rates->bin_rates_masks($ARGV_bin_number, \%args); | ||
| 74 | |||
| 75 | # output one Ali per bin | ||
| 76 | for my $i (0..$#masks) { | ||
| 77 | my $bin_ali = $masks[$i]->filtered_ali($ali); | ||
| 78 | my $outfile = secure_outfile($alifile, $ARGV_out_suffix . "-bin$i"); | ||
| 79 | # Note: $ARGV_out_suffix defaults to q{} for smooth concatenation | ||
| 80 | |||
| 81 | ### Outfile: "$outfile has " . $bin_ali->width . ' sites' | ||
| 82 | $bin_ali->$store($outfile, \%out_args); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | __END__ |