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;
}
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;
}
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());
}
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>"));
}
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));
}
Aggregations