Sequence.pm

filter: Statementstmt 24.0% 13 / 54 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 5 / 10 Podpod n/a totaltotal 16.9% 18 / 106 CC 18 SCAR 32.8
CC
18
Cov
14%
parse
56.0
next_seq
30.0
new
20.0
CRAP 26.6
« EntrezGene.pm ↑ Summary EntrezGene/Indexer.pm »
1 package Bio::ASN1::Sequence;
2 BEGIN {
3 4 ▶ $Bio::ASN1::Sequence::AUTHORITY = 'cpan:BIOPERLML';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 {
6 $Bio::ASN1::Sequence::VERSION = '1.70';
7 }
8 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Regular expression-based Perl Parser for ASN.1-formatted NCBI Sequences.
14 # AUTHOR: Dr. Mingyi Liu <mingyiliu@gmail.com>
15 # OWNER: 2005 Mingyi Liu
16 # OWNER: 2005 GPC Biotech AG
17 # OWNER: 2005 Altana Research Institute
18 # LICENSE: Perl_5
19  
20  
21  
22 sub new
23 {
24 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=30.0
25 0 ▶ $class = ref($class) if(ref($class));
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
26 0 my $self = { maxerrstr => 20, @_ };
27 0 bless $self, $class;
28 0 ▶ map { $self->input_file($self->{$_}) if($self->{$_}) } qw(file -file);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} 0 0 0
29 0 ▶ map { $self->fh($self->{$_}) if($self->{$_}) } qw(fh -fh);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} 0 0 0
30 0 return $self;
31 }
32  
33  
34 sub maxerrstr
35 {
36 0 ▶ my ($self, $value) = @_;
Subroutine maxerrstr: not called CC=2 SCAR=17.9
37 0 ▶ $self->{maxerrstr} = $value if $value > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value > 0 0 0 0
38 0 return $self->{maxerrstr};
39 }
40  
41  
42  
43 sub parse
44 {
45 0 ▶ my ($self, $input, $compact, $noreset) = @_;
Subroutine parse: not called CC=7 SCAR=40.3
46 0 ▶ $input || croak "must have input!\n";
Branch: 1 branch
BranchTrueFalseTotal
$input or croak("must have input!\n") 0 0 0
47 0 $self->{input} = $input;
48 0 ▶ $self->{filename} = "input" unless $self->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"filename"} 0 0 0
49 0 ▶ $self->{linenumber} = 1 unless $self->{linenumber} && $noreset;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"linenumber"} and $noreset 0 0 0
Condition0%0/3
$self->{"linenumber"} and $noreset
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"linenumber"} and $noreset
$self->{"linenumber"} $noreset
Truth table0%0/3
A and B
A= $self->{"linenumber"}B= $noreset
ABresult
0X0
100
111
50 0 $self->{depth} = 0;
51 0 my $result;
52 eval
53 0 {
54 0 $result = $self->_parse(); # no need to reset $self->{depth} or linenumber
55 };
56 0 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
57 {
58 0 ▶ if($@ !~ /^Data Error:/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ =~ /^Data Error:/) { } 0 0 0
59 {
60 croak "non-conforming data broke parser on line $self->{linenumber} in $self->{filename}\n".
61 "possible cause includes randomly inserted brackets in input file before line $self->{linenumber}\n".
62 "first $self->{maxerrstr} (or till end of input) characters including the non-conforming data:\n" .
63 0 substr($self->{input}, pos($self->{input}), $self->{maxerrstr}) . "\nRaw error mesg: $@\n";
64 }
65 0 else { die $@ }
66 }
67 0 trimdata($result, $compact);
68 0 return $result;
69 }
70  
71  
72 sub input_file
73 {
74 0 ▶ my ($self, $filename) = @_;
Subroutine input_file: not called CC=4 SCAR=30.0
75 # in case user's Perl system can't handle large file. Assuming Unix, otherwise raise error
76 0 local *IN; # older styled code to enable module to work with perl 5.005_03
77 0 ▶ open(*IN, $filename) ||
Branch: 1 branch
BranchTrueFalseTotal
open *IN, $filename or $! =~ /too large/i and open *IN, "cat $filename |" or croak("can't open $filename! -- $!\n") 0 0 0
Condition0%0/6
open *IN, $filename or $! =~ /too large/i and open *IN, "cat $filename |"
l!l&&r!l&&!r
000
$! =~ /too large/i and open *IN, "cat $filename |"
!ll&&!rl&&r
000
MC/DC0%0/3
open *IN, $filename or $! =~ /too large/i and open *IN, "cat $filename |"
open *IN, $filename $! =~ /too large/i open *IN, "cat $filename |"
Truth table0%0/4
A or B and C
A= open *IN, $filenameB= $! =~ /too large/iC= open *IN, "cat $filename |"
ABCresult
1XX1
0111
00X0
0100
78 ($! =~ /too large/i && open(*IN, "cat $filename |")) ||
79 croak "can't open $filename! -- $!\n";
80 0 $self->{fh} = *IN;
81 0 $self->{filename} = $filename;
82 0 $self->{linenumber} = 0; # reset line number
83 }
84  
85  
86 sub next_seq
87 {
88 0 ▶ my ($self, $compact) = @_;
Subroutine next_seq: not called CC=5 SCAR=34.0
89 0 ▶ $self->{fh} || croak "you must pass in a file name or handle through new() or input_file() first before calling next_seq!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"} or croak("you must pass in a file name or handle through new() or input_file() first before calling next_seq!\n") 0 0 0
90 0 local $/ = "Seq-entry ::= set {"; # set record separator
91 0 ▶ while($_ = readline($self->{fh}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self->{"fh"})) 0 0 0
92 {
93 0 chomp;
94 0 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 0 0 0
95 0 ▶ my $tmp = (/^\s*Seq-entry ::= set ({.*)/si)? $1 : "{" . $_; # get rid of the 'Seq-entry ::= set ' at the beginning of Sequence record
Branch: 1 branch
BranchTrueFalseTotal
/^\s*Seq-entry ::= set ({.*)/is ? : 0 0 0
96 0 return $self->parse($tmp, $compact, 1); # 1 species no resetting line number
97 }
98 }
99  
100  
101 sub _parse
102 {
103 my ($self, $flag) = @_;
104 my $data;
105 while(1)
106 {
107 # changing orders of regex if/elsif statements made little difference. current order is close to optimal
108 if($self->{input} =~ /\G[ \t]*,?[ \t]*\n/cg) # cleanup leftover
109 {
110 $self->{linenumber}++;
111 next;
112 }
113 if($self->{input} =~ /\G[ \t]*}/cg)
114 {
115 if(!($self->{depth}--) && $self->{input} =~ /\S/)
116 {
117 croak "Data Error: extra (mismatched) '}' found on line $self->{linenumber} in $self->{filename}!\n";
118 }
119 return $data
120 }
121 elsif($self->{input} =~ /\G[ \t]*{/cg)
122 {
123 $self->{depth}++;
124 push(@$data, $self->_parse())
125 }
126 elsif($self->{input} =~ /\G[ \t]*([\w-]+)(\s*)/cg)
127 {
128 my ($id, $lines) = ($1, $2);
129 # we're prepared for NCBI to make the format even worse:
130 # note: to count line numbers right for text files on different OS, I'm sacrificing much speed (maybe I shouldn't worry so much)
131 $self->{linenumber} += $lines =~ s/\n//g || $lines =~ s/\r//g; # count by *NIX/Win or Mac
132 my ($tmp, $tmp1);
133 # we put \s* in lookahead for linenumber counting purpose (which slows things down)
134 if(($self->{input} =~ /\G"((?:[^"]+|"")*)"(?=\s*[,}])/cg && ++$tmp) ||
135 ($self->{input} =~ /\G'([^']+)'\s*H/icg && ++$tmp1) || # this is the only difference b/w sequence and entrez gene formats so far
136 $self->{input} =~ /\G([\w-]+)(?=\s*[,}])/cg)
137 {
138 my $value = $1;
139 if($tmp) # slight speed optimization, not really necessary since regex is fast enough
140 {
141 $value =~ s/""/"/g;
142 $self->{linenumber} += $value =~ s/\n//g || $value =~ s/\r//g; # count by *NIX/Win or Mac
143 $value =~ s/[\r\n]+//g; # in case it's Win format
144 }
145 elsif($tmp1) # slight speed optimization, not really necessary since regex is fast enough
146 {
147 $value =~ tr/fF8421/NNTGCA/; # good for NCBI4na. But if NCBI8na was used, then more needs to be transliterated
148 $self->{linenumber} += $value =~ s/\n//g || $value =~ s/\r//g; # count by *NIX/Win or Mac
149 $value =~ s/[\r\n0]+//g; # in case it's Win format (get rid of '0' at end of seq too)
150 }
151 if(ref($data->{$id})) { push(@{$data->{$id}}, $value) } # hash value is not a terminal (or have multiple values), create array to avoid multiple same-keyed hash overwrite each other
152 elsif($data->{$id}) { $data->{$id} = [$data->{$id}, $value] } # hash value has a second terminal value now!
153 else { $data->{$id} = $value } # the first terminal value
154 }
155 elsif($self->{input} =~ /\G{/cg)
156 {
157 $self->{depth}++;
158 push(@{$data->{$id}}, $self->_parse());
159 }
160 elsif($self->{input} =~ /\G(?=[,}])/cg) { push(@$data, $id) }
161 else # must be "id value value" format
162 {
163 $self->{depth}++;
164 push(@{$data->{$id}}, $self->_parse(1))
165 }
166 if($flag)
167 {
168 if(!($self->{depth}--) && $self->{input} =~ /\S/)
169 {
170 croak "Data Error: extra (mismatched) '}' found on line $self->{linenumber} in $self->{filename}!\n";
171 }
172 return $data;
173 }
174 }
175 elsif($self->{input} =~ /\G[ \t]*"((?:[^"]+|"")*)"(?=\s*[,}])/cg)
176 {
177 my $value = $1;
178 $value =~ s/""/"/g;
179 $self->{linenumber} += $value =~ s/\n//g || $value =~ s/\r//g; # count by *NIX/Win or Mac
180 $value =~ s/[\r\n]+//g; # in case it's Win format
181 push(@$data, $value)
182 }
183 else # end of input
184 {
185 my ($pos, $len) = (pos($self->{input}), length($self->{input}));
186 if($pos != $len && $self->{input} =~ /\G\s*\S/cg) # problem with parsing, must be non-conforming data
187 {
188 croak "Data Error: none conforming data found on line $self->{linenumber} in $self->{filename}!\n" .
189 "first $self->{maxerrstr} (or till end of input) characters including the non-conforming data:\n" .
190 substr($self->{input}, $pos, $self->{maxerrstr}) . "\n";
191 }
192 elsif($self->{depth} > 0)
193 {
194 croak "Data Error: missing '}' found at end of input in $self->{filename}!";
195 }
196 elsif($self->{depth} < 0)
197 {
198 croak "Data Error: extra (mismatched) '}' found at end of input in $self->{filename}!";
199 }
200 return $data;
201 }
202 }
203 }
204  
205 # following copied directly from my Pipeline::Util::util just to make this module
206 # more self-sufficient. Changes should be made over in that module though.
207  
208 # trims arrayrefs that points to one-element array to slims the
209 # data structure down (calls Pipeline::Util::util::trimdata)
210 # something like
211 # 'comments' => ARRAY(0x898be94)
212 # 0 ARRAY(0x883fc54)
213 # 0 ARRAY(0x886aef4)
214 # 0 HASH(0x884d554)
215 # 'heading' => 'LocusTagLink'
216 # 'source' => ARRAY(0x8810714)
217 # 0 ARRAY(0x8a7df18)
218 # 0 ARRAY(0x889f940)
219 # 0 HASH(0x886ada4)
220 # 'src' => ARRAY(0x88454fc)
221 # 0 ARRAY(0x8845598)
222 # 0 HASH(0x898c0ec)
223 # 'db' => 'HGNC'
224 # 'tag' => ARRAY(0x898bfb4)
225 # 0 HASH(0x898c164)
226 # 'id' => 5
227 # becomes this if $flag == 1:
228 # 'comments' => ARRAY(0x8840014)
229 # 0 HASH(0x884d8a4)
230 # 'heading' => 'LocusTagLink'
231 # 'source' => HASH(0x8a9869c)
232 # 'src' => HASH(0x884534c)
233 # 'db' => 'HGNC'
234 # 'tag' => HASH(0x88453c4)
235 # 'id' => 5
236 # so now $hash->{comments}->[0]->[0]->[0]->{source}->[0]->[0]->[0]->{src}->[0]->[0]->{tag}->[0]->{id}
237 # becomes $hash->{comments}->[0]->{source}->{src}->{tag}->{id}
238 # this may create problem as array might suddenly change to hash depending on whether it
239 # has multiple elements or not. So set $flag to 2 or 0/undef would disallow trimming that
240 # would lead to data type change, thus resulting in data structure like:
241 # 'comments' => ARRAY(0x88617e8)
242 # 0 HASH(0x889d578)
243 # 'heading' => 'LocusTagLink'
244 # 'source' => ARRAY(0x8912244)
245 # 0 HASH(0x8a5d648)
246 # 'src' => ARRAY(0x8a2203c)
247 # 0 HASH(0x8a1af10)
248 # 'db' => 'HGNC'
249 # 'tag' => ARRAY(0x8a1add8)
250 # 0 HASH(0x8a1af88)
251 # 'id' => 5
252 # still not the safest, but saves some hassle writing code
253  
254  
255 sub trimdata
256 {
257 my ($ref, $flag) = @_;
258 $flag = 2 unless $flag;
259 return if $flag == 3 || !ref($ref);
260 if(ref($ref) ne 'ARRAY') # allows for object refs
261 {
262 my @keys;
263 eval { @keys = keys %$ref }; # let's be careful and check if it can work as a hash
264 return if $@;
265 foreach my $key (@keys)
266 {
267 my $tmp = $ref->{$key};
268 while(ref($tmp) eq 'ARRAY' && @$tmp == 1)
269 {
270 last if($flag == 2 && ref($tmp->[0]) ne 'ARRAY');
271 $tmp = $tmp->[0]
272 }
273 $ref->{$key} = $tmp;
274 trimdata($ref->{$key}, $flag) if(ref($ref->{$key}))
275 }
276 }
277 else
278 {
279 # since the only situations where we would get an array of array is
280 # when ASN file has a bracket of brackets (otherwise we'd get at least
281 # a hash), it makes sense to reduce the arrayrefs to one level
282 foreach my $item (@$ref)
283 {
284 my $tmp = $item;
285 while(ref($tmp) eq 'ARRAY' && @$tmp == 1)
286 {
287 $tmp = $tmp->[0];
288 }
289 $item = $tmp;
290 trimdata($item, $flag) if(ref($item))
291 }
292 }
293 }
294  
295  
296 sub fh
297 {
298 my ($self, $filehandle) = @_;
299 if($filehandle)
300 {
301 $self->{fh} = $filehandle;
302 $self->{linenumber} = 0; # reset line number
303 }
304 return $self->{fh};
305 }
306  
307  
308 sub rawdata
309 {
310 my $self = shift;
311 return "Seq-entry ::= set $self->{input}";
312 }
313  
314 1;
315  
316 __END__
« EntrezGene.pm ↑ Summary EntrezGene/Indexer.pm »