« blib/lib/Bio/Gonzales/Seq/Validate/fasta.pm
↑ Summary
blib/lib/Bio/Gonzales/Util/Development/File.pm »
| 1 | package Bio::Gonzales::Feat::IO::MUM; | ||
| 2 | |||
| 3 | use Mouse; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | use 5.010; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | use List::MoreUtils qw/zip/; | ||
| 12 | use Bio::Gonzales::Feat; | ||
| 13 | use Data::Dumper; | ||
| 14 | use Carp; | ||
| 15 | use Scalar::Util qw/blessed/; | ||
| 16 | use Bio::Gonzales::Seq::Util qw/strand_convert/; | ||
| 17 | |||
| 18 | extends 'Bio::Gonzales::Feat::IO::Base'; | ||
| 19 | |||
| 20 | has _current_seq_id => ( is => 'rw' ); | ||
| 21 | has _current_strand => ( is => 'rw' ); | ||
| 22 | has _current_num => ( is => 'rw', default => 0 ); | ||
| 23 | has query_reverse_match_orientation => ( is => 'rw', default => 1 ); | ||
| 24 | has seq_id => ( is => 'rw', default => 'ref01' ); | ||
| 25 | has format => ( is => 'rw', default => 3 ); | ||
| 26 | has invert => (is => 'rw'); | ||
| 27 | |||
| 28 | our $VERSION = '0.062'; # VERSION | ||
| 29 | |||
| 30 | sub BUILD { | ||
| 31 | my ($self) = @_; | ||
| 32 | |||
| 33 | $self->_parse_header if ( $self->mode eq '<' ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _parse_header { | ||
| 37 | my ($self) = @_; | ||
| 38 | |||
| 39 | my $fhi = $self->_fhi; | ||
| 40 | |||
| 41 | my $l; | ||
| 42 | while ( defined( $l = $fhi->() ) ) { | ||
| 43 | next if ( !$l || $l =~ /^\s*$/ ); | ||
| 44 | last if ( $l =~ /^>/ ); | ||
| 45 | } | ||
| 46 | |||
| 47 | push @{ $self->_cached_records }, $l; | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub next_feat { | ||
| 52 | my ($self) = @_; | ||
| 53 | |||
| 54 | my $fhi = $self->_fhi; | ||
| 55 | |||
| 56 | my $l; | ||
| 57 | my $cur_id = $self->_current_seq_id; | ||
| 58 | my $cur_strand = $self->_current_strand; | ||
| 59 | while ( defined( $l = $fhi->() ) ) { | ||
| 60 | my ( $seq_id, $rstart, $qstart, $len ); | ||
| 61 | if ( $l =~ /^>\s*(.*)/ ) { | ||
| 62 | $cur_id = $1; | ||
| 63 | if ( $cur_id =~ s/\s+Reverse//i ) { | ||
| 64 | $cur_strand = -1; | ||
| 65 | } else { | ||
| 66 | $cur_strand = 1; | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->_current_seq_id($cur_id); | ||
| 70 | $self->_current_strand($cur_strand); | ||
| 71 | next; | ||
| 72 | } elsif ( $l =~ /^\s+/ ) { | ||
| 73 | if ( $self->format eq '3' ) { | ||
| 74 | # 3 col format | ||
| 75 | ( undef, $rstart, $qstart, $len, my $rest ) = split /\s+/, $l; | ||
| 76 | if($rest) { | ||
| 77 | confess "it seems that you have 4 column output format"; | ||
| 78 | } | ||
| 79 | $seq_id = $self->seq_id; | ||
| 80 | } else { | ||
| 81 | # 4 col format | ||
| 82 | ( undef, $seq_id, $rstart, $qstart, $len ) = split /\s+/, $l; | ||
| 83 | if(not defined($len)) { | ||
| 84 | confess "it seems that you have 3 column output format"; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | if($self->invert) { | ||
| 88 | return Bio::Gonzales::Feat->new( | ||
| 89 | seq_id => $cur_id, | ||
| 90 | source => 'mummer', | ||
| 91 | type => 'match', | ||
| 92 | start => $qstart, | ||
| 93 | end => $qstart + $len, | ||
| 94 | strand => $cur_strand, | ||
| 95 | attributes => { | ||
| 96 | ID => [ $self->_next_id ], | ||
| 97 | Target => [ join( " ", $seq_id, $rstart, $rstart + $len, '+') ], | ||
| 98 | Query_reverse_match_orientation => [ $self->query_reverse_match_orientation ], | ||
| 99 | Ontology_term => ['SO:0000343'], | ||
| 100 | Match_len => [$len], | ||
| 101 | }, | ||
| 102 | ); | ||
| 103 | } else { | ||
| 104 | return Bio::Gonzales::Feat->new( | ||
| 105 | seq_id => $seq_id, | ||
| 106 | source => 'mummer', | ||
| 107 | type => 'match', | ||
| 108 | start => $rstart, | ||
| 109 | end => $rstart + $len, | ||
| 110 | strand => 1, | ||
| 111 | attributes => { | ||
| 112 | ID => [ $self->_next_id ], | ||
| 113 | Target => [ join( " ", $cur_id, $qstart, $qstart + $len, strand_convert($cur_strand) ) ], | ||
| 114 | Query_reverse_match_orientation => [ $self->query_reverse_match_orientation ], | ||
| 115 | Ontology_term => ['SO:0000343'], | ||
| 116 | Match_len => [$len], | ||
| 117 | }, | ||
| 118 | ); | ||
| 119 | } | ||
| 120 | } else { | ||
| 121 | last; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | $self->close; | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _next_id { | ||
| 129 | my $self = shift; | ||
| 130 | my $i = $self->_current_num; | ||
| 131 | $self->_current_num( ++$i ); | ||
| 132 | return sprintf( "match_%09d", $i ); | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; |