Search in sources :

Example 16 with AtomContainerSet

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

the class Importer method importPDBFile.

// 
/**
 * Imports a PDB file.
 *
 * @param aFile PDB file
 * @return the imported molecules in an IAtomContainerSet
 * @throws CDKException if the given PDB file cannot be read
 * @throws FileNotFoundException if a file input stream cannot be opened for the given file
 */
private IAtomContainerSet importPDBFile(File aFile) throws FileNotFoundException, CDKException {
    IAtomContainerSet tmpAtomContainerSet = new AtomContainerSet();
    PDBReader tmpPDBReader = new PDBReader(new FileInputStream(aFile));
    IAtomContainer tmpAtomContainer = tmpPDBReader.read(new AtomContainer());
    String tmpName = this.findMoleculeName(tmpAtomContainer);
    if (tmpName == null || tmpName.isBlank() || tmpName.isEmpty())
        tmpName = FileUtil.getFileNameWithoutExtension(aFile);
    tmpAtomContainer.setProperty(Importer.MOLECULE_NAME_PROPERTY_KEY, tmpName);
    tmpAtomContainerSet.addAtomContainer(tmpAtomContainer);
    return tmpAtomContainerSet;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) PDBReader(org.openscience.cdk.io.PDBReader) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) FileInputStream(java.io.FileInputStream)

Example 17 with AtomContainerSet

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

the class Importer method importMolFile.

// 
/**
 * Imports a mol file as AtomContainer and adds the first line of the mol file (name of the
 * molecule in most cases) as "name-property". MDL v2000 and v3000 MOL files are accepted and the used format
 * determined automatically.
 *
 * @param aFile mol file
 * @return the imported molecule in an IAtomContainerSet
 * @throws CDKException if the given mol file cannot be read
 * @throws IOException if the given file cannot be found or read
 */
private IAtomContainerSet importMolFile(File aFile) throws IOException, CDKException {
    IAtomContainerSet tmpAtomContainerSet = new AtomContainerSet();
    BufferedInputStream tmpInputStream = new BufferedInputStream(new FileInputStream(aFile));
    FormatFactory tmpFactory = new FormatFactory();
    IChemFormat tmpFormat = tmpFactory.guessFormat(tmpInputStream);
    IAtomContainer tmpAtomContainer;
    if (tmpFormat.getFormatName().equalsIgnoreCase(MDLV2000Format.getInstance().getFormatName())) {
        MDLV2000Reader tmpReader = new MDLV2000Reader(new FileInputStream(aFile), IChemObjectReader.Mode.RELAXED);
        tmpAtomContainer = tmpReader.read(new AtomContainer());
    } else if (tmpFormat.getFormatName().equalsIgnoreCase(MDLV3000Format.getInstance().getFormatName())) {
        MDLV3000Reader tmpReader = new MDLV3000Reader(new FileInputStream(aFile), IChemObjectReader.Mode.RELAXED);
        tmpAtomContainer = tmpReader.read(new AtomContainer());
    } else {
        throw new CDKException("The mol file does not correspond to either the MDLV2000 or the MDLV3000 format and " + "therefore cannot be imported.");
    }
    String tmpName = this.findMoleculeName(tmpAtomContainer);
    if (tmpName == null) {
        BufferedReader tmpBufferedReader = new BufferedReader(new FileReader(aFile));
        tmpName = tmpBufferedReader.readLine();
        if (tmpName == null || tmpName.isBlank() || tmpName.isEmpty())
            tmpName = FileUtil.getFileNameWithoutExtension(aFile);
        tmpBufferedReader.close();
    }
    tmpAtomContainer.setProperty(Importer.MOLECULE_NAME_PROPERTY_KEY, tmpName);
    tmpAtomContainerSet.addAtomContainer(tmpAtomContainer);
    tmpInputStream.close();
    return tmpAtomContainerSet;
}
Also used : FormatFactory(org.openscience.cdk.io.FormatFactory) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) MDLV2000Reader(org.openscience.cdk.io.MDLV2000Reader) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) FileInputStream(java.io.FileInputStream) IChemFormat(org.openscience.cdk.io.formats.IChemFormat) BufferedInputStream(java.io.BufferedInputStream) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) BufferedReader(java.io.BufferedReader) MDLV3000Reader(org.openscience.cdk.io.MDLV3000Reader) FileReader(java.io.FileReader)

Example 18 with AtomContainerSet

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

the class IDCreatorTest method testCallingTwice.

/**
 * @cdk.bug 1455341
 */
@Test
public void testCallingTwice() {
    IAtomContainerSet molSet = new AtomContainerSet();
    IAtomContainer mol = new AtomContainer();
    Atom atom0 = new Atom("C");
    Atom atom2 = new Atom("C");
    atom0.setID("a1");
    mol.addAtom(atom2);
    mol.addAtom(atom0);
    molSet.addAtomContainer(mol);
    IDCreator.createIDs(molSet);
    List<String> ids = MoleculeSetManipulator.getAllIDs(molSet);
    Assert.assertEquals(4, ids.size());
    mol = new AtomContainer();
    Atom atom1 = new Atom("C");
    atom2 = new Atom("C");
    atom1.setID("a2");
    mol.addAtom(atom2);
    mol.addAtom(atom1);
    molSet.addAtomContainer(mol);
    IDCreator.createIDs(molSet);
    ids = MoleculeSetManipulator.getAllIDs(molSet);
    Assert.assertEquals(7, ids.size());
    mol = new AtomContainer();
    atom1 = new Atom("C");
    atom2 = new Atom("C");
    mol.addAtom(atom2);
    mol.addAtom(atom1);
    molSet.addAtomContainer(mol);
    atom0.setID("atomX");
    ids = MoleculeSetManipulator.getAllIDs(molSet);
    Assert.assertFalse(ids.contains("a1"));
    IDCreator.createIDs(molSet);
    List<String> idsAfter = MoleculeSetManipulator.getAllIDs(molSet);
    Assert.assertTrue(idsAfter.contains("a1"));
    Assert.assertEquals(10, idsAfter.size());
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) AtomContainerSet(org.openscience.cdk.AtomContainerSet) Atom(org.openscience.cdk.Atom) Test(org.junit.Test)

Example 19 with AtomContainerSet

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

the class SDFWriterTest method testWrite_IAtomContainerSet_Properties_Off.

@Test
public void testWrite_IAtomContainerSet_Properties_Off() throws Exception {
    StringWriter writer = new StringWriter();
    IAtomContainerSet molSet = new AtomContainerSet();
    IAtomContainer molecule = new AtomContainer();
    molecule.addAtom(new Atom("C"));
    molecule.setProperty("foo", "bar");
    molSet.addAtomContainer(molecule);
    SDFWriter sdfWriter = new SDFWriter(writer);
    Properties sdfWriterProps = new Properties();
    sdfWriterProps.put("writeProperties", "false");
    sdfWriter.addChemObjectIOListener(new PropertiesListener(sdfWriterProps));
    sdfWriter.customizeJob();
    sdfWriter.write(molSet);
    sdfWriter.close();
    String result = writer.toString();
    Assert.assertFalse(result.contains("<foo>"));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) StringWriter(java.io.StringWriter) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) PropertiesListener(org.openscience.cdk.io.listener.PropertiesListener) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IAtom(org.openscience.cdk.interfaces.IAtom) Atom(org.openscience.cdk.Atom) ChemObjectWriterTest(org.openscience.cdk.test.io.ChemObjectWriterTest) Test(org.junit.Test)

Example 20 with AtomContainerSet

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

the class SDFWriterTest method testWrite_IAtomContainerSet_CDKProperties.

@Test
public void testWrite_IAtomContainerSet_CDKProperties() throws Exception {
    StringWriter writer = new StringWriter();
    IAtomContainerSet molSet = new AtomContainerSet();
    IAtomContainer molecule = new AtomContainer();
    molecule.addAtom(new Atom("C"));
    molecule.setProperty(InvPair.CANONICAL_LABEL, "bar");
    molSet.addAtomContainer(molecule);
    SDFWriter sdfWriter = new SDFWriter(writer);
    sdfWriter.write(molSet);
    sdfWriter.close();
    Assert.assertTrue(!writer.toString().contains(InvPair.CANONICAL_LABEL));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) StringWriter(java.io.StringWriter) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IAtom(org.openscience.cdk.interfaces.IAtom) Atom(org.openscience.cdk.Atom) ChemObjectWriterTest(org.openscience.cdk.test.io.ChemObjectWriterTest) Test(org.junit.Test)

Aggregations

AtomContainerSet (org.openscience.cdk.AtomContainerSet)30 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)30 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)26 Test (org.junit.Test)18 AtomContainer (org.openscience.cdk.AtomContainer)16 IAtom (org.openscience.cdk.interfaces.IAtom)12 Atom (org.openscience.cdk.Atom)11 InputStream (java.io.InputStream)6 SimpleChemObjectReaderTest (org.openscience.cdk.test.io.SimpleChemObjectReaderTest)6 StringWriter (java.io.StringWriter)5 ChemObjectWriterTest (org.openscience.cdk.test.io.ChemObjectWriterTest)5 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 ArrayList (java.util.ArrayList)4 ChemModel (org.openscience.cdk.ChemModel)4 CDKException (org.openscience.cdk.exception.CDKException)4 SDFWriter (org.openscience.cdk.io.SDFWriter)4 IteratingSMILESReader (org.openscience.cdk.io.iterator.IteratingSMILESReader)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3