1 package JSci.biology; 2 3 import java.lang.Double ; 4 import java.io.IOException ; 5 import java.util.Collections ; 6 import java.util.Map ; 7 import java.util.HashMap ; 8 import javax.xml.parsers.*; 9 import org.w3c.dom.*; 10 import org.xml.sax.SAXException ; 11 import JSci.chemistry.Molecule; 12 13 19 public final class AminoAcids { 20 private static final Map table = new HashMap (); 21 private static Map symbols; 22 23 private AminoAcids() {} 24 27 public static String getName(String symbol) { 28 if(symbols == null) 29 symbols = loadIndex(); 30 return (String )symbols.get(symbol); 31 } 32 35 public static AminoAcid getAminoAcid(String name) { 36 name=name.toLowerCase(); 37 AminoAcid aminoacid = (AminoAcid) table.get(name); 38 if(aminoacid==null) { 39 aminoacid=loadAminoAcid("aminoacids/"+name.replace(' ', '-')+".xml"); 40 if(aminoacid != null) 41 table.put(name,aminoacid); 42 } 43 return aminoacid; 44 } 45 48 private static Map loadIndex() { 49 DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance(); 50 try { 51 DocumentBuilder docBuilder=docFactory.newDocumentBuilder(); 52 Document doc=docBuilder.parse(AminoAcids.class.getResource("aminoacids/index.xml").toString()); 53 Node root=doc.getDocumentElement(); 54 NodeList nl=root.getChildNodes(); 55 Map index = new HashMap (); 56 for(int i=0; i<nl.getLength(); i++) { 57 Node node = nl.item(i); 58 if(node.getNodeName().equals("amino-acid")) { 59 NamedNodeMap attr = node.getAttributes(); 60 index.put(attr.getNamedItem("symbol").getNodeValue(), attr.getNamedItem("name").getNodeValue()); 61 } 62 } 63 return index; 64 } catch(ParserConfigurationException e) { 65 return Collections.EMPTY_MAP; 66 } catch(IOException e) { 67 return Collections.EMPTY_MAP; 68 } catch(SAXException e) { 69 return Collections.EMPTY_MAP; 70 } 71 } 72 75 private static AminoAcid loadAminoAcid(String resname) { 76 DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance(); 77 try { 78 DocumentBuilder docBuilder=docFactory.newDocumentBuilder(); 79 Document doc=docBuilder.parse(AminoAcids.class.getResource(resname).toString()); 80 Node root=doc.getDocumentElement(); 81 NodeList nl=root.getChildNodes(); 82 String name=findStringValue(nl,"name"); 83 String abbreviation=findStringValue(nl,"abbreviation"); 84 String symbol=findStringValue(nl,"symbol"); 85 String formula=findStringValue(nl,"molecular-formula"); 86 AminoAcid aminoacid=new AminoAcid(name, abbreviation, symbol, formula); 87 aminoacid.setMolecularWeight(findDoubleValue(nl,"molecular-weight")); 88 aminoacid.setIsoelectricPoint(findDoubleValue(nl,"isoelectric-point")); 89 aminoacid.setCASRegistryNumber(findStringValue(nl,"CAS-registry-number")); 90 return aminoacid; 91 } catch(ParserConfigurationException e) { 92 return null; 93 } catch(IOException e) { 94 return null; 95 } catch(SAXException e) { 96 return null; 97 } 98 } 99 private static String findStringValue(NodeList nl,String name) { 100 Node item; 101 for(int i=0;i<nl.getLength();i++) { 102 item=nl.item(i); 103 if(item.getNodeName().equals(name)) 104 return item.getFirstChild().getNodeValue(); 105 } 106 return ""; 107 } 108 private static double findDoubleValue(NodeList nl,String name) { 109 Node item; 110 for(int i=0;i<nl.getLength();i++) { 111 item=nl.item(i); 112 if(item.getNodeName().equals(name)) 113 return Double.parseDouble(item.getFirstChild().getNodeValue()); 114 } 115 return Double.NaN; 116 } 117 } 118 | Popular Tags |