001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.quaternary.io;
022
023import org.biojava.nbio.structure.Structure;
024import org.biojava.nbio.structure.align.client.JFatCatClient;
025import org.biojava.nbio.structure.align.util.AtomCache;
026import org.biojava.nbio.structure.align.util.HTTPConnectionTools;
027import org.biojava.nbio.structure.quaternary.BiologicalAssemblyTransformation;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.w3c.dom.Document;
031import org.w3c.dom.NamedNodeMap;
032import org.w3c.dom.Node;
033import org.w3c.dom.NodeList;
034import org.xml.sax.InputSource;
035import org.xml.sax.SAXException;
036
037import javax.xml.parsers.DocumentBuilder;
038import javax.xml.parsers.DocumentBuilderFactory;
039import javax.xml.parsers.ParserConfigurationException;
040import java.io.IOException;
041import java.io.InputStream;
042import java.io.StringReader;
043import java.net.URL;
044import java.util.ArrayList;
045import java.util.List;
046
047public class RemoteBioUnitDataProvider implements BioUnitDataProvider {
048
049
050        private static final Logger logger = LoggerFactory.getLogger(RemoteBioUnitDataProvider.class);
051
052        public static final String DEFAULT_SERVERNAME = "http://pepper.rcsb.org:8080/pdb/rest/biolassembly/";
053
054        public static final String NR_BIOL_APPEND = "nrBiolAssemblies?structureId=%s";
055
056        public static final String BIO_ASSEMBLY = "bioAssembly?structureId=%s&nr=%s";
057
058        private static final int DEFAULT_TIMEOUT = 5000;
059
060        private String serverName;
061
062        private int timeout;
063
064        public RemoteBioUnitDataProvider(){
065                serverName = DEFAULT_SERVERNAME;
066                timeout = DEFAULT_TIMEOUT;
067        }
068
069        @Override
070        public List<BiologicalAssemblyTransformation> getBioUnitTransformationList(
071                        String pdbId, int biolAssemblyNr) {
072
073                String serverURL = serverName + BIO_ASSEMBLY;
074
075                String u = String.format(serverURL,pdbId, biolAssemblyNr) ;
076                List<BiologicalAssemblyTransformation> transformations = new ArrayList<BiologicalAssemblyTransformation>();
077
078                try {
079                        URL url = new URL(u);
080                        System.out.println("requesting biol assemblies from server..."  + url);
081                        // have a short timeout for this...
082                        // 5 sec
083                        InputStream stream = HTTPConnectionTools.getInputStream(url,timeout);
084
085                        String xml = null;
086
087
088                        xml = JFatCatClient.convertStreamToString(stream);
089
090                        if ( stream != null) {
091
092                                System.out.println(xml);
093                                transformations = BiologicalAssemblyTransformation.fromMultiXML(xml);
094                        }
095                } catch (IOException e){
096                        logger.error("Exception caught while getting biological assemblies",e);
097                } catch (SAXException e) {
098                        logger.error("Exception caught while getting biological assemblies",e);
099                } catch (ParserConfigurationException e) {
100                        logger.error("Exception caught while getting biological assemblies",e);
101                }
102
103                return transformations;
104
105        }
106
107        @Override
108        public int getNrBiolAssemblies(String pdbId) {
109                String serverURL = serverName + NR_BIOL_APPEND;
110                int nrBiolAssemblies = -1;
111                try {
112                        String u = String.format(serverURL,pdbId) ;
113
114
115                        URL url = new URL(u);
116                        System.out.println("requesting nr biol assemblies from server..."  + url);
117                        // have a short timeout for this...
118                        // 5 sec
119                        InputStream stream = HTTPConnectionTools.getInputStream(url,timeout);
120
121                        String xml = null;
122
123                        if ( stream != null) {
124
125                                xml = JFatCatClient.convertStreamToString(stream);
126                                System.out.println("got XML from server: " + xml);
127
128                                nrBiolAssemblies = extractNrBiolAssemblies(xml);
129                        }
130                } catch (IOException e){
131                        logger.error("Exception caught while getting number of biological assemblies",e);
132                }
133                return nrBiolAssemblies;
134        }
135
136        @Override
137        public boolean hasBiolAssembly(String pdbId) {
138                int nrBiolAssemblies =  getNrBiolAssemblies(pdbId);
139                if ( nrBiolAssemblies > 0)
140                        return true;
141                return false;
142        }
143
144        private static int extractNrBiolAssemblies(String xml) {
145                int nrBiolAssemblies = -1;
146
147                try {
148                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
149                        DocumentBuilder db = factory.newDocumentBuilder();
150                        InputSource inStream = new InputSource();
151                        inStream.setCharacterStream(new StringReader(xml));
152                        Document doc = db.parse(inStream);
153
154                        // normalize text representation
155                        doc.getDocumentElement().normalize();
156
157
158                        //Element rootElement = doc.getDocumentElement();
159
160                        NodeList listOfPairs = doc.getElementsByTagName("nrBiolAssemblies");
161                        //int numArrays = listOfArrays.getLength();
162
163                        // go over the blocks
164                        for(int i=0; i<listOfPairs.getLength() ; i++)
165                        {
166                                Node pair       = listOfPairs.item(i);
167                                //NodeList valList = pair.getChildNodes();
168                                //int numChildren  = valList.getLength();
169
170                                NamedNodeMap map = pair.getAttributes();
171
172                                String count =  map.getNamedItem("count").getTextContent();
173                                nrBiolAssemblies = Integer.parseInt(count);
174                        }
175
176                } catch (IOException e){
177                        logger.error("Exception caught while getting number of biological assemblies",e);
178                } catch (SAXException e) {
179                        logger.error("Exception caught while getting number of biological assemblies",e);
180                } catch (ParserConfigurationException e) {
181                        logger.error("Exception caught while getting number of biological assemblies",e);
182                }
183                return nrBiolAssemblies;
184        }
185
186        @Override
187        public Structure getAsymUnit(String pdbId) {
188                logger.error("RemoteBioUnitDataProvider getAsymUnit Not implemented yet!");
189                return null;
190
191
192        }
193
194        @Override
195        public void setAsymUnit(Structure s){
196                // nothing to be done here so far..
197        }
198
199        @Override
200        public void setAtomCache(AtomCache cache) {
201                // TODO Auto-generated method stub
202
203        }
204
205        @Override
206        public AtomCache getAtomCache() {
207                // TODO Auto-generated method stub
208                return null;
209        }
210
211}