blib/lib/Chemistry/File.pm

filter: Statementstmt 82.3% 121 / 147 Branchbran 66.6% 44 / 66 Conditioncond 62.9% 17 / 27 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 80.0% 24 / 30 Podpod 95.2% 20 / 21 totaltotal 76.1% 233 / 306 CC 47 SCAR 22.2
CC
47
Cov
76%
open
21.8
close
13.7
import
7.0
CRAP 9.2
« blib/lib/Math/VectorReal.pm ↑ Summary blib/lib/Chemistry/Mol.pm »
1 package Chemistry::File;
2 24 $VERSION = '0.35';
3  
4 =head1 NAME
5  
6 Chemistry::File - Molecule file I/O base class
7  
8 =head1 SYNOPSIS
9  
10 # As a convenient interface for several mol readers:
11 use Chemistry::File qw(PDB MDLMol); # load PDB and MDL modules
12
13 # or try to use every file I/O module installed in the system:
14 use Chemistry::File ':auto';
15  
16 my $mol1 = Chemistry::Mol->read("file.pdb");
17 my $mol2 = Chemistry::Mol->read("file.mol");
18  
19  
20 # as a base for a mol reader:
21  
22 package Chemistry::File::Myfile;
23 use base qw(Chemistry::File);
24 Chemistry::Mol->register_type("myfile", __PACKAGE__);
25  
26 # override the read_mol method
27 sub read_mol {
28 my ($self, $fh, %opts) = shift;
29 my $mol_class = $opts{mol_class} || "Chemistry::Mol";
30 my $mol = $mol_class->new;
31 # ... do some stuff with $fh and $mol ...
32 return $mol;
33 }
34  
35 # override the write_mol method
36 sub write_mol {
37 my ($self, $fh, $mol, %opts) = shift;
38 print $fh $mol->name, "\n";
39 # ... do some stuff with $fh and $mol ...
40 }
41  
42 =head1 DESCRIPTION
43  
44 The main use of this module is as a base class for other molecule file I/O
45 modules (for example, Chemistry::File::PDB). Such modules should override and
46 extend the Chemistry::File methods as needed. You only need to care about the
47 methods here if if you are writing a file I/O module or if you want a finer
48 degree of control than what is offered by the simple read and write methods
49 in the Chemistry::Mol class.
50  
51 From the user's point of view, this module can also be used as shorthand
52 for using several Chemistry::File modules at the same time.
53  
54 use Chemistry::File qw(PDB MDLMol);
55  
56 is exactly equivalent to
57  
58 use Chemistry::File::PDB;
59 use Chemistry::File::MDLMol;
60  
61 If you use the :auto keyword, Chemistry::File will autodetect and load
62 all the Chemistry::File::* modules installed in your system.
63  
64 use Chemistry::File ':auto';
65  
66 =head1 FILE I/O MODEL
67  
68 Before version 0.30, file I/O modules typically used only parse_string,
69 write_string, parse_file, and write_file, and they were generally used as class
70 methods. A file could contain one or more molecules and only be read or written
71 whole; reading it would return every molecule on the file. This was problematic
72 when dealing with large multi-molecule files (such as SDF files), because all
73 the molecules would have to be loaded into memory at the same time.
74  
75 While version 0.30 retains backward compatibility with that simple model, it
76 also allows a more flexible interface that allows reading one molecule at a
77 time, skipping molecules, and reading and writing file-level information that
78 is not associated with specific molecules. The following diagram shows the
79 global structure of a file according to the new model:
80  
81 +-----------+
82 | header |
83 +-----------+
84 | molecule |
85 +-----------+
86 | molecule |
87 +-----------+
88 | ... |
89 +-----------+
90 | footer |
91 +-----------+
92  
93 In cases where the header and the footer are empty, the model reduces to the
94 pre-0.30 version. The low-level steps to read a file are the following:
95  
96 $file = Chemistry::File::MyFormat->new(file => 'xyz.mol');
97 $file->open('<');
98 $file->read_header;
99 while (my $mol = $self->read_mol($file->fh, %opts)) {
100 # do something with $mol...
101 }
102 $self->read_footer;
103  
104 The C<read> method does all the above automatically, and it stores all the
105 molecules read in the mols property.
106  
107 =head1 STANDARD OPTIONS
108  
109 All the methods below include a list of options %opts at the end of the
110 parameter list. Each class implementing this interface may have its own
111 particular options. However, the following options should be recognized by all
112 classes:
113  
114 =over
115  
116 =item mol_class
117  
118 A class or object with a C<new> method that constructs a molecule. This is
119 needed when the user want to specify a molecule subclass different from the
120 default. When this option is not defined, the module may use Chemistry::Mol
121 or whichever class is appropriate for that file format.
122  
123 =item format
124  
125 The name of the file format being used, as registered by
126 Chemistry::Mol->register_format.
127  
128 =item fatal
129  
130 If true, parsing errors should throw an exception; if false, they should just
131 try to recover if possible. True by default.
132  
133 =back
134  
135 =head1 CLASS METHODS
136  
137 The class methods in this class (or rather, its derived classes) are usually
138 not called directly. Instead, use Chemistry::Mol->read, write, print, parse,
139 and file. These methods also work if called as instance methods.
140  
141 =over
142  
143  
144 =cut
145  
146 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 24 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
149 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 24 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 24 ▶use base qw(Chemistry::Obj);
Subroutine BEGIN: called CC=1 SCAR=0.0
152 # don't blame our problems in the Chemistry::Mol module ;-)
153 24 our @CARP_NOT = qw(Chemistry::Mol);
154  
155 # This subroutine implements the :auto functionality
156 35 ▶sub import {
Subroutine import: called CC=6 SCAR=19.4
157 35 my $pack = shift;
158 35 for my $param (@_){
159 9 ▶ if ($param eq ':auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq ':auto') { } 9 0 9
160 24 ▶ for my $pmfile (map {glob "$_/Chemistry/File/*.pm"} @INC) {
Subroutine BEGIN: called CC=1 SCAR=0.0
161 338 my ($pm) = $pmfile =~ m|(Chemistry/File/.*\.pm)$|;
162 #warn "requiring $pm\n";
163 338 eval { require $pm };
164 338 ▶ die "Error in Chemistry::File: '$@'; pmfile='$pmfile'; pm='$pm'\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 338 338
165 }
166 } else {
167 0 eval "use ${pack}::$param";
168 0 ▶ die "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
169 }
170 }
171 }
172  
173 =item $class->parse_string($s, %options)
174  
175 Parse a string $s and return one or mole molecule objects. This is an abstract
176 method, so it should be provided by all derived classes.
177  
178 =cut
179  
180 14 ▶sub parse_string {
Subroutine parse_string: called CC=3 SCAR=16.0
Pod documented
181 14 my ($self, $s, %opts) = @_;
182 14 ▶ if ($opts{_must_override}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"_must_override"}) 0 14 14
183 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
184 0 croak "parse_string() is not implemented for $class";
185 }
186 14 $self->new(file => \$s, opts => \%opts)->read;
187 }
188  
189  
190 =item $class->write_string($mol, %options)
191  
192 Convert a molecule to a string. This is an abstract method, so it should be
193 provided by all derived classes.
194  
195 =cut
196  
197 4 ▶sub write_string {
Subroutine write_string: called CC=3 SCAR=14.2
Pod documented
198 4 my ($self, $mol, %opts) = @_;
199 4 ▶ if ($opts{_must_override}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"_must_override"}) 0 4 4
200 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
201 0 croak "write_string() is not implemented for $class";
202 }
203 4 my $s;
204 4 $self->new(file => \$s, mols => [$mol], opts => \%opts)->write;
205 4 $s;
206 }
207  
208 =item $class->parse_file($file, %options)
209  
210 Reads the file $file and returns one or more molecules. The default method
211 slurps the whole file and then calls parse_string, but derived classes may
212 choose to override it. $file can be a filehandle, a filename, or a scalar
213 reference. See C<new> for details.
214  
215 =cut
216  
217 13 ▶sub parse_file {
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
218 13 my ($self, $file, %opts) = @_;
219 13 $self->new(file => $file, opts => \%opts)->read;
220 }
221  
222 =item $class->write_file($mol, $file, %options)
223  
224 Writes a file $file containing the molecule $mol. The default method calls
225 write_string first and then saves the string to a file, but derived classes
226 may choose to override it. $file can be either a filehandle or a filename.
227  
228 =cut
229  
230 3 ▶sub write_file {
Subroutine write_file: called CC=1 SCAR=0.0
Pod documented
231 3 my ($self, $mol, $file, %opts) = @_;
232  
233 3 $self->new(file => $file, mols => [$mol], opts => \%opts)->write;
234 }
235  
236 =item $class->name_is($fname, %options)
237  
238 Returns true if a filename is of the format corresponding to the class.
239 It should look at the filename only, because it may be called with
240 non-existent files. It is used to determine with which format to save a file.
241 For example, the Chemistry::File::PDB returns true if the file ends in .pdb.
242  
243 =cut
244  
245 0 ▶sub name_is {
Subroutine name_is: not called CC=1 SCAR=6.9
Pod documented
246 0 0;
247 }
248  
249 =item $class->string_is($s, %options)
250  
251 Examines the string $s and returns true if it has the format of the class.
252  
253 =cut
254  
255 0 ▶sub string_is {
Subroutine string_is: not called CC=1 SCAR=6.9
Pod documented
256 0 0;
257 }
258  
259 =item $class->file_is($file, %options)
260  
261 Examines the file $file and returns true if it has the format of the class.
262 The default method slurps the whole file and then calls string_is, but derived
263 classes may choose to override it.
264  
265 =cut
266  
267 10 ▶sub file_is {
Subroutine file_is: called CC=3 SCAR=12.3
Pod documented
268 10 my ($self, $file, %opts) = @_;
269
270 10 my $s = eval {
271 10 $self->open('<');
272 0 $self->slurp;
273 };
274 10 ▶ if ($s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s) { } 0 10 10
elsif (not ref $file) { } 10 0 10
275 0 $self->string_is($s, %opts);
276 } elsif (! ref $file) {
277 10 $self->name_is($file, %opts);
278 }
279 }
280  
281 =item $class->slurp($file %opts)
282  
283 Reads a file into a scalar. Automatic decompression of gzipped files is
284 supported if the Compress::Zlib module is installed. Files ending in .gz are
285 assumed to be compressed; otherwise it is possible to force decompression by
286 passing the gzip => 1 option (or no decompression with gzip => 0).
287  
288 =cut
289  
290 # slurp a file into a scalar, with transparent decompression
291 0 ▶sub slurp {
Subroutine slurp: not called CC=1 SCAR=6.9
Pod documented
292 0 my ($self) = @_;
293  
294 0 my $fh = $self->fh;
295 0 local $/;
296 0 <$fh>;
297 }
298  
299 =item $class->new(file => $file, opts => \%opts)
300  
301 Create a new file object. This method is usually called indirectly via
302 the Chemistry::Mol->file method. $file may be a scalar with a filename, an
303 open filehandle, or a reference to a scalar. If a reference to a scalar is
304 used, the string contained in the scalar is used as an in-memory file.
305  
306 =cut
307  
308 39 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
309 39 my $self = shift->SUPER::new(@_);
310 39 ▶ $self->{opts}{fatal} = 1 unless exists $self->{opts}{fatal};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"opts"}{"fatal"} 39 0 39
311 39 $self;
312 }
313  
314 24 Chemistry::Obj::accessor(qw(file fh opts mols mode));
315  
316 =back
317  
318 =head1 INSTANCE METHODS
319  
320 =head2 Accessors
321  
322 Chemistry::File objects are derived from Chemistry::Obj and have the same
323 properties (name, id, and type), as well as the following ones:
324  
325 =over
326  
327 =item file
328  
329 The "file" as described above under C<new>.
330  
331 =item fh
332  
333 The filehandle used for reading and writing molecules. It is opened by C<open>.
334  
335 =item opts
336  
337 A hashref containing the options that are passed through to the old-style class
338 methods. They are also passed to the instance method to keep a similar
339 interface, but they could access them via $self->opts anyway.
340  
341 =item mode
342  
343 '>' if the file is open for writing, '<' for reading, and false if not open.
344  
345 =item mols
346  
347 C<read> stores all the molecules that were read in this property as an array
348 reference. C<write> gets the molecules to write from here.
349  
350 =back
351  
352 =head2 Abstract methods
353  
354 These methods should be overridden, because they don't really do much by
355 default.
356  
357 =over
358  
359 =item $file->read_header
360  
361 Read whatever information is available in the file before the first molecule.
362 Does nothing by default.
363  
364 =cut
365  
366 29 ▶sub read_header { }
Subroutine read_header: called CC=1 SCAR=0.0
Pod documented
367  
368 =item $file->read_footer
369  
370 Read whatever information is available in the file after the last molecule.
371 Does nothing by default.
372  
373 =cut
374  
375 29 ▶sub read_footer { }
Subroutine read_footer: called CC=1 SCAR=0.0
Pod documented
376  
377 =item $self->slurp_mol($fh)
378  
379 Reads from the input string until the end of the current molecule and returns
380 the "slurped" string. It does not parse the string. It returns undefined if
381 there are no more molecules in the file. This method should be overridden if
382 needed; by default, it slurps until the end of the file.
383  
384 =cut
385  
386 0 ▶sub slurp_mol {
Subroutine slurp_mol: not called CC=1 SCAR=6.9
Pod documented
387 0 my ($self, $fh) = @_;
388 0 local $/; <$fh>;
389 }
390  
391 =item $self->skip_mol($fh)
392  
393 Similar to slurp_mol, but it doesn't need to return anything except true or
394 false. It should also be overridden if needed; by default, it just calls
395 slurp_mol.
396  
397 =cut
398  
399 0 ▶sub skip_mol { shift->slurp_mol(@_) }
Subroutine skip_mol: not called CC=1 SCAR=6.9
Pod documented
400  
401 =item $file->read_mol($fh, %opts)
402  
403 Read the next molecule in the input stream. It returns false if there are no
404 more molecules in the file. This method should be overridden by derived
405 classes; otherwise it will call slurp_mol and parse_string (for backwards
406 compatibility; it is recommended to override read_mol directly in new modules).
407  
408 Note: some old file I/O modules (written before the 0.30 interface) may return
409 more than one molecule anyway, so it is recommended to call read_mol in list
410 context to be safe:
411  
412 ($mol) = $file->read_mol($fh, %opts);
413  
414 =cut
415  
416 4 ▶sub read_mol {
Subroutine read_mol: called CC=3 SCAR=11.0
Pod documented
417 4 my ($self, $fh, %opts) = @_;
418 4 my $s = $self->slurp_mol($fh);
419 4 ▶ return unless defined $s and length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 1 3 4
Condition66%2/3
defined $s and length $s
!ll&&!rl&&r
103
MC/DC50%1/2
defined $s and length $s
defined $s length $s
Truth table66%2/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
420 3 $self->parse_string($s, %opts, _must_override => 1);
421 }
422 =item $file->write_header
423  
424 Write whatever information is needed before the first molecule.
425 Does nothing by default.
426  
427 =cut
428  
429 7 ▶sub write_header { }
Subroutine write_header: called CC=1 SCAR=0.0
Pod undocumented
430  
431 =item $file->write_footer
432  
433 Write whatever information is needed after the last molecule.
434 Does nothing by default.
435  
436 =cut
437  
438 7 ▶sub write_footer { }
Subroutine write_footer: called CC=1 SCAR=0.0
Pod documented
439  
440 =item $self->write_mol($fh, $mol, %opts)
441  
442 Write one molecule to $fh. By default and for backward compatibility, it just
443 calls C<write_string> and prints its return value to $self->fh. New classes
444 should override it.
445  
446 =cut
447  
448 0 ▶sub write_mol {
Subroutine write_mol: not called CC=1 SCAR=6.9
Pod documented
449 0 my ($self, $fh, $mol, %opts) = @_;
450 0 print $fh $self->write_string($mol, %opts, _must_override => 1);
451 }
452  
453 ########################## OTHER ##################################
454  
455 =back
456  
457 =head2 Other methods
458  
459 =over
460  
461 =item $self->open($mode)
462  
463 Opens the file (held in $self->file) for reading by default, or for writing if
464 $mode eq '>'. This method sets $self->fh transparently regardless of whether
465 $self->file is a filename (compressed or not), a scalar reference, or a
466 filehandle.
467  
468 =cut
469  
470 48 ▶sub open {
Subroutine open: called CC=18 SCAR=30.8
Pod documented
471 48 my ($self, $mode) = @_;
472 48 my $fh;
473 my $s;
474 48 ▶ $mode ||= '<';
Condition50%1/2
$mode ||= "<"
l!l
480
MC/DC0%0/1
$mode ||= "<"
$mode
Truth table50%1/2
A
A= $mode
Aresult
11
00
475 48 $self->mode($mode);
476 48 my $file = $self->file;
477 48 ▶ croak "Chemistry::File::open: no file supplied" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 10 38 48
478 38 ▶ if (ref $file eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $file eq 'SCALAR') { } 19 19 38
elsif (ref $file) { } 0 19 19
elsif ($self->{'opts'}{'gzip'} or not defined $self->{'opts'}{'gzip'} and $file =~ /.gz$/) { } 4 15 19
479 19 ▶ croak "decompression only supported for files" if $self->{opts}{gzip};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"gzip"} 0 19 19
480 19 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.008) { } 19 0 19
481 19 open $fh, $mode, $file;
482 } else {
483 0 require IO::String;
484 0 $fh = IO::String->new($$file);
485 }
486 } elsif (ref $file) {
487 0 ▶ croak "decompression only supported for files" if $self->{opts}{gzip};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"gzip"} 0 0 0
488 0 $fh = $file;
489 ▶ } elsif ($self->{opts}{gzip}
Condition83%5/6
$self->{"opts"}{"gzip"} or not defined $self->{"opts"}{"gzip"} and $file =~ /.gz$/
l!l&&r!l&&!r
2215
not defined $self->{"opts"}{"gzip"} and $file =~ /.gz$/
!ll&&!rl&&r
0152
MC/DC66%2/3
$self->{"opts"}{"gzip"} or not defined $self->{"opts"}{"gzip"} and $file =~ /.gz$/
$self->{"opts"}{"gzip"} not defined $self->{"opts"}{"gzip"} $file =~ /.gz$/
Truth table75%3/4
A or not(B) and C
A= $self->{"opts"}{"gzip"}B= not defined $self->{"opts"}{"gzip"}C= $file =~ /.gz$/
ABCresult
1XX1
0111
00X0
0100
490 or !defined $self->{opts}{gzip} and $file =~ /.gz$/)
491 {
492 4 ▶ eval { require Compress::Zlib } # Carp
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Compress::Zlib } } or croak("Compress::Zlib not installed!") 4 0 4
493 or croak "Compress::Zlib not installed!";
494 4 require File::Temp;
495  
496 4 $fh = File::Temp::tempfile();
497 4 ▶ $self->{opts}{gzip} ||= 1;
Condition100%2/2
$self->{"opts"}{"gzip"} ||= 1
l!l
22
MC/DC100%1/1
$self->{"opts"}{"gzip"} ||= 1
$self->{"opts"}{"gzip"}
Truth table100%2/2
A
A= $self->{"opts"}{"gzip"}
Aresult
11
00
498 4 ▶ unless ($mode eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode eq ">") 2 2 4
499 2 ▶ my $gz = Compress::Zlib::gzopen($file, "rb")
Branch: 1 branch
BranchTrueFalseTotal
my $gz = Compress::Zlib::gzopen($file, "rb") or croak("Cannot open compressed ${file}: " . "$Compress::Zlib::gzerrno\n") 2 0 2
500 or croak "Cannot open compressed $file: "
501 . "$Compress::Zlib::gzerrno\n";
502  
503 2 my $buffer;
504 2 ▶ print $fh $buffer while $gz->gzread($buffer) > 0;
Branch: 1 branch
BranchTrueFalseTotal
$gz->gzread($buffer) > 0 and do { print $fh $buffer } 4 2 6
505
506 2 ▶ if ($Compress::Zlib::gzerrno != Compress::Zlib::Z_STREAM_END()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Compress::Zlib::gzerrno != Compress::Zlib::Z_STREAM_END()) 0 2 2
507 0 croak "Error reading from $file: $Compress::Zlib::gzerrno"
508 . ($Compress::Zlib::gzerrno+0) . "\n";
509 }
510 2 $gz->gzclose();
511 2 seek $fh, 0, 0;
512 }
513 } else {
514 15 ▶ $fh = FileHandle->new("$mode$file")
Branch: 1 branch
BranchTrueFalseTotal
$fh = "FileHandle"->new("$mode$file") or croak("Could not open file ${file}: $!") 14 1 15
515 or croak "Could not open file $file: $!";
516 }
517 37 $self->fh($fh);
518 37 $self;
519 }
520  
521 =item $self->close
522  
523 Close the file. For regular files this just closes the filehandle, but for
524 gzipped files it does some additional postprocessing. This method is called
525 automatically on object destruction, so it is not mandatory to call it
526 explicitly.
527  
528 =cut
529  
530 76 ▶sub close {
Subroutine close: called CC=13 SCAR=26.2
Pod documented
531 76 my ($self) = @_;
532 76 my $fh = $self->fh;
533 76 ▶ if ($fh and $self->mode eq '>' and $self->{opts}{gzip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh and $self->mode eq ">" and $self->{"opts"}{"gzip"}) 2 74 76
Condition100%6/6
$fh and $self->mode eq ">" and $self->{"opts"}{"gzip"}
!ll&&!rl&&r
6952
$fh and $self->mode eq ">"
!ll&&!rl&&r
2677
MC/DC100%3/3
$fh and $self->mode eq ">" and $self->{"opts"}{"gzip"}
$fh $self->mode eq ">" $self->{"opts"}{"gzip"}
Truth table100%4/4
A and B and C
A= $fhB= $self->mode eq ">"C= $self->{"opts"}{"gzip"}
ABCresult
0XX0
10X0
1100
1111
534 2 ▶ my $level = $self->{opts}{gzip} || 6;
Condition50%1/2
$self->{'opts'}{'gzip'} || 6
l!l
20
MC/DC0%0/1
$self->{'opts'}{'gzip'} || 6
$self->{'opts'}{'gzip'}
Truth table50%1/2
A
A= $self->{'opts'}{'gzip'}
Aresult
11
00
535 2 ▶ $level = 6 if $level == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 1 2 0 2
536 2 my $file = $self->file;
537 2 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 0 2 2
538 0 croak "compression only supported for files";
539 } else {
540 2 seek $fh, 0, 0;
541 2 ▶ my $gz = Compress::Zlib::gzopen($file, "wb$level")
Branch: 1 branch
BranchTrueFalseTotal
my $gz = Compress::Zlib::gzopen($file, "wb$level") or croak("Cannot open $file $Compress::Zlib::gzerrno\n") 2 0 2
542 or croak "Cannot open $file $Compress::Zlib::gzerrno\n";
543 2 local $_;
544 2 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 508 2 510
545 508 ▶ $gz->gzwrite($_)
Branch: 1 branch
BranchTrueFalseTotal
$gz->gzwrite($_) or croak("error writing: $Compress::Zlib::gzerrno\n") 508 0 508
546 or croak "error writing: $Compress::Zlib::gzerrno\n";
547 }
548 2 $gz->gzclose;
549 }
550 }
551 76 ▶ if ($self->mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode) 38 38 76
552 38 ▶ if ($fh) { $fh->close or croak "$!" };
Branch: 2 branches
BranchTrueFalseTotal
if ($fh) 37 1 38
$fh->close or croak("$!") 37 0 37
553 38 $self->mode('');
554 }
555 }
556  
557 39 ▶sub DESTROY { shift->close }
Subroutine DESTROY: called CC=1 SCAR=0.0
558  
559 =item $file->read
560  
561 Read the whole file. This calls open, read_header, read_mol until there are no
562 more molecules left, read_footer, and close. Returns a list of molecules if
563 called in list context, or the first molecule in scalar context.
564  
565 =cut
566  
567 31 ▶sub read {
Subroutine read: called CC=3 SCAR=11.0
Pod documented
568 31 my ($self) = @_;
569 31 $self->open('<');
570 30 $self->read_header;
571 30 my @all_mols;
572 30 ▶ while (my @mols = $self->read_mol($self->fh, %{$self->{opts}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@mols) = $self->read_mol($self->fh, %{$$self{"opts"};})) 32 30 62
573 32 push @all_mols, @mols;
574 }
575 30 $self->read_footer;
576 30 $self->close;
577 30 $self->mols(\@all_mols);
578 30 ▶ wantarray ? @all_mols : $all_mols[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 29 30
579 }
580  
581 =item $self->write
582  
583 Write all the molecules in $self->mols. It just calls open, write_header,
584 write_mol (per each molecule), write_footer, and close.
585  
586 =cut
587  
588 7 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod documented
589 7 my ($self) = @_;
590 7 $self->open('>');
591 7 $self->write_header;
592 7 for my $mol (@{$self->mols}) {
593 7 $self->write_mol($self->fh, $mol, %{$self->{opts}});
594 }
595 7 $self->write_footer;
596 7 $self->close;
597 }
598  
599 24 1;
600  
601 =back
602  
603 =head1 CAVEATS
604  
605 The :auto feature may not be entirely portable, but it is known to work under
606 Unix and Windows (either Cygwin or Activestate).
607  
608 =head1 VERSION
609  
610 0.35
611  
612 =head1 SEE ALSO
613  
614 L<Chemistry::Mol>
615  
616 The PerlMol website L<http://www.perlmol.org/>
617  
618 =head1 AUTHOR
619  
620 Ivan Tubert-Brohman-Brohman <itub@cpan.org>
621  
622 =head1 COPYRIGHT
623  
624 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
625 free software; you can redistribute it and/or modify it under the same terms as
626 Perl itself.
627  
628 =cut
629  
« blib/lib/Math/VectorReal.pm ↑ Summary blib/lib/Chemistry/Mol.pm »