0.9916 beta
[jalview.git] / forester / java / src / org / forester / development / HmmerRest.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.development;
27
28 import java.io.BufferedReader;
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.PrintStream;
33 import java.net.URL;
34 import java.net.URLConnection;
35
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38 import javax.xml.parsers.ParserConfigurationException;
39
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.NodeList;
43 import org.xml.sax.SAXException;
44
45 public class HmmerRest {
46
47     final static String         LIST_SEPARATOR = "%0A";
48     final static String         LINE_SEPARATOR = "\n";
49     private final static String BASE_URL       = "http://pfam.sanger.ac.uk/search/sequence";
50
51     public static void main( final String[] args ) {
52         final String seq = "MASTENNEKDNFMRDTASRSKKSRRRSLWIAAGAVPTAIALSLSLASPA"
53                 + "AVAQSSFGSSDIIDSGVLDSITRGLTDYLTPRDEALPAGEVTYPAIEGLP"
54                 + "AGVRVNSAEYVTSHHVVLSIQSAAMPERPIKVQLLLPRDWYSSPDRDFPE"
55                 + "IWALDGLRAIEKQSGWTIETNIEQFFADKNAIVVLPVGGESSFYTDWNEP"
56                 + "NNGKNYQWETFLTEELAPILDKGFRSNGERAITGISMGGTAAVNIATHNP"
57                 + "EMFNFVGSFSGYLDTTSNGMPAAIGAALADAGGYNVNAMWGPAGSERWLE"
58                 + "NDPKRNVDQLRGKQVYVSAGSGADDYGQDGSVATGPANAAGVGLELISRM"
59                 + "TSQTFVDAANGAGVNVIANFRPSGVHAWPYWQFEMTQAWPYMADSLGMSR"
60                 + "EDRGADCVALGAIADATADGSLGSCLNNEYLVANGVGRAQDFTNGRAYWS"
61                 + "PNTGAFGLFGRINARYSELGGPDSWLGFPKTRELSTPDGRGRYVHFENGS"
62                 + "IYWSAATGPWEIPGDMFTAWGTQGYEAGGLGYPVGPAKDFNGGLAQEFQG"
63                 + "GYVLRTPQNRAYWVRGAISAKYMEPGVATTLGFPTGNERLIPGGAFQEFT"
64                 + "NGNIYWSASTGAHYILRGGIFDAWGAKGYEQGEYGWPTTDQTSIAAGGET" + "ITFQNGTIRQVNGRIEESR";
65         final String query = "seq=" + seq + "" + "&" + "output=xml";
66         String result = "";
67         try {
68             result = getResult( query );
69         }
70         catch ( final IOException e ) {
71             // TODO Auto-generated catch block
72             e.printStackTrace();
73         }
74         System.out.println( result );
75         final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
76         Document dom = null;
77         try {
78             //Using factory get an instance of document builder
79             final DocumentBuilder db = dbf.newDocumentBuilder();
80             //parse using builder to get DOM representation of the XML file
81             dom = db.parse( new ByteArrayInputStream( result.getBytes() ) );
82         }
83         catch ( final ParserConfigurationException pce ) {
84             pce.printStackTrace();
85         }
86         catch ( final SAXException se ) {
87             se.printStackTrace();
88         }
89         catch ( final IOException ioe ) {
90             ioe.printStackTrace();
91         }
92         final Element docEle = dom.getDocumentElement();
93         final NodeList nl = docEle.getElementsByTagName( "job" );
94         String result_url = "";
95         for( int i = 0; i < nl.getLength(); i++ ) {
96             //System.out.println( nl.item( i ) );
97             result_url = getTextValue( ( Element ) nl.item( i ), "result_url" );
98         }
99         System.out.println( "result url = " + result_url );
100         //gettin the result....
101         try {
102             //do what you want to do before sleeping
103             Thread.sleep( 5000 );//sleep for x ms
104             //do what you want to do after sleeptig
105         }
106         catch ( final InterruptedException ie ) {
107             ie.printStackTrace();
108         }
109         try {
110             result = getResult( result_url, "" );
111         }
112         catch ( final IOException e ) {
113             // TODO Auto-generated catch block
114             e.printStackTrace();
115         }
116         System.out.println( result );
117     }
118
119     private static String getTextValue( final Element ele, final String tagName ) {
120         String textVal = null;
121         final NodeList nl = ele.getElementsByTagName( tagName );
122         if ( ( nl != null ) && ( nl.getLength() > 0 ) ) {
123             final Element el = ( Element ) nl.item( 0 );
124             textVal = el.getFirstChild().getNodeValue();
125         }
126         return textVal;
127     }
128
129     public static String getResult( final String base_url, final String query ) throws IOException {
130         System.out.println( query );
131         final URL url = new URL( base_url );
132         final URLConnection urlc = url.openConnection();
133         urlc.setDoOutput( true );
134         urlc.setAllowUserInteraction( false );
135         final PrintStream ps = new PrintStream( urlc.getOutputStream() );
136         //System.out.println( "query: " + query );
137         ps.print( query.trim() );
138         ps.close();
139         final BufferedReader br = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
140         final StringBuffer sb = new StringBuffer();
141         String line = null;
142         while ( ( line = br.readLine() ) != null ) {
143             sb.append( line + LINE_SEPARATOR );
144         }
145         br.close();
146         return sb.toString().trim();
147     }
148
149     public static String getResult( final String query ) throws IOException {
150         System.out.println( query );
151         final URL url = new URL( BASE_URL );
152         final URLConnection urlc = url.openConnection();
153         urlc.setDoOutput( true );
154         urlc.setAllowUserInteraction( false );
155         final PrintStream ps = new PrintStream( urlc.getOutputStream() );
156         //System.out.println( "query: " + query );
157         ps.print( query.trim() );
158         ps.close();
159         final BufferedReader br = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
160         final StringBuffer sb = new StringBuffer();
161         String line = null;
162         while ( ( line = br.readLine() ) != null ) {
163             sb.append( line + LINE_SEPARATOR );
164         }
165         br.close();
166         return sb.toString().trim();
167     }
168 }