| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: compress-db.pl | ||
| 3 | # ABSTRACT: Post-process assembler for transcript enrichment using 42 | ||
| 4 | |||
| 5 | use Modern::Perl '2011'; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | use Getopt::Euclid qw(:vars); | ||
| 9 | use Path::Class qw(file dir); | ||
| 10 | |||
| 11 | ## no critic (RequireLocalizedPunctuationVars) | ||
| 12 | BEGIN{ | ||
| 13 | $ENV{Smart_Comments} = $ARGV_verbosity | ||
| 14 | ? join q{ }, map { '#' x (2 + $_) } 1..$ARGV_verbosity | ||
| 15 | : q{} | ||
| 16 | ; | ||
| 17 | } | ||
| 18 | ## use critic | ||
| 19 | |||
| 20 | use Smart::Comments -ENV; | ||
| 21 | use Config::Any; | ||
| 22 | |||
| 23 | use Bio::MUST::Core; | ||
| 24 | use Bio::MUST::Drivers; | ||
| 25 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 26 | use aliased 'Bio::MUST::Core::Seq'; | ||
| 27 | use aliased 'Bio::MUST::Core::SeqId'; | ||
| 28 | use aliased 'Bio::MUST::Drivers::Cap3'; | ||
| 29 | use aliased 'Bio::MUST::Apps::Debrief42'; | ||
| 30 | |||
| 31 | |||
| 32 | # read configuration file | ||
| 33 | my $config = Config::Any->load_files( { | ||
| 34 | files => [ file($ARGV_config) ], | ||
| 35 | flatten_to_hash => 1, | ||
| 36 | use_ext => 1, | ||
| 37 | } | ||
| 38 | ); | ||
| 39 | |||
| 40 | # build Debrief42 object | ||
| 41 | my $debrief42 = Debrief42->new( | ||
| 42 | config => $config->{$ARGV_config}, | ||
| 43 | report_dir => $ARGV_indir, | ||
| 44 | ); | ||
| 45 | |||
| 46 | ### Reading Tax-Reports... | ||
| 47 | my $run_report = $debrief42->run_report; | ||
| 48 | my @orgs = $run_report->all_orgs; | ||
| 49 | |||
| 50 | # create output dir named after input dir and settings | ||
| 51 | my $dirname = dir($ARGV_indir)->stringify; # ignore trailing '/' if any | ||
| 52 | my $outdir = dir($dirname . '-compress' . "-o$ARGV_cap3_o" . "-p$ARGV_cap3_p"); | ||
| 53 | |||
| 54 | ### Creating outdir: $outdir->stringify | ||
| 55 | $outdir->mkpath(); | ||
| 56 | |||
| 57 | for my $org (@orgs) { | ||
| 58 | |||
| 59 | ### Processing: $org | ||
| 60 | my $org_report = $run_report->org_report_for($org); | ||
| 61 | |||
| 62 | my $ali = Ali->new; | ||
| 63 | |||
| 64 | OUTFILE: | ||
| 65 | for my $new_seqs (sort $org_report->all_new_seqs_by_outfile) { | ||
| 66 | # TODO: try to avoid sort by making method deterministic (not easy) | ||
| 67 | |||
| 68 | # collect new_seqs for current org and outfile | ||
| 69 | my $outfile; | ||
| 70 | my @seqs2cap; | ||
| 71 | for my $new_seq ( @{$new_seqs} ) { | ||
| 72 | $outfile //= $new_seq->outfile; | ||
| 73 | my $seq_id = SeqId->new( full_id => $new_seq->seq_id )->accession; | ||
| 74 | my $seq = $new_seq->seq; | ||
| 75 | push @seqs2cap, Seq->new( seq_id => $seq_id, seq => $seq ); | ||
| 76 | } | ||
| 77 | |||
| 78 | # proceed only for combos with at least two new_seqs | ||
| 79 | # otherwise add lone seq to database | ||
| 80 | if (@seqs2cap < 2) { | ||
| 81 | $ali->add_seq( shift @seqs2cap ); | ||
| 82 | next OUTFILE; | ||
| 83 | } | ||
| 84 | |||
| 85 | # try to cap new_seqs for current org and outfile | ||
| 86 | my $cap = Cap3->new( | ||
| 87 | seqs => \@seqs2cap, | ||
| 88 | cap3_args => { -o => $ARGV_cap3_o, -p => $ARGV_cap3_p }, | ||
| 89 | ); | ||
| 90 | |||
| 91 | # add singlet seqs to database | ||
| 92 | my @singlets = $cap->all_singlets; | ||
| 93 | $ali->add_seq($_) for @singlets; | ||
| 94 | |||
| 95 | # proceed only for combos with contigs of new_seqs | ||
| 96 | my @contigs = $cap->all_contigs; | ||
| 97 | next OUTFILE unless @contigs; | ||
| 98 | |||
| 99 | ### $outfile | ||
| 100 | for my $contig (@contigs) { | ||
| 101 | my @ids = map { $_->full_id } | ||
| 102 | @{ $cap->seq_ids_for( $contig->full_id ) }; | ||
| 103 | ### $contig | ||
| 104 | ### @ids | ||
| 105 | my $contig_id = shift(@ids) . '+' . @ids; | ||
| 106 | ### $contig_id | ||
| 107 | my $contig_seq = $contig->seq; | ||
| 108 | ### $contig_seq | ||
| 109 | |||
| 110 | # add contig seq to database | ||
| 111 | $ali->add_seq( | ||
| 112 | Seq->new( seq_id => $contig_id, seq => $contig_seq ) | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | # store contig database for org | ||
| 118 | (my $outfile = $org) =~ tr/ /_/; | ||
| 119 | $outfile .= '_new_cap3.fasta'; | ||
| 120 | $ali->store( file($outdir, $outfile) ); | ||
| 121 | } | ||
| 122 | |||
| 123 | __END__ | ||
| 124 | |||
| 125 | =pod | ||
| 126 | |||
| 127 | =head1 NAME | ||
| 128 | |||
| 129 | compress-db.pl - Post-process assembler for transcript enrichment using 42 | ||
| 130 | |||
| 131 | =head1 VERSION | ||
| 132 | |||
| 133 | version 0.213470 | ||
| 134 | |||
| 135 | =head1 USAGE | ||
| 136 | |||
| 137 | compress.pl --config=<file> --indir=<dir> [optional arguments] | ||
| 138 | |||
| 139 | =head1 REQUIRED ARGUMENTS | ||
| 140 | |||
| 141 | =over | ||
| 142 | |||
| 143 | =item --config=<file> | ||
| 144 | |||
| 145 | Path to YAML C<config> file for 42. | ||
| 146 | |||
| 147 | =for Euclid: file.type: string | ||
| 148 | |||
| 149 | =item --indir=<dir> | ||
| 150 | |||
| 151 | Path to input tax-report files. | ||
| 152 | |||
| 153 | =for Euclid: dir.type: str | ||
| 154 | |||
| 155 | =back | ||
| 156 | |||
| 157 | =head1 OPTIONAL ARGUMENTS | ||
| 158 | |||
| 159 | =item --cap3-o=<n> | ||
| 160 | |||
| 161 | Overlap length cutoff for CAP3 (should be > 15) [default: 40]. | ||
| 162 | |||
| 163 | =for Euclid: n.type: n > 15 | ||
| 164 | n.default: 40 | ||
| 165 | |||
| 166 | =item --cap3-p=<n> | ||
| 167 | |||
| 168 | Overlap percent identity cutoff for CAP3 (should be > 65) [default: 90]. | ||
| 169 | |||
| 170 | =for Euclid: n.type: n > 65 | ||
| 171 | n.default: 90 | ||
| 172 | |||
| 173 | =item --verbosity=<level> | ||
| 174 | |||
| 175 | Verbosity level for logging to STDERR [default: 0]. Available levels range from | ||
| 176 | 0 to 6. Level 6 corresponds to debugging mode. | ||
| 177 | |||
| 178 | =for Euclid: level.type: int, level >= 0 && level <= 6 | ||
| 179 | level.default: 1 | ||
| 180 | |||
| 181 | =item --version | ||
| 182 | |||
| 183 | =item --usage | ||
| 184 | |||
| 185 | =item --help | ||
| 186 | |||
| 187 | =item --man | ||
| 188 | |||
| 189 | =over | ||
| 190 | |||
| 191 | Print the usual program information | ||
| 192 | |||
| 193 | =back | ||
| 194 | |||
| 195 | =head1 AUTHOR | ||
| 196 | |||
| 197 | Denis BAURAIN <denis.baurain@uliege.be> | ||
| 198 | |||
| 199 | =head1 COPYRIGHT AND LICENSE | ||
| 200 | |||
| 201 | This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN. | ||
| 202 | |||
| 203 | This is free software; you can redistribute it and/or modify it under | ||
| 204 | the same terms as the Perl 5 programming language system itself. | ||
| 205 | |||
| 206 | =cut |