| 1 | package Chemistry::File::SDF; | ||||||||||
| 2 | 9 | $VERSION = '0.20'; | |||||||||
| 3 | # $Id: SDF.pm,v 1.9 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 Chemistry::File::MDLMol; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Chemistry::File::SDF - MDL Structure Data File reader/writer | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | use Chemistry::File::SDF; | ||||||||||
| 18 | |||||||||||
| 19 | # Simple interface (all at once) | ||||||||||
| 20 | # read all the molecules in the file | ||||||||||
| 21 | my @mols = Chemistry::Mol->read('myfile.sdf'); | ||||||||||
| 22 | |||||||||||
| 23 | # assuming that the file includes a <PKA> data item... | ||||||||||
| 24 | print $mols[0]->attr("sdf/data")->{PKA}; | ||||||||||
| 25 | |||||||||||
| 26 | # write a bunch of molecules to an SDF file | ||||||||||
| 27 | Chemistry::Mol->write('myfile.sdf', mols => \@mols); | ||||||||||
| 28 | |||||||||||
| 29 | # or write just one molecule | ||||||||||
| 30 | $mol->write('myfile.sdf'); | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | # Low level interface (one at a time) | ||||||||||
| 34 | # create reader | ||||||||||
| 35 | my $reader = Chemistry::Mol->file('myfile.sdf'); | ||||||||||
| 36 | $reader->open('<'); | ||||||||||
| 37 | while (my $mol = $reader->read_mol($reader->fh)) { | ||||||||||
| 38 | # do something with $mol | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | =cut | ||||||||||
| 42 | |||||||||||
| 43 | 9 | Chemistry::Mol->register_format(sdf => __PACKAGE__); | |||||||||
| 44 | |||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||
| 46 | |||||||||||
| 47 | MDL SDF (V2000) reader. | ||||||||||
| 48 | |||||||||||
| 49 | This module automatically registers the 'sdf' format with Chemistry::Mol. | ||||||||||
| 50 | |||||||||||
| 51 | The parser returns a list of Chemistry::Mol objects. SDF data can be accessed | ||||||||||
| 52 | by the $mol->attr method. Attribute names are stored as a hash ref at the | ||||||||||
| 53 | "sdf/data" attribute, as shown in the synopsis. When a data item has a single | ||||||||||
| 54 | line in the SDF file, the attribute is stored as a string; when there's more | ||||||||||
| 55 | than one line, they are stored as an array reference. The rest of the | ||||||||||
| 56 | information on the line that holds the field name is ignored. | ||||||||||
| 57 | |||||||||||
| 58 | This module is part of the PerlMol project, L<http://www.perlmol.org>. | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub slurp_mol { | ||||||||
|
Subroutine
slurp_mol: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 63 | 0 | my ($self, $fh, %opts) = @_; | |||||||||
| 64 | 0 | ▶ | return if $fh->eof; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | my $s; | |||||||||
| 66 | 0 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | ▶ | last if /^\$\$\$\$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | $s .= $_; | |||||||||
| 69 | } | ||||||||||
| 70 | 0 | $s =~ s/\r\n?/\n/g; # normalize EOL | |||||||||
| 71 | 0 | $s; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | sub skip_mol { | ||||||||
|
Subroutine
skip_mol: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 75 | 0 | my ($self, $fh, %opts) = @_; | |||||||||
| 76 | 0 | ▶ | return if $fh->eof; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | ▶ | return 1 if /^\$\$\$\$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 0 | return 0; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub read_mol { | ||||||||
|
Subroutine
read_mol: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 84 | 0 | my ($self, $fh, %opts) = @_; | |||||||||
| 85 | 0 | ▶ | my $s = $self->slurp_mol($fh, %opts) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | my $mol = Chemistry::File::MDLMol->parse_string($s, %opts); | |||||||||
| 87 | 0 | $self->parse_data($mol, $s); | |||||||||
| 88 | 0 | $mol; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | sub parse_data { | ||||||||
|
Subroutine
parse_data: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 92 | 0 | my ($self, $mol, $mol_string) = @_; | |||||||||
| 93 | 0 | my (@items) = split /\n>/, $mol_string; | |||||||||
| 94 | 0 | shift @items; # drop everything until first datum | |||||||||
| 95 | 0 | my %data_block; | |||||||||
| 96 | 0 | for my $item (@items) { | |||||||||
| 97 | 0 | my ($header, @data) = split /\n/, $item; | |||||||||
| 98 | 0 | my ($field_name) = $header =~ /<(.*?)>/g; | |||||||||
| 99 | 0 | ▶ | warn "SDF: no field name\n", next unless $field_name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | #$mol->attr("sdf/$field_name", @data == 1 ? $data[0] : \@data); | ||||||||||
| 101 | 0 | ▶ | $data_block{$field_name} = @data == 1 ? $data[0] : \@data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | |||||||||||
| 103 | } | ||||||||||
| 104 | 0 | $mol->attr("sdf/data", \%data_block); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | sub write_string { | ||||||||
|
Subroutine
write_string: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 108 | 0 | my ($self, $mol_ref, %opts) = @_; | |||||||||
| 109 | 0 | my @mols; | |||||||||
| 110 | 0 | my $ret = ''; | |||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | if ($opts{mols}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | @mols = @{$opts{mols}}; | |||||||||
| 114 | } else { | ||||||||||
| 115 | 0 | @mols = $mol_ref; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | for my $mol (@mols) { | |||||||||
| 119 | 0 | $ret .= $mol->print(format => 'mdl'); | |||||||||
| 120 | 0 | $ret .= format_data($mol->attr('sdf/data')) . '$$$$'."\n"; | |||||||||
| 121 | } | ||||||||||
| 122 | 0 | $ret; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | sub format_data { | ||||||||
|
Subroutine
format_data: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 126 | 0 | my ($data) = @_; | |||||||||
| 127 | 0 | my $ret = ''; | |||||||||
| 128 | 0 | ▶ | return $ret unless $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | for my $field_name (sort keys %$data) { | |||||||||
| 130 | 0 | $ret .= "> <$field_name>\n"; | |||||||||
| 131 | 0 | my $value = $data->{$field_name}; | |||||||||
| 132 | 0 | ▶ | if (ref $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | $ret .= join "\n", @$value; | |||||||||
| 134 | } else { | ||||||||||
| 135 | 0 | $ret .= "$value\n"; | |||||||||
| 136 | } | ||||||||||
| 137 | 0 | $ret .= "\n"; | |||||||||
| 138 | } | ||||||||||
| 139 | 0 | $ret; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | sub file_is { | ||||||||
|
Subroutine
file_is: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 143 | 0 | my ($self, $fname) = @_; | |||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | return 1 if $fname =~ /\.sdf?$/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 0 | return 0; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | sub name_is { | ||||||||
|
Subroutine
name_is: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 150 | 0 | my ($self, $fname) = @_; | |||||||||
| 151 | 0 | $fname =~ /\.sdf?$/i; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | sub string_is { | ||||||||
|
Subroutine
string_is: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 155 | 0 | my ($self, $s) = @_; | |||||||||
| 156 | 0 | ▶ | /\$\$\$\$/ ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | } | ||||||||||
| 158 | 9 | 1; | |||||||||
| 159 | |||||||||||
| 160 | =head1 CAVEATS | ||||||||||
| 161 | |||||||||||
| 162 | Note that by storing the SDF data as a hash, there can be only one field with | ||||||||||
| 163 | a given name. The SDF format description is not entirely clear in this regard. | ||||||||||
| 164 | Also note that SDF data field names are considered to be case-sensitive. | ||||||||||
| 165 | |||||||||||
| 166 | =head1 VERSION | ||||||||||
| 167 | |||||||||||
| 168 | 0.20 | ||||||||||
| 169 | |||||||||||
| 170 | =head1 SEE ALSO | ||||||||||
| 171 | |||||||||||
| 172 | L<Chemistry::Mol> | ||||||||||
| 173 | |||||||||||
| 174 | The MDL file format specification. | ||||||||||
| 175 | L<http://www.mdl.com/downloads/public/ctfile/ctfile.pdf> or | ||||||||||
| 176 | Arthur Dalby et al., J. Chem. Inf. Comput. Sci, 1992, 32, 244-255. | ||||||||||
| 177 | |||||||||||
| 178 | The PerlMol website L<http://www.perlmol.org/> | ||||||||||
| 179 | |||||||||||
| 180 | =head1 AUTHOR | ||||||||||
| 181 | |||||||||||
| 182 | Ivan Tubert-Brohman <itub@cpan.org> | ||||||||||
| 183 | |||||||||||
| 184 | =head1 COPYRIGHT | ||||||||||
| 185 | |||||||||||
| 186 | Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is | ||||||||||
| 187 | free software; you can redistribute it and/or modify it under the same terms as | ||||||||||
| 188 | Perl itself. | ||||||||||
| 189 | |||||||||||
| 190 | =cut | ||||||||||
| 191 | |||||||||||