📄 sln.java
字号:
/* -- labels -- */ case '@': dst = this.labels[this.getLabel(len)]; break; /* -- bonds -- */ case '.': b = Bond.NULL; break; case '-': b = Bond.SINGLE; break; case ':': b = Bond.AROMATIC; break; case '=': b = Bond.DOUBLE; break; case '#': b = Bond.TRIPLE; break; /* -- atoms -- */ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': dst = this.mol.addAtom(this.getAtom(len)); i = this.labels[0]; /* get atom type and add a new atom */ if (i <= 0) break; /* if there is no label, abort */ if (i >= this.labels.length) { int[] v = new int[i +16]; System.arraycopy(this.labels, 0, v, 0, this.labels.length); this.labels = v; /* if the label vector is too small, */ } /* enlarge the label vector */ this.labels[i] = dst; /* store the new atom index */ break; /* with the read label */ default : throw new IOException("illegal character "+c); } /* catch all other characters */ if ((src < 0) && (b != Bond.UNKNOWN)) throw new IOException("unexpected bond"); if (dst < 0) continue; /* if no bond to add, continue */ if (src >= 0) { /* if there is no source, skip bond */ if (b == Bond.UNKNOWN) /* if the bond type is unknown, */ b = Bond.SINGLE; /* the bond must be single */ 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 = -1; /* clear the atom 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) { return this.format(type, 0); } public String format (int type, int label) { /* --- describe an atom */ 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 */ c = (type != Atom.CHAIN) ? Atom.getCharge(type) : 0; if ((label <= 0) && (c == 0)) return s; /* if uncharged and unlabelled, abort */ s += "["; /* add atom attributes and the label */ if (label > 0) s += label +((c != 0) ? ";" : ""); if (c > 0) s += (c > +1) ? "+" +c : "+"; else if (c < 0) s += (c < -1) ? "" +c : "-"; return s +"]"; /* add the charge and brackets */ } /* format() */ /*------------------------------------------------------------------*/ protected void out (Atom a, TypeMap map, StringBuffer s) { /* --- recursive part of output */ int i, k, n; /* loop variables, number of branches */ 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); n = a.mark; /* note the number of labels */ if (a.mark < 0) /* if the atom needs a label, */ a.mark = ++this.labels[0];/* get the next label number */ s.append(this.format(k, a.mark)); /* append the atom description */ 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 */ } /* (bond and label) */ /* 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 = 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 */ SLN n = new SLN(); /* create an SLN object */ if (args.length != 1) { /* if wrong number of arguments */ System.err.println("usage: java " +n.getClass().getName() +" <SLN 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 SLN */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -