Bio/SeqIO/gcg.pm

filter: Statementstmt 95.4% 104 / 109 Branchbran 69.7% 46 / 66 Conditioncond 56.2% 9 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 81.9% 173 / 211 CC 42 SCAR 27.8
CC
42
Cov
83%
write_seq
21.2
next_seq
20.5
_validate_checksum
4.0
CRAP 16.1
« Bio/SeqIO/fastq.pm ↑ Summary Bio/DB/Failover.pm »
1 #
2 # BioPerl module for Bio::SeqIO::gcg
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Ewan Birney <birney@ebi.ac.uk>
7 # and Lincoln Stein <lstein@cshl.org>
8 #
9 # Copyright Ewan Birney & Lincoln Stein
10 #
11 # You may distribute this module under the same terms as perl itself
12 #
13 # _history
14 # October 18, 1999 Largely rewritten by Lincoln Stein
15  
16 # POD documentation - main docs before the code
17  
18 =head1 NAME
19  
20 Bio::SeqIO::gcg - GCG sequence input/output stream
21  
22 =head1 SYNOPSIS
23  
24 Do not use this module directly. Use it via the Bio::SeqIO class.
25  
26 =head1 DESCRIPTION
27  
28 This object can transform Bio::Seq objects to and from GCG flat
29 file databases.
30  
31 =head1 FEEDBACK
32  
33 =head2 Mailing Lists
34  
35 User feedback is an integral part of the evolution of this and other
36 Bioperl modules. Send your comments and suggestions preferably to one
37 of the Bioperl mailing lists. Your participation is much appreciated.
38  
39 bioperl-l@bioperl.org - General discussion
40 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
41  
42 =head2 Support
43  
44 Please direct usage questions or support issues to the mailing list:
45  
46 I<bioperl-l@bioperl.org>
47  
48 rather than to the module maintainer directly. Many experienced and
49 reponsive experts will be able look at the problem and quickly
50 address it. Please include a thorough description of the problem
51 with code and data examples if at all possible.
52  
53 =head2 Reporting Bugs
54  
55 Report bugs to the Bioperl bug tracking system to help us keep track
56 the bugs and their resolution. Bug reports can be submitted via the web:
57  
58 https://github.com/bioperl/bioperl-live/issues
59  
60 =head1 AUTHORS - Ewan Birney & Lincoln Stein
61  
62 Email: E<lt>birney@ebi.ac.ukE<gt>
63 E<lt>lstein@cshl.orgE<gt>
64  
65 =head1 CONTRIBUTORS
66  
67 Jason Stajich, jason@bioperl.org
68  
69 =head1 APPENDIX
70  
71 The rest of the documentation details each of the object
72 methods. Internal methods are usually preceded with a _
73  
74 =cut
75  
76 # Let the code begin...
77  
78 package Bio::SeqIO::gcg;
79 6 $Bio::SeqIO::gcg::VERSION = '1.7.8';
80 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 6 ▶use Bio::Seq::SeqFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 6 ▶use base qw(Bio::SeqIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 8 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=7.0
87 8 my($self,@args) = @_;
88 8 $self->SUPER::_initialize(@args);
89 8 ▶ if( ! defined $self->sequence_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->sequence_factory) 8 0 8
90 8 $self->sequence_factory(Bio::Seq::SeqFactory->new
91 (-verbose => $self->verbose(),
92 -type => 'Bio::Seq::RichSeq'));
93 }
94 }
95  
96 =head2 next_seq
97  
98 Title : next_seq
99 Usage : $seq = $stream->next_seq()
100 Function: returns the next sequence in the stream
101 Returns : Bio::Seq object
102 Args :
103  
104 =cut
105  
106 7 ▶sub next_seq {
Subroutine next_seq: called CC=18 SCAR=30.2
Pod documented
107 7 my ($self,@args) = @_;
108 7 my($id,$type,$desc,$line,$chksum,$sequence,$date,$len);
109  
110 7 ▶ while( defined($_ = $self->_readline()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_readline)) 13 2 15
111  
112 ## Get the descriptive info (anything before the line with '..')
113 13 ▶ unless( /\.\.$/ ) { $desc.= $_; }
Branch: 1 branch
BranchTrueFalseTotal
unless (/\.\.$/) 8 5 13
114 ## Pull ID, Checksum & Type from the line containing '..'
115 13 ▶ /\.\.$/ && do { $line = $_; chomp;
Branch: 1 branch
BranchTrueFalseTotal
/\.\.$/ and do { $line = $_; chomp $_; if (/Check\:\s(\d+)\s/) { $chksum = $1; } ; if (/Type:\s(\w)\s/) { $type = $1; } ; if (/(\S+)\s+Length/) { $id = $1; } ; if (/Length:\s+(\d+)\s+(\S.+\S)\s+Type/) { $len = $1; $date = $2; } ; last } 5 8 13
116 5 ▶ if(/Check\:\s(\d+)\s/) { $chksum = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if (/Check\:\s(\d+)\s/) 5 0 5
117 5 ▶ if(/Type:\s(\w)\s/) { $type = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if (/Type:\s(\w)\s/) 5 0 5
118 5 ▶ if(/(\S+)\s+Length/)
Branch: 1 branch
BranchTrueFalseTotal
if (/(\S+)\s+Length/) 5 0 5
119 5 { $id = $1; }
120 5 ▶ if(/Length:\s+(\d+)\s+(\S.+\S)\s+Type/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/Length:\s+(\d+)\s+(\S.+\S)\s+Type/) 5 0 5
121 5 { $len = $1; $date = $2;}
122 5 last;
123 }
124 }
125 7 ▶ return if ( !defined $_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 2 5 7
126 5 chomp($desc); # remove last "\n"
127  
128 5 ▶ while( defined($_ = $self->_readline()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_readline)) 70 5 75
129  
130 ## This is where we grab the sequence info.
131  
132 70 ▶ if( /\.\.$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\.\.$/) 0 70 70
133 0 $self->throw("Looks like start of another sequence. See documentation. ");
134 }
135  
136 70 ▶ next if($_ eq "\n"); ## skip whitespace lines in formatted seq
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "\n" 35 35 70
137 35 s/[\d\s\t]//g; ## remove anything that is not alphabet char: preserve anything that is not explicitly specified for removal (Stefan Kirov)
138 # $_ = uc($_); ## uppercase sequence: NO. Keep the case. HL
139 35 $sequence .= $_;
140 }
141 ##If we parsed out a checksum, we might as well test it
142  
143 5 ▶ if(defined $chksum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chksum) 5 0 5
144 5 ▶ unless(_validate_checksum(uc($sequence),$chksum)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_validate_checksum(uc $sequence, $chksum)) 0 5 5
145 0 $self->throw("Checksum failure on parsed sequence.");
146 }
147 }
148  
149 ## Remove whitespace from identifier because the constructor
150 ## will throw a warning otherwise...
151 5 ▶ if(defined $id) { $id =~ s/\s+//g;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 5 0 5
152  
153 ## Turn our parsed "Type: N" or "Type: P" (if found) into the appropriate
154 ## keyword that the constructor expects...
155 5 ▶ if(defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 5 0 5
156 0 ▶ if($type eq "N") { $type = "dna"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "N") 0 5 5
157 5 ▶ if($type eq "P") { $type = "prot"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "P") 5 0 5
158 }
159  
160 5 return $self->sequence_factory->create(-seq => $sequence,
161 -id => $id,
162 -desc => $desc,
163 -type => $type,
164 -dates => [ $date ]
165 );
166 }
167  
168 =head2 write_seq
169  
170 Title : write_seq
171 Usage : $stream->write_seq(@seq)
172 Function: writes the formatted $seq object into the stream
173 Returns : 1 for success and 0 for error
174 Args : array of Bio::PrimarySeqI object
175  
176  
177 =cut
178  
179 3 ▶sub write_seq {
Subroutine write_seq: called CC=18 SCAR=30.5
Pod documented
180 3 my ($self,@seq) = @_;
181 3 for my $seq (@seq) {
182 3 ▶ $self->throw("Did not provide a valid Bio::PrimarySeqI object")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq and ref $seq and $seq->isa("Bio::PrimarySeqI") 0 3 3
Condition33%2/6
defined $seq and ref $seq and $seq->isa("Bio::PrimarySeqI")
!ll&&!rl&&r
003
defined $seq and ref $seq
!ll&&!rl&&r
003
MC/DC0%0/3
defined $seq and ref $seq and $seq->isa("Bio::PrimarySeqI")
defined $seq ref $seq $seq->isa("Bio::PrimarySeqI")
Truth table25%1/4
A and B and C
A= defined $seqB= ref $seqC= $seq->isa("Bio::PrimarySeqI")
ABCresult
0XX0
10X0
1100
1111
183 unless defined $seq && ref($seq) && $seq->isa('Bio::PrimarySeqI');
184  
185 3 ▶ $self->warn("No whitespace allowed in GCG ID [". $seq->display_id. "]")
Branch: 1 branch
BranchTrueFalseTotal
if $seq->display_id =~ /\s/ 0 3 3
186 if $seq->display_id =~ /\s/;
187  
188 3 my $str = $seq->seq;
189 3 ▶ my $comment = $seq->desc || '';
Condition50%1/2
$seq->desc || ''
l!l
30
MC/DC0%0/1
$seq->desc || ''
$seq->desc
Truth table50%1/2
A
A= $seq->desc
Aresult
11
00
190 3 my $id = $seq->id;
191 3 ▶ my $type = ( $seq->alphabet() =~ /[dr]na/i ) ? 'N' : 'P';
Branch: 1 branch
BranchTrueFalseTotal
$seq->alphabet =~ /[dr]na/i ? : 0 3 3
192 3 my $timestamp;
193  
194 3 ▶ if( $seq->can('get_dates') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->can('get_dates')) { } 3 0 3
195 3 ($timestamp) = $seq->get_dates;
196 } else {
197 0 $timestamp = localtime(time);
198 }
199 3 my($sum,$offset,$len,$i,$j,$cnt,@out);
200  
201 3 $len = length($str);
202 ## Set the offset if we have any non-standard numbering going on
203 3 $offset=1;
204 # checksum
205 3 $sum = $self->GCG_checksum($seq);
206  
207 #Output the sequence header info
208 3 push(@out,"$comment\n");
209 3 push(@out,"$id Length: $len $timestamp Type: $type Check: $sum ..\n\n");
210  
211 #Format the sequence
212 3 $i = $#out + 1;
213 3 ▶ for($j = 0 ; $j < $len ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $len) 108 3 111
214 108 ▶ if( $j % 50 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j % 50 == 0) 24 84 108
215 24 $out[$i] = sprintf("%8d ",($j+$offset)); #numbering
216 }
217 108 $out[$i] .= sprintf("%s",substr($str,$j,10));
218 108 $j += 10;
219 108 ▶ if( $j < $len && $j % 50 != 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($j < $len and $j % 50 != 0) { } 84 24 108
elsif ($j % 50 == 0) { } 21 3 24
Condition100%3/3
$j < $len and $j % 50 != 0
!ll&&!rl&&r
32184
MC/DC100%2/2
$j < $len and $j % 50 != 0
$j < $len $j % 50 != 0
Truth table100%3/3
A and B
A= $j < $lenB= $j % 50 != 0
ABresult
0X0
100
111
220 84 $out[$i] .= " ";
221 }elsif($j % 50 == 0 ) {
222 21 $out[$i++] .= "\n\n";
223 }
224 }
225 3 local($^W) = 0;
226 3 ▶ if($j % 50 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j % 50 != 0) 3 0 3
227 3 $out[$i] .= "\n";
228 }
229 3 $out[$i] .= "\n";
230 3 ▶ return unless $self->_print(@out);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_print(@out) 0 3 3
231 }
232  
233 3 ▶ $self->flush if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 3 0 3
Condition33%1/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
003
MC/DC0%0/2
$self->_flush_on_write and defined $self->_fh
$self->_flush_on_write defined $self->_fh
Truth table33%1/3
A and B
A= $self->_flush_on_writeB= defined $self->_fh
ABresult
0X0
100
111
234 3 return 1;
235 }
236  
237 =head2 GCG_checksum
238  
239 Title : GCG_checksum
240 Usage : $cksum = $gcgio->GCG_checksum($seq);
241 Function : returns a gcg checksum for the sequence specified
242  
243 This method can also be called as a class method.
244 Example :
245 Returns : a GCG checksum string
246 Argument : a Bio::PrimarySeqI implementing object
247  
248 =cut
249  
250 25 ▶sub GCG_checksum {
Subroutine GCG_checksum: called CC=4 SCAR=13.9
Pod documented
251 25 my ($self,$seqobj) = @_;
252 25 my $index = 0;
253 25 my $checksum = 0;
254 25 my $char;
255  
256 25 my $seq = $seqobj->seq();
257 25 $seq =~ tr/a-z/A-Z/;
258  
259 25 foreach $char ( split(/[\.\-]*/, $seq)) {
260 6280 $index++;
261 6280 ▶ $checksum += ($index * (unpack("c",$char) || 0) );
Condition100%2/2
unpack('c', $char) || 0
l!l
62755
MC/DC100%1/1
unpack('c', $char) || 0
unpack('c', $char)
Truth table100%2/2
A
A= unpack('c', $char)
Aresult
11
00
262 6280 ▶ if( $index == 57 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 57) 105 6175 6280
263 105 $index = 0;
264 }
265 }
266  
267 25 return ($checksum % 10000);
268 }
269  
270 =head2 _validate_checksum
271  
272 Title : _validate_checksum
273 Usage : n/a - internal method
274 Function: if parsed gcg sequence contains a checksum field
275 : we compare it to a value computed here on the parsed
276 : sequence. A checksum mismatch would indicate some
277 : type of parsing failure occurred.
278 :
279 Returns : 1 for success, 0 for failure
280 Args : string containing parsed seq, value of parsed checksum
281  
282  
283 =cut
284  
285 5 ▶sub _validate_checksum {
Subroutine _validate_checksum: called CC=4 SCAR=13.9
286 5 my($seq,$parsed_sum) = @_;
287 5 my($i,$len,$computed_sum,$cnt);
288  
289 5 $len = length($seq);
290  
291 #Generate the GCG Checksum value
292  
293 5 ▶ for($i=0; $i<$len ;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 1578 5 1583
294 1578 $cnt++;
295 1578 $computed_sum += $cnt * ord(substr($seq,$i,1));
296 1578 ▶ ($cnt == 57) && ($cnt=0);
Branch: 1 branch
BranchTrueFalseTotal
$cnt == 57 and $cnt = 0 26 1552 1578
297 }
298 5 $computed_sum %= 10000;
299  
300 ## Compare and decide if success or failure
301  
302 5 ▶ if($parsed_sum == $computed_sum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed_sum == $computed_sum) { } 5 0 5
303 5 return 1;
304 0 } else { return 0; }
305  
306  
307 }
308  
309 6 1;
« Bio/SeqIO/fastq.pm ↑ Summary Bio/DB/Failover.pm »