blib/lib/FASTAParse.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 89.6% 52 / 58 CC 8 SCAR 16.6
CC
8
Cov
88%
load_FASTA
8.4
BEGIN
1.0
BEGIN
1.0
CRAP 5.2
↑ Summary
1 package FASTAParse;
2  
3 # | PACKAGE | FASTAParse
4 # | AUTHOR | Todd Wylie
5 # | EMAIL | perldev@monkeybytes.org
6  
7 2 ▶use version; $VERSION = qv('0.0.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # --------------------------------------------------------------------------
14 # N E W (class CONSTRUCTOR)
15 # ==========================================================================
16 # USAGE : FASTAParse->new();
17 # PURPOSE : constructor for class
18 # RETURNS : object handle
19 # PARAMETERS : none
20 # COMMENTS : FASTA object.
21 # --------------------------------------------------------------------------
22 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
23 1 my $class = shift;
24  
25 1 my $self = {
26 _id => '',
27 _descriptors => [],
28 _comments => [],
29 _sequence => [],
30 };
31  
32 1 bless($self, $class);
33 1 return($self);
34 }
35  
36  
37 # --------------------------------------------------------------------------
38 # L O A D F A S T A (method)
39 # ==========================================================================
40 # USAGE : FASTAParse->load_FASTA();
41 # PURPOSE : loads a chunk of FASTA into the class
42 # RETURNS : none
43 # PARAMETERS : fasta => ''
44 # THROWS : croaks if no FASTA attribute or bad FASTA header
45 # COMMENTS : The user sould be passing a chunk of text (scalar) to
46 # : this method which represents 1 FASTA entry... from the
47 # : > symbol to the end of the sequence; includes line
48 # : returns.
49 # EXAMPLE : Example format:
50 # : >gi|55416189|gb|AAV50056.1| NADH dehydrogenase subunit 1 [Dasyurus hallucatus]
51 # : ;Taken from nr GenBank
52 # : MFTINLLIYIIPILLAVAFLTLIERKMLGYMQFRKGPNIVGPYGLLQPFADAVKLFTKEPLRPLTSSISIFIIAPILALT
53 # : IALTIWTPLPMPNTLLDLNLGLIFILSLSGLSVYSILWSGWASNSKYALIGALRAVAQTISYEVSLAIILLSIMLINGSF
54 # : TLKTLSITQENLWLIITTWPLAMMWYISTLAETNRAPFDLTEGESELVSGFNVEYAAGPFAMFFLAEYANIIAMNAITTI
55 # : LFLGPSLTPNLSHLNTLSFMLKTLLLTMVFLWVRASYPRFRYDQLMHLLWKNFLPMTLAM
56 # --------------------------------------------------------------------------
57 1 ▶sub load_FASTA {
Subroutine load_FASTA: called CC=8 SCAR=21.3
58 1 my ($class, %arg) = @_;
59  
60 # Check incoming FASTA format:
61 0 ▶ if (!$arg{fasta}) { croak "load_FASTA needs FASTA attribute" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"fasta"}) 0 1 1
62 0 ▶ if ($arg{fasta} !~ />/) { croak "no FASTA header found for input" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"fasta"} =~ />/) 0 1 1
63  
64 # Parse sequence and load the FASTA object:
65 1 my @lines = split( /\n/, $arg{fasta} );
66 1 foreach my $line (@lines) {
67 4 ▶ if ($line =~ /^>\S+/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^>\S+/) { } 1 3 4
elsif ($line =~ /^;\s*(.+)/) { } 1 2 3
68 # Header line:
69 1 my ($id, $descriptions) = $line =~ /^>(\S+)\s*(.*)/;
70 1 $class->{_id} = $id;
71 1 ▶ if (defined$descriptions) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $descriptions) 1 0 1
72 1 @{$class->{_descriptors}} = split( /\cA/, $descriptions );
73 }
74 }
75 elsif ($line =~ /^;\s*(.+)/) {
76 # Comment lines:
77 1 push( @{$class->{_comments}}, $1 );
78 }
79 else {
80 # Sequence lines:
81 2 $line =~ s/\s+//g;
82 2 ▶ unless( $line eq "" ) { push( @{$class->{_sequence}}, $line ) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($line eq "") 2 0 2
83 }
84 }
85 1 return($class);
86 }
87  
88  
89 # --------------------------------------------------------------------------
90 # F O R M A T F A S T A (method)
91 # ==========================================================================
92 # USAGE : $fasta->format_FASTA();
93 # PURPOSE : Manually populate the FASTA class.
94 # RETURNS : none
95 # PARAMETERS : id => '' # REQUIRED
96 # : sequnce => '' # REQUIRED
97 # : comments => []
98 # : descriptors => []
99 # : cols => ''
100 # COMMENTS : A user may manually load a FASTA object. Only ID and SEQUENCE
101 # : are required, all others are optional. The SEQUENCE attribute
102 # : should be a flat, single line scalar value (i.e., a single
103 # : string of sequence).
104 # --------------------------------------------------------------------------
105 sub format_FASTA {
106 my ($class, %arg) = @_;
107  
108 # Format the incoming sequence. Sequence must be single line,
109 # flatten sequence string. Check incoming FASTA format first:
110 if (!$arg{sequence}) { croak "format_FASTA needs SEQUENCE attribute" }
111 if (!$arg{id} ) { croak "format_FASTA needs ID attribute" }
112 my $columns = defined $arg{cols} ? $arg{cols} : 60; # Default is 60 cols.
113  
114 # If incoming sequence is multi-part, join them before breaking
115 # into FASTA lines:
116 delete( $class->{_sequence} );
117 my $fasta = join( "", $arg{sequence} );
118 my $length = length( $fasta );
119 my $pos = 0;
120 my $lines = int( $length / $columns) + 1;
121 for (my $i = 1; $i <= $lines; $i++) {
122 my $line = substr( $fasta, $pos, $columns );
123 push( @{$class->{_sequence}}, $line );
124 $pos = $pos + $columns;
125 }
126  
127 # Descriptions, comments, etc.
128 $class->{_id} = $arg{id};
129 if (defined @{$arg{comments}}) {
130 @{$class->{_comments}} = @{$arg{comments}};
131 }
132 if (defined @{$arg{descriptors}}) {
133 @{$class->{_descriptors}} = @{$arg{descriptors}};
134 }
135  
136 return( $class );
137 }
138  
139  
140 # --------------------------------------------------------------------------
141 # D U M P F A S T A (method)
142 # ==========================================================================
143 # USAGE : $fasta->dump_FASTA();
144 # PURPOSE : Accessor to dump the FASTA class into text.
145 # RETURNS : scalar (chunk of FASTA text)
146 # PARAMETERS : none
147 # --------------------------------------------------------------------------
148 sub dump_FASTA {
149 my $class = shift;
150  
151 # Dump the class in scalar context:
152 my $returnable;
153 if (defined $class->{_id}) {
154 my $descriptors = join( "\cA", @{$class->{_descriptors}} ); # ^A delimiter
155 $returnable = ">$class->{_id} $descriptors\n";
156 foreach my $comment ( @{$class->{_comments}} ) {
157 $returnable .= ";$comment\n";
158 }
159 foreach my $sequence ( @{$class->{_sequence}} ) {
160 $returnable .= "$sequence\n";
161 }
162 }
163 else {
164 croak "ID is missing from the object";
165 }
166  
167 return( $returnable );
168 }
169  
170  
171 # --------------------------------------------------------------------------
172 # S A V E F A S T A (method)
173 # ==========================================================================
174 # USAGE : $fasta->save_FASTA( save => '' );
175 # PURPOSE : Accessor to save the FASTA entry to a file.
176 # RETURNS : none
177 # PARAMETERS : save => ''
178 # --------------------------------------------------------------------------
179 sub save_FASTA {
180 my ($class, %arg) = @_;
181  
182 if (!$arg{save}) { croak "save_FASTA needs SAVE attribute" }
183  
184 # Save the class information to a file:
185 my $save = new IO::File ">>$arg{save}" or croak "could not save to file $arg{save}";
186 my $returnable;
187 if (defined $class->{_id}) {
188 my $descriptors = join( "\cA", @{$class->{_descriptors}} ); # ^A delimiter
189 $returnable = ">$class->{_id} $descriptors\n";
190 foreach my $comment ( @{$class->{_comments}} ) {
191 $returnable .= ";$comment\n";
192 }
193 foreach my $sequence ( @{$class->{_sequence}} ) {
194 $returnable .= "$sequence\n";
195 }
196 }
197 else {
198 croak "ID is missing from the object";
199 }
200 print $save "$returnable";
201  
202 return($class);
203 }
204  
205  
206 # --------------------------------------------------------------------------
207 # P R I N T (method)
208 # ==========================================================================
209 # USAGE : $fasta->print();
210 # PURPOSE : Accessor to print the FASTA class to STDOUT.
211 # RETURNS : none
212 # PARAMETERS : none
213 # --------------------------------------------------------------------------
214 sub print {
215 my $class = shift;
216  
217 # Print the class to STDOUT:
218 my $printable;
219 if (defined $class->{_id}) {
220 my $descriptors = join( "\cA", @{$class->{_descriptors}} ); # ^A delimiter
221 $printable = ">$class->{_id} $descriptors\n";
222 foreach my $comment ( @{$class->{_comments}} ) {
223 $printable .= ";$comment\n";
224 }
225 foreach my $sequence ( @{$class->{_sequence}} ) {
226 $printable .= "$sequence\n";
227 }
228 print $printable;
229 }
230 else {
231 croak "ID is missing from the object";
232 }
233  
234 return( $class );
235 }
236  
237  
238 # --------------------------------------------------------------------------
239 # I D (method)
240 # ==========================================================================
241 # USAGE : $fasta->id();
242 # PURPOSE : Accessor to retrieve the FASTA ID.
243 # RETURNS : scalar
244 # PARAMETERS : none
245 # --------------------------------------------------------------------------
246 sub id {
247 my $class = shift;
248 if (defined $class->{_id}) {
249 return( $class->{_id} );
250 }
251 else {
252 croak "ID does not exist in object";
253 }
254 }
255  
256  
257 # --------------------------------------------------------------------------
258 # S E Q U E N C E (method)
259 # ==========================================================================
260 # USAGE : $fasta->sequence();
261 # PURPOSE : Accessor to retrieve the FASTA sequence.
262 # RETURNS : scalar
263 # PARAMETERS : none
264 # --------------------------------------------------------------------------
265 sub sequence {
266 my $class = shift;
267 if (defined $class->{_sequence} ) {
268 my $sequence = join( "", @{$class->{_sequence}} );
269 return( $sequence );
270 }
271 else {
272 croak "SEQUENCE does not exist in object";
273 }
274 }
275  
276  
277 # --------------------------------------------------------------------------
278 # D E S C R I P T O R S (method)
279 # ==========================================================================
280 # USAGE : $fasta->descriptors();
281 # PURPOSE : Accessor to retrieve the FASTA descriptors.
282 # RETURNS : array reference
283 # PARAMETERS : none
284 # --------------------------------------------------------------------------
285 sub descriptors {
286 my $class = shift;
287 return( \@{$class->{_descriptors}} );
288 }
289  
290  
291 # --------------------------------------------------------------------------
292 # C O M M E N T S (method)
293 # ==========================================================================
294 # USAGE : $fasta->comments();
295 # PURPOSE : Accessor to retrieve the FASTA comments.
296 # RETURNS : array reference
297 # PARAMETERS : none
298 # --------------------------------------------------------------------------
299 sub comments {
300 my $class = shift;
301 return( \@{$class->{_comments}} );
302 }
303  
304  
305 1; # End of module.
306  
307 __END__
↑ Summary