/* * EuroCarbDB, a framework for carbohydrate bioinformatics * * Copyright (c) 2006-2009, Eurocarb project, or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * A copy of this license accompanies this distribution in the file LICENSE.txt. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * Last commit: $Rev: 1210 $ by $Author: glycoslave $ on $Date:: 2009-06-12 #$ */ package org.eurocarbdb.resourcesdb.io; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import org.eurocarbdb.resourcesdb.ResourcesDbException; import org.eurocarbdb.resourcesdb.atom.*; import org.eurocarbdb.resourcesdb.monosaccharide.Monosaccharide; import org.eurocarbdb.resourcesdb.util.NumberUtils; public class Mol2Exporter { private static void writeHeader(Monosaccharide ms, PrintStream printer) throws IOException { printer.println("# Monosaccharide " + ms.getName()); printer.println("# generated by MonosaccharideDB"); printer.println(); printer.println("@<TRIPOS>MOLECULE"); printer.println(ms.getName()); printer.println(ms.getAtoms().size()); printer.println("SACCHARIDE"); printer.println("NO_CHARGES"); printer.println(); printer.println(); } private static void writeAtoms(Monosaccharide ms, PrintStream printer) throws IOException { printer.println("@<TRIPOS>ATOM"); int atomIndex = 0; for(Atom a : ms.getAtoms()) { a.setId(++atomIndex); String atomLine = a.getId() + " " + a.getName(); atomLine += " " + NumberUtils.nullsaveDoubleValue(a.getX()) + " " + NumberUtils.nullsaveDoubleValue(a.getY()) + " " +NumberUtils.nullsaveDoubleValue( a.getZ()); atomLine += " " + a.getMol2Type(); printer.println(atomLine); } } private static void writeBonds(Monosaccharide ms, PrintStream printer) throws IOException { printer.println("@<TRIPOS>BOND"); for(Atom a : ms.getAtoms()) { int bondIndex = 0; for(AtomConnection aCon : a.getConnections()) { String bondLine; int toId = aCon.getToAtom().getId(); if(toId == a.getId()) { toId = aCon.getFromAtom().getId(); } if(a.getId() < toId) { bondIndex ++; String boStr; if(aCon.getBondOrder() == 1) { boStr = "1"; } else if(aCon.getBondOrder() == 1.5) { boStr = "ar"; } else if(aCon.getBondOrder() == 2) { boStr = "2"; } else if(aCon.getBondOrder() == 3) { boStr = "3"; } else { boStr = "un"; } bondLine = bondIndex + " " + a.getId() + " " + toId + " " + boStr; printer.println(bondLine); } } } } public static void export(Monosaccharide ms, FileOutputStream fOut) throws ResourcesDbException { if(fOut == null) { throw new ResourcesDbException("File output stream is null in mol2exporter."); } if(ms == null) { throw new ResourcesDbException("Monosaccharide is null in mol2exporter."); } try { PrintStream printer = new PrintStream(fOut); writeHeader(ms, printer); writeAtoms(ms, printer); writeBonds(ms, printer); } catch(IOException ioEx) { ResourcesDbException rEx = new ResourcesDbException("IO exception in mol2exporter"); rEx.initCause(ioEx); throw rEx; } } public static void export(Monosaccharide ms, String fileName) throws ResourcesDbException { FileOutputStream fOut; try { fOut = new FileOutputStream(fileName); } catch(Exception ex) { ResourcesDbException rEx = new ResourcesDbException("Cannot initialize FileOutputStream for file name '" + fileName + "'"); rEx.initCause(ex); throw rEx; } Mol2Exporter.export(ms, fOut); try { fOut.close(); } catch(Exception ex) { ResourcesDbException rEx = new ResourcesDbException("Cannot close FileOutputStream for file name '" + fileName + "'"); rEx.initCause(ex); throw rEx; } } }