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 * Created on Aug 30, 2011
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package org.biojava.nbio.structure.scop;
026
027import org.biojava.nbio.structure.align.client.JFatCatClient;
028import org.biojava.nbio.structure.align.util.URLConnectionTools;
029import org.biojava.nbio.structure.scop.server.ScopDescriptions;
030import org.biojava.nbio.structure.scop.server.ScopDomains;
031import org.biojava.nbio.structure.scop.server.ScopNodes;
032import org.biojava.nbio.structure.scop.server.XMLUtil;
033
034import java.io.IOException;
035import java.io.InputStream;
036import java.net.URL;
037import java.util.List;
038
039
040/** A class that fetches information about SCOP from a remote data-source. It requires port 80 to open for HTTP connection.
041 *
042 * @author Andreas Prlic
043 *
044 */
045public class RemoteScopInstallation implements ScopDatabase {
046
047        public static final String DEFAULT_SERVER = "http://source.rcsb.org/jfatcatserver/domains/";
048
049        String server = DEFAULT_SERVER;
050
051        private String version = null;
052
053        public static void main(String[] args){
054
055                ScopDatabase scop = new RemoteScopInstallation();
056                ScopFactory.setScopDatabase(scop);
057
058                //System.out.println(scop.getByCategory(ScopCategory.Superfamily));
059
060                System.out.println(scop.getDomainsForPDB("4HHB"));
061        }
062
063
064        public String getServer() {
065                return server;
066        }
067
068        public void setServer(String server) {
069                this.server = server;
070        }
071
072        @Override
073        public List<ScopDescription> getByCategory(ScopCategory category) {
074                List<ScopDescription> results = null;
075                try {
076                        URL u = new URL(server + "getByCategory?category="+category+"&version="+getScopVersion());
077                        InputStream response = URLConnectionTools.getInputStream(u);
078                        String xml = JFatCatClient.convertStreamToString(response);
079
080                        if(! xml.trim().isEmpty()) {
081                                ScopDescriptions container = ScopDescriptions.fromXML(xml);
082                                results = container.getScopDescription();
083                        }
084                } catch (IOException e) {
085                        throw new RuntimeException("Unable to reach "+ server + "getByCategory?category="+category+"&version="+getScopVersion(), e);
086                }
087                return results;
088        }
089
090        @Override
091        public List<ScopDescription> filterByClassificationId(String query) {
092                List<ScopDescription> results = null;
093                try {
094                        URL u = new URL(server + "filterByClassificationId?query="+query+"&version="+getScopVersion());
095                        InputStream response = URLConnectionTools.getInputStream(u);
096                        String xml = JFatCatClient.convertStreamToString(response);
097
098                        if(! xml.trim().isEmpty()) {
099                                ScopDescriptions container = ScopDescriptions.fromXML(xml);
100                                results = container.getScopDescription();
101                        }
102                } catch (Exception e){
103                        throw new RuntimeException("Unable to reach "+ server + "filterByClassificationId?query="+query+"&version="+getScopVersion(), e);
104                }
105                return results;
106        }
107
108        @Override
109        public List<ScopNode> getTree(ScopDomain domain) {
110                List<ScopNode> results = null;
111                try {
112                        URL u = new URL(server + "getTree?scopId="+domain.getScopId()+"&version="+getScopVersion());
113                        InputStream response = URLConnectionTools.getInputStream(u);
114                        String xml = JFatCatClient.convertStreamToString(response);
115
116                        if(! xml.trim().isEmpty()) {
117                                ScopNodes container = ScopNodes.fromXML(xml);
118                                results = container.getScopNode();
119                        }
120                } catch (Exception e){
121                        throw new RuntimeException("Unable to reach "+ server + "getTree?scopId="+domain.getScopId()+"&version="+getScopVersion(), e);
122                }
123                return results;
124        }
125
126        @Override
127        public List<ScopDomain> filterByDomainName(String query) {
128                query = query.trim();
129                List<ScopDomain> results = null;
130                try {
131                        URL u = new URL(server + "filterByDomainName?query="+query+"&version="+getScopVersion());
132                        //System.out.println(u);
133                        InputStream response = URLConnectionTools.getInputStream(u);
134                        String xml = JFatCatClient.convertStreamToString(response);
135
136                        if(! xml.trim().isEmpty()) {
137                                ScopDomains container = ScopDomains.fromXML(xml);
138                                results = container.getScopDomain();
139                        }
140                } catch (Exception e){
141                        throw new RuntimeException("Unable to reach "+ server + "filterByDomainName?query="+query+"&version="+getScopVersion(), e);
142                }
143                return results;
144        }
145
146        @Override
147        public List<ScopDescription> filterByDescription(String query) {
148                List<ScopDescription> results = null;
149                try {
150                        URL u = new URL(server + "filterByDescription?query="+query+"&version="+getScopVersion());
151                        InputStream response = URLConnectionTools.getInputStream(u);
152                        String xml = JFatCatClient.convertStreamToString(response);
153
154                        if(! xml.trim().isEmpty()) {
155                                ScopDescriptions container = ScopDescriptions.fromXML(xml);
156                                results = container.getScopDescription();
157                        }
158                } catch (Exception e){
159                        throw new RuntimeException("Unable to reach "+ server + "filterByDescription?query="+query+"&version="+getScopVersion(), e);
160                }
161                return results;
162        }
163
164        @Override
165        public ScopDescription getScopDescriptionBySunid(int sunid) {
166
167                ScopDescription desc = null;
168
169
170                try {
171
172                        URL u = new URL(server + "getScopDescriptionBySunid?sunid="+sunid+"&version="+getScopVersion());
173                        InputStream response = URLConnectionTools.getInputStream(u);
174                        String xml = JFatCatClient.convertStreamToString(response);
175
176                        if(! xml.trim().isEmpty()) {
177                                desc = XMLUtil.getScopDescriptionFromXML(xml);
178                        }
179
180                } catch (Exception e){
181                        throw new RuntimeException("Unable to reach "+ server + "getScopDescriptionBySunid?sunid="+sunid+"&version="+getScopVersion(), e);
182                }
183                return desc;
184        }
185
186        @Override
187        public List<ScopDomain> getDomainsForPDB(String pdbId) {
188                List<ScopDomain> results = null;
189                try {
190                        URL u = new URL(server + "getDomainsForPDB?pdbId="+pdbId+"&version="+getScopVersion());
191                        InputStream response = URLConnectionTools.getInputStream(u);
192                        String xml = JFatCatClient.convertStreamToString(response);
193
194                        if( !xml.trim().isEmpty()) {
195                                ScopDomains container = ScopDomains.fromXML(xml);
196                                results = container.getScopDomain();
197                        }
198                } catch (Exception e){
199                        throw new RuntimeException("Unable to reach "+ server + "getDomainsForPDB?pdbId="+pdbId+"&version="+getScopVersion(), e);
200                }
201                return results;
202        }
203
204        private ScopDomain requestRemoteDomainByScopID(String scopId)
205                        throws IOException{
206                scopId = scopId.trim();
207                URL u = new URL(server + "getDomainByScopID?scopId="+scopId+"&version="+getScopVersion());
208                InputStream response = URLConnectionTools.getInputStream(u);
209                String xml = JFatCatClient.convertStreamToString(response);
210
211                if( !xml.trim().isEmpty()) {
212                        return XMLUtil.getScopDomainFromXML(xml);
213                }
214                return null;
215        }
216
217        @Override
218        public ScopDomain getDomainByScopID(String scopId) {
219                try {
220                        return requestRemoteDomainByScopID(scopId);
221                } catch (Exception e){
222                        throw new RuntimeException("Unable to reach "+ server + "getDomainByScopID?scopId="+scopId+"&version="+getScopVersion(), e);
223                }
224        }
225
226        @Override
227        public ScopNode getScopNode(int sunid) {
228                ScopNode desc = null;
229                try {
230                        URL u = new URL(server + "getScopNode?sunid="+sunid+"&version="+getScopVersion());
231                        InputStream response = URLConnectionTools.getInputStream(u);
232                        String xml = JFatCatClient.convertStreamToString(response);
233
234                        if( !xml.trim().isEmpty()) {
235                                desc = XMLUtil.getScopNodeFromXML(xml);
236                        }
237                } catch (Exception e){
238                        throw new RuntimeException("Unable to reach "+ server + "getScopNode?sunid="+sunid+"&version="+getScopVersion(), e);
239                }
240                return desc;
241        }
242
243        @Override
244        public String getScopVersion() {
245                // If no version is set, request the default version from the website
246                if( version == null) {
247                        try {
248                                URL u = new URL(server + "getScopVersion");
249                                InputStream response = URLConnectionTools.getInputStream(u);
250                                version = JFatCatClient.convertStreamToString(response);
251                                if( version != null)
252                                        version = version.trim();
253
254                        } catch (Exception e){
255                                throw new RuntimeException("Unable to reach "+ server + "getScopVersion", e);
256                        }
257                }
258                return version;
259        }
260
261        @Override
262        public void setScopVersion(String version) {
263                this.version = version;
264        }
265
266        @Override
267        public List<ScopDomain> getScopDomainsBySunid(Integer sunid) {
268                List<ScopDomain> results = null;
269                try {
270                        URL u = new URL(server + "getScopDomainsBySunid?sunid="+sunid+"&version="+getScopVersion());
271                        InputStream response = URLConnectionTools.getInputStream(u);
272                        String xml = JFatCatClient.convertStreamToString(response);
273
274                        if( !xml.trim().isEmpty()) {
275                                ScopDomains container = ScopDomains.fromXML(xml);
276                                results = container.getScopDomain();
277                        }
278                } catch (Exception e){
279                        throw new RuntimeException("Unable to reach "+ server + "getScopDomainsBySunid?sunid="+sunid+"&version="+getScopVersion(), e);
280                }
281                return results;
282        }
283
284
285        @Override
286        public List<String> getComments(int sunid) {
287                List<String> results = null;
288                try {
289                        URL u = new URL(server + "getComments?sunid="+sunid+"&version="+getScopVersion());
290                        InputStream response = URLConnectionTools.getInputStream(u);
291                        String xml = JFatCatClient.convertStreamToString(response);
292
293                        if( !xml.trim().isEmpty()) {
294                                results = XMLUtil.getCommentsFromXML(xml);
295                        }
296                } catch (Exception e){
297                        throw new RuntimeException("Unable to reach "+ server + "getComments?sunid="+sunid+"&version="+getScopVersion(), e);
298                }
299                return results;
300        }
301
302}