| 1 | package Bioinfo::App::Cmd::Blast::Cmd::ParseXML; | ||
| 2 | use Modern::Perl; | ||
| 3 | use Moo; | ||
| 4 | use MooX::Cmd; | ||
| 5 | use MooX::Options prefer_commandline => 1; | ||
| 6 | use IO::All; | ||
| 7 | use XML::Twig; | ||
| 8 | |||
| 9 | our $VERSION = '0.1.14'; # VERSION: | ||
| 10 | # ABSTRACT: parse XML file of blast+(with outfmt=5) into tabular format | ||
| 11 | |||
| 12 | |||
| 13 | option input => ( | ||
| 14 | is => 'ro', | ||
| 15 | required => 1, | ||
| 16 | format => 's', | ||
| 17 | short => 'i', | ||
| 18 | doc => 'a file of xml format generated by blast+' | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | option output => ( | ||
| 23 | is => 'ro', | ||
| 24 | format => 's', | ||
| 25 | short => 'o', | ||
| 26 | doc => 'the outfile name of outfmt6', | ||
| 27 | ); | ||
| 28 | |||
| 29 | my $out_handle; | ||
| 30 | my $out_handle_s; | ||
| 31 | |||
| 32 | |||
| 33 | sub execute { | ||
| 34 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 35 | $self->options_usage unless (@$args_ref); | ||
| 36 | my $input = $self->input; | ||
| 37 | $out_handle = io($self->output); | ||
| 38 | $out_handle_s = io($self->output . ".m8"); | ||
| 39 | my $twig = XML::Twig->new( | ||
| 40 | twig_handlers => { | ||
| 41 | Iteration => \&_iteration, | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | $twig->parsefile($input); | ||
| 45 | say "finished to parse $input to $out_handle"; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub _iteration { | ||
| 50 | my ($self, $iteration) = @_; | ||
| 51 | |||
| 52 | # iteration's content | ||
| 53 | my $query_name = $iteration->first_child_text("Iteration_query-ID"); | ||
| 54 | my @hits = $iteration->descendants("Hit"); | ||
| 55 | for my $hit (@hits) { | ||
| 56 | |||
| 57 | # hit's info | ||
| 58 | my $hit_name = $hit->first_child_text("Hit_id"); | ||
| 59 | my $hit_def = $hit->first_child_text("Hit_def"); | ||
| 60 | my @hsps = $hit->descendants("Hsp"); | ||
| 61 | for my $hsp (@hsps) { | ||
| 62 | |||
| 63 | # hsp's information | ||
| 64 | my $score = $hsp->first_child_text("Hsp_bit-score"); | ||
| 65 | my $evalue = $hsp->first_child_text("Hsp_evalue"); | ||
| 66 | my $q_start = $hsp->first_child_text("Hsp_query-from"); | ||
| 67 | my $q_end = $hsp->first_child_text("Hsp_query-to"); | ||
| 68 | my $s_start = $hsp->first_child_text("Hsp_hit-from"); | ||
| 69 | my $s_end = $hsp->first_child_text("Hsp_hit-to"); | ||
| 70 | my $identity = $hsp->first_child_text("Hsp_identity"); | ||
| 71 | my $gaps = $hsp->first_child_text("Hsp_gaps"); | ||
| 72 | my $len = $hsp->first_child_text("Hsp_align-len"); | ||
| 73 | my $mismatches = $len - $identity - $gaps; | ||
| 74 | $score = int($score); | ||
| 75 | $identity = sprintf("%.3f", $identity / $len * 100, 3); | ||
| 76 | $query_name =~s/(.+?)\s+(.+)/$1/; | ||
| 77 | my @cols = ($query_name, $hit_name, $identity, $len, $mismatches, $gaps, $q_start, $q_end, $s_start, $s_end, $evalue, $score, $hit_def); | ||
| 78 | my $line = join("\t", @cols) . "\n"; | ||
| 79 | $out_handle->print($line); | ||
| 80 | pop (@cols); | ||
| 81 | $line = join("\t", @cols) . "\n"; | ||
| 82 | $out_handle_s->print($line); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | $iteration->purge; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | __END__ | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =encoding UTF-8 | ||
| 96 | |||
| 97 | =head1 NAME | ||
| 98 | |||
| 99 | Bioinfo::App::Cmd::Blast::Cmd::ParseXML - parse XML file of blast+(with outfmt=5) into tabular format | ||
| 100 | |||
| 101 | =head1 VERSION | ||
| 102 | |||
| 103 | version 0.1.14 | ||
| 104 | |||
| 105 | =head1 SYNOPSIS | ||
| 106 | |||
| 107 | use Bioinfo::App::Cmd::Blast::Cmd::ParseXML; | ||
| 108 | Bioinfo::App::Cmd::Blast::Cmd::ParseXML->new_with_cmd; | ||
| 109 | ... | ||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | parse XML file of blast+(with outfmt=5) into tabular format. | ||
| 114 | In addition, the definition of a sequence is added to the last | ||
| 115 | column of each row, as which is usually important to our job. | ||
| 116 | |||
| 117 | =head1 ATTRIBUTES | ||
| 118 | |||
| 119 | =head2 input | ||
| 120 | |||
| 121 | a file of xml format generated by blast+ | ||
| 122 | |||
| 123 | =head2 output | ||
| 124 | |||
| 125 | the outfile name of tabular format(similar to the result of blast+ with parameter outfmt=6. | ||
| 126 | |||
| 127 | =head1 METHODS | ||
| 128 | |||
| 129 | =head2 execute | ||
| 130 | |||
| 131 | =head2 _iteration | ||
| 132 | |||
| 133 | the iteration routine used by Twig; | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | Yan Xueqing <yanxueqing621@163.com> | ||
| 138 | |||
| 139 | =head1 COPYRIGHT AND LICENSE | ||
| 140 | |||
| 141 | This software is copyright (c) 2017 by Yan Xueqing. | ||
| 142 | |||
| 143 | This is free software; you can redistribute it and/or modify it under | ||
| 144 | the same terms as the Perl 5 programming language system itself. | ||
| 145 | |||
| 146 | =cut |