extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/wrapper/ParameterInfo.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
1115 10 Jun 09 nicklas 3 import java.util.Map;
1115 10 Jun 09 nicklas 4
1115 10 Jun 09 nicklas 5 import net.sf.basedb.core.BaseException;
1115 10 Jun 09 nicklas 6 import net.sf.basedb.core.FileParameterType;
1115 10 Jun 09 nicklas 7 import net.sf.basedb.core.FloatParameterType;
1115 10 Jun 09 nicklas 8 import net.sf.basedb.core.IntegerParameterType;
1115 10 Jun 09 nicklas 9 import net.sf.basedb.core.ParameterType;
1115 10 Jun 09 nicklas 10 import net.sf.basedb.core.PluginParameter;
1115 10 Jun 09 nicklas 11 import net.sf.basedb.core.StringParameterType;
1115 10 Jun 09 nicklas 12 import net.sf.basedb.util.Enumeration;
1115 10 Jun 09 nicklas 13 import net.sf.basedb.util.Values;
1115 10 Jun 09 nicklas 14
1115 10 Jun 09 nicklas 15 /**
1115 10 Jun 09 nicklas 16   Wraps information about a module parameter.
1115 10 Jun 09 nicklas 17   Information is exposed as it is needed by other parts of our code. Use
1115 10 Jun 09 nicklas 18   {@link #getGPParameterInfo()} to access the underlying {@link 
1115 10 Jun 09 nicklas 19   org.genepattern.webservice.ParameterInfo} instance.
1115 10 Jun 09 nicklas 20   @author Nicklas
1115 10 Jun 09 nicklas 21   @since 1.0
1115 10 Jun 09 nicklas 22 */
1115 10 Jun 09 nicklas 23 public class ParameterInfo 
1115 10 Jun 09 nicklas 24 {
1115 10 Jun 09 nicklas 25
1115 10 Jun 09 nicklas 26   private final org.genepattern.webservice.ParameterInfo gpInfo;
1115 10 Jun 09 nicklas 27   
1115 10 Jun 09 nicklas 28   /**
1115 10 Jun 09 nicklas 29     Creates a new wrapper instance.
1115 10 Jun 09 nicklas 30     @param gpInfo The underlying parameter information from the GenePattern server
1115 10 Jun 09 nicklas 31   */
1115 10 Jun 09 nicklas 32   public ParameterInfo(org.genepattern.webservice.ParameterInfo gpInfo)
1115 10 Jun 09 nicklas 33   {
1115 10 Jun 09 nicklas 34     this.gpInfo = gpInfo;
1115 10 Jun 09 nicklas 35   }
1115 10 Jun 09 nicklas 36   
1115 10 Jun 09 nicklas 37   /**
1115 10 Jun 09 nicklas 38     Get the underlying {@link org.genepattern.webservice.ParameterInfo} instance.
1115 10 Jun 09 nicklas 39     Use this method to access functionality that is not exposed by this
1115 10 Jun 09 nicklas 40     class.
1115 10 Jun 09 nicklas 41   */
1115 10 Jun 09 nicklas 42   public org.genepattern.webservice.ParameterInfo getGPParameterInfo()
1115 10 Jun 09 nicklas 43   {
1115 10 Jun 09 nicklas 44     return gpInfo;
1115 10 Jun 09 nicklas 45   }
1115 10 Jun 09 nicklas 46
1115 10 Jun 09 nicklas 47   /**
1115 10 Jun 09 nicklas 48     Get the name of the parameter.
1115 10 Jun 09 nicklas 49   */
1115 10 Jun 09 nicklas 50   public String getName()
1115 10 Jun 09 nicklas 51   {
1115 10 Jun 09 nicklas 52     return getGPParameterInfo().getName();
1115 10 Jun 09 nicklas 53   }
1115 10 Jun 09 nicklas 54   
1115 10 Jun 09 nicklas 55   /**
1115 10 Jun 09 nicklas 56     Get a description of the parameter.
1115 10 Jun 09 nicklas 57   */
1115 10 Jun 09 nicklas 58   public String getDescription()
1115 10 Jun 09 nicklas 59   {
1115 10 Jun 09 nicklas 60     return getGPParameterInfo().getDescription();
1115 10 Jun 09 nicklas 61   }
1115 10 Jun 09 nicklas 62   
1115 10 Jun 09 nicklas 63   /**
1115 10 Jun 09 nicklas 64     Get the display label of the parameter. If label has been specified a
1115 10 Jun 09 nicklas 65     label is automatically created based on the name (capitalized first letter,
1115 10 Jun 09 nicklas 66     dots replaced with spaces).
1115 10 Jun 09 nicklas 67   */
1115 10 Jun 09 nicklas 68   public String getLabel()
1115 10 Jun 09 nicklas 69   {
1115 10 Jun 09 nicklas 70     String label = getGPParameterInfo().getLabel();
1115 10 Jun 09 nicklas 71     if (label == null || label.length() == 0)
1115 10 Jun 09 nicklas 72     {
1115 10 Jun 09 nicklas 73       label = makeNiceLabel(getName());
1115 10 Jun 09 nicklas 74     }
1115 10 Jun 09 nicklas 75     return label;
1115 10 Jun 09 nicklas 76   }
1115 10 Jun 09 nicklas 77
1115 10 Jun 09 nicklas 78   /**
1115 10 Jun 09 nicklas 79     Is the parameter optional?
1115 10 Jun 09 nicklas 80   */
1115 10 Jun 09 nicklas 81   public boolean isOptional()
1115 10 Jun 09 nicklas 82   {
1115 10 Jun 09 nicklas 83     return getGPParameterInfo().isOptional();
1115 10 Jun 09 nicklas 84   }
1115 10 Jun 09 nicklas 85
1115 10 Jun 09 nicklas 86   /**
1115 10 Jun 09 nicklas 87     Get the default value for the parameter.
1115 10 Jun 09 nicklas 88   */
1115 10 Jun 09 nicklas 89   public String getDefaultValue()
1115 10 Jun 09 nicklas 90   {
1128 15 Jun 09 nicklas 91     String d = getGPParameterInfo().getDefaultValue();
1128 15 Jun 09 nicklas 92     if (hasChoices())
1128 15 Jun 09 nicklas 93     {
1128 15 Jun 09 nicklas 94       Map<String, String> choices = getChoices();
1128 15 Jun 09 nicklas 95       if (!choices.values().contains(d))
1128 15 Jun 09 nicklas 96       {
1128 15 Jun 09 nicklas 97         d = choices.get(d);
1128 15 Jun 09 nicklas 98       }
1128 15 Jun 09 nicklas 99     }
1128 15 Jun 09 nicklas 100     return Values.getStringOrNull(d);
1115 10 Jun 09 nicklas 101   }
1115 10 Jun 09 nicklas 102
1115 10 Jun 09 nicklas 103   /**
1115 10 Jun 09 nicklas 104     Does this parameter represents a file with input data for the module?
1115 10 Jun 09 nicklas 105   */
1115 10 Jun 09 nicklas 106   public boolean isInputFile()
1115 10 Jun 09 nicklas 107   {
1115 10 Jun 09 nicklas 108     return getGPParameterInfo().isInputFile();
1115 10 Jun 09 nicklas 109   }
1115 10 Jun 09 nicklas 110
1115 10 Jun 09 nicklas 111   /**
1115 10 Jun 09 nicklas 112     Get a map with valid choices for this parameter. The 
1115 10 Jun 09 nicklas 113     keys are user-readable text. The values are usually (unique) 
1115 10 Jun 09 nicklas 114     numbers or other short strings that are actually sent to
1115 10 Jun 09 nicklas 115     the module.
1115 10 Jun 09 nicklas 116   */
1115 10 Jun 09 nicklas 117   public Map<String, String> getChoices()
1115 10 Jun 09 nicklas 118   {
1115 10 Jun 09 nicklas 119     return getGPParameterInfo().getChoices();
1115 10 Jun 09 nicklas 120   }
1115 10 Jun 09 nicklas 121
1115 10 Jun 09 nicklas 122   /**
1115 10 Jun 09 nicklas 123     Does this parameter have choices?
1115 10 Jun 09 nicklas 124   */
1115 10 Jun 09 nicklas 125   public boolean hasChoices()
1115 10 Jun 09 nicklas 126   {
1115 10 Jun 09 nicklas 127     Map<String, String> choices = getChoices();
1115 10 Jun 09 nicklas 128     return choices != null && !choices.isEmpty();
1115 10 Jun 09 nicklas 129   }
1115 10 Jun 09 nicklas 130   
1115 10 Jun 09 nicklas 131   /**
1115 10 Jun 09 nicklas 132     Convert the choices (if any) to an {@link Enumeration}. Note
1115 10 Jun 09 nicklas 133     that the key/values are reversed in the enumeration.
1115 10 Jun 09 nicklas 134     @return An enumeration or null if the parameter doesn't have any choices
1115 10 Jun 09 nicklas 135   */
1115 10 Jun 09 nicklas 136   public Enumeration<String, String> getChoicesAsEnumeration()
1115 10 Jun 09 nicklas 137   {
1115 10 Jun 09 nicklas 138     Map<String, String> choices = getChoices();
1115 10 Jun 09 nicklas 139     if (choices == null || choices.isEmpty()) return null;
1115 10 Jun 09 nicklas 140     Enumeration<String, String> options = new Enumeration<String, String>();
1115 10 Jun 09 nicklas 141     for (Map.Entry<String, String> entry : choices.entrySet())
1115 10 Jun 09 nicklas 142     {
1115 10 Jun 09 nicklas 143       options.add(entry.getValue(), entry.getKey());
1115 10 Jun 09 nicklas 144     }
1115 10 Jun 09 nicklas 145     return options;
1115 10 Jun 09 nicklas 146   }
1115 10 Jun 09 nicklas 147
1115 10 Jun 09 nicklas 148   
1115 10 Jun 09 nicklas 149   /**
1115 10 Jun 09 nicklas 150     Get the value of some other attribute as a string.
1115 10 Jun 09 nicklas 151     If the attribute is not a string, it is converted by calling
1115 10 Jun 09 nicklas 152     the 'toString()' method. 
1115 10 Jun 09 nicklas 153     @param key The name of the attribute
1115 10 Jun 09 nicklas 154   */
1115 10 Jun 09 nicklas 155   public String getAttribute(String key)
1115 10 Jun 09 nicklas 156   {
1115 10 Jun 09 nicklas 157     Object value = getGPParameterInfo().getAttributes().get(key);
1115 10 Jun 09 nicklas 158     return value == null ? null : value.toString();
1115 10 Jun 09 nicklas 159   }
1115 10 Jun 09 nicklas 160   
1115 10 Jun 09 nicklas 161   
1115 10 Jun 09 nicklas 162   /**
1115 10 Jun 09 nicklas 163     Get the type of this parameter as a string. Uses the value from
1115 10 Jun 09 nicklas 164     the 'type' attribute.
1115 10 Jun 09 nicklas 165   */
1115 10 Jun 09 nicklas 166   public String getType()
1115 10 Jun 09 nicklas 167   {
1115 10 Jun 09 nicklas 168     return getAttribute("type");
1115 10 Jun 09 nicklas 169   }
1115 10 Jun 09 nicklas 170   
1115 10 Jun 09 nicklas 171   /**
1115 10 Jun 09 nicklas 172     Use the information about this parameter to create a plug-in parameter
1115 10 Jun 09 nicklas 173     that can be used by a plug-in in BASE.
1115 10 Jun 09 nicklas 174     @param prefix A prefix for the name of the parameter, or null to
1115 10 Jun 09 nicklas 175       only use the name of this parameter
1115 10 Jun 09 nicklas 176     @throws BaseException If there is a problem with the conversion
1115 10 Jun 09 nicklas 177   */
1115 10 Jun 09 nicklas 178   @SuppressWarnings("unchecked")
1128 15 Jun 09 nicklas 179   public PluginParameter createPluginParameter(String prefix, Object defaultValue)
1115 10 Jun 09 nicklas 180   {
1115 10 Jun 09 nicklas 181     String type = getType();
1128 15 Jun 09 nicklas 182     ParameterType<?> pt = null;
1115 10 Jun 09 nicklas 183     if (isInputFile())
1115 10 Jun 09 nicklas 184     {
1115 10 Jun 09 nicklas 185       pt = new FileParameterType(null, !isOptional(), 1);
1115 10 Jun 09 nicklas 186     }
1115 10 Jun 09 nicklas 187     else if (hasChoices())
1115 10 Jun 09 nicklas 188     {
1128 15 Jun 09 nicklas 189       pt = new StringParameterType(255, getDefaultValue(), !isOptional(), 1, 0, 0, getChoicesAsEnumeration());
1115 10 Jun 09 nicklas 190     }
1115 10 Jun 09 nicklas 191     else if (type.equals("java.lang.String"))
1115 10 Jun 09 nicklas 192     {
1115 10 Jun 09 nicklas 193       pt = new StringParameterType(255, getDefaultValue(), !isOptional());
1115 10 Jun 09 nicklas 194     }
1115 10 Jun 09 nicklas 195     else if (type.equals("java.lang.Integer"))
1115 10 Jun 09 nicklas 196     {
1115 10 Jun 09 nicklas 197       pt = new IntegerParameterType(null, null, 
1128 15 Jun 09 nicklas 198           Values.getInteger(getDefaultValue(), null), !isOptional());
1115 10 Jun 09 nicklas 199     }
1115 10 Jun 09 nicklas 200     else if (type.equals("java.lang.Float"))
1115 10 Jun 09 nicklas 201     {
1115 10 Jun 09 nicklas 202       pt = new FloatParameterType(null, null, 
1115 10 Jun 09 nicklas 203           Values.getFloat(getDefaultValue(), null), !isOptional());
1115 10 Jun 09 nicklas 204     }
1115 10 Jun 09 nicklas 205     
1115 10 Jun 09 nicklas 206     if (pt == null)
1115 10 Jun 09 nicklas 207     {
1115 10 Jun 09 nicklas 208       throw new BaseException("Don't know how to create plug-in parameter " +
1115 10 Jun 09 nicklas 209           "for GenePattern parameter " + getName() + " (" + type + ")");
1115 10 Jun 09 nicklas 210     }
1115 10 Jun 09 nicklas 211     if (prefix == null) prefix = "";
1115 10 Jun 09 nicklas 212     PluginParameter parameter = new PluginParameter(
1115 10 Jun 09 nicklas 213         prefix + getName(),
1115 10 Jun 09 nicklas 214         getLabel(),
1115 10 Jun 09 nicklas 215         getDescription(),
1128 15 Jun 09 nicklas 216         defaultValue,
1115 10 Jun 09 nicklas 217         pt
1128 15 Jun 09 nicklas 218         );
1115 10 Jun 09 nicklas 219     return parameter;
1115 10 Jun 09 nicklas 220   }
1115 10 Jun 09 nicklas 221   
1115 10 Jun 09 nicklas 222   
1115 10 Jun 09 nicklas 223   private String makeNiceLabel(String ugly)
1115 10 Jun 09 nicklas 224   {
1115 10 Jun 09 nicklas 225     // Convert dot to space
1115 10 Jun 09 nicklas 226     String nice = ugly.replace(".", " ");
1115 10 Jun 09 nicklas 227     // Make sure first letter is uppercase
1115 10 Jun 09 nicklas 228     nice = nice.substring(0, 1).toUpperCase() + nice.substring(1);
1115 10 Jun 09 nicklas 229     return nice;
1115 10 Jun 09 nicklas 230   }
1115 10 Jun 09 nicklas 231   
1115 10 Jun 09 nicklas 232   
1115 10 Jun 09 nicklas 233 }