query uniprot via REST web service using Java

package com.silicosciences.ws.rest.client.embl;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.kerner.utils.collections.trasformer.ToString;
public class RestClientDBQuery {
	public static void main(String[] args) {
		final String dbName = "swissprot"; // Database name (e.g. UniProtKB)
		final String id = "HBA_HUMAN"; // Entry identifier, name or accession
		final String format = "uniprot"; // Data format
		final String urlBase = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/";
		final String urlFinal = urlBase + dbName + "/" + id + "/" + format;
		System.out.println("URL is: " + urlFinal);
		CloseableHttpResponse response;
		final CloseableHttpClient client = HttpClients.createDefault();
		try {
			// Create a HTTP GET request
			final HttpGet method = new HttpGet(urlFinal);
			response = client.execute(method);
			final HttpEntity entity = response.getEntity();
			final InputStream stream = entity.getContent();
			final List lines = IOUtils.readLines(stream);
			System.out.println("Received " + lines.size() + " lines");
			System.out.println(new ToString().toString(lines));
			EntityUtils.consume(entity);
		} catch (final Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

pom.xml:

	4.0.0
	com.silicosciences.ws
	rest.client
	0.0.1-SNAPSHOT
	REST Client
	
		
			org.apache.httpcomponents
			httpclient
			4.5.1
		
		
			commons-io
			commons-io
			2.4
		
		
			net.sf.kerner-utils-collections
			kerner-utils-collections
			1.1.2-SNAPSHOT
		
	

Share itShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *