Changeset 862


Ignore:
Timestamp:
Dec 2, 2008, 11:22:29 PM (13 years ago)
Author:
Jari Häkkinen
Message:

Cleanup of the abstract super class. More to come ...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • plugins/base2/net.sf.basedb.pluginutilities/trunk/src/net/sf/basedb/plugins/AbstractRunBinaryPlugin.java

    r818 r862  
    44  Copyright (C) 2008 Jari Hakkinen
    55
    6   This file is part of the net.sf.basedb.pluginutilities package, a
    7   utility package that simplifies creation of BASE plug-ins. The
    8   package is available at http://baseplugins.thep.lu.se/ and BASE web
    9   site is http://base.thep.lu.se
     6  This file is part of the PluginUtilities
     7  (net.sf.basedb.pluginutilities) package, a utility package that
     8  simplifies creation of BASE plug-ins. The package is available at
     9  http://baseplugins.thep.lu.se/ and BASE web site is
     10  http://base.thep.lu.se
    1011
    1112  This is free software; you can redistribute it and/or modify it
     
    2829import net.sf.basedb.core.BioAssaySet;
    2930import net.sf.basedb.core.DbControl;
    30 import net.sf.basedb.core.Experiment;
    31 import net.sf.basedb.core.File;
    32 import net.sf.basedb.core.Include;
    33 import net.sf.basedb.core.InvalidDataException;
    34 import net.sf.basedb.core.InvalidUseOfNullException;
    35 import net.sf.basedb.core.Item;
    36 import net.sf.basedb.core.ItemParameterType;
    37 import net.sf.basedb.core.ItemQuery;
     31import net.sf.basedb.core.Experiment; // Jari, remove this?
     32import net.sf.basedb.core.InvalidUseOfNullException; // Jari, remove this?
    3833import net.sf.basedb.core.Job;
    39 import net.sf.basedb.core.Platform;
    4034import net.sf.basedb.core.PluginDefinition;
    4135import net.sf.basedb.core.PluginParameter;
    4236import net.sf.basedb.core.ProgressReporter;
    43 import net.sf.basedb.core.RawBioAssay;
     37import net.sf.basedb.core.RawBioAssay; // Jari, remove this?
    4438import net.sf.basedb.core.RequestInformation;
    45 import net.sf.basedb.core.StringParameterType;
    4639
    4740import net.sf.basedb.core.plugin.About;
    48 import net.sf.basedb.core.plugin.AbstractPlugin;
     41import net.sf.basedb.core.plugin.AbstractAnalysisPlugin;
    4942import net.sf.basedb.core.plugin.GuiContext;
    5043import net.sf.basedb.core.plugin.InteractivePlugin;
     44import net.sf.basedb.core.plugin.Permissions;
    5145import net.sf.basedb.core.plugin.Plugin;
    5246import net.sf.basedb.core.plugin.Request;
    5347import net.sf.basedb.core.plugin.Response;
    5448
    55 import net.sf.basedb.core.query.Hql;
    56 import net.sf.basedb.core.query.Orders;
    57 
    5849import java.io.IOException;
    5950
    6051import java.util.ArrayList;
    6152import java.util.Arrays;
    62 import java.util.Collections;
     53import java.util.Collection;
     54import java.util.HashSet;
    6355import java.util.List;
    6456import java.util.Set;
    6557
    6658/**
    67    This is an abstract base class useful for developing plugins.
    68    <pre>
    69    String              : name; The name of the new bioassayset.
    70    Experiment          : experiment; The experiment we are working on.
    71    List&gt;RawBioAssay&lt;   : rawBioAssays; The raw bioassays to create bioassays for
    72                          (must be part of the experiment).
    73    </pre>
    74 
    75    @author Peter
     59   This is an abstract base class useful for developing plugins that
     60   call external binaries.
     61
     62   Two methods must be defined by the derived class runBinary and
     63   storeResult
     64
     65   The derived class constructor should set the protected member
     66   variables about, defaultChildName, permissions, and
     67   plugin_maintype.
     68
     69   @author Jari Hakkinen
    7670   @version MAKESUBSTOFVERSIONNUMBER
     71   @since BASE 2.9
    7772   @base.modified $Date$
    7873*/
    7974
    8075abstract public class AbstractRunBinaryPlugin
    81   extends AbstractPlugin
     76  extends AbstractAnalysisPlugin
    8277  implements InteractivePlugin
    8378{
     
    8883  public void configure(GuiContext context, Request request, Response response)
    8984  {
    90     try {
    91       if (request.getCommand().equals(Request.COMMAND_CONFIGURE_JOB)) {
    92         List<Throwable> errors = validateRequestParameters
    93           (getConfigureJobParameters().getParameters(), request);
    94         if (errors != null) {
     85    try
     86    {
     87      if (request.getCommand().equals(Request.COMMAND_CONFIGURE_JOB))
     88      {
     89        RequestInformation ri = getConfigureJobParameters();
     90        List<Throwable> errors = validateRequestParameters(ri.getParameters(),
     91                                                           request);
     92        if (errors != null)
     93        {
    9594          response.setError(errors.size() +
    9695                            " invalid parameter(s) were found in the request",
     
    9897          return;
    9998        }
    100         Experiment experiment =
    101           (Experiment)request.getParameterValue("experiment");
     99
     100        storeValue(job, request, ri.getParameter(SOURCE_BIOASSAYSET));
     101        storeValue(job, request, ri.getParameter(CHILD_NAME));
     102        storeValue(job, request, ri.getParameter(CHILD_DESCRIPTION));
     103
     104        Job.ExecutionTime execTime = Job.ExecutionTime.SHORT;
    102105        DbControl dc = sc.newDbControl();
    103         int numCels=0;
    104106        try {
    105           experiment = Experiment.getById(dc, experiment.getId());
    106           if (!experiment.getRawDataType().getPlatform(dc).equals(Platform.AFFYMETRIX))
    107             throw new BaseException("Raw data type '" +
    108                                     experiment.getRawDataType() +
    109                                     "' is not supoorted by this plug-in.");
    110           List<RawBioAssay> rawBioAssays =
    111             (List<RawBioAssay>)request.getParameterValues("rawBioAssays");
    112           for (RawBioAssay rba : rawBioAssays)
    113             if (!experiment.isUsing(rba))
    114               throw new InvalidDataException
    115                 ("Raw bioassay '" + rba.getName() +
    116                  "' is not part of the experiment '" + experiment.getName()+"'");
    117           numCels=rawBioAssays.size();
     107          BioAssaySet source = getSourceBioAssaySet(dc);
     108          int nofAssays = source.getBioAssays().list(dc).size();
     109          if (nofAssays>10 && nofAssays<30)
     110            execTime = Job.ExecutionTime.MEDIUM;
     111          else if (nofAssays>=30)
     112            execTime = Job.ExecutionTime.LONG;
    118113        }
    119114        finally {
     
    121116            dc.close();
    122117        }
    123         storeValue(job, request, nameParameter);
    124         storeValue(job, request, experimentParameter);
    125         storeValues(job, request, rawBioAssaysParameter);
    126         Job.ExecutionTime execTime = Job.ExecutionTime.SHORT;
    127         if (numCels>10 && numCels<30)
    128           execTime = Job.ExecutionTime.MEDIUM;
    129         else if (numCels>=30)
    130           execTime = Job.ExecutionTime.LONG;
    131118        response.setDone("Job configuration complete", execTime);
    132119      }
     
    138125
    139126
    140   protected void download(File file)
    141     throws IOException
    142   {
    143     java.io.FileOutputStream os = new java.io.FileOutputStream
    144       (new java.io.File(getExecDirectory(),file.getName()));
    145     file.download(os,0);
    146   }
    147 
    148 
    149   abstract public About getAbout();
    150 
    151 
    152   protected String getBinaryPath(String binary)
    153   {
    154     DbControl dc = sc.newDbControl();
    155     String jarpath=PluginDefinition.getByClassName(dc,binary).getJarPath();
    156     int pos = jarpath.length();
    157     while ((pos>0) && (jarpath.charAt(pos-1)!=java.io.File.separatorChar))
    158       --pos;
    159     return jarpath.substring(0,pos);
    160   }
    161 
    162 
    163   private RequestInformation getConfigureJobParameters()
    164   {
    165     if (configureJob == null) {
    166       List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
    167      
    168       experimentType = new ItemParameterType<Experiment>(Experiment.class, null,
    169                                                          true, 1, null);
    170       experimentParameter = new PluginParameter<Experiment>(
    171         "experiment", "Experiment", "The experiment we are working with",
    172         experimentType);
    173       parameters.add(experimentParameter);
    174       parameters.add(nameParameter);
     127  protected void execute(String[] cmd, ProgressReporter progress)
     128    throws InterruptedException, IOException
     129  {
     130    percentDone+=1;
     131    progress.display(percentDone,"Running " + about.getName());
     132    Process p = Runtime.getRuntime().exec(cmd, null, getExecDirectory());
     133    p.waitFor();
     134    int status=p.exitValue();
     135    if (status>0)
     136      throw new IOException("Unexpected exit of " + about.getName() +
     137                            "WeNNI sub-process. Return value: " + status);
     138  }
     139
     140
     141  public About getAbout()
     142  {
     143    return about;
     144  }
     145
     146
     147  protected String getBinaryPath(Class cls, String binaryname)
     148  {
     149    if (BinaryPath == null) {
    175150      DbControl dc = sc.newDbControl();
    176151      try {
    177         int experimentId = sc.getCurrentContext(Item.EXPERIMENT).getId();
    178         Experiment experiment = Experiment.getById(dc, experimentId);
    179         ItemQuery<RawBioAssay> rawBioAssayQuery = experiment.getRawBioAssays();
    180         rawBioAssayQuery.include(Include.MINE, Include.SHARED, Include.OTHERS,
    181                                  Include.IN_PROJECT);
    182         rawBioAssayQuery.order(Orders.asc(Hql.property("name")));
    183         List<RawBioAssay> rawBioAssays = new ArrayList<RawBioAssay>(
    184                                                       rawBioAssayQuery.list(dc));
    185         rawBioAssaysType = new ItemParameterType<RawBioAssay>(RawBioAssay.class,
    186                                                               null, true, 0,
    187                                                               rawBioAssays);
    188         rawBioAssaysParameter = new PluginParameter<RawBioAssay>(
    189           "rawBioAssays", "Raw bioassays",
    190           "Select the raw bioassays to use when creating the root bioassay set",
    191           rawBioAssaysType);
    192         parameters.add(rawBioAssaysParameter);
     152        String jarpath=
     153          PluginDefinition.getByClassName(dc, cls.getName()).getJarPath();
     154        int pos = jarpath.length();
     155        while ((pos>0) && (jarpath.charAt(pos-1)!=java.io.File.separatorChar))
     156          --pos;
     157        BinaryPath=jarpath.substring(0, pos) + binaryname;
    193158      }
    194159      finally {
     
    196161          dc.close();
    197162      }
     163    }
     164    return BinaryPath;
     165  }
     166
     167
     168  private RequestInformation getConfigureJobParameters()
     169  {
     170    if (configureJob == null)
     171    {
     172      List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
     173     
     174      // Source and child bioassay set parameters
     175      parameters.add(getSourceBioAssaySetParameter(null, null));
     176      parameters.add(getChildNameParameter(null, null, defaultChildName));
     177      parameters.add(getChildDescriptionParameter(null, null, null));
     178
    198179      configureJob = new RequestInformation(
    199         Request.COMMAND_CONFIGURE_JOB, "Intensity calculation options",
    200         "Select which raw bioassays to use and a formula for the calculations",
    201         parameters);
     180        Request.COMMAND_CONFIGURE_JOB, about.getName() + " options",
     181        "Select ...", parameters);
    202182    }
    203183    return configureJob;
     
    217197
    218198
    219   public Set<GuiContext> getGuiContexts() { return guiContexts; }
    220 
    221 
    222   public Plugin.MainType getMainType() { return Plugin.MainType.INTENSITY; }
    223 
    224 
    225   public RequestInformation getRequestInformation(GuiContext context,
    226                                                   String command)
     199  public Plugin.MainType getMainType()
     200  {
     201    return plugin_maintype;
     202  }
     203
     204
     205  public Collection<Permissions> getPermissions()
     206  {
     207    return permissions;
     208  }
     209
     210
     211  public RequestInformation
     212  getRequestInformation(GuiContext context, String command)
    227213    throws BaseException
    228214  {
     
    233219
    234220
    235   /*
    236   public String getRMAExpressConsolePath()
    237   {
    238     if (RMAExpressConsolePath == null) {
    239       DbControl dc = sc.newDbControl();
    240       String jarpath=PluginDefinition.getByClassName(dc,
    241                                 "se.lu.thep.affymetrix.RMAExpress").getJarPath();
    242       int pos = jarpath.length();
    243       while ((pos>0) && (jarpath.charAt(pos-1)!=java.io.File.separatorChar))
    244         --pos;
    245       RMAExpressConsolePath=jarpath.substring(0,pos);
    246       // Hard coded file name and the binary must reside in the same
    247       // directory as the plug-in jar. The idea behind this is that
    248       // only the BASE administrator is allowed to change files in the
    249       // BASE application direcotry structure.
    250       RMAExpressConsolePath+="RMAExpressConsole";
    251     }
    252     return RMAExpressConsolePath;
    253   }
    254   */
    255 
    256   public String isInContext(GuiContext context, Object item)
    257   {
    258     DbControl dc = sc.newDbControl();
    259     try {
    260       Experiment e=Experiment.getById
    261         (dc, sc.getCurrentContext(Item.EXPERIMENT).getId());
    262       if (!e.getRawDataType().getPlatform(dc).equals(Platform.AFFYMETRIX))
    263         return ("Raw data type '" + e.getRawDataType() +
    264                 "' is not supoorted by this plug-in.");
    265     }
    266     catch (Throwable e) {
    267     }
    268     finally {
    269       if (dc != null)
    270         dc.close();
    271     }
    272     return null;
    273   }
    274 
    275 
    276   public boolean requiresConfiguration() { return false; }
     221  public boolean requiresConfiguration()
     222  {
     223    return false;
     224  }
    277225
    278226
     
    296244      runBinary(sources,progress);
    297245      storeResult(dc,experiment,sources,name,progress);
    298       dc.commit();
     246//      dc.commit();
    299247      response.setDone("Plug-in ended successfully");
    300248    }
     
    324272
    325273
    326   public boolean supportsConfigurations() { return false; }
    327 
    328 
     274  public boolean supportsConfigurations()
     275  {
     276    return false;
     277  }
     278
     279
     280  protected About about=null;
     281  protected String defaultChildName = "New bioassayset";
     282  protected Set<Permissions> permissions = new HashSet<Permissions>();
     283  protected Plugin.MainType plugin_maintype=null;
     284
     285  private String BinaryPath=null;
    329286  private RequestInformation configureJob = null;
    330   private RequestInformation configurePlugin = null;
    331   private ItemParameterType<Experiment> experimentType;
    332   /**
    333      This is the directory where the plug-in will generate its output.
    334   */
    335287  private java.io.File execDirectory;
    336   private PluginParameter<Experiment> experimentParameter;
    337   private static final Set<GuiContext> guiContexts = Collections.singleton
    338     (new GuiContext(Item.BIOASSAYSET,GuiContext.Type.LIST));
    339   private static final StringParameterType nameType =
    340     new StringParameterType(BioAssaySet.MAX_NAME_LENGTH, "New bioassayset",
    341                             true);
    342   private static final PluginParameter<String> nameParameter =
    343     new PluginParameter<String>("name", "Bioassay set name",
    344                                 "The name of the root bioassayset", nameType);
    345288  private int percentDone=0;
    346   private ItemParameterType<RawBioAssay> rawBioAssaysType;
    347   private PluginParameter<RawBioAssay> rawBioAssaysParameter;
    348289}
Note: See TracChangeset for help on using the changeset viewer.