Search in sources :

Example 1 with SMILESWriter

use of org.openscience.cdk.io.SMILESWriter in project ambit-mirror by ideaconsult.

the class FileOutputState method getWriter.

public static IChemObjectWriter getWriter(OutputStream stream, String ext) throws AmbitIOException {
    String fname = ext.toLowerCase();
    IChemObjectWriter writer = null;
    try {
        if (fname.endsWith(_FILE_TYPE.SDF_INDEX.getExtension())) {
            writer = new SDFWriter(stream);
        } else if (fname.endsWith(_FILE_TYPE.CSV_INDEX.getExtension()))
            writer = new DelimitedFileWriter(stream);
        else if ((fname.endsWith(_FILE_TYPE.TXT_INDEX.getExtension())))
            writer = new DelimitedFileWriter(stream, new DelimitedFileFormat("\t", '"'));
        else if ((fname.endsWith(_FILE_TYPE.SMI_INDEX.getExtension())))
            writer = new SMILESWriter(stream);
        else /*
			else if ((fname.endsWith(extensions[HTML_INDEX]))) 
				writer = new HTMLTableWriter(stream);
			else if ((fname.endsWith(extensions[JPG_INDEX]))) 			
				writer = new ImageWriter(stream);
			else if ((fname.endsWith(extensions[PNG_INDEX])))  
				writer = new ImageWriter(stream);
				*/
        if ((fname.endsWith(_FILE_TYPE.PDF_INDEX.getExtension()))) {
            try {
                writer = createWriterByReflection("ambit2.core.io.PDFWriter", stream);
            } catch (Exception x) {
                throw new AmbitIOException(x);
            }
        } else if ((fname.endsWith(_FILE_TYPE.XYZ_INDEX.getExtension())))
            writer = new XYZWriter(stream);
        else if ((fname.endsWith(_FILE_TYPE.HIN_INDEX.getExtension())))
            writer = new HINWriter(stream);
        else if ((fname.endsWith(_FILE_TYPE.MOL_INDEX.getExtension())))
            writer = new SDFWriter(stream);
        else if ((fname.endsWith(_FILE_TYPE.VW_INDEX.getExtension()))) {
            writer = new DelimitedFileWriter(stream, new DelimitedFileFormat("|", '"'));
            ((DelimitedFileWriter) writer).setAddSMILEScolumn(false);
        } else /*
			else if ((fname.endsWith(extensions[SVG_INDEX]))) 
				writer = new SVGWriter(stream);
				*/
        if ((fname.endsWith(_FILE_TYPE.XLS_INDEX.getExtension())))
            try {
                writer = createXLSXWriterByReflection("ambit2.core.io.XLSFileWriter", stream, true);
            } catch (Exception x) {
                throw new AmbitIOException(x);
            }
        else if ((fname.endsWith(_FILE_TYPE.XLSX_INDEX.getExtension())))
            try {
                writer = createXLSXWriterByReflection("ambit2.core.io.XLSFileWriter", stream, false);
            } catch (Exception x) {
                throw new AmbitIOException(x);
            }
        else
            throw new AmbitIOException(MSG_UNSUPPORTEDFORMAT + ext);
    } catch (Exception x) {
        // logger.error(MSG_ERRORSAVE,filename);
        throw new AmbitIOException(MSG_ERRORSAVE, x);
    }
    return writer;
}
Also used : XYZWriter(org.openscience.cdk.io.XYZWriter) AmbitIOException(ambit2.base.exceptions.AmbitIOException) SMILESWriter(org.openscience.cdk.io.SMILESWriter) HINWriter(org.openscience.cdk.io.HINWriter) SDFWriter(org.openscience.cdk.io.SDFWriter) IChemObjectWriter(org.openscience.cdk.io.IChemObjectWriter) AmbitIOException(ambit2.base.exceptions.AmbitIOException)

Example 2 with SMILESWriter

use of org.openscience.cdk.io.SMILESWriter in project ambit-mirror by ideaconsult.

the class AutomaticTautomerTests method processNCILine.

int processNCILine(String line) {
    // System.out.println(line);
    List<String> tokens = filterTokens(line.split(" "));
    if (tokens.size() < 2) {
        System.out.println("Line " + curLine + "  has less than 2 tokens.");
        return (-1);
    }
    if (tokens.size() > 2) {
        System.out.println("Line " + curLine + "  has more than 2 tokens.");
    }
    try {
        // Create molecule from SMILES
        IAtomContainer mol = null;
        SmilesParser sp = new SmilesParser(SilentChemObjectBuilder.getInstance());
        mol = sp.parseSmiles(tokens.get(1));
        // Generate new SMILES
        java.io.StringWriter result = new java.io.StringWriter();
        SMILESWriter writer = new SMILESWriter(result);
        writer.write(mol);
        writer.close();
        // System.out.println(tokens.get(1) + "  -->  " + result.toString());
        String smiles = result.toString();
        int dot_index = smiles.indexOf(".");
        if (dot_index == -1)
            output(smiles);
        else
            output(smiles.substring(0, dot_index));
    } catch (Exception e) {
    }
    return (0);
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) SMILESWriter(org.openscience.cdk.io.SMILESWriter) EmptyMoleculeException(ambit2.base.exceptions.EmptyMoleculeException)

Example 3 with SMILESWriter

use of org.openscience.cdk.io.SMILESWriter in project ambit-mirror by ideaconsult.

the class VegaShell method transform_input.

@Override
protected synchronized IAtomContainer transform_input(IAtomContainer mol) throws ShellException {
    final String msg = "Empty molecule after %s processing";
    if ((mol == null) || (mol.getAtomCount() == 0))
        throw new ShellException(this, "Empty molecule");
    String homeDir = getHomeDir(null);
    File dir = new File(homeDir);
    if (!dir.exists()) {
        logger.log(Level.INFO, String.format("%s do not exist, mkdir", dir.getAbsolutePath()));
        dir.mkdirs();
    }
    String molfile = String.format("%s%s%s", homeDir, File.separator, inFile[1]);
    String predfile = String.format("%s%s%s", homeDir, File.separator, outFile[0]);
    try (SMILESWriter writer = new SMILESWriter(new FileOutputStream(molfile))) {
        writer.write(mol);
    } catch (Exception x) {
        throw new ShellException(this, x);
    }
    // reading the script from resource
    String script = null;
    try (InputStream in = VegaShell.class.getClassLoader().getResourceAsStream("ambit2/vega/script.xml")) {
        script = String.format(IOUtils.toString(in), molfile, predfile);
    } catch (Exception x) {
        throw new ShellException(this, x);
    }
    // writing the script with proper file name
    try (FileWriter writer = new FileWriter(new File(String.format("%s/%s", homeDir, inFile[0])))) {
        writer.write(script);
    } catch (Exception x) {
        throw new ShellException(this, String.format("Can't write Vega script! %s", x.getMessage()));
    }
    for (int i = 0; i < outFile.length; i++) {
        File file = new File(homeDir + "/" + outFile[i]);
        if (file.exists())
            file.delete();
    }
    return mol;
}
Also used : SMILESWriter(org.openscience.cdk.io.SMILESWriter) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) ShellException(ambit2.base.external.ShellException) File(java.io.File) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) CDKException(org.openscience.cdk.exception.CDKException) ShellException(ambit2.base.external.ShellException)

Aggregations

SMILESWriter (org.openscience.cdk.io.SMILESWriter)3 AmbitIOException (ambit2.base.exceptions.AmbitIOException)1 EmptyMoleculeException (ambit2.base.exceptions.EmptyMoleculeException)1 ShellException (ambit2.base.external.ShellException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)1 CDKException (org.openscience.cdk.exception.CDKException)1 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)1 HINWriter (org.openscience.cdk.io.HINWriter)1 IChemObjectWriter (org.openscience.cdk.io.IChemObjectWriter)1 SDFWriter (org.openscience.cdk.io.SDFWriter)1 XYZWriter (org.openscience.cdk.io.XYZWriter)1 SmilesParser (org.openscience.cdk.smiles.SmilesParser)1