| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: classify-ali.pl | ||
| 3 | # ABSTRACT: Classify ALI files based on taxonomic filters | ||
| 4 | |||
| 5 | use Modern::Perl '2011'; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | use Config::Any; | ||
| 9 | use File::Basename; | ||
| 10 | use File::Find::Rule; | ||
| 11 | use Getopt::Euclid qw(:vars); | ||
| 12 | use Path::Class qw(dir file); | ||
| 13 | use Smart::Comments; | ||
| 14 | |||
| 15 | use Bio::MUST::Core; | ||
| 16 | use Bio::MUST::Core::Constants qw(:dirs); | ||
| 17 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 18 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 19 | |||
| 20 | |||
| 21 | # read configuration file | ||
| 22 | my $config = Config::Any->load_files( { | ||
| 23 | files => [ $ARGV_config ], | ||
| 24 | flatten_to_hash => 1, | ||
| 25 | use_ext => 1, | ||
| 26 | } ); | ||
| 27 | ### config: $config->{$ARGV_config} | ||
| 28 | |||
| 29 | die "Error: no config file specified; aborting...\n" | ||
| 30 | unless $config; | ||
| 31 | |||
| 32 | # build taxonomy and classifier objects | ||
| 33 | my $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir ); | ||
| 34 | my $classifier = $tax->tax_classifier( $config->{$ARGV_config} ); | ||
| 35 | |||
| 36 | for my $indir (@ARGV_indirs) { | ||
| 37 | |||
| 38 | ### Processing: $indir | ||
| 39 | my @infiles = File::Find::Rule | ||
| 40 | ->file() | ||
| 41 | ->maxdepth(1) | ||
| 42 | ->name( $SUFFICES_FOR{Ali} ) | ||
| 43 | ->in($indir) | ||
| 44 | ; | ||
| 45 | |||
| 46 | # create output directories named after input directory and categories | ||
| 47 | my $outdir = dir($indir)->basename . '-classify'; | ||
| 48 | for my $cat ( $classifier->all_categories ) { | ||
| 49 | my $subdir = dir( $outdir, $cat->label )->relative; | ||
| 50 | $subdir->mkpath(); | ||
| 51 | } | ||
| 52 | |||
| 53 | ALI: | ||
| 54 | for my $infile (@infiles) { | ||
| 55 | |||
| 56 | ### Processing: $infile | ||
| 57 | my $ali = Ali->load($infile); | ||
| 58 | $ali->dont_guess; | ||
| 59 | |||
| 60 | # classify Ali | ||
| 61 | my $cat_label = $classifier->classify($ali); | ||
| 62 | ### classified to: $cat_label | ||
| 63 | next ALI unless $cat_label; | ||
| 64 | |||
| 65 | # store Ali in corresponding directory | ||
| 66 | my $subdir = dir($outdir, $cat_label)->relative; | ||
| 67 | my ($filename) = fileparse($infile); | ||
| 68 | my $outfile = file($subdir, $filename); | ||
| 69 | $ali->store($outfile); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | __END__ |