2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.development;
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;
34 import java.net.URLConnection;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38 import javax.xml.parsers.ParserConfigurationException;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.NodeList;
43 import org.xml.sax.SAXException;
45 public class HmmerRest {
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";
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";
68 result = getResult( query );
70 catch ( final IOException e ) {
71 // TODO Auto-generated catch block
74 System.out.println( result );
75 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
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() ) );
83 catch ( final ParserConfigurationException pce ) {
84 pce.printStackTrace();
86 catch ( final SAXException se ) {
89 catch ( final IOException ioe ) {
90 ioe.printStackTrace();
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" );
99 System.out.println( "result url = " + result_url );
100 //gettin the result....
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
106 catch ( final InterruptedException ie ) {
107 ie.printStackTrace();
110 result = getResult( result_url, "" );
112 catch ( final IOException e ) {
113 // TODO Auto-generated catch block
116 System.out.println( result );
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();
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() );
139 final BufferedReader br = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
140 final StringBuffer sb = new StringBuffer();
142 while ( ( line = br.readLine() ) != null ) {
143 sb.append( line + LINE_SEPARATOR );
146 return sb.toString().trim();
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() );
159 final BufferedReader br = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
160 final StringBuffer sb = new StringBuffer();
162 while ( ( line = br.readLine() ) != null ) {
163 sb.append( line + LINE_SEPARATOR );
166 return sb.toString().trim();