use of org.openscience.cdk.interfaces.IChemModel in project cdk by cdk.
the class XYZReader method readChemFile.
// private procedures
/**
* Private method that actually parses the input to read a ChemFile
* object.
*
* @return A ChemFile containing the data parsed from input.
*/
private IChemFile readChemFile(IChemFile file) {
IChemSequence chemSequence = file.getBuilder().newInstance(IChemSequence.class);
int number_of_atoms;
StringTokenizer tokenizer;
try {
String line = input.readLine();
while (input.ready() && line != null) {
// parse frame by frame
tokenizer = new StringTokenizer(line, "\t ,;");
String token = tokenizer.nextToken();
number_of_atoms = Integer.parseInt(token);
String info = input.readLine();
IChemModel chemModel = file.getBuilder().newInstance(IChemModel.class);
IAtomContainerSet setOfMolecules = file.getBuilder().newInstance(IAtomContainerSet.class);
IAtomContainer m = file.getBuilder().newInstance(IAtomContainer.class);
m.setTitle(info);
for (int i = 0; i < number_of_atoms; i++) {
line = input.readLine();
if (line == null)
break;
if (line.startsWith("#") && line.length() > 1) {
Object comment = m.getProperty(CDKConstants.COMMENT);
if (comment == null) {
comment = "";
}
comment = comment + line.substring(1).trim();
m.setProperty(CDKConstants.COMMENT, comment);
logger.debug("Found and set comment: ", comment);
// a comment line does not count as an atom
i--;
} else {
double x, y, z;
double charge = 0.0f;
tokenizer = new StringTokenizer(line, "\t ,;");
int fields = tokenizer.countTokens();
if (fields < 4) {
// this is an error but cannot throw exception
} else {
String atomtype = tokenizer.nextToken();
x = new Double(tokenizer.nextToken());
y = new Double(tokenizer.nextToken());
z = new Double(tokenizer.nextToken());
if (fields == 8)
charge = new Double(tokenizer.nextToken());
IAtom atom = file.getBuilder().newInstance(IAtom.class, atomtype, new Point3d(x, y, z));
atom.setCharge(charge);
m.addAtom(atom);
}
}
}
setOfMolecules.addAtomContainer(m);
chemModel.setMoleculeSet(setOfMolecules);
chemSequence.addChemModel(chemModel);
line = input.readLine();
}
file.addChemSequence(chemSequence);
} catch (IOException e) {
// should make some noise now
file = null;
logger.error("Error while reading file: ", e.getMessage());
logger.debug(e);
}
return file;
}
use of org.openscience.cdk.interfaces.IChemModel in project cdk by cdk.
the class CTXReader method readChemFile.
private IChemFile readChemFile() throws CDKException {
IChemSequence seq = file.getBuilder().newInstance(IChemSequence.class);
IChemModel model = file.getBuilder().newInstance(IChemModel.class);
IAtomContainerSet containerSet = file.getBuilder().newInstance(IAtomContainerSet.class);
IAtomContainer container = file.getBuilder().newInstance(IAtomContainer.class);
int lineNumber = 0;
try {
String line = input.readLine();
while (input.ready() && line != null) {
logger.debug((lineNumber++) + ": ", line);
String command;
if (isCommand(line)) {
command = getCommand(line);
int lineCount = getContentLinesCount(line);
if ("ATOMS".equals(command)) {
processAtomsBlock(lineCount, container);
} else if ("BONDS".equals(command)) {
processBondsBlock(lineCount, container);
} else if ("IDENT".equals(command)) {
processIdentBlock(lineCount, container);
} else if ("NAME".equals(command)) {
processNameBlock(lineCount, container);
} else {
// skip lines
logger.warn("Dropping block: ", command);
for (int i = 0; i < lineCount; i++) {
line = input.readLine();
if (line == null)
throw new CDKException("End of input while skipping lines!");
}
}
} else {
logger.warn("Unexpected content at line: ", lineNumber);
}
line = input.readLine();
}
containerSet.addAtomContainer(container);
model.setMoleculeSet(containerSet);
seq.addChemModel(model);
file.addChemSequence(seq);
} catch (Exception exception) {
String message = "Error while parsing CTX file: " + exception.getMessage();
logger.error(message);
logger.debug(exception);
throw new CDKException(message, exception);
}
return file;
}
use of org.openscience.cdk.interfaces.IChemModel in project cdk by cdk.
the class PCCompoundASNReader method readChemFile.
// private procedures
private IChemFile readChemFile(IChemFile file) throws Exception {
IChemSequence chemSequence = file.getBuilder().newInstance(IChemSequence.class);
IChemModel chemModel = file.getBuilder().newInstance(IChemModel.class);
IAtomContainerSet moleculeSet = file.getBuilder().newInstance(IAtomContainerSet.class);
molecule = file.getBuilder().newInstance(IAtomContainer.class);
atomIDs = new HashMap<>();
String line = input.readLine();
while (input.ready() && line != null) {
if (line.indexOf('{') != -1) {
processBlock(line);
} else {
logger.warn("Skipping non-block: " + line);
}
line = input.readLine();
}
moleculeSet.addAtomContainer(molecule);
chemModel.setMoleculeSet(moleculeSet);
chemSequence.addChemModel(chemModel);
file.addChemSequence(chemSequence);
return file;
}
use of org.openscience.cdk.interfaces.IChemModel in project cdk by cdk.
the class AbstractChemModelTest method testSetRingSet_IRingSet.
@Test
public void testSetRingSet_IRingSet() {
IChemModel chemModel = (IChemModel) newChemObject();
IRingSet crystal = chemModel.getBuilder().newInstance(IRingSet.class);
chemModel.setRingSet(crystal);
Assert.assertEquals(crystal, chemModel.getRingSet());
}
use of org.openscience.cdk.interfaces.IChemModel in project cdk by cdk.
the class AbstractChemModelTest method testToString.
@Test
public void testToString() {
IChemModel model = (IChemModel) newChemObject();
String description = model.toString();
for (int i = 0; i < description.length(); i++) {
Assert.assertTrue(description.charAt(i) != '\n');
Assert.assertTrue(description.charAt(i) != '\r');
}
}
Aggregations