001 package org.broadinstitute.genee.client;
002
003 import java.io.IOException;
004 import java.io.InputStream;
005 import java.io.OutputStream;
006 import java.io.OutputStreamWriter;
007 import java.io.PrintWriter;
008 import java.text.NumberFormat;
009
010 import org.apache.http.Header;
011 import org.apache.http.HttpEntity;
012 import org.apache.http.HttpResponse;
013 import org.apache.http.client.HttpClient;
014 import org.apache.http.client.methods.HttpGet;
015 import org.apache.http.client.methods.HttpPost;
016 import org.apache.http.entity.mime.MultipartEntity;
017 import org.apache.http.entity.mime.content.AbstractContentBody;
018 import org.apache.http.entity.mime.content.StringBody;
019 import org.apache.http.impl.client.DefaultHttpClient;
020 import org.apache.http.util.EntityUtils;
021 import org.broadinstitute.genee.io.matrix.gct.GctDatasetWriter13;
022 import org.broadinstitute.genee.io.matrix.gct.GctReader;
023 import org.broadinstitute.genee.io.util.StringConverter;
024 import org.broadinstitute.genee.matrix.Dataset;
025
026 /**
027 *
028 * Class for interacting with GENE-E.
029 *
030 */
031 public class GeneeClient {
032
033 private String server;
034 private int port;
035 private String format = "gct";
036 static {
037 System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
038 }
039
040 /**
041 * Constructs a client to interact with GENE-E on localhost:9998.
042 *
043 */
044 public GeneeClient() {
045 this("localhost", 9998);
046 }
047
048 /**
049 * Constructs a client to interact with GENE-E.
050 *
051 * @param server
052 * The server to connect to
053 * @param port
054 * The port to connect to
055 */
056 public GeneeClient(String server, int port) {
057 this.server = server;
058 this.port = port;
059 }
060
061 /**
062 * Views the given dataset in GENE-E.
063 *
064 * @param dataset
065 * The dataset to view
066 * @throws IOException
067 * If an error occurs
068 */
069 public void toGenee(Dataset dataset) throws IOException {
070 HttpClient httpclient = new DefaultHttpClient();
071
072 try {
073 HttpPost post = new HttpPost("http://" + server + ":" + port + "/api/to");
074 final Dataset _dataset = dataset;
075 MultipartEntity reqEntity = new MultipartEntity();
076 reqEntity.addPart("fileData", new AbstractContentBody(format) {
077
078 @Override
079 public String getFilename() {
080 return "fileData";
081 }
082
083 @Override
084 public void writeTo(OutputStream os) throws IOException {
085 PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
086 final NumberFormat nf = NumberFormat.getInstance();
087 nf.setMaximumFractionDigits(2);
088 GctDatasetWriter13 writer = new GctDatasetWriter13(new StringConverter() {
089
090 @Override
091 public String toString(Object value) {
092 if (value instanceof Float) {
093 return nf.format(((Float) value).floatValue());
094 }
095 return String.valueOf(value);
096 }
097
098 @Override
099 public String format(float value) {
100 return nf.format(value);
101 }
102 });
103 writer.write(_dataset, pw);
104 pw.flush();
105 }
106
107 @Override
108 public String getCharset() {
109 return null;
110 }
111
112 @Override
113 public long getContentLength() {
114 return -1;
115 }
116
117 @Override
118 public String getTransferEncoding() {
119 return "binary";
120 }
121 });
122
123 reqEntity.addPart("fileType", new StringBody(format));
124 reqEntity.addPart("fileName", new StringBody(dataset.getName()));
125 post.setEntity(reqEntity);
126 HttpResponse response = httpclient.execute(post);
127 HttpEntity resEntity = response.getEntity();
128 EntityUtils.consume(resEntity);
129 } finally {
130 httpclient.getConnectionManager().shutdown();
131 }
132 }
133
134 /**
135 * Returns the selection from GENE-E. If no rows or columns are selected,
136 * the full dataset is returned.
137 *
138 * @return The dataset from GENE-E
139 * @throws IOException
140 * If an error occurs
141 */
142 public Dataset fromGenee() throws IOException {
143 HttpClient httpclient = new DefaultHttpClient();
144 try {
145 HttpGet get = new HttpGet("http://" + server + ":" + port + "/api/from?fileType=" + format);
146 HttpResponse response = httpclient.execute(get);
147 HttpEntity resEntity = response.getEntity();
148 Header name = response.getFirstHeader("filename");
149 InputStream is = resEntity.getContent();
150 GctReader reader = new GctReader();
151 Dataset dataset = reader.read(name != null ? name.getValue() : "GENE-E", is);
152 is.close();
153 return dataset;
154 } finally {
155 httpclient.getConnectionManager().shutdown();
156
157 }
158
159 }
160
161 }