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