Search in sources :

Example 1 with StructureStandardizer

use of ambit2.tautomers.processor.StructureStandardizer in project ambit-mirror by ideaconsult.

the class DepictQueryStandardize method parseQuery.

@Override
public void parseQuery(Form form) {
    super.parseQuery(form);
    if (standardizer == null) {
        standardizer = new StructureStandardizer();
        standardizer.setImplicitHydrogens(true);
        standardizer.setClearIsotopes(true);
        standardizer.setGenerateSMILES(false);
        standardizer.setGenerateInChI(true);
    }
    try {
        String ok = form.getFirstValue("neutralise");
        standardizer.setNeutralise("ON".equals(ok.toUpperCase()) || Boolean.parseBoolean(ok));
    } catch (Exception x) {
        standardizer.setNeutralise(false);
    }
    try {
        String ok = form.getFirstValue("tautomer");
        standardizer.setGenerateTautomers("ON".equals(ok.toUpperCase()) || Boolean.parseBoolean(ok));
    } catch (Exception x) {
        standardizer.setGenerateTautomers(false);
    }
    try {
        String ok = form.getFirstValue("fragment");
        standardizer.setSplitFragments("ON".equals(ok.toUpperCase()) || Boolean.parseBoolean(ok));
    } catch (Exception x) {
        standardizer.setSplitFragments(false);
    }
}
Also used : StructureStandardizer(ambit2.tautomers.processor.StructureStandardizer)

Example 2 with StructureStandardizer

use of ambit2.tautomers.processor.StructureStandardizer in project ambit-mirror by ideaconsult.

the class AmbitPipeline method parseCommandStandardize.

public void parseCommandStandardize(String subcommand, long now) throws Exception {
    int page = parsePageParam();
    int pagesize = parsePageSizeParam();
    Object tmpTag = parseSdfTitleParam();
    String smiles_header = parseInputTag_Param("smiles", IteratingDelimitedFileReader.defaultSMILESHeader);
    String inchi_header = parseInputTag_Param("inchi", "InChI");
    String inchikey_header = parseInputTag_Param("inchikey", "InChIKey");
    final String sdf_title = tmpTag == null ? null : tmpTag.toString().toLowerCase();
    final StructureStandardizer standardprocessor = new StructureStandardizer(logger_cli);
    standardprocessor.setGenerate2D(parseBooleanParam(":generate2D", false));
    standardprocessor.setGenerateTautomers(parseBooleanParam(":tautomers", false));
    SMIRKSProcessor tmp = null;
    try {
        Object o = options.getParam(":smirks");
        if (o != null) {
            File smirksConfig = new File(o.toString());
            if (smirksConfig.exists()) {
                tmp = new SMIRKSProcessor(smirksConfig, logger_cli);
                tmp.setEnabled(true);
            } else
                logger_cli.log(Level.WARNING, "SMIRKS transformation file not found");
        }
    } catch (Exception x) {
        logger_cli.log(Level.SEVERE, x.getMessage());
        tmp = null;
    }
    final SMIRKSProcessor smirksProcessor = tmp;
    standardprocessor.setSplitFragments(parseBooleanParam(":splitfragments", false));
    standardprocessor.setImplicitHydrogens(parseBooleanParam(":implicith", false));
    standardprocessor.setNeutralise(parseBooleanParam(":neutralise", false));
    final String[] tags_to_keep = parsetags_to_keep();
    standardprocessor.setRankTag(parseStringParam(":tag_rank", "RANK"));
    standardprocessor.setInchiTag(parseStringParam(":tag_inchi", "InChI"));
    standardprocessor.setInchiKeyTag(parseStringParam(":tag_inchikey", "InChIKey"));
    standardprocessor.setSMILESTag(parseStringParam(":tag_smiles", "SMILES"));
    standardprocessor.setGenerateInChI(parseBooleanParam(":inchi", true));
    standardprocessor.setGenerateSMILES(parseBooleanParam(":smiles", true));
    standardprocessor.setGenerateSMILES_Canonical(parseBooleanParam(":smilescanonical", false));
    standardprocessor.setGenerateSMILES_Aromatic(parseBooleanParam(":smilesaromatic", false));
    standardprocessor.setGenerateStereofrom2D(parseBooleanParam(":generatestereofrom2d", false));
    standardprocessor.setClearIsotopes(parseBooleanParam(":setClearIsotopes", false));
    final boolean debugatomtypes = parseBooleanParam(":debugatomtypes", false);
    final int startRecord = pagesize > 0 ? (page * pagesize + 1) : 1;
    final int maxRecord = pagesize > 0 ? ((page + 1) * pagesize + 1) : pagesize;
    final File file = getInputFile();
    FileInputState in = new FileInputState(file);
    in.setOptionalInChIHeader(inchi_header);
    in.setOptionalInChIKeyHeader(inchikey_header);
    in.setOptionalSMILESHeader(smiles_header);
    if (options.output == null)
        throw new FileNotFoundException("Output file not specified. Please use -o {file}");
    final File outfile = new File(options.output);
    logger_cli.log(Level.INFO, "MSG_INFO_READINGWRITING", new Object[] { file.getAbsoluteFile(), outfile.getAbsolutePath() });
    FileOutputState out = new FileOutputState(outfile);
    final IChemObjectWriter writer = out.getWriter();
    if (writer instanceof FilesWithHeaderWriter)
        ((FilesWithHeaderWriter) writer).setAddSMILEScolumn(false);
    final BatchDBProcessor<IStructureRecord> batch = new BatchDBProcessor<IStructureRecord>() {

        @Override
        public void onItemRead(IStructureRecord input, IBatchStatistics stats) {
            super.onItemRead(input, stats);
            if ((maxRecord > 0) && stats.getRecords(RECORDS_STATS.RECORDS_READ) >= (maxRecord))
                cancel();
        }

        @Override
        public boolean skip(IStructureRecord input, IBatchStatistics stats) {
            return (stats.getRecords(RECORDS_STATS.RECORDS_READ) < startRecord) || ((maxRecord > 0) && (stats.getRecords(RECORDS_STATS.RECORDS_READ) >= maxRecord));
        }

        @Override
        public void onItemSkipped(IStructureRecord input, IBatchStatistics stats) {
            super.onItemSkipped(input, stats);
            if (stats.isTimeToPrint(getSilentInterval() * 2))
                propertyChangeSupport.firePropertyChange(PROPERTY_BATCHSTATS, null, stats);
        }

        @Override
        public void onItemProcessing(IStructureRecord input, Object output, IBatchStatistics stats) {
        }

        @Override
        public void onError(IStructureRecord input, Object output, IBatchStatistics stats, Exception x) {
            super.onError(input, output, stats, x);
            logger_cli.log(Level.SEVERE, x.getMessage());
        }

        @Override
        public long getSilentInterval() {
            return 30000L;
        }

        @Override
        public void close() throws Exception {
            try {
                writer.close();
            } catch (Exception x) {
            } finally {
            }
            super.close();
        }
    };
    batch.setProcessorChain(new ProcessorsChain<IStructureRecord, IBatchStatistics, IProcessor>());
    batch.getProcessorChain().add(new StdzBatchProcessor(standardprocessor, smirksProcessor, tags_to_keep, logger_cli, writer, sdf_title, debugatomtypes));
    batch.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (AbstractBatchProcessor.PROPERTY_BATCHSTATS.equals(evt.getPropertyName()))
                logger_cli.log(Level.INFO, evt.getNewValue().toString());
        }
    });
    /*
		 * standardprocessor.setCallback(new
		 * DefaultAmbitProcessor<IAtomContainer, IAtomContainer>() {
		 * 
		 * @Override public IAtomContainer process(IAtomContainer target) throws
		 * Exception { try { //writer.write(target); } catch (Exception x) {
		 * logger.log(Level.SEVERE, x.getMessage()); } return target; } });
		 */
    IBatchStatistics stats = null;
    try {
        stats = batch.process(in);
    } catch (Exception x) {
        StringWriter w = new StringWriter();
        x.printStackTrace(new PrintWriter(w));
        logger_cli.log(Level.WARNING, "MSG_ERR", new Object[] { x.getMessage() });
        logger_cli.log(Level.FINE, "MSG_ERR_DEBUG", new Object[] { x.getMessage(), w.toString() });
    } finally {
        try {
            if (batch != null)
                batch.close();
        } catch (Exception x) {
            logger_cli.log(Level.WARNING, "MSG_ERR", new Object[] { x.getMessage() });
        }
        if (stats != null)
            logger_cli.log(Level.INFO, "MSG_INFO", new Object[] { stats.toString() });
    }
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) FileNotFoundException(java.io.FileNotFoundException) IChemObjectWriter(org.openscience.cdk.io.IChemObjectWriter) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) PropertyChangeEvent(java.beans.PropertyChangeEvent) FileOutputState(ambit2.core.io.FileOutputState) IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) IProcessor(net.idea.modbcum.i.processors.IProcessor) FileNotFoundException(java.io.FileNotFoundException) BatchDBProcessor(ambit2.db.processors.BatchDBProcessor) StructureStandardizer(ambit2.tautomers.processor.StructureStandardizer) StdzBatchProcessor(ambit2.dbcli.processor.StdzBatchProcessor) FilesWithHeaderWriter(ambit2.core.io.FilesWithHeaderWriter) SMIRKSProcessor(ambit2.smarts.processors.SMIRKSProcessor) File(java.io.File) FileInputState(ambit2.core.io.FileInputState)

Example 3 with StructureStandardizer

use of ambit2.tautomers.processor.StructureStandardizer in project ambit-mirror by ideaconsult.

the class ParserTest method testStereo.

public void testStereo(SmilesGenerator g) throws Exception {
    SmilesParser p = new SmilesParser(SilentChemObjectBuilder.getInstance());
    InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
    IIteratingChemObjectReader<IAtomContainer> reader = null;
    int error = 0;
    try {
        InputStream in = ParserTest.class.getClassLoader().getResourceAsStream("ambit2/dbcli/test/stereo.sdf");
        Assert.assertNotNull(in);
        reader = new InteractiveIteratingMDLReader(in, SilentChemObjectBuilder.getInstance());
        Assert.assertNotNull(reader);
        StructureStandardizer standardizer = new StructureStandardizer();
        standardizer.setGenerateSMILES(true);
        standardizer.setGenerateSMILES_Canonical(false);
        standardizer.setNeutralise(true);
        standardizer.setSplitFragments(true);
        standardizer.setGenerateTautomers(false);
        standardizer.setImplicitHydrogens(true);
        standardizer.setClearIsotopes(true);
        while (reader.hasNext()) {
            IAtomContainer m1 = reader.next();
            AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m1);
            try {
                String smi = g.create(m1);
                IAtomContainer m2 = p.parseSmiles(smi);
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m2);
                String newsmiles = g.create(m2);
                Assert.assertEquals(smi, newsmiles);
                InChIGenerator gen1 = factory.getInChIGenerator(m1);
                InChIGenerator gen2 = factory.getInChIGenerator(m2);
                Assert.assertEquals(gen1.getInchi(), gen2.getInchi());
                Assert.assertEquals(gen1.getInchiKey(), gen2.getInchiKey());
                Assert.assertEquals(g.create(m1), g.create(m2));
                System.out.println(smi);
                printstereo(m1);
                System.out.println(newsmiles);
                printstereo(m2);
                IAtomContainer m1_transformed = standardizer.process(m1);
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m1_transformed);
                IAtomContainer m2_transformed = standardizer.process(m2);
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m2_transformed);
                System.out.println(g.create(m1_transformed));
                printstereo(m1_transformed);
                System.out.println(g.create(m2_transformed));
                printstereo(m2_transformed);
                gen1 = factory.getInChIGenerator(m1_transformed);
                gen2 = factory.getInChIGenerator(m2_transformed);
                Assert.assertEquals(gen1.getInchi(), gen2.getInchi());
                Assert.assertEquals(gen1.getInchiKey(), gen2.getInchiKey());
            } catch (Exception x) {
                error++;
                System.out.println(x.getMessage());
            }
        }
    } finally {
        if (reader != null)
            reader.close();
    }
    Assert.assertEquals(0, error);
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) InputStream(java.io.InputStream) InteractiveIteratingMDLReader(ambit2.core.io.InteractiveIteratingMDLReader) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory) StructureStandardizer(ambit2.tautomers.processor.StructureStandardizer)

Example 4 with StructureStandardizer

use of ambit2.tautomers.processor.StructureStandardizer in project ambit-mirror by ideaconsult.

the class StandardisationTest method test_xv5_sh.

@Test
public void test_xv5_sh() throws Exception {
    try (InputStream in = getClass().getClassLoader().getResourceAsStream("ambit2/dbcli/test/xv5.sdf")) {
        // IteratingSDFReader reader = new IteratingSDFReader(in,
        // SilentChemObjectBuilder.getInstance());
        InteractiveIteratingMDLReader reader = new InteractiveIteratingMDLReader(in, SilentChemObjectBuilder.getInstance());
        StructureStandardizer z = new StructureStandardizer();
        z.setImplicitHydrogens(true);
        z.setNeutralise(false);
        z.setGenerateTautomers(false);
        z.setGenerateSMILES(true);
        z.setGenerateSMILES_Canonical(false);
        z.setGenerateSMILES_Aromatic(false);
        z.setSplitFragments(false);
        z.setClearIsotopes(true);
        z.setGenerateInChI(true);
        CDKHydrogenAdder h = CDKHydrogenAdder.getInstance(SilentChemObjectBuilder.getInstance());
        while (reader.hasNext()) {
            IAtomContainer mol = reader.next();
            AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
            h.addImplicitHydrogens(mol);
            System.out.println(SmilesGenerator.generic().create(mol));
            mol = z.process(mol);
            // mol = AtomContainerManipulator.suppressHydrogens(mol);
            // System.out.println(SmilesGenerator.generic().create(mol));
            System.out.println(mol.getProperties().get(Property.getSMILESInstance()));
        }
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InputStream(java.io.InputStream) InteractiveIteratingMDLReader(ambit2.core.io.InteractiveIteratingMDLReader) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) StructureStandardizer(ambit2.tautomers.processor.StructureStandardizer) Test(org.junit.Test)

Aggregations

StructureStandardizer (ambit2.tautomers.processor.StructureStandardizer)4 InteractiveIteratingMDLReader (ambit2.core.io.InteractiveIteratingMDLReader)2 InputStream (java.io.InputStream)2 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)2 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 FileInputState (ambit2.core.io.FileInputState)1 FileOutputState (ambit2.core.io.FileOutputState)1 FilesWithHeaderWriter (ambit2.core.io.FilesWithHeaderWriter)1 BatchDBProcessor (ambit2.db.processors.BatchDBProcessor)1 StdzBatchProcessor (ambit2.dbcli.processor.StdzBatchProcessor)1 SMIRKSProcessor (ambit2.smarts.processors.SMIRKSProcessor)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 IBatchStatistics (net.idea.modbcum.i.batch.IBatchStatistics)1 IProcessor (net.idea.modbcum.i.processors.IProcessor)1 Test (org.junit.Test)1