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.align.client.JFatCatClient;
024import org.biojava.nbio.structure.align.util.HTTPConnectionTools;
025import org.biojava.nbio.structure.io.mmcif.model.*;
026import org.w3c.dom.Document;
027import org.w3c.dom.NamedNodeMap;
028import org.w3c.dom.Node;
029import org.w3c.dom.NodeList;
030import org.xml.sax.InputSource;
031
032import javax.xml.parsers.DocumentBuilder;
033import javax.xml.parsers.DocumentBuilderFactory;
034import java.io.InputStream;
035import java.io.StringReader;
036import java.net.URL;
037import java.util.List;
038
039/** A BioUnitDataProvider that fetches the symmetry operations via remote calls to servers from RCSB PDB
040 *
041 * @author Andreas Prlic
042 *
043 */
044public class RemoteRawBioUnitDataProvider implements RawBioUnitDataProvider {
045
046        String pdbId;
047
048        public static String DEFAULT_SERVERNAME = "http://pepper.rcsb.org:8080/pdb/rest/biolassembly/";
049
050        public static String NR_BIOL_APPEND = "nrBiolAssemblies?structureId=%s";
051
052        public static String GET_ASSEMBLY =  "pdbxStructAssemblies?structureId=%s";
053
054        public static String GET_ASSEMBLY_GENS =  "pdbxStructAssemblyGens?structureId=%s";
055
056        public static String GET_STRUCT_OPER = "pdbxStructOperList?structureId=%s";
057
058        String serverName;
059        private static final int DEFAULT_TIMEOUT = 5000;
060
061        int timeout;
062        public RemoteRawBioUnitDataProvider(){
063                serverName = DEFAULT_SERVERNAME;
064                timeout = DEFAULT_TIMEOUT;
065        }
066
067        public static void main(String[] args){
068
069                RemoteRawBioUnitDataProvider me = new RemoteRawBioUnitDataProvider();
070
071                me.setPdbId("4hhb");
072
073                System.out.println("Nr biol assemblies: " + me.getNrBiolAssemblies());
074                System.out.println("has biol assembly:" + me.hasBiolAssembly());
075                System.out.println("assemblies: " + me.getPdbxStructAssemblies());
076                System.out.println("assemblygens:" + me.getPdbxStructAssemblyGens());
077                System.out.println("operations:" + me.getPdbxStructOperList());
078
079
080        }
081
082
083        public int getTimeout() {
084                return timeout;
085        }
086
087
088
089
090        public void setTimeout(int timeout) {
091                this.timeout = timeout;
092        }
093
094
095
096
097        @Override
098        public void setPdbId(String pdbId) {
099                this.pdbId = pdbId;
100
101        }
102
103
104
105        @Override
106        public int getNrBiolAssemblies() {
107
108                String serverURL = serverName + NR_BIOL_APPEND;
109                int nrBiolAssemblies = -1;
110                try {
111                        String u = String.format(serverURL,pdbId) ;
112
113
114                        URL url = new URL(u);
115                        System.out.println("requesting nr biol assemblies from server..."  + url);
116                        // have a short timeout for this...
117                        // 5 sec
118                        InputStream stream = HTTPConnectionTools.getInputStream(url,timeout);
119
120                        String xml = null;
121
122                        if ( stream != null) {
123
124                                xml = JFatCatClient.convertStreamToString(stream);
125                                System.out.println("got XML from server: " + xml);
126
127                                nrBiolAssemblies = extractNrBiolAssemblies(xml);
128                        }
129                } catch (Exception e){
130                        e.printStackTrace();
131                }
132                return nrBiolAssemblies;
133        }
134
135        private static int extractNrBiolAssemblies(String xml) {
136                int nrBiolAssemblies = -1;
137
138                try {
139                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
140                        DocumentBuilder db = factory.newDocumentBuilder();
141                        InputSource inStream = new InputSource();
142                        inStream.setCharacterStream(new StringReader(xml));
143                        Document doc = db.parse(inStream);
144
145                        // normalize text representation
146                        doc.getDocumentElement().normalize();
147
148
149                        //Element rootElement = doc.getDocumentElement();
150
151                        NodeList listOfPairs = doc.getElementsByTagName("nrBiolAssemblies");
152                        //int numArrays = listOfArrays.getLength();
153
154                        // go over the blocks
155                        for(int i=0; i<listOfPairs.getLength() ; i++)
156                        {
157                                Node pair       = listOfPairs.item(i);
158                                //NodeList valList = pair.getChildNodes();
159                                //int numChildren  = valList.getLength();
160
161                                NamedNodeMap map = pair.getAttributes();
162
163                                String count =  map.getNamedItem("count").getTextContent();
164                                nrBiolAssemblies = Integer.parseInt(count);
165                        }
166
167                } catch (Exception e){
168                        e.printStackTrace();
169                }
170                return nrBiolAssemblies;
171        }
172
173        @Override
174        public boolean hasBiolAssembly() {
175                int nrBiolAssemblies =  getNrBiolAssemblies();
176                if ( nrBiolAssemblies > 0)
177                        return true;
178                return false;
179        }
180
181        @Override
182        public PdbxStructAssembly getPdbxStructAssembly(int biolAssemblyNr) {
183                PdbxStructAssembly pdbxStructAssembly = null;
184
185                return pdbxStructAssembly;
186        }
187
188        @Override
189        public List<PdbxStructAssemblyGen> getPdbxStructAssemblyGen(int biolAssemblyNr) {
190
191                List<PdbxStructAssemblyGen> pdbxStructAssemblyGen = null;
192
193                return pdbxStructAssemblyGen;
194        }
195        @Override
196        public List<PdbxStructAssembly> getPdbxStructAssemblies() {
197                String serverURL = serverName + GET_ASSEMBLY;
198                List<PdbxStructAssembly> assemblies = null;
199                try {
200                        String u = String.format(serverURL,pdbId) ;
201
202
203                        URL url = new URL(u);
204                        System.out.println("requesting biol assemblies from server..."  + url);
205                        // have a short timeout for this...
206                        // 5 sec
207                        InputStream stream = HTTPConnectionTools.getInputStream(url,timeout);
208
209                        String xml = null;
210
211                        if ( stream != null) {
212
213                                xml = JFatCatClient.convertStreamToString(stream);
214                                System.out.println("got XML from server: " + xml);
215                                PdbxStructAssemblyXMLContainer tmp = PdbxStructAssemblyXMLContainer.fromXML(xml);
216                                assemblies = tmp.getPdbxStructAssemblies();
217                                //pdbxStructAssembly = extractNrBiolAssemblies(xml);
218                        }
219                } catch (Exception e){
220                        e.printStackTrace();
221                }
222                return assemblies;
223        }
224
225        @Override
226        public List<PdbxStructAssemblyGen> getPdbxStructAssemblyGens() {
227                String serverURL = serverName + GET_ASSEMBLY_GENS;
228                List<PdbxStructAssemblyGen> assemblies = null;
229                try {
230                        String u = String.format(serverURL,pdbId) ;
231
232
233                        URL url = new URL(u);
234                        System.out.println("requesting  biol assembly gens from server..."  + url);
235                        // have a short timeout for this...
236                        // 5 sec
237                        InputStream stream = HTTPConnectionTools.getInputStream(url,timeout);
238
239                        String xml = null;
240
241                        if ( stream != null) {
242
243                                xml = JFatCatClient.convertStreamToString(stream);
244                                System.out.println("got XML from server: " + xml);
245                                PdbxStructAssemblyGenXMLContainer tmp = PdbxStructAssemblyGenXMLContainer.fromXML(xml);
246                                assemblies = tmp.getPdbxStructAssemblyGens();
247                                //pdbxStructAssembly = extractNrBiolAssemblies(xml);
248                        }
249                } catch (Exception e){
250                        e.printStackTrace();
251                }
252
253                return assemblies;
254
255        }
256
257        @Override
258        public List<PdbxStructOperList> getPdbxStructOperList() {
259                String serverURL = serverName + GET_STRUCT_OPER;
260                List<PdbxStructOperList> oper = null;
261                try {
262                        String u = String.format(serverURL,pdbId) ;
263
264
265                        URL url = new URL(u);
266                        System.out.println("requesting operators from server..."  + url);
267                        // have a short timeout for this...
268                        // 5 sec
269                        InputStream stream = HTTPConnectionTools.getInputStream(url,timeout);
270
271                        String xml = null;
272
273                        if ( stream != null) {
274
275                                xml = JFatCatClient.convertStreamToString(stream);
276                                System.out.println("got XML from server: " + xml);
277                                PdbxStructOperListXMLContainer container =  PdbxStructOperListXMLContainer.fromXML(xml);
278                                oper = container.getPdbxStructOperLists();
279                        }
280                } catch (Exception e){
281                        e.printStackTrace();
282                }
283
284                return oper;
285        }
286
287}