MolImportUtil.java |
1 package util; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import chemaxon.formats.MolExporter; 8 import chemaxon.formats.MolFormatException; 9 import chemaxon.formats.MolImporter; 10import chemaxon.jchem.db.Importer; 11import chemaxon.jchem.db.TransferException; 12import chemaxon.jchem.db.UpdateHandler; 13import chemaxon.struc.Molecule; 14import chemaxon.util.ConnectionHandler; 15import chemaxon.util.MolHandler; 16 17/** 18 * Example codes for importing molecules from molecule files or strings into {@link Molecule} 19 * objects or database tables. 20 * <p> 21 * When structures are stored in a file, we can import them into Molecule objects using 22 * {@link MolImporter} (e.g. see importMol(), method) or into database using {@link Importer} 23 * (see databaseImport() method). 24 * <p> 25 * If we know only string representation of a structure (SMILES, MDL Molfile, ...), we can 26 * import it into a Molecule object using {@link MolImporter} (see importMolFromString() 27 * method). importMolFromStringAsQuery() method is useful to import molecules from SMARTS string 28 * representation (instead of SMILES). 29 * 30 * @author JChem Base team, ChemAxon Ltd. 31 */ 32public final class MolImportUtil { 33 34 /*** 35 * Imports a molecule from a file given by full path. 36 * 37 * @param fullPath full path name of file 38 * @return first molecule stored in the given file 39 * @throws IllegalArgumentException if an error occurs during import 40 */ 41 public static Molecule importMol(String fullPath) { 42 try { 43 MolImporter mi = new MolImporter(fullPath); 44 try { 45 return mi.read(); 46 } finally { 47 mi.close(); 48 } 49 } catch (MolFormatException e) { 50 throw new IllegalArgumentException("Invalid molecule format", e); 51 } catch (IOException e) { 52 throw new IllegalArgumentException("Error reading input file", e); 53 } 54 } 55 56 /** 57 * Imports a molecule given by its SMARTS representation. 58 * 59 * @param molString molecule string (SMARTS) 60 * @return the molecule object 61 * @throws IllegalArgumentException if an error occurs during import 62 */ 63 public static Molecule importMolFromStringAsQuery(String molString) { 64 try { 65 // If queryMode (second parameter) is set true, the input will be interpreted 66 // as SMARTS. If SMILES import is needed, set queryMode to false (default). 67 MolHandler mh = new MolHandler(molString, true); 68 return mh.getMolecule(); 69 } catch (MolFormatException e) { 70 throw new IllegalArgumentException("Invalid molecule format", e); 71 } 72 } 73 74 /** 75 * Imports the content of an input molecule file to a specified structure table in a 76 * database. 77 * 78 * @param inputFile full path of input file 79 * @param connHandler open connection handler 80 * @param tableName structure table name 81 * @throws TransferException if an error occurs during import 82 */ 83 public static void databaseImport(String inputFile, ConnectionHandler connHandler, 84 String tableName) throws TransferException { 85 86 Importer importer = new Importer(); 87 88 importer.setInput(inputFile); 89 importer.setConnectionHandler(connHandler); 90 importer.setTableName(tableName); 92 importer.setHaltOnError(false); 93 // Checking duplicates may slow down import! 94 importer.setDuplicateImportAllowed(UpdateHandler.DUPLICATE_FILTERING_OFF); 95 96 // Gather information about file 97 importer.init(); 98 99 // Import molecules into database table 00 importer.importMols(); 01 } 02 03 /** 04 * Imports a single input molecule object to a specified structure table in a database. 05 * 06 * @param mol the molecule to import 07 * @param connHandler open connection handler 08 * @param tableName structure table name 09 * @throws Exception if an error occurs during import 10 */ 11 public static void databaseImportFromMolObject(Molecule mol, ConnectionHandler connHandler, 12 String tableName) throws Exception { 13 14 UpdateHandler uh = 15 new UpdateHandler(connHandler, UpdateHandler.INSERT, tableName, null); 16 try { 17 // The molecule has to be converted to one of the available formats 18 uh.setStructure(MolExporter.exportToFormat(mol, "mrv")); 19 uh.execute(); 20 } finally { 21 uh.close(); 22 } 23 24 } 25 26 /** 27 * Imports the content of an input molecule file to a list of Molecule objects. 28 * 29 * @param inputFile full path of input file 30 * @throws IOException if I/O error occurs during import 31 * @throws MolFormatException if the input file contains erroneous structures 32 */ 33 public static List<Molecule> moleculeListImport(String inputFile) 34 throws MolFormatException, IOException { 35 36 List<Molecule> molList = new ArrayList<Molecule>(); 37 MolImporter imp = new MolImporter(inputFile); 38 try { 39 Molecule mol; 40 while ((mol = imp.read()) != null) { 41 molList.add(mol); 42 } 43 } finally { 44 imp.close(); 45 } 46 47 return molList; 48 } 49 50} 51