Search in sources :

Example 11 with AtomContainer

use of org.openscience.cdk.AtomContainer in project cdk by cdk.

the class SybylAtomTypeMatcherTest method testCroh.

@Test
public void testCroh() throws Exception {
    IAtomContainer mol = new AtomContainer();
    // this is made up, and may be wrong; info on the web is sparse, and PubChem has no
    // octa-coordinate structure; lone pairs involved?
    IAtom a1 = new Atom("Cr");
    mol.addAtom(a1);
    for (int i = 0; i < 6; i++) {
        IAtom atom = new Atom("O");
        mol.addAtom(atom);
        mol.addBond(new Bond(a1, atom, IBond.Order.SINGLE));
    }
    String[] expectedTypes = { "Cr.oh", "O.3", "O.3", "O.3", "O.3", "O.3", "O.3" };
    assertAtomTypes(testedAtomTypes, expectedTypes, mol);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) Bond(org.openscience.cdk.Bond) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) PseudoAtom(org.openscience.cdk.PseudoAtom) Atom(org.openscience.cdk.Atom) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 12 with AtomContainer

use of org.openscience.cdk.AtomContainer in project cdk by cdk.

the class SybylAtomTypeMatcherTest method testDMSOO.

@Test
public void testDMSOO() throws Exception {
    IAtomContainer mol = new AtomContainer();
    IAtom atom = new Atom("O");
    IAtom atom1 = new Atom("O");
    IAtom atom2 = new Atom("S");
    IAtom atom3 = new Atom("C");
    IAtom atom4 = new Atom("C");
    mol.addAtom(atom);
    mol.addAtom(atom1);
    mol.addAtom(atom2);
    mol.addAtom(atom3);
    mol.addAtom(atom4);
    mol.addBond(0, 2, Order.DOUBLE);
    mol.addBond(1, 2, Order.DOUBLE);
    mol.addBond(2, 3, Order.SINGLE);
    mol.addBond(2, 4, Order.SINGLE);
    String[] expectedTypes = { "O.2", "O.2", "S.O2", "C.3", "C.3" };
    assertAtomTypes(testedAtomTypes, expectedTypes, mol);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtom(org.openscience.cdk.interfaces.IAtom) PseudoAtom(org.openscience.cdk.PseudoAtom) Atom(org.openscience.cdk.Atom) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 13 with AtomContainer

use of org.openscience.cdk.AtomContainer in project cdk by cdk.

the class SybylAtomTypeMatcherTest method testMethylAmine.

@Test
public void testMethylAmine() throws Exception {
    IAtomContainer mol = new AtomContainer();
    IAtom atom = new Atom("N");
    IAtom atom2 = new Atom("C");
    mol.addAtom(atom);
    mol.addAtom(atom2);
    mol.addBond(0, 1, Order.SINGLE);
    String[] expectedTypes = { "N.3", "C.3" };
    assertAtomTypes(testedAtomTypes, expectedTypes, mol);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtom(org.openscience.cdk.interfaces.IAtom) PseudoAtom(org.openscience.cdk.PseudoAtom) Atom(org.openscience.cdk.Atom) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 14 with AtomContainer

use of org.openscience.cdk.AtomContainer in project cdk by cdk.

the class SybylAtomTypeMatcherTest method testFindMatchingAtomType_IAtomContainer.

@Test
public void testFindMatchingAtomType_IAtomContainer() throws Exception {
    String filename = "atomtyping.mol2";
    InputStream ins = this.getClass().getResourceAsStream(filename);
    Mol2Reader reader = new Mol2Reader(ins);
    IAtomContainer mol = reader.read(new AtomContainer());
    // just check consistency; other methods do perception testing
    SybylAtomTypeMatcher matcher = SybylAtomTypeMatcher.getInstance(DefaultChemObjectBuilder.getInstance());
    IAtomType[] types = matcher.findMatchingAtomTypes(mol);
    for (int i = 0; i < types.length; i++) {
        IAtomType type = matcher.findMatchingAtomType(mol, mol.getAtom(i));
        Assert.assertEquals(type.getAtomTypeName(), types[i].getAtomTypeName());
    }
}
Also used : IAtomType(org.openscience.cdk.interfaces.IAtomType) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) InputStream(java.io.InputStream) Mol2Reader(org.openscience.cdk.io.Mol2Reader) Test(org.junit.Test)

Example 15 with AtomContainer

use of org.openscience.cdk.AtomContainer in project MORTAR by FelixBaensch.

the class Importer method importSMILESFile.

// </editor-fold>
// 
// <editor-fold desc="protected methods" defaultstate="collapsed">
/**
 * Imports a SMILES file. This method is able to parse different types of SMILES files, e.g. with and without header
 * or with only one column or two (SMILES and name/ID, which is in which column is detected).
 * Protected and not private for testing in class ImporterTest.
 *
 * @param aFile a SMILES codes-containing *.txt or *.smi file
 * @return the imported molecules in an IAtomContainerSet
 * @throws IOException if the given file does not fit to the expected format of a SMILES file
 * @author Samuel Behr
 */
protected IAtomContainerSet importSMILESFile(File aFile) throws IOException {
    try (FileReader tmpSmilesFileReader = new FileReader(aFile);
        BufferedReader tmpSmilesFileBufferedReader = new BufferedReader(tmpSmilesFileReader, BasicDefinitions.BUFFER_SIZE)) {
        IAtomContainerSet tmpAtomContainerSet = new AtomContainerSet();
        // AtomContainer to save the parsed SMILES in
        IAtomContainer tmpMolecule = new AtomContainer();
        SmilesParser tmpSmilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
        String tmpSmilesFileNextLine = "";
        String tmpSmilesFileDeterminedSeparator = "";
        String[] tmpProcessedLineArray;
        int tmpSmilesCodeExpectedPosition = 0;
        int tmpIDExpectedPosition = 0;
        int tmpSmilesFileParsableLinesCounter = 0;
        int tmpSmilesFileInvalidLinesCounter = 0;
        // marking the BufferedReader to reset the reader after checking the format and determining the separator
        tmpSmilesFileBufferedReader.mark(BasicDefinitions.BUFFER_SIZE);
        // as potential headline the first line should be avoided for separator determination
        String tmpSmilesFileFirstLine = tmpSmilesFileBufferedReader.readLine();
        /*  first block
                Checking for parsable SMILES code and saving the determined separator (if one is used).
                If no parsable SMILES code is found in the second and third line of the file, tmpMolecule stays empty
                and the file is assumed to be no SMILES file -> return null
             */
        int tmpFilesLine = 2;
        findSeparatorLoop: while (!Thread.currentThread().isInterrupted() && tmpFilesLine <= 3) {
            if ((tmpSmilesFileNextLine = tmpSmilesFileBufferedReader.readLine()) == null) {
                // if the file's end is reached at this point, the first line is used to determine the separator
                if (tmpSmilesFileFirstLine != null || !tmpSmilesFileFirstLine.isEmpty()) {
                    tmpSmilesFileNextLine = tmpSmilesFileFirstLine;
                    tmpSmilesFileFirstLine = null;
                } else {
                    break;
                }
            }
            for (String tmpSeparator : BasicDefinitions.POSSIBLE_SMILES_FILE_SEPARATORS) {
                // maximum of two array elements expected, otherwise the separator or the line itself are assumed to be invalid
                tmpProcessedLineArray = tmpSmilesFileNextLine.split(tmpSeparator, 3);
                if (tmpProcessedLineArray.length > 2) {
                    continue;
                }
                int tmpIndex = 0;
                for (String tmpNextElementOfLine : tmpProcessedLineArray) {
                    if (tmpNextElementOfLine.isEmpty()) {
                        continue;
                    }
                    try {
                        tmpMolecule = tmpSmilesParser.parseSmiles(tmpNextElementOfLine);
                        if (!tmpMolecule.isEmpty()) {
                            tmpSmilesFileDeterminedSeparator = tmpSeparator;
                            tmpSmilesCodeExpectedPosition = tmpIndex;
                            if (tmpProcessedLineArray.length > 1) {
                                if (tmpSmilesCodeExpectedPosition == 0) {
                                    tmpIDExpectedPosition = 1;
                                } else {
                                    tmpIDExpectedPosition = 0;
                                }
                            }
                            break findSeparatorLoop;
                        }
                    } catch (InvalidSmilesException anException) {
                        tmpIndex++;
                    }
                }
            }
            tmpFilesLine++;
        }
        if (tmpMolecule.isEmpty()) {
            throw new IOException("Chosen file does not fit to the expected format of a SMILES file.");
        }
        // resetting the BufferedReader to the first line of the file
        tmpSmilesFileBufferedReader.reset();
        // to avoid the memory of unnecessary data
        tmpSmilesFileBufferedReader.mark(0);
        /*  second block
                Reading the file line by line and adding an AtomContainer to the AtomContainerSet for each line with parsable SMILES code
             */
        while (!Thread.currentThread().isInterrupted() && (tmpSmilesFileNextLine = tmpSmilesFileBufferedReader.readLine()) != null) {
            // trying to parse as SMILES code
            try {
                tmpProcessedLineArray = tmpSmilesFileNextLine.split(tmpSmilesFileDeterminedSeparator, 2);
                if (!tmpProcessedLineArray[tmpSmilesCodeExpectedPosition].isEmpty()) {
                    tmpMolecule = tmpSmilesParser.parseSmiles(tmpProcessedLineArray[tmpSmilesCodeExpectedPosition]);
                    tmpSmilesFileParsableLinesCounter++;
                } else {
                    tmpSmilesFileInvalidLinesCounter++;
                    continue;
                }
            } catch (InvalidSmilesException | IndexOutOfBoundsException anException) {
                // case: invalid line or SMILES code
                tmpSmilesFileInvalidLinesCounter++;
                continue;
            }
            // setting the name of the atom container
            String tmpName = "";
            if (tmpProcessedLineArray.length > 1 && !tmpProcessedLineArray[tmpIDExpectedPosition].isEmpty()) {
                tmpName = tmpProcessedLineArray[tmpIDExpectedPosition];
            } else {
                tmpName = FileUtil.getFileNameWithoutExtension(aFile) + tmpSmilesFileParsableLinesCounter;
            }
            tmpMolecule.setProperty(Importer.MOLECULE_NAME_PROPERTY_KEY, tmpName);
            // adding tmpMolecule to the AtomContainerSet
            tmpAtomContainerSet.addAtomContainer(tmpMolecule);
        }
        Importer.LOGGER.log(Level.INFO, "\tSmilesFile ParsableLinesCounter:\t" + tmpSmilesFileParsableLinesCounter + "\n\tSmilesFile InvalidLinesCounter:\t\t" + tmpSmilesFileInvalidLinesCounter);
        return tmpAtomContainerSet;
    }
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IOException(java.io.IOException) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException)

Aggregations

AtomContainer (org.openscience.cdk.AtomContainer)743 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)730 Test (org.junit.Test)641 IAtom (org.openscience.cdk.interfaces.IAtom)556 Atom (org.openscience.cdk.Atom)523 PseudoAtom (org.openscience.cdk.PseudoAtom)206 IBond (org.openscience.cdk.interfaces.IBond)136 Bond (org.openscience.cdk.Bond)90 Point2d (javax.vecmath.Point2d)86 InputStream (java.io.InputStream)80 IQueryAtomContainer (org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer)61 QueryAtomContainer (org.openscience.cdk.isomorphism.matchers.QueryAtomContainer)57 MDLV2000Reader (org.openscience.cdk.io.MDLV2000Reader)44 SimpleChemObjectReaderTest (org.openscience.cdk.test.io.SimpleChemObjectReaderTest)43 ByteArrayInputStream (java.io.ByteArrayInputStream)41 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)32 SlowTest (org.openscience.cdk.test.SlowTest)32 Point3d (javax.vecmath.Point3d)30 IPseudoAtom (org.openscience.cdk.interfaces.IPseudoAtom)29 DoubleArrayResult (org.openscience.cdk.qsar.result.DoubleArrayResult)23