Skip to content

Commit

Permalink
Fix setting fields on new Atom(String)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-ujihara committed Jul 29, 2018
1 parent 4a82401 commit 48aefb4
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 145 deletions.
147 changes: 77 additions & 70 deletions base/data/src/main/java/org/openscience/cdk/Atom.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,12 @@ public Atom(String symbol) {
throw new IllegalArgumentException("Cannot pass atom symbol: " + symbol);
}


/**
* Constructs an Atom from an Element and a Point3d.
*
* @param elementSymbol The symbol of the atom
* @param point3d The 3D coordinates of the atom
*/
* Constructs an Atom from an Element and a Point3d.
*
* @param elementSymbol The symbol of the atom
* @param point3d The 3D coordinates of the atom
*/
public Atom(String elementSymbol, Point3d point3d) {
this(elementSymbol);
this.point3d = point3d;
Expand Down Expand Up @@ -548,7 +547,6 @@ public IAtom clone() throws CloneNotSupportedException {
return (IAtom) clone;
}


private static boolean isUpper(char c) {
return c >= 'A' && c <= 'Z';
}
Expand All @@ -569,94 +567,103 @@ private static boolean parseAtomSymbol(IAtom atom, String str) {
int anum = 0;
int hcnt = 0;
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)) {
atom.setAtomicNumber(0);
atom.setSymbol("R");
return true;
anum = 0;
symbol = "R";
flag = true;
} else if ("*".equals(str)) {
atom.setAtomicNumber(0);
atom.setSymbol("*");
return true;
anum = 0;
symbol = "*";
flag = true;
} else if ("D".equals(str)) {
atom.setAtomicNumber(1);
atom.setMassNumber(2);
atom.setSymbol("H");
return true;
anum = 1;
mass = 2;
symbol = "H";
flag = true;
} else if ("T".equals(str)) {
atom.setAtomicNumber(1);
atom.setMassNumber(3);
atom.setSymbol("H");
return true;
anum = 1;
mass = 3;
symbol = "H";
flag = true;
}

// atom symbol
if (pos < len && isUpper(str.charAt(pos))) {
int beg = pos;
pos++;
while (pos < len && isLower(str.charAt(pos)))
if (flag == false) {
// atom symbol
if (pos < len && isUpper(str.charAt(pos))) {
int beg = 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))) {
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;
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))) {
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;
}
} 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(Elements.ofNumber(anum).symbol());
atom.setSymbol(symbol);
atom.setImplicitHydrogenCount(hcnt);
atom.setFormalCharge(chg);

return pos == len && len > 0;
return true;
}

/**
Expand Down
152 changes: 77 additions & 75 deletions base/silent/src/main/java/org/openscience/cdk/silent/Atom.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Atom(String elementSymbol, Point2d point2d) {
* and covalent radii, formal charge, hybridization, electron
* valency, formal neighbour count and atom type name from the
* given IAtomType. It does not copy the listeners and
* properties. If the element is an instanceof
* properties. If the element is an instance of
* IAtom, then the 2D, 3D and fractional coordinates, partial
* atomic charge, hydrogen count and stereo parity are copied
* too.
Expand Down Expand Up @@ -556,108 +556,110 @@ private static boolean isDigit(char c) {
}

private static boolean parseAtomSymbol(IAtom atom, String str) {
Elements elem = Elements.ofString(str);
if (elem != Elements.Unknown) {
atom.setAtomicNumber(elem.number());
atom.setSymbol(elem.symbol());
return true;
} else if ("R".equals(str)) {
atom.setAtomicNumber(0);
atom.setSymbol("R");
return true;
} else if ("*".equals(str)) {
atom.setAtomicNumber(0);
atom.setSymbol("*");
return true;
} else if ("D".equals(str)) {
atom.setAtomicNumber(1);
atom.setMassNumber(2);
atom.setSymbol("H");
return true;
} else if ("T".equals(str)) {
atom.setAtomicNumber(1);
atom.setMassNumber(3);
atom.setSymbol("H");
return true;
}

final int len = str.length();
int pos = 0;

int mass = -1;
int anum = 0;
int hcnt = 0;
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;
}

// atom symbol
if (pos < len && isUpper(str.charAt(pos))) {
int beg = pos;
pos++;
while (pos < len && isLower(str.charAt(pos)))
if (flag == false) {
// atom symbol
if (pos < len && isUpper(str.charAt(pos))) {
int beg = pos;
pos++;
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))) {
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;
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))) {
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;
}
} 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(Elements.ofNumber(anum).symbol());
atom.setSymbol(symbol);
atom.setImplicitHydrogenCount(hcnt);
atom.setFormalCharge(chg);

return pos == len && len > 0;
return true;
}

/**
Expand Down

0 comments on commit 48aefb4

Please sign in to comment.