lib/Chemistry/File/InChI/Parser.yp

filter: Statementstmt 86.8% 139 / 160 Branchbran 83.3% 30 / 36 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 78.6% 48 / 61 Podpod 0.0% 0 / 2 totaltotal 82.9% 219 / 264 CC 24 SCAR 12.6
CC
24
Cov
86%
_Lexer
13.1
_add_hydrogens
5.0
__ANON__
4.2
CRAP 3.5
↑ Summary blib/lib/Chemistry/File/InChI.pm »
1 # Header section
2  
3 %{
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Chemistry::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 my @LAYER_ORDER = qw( PREFIX FORMULA CONNECTIONS H_ATOMS CHARGE TETRAHEDRAL STEREOCHEMISTRY );
14  
15 %}
16  
17 30 ▶%%
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
18 30  
19 30 ▶# Rules section
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 30 30
20  
21 # The grammar is taken from https://github.com/metamolecular/inchi-grammar/blob/master/grammar.ebnf, commit 74a8858, MIT license
22  
23 string: prefix
24 30 ▶ { 'PREFIX' }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 | string layer
26 90 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.2
27 90 ▶ my $layer1_id = first { $LAYER_ORDER[$_] eq $_[1] } 0..$#LAYER_ORDER;
Subroutine __ANON__: called CC=1 SCAR=0.0
28 90 ▶ my $layer2_id = first { $LAYER_ORDER[$_] eq $_[2] } 0..$#LAYER_ORDER;
Subroutine __ANON__: called CC=1 SCAR=0.0
29 90 ▶ die "unknown layer $_[2]\n" unless defined $layer2_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $layer2_id 0 90 90
30 90 ▶ die "duplicated layer $_[1]\n" if $layer1_id == $layer2_id;
Branch: 1 branch
BranchTrueFalseTotal
if $layer1_id == $layer2_id 0 90 90
31 90 ▶ die "incorrect layer order, $_[2] must appear before $_[1]\n" if $layer1_id > $layer2_id;
Branch: 1 branch
BranchTrueFalseTotal
if $layer1_id > $layer2_id 0 90 90
32 90 $_[2];
33 }
34 ;
35  
36 layer: formula
37 30 ▶ { 'FORMULA' }
Subroutine __ANON__: called CC=1 SCAR=0.0
38 | connections
39 30 ▶ { 'CONNECTIONS' }
Subroutine __ANON__: called CC=1 SCAR=0.0
40 | h_atoms
41 28 ▶ { 'H_ATOMS' }
Subroutine __ANON__: called CC=1 SCAR=0.0
42 | charge
43 0 ▶ { 'CHARGE' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 | tetrahedral
45 2 ▶ { 'TETRAHEDRAL' }
Subroutine __ANON__: called CC=1 SCAR=0.0
46 | stereochemistry
47 0 ▶ { 'STEREOCHEMISTRY' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 ;
49  
50 formula: formula_first
51 30 ▶ { [ $_[1] ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
52 | formula formula_continuation
53 3 ▶ { push @{$_[1]}, $_[2] }
Subroutine __ANON__: called CC=1 SCAR=0.0
54 ;
55  
56 connections: '/' 'c'
57 0 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 | '/' 'c' graph
59 30 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
60 | connections ';'
61 2 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
62 | connections ';' graph
63 1 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
64 ;
65  
66 h_atoms: '/' 'h'
67 1 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
68 | '/' 'h' hydrogens
69 27 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
70 | h_atoms ';'
71 1 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
72 | h_atoms ';' hydrogens
73 2 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
74 ;
75  
76 tetrahedral: '/' 't'
77 | '/' 't' tetrahedral_centers
78 | tetrahedral ';'
79 | tetrahedral ';' tetrahedral_centers
80 ;
81  
82 stereochemistry: '/' 's' '1'
83 0 ▶ { $_[0]->{USER}{MOL}->attr( 'inchi/stereochemistry', $_[3] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 | '/' 's' '2'
85 0 ▶ { $_[0]->{USER}{MOL}->attr( 'inchi/stereochemistry', $_[3] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 ;
87  
88 # Production 'tail' is merged to 'graph' and 'body' for simplicity.
89  
90 graph: chain
91 | count '*' chain
92 ;
93  
94 # Return: The last atom in a chain
95 chain: index '-' index
96 31 ▶ { $_[0]->_add_bonds( $_[1], $_[3] ); return $_[3] }
Subroutine __ANON__: called CC=1 SCAR=0.0
97 | index branches index
98 0 ▶ { $_[0]->_add_bonds( $_[1], [ @{$_[2]}, $_[3] ] ); return $_[3] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 | chain '-' index
100 95 ▶ { $_[0]->_add_bonds( $_[1], $_[3] ); return $_[3] }
Subroutine __ANON__: called CC=1 SCAR=0.0
101 | chain branches index
102 64 ▶ { $_[0]->_add_bonds( $_[1], [ @{$_[2]}, $_[3] ] ); return $_[3] }
Subroutine __ANON__: called CC=1 SCAR=0.0
103 ;
104  
105 74 ▶branch: '(' branch_body ')' { $_[2]->{bonds} } ;
Subroutine __ANON__: called CC=1 SCAR=0.0
106  
107 # Return: Array with all atoms connected to the parent one
108 branch_body: index
109 74 ▶ { { bonds => [ $_[1] ], last => $_[1] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
110 | branch_body ',' index
111 3 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 3 push @{$_[1]->{bonds}}, $_[3];
113 3 $_[1]->{last} = $_[3];
114 3 return $_[1];
115 }
116 | branch_body '-' index
117 20 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
118 20 $_[0]->_add_bonds( $_[1]->{last}, $_[3] );
119 20 $_[1]->{last} = $_[3];
120 20 return $_[1];
121 }
122 | branch_body branches index
123 10 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 10 $_[0]->_add_bonds( $_[1]->{last}, [ @{$_[2]}, $_[3] ] );
125 10 $_[1]->{last} = $_[3];
126 10 return $_[1];
127 }
128 ;
129  
130 branches: branch
131 74 ▶ { $_[1] }
Subroutine __ANON__: called CC=1 SCAR=0.0
132 | branches branch
133 0 ▶ { [ @{$_[1]}, @{$_[2]} ] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 ;
135  
136 hydrogens: vh_count
137 | vh_count mh_counts
138 | count '*' vh_count
139 | count '*' vh_count mh_counts
140 | mh_count
141 ;
142  
143 vh_count: virtual_hydrogens
144 | vh_count ',' virtual_hydrogens
145 ;
146  
147 mh_count: mobile_hydrogens
148 | mh_count mobile_hydrogens
149 ;
150  
151 mh_counts: ',' mh_count
152 | mh_counts ',' mh_count
153 ;
154  
155 virtual_hydrogens: virtual_hydrogen_receivers 'H'
156 23 ▶ { $_[0]->_add_hydrogens( $_[1] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
157 | virtual_hydrogen_receivers 'H' count
158 28 ▶ { $_[0]->_add_hydrogens( $_[1], $_[3] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
159 ;
160  
161 virtual_hydrogen_receivers: index
162 51 ▶ { [ $_[1] ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
163 | virtual_hydrogen_receivers ',' index
164 16 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 16 push @{$_[1]}, $_[3];
166 16 return $_[1];
167 }
168 | virtual_hydrogen_receivers '-' index
169 38 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
170 38 push @{$_[1]}, ($_[1]->[-1] + 1)..$_[3];
171 38 return $_[1]
172 }
173 ;
174  
175 mobile_hydrogens: '(' 'H' comma_separated_indexes ')'
176 | '(' 'H' '-' comma_separated_indexes ')'
177 | '(' 'H' count '-' comma_separated_indexes ')'
178 | '(' 'H' count comma_separated_indexes ')'
179 ;
180  
181 comma_separated_indexes: ',' index
182 13 ▶ { [ $_[2] ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
183 | comma_separated_indexes ',' index
184 27 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 27 push @{$_[1]}, $_[3];
186 27 return $_[1];
187 }
188 ;
189  
190 tetrahedral_center: index '+'
191 2 ▶ { $_[0]->_get_atom( $_[1] )->attr( 'inchi/chirality', $_[2] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
192 | index '-'
193 5 ▶ { $_[0]->_get_atom( $_[1] )->attr( 'inchi/chirality', $_[2] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
194 | index '?'
195 ;
196  
197 tetrahedral_centers: tetrahedral_center
198 | tetrahedral_centers ',' tetrahedral_center
199 ;
200  
201 charge: '/' 'q'
202 0 ▶ { $_[0]->{USER}{MOL}->attr( 'inchi/charges', [] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 | charge '+' natural
204 0 ▶ { $_[0]->{USER}{MOL}->attr( 'inchi/charges' )->[$_[0]->{USER}{CURSOR}] = int( $_[3] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
205 | charge '-' natural
206 0 ▶ { $_[0]->{USER}{MOL}->attr( 'inchi/charges' )->[$_[0]->{USER}{CURSOR}] = int( -$_[3] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 | charge ';'
208 0 ▶ { $_[0]->{USER}{CURSOR}++ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 ;
210  
211 index: '1' | count ;
212  
213 count: '1' digit
214 118 ▶ { join '', @_[1..2] }
Subroutine __ANON__: called CC=1 SCAR=0.0
215 | count digit
216 45 ▶ { join '', @_[1..2] }
Subroutine __ANON__: called CC=1 SCAR=0.0
217 | twoplus
218 ;
219  
220 natural: '1'
221 | twoplus
222 | natural digit
223 0 ▶ { join '', @_[1..2] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
224 30 ;
225  
226 digit: '0' | '1' | twoplus ;
227  
228 twoplus: '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
229  
230 %%
231  
232 # Footer section
233  
234 sub parse
235 30 ▶{
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
236 30 my( $self, $string ) = @_;
237  
238 30 $self->YYData->{INPUT} = $string;
239 30 $self->{USER}{MOL} = Chemistry::Mol->new;
240 30 $self->{USER}{CURSOR} = 0;
241 30 $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error, yydebug => 0 );
242  
243 30 return $self->{USER}{MOL};
244 }
245  
246 sub _Lexer
247 1442 ▶{
Subroutine _Lexer: called CC=13 SCAR=25.7
248 1442 my( $self ) = @_;
249  
250 # If the line is empty and the input is originating from the file,
251 # another line is read.
252 1442 ▶ if( !$self->YYData->{INPUT} && $self->{USER}{FILEIN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->YYData->{"INPUT"} and $self->{"USER"}{"FILEIN"}) 0 1442 1442
Condition66%2/3
not $self->YYData->{"INPUT"} and $self->{"USER"}{"FILEIN"}
!ll&&!rl&&r
1412300
MC/DC0%0/2
not $self->YYData->{"INPUT"} and $self->{"USER"}{"FILEIN"}
not $self->YYData->{"INPUT"} $self->{"USER"}{"FILEIN"}
Truth table66%2/3
not(A) and B
A= not $self->YYData->{"INPUT"}B= $self->{"USER"}{"FILEIN"}
ABresult
0X0
100
111
253 0 my $filein = $self->{USER}{FILEIN};
254 0 $self->YYData->{INPUT} = <$filein>;
255 0 $self->{USER}{CHARNO} = 0;
256 }
257  
258 # Prefix
259 1442 ▶ if( $self->YYData->{INPUT} =~ s/^(InChI=1S?)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->YYData->{"INPUT"} =~ s/^(InChI=1S?)//) 30 1412 1442
260 30 return ( 'prefix', $1 );
261 }
262  
263 # Formula parts
264 # TODO: Check Hill order, require counts > 1
265 1412 ▶ if( $self->YYData->{INPUT} =~ s/^([\/\.])([2-9]|[1-9][0-9]+)?(([A-Z][a-z]?\d*)+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->YYData->{"INPUT"} =~ s[^([/\.])([2-9]|[1-9][0-9]+)?(([A-Z][a-z]?\d*)+)][]) 33 1379 1412
266 33 my( $sep, $count, $formula ) = ( $1, $2, $3 );
267 33 ▶ $count = 1 unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 30 3 33
268 33 my %atom_map;
269  
270 33 ▶ while( $formula =~ /([A-Z][a-z]?)(\d*)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($formula =~ /([A-Z][a-z]?)(\d*)/g) 103 33 136
271 103 my( $element, $count ) = ( $1, $2 );
272 103 ▶ next if $element eq 'H'; # H atoms will be added later
Branch: 1 branch
BranchTrueFalseTotal
if $element eq "H" 30 73 103
273 73 ▶ $count = 1 unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 23 50 73
274 73 for (1..$count) {
275 294 my $atom = Chemistry::Atom->new( symbol => $element );
276 294 $self->{USER}{MOL}->add_atom( $atom );
277 294 $atom_map{scalar( keys %atom_map ) + 1} = $atom;
278 }
279 }
280  
281 33 ▶ if( $sep eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep eq '/') { } 30 3 33
282 30 $self->{USER}{ATOM_MAPS} = [ \%atom_map ];
283 30 $self->{USER}{MOL}->attr( 'inchi/counts', [ $count ] );
284 30 return ( 'formula_first', $formula );
285 } else {
286 3 push @{$self->{USER}{ATOM_MAPS}}, \%atom_map;
287 3 push @{$self->{USER}{MOL}->attr( 'inchi/counts' )}, $count;
288 3 return ( 'formula_continuation', $formula );
289 }
290 }
291  
292 # Reset cursor on 'h', 'q' or 't'
293 1379 ▶ if( $self->YYData->{INPUT} =~ s/^([hqt])// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->YYData->{"INPUT"} =~ s/^([hqt])//) 30 1349 1379
294 30 $self->{USER}{CURSOR} = 0;
295 30 return ( $1, $1 );
296 }
297  
298 # Remove unsupported layers
299 1349 $self->YYData->{INPUT} =~ s/^(\/[pbmsifo][^\/]*)+//;
300  
301 # Any other character
302 1349 ▶ if( $self->YYData->{INPUT} =~ s/^(.)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->YYData->{"INPUT"} =~ s/^(.)//) 1319 30 1349
303 1319 return ( $1, $1 );
304 }
305  
306 30 return ( '', '' );
307 }
308  
309 sub _Error
310 0 ▶{
Subroutine _Error: not called CC=1 SCAR=6.9
311 0 my( $self ) = @_;
312 0 die 'ERROR: ', $self->YYData->{INPUT}, "\n";
313 }
314  
315 sub _add_bonds
316 220 ▶{
Subroutine _add_bonds: called CC=3 SCAR=11.0
317 220 my( $self, $a, $b ) = @_;
318  
319 220 ▶ my @bonds = ref $b eq 'ARRAY' ? @$b : $b;
Branch: 1 branch
BranchTrueFalseTotal
ref $b eq 'ARRAY' ? : 74 146 220
320 220 for (@bonds) {
321 297 $self->{USER}{MOL}->new_bond( atoms => [ $self->_get_atom( $a ),
322 $self->_get_atom( $_ ) ] );
323 }
324 }
325  
326 sub _add_hydrogens
327 51 ▶{
Subroutine _add_hydrogens: called CC=5 SCAR=16.1
328 51 my( $self, $atoms, $count ) = @_;
329  
330 51 my $atom_map = $self->{USER}{ATOM_MAPS}[$self->{USER}{CURSOR}];
331 51 ▶ my @atoms = map { $atom_map->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
ref $atoms ? : 51 0 51
332 ref $atoms ? @$atoms : ( $atoms );
333 51 ▶ $count = 1 unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 23 28 51
334 51 for my $atom (@atoms) {
335 147 for (1..$count) {
336 252 my $H = Chemistry::Atom->new( symbol => 'H' );
337 252 $self->{USER}{MOL}->add_atom( $H );
338 252 $self->{USER}{MOL}->new_bond( atoms => [ $atom, $H ] );
339 }
340 }
341 }
342  
343 sub _get_atom
344 601 ▶{
Subroutine _get_atom: called CC=2 SCAR=6.9
345 601 my( $self, $atom ) = @_;
346  
347 601 my $atom_map = $self->{USER}{ATOM_MAPS}[$self->{USER}{CURSOR}];
348  
349 601 ▶ if( !exists $atom_map->{$atom} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $atom_map->{$atom}) 2 599 601
350 # If an atom with given index does not exist, this is probably a hydrogen.
351 # FIXME: Check if there are unused hydrogen atoms.
352  
353 2 my $H = Chemistry::Atom->new( symbol => 'H' );
354 2 $self->{USER}{MOL}->add_atom( $H );
355 2 $atom_map->{$atom} = $H;
356 }
357  
358 601 return $atom_map->{$atom};
359 }
360  
361 1;
↑ Summary blib/lib/Chemistry/File/InChI.pm »