use of org.openscience.cdk.config.Elements in project cdk by cdk.
the class FischerRecognition method recognise.
/**
* Recognise the tetrahedral stereochemistry in the provided structure.
*
* @param projections allowed projection types
* @return zero of more stereo elements
*/
List<IStereoElement> recognise(Set<Projection> projections) {
if (!projections.contains(Projection.Fischer))
return Collections.emptyList();
// build atom index and only recognize 2D depictions
Map<IAtom, Integer> atomToIndex = new HashMap<>();
for (IAtom atom : container.atoms()) {
if (atom.getPoint2d() == null)
return Collections.emptyList();
atomToIndex.put(atom, atomToIndex.size());
}
RingSearch ringSearch = new RingSearch(container, graph);
final List<IStereoElement> elements = new ArrayList<>(5);
for (int v = 0; v < container.getAtomCount(); v++) {
IAtom focus = container.getAtom(v);
Elements elem = Elements.ofNumber(focus.getAtomicNumber());
if (elem != Carbon)
continue;
if (ringSearch.cyclic(v))
continue;
if (stereocenters.elementType(v) != Tetracoordinate)
continue;
if (!stereocenters.isStereocenter(v))
continue;
ITetrahedralChirality element = newTetrahedralCenter(focus, neighbors(v, graph, bonds));
if (element == null)
continue;
// east/west bonds must be to terminal atoms
IAtom east = element.getLigands()[EAST];
IAtom west = element.getLigands()[WEST];
if (!east.equals(focus) && !isTerminal(east, atomToIndex))
continue;
if (!west.equals(focus) && !isTerminal(west, atomToIndex))
continue;
elements.add(element);
}
return elements;
}
use of org.openscience.cdk.config.Elements in project cdk by cdk.
the class Abbreviations method newSymbol.
private String newSymbol(int atomnum, int hcount, boolean prefix) {
StringBuilder sb = new StringBuilder();
Elements elem = Elements.ofNumber(atomnum);
if (elem == Elements.Carbon && hcount == 3)
return "Me";
if (prefix) {
if (hcount > 0) {
sb.append('H');
if (hcount > 1)
sb.append(hcount);
}
sb.append(elem.symbol());
} else {
sb.append(elem.symbol());
if (hcount > 0) {
sb.append('H');
if (hcount > 1)
sb.append(hcount);
}
}
return sb.toString();
}
use of org.openscience.cdk.config.Elements in project cdk by cdk.
the class MDLV3000Reader method readAtomBlock.
/**
* Reads the atoms, coordinates and charges.
*
* <p>IMPORTANT: it does not support the atom list and its negation!
*/
public void readAtomBlock(ReadState state) throws CDKException {
IAtomContainer readData = state.mol;
logger.info("Reading ATOM block");
int RGroupCounter = 1;
int Rnumber;
String[] rGroup;
boolean foundEND = false;
while (isReady() && !foundEND) {
String command = readCommand(readLine());
if ("END ATOM".equals(command)) {
// FIXME: should check whether 3D is really 2D
foundEND = true;
} else {
logger.debug("Parsing atom from: " + command);
IAtom atom = readData.getBuilder().newAtom();
StringTokenizer tokenizer = new StringTokenizer(command);
// parse the index
try {
atom.setID(tokenizer.nextToken());
} catch (Exception exception) {
String error = "Error while parsing atom index";
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
// parse the element
String element = tokenizer.nextToken();
Elements e = Elements.ofString(element);
if (e != Elements.Unknown) {
atom.setAtomicNumber(e.number());
} else if ("D".equals(element) && optHydIso.isSet()) {
atom.setMassNumber(2);
atom.setAtomicNumber(IElement.H);
} else if ("T".equals(element) && optHydIso.isSet()) {
atom.setMassNumber(3);
atom.setAtomicNumber(IElement.H);
} else if ("A".equals(element)) {
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
} else if ("Q".equals(element)) {
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
} else if ("*".equals(element)) {
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
} else if ("LP".equals(element)) {
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
} else if ("L".equals(element)) {
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
} else if (element.length() > 0 && element.charAt(0) == 'R') {
logger.debug("Atom ", element, " is not an regular element. Creating a PseudoAtom.");
// check if the element is R
rGroup = element.split("^R");
if (rGroup.length > 1) {
try {
Rnumber = Integer.parseInt(rGroup[(rGroup.length - 1)]);
RGroupCounter = Rnumber;
} catch (Exception ex) {
Rnumber = RGroupCounter;
RGroupCounter++;
}
element = "R" + Rnumber;
}
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
} else {
if (mode == ISimpleChemObjectReader.Mode.STRICT) {
throw new CDKException("Invalid element type. Must be an existing element, or one in: A, Q, L, LP, *.");
}
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
atom.setSymbol(element);
}
// parse atom coordinates (in Angstrom)
try {
String xString = tokenizer.nextToken();
String yString = tokenizer.nextToken();
String zString = tokenizer.nextToken();
double x = Double.parseDouble(xString);
double y = Double.parseDouble(yString);
double z = Double.parseDouble(zString);
atom.setPoint3d(new Point3d(x, y, z));
} catch (Exception exception) {
String error = "Error while parsing atom coordinates";
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
// atom-atom mapping
String mapping = tokenizer.nextToken();
if (!mapping.equals("0")) {
logger.warn("Skipping atom-atom mapping: " + mapping);
}
// the rest are key value things
if (command.indexOf('=') != -1) {
Map<String, String> options = parseOptions(exhaustStringTokenizer(tokenizer));
for (String key : options.keySet()) {
String value = options.get(key);
try {
switch(key) {
case "CFG":
int cfg = Integer.parseInt(value);
if (cfg != 0) {
atom.setStereoParity(cfg);
state.stereo0d.put(atom, cfg);
}
break;
case "CHG":
int charge = Integer.parseInt(value);
if (charge != 0) {
// zero is no charge specified
atom.setFormalCharge(charge);
}
break;
case "RAD":
MDLV2000Writer.SPIN_MULTIPLICITY spinMultiplicity = MDLV2000Writer.SPIN_MULTIPLICITY.ofValue(Integer.parseInt(value));
int numElectons = spinMultiplicity.getSingleElectrons();
atom.setProperty(CDKConstants.SPIN_MULTIPLICITY, spinMultiplicity);
while (numElectons-- > 0) {
readData.addSingleElectron(readData.getBuilder().newInstance(ISingleElectron.class, atom));
}
break;
case "MASS":
atom.setMassNumber(Integer.parseInt(value));
break;
case "VAL":
if (!(atom instanceof IPseudoAtom)) {
try {
int valence = Integer.parseInt(value);
if (valence != 0) {
// 15 is defined as 0 in mol files
if (valence == 15)
atom.setValency(0);
else
atom.setValency(valence);
}
} catch (Exception exception) {
handleError("Could not parse valence information field", lineNumber, 0, 0, exception);
}
} else {
logger.error("Cannot set valence information for a non-element!");
}
break;
default:
logger.warn("Not parsing key: " + key);
break;
}
} catch (Exception exception) {
String error = "Error while parsing key/value " + key + "=" + value + ": " + exception.getMessage();
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
}
}
// store atom
readData.addAtom(atom);
logger.debug("Added atom: " + atom);
}
}
}
use of org.openscience.cdk.config.Elements in project cdk by cdk.
the class Atom method parseAtomSymbol.
private static boolean parseAtomSymbol(IAtom atom, String str) {
final int len = str.length();
int pos = 0;
int mass = -1;
int anum = 0;
int hcnt = -1;
int chg = 0;
String symbol = null;
boolean flag = false;
// optional mass
if (pos < len && isDigit(str.charAt(pos))) {
mass = (str.charAt(pos++) - '0');
while (pos < len && isDigit(str.charAt(pos))) mass = 10 * mass + (str.charAt(pos++) - '0');
} else if ("R".equals(str)) {
anum = 0;
symbol = "R";
flag = true;
} else if ("*".equals(str)) {
anum = 0;
symbol = "*";
flag = true;
} else if ("D".equals(str)) {
anum = 1;
mass = 2;
symbol = "H";
flag = true;
} else if ("T".equals(str)) {
anum = 1;
mass = 3;
symbol = "H";
flag = true;
}
if (flag == false) {
// atom symbol
if (pos < len && isUpper(str.charAt(pos))) {
int beg = pos;
pos++;
while (pos < len && isLower(str.charAt(pos))) pos++;
Elements elem = Elements.ofString(str.substring(beg, pos));
if (elem == Elements.Unknown)
return false;
anum = elem.number();
// optional fields after atom symbol
while (pos < len) {
switch(str.charAt(pos)) {
case 'H':
pos++;
if (pos < len && isDigit(str.charAt(pos))) {
hcnt = 0;
while (pos < len && isDigit(str.charAt(pos))) hcnt = 10 * hcnt + (str.charAt(pos++) - '0');
} else {
hcnt = 1;
}
break;
case '+':
pos++;
if (pos < len && isDigit(str.charAt(pos))) {
chg = (str.charAt(pos++) - '0');
while (pos < len && isDigit(str.charAt(pos))) chg = 10 * chg + (str.charAt(pos++) - '0');
} else {
chg = +1;
}
break;
case '-':
pos++;
if (pos < len && isDigit(str.charAt(pos))) {
chg = (str.charAt(pos++) - '0');
while (pos < len && isDigit(str.charAt(pos))) chg = 10 * chg + (str.charAt(pos++) - '0');
chg *= -1;
} else {
chg = -1;
}
break;
default:
return false;
}
}
} else {
return false;
}
flag = pos == len && len > 0;
symbol = Elements.ofNumber(anum).symbol();
}
if (!flag)
return false;
if (mass < 0)
atom.setMassNumber(null);
else
atom.setMassNumber(mass);
atom.setAtomicNumber(anum);
atom.setSymbol(symbol);
if (hcnt >= 0)
atom.setImplicitHydrogenCount(hcnt);
atom.setFormalCharge(chg);
return true;
}
use of org.openscience.cdk.config.Elements in project cdk by cdk.
the class Atom method parseAtomSymbol.
private static boolean parseAtomSymbol(IAtom atom, String str) {
final int len = str.length();
int pos = 0;
int mass = -1;
int anum = 0;
int hcnt = -1;
int chg = 0;
String symbol = null;
boolean flag = false;
// optional mass
if (pos < len && isDigit(str.charAt(pos))) {
mass = (str.charAt(pos++) - '0');
while (pos < len && isDigit(str.charAt(pos))) mass = 10 * mass + (str.charAt(pos++) - '0');
} else if ("R".equals(str)) {
anum = 0;
symbol = "R";
flag = true;
} else if ("*".equals(str)) {
anum = 0;
symbol = "*";
flag = true;
} else if ("D".equals(str)) {
anum = 1;
mass = 2;
symbol = "H";
flag = true;
} else if ("T".equals(str)) {
anum = 1;
mass = 3;
symbol = "H";
flag = true;
}
if (flag == false) {
// atom symbol
if (pos < len && isUpper(str.charAt(pos))) {
int beg = pos;
pos++;
while (pos < len && isLower(str.charAt(pos))) pos++;
Elements elem = Elements.ofString(str.substring(beg, pos));
if (elem == Elements.Unknown)
return false;
anum = elem.number();
// optional fields after atom symbol
while (pos < len) {
switch(str.charAt(pos)) {
case 'H':
pos++;
if (pos < len && isDigit(str.charAt(pos))) {
hcnt = 0;
while (pos < len && isDigit(str.charAt(pos))) hcnt = 10 * hcnt + (str.charAt(pos++) - '0');
} else {
hcnt = 1;
}
break;
case '+':
pos++;
if (pos < len && isDigit(str.charAt(pos))) {
chg = (str.charAt(pos++) - '0');
while (pos < len && isDigit(str.charAt(pos))) chg = 10 * chg + (str.charAt(pos++) - '0');
} else {
chg = +1;
}
break;
case '-':
pos++;
if (pos < len && isDigit(str.charAt(pos))) {
chg = (str.charAt(pos++) - '0');
while (pos < len && isDigit(str.charAt(pos))) chg = 10 * chg + (str.charAt(pos++) - '0');
chg *= -1;
} else {
chg = -1;
}
break;
default:
return false;
}
}
} else {
return false;
}
flag = pos == len && len > 0;
symbol = Elements.ofNumber(anum).symbol();
}
if (!flag)
return false;
if (mass < 0)
atom.setMassNumber(null);
else
atom.setMassNumber(mass);
atom.setAtomicNumber(anum);
atom.setSymbol(symbol);
if (hcnt >= 0)
atom.setImplicitHydrogenCount(hcnt);
atom.setFormalCharge(chg);
return true;
}
Aggregations