line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chemistry::OpenSMILES::Aromaticity; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
513
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Aromaticity handling routines |
7
|
|
|
|
|
|
|
our $VERSION = '0.8.5'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
|
|
60
|
use Chemistry::OpenSMILES qw( |
10
|
|
|
|
|
|
|
is_aromatic |
11
|
|
|
|
|
|
|
is_double_bond |
12
|
|
|
|
|
|
|
is_single_bond |
13
|
1
|
|
|
1
|
|
6
|
); |
|
1
|
|
|
|
|
3
|
|
14
|
1
|
|
|
1
|
|
447
|
use Graph::Traversal::DFS; |
|
1
|
|
|
|
|
282
|
|
|
1
|
|
|
|
|
31
|
|
15
|
1
|
|
|
1
|
|
6
|
use List::Util qw( all ); |
|
1
|
|
|
|
|
16
|
|
|
1
|
|
|
|
|
1172
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Chemistry::OpenSMILES::Aromaticity - Aromaticity handling routines |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Chemistry::OpenSMILES::Aromaticity encodes some aromaticity handling |
24
|
|
|
|
|
|
|
subroutines for aromatisation and kekulisation. Both implementations are |
25
|
|
|
|
|
|
|
experimental, handle only some specific cases and are neither stable nor |
26
|
|
|
|
|
|
|
bug-free, thus should be used with caution. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
require Exporter; |
31
|
|
|
|
|
|
|
our @ISA = qw( Exporter ); |
32
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
33
|
|
|
|
|
|
|
aromatise |
34
|
|
|
|
|
|
|
electron_cycles |
35
|
|
|
|
|
|
|
kekulise |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 METHODS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=over 4 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item aromatise( $moiety ) |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Mark electron cycles as aromatic. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub aromatise |
49
|
|
|
|
|
|
|
{ |
50
|
1
|
|
|
1
|
1
|
7
|
my( $moiety ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
5
|
my @electron_cycles = electron_cycles( $moiety ); |
53
|
1
|
|
|
|
|
4
|
for my $cycle (@electron_cycles) { |
54
|
1
|
|
|
|
|
5
|
for my $i (0..$#$cycle) { |
55
|
|
|
|
|
|
|
# Set bond to aromatic |
56
|
6
|
|
|
|
|
27
|
$moiety->set_edge_attribute( $cycle->[$i], |
57
|
|
|
|
|
|
|
$cycle->[($i + 1) % scalar @$cycle], |
58
|
|
|
|
|
|
|
'bond', |
59
|
|
|
|
|
|
|
':' ); |
60
|
|
|
|
|
|
|
# Set atom to aromatic |
61
|
6
|
50
|
|
|
|
1389
|
if( $cycle->[$i]{symbol} =~ /^([BCNOPS]|Se|As)$/ ) { |
62
|
6
|
|
|
|
|
32
|
$cycle->[$i]{symbol} = lcfirst $cycle->[$i]{symbol}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item kekulise( $moiety ) |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Find nonfused even-length aromatic cycles consisting only of B, C, N, P, S |
71
|
|
|
|
|
|
|
and mark them with aliterating single and double bonds. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub kekulise |
76
|
|
|
|
|
|
|
{ |
77
|
1
|
|
|
1
|
1
|
962
|
my( $moiety ) = @_; |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
12
|
my $aromatic_only = $moiety->copy_graph; |
80
|
1
|
|
|
|
|
2944
|
$aromatic_only->delete_vertices( grep { !is_aromatic $_ } |
|
12
|
|
|
|
|
59
|
|
81
|
|
|
|
|
|
|
$aromatic_only->vertices ); |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
1499
|
my @components; |
84
|
|
|
|
|
|
|
my $get_root = sub { |
85
|
2
|
|
|
2
|
|
1002
|
my( $self, $unseen ) = @_; |
86
|
2
|
|
|
|
|
13
|
my( $next ) = sort { $unseen->{$a}{number} <=> $unseen->{$b}{number} } |
|
9
|
|
|
|
|
18
|
|
87
|
|
|
|
|
|
|
keys %$unseen; |
88
|
2
|
100
|
|
|
|
10
|
return unless defined $next; |
89
|
|
|
|
|
|
|
|
90
|
1
|
|
|
|
|
4
|
push @components, []; |
91
|
1
|
|
|
|
|
3
|
return $unseen->{$next}; |
92
|
1
|
|
|
|
|
18
|
}; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $operations = { |
95
|
|
|
|
|
|
|
first_root => $get_root, |
96
|
|
|
|
|
|
|
next_root => $get_root, |
97
|
6
|
|
|
6
|
|
1644
|
pre => sub { push @{$components[-1]}, $_[0] }, |
|
6
|
|
|
|
|
14
|
|
98
|
1
|
|
|
|
|
14
|
}; |
99
|
1
|
|
|
|
|
7
|
my $traversal = Graph::Traversal::DFS->new( $aromatic_only, %$operations ); |
100
|
1
|
|
|
|
|
317
|
$traversal->dfs; |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
|
|
|
8
|
for my $component (@components) { |
103
|
|
|
|
|
|
|
# Taking only simple even-length cycles into consideration |
104
|
1
|
50
|
|
6
|
|
12
|
next unless all { $aromatic_only->degree( $_ ) == 2 } @$component; |
|
6
|
|
|
|
|
2311
|
|
105
|
1
|
50
|
|
6
|
|
432
|
next unless all { $moiety->degree( $_ ) <= 3 } @$component; |
|
6
|
|
|
|
|
2603
|
|
106
|
1
|
50
|
|
6
|
|
519
|
next unless all { $_->{symbol} =~ /^[BCNPS]$/i } @$component; |
|
6
|
|
|
|
|
22
|
|
107
|
1
|
50
|
|
|
|
14
|
next if @$component % 2; |
108
|
|
|
|
|
|
|
|
109
|
1
|
|
|
|
|
3
|
my( $first ) = sort { $a->{number} <=> $b->{number} } @$component; |
|
9
|
|
|
|
|
18
|
|
110
|
1
|
|
|
|
|
8
|
my( $second ) = sort { $a->{number} <=> $b->{number} } |
|
1
|
|
|
|
|
112
|
|
111
|
|
|
|
|
|
|
$aromatic_only->neighbours( $first ); |
112
|
1
|
|
|
|
|
3
|
my $n = 0; |
113
|
1
|
|
|
|
|
6
|
while( $n < @$component ) { |
114
|
6
|
|
|
|
|
16
|
$first->{symbol} = ucfirst $first->{symbol}; |
115
|
6
|
100
|
|
|
|
16
|
if( $n % 2 ) { |
116
|
3
|
|
|
|
|
10
|
$moiety->set_edge_attribute( $first, $second, 'bond', '=' ); |
117
|
|
|
|
|
|
|
} else { |
118
|
3
|
|
|
|
|
15
|
$moiety->delete_edge_attribute( $first, $second, 'bond' ); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
( $first, $second ) = |
121
|
6
|
|
|
|
|
1338
|
( $second, grep { $_ ne $first } $aromatic_only->neighbours( $second ) ); |
|
12
|
|
|
|
|
609
|
|
122
|
6
|
|
|
|
|
72
|
$n++; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item electron_cycles( $moiety ) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Find electron cycles according to "Finding Electron Cycles" algorithm from |
130
|
|
|
|
|
|
|
L. |
131
|
|
|
|
|
|
|
Use with caution: the implementation is experimental. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub electron_cycles |
136
|
|
|
|
|
|
|
{ |
137
|
1
|
|
|
1
|
1
|
3
|
my( $moiety ) = @_; |
138
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
4
|
my @cycles; |
140
|
1
|
|
|
|
|
4
|
for my $start ($moiety->vertices) { |
141
|
12
|
|
|
|
|
2346
|
my %seen; |
142
|
|
|
|
|
|
|
my %prev; |
143
|
|
|
|
|
|
|
my $operations = { |
144
|
12
|
|
|
12
|
|
536
|
start => sub { return $start }, |
145
|
144
|
|
|
144
|
|
4882
|
pre => sub { $seen{$_[0]} = 1 }, |
146
|
|
|
|
|
|
|
pre_edge => sub { |
147
|
132
|
|
|
132
|
|
59734
|
my( $u, $v ) = @_; |
148
|
132
|
50
|
|
|
|
372
|
( $u, $v ) = ( $v, $u ) if $seen{$v}; |
149
|
132
|
|
|
|
|
363
|
$prev{$v} = $u; |
150
|
|
|
|
|
|
|
}, |
151
|
|
|
|
|
|
|
non_tree_edge => sub { |
152
|
24
|
|
|
24
|
|
15995
|
my( $u, $v ) = @_; |
153
|
24
|
100
|
100
|
|
|
117
|
if( $u == $start || $v == $start ) { |
154
|
12
|
100
|
|
|
|
35
|
( $u, $v ) = ( $v, $u ) if $v == $start; |
155
|
12
|
|
|
|
|
21
|
my $current = $v; |
156
|
12
|
|
|
|
|
26
|
my $prev_bond_is_single; |
157
|
12
|
|
|
|
|
30
|
my $cycle_is_alterating = 1; |
158
|
12
|
|
|
|
|
33
|
my @cycle = ( $u ); |
159
|
12
|
|
|
|
|
36
|
while( $prev{$current} ) { |
160
|
60
|
50
|
66
|
|
|
320
|
if( ( !defined $prev_bond_is_single && ( |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
161
|
|
|
|
|
|
|
is_single_bond( $moiety, $current, $prev{$current} ) || |
162
|
|
|
|
|
|
|
is_double_bond( $moiety, $current, $prev{$current} ) ) ) || |
163
|
|
|
|
|
|
|
( $prev_bond_is_single && is_double_bond( $moiety, $current, $prev{$current} ) ) || |
164
|
|
|
|
|
|
|
( !$prev_bond_is_single && is_single_bond( $moiety, $current, $prev{$current} ) ) ) { |
165
|
|
|
|
|
|
|
# Logic is inverted here as $prev_bond_is_single is |
166
|
|
|
|
|
|
|
# inverted after the conditional. |
167
|
60
|
|
|
|
|
17440
|
$prev_bond_is_single = !is_single_bond( $moiety, $current, $prev{$current} ); |
168
|
60
|
|
|
|
|
17219
|
push @cycle, $current; |
169
|
60
|
|
|
|
|
138
|
$current = $prev{$current}; |
170
|
|
|
|
|
|
|
} else { |
171
|
0
|
|
|
|
|
0
|
$cycle_is_alterating = 0; |
172
|
0
|
|
|
|
|
0
|
last; |
173
|
|
|
|
|
|
|
} |
174
|
60
|
50
|
|
|
|
125
|
last unless $cycle_is_alterating; |
175
|
60
|
|
|
|
|
164
|
$prev_bond_is_single = 1 - $prev_bond_is_single; |
176
|
|
|
|
|
|
|
} |
177
|
12
|
50
|
|
|
|
58
|
push @cycles, \@cycle if $cycle_is_alterating; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
}, |
180
|
12
|
|
|
|
|
151
|
}; |
181
|
|
|
|
|
|
|
|
182
|
12
|
|
|
|
|
108
|
my $traversal = Graph::Traversal::DFS->new( $moiety, %$operations ); |
183
|
12
|
|
|
|
|
3849
|
$traversal->dfs; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
1
|
|
|
|
|
311
|
my %unique; |
187
|
1
|
|
|
|
|
7
|
for (@cycles) { |
188
|
12
|
|
|
|
|
80
|
$unique{join '', sort @$_} = $_; |
189
|
|
|
|
|
|
|
} |
190
|
1
|
|
|
|
|
13
|
return values %unique; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=back |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=cut |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
1; |