KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > biology > AminoAcids


1 package JSci.biology;
2
3 import java.lang.Double JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import javax.xml.parsers.*;
9 import org.w3c.dom.*;
10 import org.xml.sax.SAXException JavaDoc;
11 import JSci.chemistry.Molecule;
12
13 /**
14  * This class provides access to amino acids.
15  * @version 1.0
16  * @author Silvere Martin-Michiellot
17  * @author Mark Hale
18  */

19 public final class AminoAcids {
20         private static final Map JavaDoc table = new HashMap JavaDoc();
21         private static Map JavaDoc symbols;
22
23         private AminoAcids() {}
24         /**
25         * Returns the name for a symbol.
26         */

27         public static String JavaDoc getName(String JavaDoc symbol) {
28                 if(symbols == null)
29                         symbols = loadIndex();
30                 return (String JavaDoc)symbols.get(symbol);
31         }
32     /**
33      * Returns an amino acid.
34      */

35     public static AminoAcid getAminoAcid(String JavaDoc 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         /**
46         * Loads the XML index.
47         */

48         private static Map JavaDoc 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 JavaDoc index = new HashMap JavaDoc();
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 JavaDoc e) {
67                         return Collections.EMPTY_MAP;
68                 } catch(SAXException JavaDoc e) {
69                         return Collections.EMPTY_MAP;
70                 }
71         }
72     /**
73      * Loads an amino acid from its XML resource.
74      */

75     private static AminoAcid loadAminoAcid(String JavaDoc 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 JavaDoc name=findStringValue(nl,"name");
83             String JavaDoc abbreviation=findStringValue(nl,"abbreviation");
84             String JavaDoc symbol=findStringValue(nl,"symbol");
85             String JavaDoc 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 JavaDoc e) {
94                         return null;
95                 } catch(SAXException JavaDoc e) {
96                         return null;
97         }
98     }
99     private static String JavaDoc findStringValue(NodeList nl,String JavaDoc 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 JavaDoc 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