Bioinfo

 view release on metacpan or  search on metacpan

lib/Bioinfo/App/Cmd/Blast/Cmd/ParseXML.pm  view on Meta::CPAN

package Bioinfo::App::Cmd::Blast::Cmd::ParseXML;
use Modern::Perl;
use Moo;
use MooX::Cmd;
use MooX::Options prefer_commandline => 1;
use IO::All;
use XML::Twig;

our $VERSION = '0.1.15'; # VERSION:
# ABSTRACT: parse XML file of blast+(with outfmt=5) into tabular format


option input => (
  is  => 'ro',
  required  => 1,
  format  => 's',
  short => 'i',
  doc => 'a file of xml format generated by blast+'
);


option output => (
  is => 'ro',
  format => 's',
  short => 'o',
  doc => 'the outfile name of outfmt6',
);

my $out_handle;
my $out_handle_s;


sub execute {
  my ($self, $args_ref, $chain_ref) = @_;
  $self->options_usage unless (@$args_ref);
  my $input = $self->input;
  $out_handle = io($self->output);
  $out_handle_s = io($self->output . ".m8");
  my $twig = XML::Twig->new(
    twig_handlers => {
      Iteration => \&_iteration,
    }
  );
  $twig->parsefile($input);
  say "finished to parse $input to $out_handle";
}


sub _iteration {
  my ($self, $iteration) = @_;

  # iteration's content
  my $query_name = $iteration->first_child_text("Iteration_query-ID");
  my @hits = $iteration->descendants("Hit");
  for my $hit (@hits) {

    # hit's info
    my $hit_name = $hit->first_child_text("Hit_id");
    my $hit_def = $hit->first_child_text("Hit_def");
    my @hsps = $hit->descendants("Hsp");
    for my $hsp (@hsps) {



( run in 0.927 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )