swuecho
12/16/2014 - 4:26 AM

fasta_1_seq_obj.pl

use v5.20;
use DDP;

package Seq {
    use Moo;
    has [ 'id', 'comment', 'sequence' ] => ( is => 'rw' );
    1;
}

package FASTA {
    use Moo;

    my $parser = do {
        use Regexp::Grammars;
        qr/
    <TOP>
    <nocontext:>
    <token: TOP>  <[record]>+
    <token: record> <.start=(\>)><id><comment>?\n<sequence> 
    <token: id>  [^\-\s\n]+ 
    <token: comment> \s[^\n]+
    <token: sequence> <dna>|<rna>|<aa> 
    <token: dna> [ACGTRYKMSWBDHVNX\-\n]+ 
    <token: rna> [ACGURYKMSWBDHVNX\-\n]+ 
    <token: aa> [A-Z\*\-\n]+ 
    /;
    };

    has 'parser' => ( is => 'ro', default => sub { $parser } );

    sub record {
        my ( $self, $result ) = @_;
        return Seq->new(%$result);
    }

}

my $content = <<'END';
>hello
GCTATATAAGC
>world prot
TATAKEKEKELKL
END
my $fasta = FASTA->new();
if ( $content =~ $fasta->parser->with_actions($fasta) ) {
    p %/;
}

__END__
{
TOP   {
  record   [
	    [0] Seq  {
	      Parents       Moo::Object
		public methods (4) : comment, id, new, sequence
		private methods (0)
	      internals: {
		  id         "hello",
		    sequence   {
		                            dna   "GCTATATAAGC
"
					  }
		  }
	    },
	    [1] Seq  {
	      Parents       Moo::Object
		public methods (4) : comment, id, new, sequence
		private methods (0)
	      internals: {
		  comment    " prot",
		    id         "world",
		    sequence   {
		      dna   "TATAK"
		    }
		  }
	    }
	   ]
 }
}