1 package MoleculeFileIO; 2 # 3 # $RCSfile: MoleculeFileIO.pm,v $ 4 # $Date: 2008/04/25 00:00:46 $ 5 # $Revision: 1.15 $ 6 # 7 # Author: Manish Sud <msud@san.rr.com> 8 # 9 # Copyright (C) 2004-2008 Manish Sud. All rights reserved. 10 # 11 # This file is part of MayaChemTools. 12 # 13 # MayaChemTools is free software; you can redistribute it and/or modify it under 14 # the terms of the GNU Lesser General Public License as published by the Free 15 # Software Foundation; either version 3 of the License, or (at your option) any 16 # later version. 17 # 18 # MayaChemTools is distributed in the hope that it will be useful, but without 19 # any warranty; without even the implied warranty of merchantability of fitness 20 # for a particular purpose. See the GNU Lesser General Public License for more 21 # details. 22 # 23 # You should have received a copy of the GNU Lesser General Public License 24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or 25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330, 26 # Boston, MA, 02111-1307, USA. 27 # 28 use 5.006; 29 use strict; 30 use Carp; 31 use Exporter; 32 use Scalar::Util (); 33 use FileIO::SDFileIO; 34 use FileIO::MDLMolFileIO; 35 36 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); 37 38 $VERSION = '1.00'; 39 @ISA = qw(Exporter); 40 @EXPORT = qw(); 41 @EXPORT_OK = qw(IsSupportedMoleculeFileFormat); 42 43 %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); 44 45 # Setup class variables... 46 my($ClassName); 47 _InitializeClass(); 48 49 # Class constructor... 50 sub new { 51 my($Class, %NamesAndValues) = @_; 52 53 # Initialize object... 54 my $This = {}; 55 bless $This, ref($Class) || $Class; 56 $This->_InitializeMoleculeFileIO(); 57 58 $This->_InitializeMoleculeFileIOProperties(%NamesAndValues); 59 60 return $This; 61 } 62 63 # Initialize object data... 64 # 65 sub _InitializeMoleculeFileIO { 66 my($This) = @_; 67 68 # Reference to specific FileIO object... 69 $This->{FileIORef} = ''; 70 71 return $This; 72 } 73 74 # Initialize class ... 75 sub _InitializeClass { 76 #Class name... 77 $ClassName = __PACKAGE__; 78 79 } 80 81 # Initialize object properties...... 82 # 83 sub _InitializeMoleculeFileIOProperties { 84 my($This, %NamesAndValues) = @_; 85 86 if (!exists $NamesAndValues{Name}) { 87 croak "Error: ${ClassName}->New: Object can't be instantiated without specifying file name..."; 88 } 89 90 if (!exists $NamesAndValues{Mode}) { 91 $NamesAndValues{Mode} = 'Read'; 92 } 93 94 # Make sure its a supported format and intialize FileIO object reference... 95 $This->_SetFileIORef(%NamesAndValues); 96 97 return $This; 98 } 99 100 # Setup FileIO object reference... 101 sub _SetFileIORef { 102 my($This, %NamesAndValues) = @_; 103 my($Name, $Status, $Format, $IOPackageName); 104 105 $Name = $NamesAndValues{Name}; 106 107 ($Status, $Format, $IOPackageName) = $This->IsSupportedMoleculeFileFormat($Name); 108 if (!$Status) { 109 croak "Error: ${ClassName}->New: Object can't be instantiated: File format, $Name, is not supported: Currently supported file formats are: SDF, MDLMol..."; 110 } 111 112 $This->{FileIORef} = ${IOPackageName}->new(%NamesAndValues); 113 114 return $This; 115 } 116 117 # Is it a supported file format? 118 # 119 # In scalar context only status is returned; otherwise, file format and file IO package name is also 120 # returned. 121 # 122 # Note: 123 # . To support additional file formats, this is the only method which needs to be changed. 124 # 125 # . Currently supported file formats are: 126 # 127 # SDF .sdf, .sd 128 # MDLMol .mol 129 # 130 sub IsSupportedMoleculeFileFormat { 131 my($FirstParameter, $SecondParameter) = @_; 132 my($This, $Name); 133 134 if ((@_ == 2) && (_IsMoleculeFileIO($FirstParameter))) { 135 ($This, $Name) = ($FirstParameter, $SecondParameter); 136 } 137 else { 138 ($Name) = ($FirstParameter); 139 } 140 my($Status, $Format, $IOPackageName); 141 142 $Status = 0; $Format = 'NotSupported'; $IOPackageName = 'Unknown'; 143 144 FORMAT: { 145 if (SDFileIO::IsSDFile($Name)) { $Status = 1; $Format = 'SDF'; $IOPackageName = 'SDFileIO'; last FORMAT; } 146 if (MDLMolFileIO::IsMDLMolFile($Name)) { $Status = 1; $Format = 'MDLMol'; $IOPackageName = 'MDLMolFileIO'; last FORMAT; } 147 $Status = 0; $Format = 'NotSupported'; $IOPackageName = 'Unknown'; 148 } 149 150 return wantarray ? ($Status, $Format, $IOPackageName) : $Status; 151 } 152 153 # Prohibit file ref change... 154 # 155 sub SetFileIORef { 156 my($This, $Value) = @_; 157 158 carp "Warning: ${ClassName}->SetFileIORef: Explicit setting of file ref is not supported..."; 159 160 return $This; 161 } 162 163 # Prohibit file name change... 164 # 165 sub SetName { 166 my($This, $Name) = @_; 167 168 carp "Warning: ${ClassName}->SetName: Explicit setting of file name is not supported: It must be set during object instantiation..."; 169 170 return $This; 171 } 172 173 # Prohibit file mode change... 174 # 175 sub SetMode { 176 my($This, $Mode) = @_; 177 178 carp "Warning: ${ClassName}->SetMode: Explicit setting of file mode is not supported: It must be set during object instantiation..."; 179 180 return $This; 181 } 182 183 # Open file in a specific mode; default mode is Read only. 184 # Supported mode values are: Read, Write, Append, <, >, >>, r, w, a 185 # 186 sub Open { 187 my($This, $Mode) = @_; 188 189 return $This->{FileIORef}->Open($Mode); 190 } 191 192 # close file... 193 sub Close { 194 my($This) = @_; 195 196 return $This->{FileIORef}->Close(); 197 } 198 199 # Read molecule string from file and return a molecule object... 200 sub ReadMolecule { 201 my($This) = @_; 202 203 return $This->{FileIORef}->ReadMolecule(); 204 } 205 206 # Retrieve molecule string from file... 207 sub ReadMoleculeString { 208 my($This) = @_; 209 210 return $This->{FileIORef}->ReadMoleculeString(); 211 } 212 213 # Write molecule using molecule object... 214 sub WriteMolecule { 215 my($This, $Molecule) = @_; 216 217 return $This->{FileIORef}->WriteMolecule($Molecule); 218 } 219 220 # Is it a MoleculeFileIO object? 221 sub _IsMoleculeFileIO { 222 my($Object) = @_; 223 224 return (Scalar::Util::blessed($Object) && $Object->isa($ClassName)) ? 1 : 0; 225 } 226