JmolNode[] sp2Atoms = prevSp2Atoms;
int nSp2Atoms = 0;
int atomicNumber = atom.getElementNumber();
int nH = 0;
List v = new ArrayList();
JmolEdge bond0 = null;
JmolEdge bondPrev = null;
JmolEdge[] bonds = atom.getEdges();
JmolNode aH = null;
int stereoFlag = (isAromatic ? 10 : 0);
JmolNode[] stereo = new JmolNode[7];
if (Logger.debugging)
Logger.debug(sb.toString());
// first look through the bonds for the best
// continuation -- bond0 -- and count hydrogens
// and create a list of bonds to process.
if (bonds != null)
for (int i = bonds.length; --i >= 0;) {
JmolEdge bond = bonds[i];
if (!bond.isCovalent())
continue;
JmolNode atom1 = bonds[i].getOtherAtom(atom);
int index1 = atom1.getIndex();
if (index1 == prevIndex) {
bondPrev = bonds[i];
continue;
}
boolean isH = (atom1.getElementNumber() == 1 && atom1
.getIsotopeNumber() == 0);
if (!bsIncludingH.get(index1)) {
if (!isH && allowConnectionsToOutsideWorld
&& bsSelected.get(atomIndex))
bsToDo.set(index1);
else
continue;
}
if (isH) {
aH = atom1;
nH++;
if (nH > 1)
stereoFlag = 10;
} else {
v.add(bonds[i]);
}
}
// order of listing is critical for stereochemistry:
//
// 1) previous atom
// 2) bond to previous atom
// 3) atom symbol
// 4) hydrogen atoms
// 5) branches
// 6) rings
// add the bond to the previous atom
String strBond = null;
if (sp2Atoms == null)
sp2Atoms = new JmolNode[5];
if (bondPrev == null) {
} else {
strBond = SmilesBond.getBondOrderString(bondPrev.getCovalentOrder());
if (prevSp2Atoms == null)
sp2Atoms[nSp2Atoms++] = prevAtom;
else
nSp2Atoms = 2;
}
nSp2Atoms += nH;
// get bond0
int nMax = 0;
BitSet bsBranches = new BitSet();
if (allowBranches)
for (int i = 0; i < v.size(); i++) {
JmolEdge bond = (JmolEdge) v.get(i);
JmolNode a = bond.getOtherAtom(atom);
int n = a.getCovalentBondCount() - a.getCovalentHydrogenCount();
int order = bond.getCovalentOrder();
if (order == 1 && n == 1 && i < v.size() - (bond0 == null ? 1 : 0)) {
bsBranches.set(bond.getIndex());
} else if ((order > 1 || n > nMax)
&& !htRings.containsKey(getRingKey(a.getIndex(), atomIndex))) {
nMax = (order > 1 ? 1000 + order : n);
bond0 = bond;
}
}
JmolNode atomNext = (bond0 == null ? null : bond0.getOtherAtom(atom));
int orderNext = (bond0 == null ? 0 : bond0.getCovalentOrder());
if (stereoFlag < 7 && bondPrev != null) {
if (bondPrev.getCovalentOrder() == 2 && orderNext == 2
&& prevSp2Atoms != null && prevSp2Atoms[1] != null) {
// allene continuation
stereo[stereoFlag++] = prevSp2Atoms[0];
stereo[stereoFlag++] = prevSp2Atoms[1];
} else {
stereo[stereoFlag++] = prevAtom;
}
}
if (stereoFlag < 7 && nH == 1)
stereo[stereoFlag++] = aH;
boolean deferStereo = (orderNext == 1 && prevSp2Atoms == null);
char chBond = getBondStereochemistry(bondPrev, prevAtom);
// now construct the branches part
StringBuffer sMore = new StringBuffer();
for (int i = 0; i < v.size(); i++) {
JmolEdge bond = (JmolEdge) v.get(i);
if (!bsBranches.get(bond.getIndex()))
continue;
JmolNode a = bond.getOtherAtom(atom);
StringBuffer s2 = new StringBuffer();
s2.append("(");
prevAtom = atom;
prevSp2Atoms = null;
JmolEdge bond0t = bond0;
getSmiles(s2, a, allowConnectionsToOutsideWorld, allowBranches);
bond0 = bond0t;
s2.append(")");
if (sMore.indexOf(s2.toString()) >= 0)
stereoFlag = 10;
sMore.append(s2);
v.remove(i--);
if (stereoFlag < 7)
stereo[stereoFlag++] = a;
if (nSp2Atoms < 5)
sp2Atoms[nSp2Atoms++] = a;
}
// from here on, prevBondAtoms and prevAtom must not be used.
// process the bond to the next atom
// and cancel any double bond stereochemistry if nec.
int index2 = (orderNext == 2 ? atomNext.getIndex() : -1);
if (nH > 1 || isAromatic || index2 < 0
|| SmilesSearch.isRingBond(ringSets, atomIndex, index2)) {
nSp2Atoms = -1;
}
if (nSp2Atoms < 0) {
sp2Atoms = null;
}
// output section
if (strBond != null || chBond != '\0') {
if (chBond != '\0') {
strBond = "" + chBond;
}
sb.append(strBond);
}
// now process any rings
String atat = null;
if (!allowBranches && (v.size() == 5 || v.size() == 6))
atat = sortInorganic(atom, v);
for (int i = 0; i < v.size(); i++) {
JmolEdge bond = (JmolEdge) v.get(i);
if (bond == bond0)
continue;
JmolNode a = bond.getOtherAtom(atom);
String s = getRingCache(atomIndex, a.getIndex(), htRings);
strBond = SmilesBond.getBondOrderString(bond.getOrder());
if (!deferStereo) {
chBond = getBondStereochemistry(bond, atom);
if (chBond != '\0') {
strBond = "" + chBond;
}