src/core/net/sf/basedb/util/jep/RawChannelFunction.java

Code
Comments
Other
Rev Date Author Line
4912 29 Apr 09 nicklas 1 /*
4912 29 Apr 09 nicklas 2   $Id$
4912 29 Apr 09 nicklas 3
4912 29 Apr 09 nicklas 4   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg
4912 29 Apr 09 nicklas 5
4912 29 Apr 09 nicklas 6   This file is part of BASE - BioArray Software Environment.
4912 29 Apr 09 nicklas 7   Available at http://base.thep.lu.se/
4912 29 Apr 09 nicklas 8
4912 29 Apr 09 nicklas 9   BASE is free software; you can redistribute it and/or
4912 29 Apr 09 nicklas 10   modify it under the terms of the GNU General Public License
4912 29 Apr 09 nicklas 11   as published by the Free Software Foundation; either version 3
4912 29 Apr 09 nicklas 12   of the License, or (at your option) any later version.
4912 29 Apr 09 nicklas 13
4912 29 Apr 09 nicklas 14   BASE is distributed in the hope that it will be useful,
4912 29 Apr 09 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4912 29 Apr 09 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4912 29 Apr 09 nicklas 17   GNU General Public License for more details.
4912 29 Apr 09 nicklas 18
4912 29 Apr 09 nicklas 19   You should have received a copy of the GNU General Public License
4912 29 Apr 09 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4912 29 Apr 09 nicklas 21 */
4912 29 Apr 09 nicklas 22 package net.sf.basedb.util.jep;
4912 29 Apr 09 nicklas 23
4912 29 Apr 09 nicklas 24 import net.sf.basedb.core.BaseException;
4912 29 Apr 09 nicklas 25 import net.sf.basedb.core.DbControl;
5319 20 Apr 10 nicklas 26 import net.sf.basedb.core.DynamicQuery;
4912 29 Apr 09 nicklas 27 import net.sf.basedb.core.VirtualColumn;
4912 29 Apr 09 nicklas 28 import net.sf.basedb.core.query.Dynamic;
4912 29 Apr 09 nicklas 29 import net.sf.basedb.core.query.Expression;
4912 29 Apr 09 nicklas 30 import net.sf.basedb.core.query.SqlResult;
4912 29 Apr 09 nicklas 31 import net.sf.basedb.util.BioAssaySetUtil;
4912 29 Apr 09 nicklas 32
4912 29 Apr 09 nicklas 33 import java.sql.SQLException;
4912 29 Apr 09 nicklas 34 import java.util.Stack;
4912 29 Apr 09 nicklas 35
4912 29 Apr 09 nicklas 36 import org.nfunk.jep.Node;
4912 29 Apr 09 nicklas 37 import org.nfunk.jep.ParseException;
4912 29 Apr 09 nicklas 38
4912 29 Apr 09 nicklas 39 /**
4912 29 Apr 09 nicklas 40   A JEP function class that adds a <code>rawCh(int)</code> function to a 
4912 29 Apr 09 nicklas 41   JEP expression parser. The function will look up the (possible transformed) 
4912 29 Apr 09 nicklas 42   intensity value for the given channel. For example: <code>rawCh(1)</code>
4912 29 Apr 09 nicklas 43   <p>
4912 29 Apr 09 nicklas 44   Note! This function always return the values as they are stored in 
4912 29 Apr 09 nicklas 45   the database. Use {@link ChannelFunction} if you need the untransformed 
4912 29 Apr 09 nicklas 46   intensity values.  
4912 29 Apr 09 nicklas 47   <p>
4912 29 Apr 09 nicklas 48   To be able to use this function it must be registered with the JEP
4912 29 Apr 09 nicklas 49   parser and, before the expression is evaluated, a {@link SqlResult} object must be set.
4912 29 Apr 09 nicklas 50
4912 29 Apr 09 nicklas 51   @author Nicklas
4912 29 Apr 09 nicklas 52   @version 2.12
4912 29 Apr 09 nicklas 53   @base.modified $Date$
4912 29 Apr 09 nicklas 54   @see Jep
5319 20 Apr 10 nicklas 55   @see BioAssaySetUtil#createJepExpression(DbControl, String, DynamicQuery)
4912 29 Apr 09 nicklas 56 */
4912 29 Apr 09 nicklas 57 public class RawChannelFunction
4912 29 Apr 09 nicklas 58   implements JepExpressionFunction
4912 29 Apr 09 nicklas 59 {
4912 29 Apr 09 nicklas 60
4912 29 Apr 09 nicklas 61   private int[] channelToIndex;
4912 29 Apr 09 nicklas 62   private SqlResult result;
4912 29 Apr 09 nicklas 63   private int numParameters;
4912 29 Apr 09 nicklas 64
4912 29 Apr 09 nicklas 65   /**
4912 29 Apr 09 nicklas 66     Create a new instance of this function. The new instance cannot be used
4912 29 Apr 09 nicklas 67     to dynamically evaluate expressions. It should only be used for converting
4912 29 Apr 09 nicklas 68     JEP formulas to {@link Expression}:s.
4912 29 Apr 09 nicklas 69     @see Jep#formulaToExpression(String, JepFunction[])  
4912 29 Apr 09 nicklas 70   */
4912 29 Apr 09 nicklas 71   public RawChannelFunction()
4912 29 Apr 09 nicklas 72   {}
4912 29 Apr 09 nicklas 73   
4912 29 Apr 09 nicklas 74   /**
4912 29 Apr 09 nicklas 75     Create a new instance of this function which can be used
4912 29 Apr 09 nicklas 76     to dynamically evaluate expressions.
4912 29 Apr 09 nicklas 77     @param channelToIndex An array that maps channel numbers to column indexes in
4912 29 Apr 09 nicklas 78       the SqlResult, array position 0 maps the index of channel number 1, etc.
4912 29 Apr 09 nicklas 79     @see #setSqlResult(SqlResult)
4912 29 Apr 09 nicklas 80   */
4912 29 Apr 09 nicklas 81   public RawChannelFunction(int[] channelToIndex)
4912 29 Apr 09 nicklas 82   {
4912 29 Apr 09 nicklas 83     this.channelToIndex = channelToIndex;
4912 29 Apr 09 nicklas 84   }
4912 29 Apr 09 nicklas 85   
4912 29 Apr 09 nicklas 86   /*
4912 29 Apr 09 nicklas 87     From the JepFunction interface
4912 29 Apr 09 nicklas 88     -------------------------------------------
4912 29 Apr 09 nicklas 89   */
4912 29 Apr 09 nicklas 90   /**
4912 29 Apr 09 nicklas 91     @return The string "rawCh"
4912 29 Apr 09 nicklas 92   */
6127 14 Sep 12 nicklas 93   @Override
4912 29 Apr 09 nicklas 94   public String getFunctionName()
4912 29 Apr 09 nicklas 95   {
4912 29 Apr 09 nicklas 96     return "rawCh";
4912 29 Apr 09 nicklas 97   }
4912 29 Apr 09 nicklas 98   // -------------------------------------------
4912 29 Apr 09 nicklas 99   /*
4912 29 Apr 09 nicklas 100     From the JepExpressionFunction interface
4912 29 Apr 09 nicklas 101     -------------------------------------------
4912 29 Apr 09 nicklas 102   */
4912 29 Apr 09 nicklas 103   /**
4912 29 Apr 09 nicklas 104     Use the {@link Dynamic#column(VirtualColumn)} method to create an
4912 29 Apr 09 nicklas 105     expression referencing a channel intensity.
4912 29 Apr 09 nicklas 106   */
6127 14 Sep 12 nicklas 107   @Override
4912 29 Apr 09 nicklas 108   public Expression toExpression(Node node)
4912 29 Apr 09 nicklas 109   {
4912 29 Apr 09 nicklas 110     int numChildren = node.jjtGetNumChildren();
4912 29 Apr 09 nicklas 111     if (numChildren != 1)
4912 29 Apr 09 nicklas 112     {
4912 29 Apr 09 nicklas 113       throw new BaseException("Invalid number of expressions for 'ch' function: " + numChildren);
4912 29 Apr 09 nicklas 114     }
4912 29 Apr 09 nicklas 115     return Dynamic.column(VirtualColumn.channelRaw(Jep.nodeToInt(node.jjtGetChild(0))));
4912 29 Apr 09 nicklas 116   }
4912 29 Apr 09 nicklas 117   // -------------------------------------------
4912 29 Apr 09 nicklas 118   /*
4912 29 Apr 09 nicklas 119     From the PostfixMathCommandI interface
4912 29 Apr 09 nicklas 120     -------------------------------------------
4912 29 Apr 09 nicklas 121   */
4912 29 Apr 09 nicklas 122   /**
4912 29 Apr 09 nicklas 123     @return Always 1
4912 29 Apr 09 nicklas 124   */
6127 14 Sep 12 nicklas 125   @Override
4912 29 Apr 09 nicklas 126   public int getNumberOfParameters()
4912 29 Apr 09 nicklas 127   {
4912 29 Apr 09 nicklas 128     return 1;
4912 29 Apr 09 nicklas 129   }
6127 14 Sep 12 nicklas 130   @Override
4912 29 Apr 09 nicklas 131   public void setCurNumberOfParameters(int n)
4912 29 Apr 09 nicklas 132   {
4912 29 Apr 09 nicklas 133     this.numParameters = n;
4912 29 Apr 09 nicklas 134   }
6127 14 Sep 12 nicklas 135   @Override
4912 29 Apr 09 nicklas 136   public boolean checkNumberOfParameters(int n)
4912 29 Apr 09 nicklas 137   {
4912 29 Apr 09 nicklas 138     return n == 1;
4912 29 Apr 09 nicklas 139   }
6127 14 Sep 12 nicklas 140   @Override
6875 20 Apr 15 nicklas 141   @SuppressWarnings({"unchecked", "rawtypes"})
4912 29 Apr 09 nicklas 142   public void run(Stack stack)
4912 29 Apr 09 nicklas 143     throws ParseException
4912 29 Apr 09 nicklas 144   {
4912 29 Apr 09 nicklas 145     if (stack == null || stack.empty()) 
4912 29 Apr 09 nicklas 146     {
4912 29 Apr 09 nicklas 147       throw new ParseException("Stack is empty");
4912 29 Apr 09 nicklas 148     }
4912 29 Apr 09 nicklas 149     Object channel = stack.pop();
4912 29 Apr 09 nicklas 150     if (channel instanceof Number)
4912 29 Apr 09 nicklas 151     {
4912 29 Apr 09 nicklas 152       stack.push(channel(((Number)channel).intValue()));
4912 29 Apr 09 nicklas 153     }
4912 29 Apr 09 nicklas 154     else
4912 29 Apr 09 nicklas 155     {
4912 29 Apr 09 nicklas 156       throw new ParseException("Invalid parameter type: " + channel + "; expected integer");
4912 29 Apr 09 nicklas 157     }
4912 29 Apr 09 nicklas 158   }
4912 29 Apr 09 nicklas 159   // -------------------------------------------
4912 29 Apr 09 nicklas 160
4912 29 Apr 09 nicklas 161   /**
4912 29 Apr 09 nicklas 162     Set a new {@link SqlResult} object that will be used the next time the
4912 29 Apr 09 nicklas 163     JEP expression is evaluated.
4912 29 Apr 09 nicklas 164     @param result The result object
4912 29 Apr 09 nicklas 165   */
4912 29 Apr 09 nicklas 166   public void setSqlResult(SqlResult result)
4912 29 Apr 09 nicklas 167   {
4912 29 Apr 09 nicklas 168     this.result = result;
4912 29 Apr 09 nicklas 169   }
4912 29 Apr 09 nicklas 170   /**
4912 29 Apr 09 nicklas 171     Get the value of the specified channel of the current sql result.
4912 29 Apr 09 nicklas 172     @param channel The channel number
4912 29 Apr 09 nicklas 173      @throws ParseException Either if it's an invalid channel number or 
4912 29 Apr 09 nicklas 174        if no result object has been specified.
4912 29 Apr 09 nicklas 175   */
4912 29 Apr 09 nicklas 176   public Object channel(int channel)
4912 29 Apr 09 nicklas 177     throws ParseException
4912 29 Apr 09 nicklas 178   {
4912 29 Apr 09 nicklas 179     if (result == null)
4912 29 Apr 09 nicklas 180     {
4912 29 Apr 09 nicklas 181       throw new ParseException("No result object has been specified for function ch("+channel+")");
4912 29 Apr 09 nicklas 182     }
4912 29 Apr 09 nicklas 183     if (channelToIndex == null || channel < 1 || channel > channelToIndex.length)
4912 29 Apr 09 nicklas 184     {
4912 29 Apr 09 nicklas 185       throw new ParseException("Invalid channel number for function ch("+channel+")");
4912 29 Apr 09 nicklas 186     }
4912 29 Apr 09 nicklas 187     try
4912 29 Apr 09 nicklas 188     {
4912 29 Apr 09 nicklas 189       return result.getObject(channelToIndex[channel-1]);
4912 29 Apr 09 nicklas 190     }
4912 29 Apr 09 nicklas 191     catch (SQLException ex)
4912 29 Apr 09 nicklas 192     {
4912 29 Apr 09 nicklas 193       throw new ParseException(ex.getMessage());
4912 29 Apr 09 nicklas 194     }
4912 29 Apr 09 nicklas 195   }
4912 29 Apr 09 nicklas 196 }