📄 smiles.java
字号:
else { a = Atom.CARBON; } break; case 'n': a = Atom.NITROGEN |Atom.AROMATIC; break; case 'N': a = Atom.NITROGEN; break; case 'o': a = Atom.OXYGEN |Atom.AROMATIC; break; case 'O': a = Atom.OXYGEN; break; case 'F': a = Atom.FLOURINE; break; case 'p': a = Atom.PHOSPHORUS|Atom.AROMATIC; break; case 'P': a = Atom.PHOSPHORUS; break; case 's': a = Atom.SULFUR |Atom.AROMATIC; break; case 'S': a = Atom.SULFUR; break; case 'I': a = Atom.IODINE; break; case 'H': a = Atom.HYDROGEN; break; case '[': a = this.getAtom(len); break; default : throw new IOException("illegal character "+c); } /* catch all other characters */ if ((src < 0) && (b != Bond.UNKNOWN)) throw new IOException("unexpected bond"); if (a >= 0) dst = this.mol.addAtom(a); else if (dst >= 0) a = this.mol.atoms[dst].type; else continue; /* complete the atom information */ if (src >= 0) { /* if there is no source, skip bond */ if (b == Bond.UNKNOWN){ /* if the bond type is unknown */ i = a & this.mol.atoms[src].type & Atom.AROMATIC; b = (i != 0) ? Bond.AROMATIC : Bond.SINGLE; } /* get a default bond type */ i = this.mol.addBond(src, dst, b); if (b == Bond.AROMATIC){/* add the bond to the molecule */ this.mol.atoms[src].type |= Atom.AROMATIC; this.mol.atoms[dst].type |= Atom.AROMATIC; } /* set aromatic flag in atoms */ b = Bond.UNKNOWN; /* clear the bond type */ } /* for the next step */ if (dst > src) src = dst; /* replace the source atom and */ dst = a = -1; /* clear the atom type and index */ } /* (for the next loop) */ } /* parse() */ /*------------------------------------------------------------------*/ public Molecule parse (String desc) throws IOException { /* --- create molecule from a desc. */ this.desc = desc; /* note the molecule description, */ this.pos = 0; /* initialize the character index */ this.mol = new Molecule(); /* and create an empty molecule */ for (int i = this.labels.length; --i >= 0; ) this.labels[i] = -1; /* clear the label vector */ this.parse(-1); /* recursively parse the molecule */ if (this.pos < desc.length()) throw new java.io.IOException("garbage at end of description"); this.mol.opt(); /* optimize memory usage and */ return this.mol; /* return the created molecule */ } /* parse() */ /*------------------------------------------------------------------*/ public String format (int type) { /* --- describe an atom */ int i = -1; /* loop variable */ int t, c; /* element type and charge */ String s; /* created string description */ t = type & Atom.TYPEMASK; /* get the element type */ s = Atom.names[t]; /* and its name */ if ((type & Atom.AROMATIC) != 0) s = s.toLowerCase(); /* aromatic atoms are in lowercase */ c = (type != Atom.CHAIN) ? Atom.getCharge(type) : 0; if (c > 0) s += (c > +1) ? "+" +c : "+"; else if (c < 0) s += (c < -1) ? "" +c : "-"; else { /* add a possible charge */ for (i = noBrackets.length; --i >= 0; ) if (t == noBrackets[i]) return s; } /* check whether brackets are nec. */ return "[" +s +"]"; /* add brackets if necessary */ } /* format() */ /*------------------------------------------------------------------*/ protected void out (Atom a, TypeMap map, StringBuffer s) { /* --- recursive part of output */ int i, k, n; /* loop variables, number of branches */ int p; /* preceding label, exchange buffer */ Bond b; /* to traverse the bonds */ Atom d; /* destination of bond */ k = a.type; /* get and decode chemical element */ if (map != null) k = map.decode(k); s.append(this.format(k)); /* append the atom description */ for (p = k = 0, i = n = a.mark; ++i <= 0; ) { while ((++k < this.labels.length) && (this.labels[k] >= 0)) ; /* find the next free label */ this.labels[k] = p; /* note the preceding label */ s.append(p = k); /* append the label and */ } /* switch to the next one */ a.mark = p; /* store the last label in the atom */ for (i = a.bondcnt; --i >= 0; ) { b = a.bonds[i]; /* traverse the unprocessed bonds */ if (b.mark == 0) continue;/* that lead back to some atom */ d = (b.src != a) ? b.src : b.dst; if (d.mark <= 0) { n++; continue; } b.mark = 0; /* mark the bond as processed */ k = (b.type == Bond.NULL) ? Bond.SINGLE : b.type; s.append(Bond.names[k & Bond.TYPEMASK]); s.append(d.mark); /* append the backward connection */ p = this.labels[d.mark]; /* get the next label of the atom, */ this.labels[d.mark] = -1; /* unmark the used label, */ d.mark = p; /* and note the next label */ } /* (count down the labels) */ /* Here n contains the number of branches leading away from the */ /* atom, which is the number of bonds leading to unvisited atoms */ /* minus the number of bonds that lead back to the atom through */ /* labels (obtained from the original value of atom.mark). */ for (i = 0; i < a.bondcnt; i++) { b = a.bonds[i]; /* traverse the unprocessed bonds */ if (b.mark == 0) continue; b.mark = 0; /* mark the bond as processed */ d = (b.src != a) ? b.src : b.dst; if (--n > 0) s.append("("); k = (b.type == Bond.NULL) ? Bond.SINGLE : b.type; s.append(Bond.names[k & Bond.TYPEMASK]); this.out(d, map, s); /* process the branch recursively */ if (n > 0) s.append(")"); /* if this is not the last branch, */ } /* terminate the branch */ } /* out() */ /*------------------------------------------------------------------*/ public String format (Molecule mol) { /* --- create a string description */ int i, n; /* loop variable, counter */ Atom a; /* to traverse the atoms */ StringBuffer s = new StringBuffer(); for (i = this.labels.length; --i > 0; ) this.labels[i] = -1; /* clear all labels (mark as unused) */ this.labels[0] = 0; /* and init. the label counter */ for (i = mol.atomcnt; --i >= 0; ) mol.atoms[i].mark = 1; /* mark the atoms of the molecule */ for (i = mol.bondcnt; --i >= 0; ) mol.bonds[i].mark = 0; /* mark the bonds of the molecule */ for (i = n = 0; i < mol.atomcnt; i++) { a = mol.atoms[i]; /* traverse the unprocessed atoms */ if (a.mark < 0) continue; /* (unprocessed connected components) */ if (n++ > 0) /* connect components by a null bond */ s.append(Bond.names[Bond.NULL]); mark(a); /* mark visits of each atom */ this.out(a, mol.map, s); /* output a connected component */ unmark(a); /* and clear the atom markers */ } return s.toString(); /* return the created description */ } /* format() */ /*------------------------------------------------------------------*/ public static final void main (String args[]) { /* --- main function for testing */ SMILES n = new SMILES(); /* create a SMILES object */ if (args.length != 1) { /* if wrong number of arguments */ System.err.println("usage: java " +n.getClass().getName() +" <SMILES string>"); return; /* print a usage message */ } /* and abort the program */ try { System.out.println(n.format(n.parse(args[0]))); } catch (IOException ioe) { /* try to parse the description */ System.err.println("Error: "+ioe.getMessage()); } } /* main() */ } /* class Smiles */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -