| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: jack-ali-dir.pl | ||
| 3 | # ABSTRACT: Jackknife a directory of ALI files | ||
| 4 | |||
| 5 | use Modern::Perl '2011'; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | use Getopt::Euclid qw(:vars); | ||
| 9 | use Smart::Comments; | ||
| 10 | |||
| 11 | use File::Find::Rule; | ||
| 12 | use List::AllUtils qw(shuffle); | ||
| 13 | use Path::Class qw(file dir); | ||
| 14 | use POSIX; | ||
| 15 | |||
| 16 | use Bio::MUST::Core; | ||
| 17 | use Bio::MUST::Core::Constants qw(:dirs); | ||
| 18 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 19 | use aliased 'Bio::MUST::Core::SeqMask'; | ||
| 20 | |||
| 21 | # TODO: output FASTA or p80 files? | ||
| 22 | |||
| 23 | for my $indir (@ARGV_indirs) { | ||
| 24 | |||
| 25 | ### Processing: $indir | ||
| 26 | my @infiles = File::Find::Rule | ||
| 27 | ->file() | ||
| 28 | ->maxdepth(1) | ||
| 29 | ->name( $SUFFICES_FOR{Ali} ) | ||
| 30 | ->in($indir) | ||
| 31 | ; | ||
| 32 | |||
| 33 | # create output directory named after input directory and settings | ||
| 34 | my $dirname = dir($indir)->basename | ||
| 35 | . "-jack-$ARGV_replicates-$ARGV_width" | ||
| 36 | . ($ARGV_del_const ? '-dc' : q{}) | ||
| 37 | ; | ||
| 38 | my $dir = dir($dirname)->relative; | ||
| 39 | $dir->mkpath(); | ||
| 40 | |||
| 41 | # setup replicate numbering format | ||
| 42 | my $field = ceil( log($ARGV_replicates) / log(10) ); | ||
| 43 | |||
| 44 | # build replicates | ||
| 45 | for my $rep (0..$ARGV_replicates-1) { | ||
| 46 | my @pool = shuffle @infiles; | ||
| 47 | |||
| 48 | my @alis; | ||
| 49 | my $width = 0; | ||
| 50 | |||
| 51 | ALI: | ||
| 52 | while (my $infile = shift @pool) { | ||
| 53 | my $ali = Ali->load($infile); | ||
| 54 | |||
| 55 | # optionally delete constant sites | ||
| 56 | if ($ARGV_del_const) { | ||
| 57 | my $mask = SeqMask->variable_mask($ali); | ||
| 58 | $ali->apply_mask($mask); | ||
| 59 | } | ||
| 60 | |||
| 61 | # cumulate Ali files while total width is lower than target width | ||
| 62 | push @alis, $ali; | ||
| 63 | $width += $ali->width; | ||
| 64 | |||
| 65 | last ALI if $width >= $ARGV_width; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $subdirname = sprintf 'replicate-%0*d', $field, $rep; | ||
| 69 | ### REP dir: "$subdirname has $width sites" | ||
| 70 | warn "Not enough ALI files to reach the target width!\n" | ||
| 71 | if $width < $ARGV_width; | ||
| 72 | |||
| 73 | # create subdirectory for replicate | ||
| 74 | my $subdir = dir($dirname, $subdirname)->relative; | ||
| 75 | $subdir->mkpath(); | ||
| 76 | |||
| 77 | # write Ali files to new subdirectory | ||
| 78 | # Note: we don't use a mere copy to honour the --del-const option | ||
| 79 | $_->store( file($subdir, $_->file->basename) ) for @alis; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | __END__ |