| 1 | package Bio::Gonzales::Align::IO::MAF; | ||
| 2 | |||
| 3 | use Mouse; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | use 5.010; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Bio::Gonzales::Feat; | ||
| 11 | use Bio::Gonzales::Util qw/invslice/; | ||
| 12 | use Bio::Gonzales::Seq; | ||
| 13 | use Bio::Gonzales::Align; | ||
| 14 | |||
| 15 | with 'Bio::Gonzales::Util::Role::FileIO'; | ||
| 16 | our $VERSION = '0.058'; # VERSION | ||
| 17 | |||
| 18 | sub BUILD { | ||
| 19 | my ($self) = @_; | ||
| 20 | |||
| 21 | $self->record_separator(""); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub next_aln { | ||
| 25 | my ( $self, $noseq ) = @_; | ||
| 26 | |||
| 27 | my $fhi = $self->_fhi; | ||
| 28 | |||
| 29 | my $record = $fhi->(); | ||
| 30 | |||
| 31 | return unless ($record); | ||
| 32 | |||
| 33 | my @lines = grep { !/^#/ } split /\n/, $record; | ||
| 34 | |||
| 35 | return unless ( @lines > 2 ); | ||
| 36 | |||
| 37 | die "this is not a maf alignment file::" . $record . "::" | ||
| 38 | unless ( $lines[0] =~ /^a/ ); | ||
| 39 | |||
| 40 | my $a = _a_line( shift(@lines) ); | ||
| 41 | my $s = _s_lines( \@lines, $noseq ); | ||
| 42 | |||
| 43 | return Bio::Gonzales::Align->new( seqs => $s, score => ( exists( $a->{score} ) ? $a->{score} : -1 ) ); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _a_line { | ||
| 47 | my ($line) = @_; | ||
| 48 | |||
| 49 | ( undef, my @parts ) = split /\s+/, $line; | ||
| 50 | |||
| 51 | my %a_hash; | ||
| 52 | for my $p (@parts) { | ||
| 53 | my ( $k, $v ) = split /=/, $p, 2; | ||
| 54 | $a_hash{$k} = $v; | ||
| 55 | } | ||
| 56 | |||
| 57 | return \%a_hash; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _s_lines { | ||
| 61 | my ($lines) = @_; | ||
| 62 | |||
| 63 | my @s; | ||
| 64 | for my $l (@$lines) { | ||
| 65 | my ( undef, $src, $begin, $len, $strand, $src_size, $seq ) = split /\s+/, $l; | ||
| 66 | |||
| 67 | if ( $strand eq '-' ) { $strand = -1; } | ||
| 68 | elsif ( $strand eq '+' ) { $strand = 1; } | ||
| 69 | else { $strand = 0; } | ||
| 70 | |||
| 71 | push @s, | ||
| 72 | Bio::Gonzales::Seq->new( | ||
| 73 | { | ||
| 74 | id => $src, | ||
| 75 | info => { | ||
| 76 | start => $begin + 1, | ||
| 77 | end => $begin + $len, | ||
| 78 | length => $len, | ||
| 79 | seq_length => $src_size, | ||
| 80 | strand => $strand | ||
| 81 | }, | ||
| 82 | seq => $seq | ||
| 83 | } | ||
| 84 | ); | ||
| 85 | |||
| 86 | } | ||
| 87 | return \@s; | ||
| 88 | } | ||
| 89 | |||
| 90 | __PACKAGE__->meta->make_immutable; | ||
| 91 | |||
| 92 | 1; |