| 1 | package BioX::Seq::Stream::FASTA; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use parent qw/BioX::Seq::Stream/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 15 | ▶ | sub _check_type { | ||||||||
|
Subroutine
_check_type: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 15 | my ($class,$self) = @_; | |||||||||
| 11 | 15 | return substr($self->{buffer},0,1) eq '>'; | |||||||||
| 12 | |||||||||||
| 13 | } | ||||||||||
| 14 | |||||||||||
| 15 | 10 | ▶ | sub _init { | ||||||||
|
Subroutine
_init: called CC=4 SCAR=14.0
| |||||||||||
| 16 | |||||||||||
| 17 | 10 | my ($self) = @_; | |||||||||
| 18 | |||||||||||
| 19 | # First two bytes should not contain line ending chars | ||||||||||
| 20 | die "Missing ID in initial header (check file format)\n" | ||||||||||
| 21 | 10 | ▶ | if ($self->{buffer} =~ /[\r\n]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 10 | my $fh = $self->{fh}; | |||||||||
| 23 | 10 | $self->{buffer} .= <$fh>; | |||||||||
| 24 | |||||||||||
| 25 | # detect line endings for text files based on first line | ||||||||||
| 26 | # (other solutions, such as using the :crlf layer or s/// | ||||||||||
| 27 | # instead of chomp may be marginally more robust but slow | ||||||||||
| 28 | # things down too much) | ||||||||||
| 29 | 10 | ▶ | if ($self->{buffer} =~ /([\r\n]{1,2})$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 9 | $self->{rec_sep} = $1; | |||||||||
| 31 | } | ||||||||||
| 32 | else { | ||||||||||
| 33 | 1 | die "Failed to detect line endings\n"; | |||||||||
| 34 | } | ||||||||||
| 35 | 9 | local $/ = $self->{rec_sep}; | |||||||||
| 36 | |||||||||||
| 37 | # Parse initial header line | ||||||||||
| 38 | 9 | chomp $self->{buffer}; | |||||||||
| 39 | 9 | ▶ | if ($self->{buffer} =~ /^>(\S+)\s*(.+)?$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 9 | $self->{next_id} = $1; | |||||||||
| 41 | 9 | $self->{next_desc} = $2; | |||||||||
| 42 | 9 | $self->{buffer} = undef; | |||||||||
| 43 | } | ||||||||||
| 44 | else { | ||||||||||
| 45 | 0 | die "Failed to parse initial FASTA header (check file format)\n"; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 9 | return; | |||||||||
| 49 | |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 114 | ▶ | sub next_seq { | ||||||||
|
Subroutine
next_seq: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||
| 53 | |||||||||||
| 54 | 114 | my ($self) = @_; | |||||||||
| 55 | |||||||||||
| 56 | 114 | my $fh = $self->{fh}; | |||||||||
| 57 | 114 | my $id = $self->{next_id}; | |||||||||
| 58 | 114 | my $desc = $self->{next_desc}; | |||||||||
| 59 | 114 | my $seq = ''; | |||||||||
| 60 | |||||||||||
| 61 | 114 | local $/ = $self->{rec_sep}; | |||||||||
| 62 | |||||||||||
| 63 | 114 | my $line = <$fh>; | |||||||||
| 64 | |||||||||||
| 65 | 114 | ▶ | while ($line) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 692 | chomp $line; | |||||||||
| 68 | |||||||||||
| 69 | # match next record header | ||||||||||
| 70 | 692 | ▶ | if ($line =~ /^>(\S+)\s*(.+)?$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | 108 | $self->{next_id} = $1; | |||||||||
| 73 | 108 | $self->{next_desc} = $2; | |||||||||
| 74 | |||||||||||
| 75 | # if not in fast mode, double-check sequence for correctness | ||||||||||
| 76 | 108 | ▶ | if (! $self->{fast}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | # allow any alpha character or characters possibly representing | ||||||||||
| 78 | # gaps or stop codons | ||||||||||
| 79 | 59 | ▶ | if ($seq =~ /[^A-Za-z\-\.\*]/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 1 | die "Previous sequence record invalid\n"; | |||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 107 | my $obj = BioX::Seq->new($seq, $id, $desc); | |||||||||
| 85 | 107 | $obj->{_input_format} = 'fasta'; | |||||||||
| 86 | 107 | return $obj; | |||||||||
| 87 | |||||||||||
| 88 | } | ||||||||||
| 89 | else { | ||||||||||
| 90 | 584 | $seq .= $line; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 584 | $line = <$fh>; | |||||||||
| 94 | |||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | # should only reach here on last read | ||||||||||
| 98 | 6 | ▶ | if (defined $self->{next_id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 4 | delete $self->{next_id}; | |||||||||
| 100 | 4 | delete $self->{next_desc}; | |||||||||
| 101 | |||||||||||
| 102 | # if not in fast mode, double-check sequence for correctness | ||||||||||
| 103 | 4 | ▶ | if (! $self->{fast}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | # allow any alpha character or characters possibly representing | ||||||||||
| 105 | # gaps or stop codons | ||||||||||
| 106 | 3 | ▶ | if ($seq =~ /[^A-Za-z\-\.\*]/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | die "Previous sequence record invalid\n"; | |||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 4 | my $obj = BioX::Seq->new($seq, $id, $desc); | |||||||||
| 112 | 4 | $obj->{_input_format} = 'fasta'; | |||||||||
| 113 | 4 | return $obj; | |||||||||
| 114 | } | ||||||||||
| 115 | 2 | return; | |||||||||
| 116 | |||||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 2 | 1; | |||||||||
| 120 | |||||||||||
| 121 | __END__ | ||||||||||