Skip to content

Commit

Permalink
Merge pull request #354 from cdk/patch/22Aug17
Browse files Browse the repository at this point in the history
Looks good.
  • Loading branch information
egonw committed Aug 23, 2017
2 parents ac6a353 + 9caea10 commit 4de578c
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 37 deletions.
Expand Up @@ -681,9 +681,11 @@ private boolean ensure2dLayout(IAtomContainer container) throws CDKException {
* @throws CDKException coordinates could not be generated
*/
private void ensure2dLayout(IReaction rxn) throws CDKException {
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
sdg.setAlignMappedReaction(alignMappedReactions);
sdg.generateCoordinates(rxn);
if (!GeometryUtil.has2DCoordinates(rxn)) {
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
sdg.setAlignMappedReaction(alignMappedReactions);
sdg.generateCoordinates(rxn);
}
}

/**
Expand Down
Expand Up @@ -715,7 +715,17 @@ public IChemObjectBuilder getBuilder() {
public void stateChanged(IChemObjectChangeEvent event) {

}
};

@Override
public String getTitle() {
return null;
}

@Override
public void setTitle(String title) {

}
};

/**
* pseudo shortest-paths - when an invalid atom is given. this will always
Expand Down
17 changes: 17 additions & 0 deletions base/data/src/main/java/org/openscience/cdk/AtomContainer.java
Expand Up @@ -1479,6 +1479,23 @@ public boolean isEmpty() {
return atomCount == 0;
}


/**
* {@inheritDoc}
*/
@Override
public String getTitle() {
return getProperty(CDKConstants.TITLE);
}

/**
* {@inheritDoc}
*/
@Override
public void setTitle(String title) {
setProperty(CDKConstants.TITLE, title);
}

/**
* The inner AtomIterator class.
*/
Expand Down
Expand Up @@ -752,6 +752,20 @@ public interface IAtomContainer extends IChemObject, IChemObjectListener {
*/
boolean isEmpty();

/**
* Access the title of the record.
*
* @return the title
*/
String getTitle();

/**
* Modify the title of the record.
*
* @param title the title
*/
void setTitle(String title);

/**
* {@inheritDoc}
*/
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
Expand Down Expand Up @@ -1615,6 +1616,22 @@ public boolean isEmpty() {
return atomCount == 0;
}

/**
* {@inheritDoc}
*/
@Override
public String getTitle() {
return getProperty(CDKConstants.TITLE);
}

/**
* {@inheritDoc}
*/
@Override
public void setTitle(String title) {
setProperty(CDKConstants.TITLE, title);
}

@Override
public void setStereoElements(List<IStereoElement> elements) {
this.stereoElements.clear();
Expand Down
Expand Up @@ -1413,6 +1413,22 @@ public boolean isEmpty() {
return atomCount == 0;
}

/**
* {@inheritDoc}
*/
@Override
public String getTitle() {
return getProperty(CDKConstants.TITLE);
}

/**
* {@inheritDoc}
*/
@Override
public void setTitle(String title) {
setProperty(CDKConstants.TITLE, title);
}

/**
* The inner AtomIterator class.
*/
Expand Down
Expand Up @@ -967,6 +967,25 @@ public static boolean has2DCoordinates(IAtomContainer container) {

}

/**
* Determine if all parts of a reaction have coodinates
*
* @param reaction a reaction
* @return the reaction has coordinates
*/
public static boolean has2DCoordinates(IReaction reaction) {
for (IAtomContainer mol : reaction.getReactants().atomContainers())
if (!has2DCoordinates(mol))
return false;
for (IAtomContainer mol : reaction.getProducts().atomContainers())
if (!has2DCoordinates(mol))
return false;
for (IAtomContainer mol : reaction.getAgents().atomContainers())
if (!has2DCoordinates(mol))
return false;
return true;
}

/**
* Determines the coverage of this {@link org.openscience.cdk.interfaces.IAtomContainer}'s 2D
* coordinates. If all atoms are non-null and have 2D coordinates this method will return {@link
Expand Down
Expand Up @@ -200,15 +200,23 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {

int reactantCount = 0;
int productCount = 0;
int agentCount = 0;
try {
String countsLine = input.readLine();
/*
* this line contains the number of reactants and products
*/
StringTokenizer tokenizer = new StringTokenizer(countsLine);
reactantCount = Integer.valueOf(tokenizer.nextToken()).intValue();
reactantCount = Integer.valueOf(tokenizer.nextToken());
logger.info("Expecting " + reactantCount + " reactants in file");
productCount = Integer.valueOf(tokenizer.nextToken()).intValue();
productCount = Integer.valueOf(tokenizer.nextToken());
if (tokenizer.hasMoreTokens()) {
agentCount = Integer.valueOf(tokenizer.nextToken());
// ChemAxon extension, technically BIOVIA now support this but
// not documented yet
if (mode == Mode.STRICT && agentCount > 0)
throw new CDKException("RXN files uses agent count extension");
}
logger.info("Expecting " + productCount + " products in file");
} catch (IOException | NumberFormatException exception) {
logger.debug(exception);
Expand Down Expand Up @@ -272,6 +280,34 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
throw new CDKException("Error while reading products", exception);
}

// now read the products
try {
for (int i = 1; i <= agentCount; i++) {
StringBuffer molFile = new StringBuffer();
input.readLine(); // String announceMDLFileLine =
String molFileLine = "";
do {
molFileLine = input.readLine();
molFile.append(molFileLine);
molFile.append(System.getProperty("line.separator"));
} while (!molFileLine.equals("M END"));

// read MDL molfile content
MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.toString()));
IAtomContainer product = (IAtomContainer) reader.read(builder.newInstance(IAtomContainer.class));
reader.close();

// add reactant
reaction.addAgent(product);
}
} catch (CDKException exception) {
// rethrow exception from MDLReader
throw exception;
} catch (IOException | IllegalArgumentException exception) {
logger.debug(exception);
throw new CDKException("Error while reading products", exception);
}

// now try to map things, if wanted
logger.info("Reading atom-atom mapping from file");
// distribute all atoms over two AtomContainer's
Expand Down
Expand Up @@ -703,9 +703,13 @@ IAtom readAtomFast(String line, IChemObjectBuilder builder, Map<IAtom,Integer> p
parities.put(atom, parity);

// if there was a mass difference, set the mass number
if (massDiff != 0 && atom.getAtomicNumber() > 0)
atom.setMassNumber(Isotopes.getInstance().getMajorIsotope(atom.getAtomicNumber()).getMassNumber()
+ massDiff);
if (massDiff != 0 && atom.getAtomicNumber() > 0) {
IIsotope majorIsotope = Isotopes.getInstance().getMajorIsotope(atom.getAtomicNumber());
if (majorIsotope == null)
atom.setMassNumber(-1); // checked after M ISO is processed
else
atom.setMassNumber(majorIsotope.getMassNumber() + massDiff);
}

if (valence > 0 && valence < 16) atom.setValency(valence == 15 ? 0 : valence);

Expand Down Expand Up @@ -1188,6 +1192,12 @@ void readPropertiesFast(final BufferedReader input, final IAtomContainer contain
}
}

// check of ill specified atomic mass
for (IAtom atom : container.atoms()) {
if (atom.getMassNumber() != null && atom.getMassNumber() < 0)
throw new CDKException("Unstable use of mass delta on " + atom.getSymbol() + " please use M ISO");
}


if (!sgroups.isEmpty()) {
// load Sgroups into molecule, first we downcast
Expand Down
Expand Up @@ -360,22 +360,25 @@ public void writeMolecule(IAtomContainer container) throws Exception {
writer.write(comment);
writer.newLine();

// write Counts line
line += formatMDLInt(container.getAtomCount(), 3);
line += formatMDLInt(container.getBondCount(), 3);
line += " 0 0 0 0 0 0 0 0999 V2000";
writer.write(line);
writer.newLine();

// index stereo elements for setting atom parity values
Map<IAtom,ITetrahedralChirality> atomstereo = new HashMap<>();
Map<IAtom,Integer> atomindex = new HashMap<>();
for (IStereoElement element : container.stereoElements())
if (element instanceof ITetrahedralChirality)
atomstereo.put(((ITetrahedralChirality) element).getChiralAtom(), (ITetrahedralChirality) element);
if (element instanceof ITetrahedralChirality)
atomstereo.put(((ITetrahedralChirality) element).getChiralAtom(), (ITetrahedralChirality) element);
for (IAtom atom : container.atoms())
atomindex.put(atom, atomindex.size());

// write Counts line
line += formatMDLInt(container.getAtomCount(), 3);
line += formatMDLInt(container.getBondCount(), 3);
line += " 0 0";
// we mark all stereochemistry to absolute for now
line += atomstereo.isEmpty() ? " 0" : " 1";
line += " 0 0 0 0 0999 V2000";
writer.write(line);
writer.newLine();

// write Atom block
for (int f = 0; f < container.getAtomCount(); f++) {
IAtom atom = container.getAtom(f);
Expand Down
Expand Up @@ -38,6 +38,9 @@
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* TestCase for the reading MDL RXN files using one test file.
*
Expand Down Expand Up @@ -131,4 +134,13 @@ public void testReadMapping() throws Exception {
maps.next();
Assert.assertTrue(maps.hasNext());
}

@Test
public void testAgentParts() throws Exception {
try (InputStream in = this.getClass().getResourceAsStream("ethylesterification.mol");
MDLRXNV2000Reader rdr = new MDLRXNV2000Reader(in);) {
IReaction reaction = rdr.read(new Reaction());
assertThat(reaction.getAgents().getAtomContainerCount(), is(1));
}
}
}

0 comments on commit 4de578c

Please sign in to comment.