| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: ompa-pa.pl | ||
| 3 | # ABSTRACT: Extract seqs from BLAST/HMMER interactively or in batch mode | ||
| 4 | # CONTRIBUTOR: Amandine BERTRAND <amandine.bertrand@doct.uliege.be> | ||
| 5 | |||
| 6 | use Modern::Perl '2011'; | ||
| 7 | use autodie; | ||
| 8 | |||
| 9 | use Config::Any; | ||
| 10 | use Getopt::Euclid qw(:vars); | ||
| 11 | use Path::Class qw(file dir); | ||
| 12 | use Smart::Comments; | ||
| 13 | |||
| 14 | use IO::Prompter [ | ||
| 15 | -verbatim, | ||
| 16 | -style => 'blue strong', | ||
| 17 | -must => { 'be a string' => qr{\S+}xms } | ||
| 18 | ]; | ||
| 19 | |||
| 20 | use Bio::MUST::Core; | ||
| 21 | use aliased 'Bio::MUST::Core::Ali::Stash'; | ||
| 22 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 23 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 24 | use aliased 'Bio::MUST::Core::Taxonomy::ColorScheme'; | ||
| 25 | use aliased 'Bio::MUST::Apps::OmpaPa::Parameters'; | ||
| 26 | use aliased 'Bio::MUST::Apps::OmpaPa::Blast'; | ||
| 27 | use aliased 'Bio::MUST::Apps::OmpaPa::Hmmer'; | ||
| 28 | |||
| 29 | # TODO: re-implement BLAST parsing and nr extraction; done? | ||
| 30 | |||
| 31 | die <<'EOT' if !$ARGV_database && $ARGV_extract_seqs; | ||
| 32 | Missing required arguments: | ||
| 33 | --database=<file> | ||
| 34 | EOT | ||
| 35 | |||
| 36 | die <<'EOT' if !$ARGV_taxdir && ($ARGV_skip_config || $ARGV_colorize || $ARGV_extract_tax); | ||
| 37 | Missing required arguments: | ||
| 38 | --taxdir=<dir> | ||
| 39 | EOT | ||
| 40 | |||
| 41 | die <<'EOT' if !$ARGV_skip_config && $ARGV_batch_classify; | ||
| 42 | Missing required arguments: | ||
| 43 | --skip-config=<file> | ||
| 44 | EOT | ||
| 45 | |||
| 46 | # setup OmpaPa sub-class based on report type | ||
| 47 | my $class = $ARGV_report_type eq 'blastxml' ? Blast : Hmmer; | ||
| 48 | |||
| 49 | # setup Taxonomy-related objects | ||
| 50 | my $scheme; | ||
| 51 | my $classifier; | ||
| 52 | if ($ARGV_taxdir) { | ||
| 53 | my $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir ); | ||
| 54 | $scheme = $ARGV_colorize ? $tax->load_color_scheme($ARGV_colorize) | ||
| 55 | : ColorScheme->new( | ||
| 56 | tax => $tax, | ||
| 57 | names => [ qw(Archaea Bacteria Eukaryota Viruses) ], | ||
| 58 | colors => [ qw( blue green red orange) ], | ||
| 59 | ) | ||
| 60 | ; | ||
| 61 | |||
| 62 | if ($ARGV_skip_config) { | ||
| 63 | # read configuration file | ||
| 64 | my $skip_config = Config::Any->load_files( { | ||
| 65 | files => [ $ARGV_skip_config ], | ||
| 66 | flatten_to_hash => 1, | ||
| 67 | use_ext => 1, | ||
| 68 | } ); | ||
| 69 | ### skip_config: $skip_config->{$ARGV_skip_config} | ||
| 70 | $classifier = $tax->tax_classifier( $skip_config->{$ARGV_skip_config} ); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | # build args hash for Bio::MUST::Apps::OmpaPa::XXX constructor | ||
| 75 | my %args; | ||
| 76 | $args{gnuplot_term} = $ARGV_gnuplot_term; | ||
| 77 | $args{database} = $ARGV_database if $ARGV_database; | ||
| 78 | $args{extract_seqs} = 1 if $ARGV_extract_seqs; | ||
| 79 | $args{extract_taxs} = 1 if $ARGV_extract_tax; | ||
| 80 | $args{restore_last_params} = 1 if $ARGV_restore_last_params; | ||
| 81 | $args{scheme} = $scheme if $scheme; | ||
| 82 | |||
| 83 | ### Silencing most Taxonomy-related warnings to reduce interactive clutter | ||
| 84 | local $SIG{__WARN__} = Taxonomy->no_warnings; | ||
| 85 | |||
| 86 | FILE: | ||
| 87 | for my $infile (@ARGV_infiles) { | ||
| 88 | |||
| 89 | ### Processing: $infile | ||
| 90 | my $inf_obj = file($infile); | ||
| 91 | |||
| 92 | # enforce precedence order for Parameters (supercede class defaults) | ||
| 93 | if ($ARGV_restore_last_params) { | ||
| 94 | delete $args{parameters}; | ||
| 95 | } | ||
| 96 | elsif ($ARGV_restore_params_from) { | ||
| 97 | $args{parameters} = $ARGV_restore_params_from; | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | $args{parameters} = Parameters->new( | ||
| 101 | max_hits => $ARGV_max_hits, | ||
| 102 | max_copy => $ARGV_max_copy, | ||
| 103 | min_cov => $ARGV_min_cov, | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | # read and parse report | ||
| 108 | my $oum = $class->new( %args, file => $infile ); | ||
| 109 | say '[' . $oum->count_hits . ' hits processed] '; | ||
| 110 | |||
| 111 | # optionally skip report | ||
| 112 | if ($classifier) { | ||
| 113 | |||
| 114 | # optionally create output directories named after categories | ||
| 115 | # Note: this could happen in different indirs (depending on infiles) | ||
| 116 | if ($ARGV_batch_classify) { | ||
| 117 | for my $cat ( $classifier->all_categories ) { | ||
| 118 | my $subdir = dir( $inf_obj->dir, $cat->label )->relative; | ||
| 119 | $subdir->mkpath(); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | # extract all hit ids | ||
| 124 | my @hits = map { $_->{acc} } $oum->all_hits; | ||
| 125 | my $listable = IdList->new( ids => \@hits ); | ||
| 126 | |||
| 127 | # classify report | ||
| 128 | my $cat_label = $classifier->classify($listable); | ||
| 129 | if ($cat_label) { | ||
| 130 | ### Skipping report: "$infile | $cat_label" | ||
| 131 | |||
| 132 | # optionally copy report to corresponding output directory | ||
| 133 | if ($ARGV_batch_classify) { | ||
| 134 | my $subdir = dir( $inf_obj->dir, $cat_label )->relative; | ||
| 135 | $inf_obj->copy_to( file($subdir, $inf_obj->basename) ); | ||
| 136 | } | ||
| 137 | |||
| 138 | next FILE; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | if ($ARGV_print_plots) { | ||
| 143 | my $suf = 'before'; | ||
| 144 | my $all = 'Y'; | ||
| 145 | |||
| 146 | ### print graph with all hits and with all possible colorations... | ||
| 147 | $oum->print_plot( $suf, $all ); | ||
| 148 | } | ||
| 149 | |||
| 150 | # BATCH MODE | ||
| 151 | if ($ARGV_restore_params_from) { | ||
| 152 | say q{Here's the selection based on restored parameters...}; | ||
| 153 | say $oum->list_selection('all'); | ||
| 154 | say '[' . $oum->count_selection . ' hits selected] '; | ||
| 155 | say '[' . $oum->count_filter . ' hits selected after filtering] '; | ||
| 156 | } | ||
| 157 | |||
| 158 | elsif ($ARGV_restore_last_params) { | ||
| 159 | say q{Here's the selection based on restored parameters...}; | ||
| 160 | say $oum->list_selection('keep'); | ||
| 161 | say '[' . $oum->count_filter . ' hits selected after filtering] '; | ||
| 162 | } | ||
| 163 | |||
| 164 | # INTERACTIVE MODE | ||
| 165 | else { | ||
| 166 | |||
| 167 | # repeat until user's confirmation of selected bounds | ||
| 168 | my $ans; | ||
| 169 | do { | ||
| 170 | $oum->change_filter if $ans; | ||
| 171 | $oum->select_bounds; | ||
| 172 | |||
| 173 | say q{Here's your current selection...}; | ||
| 174 | say $oum->list_selection('all'); | ||
| 175 | say '[' . $oum->count_selection . ' hits selected] '; | ||
| 176 | say '[' . $oum->count_filter . ' hits selected after filtering] '; | ||
| 177 | say 'Threshold for minimum coverage: ' . $oum->min_cov; | ||
| 178 | say 'Threshold for maximum gene copy number: ' . $oum->max_copy; | ||
| 179 | |||
| 180 | my $opt = prompt 'Do you want to see the filtered selection?', | ||
| 181 | -def => 'Y'; | ||
| 182 | say $oum->list_selection('keep') if uc $opt eq 'Y'; | ||
| 183 | |||
| 184 | $ans = prompt 'Are you satisfied with your current selection?', | ||
| 185 | -def => 'Y'; | ||
| 186 | } until uc $ans eq 'Y'; | ||
| 187 | } | ||
| 188 | |||
| 189 | if ($ARGV_print_plots) { | ||
| 190 | my $suf = 'after'; | ||
| 191 | |||
| 192 | ### print graphical selection with all possible colorations... | ||
| 193 | $oum->print_plot($suf); | ||
| 194 | } | ||
| 195 | |||
| 196 | # write accession file corresponding to current selection | ||
| 197 | $oum->save_selection; | ||
| 198 | } | ||
| 199 | |||
| 200 | __END__ |