| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: classify-mcl-out.pl | ||
| 3 | # ABSTRACT: Classify MCL clusters based on taxonomic filters | ||
| 4 | # CONTRIBUTOR: Arnaud DI FRANCO <arnaud.difranco@gmail.com> | ||
| 5 | |||
| 6 | use Modern::Perl '2011'; | ||
| 7 | use autodie; | ||
| 8 | |||
| 9 | use Config::Any; | ||
| 10 | use File::Basename; | ||
| 11 | use Getopt::Euclid qw(:vars); | ||
| 12 | use Path::Class; | ||
| 13 | use Smart::Comments; | ||
| 14 | |||
| 15 | use Bio::MUST::Core; | ||
| 16 | use Bio::MUST::Core::Utils qw(append_suffix); | ||
| 17 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 18 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 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 $infile (@ARGV_infiles) { | ||
| 37 | |||
| 38 | ### Processing: $infile | ||
| 39 | open my $in, '<', $infile; | ||
| 40 | |||
| 41 | # create directory named after infile | ||
| 42 | my ($filename) = fileparse($infile, qr{\.[^.]*}xms); | ||
| 43 | my $dir = dir( $filename . '-classify' )->relative; | ||
| 44 | $dir->mkpath(); | ||
| 45 | |||
| 46 | my %out_for; | ||
| 47 | |||
| 48 | LINE: | ||
| 49 | while (my $line = <$in>) { | ||
| 50 | |||
| 51 | # process group line and extract members | ||
| 52 | chomp $line; | ||
| 53 | my ($group, $memb_str) = $line =~ m/(\S+) \s* : \s* (.*)/xms; | ||
| 54 | my @members = split /\s+/xms, $memb_str; | ||
| 55 | my $listable = IdList->new( ids => \@members ); | ||
| 56 | |||
| 57 | # classify group | ||
| 58 | my $cat_label = $classifier->classify($listable); | ||
| 59 | next LINE unless $cat_label; | ||
| 60 | ### classified: "$group | $cat_label" | ||
| 61 | |||
| 62 | # select adequate outfile based on label | ||
| 63 | my $outfile = append_suffix($cat_label, '.txt'); | ||
| 64 | my $out = ( $out_for{$outfile} //= file($dir, $outfile)->openw ); | ||
| 65 | |||
| 66 | # output group line | ||
| 67 | say {$out} $line; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | __END__ |