📄 hosecodegenerator.java
字号:
} } } } Collections.sort(nextSphereNodes,new TreeNodeComparator()); if (sphere < maxSphere) { sphere++; nextSphere(nextSphereNodes); } } public String makeBremserCompliant(String code) { int sepIndex = code.indexOf(";"); if (sepIndex >= 0) { code = code.substring(sepIndex + 1, code.length()); } return code; } /** * After recursivly having established the spheres and assigning each node an * appropriate score, we now generate the complete HOSE code. * *@exception org.openscience.cdk.exception.CDKException Thrown if something goes wrong */ private void createCode() throws org.openscience.cdk.exception.CDKException { List sphereNodes = null; TreeNode tn = null; for (int f = 0; f < atomContainer.getAtomCount(); f++) { atomContainer.getAtom(f).setFlag(CDKConstants.VISITED, false); } for (int f = 0; f < maxSphere; f++) { sphereNodes = spheres[maxSphere - f]; for (int g = 0; g < sphereNodes.size(); g++) { tn = (TreeNode) sphereNodes.get(g); if (tn.source != null) { tn.source.ranking += tn.degree; } } } for (int f = 0; f < maxSphere; f++) { sphereNodes = spheres[f]; calculateNodeScores(sphereNodes); sortNodesByScore(sphereNodes); } for (int f = 0; f < maxSphere; f++) { sphereNodes = spheres[f]; for (int g = 0; g < sphereNodes.size() ; g++) { tn = (TreeNode) sphereNodes.get(g); tn.score += tn.ranking; } sortNodesByScore(sphereNodes); } for (int f = 0; f < maxSphere; f++) { sphereNodes = spheres[f]; for (int g = 0; g < sphereNodes.size() ; g++) { tn = (TreeNode) sphereNodes.get(g); String localscore=tn.score+""; while(localscore.length()<6){ localscore="0"+localscore; } tn.stringscore=tn.source.stringscore+""+localscore; } sortNodesByScore(sphereNodes); } HOSECode.append(centerCode); for (int f = 0; f < maxSphere; f++) { sphere = f + 1; sphereNodes = spheres[f]; String s = getSphereCode(sphereNodes); HOSECode.append(s); } } /** * Generates the string code for a given sphere * *@param sphereNodes A vector of TreeNodes for which a string code is to be generated *@return The SphereCode value *@exception org.openscience.cdk.exception.CDKException Thrown if something goes wrong */ private String getSphereCode(List sphereNodes) throws org.openscience.cdk.exception.CDKException { if (sphereNodes == null || sphereNodes.size() < 1) { return sphereDelimiters[sphere - 1]; } TreeNode treeNode = null; StringBuffer code = new StringBuffer(); /* * append the tree node code to the HOSECode in * their now determined order, using commas to * separate nodes from different branches */ IAtom branch = ((TreeNode) (((TreeNode) sphereNodes.get(0)).source)).atom; StringBuffer tempCode = null; for (int i = 0; i < sphereNodes.size(); i++) { treeNode = (TreeNode) sphereNodes.get(i); tempCode = new StringBuffer(); if (!treeNode.source.stopper && treeNode.source.atom != branch) { branch = treeNode.source.atom; code.append(","); } if (!treeNode.source.stopper && treeNode.source.atom == branch) { if (treeNode.bondType <= 4) { tempCode.append(bondSymbols[(int) treeNode.bondType]); } else { throw new CDKException("Unknown bond type"); } if (treeNode.atom != null && !treeNode.atom.getFlag(CDKConstants.VISITED)) { tempCode.append(getElementSymbol(treeNode.symbol)); } else if (treeNode.atom != null && treeNode.atom.getFlag(CDKConstants.VISITED)) { tempCode.append("&"); treeNode.stopper = true; } code.append(tempCode+createChargeCode(treeNode.atom)); treeNode.hSymbol = tempCode.toString(); } if (treeNode.atom != null) treeNode.atom.setFlag(CDKConstants.VISITED, true); if (treeNode.source.stopper) treeNode.stopper = true; } code.append(sphereDelimiters[sphere - 1]); return code.toString(); } /** * Gets the element rank for a given element symbol as given in Bremser's * publication * *@param symbol The element symbol for which the rank is to be determined *@return The element rank */ private double getElementRank(String symbol) { for (int f = 0; f < rankedSymbols.length; f++) { if (rankedSymbols[f].equals(symbol)) { return symbolRankings[f]; } } try { IIsotope isotope = IsotopeFactory.getInstance(new ChemObject().getBuilder()).getMajorIsotope(symbol); return ((double) 800000 - isotope.getMassNumber()); } catch (Exception exception) { System.err.println("Could not find major isotope for this element!!! : " + symbol); System.err.println("Because of this error: " + exception.getMessage()); } return (double)800000; } /** * Returns the Bremser-compatible symbols for a given element. Silicon, for * example, is actually "Q". :-) * *@param sym The element symbol to be converted *@return The converted symbol */ private String getElementSymbol(String sym) { if (sym.equals("Si")) { return "Q"; } if (sym.equals("Cl")) { return "X"; } if (sym.equals("Br")) { return "Y"; } if (sym.equals(",")) { return ""; } return sym; } /** * Determines the ranking score for each node, allowing for a sorting of nodes * within one sphere. * *@param sphereNodes The nodes for which the score is to be calculated. *@exception org.openscience.cdk.exception.CDKException Thrown if something goes wrong. */ private void calculateNodeScores(List sphereNodes) throws org.openscience.cdk.exception.CDKException { TreeNode treeNode = null; for (int i = 0; i < sphereNodes.size(); i++) { treeNode = (TreeNode) sphereNodes.get(i); treeNode.score += getElementRank(treeNode.symbol); if (treeNode.bondType <= 4) { treeNode.score += bondRankings[(int) treeNode.bondType]; } else { throw new CDKException("Unknown bond type encountered in HOSECodeGenerator"); } } } /** * Sorts the nodes (atoms) in the sphereNode vector according to their score * This is used for the essential ranking of nodes in HOSE code sphere * *@param sphereNodes A vector with sphere nodes to be sorted. */ private void sortNodesByScore(List sphereNodes) { Object obj; boolean changed; if (sphereNodes.size() == 0) return; /* * Now we sort by score */ do { changed = false; for (int i = 0; i < sphereNodes.size() - 1; i++) { if (((TreeNode) sphereNodes.get(i + 1)).stringscore.compareTo(((TreeNode) sphereNodes.get(i)).stringscore)>0) { obj = sphereNodes.get(i + 1); sphereNodes.remove(i + 1); sphereNodes.add(i, obj); changed = true; } } } while (changed); /* Having sorted a sphere, we lable the nodes with their sort order */ TreeNode temp = null; for (int i = 0; i < sphereNodes.size(); i++) { temp = ((TreeNode) sphereNodes.get(i)); temp.sortOrder = sphereNodes.size() - i; } } /** * If we use less than four sphere, this fills up the code with the missing * delimiters such that we are compatible with Bremser's HOSE code table. */ private void fillUpSphereDelimiters() { logger.debug("Sphere: " + sphere); for (int f = sphere; f < 4; f++) { HOSECode.append(sphereDelimiters[f]); } } class TreeNodeComparator implements Comparator { /** *The compare method, compares by canonical label of atoms * * @param obj1 The first TreeNode * @param obj2 The second TreeNode * @return -1,0,1 */ public int compare(Object obj1, Object obj2) { if(obj1==null || obj2==null || ((TreeNode) obj1).getAtom()==null || ((TreeNode) obj2).getAtom()==null) return 0; Long label1 = (Long)((TreeNode) obj1).getAtom().getProperty("CanonicalLable"); Long label2 = (Long)((TreeNode) obj2).getAtom().getProperty("CanonicalLable"); if(label1==null || label2==null) return 0; if (label1.intValue() < label2.intValue()) { return (-1); } if (label1.intValue() > label2.intValue()) { return (1); } return (0); } } /** * Helper class for storing the properties of a node in our breadth first * search * * @author steinbeck * @cdk.created 2002-11-16 */ class TreeNode { String symbol; TreeNode source; IAtom atom; double bondType; int degree; long score; int ranking; int sortOrder = 1; List childs = null; String hSymbol = null; boolean stopper = false; String stringscore=""; /** * Constructor for the TreeNode object * *@param symbol The Element symbol of the node *@param source The preceding node for this node *@param atom The cdk atom object belonging to this node *@param bondType The bond type by which this node was connect to its * predecessor *@param score The score used to rank this node within its sphere. *@param degree Description of the Parameter */ TreeNode(String symbol, TreeNode source, IAtom atom, double bondType, int degree, long score) { this.symbol = symbol; this.source = source; this.atom = atom; this.degree = degree; this.score = score; this.bondType = bondType; ranking = 0; sortOrder = 1; childs = new ArrayList(); } public IAtom getAtom(){ return atom; } /** * A TreeNode is equal to another TreeNode if it * stands for the same atom object * *@param o The object tht we compare this TreeNode to *@return True, if the this TreeNode's atom object equals the one of the other TreeNode */ public boolean equals(Object o) { try { if (this.atom == ((TreeNode) o).atom) { return true; } } catch(Exception exc) { /* we do nothing here because anything that could seriously happen here is the we got something which is not a TreeNode and then got a class cast exception. Thus we can just wait until the end of the method returns a "false" */ } return false; } public String toString() { String s = ""; try { s += (atomContainer.getAtomNumber(atom) + 1); s += " " + hSymbol; s += "; s=" + score; s += "; r=" + ranking; s += "; d = " + degree; } catch(Exception exc) { return exc.toString(); } return s; } } public List getNodesInSphere(int sphereNumber){ sphereNodes = spheres[sphereNumber-1]; List atoms=new ArrayList(); for (int g = 0; g < sphereNodes.size() ; g++) { atoms.add(((TreeNode) sphereNodes.get(g)).atom); } return(atoms); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -