blib/lib/Chemistry/File/VRML.pm

filter: Statementstmt 52.2% 264 / 505 Branchbran 26.0% 26 / 100 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.6% 31 / 41 Podpod 100% 1 / 1 totaltotal 49.3% 322 / 652 CC 79 SCAR 40.7
CC
79
Cov
48%
genBonds
295.0
readPDB
90.0
_genLineSets
37.8
CRAP 58.4
« blib/lib/Chemistry/Canonicalize.pm ↑ Summary blib/lib/Chemistry/FormulaPattern.pm »
1 package Chemistry::File::VRML;
2  
3 10 $VERSION = '0.10';
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use base qw(Chemistry::File);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use POSIX qw(acos);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 Chemistry::Mol->register_format(vrml => __PACKAGE__);
12  
13 10 my %OPTS = (
14 center => 'centerAtoms',
15 color => 'setColor',
16 style => 'setStyle',
17 stick_radius => 'setStickRadius',
18 ball_radius => 'setBallRadius',
19 compression => 'setCompression',
20 );
21  
22 1 ▶sub write_mol {
Subroutine write_mol: called CC=3 SCAR=11.0
Pod documented
23 1 my ($self, $fh, $mol, %opts) = @_;
24 1 my $vrml = Chemistry::File::VRML::PDB2VRML->new($fh);
25 1 $vrml->add_mol($mol);
26 1 ▶ while (my ($key, $val) = each %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %opts) 5 1 6
27 5 ▶ if (my $method = $OPTS{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $OPTS{$key}) 3 2 5
28 3 $vrml->$method($val);
29 }
30 }
31 1 $vrml->printVRML;
32 }
33  
34 package Chemistry::File::VRML::PDB2VRML;
35  
36 # Defaults variables
37 10 my $Compression = 0;
38 10 my $Style = 'Wireframe'; # default display style
39 10 my $Color = 'byAtom'; # default color
40 10 my $PI = 3.14159265;
41  
42 10 my $RadiusBNS = 0.2;
43 10 my $RadiusStick = 0.15;
44  
45 # Some global tables
46  
47 # color indizes per atom type
48 10 my %AtomColors = qw(
49 '' 5 H 3 HE 5 LI 5 BE 5 B 1 C 4 N 1 O 2 F 7 NE 5 NA 5
50 MG 5 AL 5 SI 5 P 6 S 0 CL 7 AR 5 K 5 CA 5 SC 5 TI 5 V 5
51 CR 5 MN 5 FE 5 CO 5 NI 5 CU 5 ZN 5 GA 5 GE 5 AS 5 SE 5 BR 7
52 KR 5 RB 5 SR 5 Y 5 ZR 5 NB 5 MO 5 TC 5 RU 5 RH 5 PD 5 AG 5
53 CD 5 IN 5 SN 5 SB 5 TE 5 I 7 XE 5 CS 5 BA 5 LA 5 CE 5 PR 5
54 ND 5 PM 5 SM 5 EU 5 GD 5 TB 5 DY 5 HO 5 ER 5 TM 5 YB 5 LU 5
55 HF 5 TA 5 W 5 RE 5 OS 5 IR 5 PT 5 AU 5 HG 5 TL 5 PB 5 BI 5
56 PO 5 AT 7 RN 5 FR 5 RA 5 AC 5 TH 5 PA 5 U 5 NP 5 PU 5 AM 5
57 CM 5 BK 5 CF 5 XX 5 FM 5 MD 5 NO 5 LW 5
58 );
59  
60 # VDW radius per atom type
61 10 my %VDWRadius = qw(
62 '' 1.00 H 1.08 HE 1.00 LI 1.00 BE 1.00 B 1.00 C 1.54 N 1.48
63 O 1.36 F 1.30 NE 1.00 NA 2.30 MG 1.00 AL 2.86 SI 2.10 P 1.75
64 S 1.70 CL 1.65 AR 1 K 1 CA 2.75 SC 1 TI 1 V 1
65 CR 1 MN 1 FE 2.27 CO 1 NI 1 CU 1.4 ZN 1.4 GA 1
66 GE 1 AS 1 SE 1 BR 1.8 KR 1 RB 1 SR 1 Y 1
67 ZR 1 NB 1 MO 1 TC 1 RU 1 RH 1 PD 1 AG 1
68 CD 1 IN 1 SN 1 SB 1 TE 1 I 1 XE 1 CS 1
69 BA 1 LA 1 CE 1 PR 1 ND 1 PM 1 SM 1 EU 1
70 GD 1 TB 1 DY 1 HO 1 ER 1 TM 1 YB 1 LU 1
71 HF 1 TA 1 W 1 RE 1 OS 1 IR 1 PT 1 AU 1
72 HG 1 TL 1 PB 1 BI 1 PO 1 AT 1 RN 1 FR 1
73 RA 1 AC 1 TH 1 PA 1 U 1 NP 1 PU 1 AM 1
74 CM 1 BK 1 CF 1 XX 1 FM 1 MD 1 NO 1 LW 1
75 );
76  
77 # atom radius per atom type
78 10 my %AtomRadius = qw(
79 '' 0.00 H 0.37 HE 0.70 LI 1.23 BE 0.89 B 0.80 C 0.77 N 0.74
80 O 0.74 F 0.72 NE 0.70 NA 1.57 MG 1.36 AL 1.25 SI 1.17 P 1.10
81 S 1.04 CL 0.99 AR 0.70 K 2.03 CA 1.74 SC 1.44 TI 1.32 V 1.22
82 CR 1.17 MN 1.16 FE 1.16 CO 1.15 NI 1.17 CU 1.25 ZN 1.25 GA 1.22
83 GE 1.21 AS 1.17 SE 0.70 BR 1.24 KR 1.91 RB 1.62 SR 1.45 Y 1.34
84 ZR 1.29 NB 1.29 MO 1.24 TC 1.25 RU 1.28 RH 1.34 PD 1.41 AG 1.50
85 CD 1.40 IN 1.41 SN 1.37 SB 1.33 TE 0.70 I 1.33 XE 1.98 CS 1.69
86 BA 1.69 LA 1.69 CE 1.69 PR 1.69 ND 1.69 PM 1.69 SM 1.69 EU 1.69
87 GD 1.69 TB 1.69 DY 1.69 HO 1.69 ER 1.69 TM 1.69 YB 1.69 LU 1.69
88 HF 1.44 TA 1.34 W 1.30 RE 1.28 OS 1.26 IR 1.29 PT 1.34 AU 1.44
89 HG 1.55 TL 1.54 PB 1.52 BI 1.52 PO 1.40 AT 0.70 RN 2.40 FR 2.00
90 RA 1.90 AC 1.90 TH 1.90 PA 1.90 U 1.90 NP 0.70 PU 0.26 AM 1.00
91 CM 1.00 BK 1.00 CF 1.00 XX 1.00 FM 1.00 MD 1.00 NO 1.00 LW 1.00
92 );
93  
94 # S N O H
95 10 my (@RGBColors) =
96 ('1 1 0', '0 0 1', '1 0 0', '1 1 1', '.5 .5 .5', '1 0 1', '1 .5 0', '0 1 0');
97  
98 # C rest P Hal
99  
100 10 my (%ColorNames) = (
101 'yellow' => 0,
102 'blue' => 1,
103 'red' => 2,
104 'white' => 3,
105 'grey' => 4,
106 'purple' => 5,
107 'brown' => 6,
108 'green' => 7
109 );
110  
111 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
112 1 my ($class, $fh) = @_;
113 1 ▶ $class = ref($class) if (ref($class));
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1 1
114  
115 1 my $this = bless {
116 fh => $fh,
117 atoms => [],
118 bonds => [],
119 points => [],
120 lines => [],
121 style => $Style,
122 color => $Color,
123 lineSets => [],
124 lineColors => [],
125 indent => 0,
126 DefUse => {}, # stores shared instances
127 BLT => {}, # bond lookup table for CONECT list
128 RadiusStick => $RadiusStick,
129 Compression => $Compression,
130 RadiusBNS => $RadiusBNS,
131 }, $class;
132  
133 1 return $this;
134 }
135  
136 1 ▶sub add_mol {
Subroutine add_mol: called CC=3 SCAR=11.4
137 1 my ($this, $mol) = @_;
138  
139 1 my %atoms;
140 1 for my $atom ($mol->atoms) {
141 9 my $label = uc $atom->symbol;
142 9 my ($x, $y, $z) = $atom->coords->array;
143 my $vrml_atom = {
144 x => $x,
145 y => $y,
146 z => $z,
147 9 nr => scalar(@{$this->{'atoms'}}),
148 label => $label,
149 9 radius => $VDWRadius{$label},
150 };
151 9 $atoms{$atom} = $vrml_atom;
152 9 push(@{$this->{'atoms'}}, $vrml_atom);
153 }
154  
155 1 for my $bond ($mol->bonds) {
156 0 my ($from, $to) = map { $atoms{$_} } $bond->atoms;
157 0 push(@{$this->{'bonds'}}, {from => $from, to => $to});
158  
159 }
160 1 1;
161 }
162 ###########################################################################
163 #
164 # Read PDB file
165 # Old atoms and bonds won't be deleted, allowing to
166 # merge several PDB file.
167 # Syntax: $object->readPDB($fileName);
168 # Diag: returns undef on error
169 #
170 # 1 2 3 4 5 6 7
171 #1234567890123456789012345678901234567890123456789012345678901234567890123456789
172 #TOM 5 O5* A A 1 -16.851 -5.543 74.981 1.00 55.62 3CRO 148
173 ###########################################################################
174 0 ▶sub readPDB {
Subroutine readPDB: not called CC=9 SCAR=45.0
175 0 my $this = shift;
176 0 my $fileName = shift;
177  
178 0 ▶ open(FILE, "$fileName") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "$fileName" or return undef 0 0 0
179 0 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
180 0 ▶ if (/^ATOM /) { # only C,H,O,P,N,S allowed
Branch: 3 branches
BranchTrueFalseTotal
if (/^ATOM /) { } 0 0 0
elsif (/^HETATM/) { } 0 0 0
elsif (/^CONECT/) { } 0 0 0
181 0 my $label = substr($_, 12, 4);
182 0 $label =~ s/\s//g;
183 0 $label = substr(uc $label, 0, 1); # only the first letter
184 0 my $x = substr($_, 30, 8);
185 0 my $y = substr($_, 38, 8);
186 0 my $z = substr($_, 46, 8);
187 my (%atom) = (
188 'x' => $x,
189 'y' => $y,
190 'z' => $z,
191 0 'nr' => scalar(@{$this->{'atoms'}}),
192 'label' => $label,
193 0 'radius' => $VDWRadius{$label}
194 );
195 0 push(@{$this->{'atoms'}}, \%atom);
196 } elsif (/^HETATM/) {
197 0 my $label = substr($_, 12, 4);
198 0 $label =~ s/\s//g;
199 0 $label =~ s/[^A-Za-z].*$//g;
200 0 $label = uc $label;
201 0 my $x = substr($_, 30, 8);
202 0 my $y = substr($_, 38, 8);
203 0 my $z = substr($_, 46, 8);
204 my (%atom) = (
205 'x' => $x,
206 'y' => $y,
207 'z' => $z,
208 0 'nr' => scalar(@{$this->{'atoms'}}),
209 'label' => $label,
210 0 'radius' => $VDWRadius{$label}
211 );
212 0 push(@{$this->{'atoms'}}, \%atom);
213 } elsif (/^CONECT/) {
214 0 my $tmp = substr($_, 0, 69);
215 0 my ($null, $a, @b) = split(/\s+/, $tmp);
216 0 foreach (@b) {
217 0 my $n1 = $a - 1;
218 0 my $n2 = $_ - 1;
219 0 ▶ if ($n1 > $n2) { my $n3 = $n1; $n1 = $n2; $n2 = $n3; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n1 > $n2) 0 0 0
220 0 my $label = $n1 . '_' . $n2;
221 0 ▶ next if (exists($this->{'BLT'}->{$label}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"BLT"}{$label} 0 0 0
222 0 my $from = $this->{'atoms'}->[$n1];
223 0 my $to = $this->{'atoms'}->[$n2];
224 0 my (%bond) = ('from' => $from, 'to' => $to);
225 0 push(@{$this->{'bonds'}}, \%bond);
226 0 $this->{'BLT'}->{$label} = 1;
227 }
228 }
229 }
230 0 CORE::close FILE;
231  
232 0 1;
233 }
234  
235 ###########################################################################
236 1 ▶sub setStyle {
Subroutine setStyle: called CC=1 SCAR=0.0
237 1 my ($this, $style) = @_;
238 1 $style = lc $style;
239 1 $style =~ s/[_ ]//g;
240 1 $this->{'style'} = $style;
241 }
242 1 ▶sub setColor { my $this = shift; $this->{'color'} = shift; }
Subroutine setColor: called CC=1 SCAR=0.0
243 0 ▶sub setStickRadius { my $this = shift; $this->{RadiusStick} = shift; }
Subroutine setStickRadius: not called CC=1 SCAR=6.9
244 0 ▶sub setBallRadius { my $this = shift; $this->{RadiusBNS} = shift; }
Subroutine setBallRadius: not called CC=1 SCAR=6.9
245 0 ▶sub setCompression { my $this = shift; $this->{Compression} = shift; }
Subroutine setCompression: not called CC=1 SCAR=6.9
246  
247 ###########################################################################
248 #
249 # Center all atoms
250 # Syntax: $object->centerAtoms();
251 #
252 ###########################################################################
253 1 ▶sub centerAtoms {
Subroutine centerAtoms: called CC=4 SCAR=13.9
254 1 my $this = shift;
255  
256 1 my ($cogX, $cogY, $cogZ) = (0, 0, 0);
257 1 foreach (@{$this->{'atoms'}}) {
258 9 $cogX += $_->{'x'};
259 9 $cogY += $_->{'y'};
260 9 $cogZ += $_->{'z'};
261 }
262 1 my $numAtoms = @{$this->{'atoms'}};
263 1 $cogX /= $numAtoms;
264 1 $cogY /= $numAtoms;
265 1 $cogZ /= $numAtoms;
266 1 foreach (($cogX, $cogY, $cogZ)) { $_ = sprintf("%.4f", $_); }
267 1 foreach (@{$this->{'atoms'}}) {
268 9 $_->{'x'} -= $cogX;
269 9 $_->{'y'} -= $cogY;
270 9 $_->{'z'} -= $cogZ;
271 }
272 }
273  
274 ###########################################################################
275 #
276 # Generate list of lines (cylinders) and points.
277 # Syntax: $object->_genDisplay();
278 #
279 ###########################################################################
280 1 ▶sub _genDisplay {
Subroutine _genDisplay: called CC=7 SCAR=30.1
281 1 my $this = shift;
282 1 my $color = $this->{'color'};
283  
284 # determine atom colors
285 1 ▶ if ($color eq 'byAtom') {
Branch: 1 branch
BranchTrueFalseTotal
if ($color eq 'byAtom') { } 1 0 1
286 1 foreach (@{$this->{'atoms'}}) {
287 9 $_->{'color'} = $AtomColors{$_->{'label'}};
288 }
289 } else {
290 0 my $c = $ColorNames{$color};
291 0 foreach (@{$this->{'atoms'}}) { $_->{'color'} = $c; }
292 }
293  
294 # create one point foreach atom
295 1 @{$this->{'points'}} = ();
296 1 foreach (@{$this->{'atoms'}}) {
297 9 my (%point) = (%$_, 'lines' => []);
298 9 push(@{$this->{'points'}}, \%point);
299 9 $_->{'point'} = \%point;
300 }
301  
302 # create one (or two) lines per bond
303 1 @{$this->{'lines'}} = ();
304 1 foreach (@{$this->{'bonds'}}) {
305 0 my ($at1, $at2) = ($_->{'from'}, $_->{'to'});
306 0 ▶ if ($at1->{'color'} == $at2->{'color'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at1->{"color"} == $at2->{"color"}) 0 0 0
307 0 my $from = $at1->{'point'};
308 my (%line) = (
309 'from' => $from,
310 'to' => $at2->{'point'},
311 0 'label' => $at1->{'nr'} . '_' . $at2->{'nr'}
312 );
313 0 push(@{$this->{'lines'}}, \%line);
314 0 push(@{$from->{'lines'}}, \%line);
315 0 next;
316 }
317  
318 # split bond
319 0 my $pt1 = $at1->{'point'};
320 0 my $pt2 = $at2->{'point'};
321  
322 0 my $x = 0.5 * ($at1->{'x'} + $at2->{'x'});
323 0 my $y = 0.5 * ($at1->{'y'} + $at2->{'y'});
324 0 my $z = 0.5 * ($at1->{'z'} + $at2->{'z'});
325 my (%point3) = (
326 'x' => $x,
327 'y' => $y,
328 'z' => $z, # no label,
329 'color' => $at2->{'color'}, # radius or
330 0 'nr' => scalar(@{$this->{'points'}})
331 ); # bonds needed
332 my (%line1) = (
333 'from' => $pt1,
334 'to' => \%point3,
335 0 'label' => $at1->{'nr'} . '_' . $at2->{'nr'}
336 );
337 my (%line2) = (
338 'from' => $pt2,
339 'to' => \%point3,
340 0 'label' => $at2->{'nr'} . '_' . $at1->{'nr'}
341 );
342 0 push(@{$this->{'lines'}}, \%line1);
343 0 push(@{$this->{'lines'}}, \%line2);
344 0 push(@{$pt1->{'lines'}}, \%line1);
345 0 push(@{$pt2->{'lines'}}, \%line2);
346 0 push(@{$this->{'points'}}, \%point3);
347 }
348 }
349  
350 ###########################################################################
351 #
352 # Optimize the wireframe representation.
353 # Create longer line strips instead of single lines.
354 #
355 ###########################################################################
356 1 ▶sub _genLineSets {
Subroutine _genLineSets: called CC=8 SCAR=36.3
357 1 my $this = shift;
358  
359 1 @{$this->{'lineSets'}} = ();
360 1 @{$this->{'lineColors'}} = ();
361 0 foreach (@{$this->{'lines'}}) { $_->{'used'} = 0; }
362 1 foreach (@{$this->{'lines'}}) {
363 0 ▶ next if $_->{'used'};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"used"} 0 0 0
364 0 my ($from, $to) = ($_->{'from'}, $_->{'to'});
365 0 push(@{$this->{'lineColors'}}, $from->{'color'});
366 0 my $set = [$from->{'nr'}, $to->{'nr'}];
367 0 push(@{$this->{'lineSets'}}, $set);
368 0 $_->{'used'} = 1;
369 0 my $next = 1;
370 0 my $bonds = $to->{'lines'};
371  
372 0 ▶ while ($next and $bonds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $bonds) 0 0 0
Condition0%0/3
$next and $bonds
!ll&&!rl&&r
000
MC/DC0%0/2
$next and $bonds
$next $bonds
Truth table0%0/3
A and B
A= $nextB= $bonds
ABresult
0X0
100
111
373 0 $next = 0;
374 0 my $b;
375 0 foreach $b (@$bonds) {
376 0 ▶ next if $b->{'used'};
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"used"} 0 0 0
377 0 my $to = $b->{'to'};
378 0 push(@$set, $to->{'nr'});
379 0 $bonds = $to->{'lines'};
380 0 $b->{'used'} = 1;
381 0 $next = 1;
382 0 last;
383 }
384 }
385 }
386 }
387  
388 ###########################################################################
389 #
390 # print VRML SceneGraph
391 #
392 ###########################################################################
393 1 ▶sub printVRML {
Subroutine printVRML: called CC=3 SCAR=11.1
394 1 my $this = shift;
395  
396 1 %{$this->{'DefUse'}} = ();
397 1 $this->{'indent'} = 0;
398 1 $this->_printHead();
399  
400 1 $this->_genDisplay();
401  
402 $this->_genLineSets(), $this->_printWire()
403 1 ▶ if ($this->{'style'} =~ /wire/);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"style"} =~ /wire/ 1 0 1
404 $this->_printAtoms()
405 1 ▶ if ($this->{'style'} =~ /(ball|stick|cpk)/);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"style"} =~ /(ball|stick|cpk)/ 1 0 1
406  
407 1 $this->_printTail();
408 }
409  
410 ###########################################################################
411 #
412 # print VRML header
413 #
414 ###########################################################################
415 1 ▶sub _printHead {
Subroutine _printHead: called CC=1 SCAR=0.0
416 1 my $this = shift;
417 1 my $fh = $this->{fh};
418  
419 1 print $fh <<EOT;
420 #VRML V2.0 utf8
421  
422 Transform {
423 children [
424 NavigationInfo { type "EXAMINE" }
425 EOT
426  
427 1 $this->{'indent'} = 2;
428 }
429  
430 ###########################################################################
431 1 ▶sub _printWire {
Subroutine _printWire: called CC=1 SCAR=0.0
432 1 my $this = shift;
433  
434 1 $this->_printWireShape();
435 }
436  
437 ###########################################################################
438 1 ▶sub _printWireShape {
Subroutine _printWireShape: called CC=2 SCAR=7.2
439 1 my $this = shift;
440  
441 1 ▶ if ($this->{'DefUse'}->{'WireShape'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"WireShape"}) 0 1 1
442 0 $this->_printLine('USE WIRESHAPE');
443 0 return;
444 }
445  
446 1 $this->_printLine('DEF WIRESHAPE Shape {');
447 1 $this->{'indent'}++;
448 1 $this->_printWireAppearance();
449 1 $this->_printWireGeometry();
450 1 $this->{'indent'}--;
451 1 $this->_printLine('}');
452  
453 1 $this->{'DefUse'}->{'WireShape'} = 1;
454 }
455  
456 ###########################################################################
457 1 ▶sub _printWireAppearance {
Subroutine _printWireAppearance: called CC=2 SCAR=7.2
458 1 my $this = shift;
459  
460 1 ▶ if ($this->{'DefUse'}->{'WireApp'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"WireApp"}) 0 1 1
461 0 $this->_printLine('USE WIREAPP');
462 0 return;
463 }
464  
465 1 $this->_printLine("appearance DEF WIREAPP Appearance {");
466 1 $this->{'indent'}++;
467 1 $this->_printLine("material Material { diffuseColor 1 1 1 }"); # dummy
468 1 $this->{'indent'}--;
469 1 $this->_printLine('}');
470  
471 1 $this->{'DefUse'}->{'WireApp'} = 1;
472 }
473  
474 ###########################################################################
475 1 ▶sub _printWireGeometry {
Subroutine _printWireGeometry: called CC=2 SCAR=7.1
476 1 my $this = shift;
477  
478 1 ▶ if ($this->{'DefUse'}->{'WireGeo'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"WireGeo"}) 0 1 1
479 0 $this->_printLine('geometry USE WIREGEO');
480 0 return;
481 }
482  
483 1 $this->_printLine('geometry DEF WIREGEO IndexedLineSet {');
484 1 $this->{'indent'}++;
485 1 $this->_printWireColor();
486 1 $this->_printWireColorIndex();
487 1 $this->_printLine('colorPerVertex FALSE');
488 1 $this->_printWireCoordinate();
489 1 $this->_printWireCoordIndex();
490 1 $this->{'indent'}--;
491 1 $this->_printLine('}');
492  
493 1 $this->{'DefUse'}->{'WireGeo'} = 1;
494 }
495  
496 ###########################################################################
497 1 ▶sub _printWireColor {
Subroutine _printWireColor: called CC=3 SCAR=11.1
498 1 my $this = shift;
499  
500 1 ▶ if ($this->{'DefUse'}->{'WireCol'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"WireCol"}) 0 1 1
501 0 $this->_printLine('color USE WIRECOL');
502 0 return;
503 }
504  
505 1 $this->_printLine('color DEF WIRECOL Color {');
506 1 $this->{'indent'}++;
507 1 $this->_printLine('color [');
508 1 $this->{'indent'}++;
509 1 foreach (@RGBColors) { $this->_printLine("$_,"); }
510 1 $this->{'indent'}--;
511 1 $this->_printLine(']');
512 1 $this->{'indent'}--;
513 1 $this->_printLine('}');
514  
515 1 $this->{'DefUse'}->{'WireCol'} = 1;
516 }
517  
518 ###########################################################################
519 1 ▶sub _printWireColorIndex {
Subroutine _printWireColorIndex: called CC=3 SCAR=11.3
520 1 my $this = shift;
521  
522 1 $this->_printLine('colorIndex [');
523 1 $this->{'indent'}++;
524 1 my $lc = $this->{'lineColors'};
525 1 my $i;
526 1 ▶ for ($i = 0 ; $i < (@$lc - 8) ; $i += 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lc - 8) 0 1 1
527 0 $this->_printLine(join(', ', @$lc[$i .. ($i + 7)]) . ',');
528 }
529 1 ▶ $this->_printLine(join(', ', @$lc[$i .. $#$lc]) . ',')
Branch: 1 branch
BranchTrueFalseTotal
if $i < @$lc 0 1 1
530 if ($i < @$lc);
531 1 $this->{'indent'}--;
532 1 $this->_printLine(']');
533 }
534  
535 ###########################################################################
536 1 ▶sub _printWireCoordinate {
Subroutine _printWireCoordinate: called CC=3 SCAR=11.1
537 1 my $this = shift;
538  
539 1 ▶ if ($this->{'DefUse'}->{'WireCoo'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"WireCoo"}) 0 1 1
540 0 $this->_printLine('coord USE WIRECOO');
541 0 return;
542 }
543  
544 1 $this->_printLine('coord DEF WIRECOO Coordinate {');
545 1 $this->{'indent'}++;
546 1 $this->_printLine('point [');
547 1 $this->{'indent'}++;
548 1 my ($x, $y, $z);
549 1 foreach (@{$this->{'points'}}) {
550 9 my $x = sprintf("%.4g", $_->{'x'});
551 9 my $y = sprintf("%.4g", $_->{'y'});
552 9 my $z = sprintf("%.4g", $_->{'z'});
553 9 $this->_printLine("$x $y $z,");
554 }
555 1 $this->{'indent'}--;
556 1 $this->_printLine(']');
557 1 $this->{'indent'}--;
558 1 $this->_printLine('}');
559  
560 1 $this->{'DefUse'}->{'WireCoo'} = 1;
561 }
562  
563 ###########################################################################
564 1 ▶sub _printWireCoordIndex {
Subroutine _printWireCoordIndex: called CC=2 SCAR=7.0
565 1 my $this = shift;
566  
567 1 $this->_printLine('coordIndex [');
568 1 $this->{'indent'}++;
569 1 my $ls = $this->{'lineSets'};
570 0 foreach (@$ls) { $this->_printLine(join(', ', @$_) . ', -1,'); }
571 1 $this->{'indent'}--;
572 1 $this->_printLine(']');
573 }
574  
575 ###########################################################################
576 1 ▶sub _printAtoms {
Subroutine _printAtoms: called CC=3 SCAR=11.1
577 1 my $this = shift;
578  
579 1 foreach (@{$this->{'atoms'}}) {
580 9 ▶ $this->_printAtom($_) if ($_->{'label'});
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"label"} 9 0 9
581 }
582 }
583  
584 ###########################################################################
585 9 ▶sub _printAtom {
Subroutine _printAtom: called CC=3 SCAR=11.5
586 9 my $this = shift;
587 9 my $atom = shift;
588  
589 9 $this->_printLine("DEF ATOM_$atom->{'nr'} Transform {");
590 9 $this->{'indent'}++;
591 9 $this->_printLine("translation $atom->{'x'} $atom->{'y'} $atom->{'z'}");
592 9 $this->_printLine('children [');
593 9 $this->{'indent'}++;
594 9 $this->_printAtomShape($atom);
595 9 ▶ if ($this->{'style'} =~ /stick/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"style"} =~ /stick/) 0 9 9
596 0 my $point = $atom->{'point'};
597 0 foreach (@{$point->{'lines'}}) { $this->_printBond($_); }
598 }
599 9 $this->{'indent'}--;
600 9 $this->_printLine(']');
601 9 $this->{'indent'}--;
602 9 $this->_printLine('}');
603 }
604  
605 ###########################################################################
606 9 ▶sub _printAtomShape {
Subroutine _printAtomShape: called CC=2 SCAR=6.9
607 9 my $this = shift;
608 9 my $atom = shift;
609  
610 9 my $l = $atom->{'label'};
611 9 ▶ if ($this->{'DefUse'}->{"AtomShape$l"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"AtomShape$l"}) 6 3 9
612 6 $this->_printLine("USE ATOMSHAPE_$l");
613 6 return;
614 }
615  
616 3 $this->_printLine("DEF ATOMSHAPE_$l Shape {");
617 3 $this->{'indent'}++;
618 3 $this->_printAtomAppearance($atom);
619 3 $this->_printAtomGeometry($atom);
620 3 $this->{'indent'}--;
621 3 $this->_printLine('}');
622  
623 3 $this->{'DefUse'}->{"AtomShape$l"} = 1;
624 }
625  
626 ###########################################################################
627 3 ▶sub _printAtomAppearance {
Subroutine _printAtomAppearance: called CC=2 SCAR=7.0
628 3 my $this = shift;
629 3 my $atom = shift;
630  
631 3 my $c = $atom->{'color'};
632 3 ▶ if ($this->{'DefUse'}->{"AtomApp$c"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"AtomApp$c"}) 0 3 3
633 0 $this->_printLine("appearance USE ATOMAPP_$c");
634 0 return;
635 }
636  
637 3 $this->_printLine("appearance DEF ATOMAPP_$c Appearance {");
638 3 $this->{'indent'}++;
639 3 $this->_printLine('material Material {');
640 3 $this->{'indent'}++;
641 3 $this->_printLine("diffuseColor $RGBColors[$c]");
642 3 $this->_printLine('specularColor 1 1 1');
643 3 $this->_printLine('shininess 0.75');
644 3 $this->{'indent'}--;
645 3 $this->_printLine('}');
646 3 $this->{'indent'}--;
647 3 $this->_printLine('}');
648  
649 3 $this->{'DefUse'}->{"AtomApp$c"} = 1;
650 }
651  
652 ###########################################################################
653 3 ▶sub _printAtomGeometry {
Subroutine _printAtomGeometry: called CC=4 SCAR=14.8
654 3 my $this = shift;
655 3 my $atom = shift;
656  
657 3 my $l = $atom->{'label'};
658 3 ▶ if ($this->{'DefUse'}->{"AtomGeo$l"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"AtomGeo$l"}) 0 3 3
659 0 $this->_printLine("geometry USE ATOMGEO_$l");
660 0 return;
661 }
662  
663 3 my $r = $this->{RadiusStick};
664 3 ▶ $r = $this->{RadiusBNS} * $atom->{'radius'} if ($this->{'style'} =~ /ball/);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"style"} =~ /ball/ 3 0 3
665 3 ▶ $r = $atom->{'radius'} if ($this->{'style'} =~ /cpk/);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"style"} =~ /cpk/ 0 3 3
666 3 $this->_printLine("geometry DEF ATOMGEO_$l Sphere { radius $r }");
667  
668 3 $this->{'DefUse'}->{"AtomGeo$l"} = 1;
669 }
670  
671 ###########################################################################
672 0 ▶sub _printBond {
Subroutine _printBond: not called CC=2 SCAR=17.9
673 0 my $this = shift;
674 0 my $bond = shift;
675  
676 0 my ($from, $to) = ($bond->{'from'}, $bond->{'to'});
677 0 my ($tx, $ty, $tz, $s, $ax, $ay, $az, $angle) =
678 $this->_calcBond($from, $to);
679  
680 0 foreach ($tx, $ty, $tz, $s, $ax, $ay, $az, $angle) {
681 0 $_ = sprintf("%.5g", $_);
682 }
683  
684 0 my $l = $bond->{'label'};
685 0 $this->_printLine("DEF BOND_$l Transform {");
686 0 $this->{'indent'}++;
687 0 $this->_printLine("translation $tx $ty $tz");
688 0 $this->_printLine("scale 1 $s 1");
689 0 $this->_printLine("rotation $ax $ay $az $angle");
690 0 $this->_printLine('children [');
691 0 $this->{'indent'}++;
692 0 $this->_printBondShape($from);
693 0 $this->{'indent'}--;
694 0 $this->_printLine(']');
695 0 $this->{'indent'}--;
696 0 $this->_printLine('}');
697 }
698  
699 ###########################################################################
700 0 ▶sub _printBondShape {
Subroutine _printBondShape: not called CC=2 SCAR=17.9
701 0 my $this = shift;
702 0 my $from = shift;
703  
704 0 my $c = $from->{'color'};
705 0 ▶ if ($this->{'DefUse'}->{"BondShape$c"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"BondShape$c"}) 0 0 0
706 0 $this->_printLine("USE BONDSHAPE_$c");
707 0 return;
708 }
709  
710 0 $this->_printLine("DEF BONDSHAPE_$c Shape {");
711 0 $this->{'indent'}++;
712 0 $this->_printAtomAppearance($from); # bond color is the same as atom color
713 0 $this->_printBondGeometry();
714 0 $this->{'indent'}--;
715 0 $this->_printLine('}');
716  
717 0 $this->{'DefUse'}->{"BondShape$c"} = 1;
718 }
719  
720 ###########################################################################
721 0 ▶sub _printBondGeometry {
Subroutine _printBondGeometry: not called CC=2 SCAR=17.9
722 0 my $this = shift;
723  
724 0 ▶ if ($this->{'DefUse'}->{'BondGeo'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DefUse"}{"BondGeo"}) 0 0 0
725 0 $this->_printLine('geometry USE BONDGEO');
726 0 return;
727 }
728  
729 $this->_printLine(
730 0 "geometry DEF BONDGEO Cylinder { radius $this->{RadiusStick} top FALSE bottom FALSE}"
731 );
732  
733 0 $this->{'DefUse'}->{'BondGeo'} = 1;
734 }
735  
736 ###########################################################################
737 #
738 # print VRML tail
739 #
740 ###########################################################################
741 1 ▶sub _printTail {
Subroutine _printTail: called CC=1 SCAR=0.0
742 1 my $this = shift;
743 1 my $fh = $this->{fh};
744 1 print $fh <<EOT;
745 ]
746 }
747 EOT
748 }
749  
750 ###########################################################################
751 118 ▶sub _printLine {
Subroutine _printLine: called CC=3 SCAR=11.1
752 118 my $this = shift;
753 118 my $str = shift;
754 118 my $fh = $this->{fh};
755  
756 0 ▶ if ($this->{Compression}) { print $fh "$str\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'Compression'}) { } 0 118 118
757 else {
758 118 my $i = "\t" x int($this->{'indent'} >> 1);
759 118 ▶ $i .= ' ' if ($this->{'indent'} & 0x1);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"indent"} & 1 53 65 118
760 118 print $fh "$i$str\n";
761 }
762 }
763  
764 ###########################################################################
765 #
766 # Calculate bond transformation parameters
767 # Syntax: @geometry = _calcBond(\%atom1, \%atom2);
768 #
769 ###########################################################################
770 0 ▶sub _calcBond {
Subroutine _calcBond: not called CC=3 SCAR=24.8
771 0 my $this = shift;
772 0 my $atom1 = shift;
773 0 my $atom2 = shift;
774  
775 0 my ($x1, $y1, $z1) = ($atom1->{'x'}, $atom1->{'y'}, $atom1->{'z'});
776 0 my ($x2, $y2, $z2) = ($atom2->{'x'}, $atom2->{'y'}, $atom2->{'z'});
777  
778 0 my ($dx, $dy, $dz) = ($x2 - $x1, $y2 - $y1, $z2 - $z1);
779  
780 # length
781 0 my $s = sqrt($dx * $dx + $dy * $dy + $dz * $dz);
782  
783 # translation
784 0 my ($tx, $ty, $tz) = (0.5 * $dx, 0.5 * $dy, 0.5 * $dz);
785  
786 0 ($dx, $dy, $dz) = ($dx / $s, $dy / $s, $dz / $s);
787  
788 # rotation axis and angle
789 0 my ($ax, $ay, $az, $angle);
790 0 ▶ if ($dy > 0.9999) { ($ax, $ay, $az, $angle) = (1, 0, 0, 0); }
Branch: 2 branches
BranchTrueFalseTotal
if ($dy > 0.9999) { } 0 0 0
elsif ($dy < -0.9999) { } 0 0 0
791 0 elsif ($dy < -0.9999) { ($ax, $ay, $az, $angle) = (1, 0, 0, $PI); }
792 0 else { ($ax, $ay, $az, $angle) = ($dz, 0, -$dx, acos($dy)); }
793  
794 0 return $tx, $ty, $tz, 0.5 * $s, $ax, $ay, $az, $angle;
795 }
796  
797 ###########################################################################
798 #
799 # Generate connectivities
800 #
801 ###########################################################################
802 0 ▶sub genBonds {
Subroutine genBonds: not called CC=18 SCAR=56.9
803 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
804 0 my $this = shift;
805  
806 # find largest possible distance
807 0 my $maxR;
808 0 ▶ foreach (values %AtomRadius) { $maxR = $_ if ($_ > $maxR); }
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $maxR 0 0 0
809 0 $maxR *= 2.4;
810  
811 # find the most negative coordinates to avoid negative indizes
812 0 my ($minX, $minY, $minZ);
813 0 foreach (@{$this->{'atoms'}}) {
814 0 ▶ $minX = $_->{'x'} if ($_->{'x'} < $minX);
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"x"} < $minX 0 0 0
815 0 ▶ $minY = $_->{'y'} if ($_->{'y'} < $minY);
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"y"} < $minY 0 0 0
816 0 ▶ $minZ = $_->{'z'} if ($_->{'z'} < $minZ);
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"z"} < $minZ 0 0 0
817 }
818 0 $minX -= 2.5 * $maxR;
819 0 $minY -= 2.5 * $maxR;
820 0 $minZ -= 2.5 * $maxR;
821  
822 # distribute atoms in a grid with $maxR cell distance
823 0 my (@grid, $maxI, $maxJ, $maxK);
824 0 foreach (@{$this->{'atoms'}}) {
825 0 my $i = int(($_->{'x'} - $minX) / $maxR);
826 0 my $j = int(($_->{'y'} - $minY) / $maxR);
827 0 my $k = int(($_->{'z'} - $minZ) / $maxR);
828 0 push(@{$grid[$i][$j][$k]}, $_);
829 0 ▶ $maxI = $i if ($i > $maxI);
Branch: 1 branch
BranchTrueFalseTotal
if $i > $maxI 0 0 0
830 0 ▶ $maxJ = $j if ($j > $maxJ);
Branch: 1 branch
BranchTrueFalseTotal
if $j > $maxJ 0 0 0
831 0 ▶ $maxK = $k if ($k > $maxK);
Branch: 1 branch
BranchTrueFalseTotal
if $k > $maxK 0 0 0
832 }
833  
834 # loop of grid cells and find bonds
835 0 my ($i, $j, $k, $a, $b, $c);
836 0 ▶ for ($i = 1 ; $i <= $maxI ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $maxI) 0 0 0
837 0 ▶ for ($j = 1 ; $j <= $maxJ ; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $maxJ) 0 0 0
838 0 ▶ for ($k = 1 ; $k <= $maxK ; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $maxK) 0 0 0
839 0 foreach (@{$grid[$i][$j][$k]}) {
840 0 foreach $a (-1 .. 1) {
841 0 foreach $b (-1 .. 1) {
842 0 foreach $c (-1 .. 1) {
843 $this->_atomToGrid($_,
844 0 \@{$grid[$i + $a][$j + $b][$k + $c]});
845 }
846 }
847 }
848 }
849 }
850 }
851 }
852 }
853  
854 ###########################################################################
855 0 ▶sub _atomToGrid {
Subroutine _atomToGrid: not called CC=5 SCAR=34.0
856 0 my $this = shift;
857 0 my $atom1 = shift;
858 0 my $grid = shift;
859  
860 0 my $n1 = $atom1->{'nr'};
861 0 my ($x1, $y1, $z1) = ($atom1->{'x'}, $atom1->{'y'}, $atom1->{'z'});
862 0 my $ar1 = $AtomRadius{$atom1->{'label'}};
863  
864 0 my $atom2;
865 0 foreach $atom2 (@$grid) {
866 0 my $n2 = $atom2->{'nr'};
867 0 ▶ next unless ($n1 < $n2);
Branch: 1 branch
BranchTrueFalseTotal
unless $n1 < $n2 0 0 0
868  
869 0 my $ar2 = $ar1 + $AtomRadius{$atom2->{'label'}};
870 0 $ar2 *= 1.2;
871 0 $ar2 *= $ar2;
872  
873 0 my ($x2, $y2, $z2) = ($atom2->{'x'}, $atom2->{'y'}, $atom2->{'z'});
874 0 my ($dx, $dy, $dz) = ($x2 - $x1, $y2 - $y1, $z2 - $z1);
875 0 my $dist = $dx * $dx + $dy * $dy + $dz * $dz;
876 0 ▶ next if ($dist > $ar2);
Branch: 1 branch
BranchTrueFalseTotal
if $dist > $ar2 0 0 0
877 0 my $label = $atom1->{'nr'} . '_' . $atom2->{'nr'};
878 0 ▶ next if (exists($this->{'BLT'}->{$label}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"BLT"}{$label} 0 0 0
879 0 my (%bond) = ('from' => $atom1, 'to' => $atom2);
880 0 push(@{$this->{'bonds'}}, \%bond);
881 }
882 }
883  
884 ###########################################################################
885  
886 10 1;
887  
888 __END__
« blib/lib/Chemistry/Canonicalize.pm ↑ Summary blib/lib/Chemistry/FormulaPattern.pm »