script/cdhit-tax-filter.pl untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 109 CC 2 SCAR 9.6
CC
2
Cov
0%
dump_mapper
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.6
1 #!/usr/bin/env perl
2 # PODNAME: cdhit-tax-filter.pl
3 # ABSTRACT:
4 # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be>
5  
6 use Modern::Perl '2011';
7 use autodie;
8  
9 use Getopt::Euclid qw(:vars);
10 use Smart::Comments;
11  
12 use Bio::MUST::Core;
13 use Bio::MUST::Core::Utils qw(:filenames secure_outfile);
14 use aliased 'Bio::MUST::Core::Ali';
15 use aliased 'Bio::MUST::Core::IdList';
16 use aliased 'Bio::MUST::Core::Taxonomy';
17 use Bio::MUST::Drivers;
18 use aliased 'Bio::MUST::Drivers::CdHit';
19  
20 # check for conditionally required arguments
21 die <<'EOT' if !$ARGV_taxdir && $ARGV_filter;
22 Missing required arguments:
23 --taxdir=<dir>
24 EOT
25  
26 # optionally build taxonomy and filter objects
27 my $filter;
28 if ($ARGV_filter) {
29 my $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir );
30 $filter = $tax->tax_filter($ARGV_filter);
31 ### Active filter: $filter->all_specs
32 }
33  
34 my $method;
35 $method = 'all' if $ARGV_keep_all;
36  
37 for my $infile (@ARGV_infiles){
38  
39 ### Processing: $infile
40 my $ali = Ali->load($infile);
41  
42 ### Running CD-HIT...
43 my $cdh = CdHit->new(
44 seqs => $infile,
45 cdhit_args => { -c => $ARGV_identity }
46 );
47 my $new_cluster_for;
48  
49 # 1. taxonomic mode (main goal)
50 if ($ARGV_filter) {
51 my $list = ( $filter->tax_list($ali) )->ids;
52 $new_cluster_for = $cdh->filter_clusters($list, $method);
53 }
54  
55 # 2. simple CDHIT mode
56 # TODO: move logic to lib?
57 else {
58 my @representatives = $cdh->all_cluster_names;
59 my @members = $cdh->all_cluster_seq_ids;
60 $new_cluster_for->{$_} = {
61 representatives => [ shift @representatives ],
62 members => [ map { $_->full_id } @{ shift @members } ],
63 } for 1..$cdh->count_representatives;
64 }
65  
66 if ($ARGV_store_id_mapper) {
67 my $idmfile = change_suffix(
68 insert_suffix($infile, $ARGV_out_suffix), '.idm'
69 );
70 dump_mapper($idmfile, $new_cluster_for);
71 }
72  
73 my @want_ids = map {
74 @{ $new_cluster_for->{$_}{representatives} }
75 } keys %$new_cluster_for;
76  
77 $ali->apply_list( IdList->new( ids => \@want_ids ) );
78 my $outfile = secure_outfile($infile, $ARGV_out_suffix);
79 $ali ->store($outfile);
80 }
81  
82 sub dump_mapper {
83 my $outfile = shift;
84 my $new_cluster_for = shift;
85  
86 open my $out, '>', $outfile;
87  
88 CLUSTER:
89 for my $cluster ( keys %{$new_cluster_for} ) {
90 my $reprs = $new_cluster_for->{$cluster}{representatives};
91 my $members = $new_cluster_for->{$cluster}{members };
92  
93 next CLUSTER unless @{$members};
94  
95 my $n = @{$members} + 1;
96 say {$out} join "\t", $_ . "#C$cluster" . "N$n#", $_ for @{$reprs};
97 }
98  
99 return;
100 }
101  
102 __END__