EntrezGene.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
↑ Summary Sequence.pm »
1 package Bio::ASN1::EntrezGene;
2 BEGIN {
3 4 ▶ $Bio::ASN1::EntrezGene::AUTHORITY = 'cpan:BIOPERLML';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 {
6 $Bio::ASN1::EntrezGene::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 NCBI Entrez Gene.
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 $/ = "Entrezgene ::= {"; # 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*Entrezgene(-Set)? ::= ({.*)/si)? $2 : "{" . $_; # get rid of the 'Entrezgene ::= ' at the beginning of Entrez Gene record
Branch: 1 branch
BranchTrueFalseTotal
/^\s*Entrezgene(-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;
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([\w-]+)(?=\s*[,}])/cg)
136 {
137 my $value = $1;
138 if($tmp) # slight speed optimization, not really necessary since regex is fast enough
139 {
140 $value =~ s/""/"/g;
141 $self->{linenumber} += $value =~ s/\n//g || $value =~ s/\r//g; # count by *NIX/Win or Mac
142 $value =~ s/[\r\n]+//g; # in case it's Win format
143 }
144 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
145 elsif($data->{$id}) { $data->{$id} = [$data->{$id}, $value] } # hash value has a second terminal value now!
146 else { $data->{$id} = $value } # the first terminal value
147 }
148 elsif($self->{input} =~ /\G{/cg)
149 {
150 $self->{depth}++;
151 push(@{$data->{$id}}, $self->_parse());
152 }
153 elsif($self->{input} =~ /\G(?=[,}])/cg) { push(@$data, $id) }
154 else # must be "id value value" format
155 {
156 $self->{depth}++;
157 push(@{$data->{$id}}, $self->_parse(1))
158 }
159 if($flag)
160 {
161 if(!($self->{depth}--) && $self->{input} =~ /\S/)
162 {
163 croak "Data Error: extra (mismatched) '}' found on line $self->{linenumber} in $self->{filename}!\n";
164 }
165 return $data;
166 }
167 }
168 elsif($self->{input} =~ /\G[ \t]*"((?:[^"]+|"")*)"(?=\s*[,}])/cg)
169 {
170 my $value = $1;
171 $value =~ s/""/"/g;
172 $self->{linenumber} += $value =~ s/\n//g || $value =~ s/\r//g; # count by *NIX/Win or Mac
173 $value =~ s/[\r\n]+//g; # in case it's Win format
174 push(@$data, $value)
175 }
176 else # end of input
177 {
178 my ($pos, $len) = (pos($self->{input}), length($self->{input}));
179 if($pos != $len && $self->{input} =~ /\G\s*\S/cg) # problem with parsing, must be non-conforming data
180 {
181 croak "Data Error: none conforming data found on line $self->{linenumber} in $self->{filename}!\n" .
182 "first $self->{maxerrstr} (or till end of input) characters including the non-conforming data:\n" .
183 substr($self->{input}, $pos, $self->{maxerrstr}) . "\n";
184 }
185 elsif($self->{depth} > 0)
186 {
187 croak "Data Error: missing '}' found at end of input in $self->{filename}!";
188 }
189 elsif($self->{depth} < 0)
190 {
191 croak "Data Error: extra (mismatched) '}' found at end of input in $self->{filename}!";
192 }
193 return $data;
194 }
195 }
196 }
197  
198 # following copied directly from my Pipeline::Util::util just to make this module
199 # more self-sufficient. Changes should be made over in that module though.
200  
201 # trims arrayrefs that points to one-element array to slims the
202 # data structure down (calls Pipeline::Util::util::trimdata)
203 # something like
204 # 'comments' => ARRAY(0x898be94)
205 # 0 ARRAY(0x883fc54)
206 # 0 ARRAY(0x886aef4)
207 # 0 HASH(0x884d554)
208 # 'heading' => 'LocusTagLink'
209 # 'source' => ARRAY(0x8810714)
210 # 0 ARRAY(0x8a7df18)
211 # 0 ARRAY(0x889f940)
212 # 0 HASH(0x886ada4)
213 # 'src' => ARRAY(0x88454fc)
214 # 0 ARRAY(0x8845598)
215 # 0 HASH(0x898c0ec)
216 # 'db' => 'HGNC'
217 # 'tag' => ARRAY(0x898bfb4)
218 # 0 HASH(0x898c164)
219 # 'id' => 5
220 # becomes this if $flag == 1:
221 # 'comments' => ARRAY(0x8840014)
222 # 0 HASH(0x884d8a4)
223 # 'heading' => 'LocusTagLink'
224 # 'source' => HASH(0x8a9869c)
225 # 'src' => HASH(0x884534c)
226 # 'db' => 'HGNC'
227 # 'tag' => HASH(0x88453c4)
228 # 'id' => 5
229 # so now $hash->{comments}->[0]->[0]->[0]->{source}->[0]->[0]->[0]->{src}->[0]->[0]->{tag}->[0]->{id}
230 # becomes $hash->{comments}->[0]->{source}->{src}->{tag}->{id}
231 # this may create problem as array might suddenly change to hash depending on whether it
232 # has multiple elements or not. So set $flag to 2 or 0/undef would disallow trimming that
233 # would lead to data type change, thus resulting in data structure like:
234 # 'comments' => ARRAY(0x88617e8)
235 # 0 HASH(0x889d578)
236 # 'heading' => 'LocusTagLink'
237 # 'source' => ARRAY(0x8912244)
238 # 0 HASH(0x8a5d648)
239 # 'src' => ARRAY(0x8a2203c)
240 # 0 HASH(0x8a1af10)
241 # 'db' => 'HGNC'
242 # 'tag' => ARRAY(0x8a1add8)
243 # 0 HASH(0x8a1af88)
244 # 'id' => 5
245 # still not the safest, but saves some hassle writing code
246  
247  
248 sub trimdata
249 {
250 my ($ref, $flag) = @_;
251 $flag = 2 unless $flag;
252 return if $flag == 3 || !ref($ref);
253 if(ref($ref) ne 'ARRAY') # allows for object refs
254 {
255 my @keys;
256 eval { @keys = keys %$ref }; # let's be careful and check if it can work as a hash
257 return if $@;
258 foreach my $key (@keys)
259 {
260 my $tmp = $ref->{$key};
261 while(ref($tmp) eq 'ARRAY' && @$tmp == 1)
262 {
263 last if($flag == 2 && ref($tmp->[0]) ne 'ARRAY');
264 $tmp = $tmp->[0]
265 }
266 $ref->{$key} = $tmp;
267 trimdata($ref->{$key}, $flag) if(ref($ref->{$key}))
268 }
269 }
270 else
271 {
272 # since the only situations where we would get an array of array is
273 # when ASN file has a bracket of brackets (otherwise we'd get at least
274 # a hash), it makes sense to reduce the arrayrefs to one level
275 foreach my $item (@$ref)
276 {
277 my $tmp = $item;
278 while(ref($tmp) eq 'ARRAY' && @$tmp == 1)
279 {
280 $tmp = $tmp->[0];
281 }
282 $item = $tmp;
283 trimdata($item, $flag) if(ref($item))
284 }
285 }
286 }
287  
288  
289 sub fh
290 {
291 my ($self, $filehandle) = @_;
292 if($filehandle)
293 {
294 $self->{fh} = $filehandle;
295 $self->{linenumber} = 0; # reset line number
296 }
297 return $self->{fh};
298 }
299  
300  
301 sub rawdata
302 {
303 my $self = shift;
304 return "Entrezgene ::= $self->{input}";
305 }
306  
307 1;
308  
309 __END__
↑ Summary Sequence.pm »