📄 mfanalyser.java
字号:
} } }else{ IElement i = si.getElement(key); mass += getNaturalMass(i)*((Integer)symbols.get(key)).intValue(); } } return mass; } /** * Produces an AtomContainer without explicit Hs but with H count from one with Hs. * Hs bonded to more than one heavy atom are preserved. The new molecule is a deep copy. * * @return The mol without Hs. * @cdk.keyword hydrogen, removal */ public IAtomContainer removeHydrogensPreserveMultiplyBonded() { IAtomContainer ac = getAtomContainer(); List h = new ArrayList(); // H list. List multi_h = new ArrayList(); // multiply bonded H // Find multiply bonded H. int count = ac.getBondCount(); for (int i = 0; i < count; i++) { java.util.Iterator atoms = ac.getBond(i).atoms(); while (atoms.hasNext()) { final IAtom atom = (IAtom)atoms.next(); if (atom.getSymbol().equals(H_ELEMENT_SYMBOL)) { (h.contains(atom) ? multi_h : h).add(atom); } } // The short version (assumes atoms.length == 2 is always true).// (h.contains(atoms[0]) ? multi_h : h).add(atoms[0]);// (h.contains(atoms[1]) ? multi_h : h).add(atoms[1]); } return removeHydrogens(multi_h); } /** * Produces an AtomContainer without explicit Hs (except those listed) but with H count from one with Hs. * The new molecule is a deep copy. * * @param preserve a list of H atoms to preserve. * @return The mol without Hs. * @cdk.keyword hydrogen, removal */ private IAtomContainer removeHydrogens(List preserve) { IAtomContainer ac = getAtomContainer(); Map map = new HashMap(); // maps original atoms to clones. List remove = new ArrayList(); // lists removed Hs. // Clone atoms except those to be removed. IMolecule mol = ac.getBuilder().newMolecule(); int count = ac.getAtomCount(); for (int i = 0; i < count; i++) { // Clone/remove this atom? IAtom atom = ac.getAtom(i); if (!atom.getSymbol().equals(H_ELEMENT_SYMBOL) || preserve.contains(atom)) { IAtom a = null; try { a = (IAtom) atom.clone(); } catch (CloneNotSupportedException e) { logger.error("Could not clone: ", atom); logger.debug(e); } a.setHydrogenCount(0); mol.addAtom(a); map.put(atom, a); } else { remove.add(atom); // maintain list of removed H. } } // Clone bonds except those involving removed atoms. count = ac.getBondCount(); for (int i = 0; i < count; i++) { // Check bond. final IBond bond = ac.getBond(i); IAtom atom0 = bond.getAtom(0); IAtom atom1 = bond.getAtom(1); java.util.Iterator atoms = bond.atoms(); boolean remove_bond = false; while (atoms.hasNext()){ if (remove.contains((IAtom)atoms.next())) { remove_bond = true; break; } } // Clone/remove this bond? if (!remove_bond) { // if (!remove.contains(atoms[0]) && !remove.contains(atoms[1])) IBond clone = null; try { clone = (IBond) ac.getBond(i).clone(); } catch (CloneNotSupportedException e) { logger.error("Could not clone: ", ac.getBond(i)); logger.debug(e); } clone.setAtoms(new IAtom[]{(IAtom) map.get(atom0), (IAtom) map.get(atom1)}); mol.addBond(clone); } } // Recompute hydrogen counts of neighbours of removed Hydrogens. for (Iterator i = remove.iterator(); i.hasNext(); ) { // Process neighbours. for (Iterator n = ac.getConnectedAtomsList((IAtom) i.next()).iterator(); n.hasNext(); ) { final IAtom neighb = (IAtom) map.get(n.next()); neighb.setHydrogenCount(neighb.getHydrogenCount() + 1); } } return (mol); } /** * Returns a set of nodes excluding all the hydrogens * * @return The heavyAtoms value * @cdk.keyword hydrogen, removal */ public List getHeavyAtoms() { ArrayList newAc = new ArrayList(); IAtomContainer ac = getAtomContainer(); for (int f = 0; f < ac.getAtomCount(); f++) { if (!ac.getAtom(f).getSymbol().equals(H_ELEMENT_SYMBOL)) { newAc.add(ac.getAtom(f)); } } return newAc; } /** * Method that actually does the work of analysing the molecular formula * * @param MF molecular formula to create an AtomContainer from * @param ac AtomContainer in which the Atom's and Bond's will be stored * @return the filled AtomContainer */ private IAtomContainer analyseMF(String MF, IAtomContainer ac) { char ThisChar; /* * Buffer for */ String RecentElementSymbol = new String(); String RecentElementCountString = new String("0"); /* * String to be converted to an integer */ int RecentElementCount; if (MF.length() == 0) { return null; } for (int f = 0; f < MF.length(); f++) { ThisChar = MF.charAt(f); if (f < MF.length()) { if (ThisChar >= 'A' && ThisChar <= 'Z') { /* * New Element begins */ RecentElementSymbol = java.lang.String.valueOf(ThisChar); RecentElementCountString = "0"; } if (ThisChar >= 'a' && ThisChar <= 'z') { /* * Two-letter Element continued */ RecentElementSymbol += ThisChar; } if (ThisChar >= '0' && ThisChar <= '9') { /* * Two-letter Element continued */ RecentElementCountString += ThisChar; } } if (f == MF.length() - 1 || (MF.charAt(f + 1) >= 'A' && MF.charAt(f + 1) <= 'Z')) { /* * Here an element symbol as well as its number should have been read completely */ Integer RecentElementCountInteger = new Integer(RecentElementCountString); RecentElementCount = RecentElementCountInteger.intValue(); if (RecentElementCount == 0) { RecentElementCount = 1; } for (int g = 0; g < RecentElementCount; g++) { ac.addAtom(ac.getBuilder().newAtom(RecentElementSymbol)); } } } return ac; } /** * creates a sorted hash map of elementsymbol-count of this ac * * @param ac the atomcontainer to calculate with * @return the hashmap */ private Map getSymolMap(IAtomContainer ac){ String symbol; SortedMap symbols = new TreeMap(); IAtom atom = null; HCount=0; for (int f = 0; f < ac.getAtomCount(); f++) { int hs=0; atom = ac.getAtom(f); symbol = atom.getSymbol(); if(useboth){ } if (atom.getHydrogenCount() > 0) { HCount += atom.getHydrogenCount(); } if (symbols.get(symbol) != null) { symbols.put(symbol, new Integer(((Integer) symbols.get(symbol)).intValue() + 1)); } else { symbols.put(symbol, new Integer(1)); } } if(useboth && symbols.get(H_ELEMENT_SYMBOL)!=null) HCount+=((Integer)symbols.get(H_ELEMENT_SYMBOL)).intValue(); return symbols; } /** * Analyses a set of Nodes that has been changed or recently loaded * and returns a molecular formula * * @param ac Description of the Parameter * @return a string containing the molecular formula. */ public String analyseAtomContainer(IAtomContainer ac) { String symbol; String mf = ""; Map symbols = this.getSymolMap(ac); mf = addSymbolToFormula(symbols, "C", mf); if(useboth){ if (HCount > 0) mf += H_ELEMENT_SYMBOL; if (HCount > 1) { mf += Integer.toString(HCount); } }else{ if (symbols.get(H_ELEMENT_SYMBOL) != null) { mf = addSymbolToFormula(symbols, H_ELEMENT_SYMBOL, mf); } else { if (HCount > 0) { mf += H_ELEMENT_SYMBOL; if (HCount > 1) { mf += Integer.toString(HCount); } } } } mf = addSymbolToFormula(symbols, "N", mf); mf = addSymbolToFormula(symbols, "O", mf); mf = addSymbolToFormula(symbols, "S", mf); mf = addSymbolToFormula(symbols, "P", mf); Iterator it = symbols.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); if (!((String) key).equals("C") && !((String) key).equals(H_ELEMENT_SYMBOL) && !((String) key).equals("N") && !((String) key).equals("O") && !((String) key).equals("S") && !((String) key).equals("P")) { mf = addSymbolToFormula(symbols, (String) key, mf); } } return mf; } /** * Adds an element to a chemical formual string * * @param sm The map containing the elements * @param symbol The symbol to add * @param formula The chemical formula * @return Description of the Return Value */ private String addSymbolToFormula(Map sm, String symbol, String formula) { if (sm.get(symbol) != null) { formula += symbol; if (!sm.get(symbol).equals(new Integer(1))) { formula += sm.get(symbol).toString(); } } return (formula); } /** * Checks a set of Nodes for the occurence of a particular * element. * * @param thisElement Description of the Parameter * @return The number of atoms for the particular element in the formula */ public int getAtomCount(String thisElement) { int atomCount = 0; if (thisElement.equals(H_ELEMENT_SYMBOL) && HCount > 0) { return HCount; } for (int f = 0; f < atomContainer.getAtomCount(); f++) { if (atomContainer.getAtom(f).getSymbol().equals(thisElement)) { atomCount++; } } return atomCount; } /** * Returns a Vector (of Strings) with asorted element names. * The order is determined by ElementComparator. * * @return The elements value * @see ElementComparator */ public List getElements() { TreeSet elements = new TreeSet(new ElementComparator()); for (int f = 0; f < atomContainer.getAtomCount(); f++) { String symbol = atomContainer.getAtom(f).getSymbol(); if (!elements.contains(symbol)) { elements.add(symbol); } } List results = new ArrayList(); Iterator iter = elements.iterator(); while (iter.hasNext()) { results.add((String) iter.next()); } return results; } /** * Returns the number of distinct elements in the formula. * * @return The elementCount value */ public int getElementCount() { return getElements().size(); } /** * Gets a Molecule and an array of element symbols. Counts how many of each of these elements * the molecule contains. Then it returns the elements followed by their number as a string, * i.e. C15H8N3. * * @param mol The Molecule to be searched * @param elements Description of the Parameter * @return The element formula as a string */ public static String generateElementFormula(IMolecule mol, String[] elements) { int num = elements.length; StringBuffer formula = new StringBuffer(); int[] elementCount = new int[num]; for (int i = 0; i < mol.getAtomCount(); i++) { for (int j = 0; j < num; j++) { if (elements[j].equals(mol.getAtom(i).getSymbol())) { elementCount[j]++; } } } for (int i = 0; i < num; i++) { formula.append(elements[i] + elementCount[i]); } return formula.toString(); } /** * Builds the elemental formula of a given molecule as a Hashtable. * Keys are the elemental symbols (Strings) and values are the no. of occurrence (Integer objects). * * @return a Hashtable, keys are the elemental symbols and values are their no. */ public Map getFormulaHashtable() { Map formula = new HashMap(); List elements = this.getElements(); for (int i = 0; i < elements.size(); i++) { Integer numOfAtom = new Integer(this.getAtomCount((String) elements.get(i))); formula.put(elements.get(i), numOfAtom); } return formula; } /** * * Returns the string representation of the molecule formula with * numbers wrapped in <sub></sub> tags and the total * charge of AtomContainer in <sup></sup> tags * Useful for displaying formulae in Swing components or on the web. * * @return The html-string representation of the sum formula with charge * @see #getHTMLMolecularFormula() */ public String getHTMLMolecularFormulaWithCharge() { String formula = new MFAnalyser(atomContainer,useboth).getHTMLMolecularFormula(); int charge = AtomContainerManipulator.getTotalFormalCharge(atomContainer); if (charge == 0) { return formula; } else if (charge < 0) { return formula + "<sup>" + charge * -1 + "-" + "</sup>"; } else { return formula + "<sup>" + charge +"+" + "</sup>"; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -