blib/lib/Chemistry/File/MDLMol.pm

filter: Statementstmt 20.5% 31 / 151 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 50.0% 8 / 16 Podpod 50.0% 4 / 8 totaltotal 15.8% 43 / 271 CC 48 SCAR 56.9
CC
48
Cov
13%
read_mol
645.2
write_string
109.4
M_ALS
56.0
CRAP 294.5
« blib/lib/Parse/Yapp/Driver.pm ↑ Summary blib/lib/Chemistry/Bond/Find.pm »
1 package Chemistry::File::MDLMol;
2 9 $VERSION = '0.20';
3 # $Id: MDLMol.pm,v 1.11 2005/05/20 19:08:45 itubert Exp $
4  
5 9 ▶use base "Chemistry::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 our $DEBUG = 0;
13  
14 =head1 NAME
15  
16 Chemistry::File::MDLMol - MDL molfile reader/writer
17  
18 =head1 SYNOPSIS
19  
20 use Chemistry::File::MDLMol;
21  
22 # read a molecule
23 my $mol = Chemistry::Mol->read('myfile.mol');
24  
25 # write a molecule
26 $mol->write("myfile.mol");
27  
28 # use a molecule as a query for substructure matching
29 use Chemistry::Pattern;
30 use Chemistry::Ring;
31 Chemistry::Ring::aromatize_mol($mol);
32  
33 my $patt = Chemistry::Pattern->read('query.mol');
34 if ($patt->match($mol)) {
35 print "it matches!\n";
36 }
37  
38 =cut
39  
40 9 Chemistry::Mol->register_format(mdl => __PACKAGE__);
41  
42 =head1 DESCRIPTION
43  
44 MDL Molfile (V2000) reader/writer.
45  
46 This module automatically registers the 'mdl' format with Chemistry::Mol.
47  
48 The first three lines of the molfile are stored as $mol->name,
49 $mol->attr("mdlmol/line2"), and $mol->attr("mdlmol/comment").
50  
51 This version only reads and writes some of the information available in a
52 molfile: it reads coordinats, atom and bond types, charges, radicals, and atom
53 lists. It does not read other things such as stereochemistry, 3d properties,
54 isotopes, etc.
55  
56 This module is part of the PerlMol project, L<http://www.perlmol.org>.
57  
58 =head2 Query properties
59  
60 The MDL molfile format supports query properties such as atom lists, and
61 special bond types such as "single or double", "single or aromatic", "double or
62 aromatic", "ring bond", or "any". These properties are supported by this module
63 in conjunction with L<Chemistry::Pattern>. However, support for query properies
64 is currently read-only, and the other properties listed in the specification
65 are not supported yet.
66  
67 When an atom or a bond uses one of these special query options, the condition
68 is represented as a Perl subroutine. The generated code can be read from the
69 'mdlmol/test_sub' attribute:
70  
71 $atom->attr('mdlmol/test_sub');
72 $bond->attr('mdlmol/test_sub');
73
74 This may be useful for debugging, such as when an atom doesn't seem to match as
75 expected.
76  
77 =cut
78  
79  
80 # some constants, based on tables from the file format specification
81  
82 9 my %OLD_CHARGE_MAP = (
83 1 => 3,
84 2 => 2,
85 3 => 1,
86 4 => 0,
87 5 => -1,
88 6 => -2,
89 7 => -3,
90 );
91  
92 9 my %BOND_TYPE_EXPR = (
93 4 => '($bond->aromatic)',
94 5 => '($bond->order == 1 or $bond->order == 2)',
95 6 => '($bond->order == 1 or $bond->aromatic)',
96 7 => '($bond->order == 2 or $bond->aromatic)',
97 8 => '(1)',
98 );
99  
100 9 my %BOND_TOPOLOGY_EXPR = (
101 1 => '@{$bond->attr("ring/rings")||[]}',
102 2 => '! @{$bond->attr("ring/rings")||[]}',
103 );
104  
105  
106 0 ▶sub read_mol {
Subroutine read_mol: not called CC=26 SCAR=64.7
Pod documented
107 0 my ($self, $fh, %opts) = @_;
108 0 ▶ return if $fh->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 0 0 0
109  
110 0 %opts = ( slurp => 1, %opts );
111 0 ▶ my $mol_class = $opts{mol_class} || "Chemistry::Mol";
Condition0%0/2
$opts{'mol_class'} || 'Chemistry::Mol'
l!l
00
MC/DC0%0/1
$opts{'mol_class'} || 'Chemistry::Mol'
$opts{'mol_class'}
Truth table0%0/2
A
A= $opts{'mol_class'}
Aresult
11
00
112 0 ▶ my $atom_class = $opts{atom_class} || $mol_class->atom_class;
Condition0%0/3
$opts{'atom_class'} || $mol_class->atom_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'atom_class'} || $mol_class->atom_class
$opts{'atom_class'} $mol_class->atom_class
Truth table0%0/3
A || B
A= $opts{'atom_class'}B= $mol_class->atom_class
ABresult
1X1
011
000
113 0 ▶ my $bond_class = $opts{bond_class} || $mol_class->bond_class;
Condition0%0/3
$opts{'bond_class'} || $mol_class->bond_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'bond_class'} || $mol_class->bond_class
$opts{'bond_class'} $mol_class->bond_class
Truth table0%0/3
A || B
A= $opts{'bond_class'}B= $mol_class->bond_class
ABresult
1X1
011
000
114 0 local $_;
115  
116 0 my $mol = $mol_class->new();
117  
118 # header
119 0 my $name = <$fh>; chomp $name;
120 0 my $line2 = <$fh>; chomp $line2;
121 0 my $comment = <$fh>; chomp $comment;
122 0 $mol->name($name);
123 0 $mol->attr("mdlmol/line2", $line2);
124 0 $mol->attr("mdlmol/comment", $comment);
125  
126 # counts line
127 0 ▶ defined ($_ = <$fh>) or croak "unexpected end of file";
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fh) or croak("unexpected end of file") 0 0 0
128 0 my ($na, $nb) = unpack("A3A3", $_);
129  
130 0 my %old_charges;
131 my %old_radicals;
132  
133 # atom block
134 0 for my $i (1 .. $na) { # for each atom...
135 0 ▶ defined (my $line = <$fh>) or croak "unexpected end of file";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $line = readline $fh) or croak("unexpected end of file") 0 0 0
136 0 my ($x, $y, $z, $symbol, $mass, $charge)
137 = unpack("A10A10A10xA3A2A3", $line);
138
139 $old_charges{$i} = $OLD_CHARGE_MAP{$charge}
140 0 ▶ if $OLD_CHARGE_MAP{$charge};
Branch: 1 branch
BranchTrueFalseTotal
if $OLD_CHARGE_MAP{$charge} 0 0 0
141 0 ▶ $old_radicals{$i} = 2
Branch: 1 branch
BranchTrueFalseTotal
if $charge and $charge == 4 0 0 0
Condition0%0/3
$charge and $charge == 4
!ll&&!rl&&r
000
MC/DC0%0/2
$charge and $charge == 4
$charge $charge == 4
Truth table0%0/3
A and B
A= $chargeB= $charge == 4
ABresult
0X0
100
111
142 if $charge && $charge == 4;
143 0 $mol->new_atom(
144 symbol => $symbol,
145 coords => [$x*1, $y*1, $z*1],
146 );
147 }
148  
149 # bond block
150 0 for my $i (1 .. $nb) { # for each bond...
151 9 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 0 ▶ defined ($_ = <$fh>) or croak "unexpected end of file";
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fh) or croak("unexpected end of file") 0 0 0
153 my ($a1, $a2, $type, $stereo, $topology, $rxn)
154 0 = map {$_*1} unpack("A3A3A3A3x3A3A3", $_);
155 0 ▶ my $order = $type =~ /^[123]$/ ? $type : 1;
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^[123]$/ ? : 0 0 0
156 0 my $bond = $mol->new_bond(
157 type => $type,
158 atoms => [$mol->atoms($a1,$a2)],
159 order => $order,
160 );
161 0 ▶ if ($mol->isa('Chemistry::Pattern')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mol->isa("Chemistry::Pattern")) 0 0 0
162 0 $self->bond_expr($bond, $i, $type, $topology);
163 }
164 }
165  
166 # properties block
167 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
168 0 ▶ if (/^M END/ or /^\$\$\$\$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^M END/ or /^\$\$\$\$/) { } 0 0 0
elsif (/^M (...)/) { } 0 0 0
Condition0%0/3
/^M END/ or /^\$\$\$\$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^M END/ or /^\$\$\$\$/
/^M END/ /^\$\$\$\$/
Truth table0%0/3
A or B
A= /^M END/B= /^\$\$\$\$/
ABresult
1X1
011
000
169 0 last;
170 } elsif (/^M (...)/) { # generic extended property handler
171 0 ▶ if ($1 eq 'CHG' or $1 eq 'RAD'){ # XXX
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "CHG" or $1 eq "RAD") 0 0 0
Condition0%0/3
$1 eq "CHG" or $1 eq "RAD"
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 eq "CHG" or $1 eq "RAD"
$1 eq "CHG" $1 eq "RAD"
Truth table0%0/3
A or B
A= $1 eq "CHG"B= $1 eq "RAD"
ABresult
1X1
011
000
172 # clear old-style info
173 0 %old_radicals = ();
174 0 %old_charges = ();
175 }
176  
177 0 my $method = "M_$1";
178 0 ▶ $self->$method($mol, $_) if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 0 0 0
179 }
180 }
181  
182 # add old-style charges and radicals if they still apply
183 0 ▶ while (my ($key, $val) = each %old_charges) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %old_charges) 0 0 0
184 0 $mol->atoms($key)->formal_charge($val);
185 }
186 0 ▶ while (my ($key, $val) = each %old_radicals) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %old_radicals) 0 0 0
187 0 $mol->atoms($key)->formal_radical($val);
188 }
189  
190 # make sure we get to the end of the file
191 0 ▶ if ($opts{slurp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"slurp"}) 0 0 0
192 0 ▶ 1 while <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fh) and do { '???' } 0 0 0
193 }
194  
195 0 $mol->add_implicit_hydrogens;
196  
197 0 return $mol;
198 }
199  
200 0 ▶sub bond_expr {
Subroutine bond_expr: not called CC=5 SCAR=34.0
Pod undocumented
201 0 my ($self, $bond, $i, $type, $topology) = @_;
202 0 my @bond_exprs;
203 0 my $s = $BOND_TOPOLOGY_EXPR{$topology};
204 0 ▶ push @bond_exprs, $s if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
205 0 $s = $BOND_TYPE_EXPR{$type};
206 0 ▶ push @bond_exprs, $s if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
207 0 ▶ if (@bond_exprs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bond_exprs) 0 0 0
208 0 my $expr = join " and ", @bond_exprs;
209 0 my $sub_txt = <<SUB;
210 sub {
211 no warnings;
212 my (\$patt, \$bond) = \@_;
213 $expr;
214 };
215 SUB
216 0 ▶ print "MDLMol bond($i) sub: <<<<$sub_txt>>>\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::MDLMol::DEBUG 0 0 0
217 0 $bond->attr('mdlmol/test_sub' => $sub_txt);
218 0 $bond->test_sub(eval $sub_txt);
219 }
220 }
221  
222 0 ▶sub M_CHG {
Subroutine M_CHG: not called CC=2 SCAR=17.9
Pod undocumented
223 0 my ($self, $mol, $line) = @_;
224 0 my ($m, $type, $n, %data) = split " ", $line;
225 0 ▶ while (my ($key, $val) = each %data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data) 0 0 0
226 0 $mol->atoms($key)->formal_charge($val);
227 }
228 }
229  
230 0 ▶sub M_RAD {
Subroutine M_RAD: not called CC=2 SCAR=17.9
Pod undocumented
231 0 my ($self, $mol, $line) = @_;
232 0 my ($m, $type, $n, %data) = split " ", $line;
233 0 ▶ while (my ($key, $val) = each %data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data) 0 0 0
234 0 $mol->atoms($key)->formal_radical($val);
235 }
236 }
237  
238 0 ▶sub M_ALS {
Subroutine M_ALS: not called CC=7 SCAR=40.3
Pod undocumented
239 0 my ($self, $mol, $line) = @_;
240  
241 0 ▶ return unless $line =~ /^M ALS (...)(...) (.)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^M ALS (...)(...) (.)/ 0 0 0
242 0 my ($n, $cnt, $exclude) = ($1, $2, $3);
243 0 my @symbols;
244 0 ▶ $exclude = $exclude =~ /^[Tt]$/ ? '!' : '';
Branch: 1 branch
BranchTrueFalseTotal
$exclude =~ /^[Tt]$/ ? : 0 0 0
245  
246 # parse the symbols out of the atom list line
247 0 for my $i (0 .. $cnt-1) {
248 0 my $s = substr $_, 16+$i*4, 4;
249 0 $s =~ s/\s+//g;
250 0 push @symbols, $s;
251 }
252  
253 # save attr
254 0 $mol->atoms($n)->attr('mdlmol/atom_list' => $_);
255  
256 # create test sub
257 0 ▶ if ($mol->isa('Chemistry::Pattern')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mol->isa("Chemistry::Pattern")) 0 0 0
258 0 my $sub_txt = <<SUB;
259 sub{
260 my (\$patt, \$atom) = \@_;
261 my \$sym = \$atom->symbol;
262 $exclude (List::Util::first {\$sym eq \$_} \@symbols);
263 };
264 SUB
265 0 ▶ print "MDLMol atom($n) sub: <<<<$sub_txt>>>\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::MDLMol::DEBUG 0 0 0
266 0 ▶ print "MDLMol symbol list: (@symbols)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::MDLMol::DEBUG 0 0 0
267 0 $mol->atoms($n)->attr('mdlmol/test_sub' => $sub_txt);
268 0 $mol->atoms($n)->test_sub(eval $sub_txt);
269 }
270 }
271  
272 0 ▶sub name_is {
Subroutine name_is: not called CC=1 SCAR=6.9
Pod documented
273 0 my ($self, $fname) = @_;
274 0 $fname =~ /\.mol$/i;
275 }
276  
277  
278 0 ▶sub file_is {
Subroutine file_is: not called CC=1 SCAR=6.9
Pod documented
279 0 my ($self, $fname) = @_;
280 0 $fname =~ /\.mol$/i;
281 }
282  
283  
284 0 ▶sub write_string {
Subroutine write_string: not called CC=11 SCAR=46.9
Pod documented
285 0 my ($self, $mol, %opts) = @_;
286  
287 9 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
288 0 my $s = sprintf "%s\n perlmol \n\n", $mol->name;
289 0 $s .= sprintf "%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i%6s\n",
290 0+$mol->atoms, 0+$mol->bonds,
291 0, 0, 0, 0, 0, 0, 0, 0, 999, "V2000"; # "counts" line
292  
293 0 my $i = 1;
294 0 my %idx_map;
295 my @charged_atoms;
296 0 my @radical_atoms;
297 0 for my $atom ($mol->atoms) {
298 0 my ($x, $y, $z) = $atom->coords->array;
299  
300 0 $s .= sprintf
301 "%10.4f%10.4f%10.4f %-3s%2i%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i\n",
302 $x, $y, $z, $atom->symbol,
303 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
304 0 ▶ push @charged_atoms, $i if $atom->formal_charge;
Branch: 1 branch
BranchTrueFalseTotal
if $atom->formal_charge 0 0 0
305 0 ▶ push @radical_atoms, $i if $atom->formal_radical;
Branch: 1 branch
BranchTrueFalseTotal
if $atom->formal_radical 0 0 0
306 0 $idx_map{$atom->id} = $i++;
307 }
308  
309 0 for my $bond ($mol->bonds) {
310 0 my ($a1, $a2) = map {$idx_map{$_->id}} $bond->atoms;
311 0 $s .= sprintf "%3i%3i%3i%3i%3i%3i%3i\n",
312 $a1, $a2, $bond->order,
313 0, 0, 0, 0;
314 }
315
316 0 ▶ while (@charged_atoms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@charged_atoms) 0 0 0
317 0 ▶ my $n = @charged_atoms > 8 ? 8 : @charged_atoms;
Branch: 1 branch
BranchTrueFalseTotal
@charged_atoms > 8 ? : 0 0 0
318 0 $s .= "M CHG $n";
319 0 for my $key (splice @charged_atoms, 0, $n) {
320 0 $s .= sprintf "%4d%4d", $key, $mol->atoms($key)->formal_charge;
321 }
322 0 $s .= "\n";
323 }
324 0 ▶ while (@radical_atoms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@radical_atoms) 0 0 0
325 0 ▶ my $n = @radical_atoms > 8 ? 8 : @radical_atoms;
Branch: 1 branch
BranchTrueFalseTotal
@radical_atoms > 8 ? : 0 0 0
326 0 $s .= "M RAD $n";
327 0 for my $key (splice @radical_atoms, 0, $n) {
328 0 $s .= sprintf "%4d%4d", $key, $mol->atoms($key)->formal_radical;
329 }
330 0 $s .= "\n";
331 }
332  
333 0 $s .= "M END\n";
334 0 $s;
335 }
336  
337  
338  
339 9 1;
340  
341 =head1 VERSION
342  
343 0.20
344  
345 =head1 SEE ALSO
346  
347 L<Chemistry::Mol>
348  
349 The MDL file format specification.
350 L<http://www.mdl.com/downloads/public/ctfile/ctfile.pdf> or
351 Arthur Dalby et al., J. Chem. Inf. Comput. Sci, 1992, 32, 244-255.
352  
353 The PerlMol website L<http://www.perlmol.org/>
354  
355 =head1 AUTHOR
356  
357 Ivan Tubert-Brohman <itub@cpan.org>
358  
359 =head1 COPYRIGHT
360  
361 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
362 free software; you can redistribute it and/or modify it under the same terms as
363 Perl itself.
364  
365 =cut
366  
« blib/lib/Parse/Yapp/Driver.pm ↑ Summary blib/lib/Chemistry/Bond/Find.pm »