extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/wrapper/GPClient.java

Code
Comments
Other
Rev Date Author Line
1115 10 Jun 09 nicklas 1 package net.sf.basedb.genepattern.wrapper;
1115 10 Jun 09 nicklas 2
1134 22 Jun 09 nicklas 3 import java.io.File;
1134 22 Jun 09 nicklas 4
1130 16 Jun 09 nicklas 5 import net.sf.basedb.core.signal.SignalException;
1115 10 Jun 09 nicklas 6 import net.sf.basedb.genepattern.GPServer;
1115 10 Jun 09 nicklas 7 import net.sf.basedb.util.filter.Filter;
1115 10 Jun 09 nicklas 8
1115 10 Jun 09 nicklas 9 import org.genepattern.webservice.AdminProxy;
1115 10 Jun 09 nicklas 10 import org.genepattern.webservice.AnalysisWebServiceProxy;
1115 10 Jun 09 nicklas 11 import org.genepattern.webservice.Parameter;
1132 18 Jun 09 nicklas 12 import org.genepattern.webservice.TaskIntegratorProxy;
1115 10 Jun 09 nicklas 13 import org.genepattern.webservice.WebServiceException;
1115 10 Jun 09 nicklas 14
1115 10 Jun 09 nicklas 15 /**
1115 10 Jun 09 nicklas 16   Wraps GenePattern web services API into a single class. Methods are exposed
1115 10 Jun 09 nicklas 17   as they are needed by other parts of our code. Use:
1115 10 Jun 09 nicklas 18   <ul>
1115 10 Jun 09 nicklas 19   <li>{@link #getGpClient()} to access the underlying {@link org.genepattern.client.GPClient} instance
1115 10 Jun 09 nicklas 20   <li>{@link #getAdminProxy()} to access the underlying {@link AdminProxy} instance
1115 10 Jun 09 nicklas 21   <li>{@link #getAnalysisProxy()} to access the underlying {@link AnalysisWebServiceProxy} instance
1115 10 Jun 09 nicklas 22   </ul>
1115 10 Jun 09 nicklas 23   
1115 10 Jun 09 nicklas 24   @author Nicklas
1115 10 Jun 09 nicklas 25   @since 1.0
1115 10 Jun 09 nicklas 26 */
1115 10 Jun 09 nicklas 27 public class GPClient 
1115 10 Jun 09 nicklas 28 {
1115 10 Jun 09 nicklas 29
1115 10 Jun 09 nicklas 30   private final String gpServer;
1115 10 Jun 09 nicklas 31   private final String login;
1115 10 Jun 09 nicklas 32   private final String password;
1115 10 Jun 09 nicklas 33   
1115 10 Jun 09 nicklas 34   private org.genepattern.client.GPClient gpClient;
1115 10 Jun 09 nicklas 35   private AdminProxy adminProxy;
1115 10 Jun 09 nicklas 36   private AnalysisWebServiceProxy wsProxy;
1132 18 Jun 09 nicklas 37   private TaskIntegratorProxy tiProxy;
1115 10 Jun 09 nicklas 38   
1115 10 Jun 09 nicklas 39   /**
1115 10 Jun 09 nicklas 40     Create a new instance using the specified GenePattern server.
1115 10 Jun 09 nicklas 41     @param gpServer Information about the GenePattern server to connect to
1115 10 Jun 09 nicklas 42   */
1115 10 Jun 09 nicklas 43   public GPClient(GPServer gpServer)
1115 10 Jun 09 nicklas 44   {
1115 10 Jun 09 nicklas 45     this(gpServer.getUrl(), gpServer.getLogin(), gpServer.getPassword());
1115 10 Jun 09 nicklas 46   }
1115 10 Jun 09 nicklas 47   
1115 10 Jun 09 nicklas 48   /**
1115 10 Jun 09 nicklas 49     Create a new instance using the specified url, login and password
1115 10 Jun 09 nicklas 50     @param gpServer The URL to the GenePattern server
1115 10 Jun 09 nicklas 51     @param login A valid login
1115 10 Jun 09 nicklas 52     @param password A valid password or null if the server is not 
1115 10 Jun 09 nicklas 53       password protected
1115 10 Jun 09 nicklas 54   */
1115 10 Jun 09 nicklas 55   public GPClient(String gpServer, String login, String password)
1115 10 Jun 09 nicklas 56   {
1115 10 Jun 09 nicklas 57     this.gpServer = gpServer;
1115 10 Jun 09 nicklas 58     this.login = login;
1115 10 Jun 09 nicklas 59     this.password = password;
1115 10 Jun 09 nicklas 60   }
1115 10 Jun 09 nicklas 61   
1115 10 Jun 09 nicklas 62   /**
1115 10 Jun 09 nicklas 63     Get the underlying {@link org.genepattern.client.GPClient} instance.
1115 10 Jun 09 nicklas 64     Use this method to access functionality that is not exposed by this
1115 10 Jun 09 nicklas 65     class.
1115 10 Jun 09 nicklas 66   */
1115 10 Jun 09 nicklas 67   public org.genepattern.client.GPClient getGpClient()
1115 10 Jun 09 nicklas 68     throws WebServiceException
1115 10 Jun 09 nicklas 69   {
1115 10 Jun 09 nicklas 70     if (gpClient == null)
1115 10 Jun 09 nicklas 71     {
1115 10 Jun 09 nicklas 72       gpClient = new org.genepattern.client.GPClient(gpServer, login, password);
1115 10 Jun 09 nicklas 73     }
1115 10 Jun 09 nicklas 74     return gpClient;
1115 10 Jun 09 nicklas 75   }
1115 10 Jun 09 nicklas 76   
1115 10 Jun 09 nicklas 77   /**
1115 10 Jun 09 nicklas 78     Get the underlying {@link AnalysisWebServiceProxy} instance.
1115 10 Jun 09 nicklas 79     Use this method to access functionality that is not exposed by this
1115 10 Jun 09 nicklas 80     class.
1115 10 Jun 09 nicklas 81   */
1132 18 Jun 09 nicklas 82   public AnalysisWebServiceProxy getAnalysisProxy()
1115 10 Jun 09 nicklas 83     throws WebServiceException
1115 10 Jun 09 nicklas 84   {
1115 10 Jun 09 nicklas 85     if (wsProxy == null)
1115 10 Jun 09 nicklas 86     {
1115 10 Jun 09 nicklas 87       wsProxy = new AnalysisWebServiceProxy(gpServer, login, password);
1115 10 Jun 09 nicklas 88     }
1115 10 Jun 09 nicklas 89     return wsProxy;
1115 10 Jun 09 nicklas 90   }
1115 10 Jun 09 nicklas 91   
1115 10 Jun 09 nicklas 92   /**
1115 10 Jun 09 nicklas 93     Get the underlying {@link AdminProxy} instance.
1115 10 Jun 09 nicklas 94     Use this method to access functionality that is not exposed by this
1115 10 Jun 09 nicklas 95     class.
1115 10 Jun 09 nicklas 96   */
1132 18 Jun 09 nicklas 97   public AdminProxy getAdminProxy()
1115 10 Jun 09 nicklas 98     throws WebServiceException
1115 10 Jun 09 nicklas 99   {
1115 10 Jun 09 nicklas 100     if (adminProxy == null)
1115 10 Jun 09 nicklas 101     {
1115 10 Jun 09 nicklas 102       adminProxy = new AdminProxy(gpServer, login, password);
1115 10 Jun 09 nicklas 103     }
1115 10 Jun 09 nicklas 104     return adminProxy;
1115 10 Jun 09 nicklas 105   }
1115 10 Jun 09 nicklas 106   
1134 22 Jun 09 nicklas 107   /**
1134 22 Jun 09 nicklas 108     Get the underlying {@link TaskIntegratorProxy} instance.
1134 22 Jun 09 nicklas 109     Use this method to access functionality that is not exposed by this
1134 22 Jun 09 nicklas 110     class.
1134 22 Jun 09 nicklas 111   */
1132 18 Jun 09 nicklas 112   public TaskIntegratorProxy getTaskIntegratorProxy()
1132 18 Jun 09 nicklas 113     throws WebServiceException
1132 18 Jun 09 nicklas 114   {
1132 18 Jun 09 nicklas 115     if (tiProxy == null)
1132 18 Jun 09 nicklas 116     {
1132 18 Jun 09 nicklas 117       tiProxy = new TaskIntegratorProxy(gpServer, login, password);
1132 18 Jun 09 nicklas 118     }
1132 18 Jun 09 nicklas 119     return tiProxy;
1132 18 Jun 09 nicklas 120     
1132 18 Jun 09 nicklas 121   }
1132 18 Jun 09 nicklas 122   
1115 10 Jun 09 nicklas 123   /**
1115 10 Jun 09 nicklas 124     Check that the specified GenePattern server can be accessed with
1115 10 Jun 09 nicklas 125     the given user credentials.
1115 10 Jun 09 nicklas 126     @throws WebServiceException If something is wrong
1115 10 Jun 09 nicklas 127   */
1115 10 Jun 09 nicklas 128   public void checkLogin()
1115 10 Jun 09 nicklas 129     throws WebServiceException
1115 10 Jun 09 nicklas 130   {
1115 10 Jun 09 nicklas 131     getAnalysisProxy().ping();
1115 10 Jun 09 nicklas 132   }
1115 10 Jun 09 nicklas 133   
1115 10 Jun 09 nicklas 134   /**
1115 10 Jun 09 nicklas 135     Get a list of all modules/pipelins that are installed on the
1115 10 Jun 09 nicklas 136     current GenePattern server. 
1115 10 Jun 09 nicklas 137     @param filter An optional filter (null to return all modules)
1115 10 Jun 09 nicklas 138     @return An array with information about the modules
1115 10 Jun 09 nicklas 139     @throws WebServiceException If there is a problem retreiving the list
1115 10 Jun 09 nicklas 140   */
1115 10 Jun 09 nicklas 141   public TaskInfo[] getModules(Filter<? super TaskInfo> filter)
1115 10 Jun 09 nicklas 142     throws WebServiceException
1115 10 Jun 09 nicklas 143   {
1115 10 Jun 09 nicklas 144     org.genepattern.webservice.TaskInfo[] gpTasks = getAdminProxy().getAllTasks();
1115 10 Jun 09 nicklas 145     TaskInfo[] tasks = new TaskInfo[gpTasks.length];
1115 10 Jun 09 nicklas 146     int index = 0;
1115 10 Jun 09 nicklas 147     for (int i = 0; i < gpTasks.length; ++i)
1115 10 Jun 09 nicklas 148     {
1115 10 Jun 09 nicklas 149       TaskInfo ti = new TaskInfo(gpTasks[i]);
1115 10 Jun 09 nicklas 150       if (filter == null || filter.evaluate(ti))
1115 10 Jun 09 nicklas 151       {
1115 10 Jun 09 nicklas 152         tasks[index] = ti;
1115 10 Jun 09 nicklas 153         index++;
1115 10 Jun 09 nicklas 154       }
1115 10 Jun 09 nicklas 155     }
1115 10 Jun 09 nicklas 156     return tasks;
1115 10 Jun 09 nicklas 157   }
1115 10 Jun 09 nicklas 158
1134 22 Jun 09 nicklas 159   /**
1134 22 Jun 09 nicklas 160     Get information about a specific module that is installed on the
1134 22 Jun 09 nicklas 161     current GenePattern server.
1134 22 Jun 09 nicklas 162     @param module The name of LSID of the module
1134 22 Jun 09 nicklas 163     @return A TaskInfo object
1134 22 Jun 09 nicklas 164     @throws WebServiceException If there is a problem retreiving the information
1134 22 Jun 09 nicklas 165   */
1132 18 Jun 09 nicklas 166   public TaskInfo getTaskInfo(String module)
1132 18 Jun 09 nicklas 167     throws WebServiceException
1132 18 Jun 09 nicklas 168   {
1132 18 Jun 09 nicklas 169     org.genepattern.webservice.TaskInfo gpInfo = getAdminProxy().getTask(module);
1132 18 Jun 09 nicklas 170     return new TaskInfo(gpInfo);
1132 18 Jun 09 nicklas 171   }
1132 18 Jun 09 nicklas 172   
1134 22 Jun 09 nicklas 173   /**
1134 22 Jun 09 nicklas 174     Get information about the support files that are required by
1134 22 Jun 09 nicklas 175     a (visualizer) module. 
1134 22 Jun 09 nicklas 176     
1134 22 Jun 09 nicklas 177     @param module The LSID of the module (the name will not work)
1134 22 Jun 09 nicklas 178     @return An array with SupprtFileInfo objects
1134 22 Jun 09 nicklas 179     @throws WebServiceException If there is a problem retreiving the information
1134 22 Jun 09 nicklas 180   */
1132 18 Jun 09 nicklas 181   public SupportFileInfo[] getSupportFiles(String module)
1132 18 Jun 09 nicklas 182     throws WebServiceException
1132 18 Jun 09 nicklas 183   {
1132 18 Jun 09 nicklas 184     TaskIntegratorProxy tiProxy = getTaskIntegratorProxy();
1132 18 Jun 09 nicklas 185     String[] fileNames = tiProxy.getSupportFileNames(module);
1132 18 Jun 09 nicklas 186     long[] dates = tiProxy.getLastModificationTimes(module, fileNames);
1132 18 Jun 09 nicklas 187     SupportFileInfo[] supportFiles = new SupportFileInfo[fileNames.length];
1132 18 Jun 09 nicklas 188     for (int i = 0; i < fileNames.length; ++i)
1132 18 Jun 09 nicklas 189     {
1132 18 Jun 09 nicklas 190       supportFiles[i] = new SupportFileInfo(fileNames[i], dates[i]);
1132 18 Jun 09 nicklas 191     }
1132 18 Jun 09 nicklas 192     return supportFiles;
1132 18 Jun 09 nicklas 193   }
1132 18 Jun 09 nicklas 194   
1134 22 Jun 09 nicklas 195   /**
1134 22 Jun 09 nicklas 196     Download support files required by a (visualizer) module. NOTE!
1134 22 Jun 09 nicklas 197     Existing files in the specified directory are overwritten!
1134 22 Jun 09 nicklas 198     
1134 22 Jun 09 nicklas 199     @param module The LSID of the module (the name will not work)
1134 22 Jun 09 nicklas 200     @param files An array with the files to download, or null to
1134 22 Jun 09 nicklas 201       download all support files
1134 22 Jun 09 nicklas 202     @param toDir The directory to download the files to
1134 22 Jun 09 nicklas 203     @throws WebServiceException If there is a problem downloading the files
1134 22 Jun 09 nicklas 204   */
1134 22 Jun 09 nicklas 205   public void downloadSupportFiles(String module, SupportFileInfo[] files, File toDir)
1134 22 Jun 09 nicklas 206     throws WebServiceException
1134 22 Jun 09 nicklas 207   {
1134 22 Jun 09 nicklas 208     TaskIntegratorProxy tiProxy = getTaskIntegratorProxy();
1134 22 Jun 09 nicklas 209     if (files == null) files = getSupportFiles(module);
1134 22 Jun 09 nicklas 210     String[] fileNames = new String[files.length];
1134 22 Jun 09 nicklas 211     for (int i = 0; i < files.length; ++i)
1134 22 Jun 09 nicklas 212     {
1134 22 Jun 09 nicklas 213       fileNames[i] = files[i].getFileName();
1134 22 Jun 09 nicklas 214     }
1134 22 Jun 09 nicklas 215     tiProxy.getSupportFiles(module, fileNames, toDir);
1134 22 Jun 09 nicklas 216   }
1132 18 Jun 09 nicklas 217   
1115 10 Jun 09 nicklas 218   /**
1115 10 Jun 09 nicklas 219     Get the input parameters for a module.
1115 10 Jun 09 nicklas 220     @param module The name of LSID of a module.
1115 10 Jun 09 nicklas 221     @param filter An optional filter (null to return all modules)
1115 10 Jun 09 nicklas 222     @return An array with information about the parameters
1115 10 Jun 09 nicklas 223     @throws WebServiceException If there is a problem retreiving the list
1115 10 Jun 09 nicklas 224   */
1115 10 Jun 09 nicklas 225   public ParameterInfo[] getParameters(String module, Filter<? super ParameterInfo> filter)
1115 10 Jun 09 nicklas 226     throws WebServiceException
1115 10 Jun 09 nicklas 227   {
1132 18 Jun 09 nicklas 228     TaskInfo ti = new TaskInfo(getAdminProxy().getTask(module));
1132 18 Jun 09 nicklas 229     return ti.getParameters(filter);
1115 10 Jun 09 nicklas 230   }
1115 10 Jun 09 nicklas 231   
1115 10 Jun 09 nicklas 232   /**
1115 10 Jun 09 nicklas 233     Submit a job to the GenePattern server.
1115 10 Jun 09 nicklas 234     @param module The name or LSID of the module/pipeline to execute
1115 10 Jun 09 nicklas 235     @param parameters An array with the parameter values
1115 10 Jun 09 nicklas 236     @return The job ID, which can be used to check the status of the job
1115 10 Jun 09 nicklas 237       and to terminate it
1115 10 Jun 09 nicklas 238     @throws WebServiceException If there is a problem submitting the job
1115 10 Jun 09 nicklas 239   */
1115 10 Jun 09 nicklas 240   public int runAnalysisNoWait(String module, Parameter... parameters)
1115 10 Jun 09 nicklas 241     throws WebServiceException
1115 10 Jun 09 nicklas 242   {
1115 10 Jun 09 nicklas 243     return getGpClient().runAnalysisNoWait(module, parameters);
1115 10 Jun 09 nicklas 244   }
1115 10 Jun 09 nicklas 245   
1115 10 Jun 09 nicklas 246   /**
1130 16 Jun 09 nicklas 247     Submit a job to the GenePattern server and wait for the 
1130 16 Jun 09 nicklas 248     result to complete. This is a convenience method for calling
1130 16 Jun 09 nicklas 249     {@link #runAnalysisNoWait(String, Parameter...)} and then
1130 16 Jun 09 nicklas 250     {@link #waitFor(int, int)}, but it throws a {@link SignalException}
1130 16 Jun 09 nicklas 251     instead of a {@link InterruptedException}.
1130 16 Jun 09 nicklas 252     
1130 16 Jun 09 nicklas 253     @param module The name or LSID of the module/pipeline to execute
1130 16 Jun 09 nicklas 254     @param parameters An array with the parameter values
1130 16 Jun 09 nicklas 255     @return The result of the job
1130 16 Jun 09 nicklas 256     @throws WebServiceException If there is a problem submitting the job
1130 16 Jun 09 nicklas 257     @throws SignalException If the wait was interrupted
1130 16 Jun 09 nicklas 258   */
1130 16 Jun 09 nicklas 259   public JobResult runAnalysis(String module, Parameter... parameters)
1130 16 Jun 09 nicklas 260     throws WebServiceException
1130 16 Jun 09 nicklas 261   {
1130 16 Jun 09 nicklas 262     int jobId = runAnalysisNoWait(module, parameters);
1130 16 Jun 09 nicklas 263     JobResult result = null;
1130 16 Jun 09 nicklas 264     try
1130 16 Jun 09 nicklas 265     {
1130 16 Jun 09 nicklas 266       result = waitFor(jobId, 5);
1130 16 Jun 09 nicklas 267     }
1130 16 Jun 09 nicklas 268     catch (InterruptedException ex)
1130 16 Jun 09 nicklas 269     {
1130 16 Jun 09 nicklas 270       throw new SignalException("Aborted by user");
1130 16 Jun 09 nicklas 271     }
1130 16 Jun 09 nicklas 272     return result;
1130 16 Jun 09 nicklas 273   }
1130 16 Jun 09 nicklas 274   
1130 16 Jun 09 nicklas 275   /**
1115 10 Jun 09 nicklas 276     Has the job finished?
1115 10 Jun 09 nicklas 277     @param jobId The ID of the job
1115 10 Jun 09 nicklas 278     @return TRUE if the job is finished, FALSE if it is still running
1115 10 Jun 09 nicklas 279     @throws WebServiceException If there is a problem retreiving the information
1115 10 Jun 09 nicklas 280   */
1115 10 Jun 09 nicklas 281   public boolean isComplete(int jobId)
1115 10 Jun 09 nicklas 282     throws WebServiceException
1115 10 Jun 09 nicklas 283   {
1115 10 Jun 09 nicklas 284     return getGpClient().isComplete(jobId);
1115 10 Jun 09 nicklas 285   }
1115 10 Jun 09 nicklas 286   
1115 10 Jun 09 nicklas 287   /**
1115 10 Jun 09 nicklas 288     Wait for a job on the GenePattern server to finish.
1115 10 Jun 09 nicklas 289     The wait can be interrupted with by calling {@link Thread#interrupt()}
1115 10 Jun 09 nicklas 290     on the waiting thread. Interrupting the wait also terminates the job
1115 10 Jun 09 nicklas 291     on the GenePattern server.
1115 10 Jun 09 nicklas 292     @param jobId The ID of the job
1115 10 Jun 09 nicklas 293     @param checkInterval Number of seconds to wait between checks if the job
1115 10 Jun 09 nicklas 294       has finished or not
1115 10 Jun 09 nicklas 295     @return The result of the job
1115 10 Jun 09 nicklas 296     @throws WebServiceException If there is a problem submitting the job
1115 10 Jun 09 nicklas 297     @throws InterruptedException If the wait was interrupted
1115 10 Jun 09 nicklas 298   */
1115 10 Jun 09 nicklas 299   public JobResult waitFor(int jobId, int checkInterval)
1115 10 Jun 09 nicklas 300     throws WebServiceException, InterruptedException
1115 10 Jun 09 nicklas 301   {
1115 10 Jun 09 nicklas 302     while (!isComplete(jobId))
1115 10 Jun 09 nicklas 303     {
1115 10 Jun 09 nicklas 304       try
1115 10 Jun 09 nicklas 305       {
1115 10 Jun 09 nicklas 306         Thread.sleep(1000 * checkInterval);
1115 10 Jun 09 nicklas 307       }
1115 10 Jun 09 nicklas 308       catch (InterruptedException ex)
1115 10 Jun 09 nicklas 309       {
1115 10 Jun 09 nicklas 310         try
1115 10 Jun 09 nicklas 311         {
1115 10 Jun 09 nicklas 312           terminateJob(jobId);
1115 10 Jun 09 nicklas 313         }
1115 10 Jun 09 nicklas 314         catch (Throwable t)
1115 10 Jun 09 nicklas 315         {}
1115 10 Jun 09 nicklas 316         throw ex;
1115 10 Jun 09 nicklas 317       }
1115 10 Jun 09 nicklas 318     }
1115 10 Jun 09 nicklas 319     return createJobResult(jobId);    
1115 10 Jun 09 nicklas 320   }
1115 10 Jun 09 nicklas 321
1115 10 Jun 09 nicklas 322   /**
1115 10 Jun 09 nicklas 323     Terminate a running job.
1115 10 Jun 09 nicklas 324     @param jobId The ID of the job
1115 10 Jun 09 nicklas 325     @throws WebServiceException If there is a problem terminating the job
1115 10 Jun 09 nicklas 326   */
1115 10 Jun 09 nicklas 327   public void terminateJob(int jobId)
1115 10 Jun 09 nicklas 328     throws WebServiceException
1115 10 Jun 09 nicklas 329   {
1115 10 Jun 09 nicklas 330     getAnalysisProxy().terminateJob(jobId);
1115 10 Jun 09 nicklas 331   }
1115 10 Jun 09 nicklas 332   
1115 10 Jun 09 nicklas 333   /**
1115 10 Jun 09 nicklas 334     Get the result from a finished job.
1115 10 Jun 09 nicklas 335     @param jobId The ID of the job
1115 10 Jun 09 nicklas 336     @return The result of the job
1115 10 Jun 09 nicklas 337     @throws WebServiceException If there is a problem with getting the result
1115 10 Jun 09 nicklas 338   */
1115 10 Jun 09 nicklas 339   public JobResult createJobResult(int jobId)
1115 10 Jun 09 nicklas 340     throws WebServiceException
1115 10 Jun 09 nicklas 341   {
1115 10 Jun 09 nicklas 342     return new JobResult(getGpClient().createJobResult(jobId));
1115 10 Jun 09 nicklas 343   }
1115 10 Jun 09 nicklas 344
1115 10 Jun 09 nicklas 345   
1115 10 Jun 09 nicklas 346 }