Changeset 2131


Ignore:
Timestamp:
Mar 30, 2006, 12:12:16 PM (17 years ago)
Author:
Nicklas Nordborg
Message:

Replaced special JFreePlotServlet with the generic PlotServlet? which can
now also create correction factor plots.

Location:
trunk
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/servlet/PlotServlet.java

    r2116 r2131  
    2626
    2727import net.sf.basedb.core.Application;
     28import net.sf.basedb.core.BaseException;
    2829import net.sf.basedb.core.BioAssay;
    2930import net.sf.basedb.core.BioAssaySet;
     
    3435import net.sf.basedb.core.SessionControl;
    3536import net.sf.basedb.core.DbControl;
     37import net.sf.basedb.core.VirtualColumn;
     38import net.sf.basedb.core.query.Dynamic;
    3639import net.sf.basedb.core.query.Expression;
    3740import net.sf.basedb.core.query.Expressions;
    3841import net.sf.basedb.core.query.JoinType;
     42import net.sf.basedb.core.query.Orders;
    3943import net.sf.basedb.core.query.Selects;
     44import net.sf.basedb.core.query.SqlResult;
     45import net.sf.basedb.core.query.SqlResultIterator;
    4046import net.sf.basedb.clients.web.util.HTML;
    4147import net.sf.basedb.clients.web.util.Values;
     
    6268import java.io.OutputStream;
    6369import java.io.IOException;
     70import java.sql.SQLException;
     71import java.util.List;
    6472
    6573/**
     
    144152    try
    145153    {
    146       // Functions for parsing ch(n) and raw(property)
    147       JepFunction ch = new ChannelFunction();
    148       JepFunction raw = new RawFunction();
    149      
    150       // Convert formulas to Expression:s
    151       Expression[] x = xFormulas == null ? null : new Expression[xFormulas.length];
    152       Expression[] y = yFormulas == null ? null : new Expression[yFormulas.length];
    153       if (xFormulas != null)
    154       {
    155         for (int i = 0; i < xFormulas.length; ++i)
    156         {
    157           x[i] = Jep.formulaToExpression(xFormulas[i], ch, raw);
    158           if (xLog) x[i] = Expressions.log2(x[i]);
    159         }
    160       }
    161       if (yFormulas != null)
    162       {
    163         for (int i = 0; i < yFormulas.length; ++i)
    164         {
    165           y[i] = Jep.formulaToExpression(yFormulas[i], ch, raw);
    166           if (yLog) y[i] = Expressions.log2(y[i]);
    167         }
    168       }
    169 
    170       // Get the query
    171154      final SessionControl sc = Application.getSessionControl(ID, request.getRemoteAddr());
    172155      dc = sc.newDbControl();
    173156      BioAssay ba = bioAssayId == 0 ? null : BioAssay.getById(dc, bioAssayId);
    174157      BioAssaySet bas = ba == null ? BioAssaySet.getById(dc, bioAssaySetId) : ba.getBioAssaySet();
    175 
    176       DynamicSpotQuery query = ba != null ? ba.getSpotData() : bas.getSpotData();
    177 
     158     
    178159      JFreeChart chart = null;
    179       if ("scatter".equals(type))
    180       {
    181         ScatterPlot plot = new ScatterPlot(xLabel, yLabel);
    182         for (int i = 0; i < x.length; ++i)
    183         {
    184           query.reset();
    185           query.joinRawData(JoinType.INNER); // TODO - only join if used in query
    186           query.select(Selects.expression(x[i], "x"));
    187           query.select(Selects.expression(y[i], "y"));
    188           plot.addData(query.iterate(dc), Integer.toString(i));
    189         }
    190         chart = plot.getChart();
     160      if ("cfplot".equals(type))
     161      {
     162        chart = generateCFPlot(ba, xLabel, yLabel);
    191163      }
    192164      else
    193165      {
    194         HistogramPlot plot = new HistogramPlot(xLabel, yLabel);
    195         query.setReturnTotalCount(true);
    196         for (int i = 0; i < x.length; ++i)
    197         {
    198           query.reset();
    199           query.joinRawData(JoinType.INNER); // TODO - only join if used in query
    200           query.select(Selects.expression(x[i], "x"));
    201           if (!isCount) query.select(Selects.expression(y[i], "y"));
    202           plot.addData(query.iterate(dc), Integer.toString(i), binSize,
    203             HistogramPlot.YAggregate.valueOf(yAggregate.toUpperCase()));
    204         }
    205         chart = plot.getChart();
    206       }
     166        // Functions for parsing ch(n) and raw(property)
     167        JepFunction ch = new ChannelFunction();
     168        JepFunction raw = new RawFunction();
     169       
     170        // Convert formulas to Expression:s
     171        Expression[] x = xFormulas == null ? null : new Expression[xFormulas.length];
     172        Expression[] y = yFormulas == null ? null : new Expression[yFormulas.length];
     173        if (xFormulas != null)
     174        {
     175          for (int i = 0; i < xFormulas.length; ++i)
     176          {
     177            x[i] = Jep.formulaToExpression(xFormulas[i], ch, raw);
     178            if (xLog) x[i] = Expressions.log2(x[i]);
     179          }
     180        }
     181        if (yFormulas != null)
     182        {
     183          for (int i = 0; i < yFormulas.length; ++i)
     184          {
     185            y[i] = Jep.formulaToExpression(yFormulas[i], ch, raw);
     186            if (yLog) y[i] = Expressions.log2(y[i]);
     187          }
     188        }
     189 
     190        // Get the query
     191        DynamicSpotQuery query = ba != null ? ba.getSpotData() : bas.getSpotData();
     192 
     193        if ("scatter".equals(type))
     194        {
     195          ScatterPlot plot = new ScatterPlot(xLabel, yLabel);
     196          for (int i = 0; i < x.length; ++i)
     197          {
     198            query.reset();
     199            query.joinRawData(JoinType.INNER); // TODO - only join if used in query
     200            query.select(Selects.expression(x[i], "x"));
     201            query.select(Selects.expression(y[i], "y"));
     202            plot.addData(query.iterate(dc), Integer.toString(i));
     203          }
     204          chart = plot.getChart();
     205        }
     206        else
     207        {
     208          HistogramPlot plot = new HistogramPlot(xLabel, yLabel);
     209          query.setReturnTotalCount(true);
     210          for (int i = 0; i < x.length; ++i)
     211          {
     212            query.reset();
     213            query.joinRawData(JoinType.INNER); // TODO - only join if used in query
     214            query.select(Selects.expression(x[i], "x"));
     215            if (!isCount) query.select(Selects.expression(y[i], "y"));
     216            plot.addData(query.iterate(dc), Integer.toString(i), binSize,
     217              HistogramPlot.YAggregate.valueOf(yAggregate.toUpperCase()));
     218          }
     219          chart = plot.getChart();
     220        }
     221      }
     222     
    207223      if (title != null) chart.setTitle(title);
    208224      chart.removeLegend();
     
    301317    doGet(request, response);
    302318  }
     319 
     320 
     321  private JFreeChart generateCFPlot(BioAssay ba, String xLabel, String yLabel)
     322    throws Exception
     323  {
     324    DbControl dc = ba.getDbControl();
     325    // Get parent bioassay
     326    List<BioAssay> parents = ba.getParents().list(dc);
     327   
     328    if (parents.size() != 1)
     329    {
     330      throw new BaseException("A bioassay must have exactly one parent to generate a correction factor plot");
     331    }
     332   
     333    BioAssay parent = parents.get(0);
     334   
     335    // Generate MA-data for it
     336    JepFunction ch = new ChannelFunction();
     337    JepFunction raw = new RawFunction();
     338   
     339    Expression m = Jep.formulaToExpression("log2(ch(1) / ch(2))", ch);
     340    Expression a = Jep.formulaToExpression("log(ch(1) * ch(2)) / 2", ch);
     341    Expression position = Dynamic.column(VirtualColumn.POSITION);
     342   
     343    DynamicSpotQuery parentQuery = parent.getSpotData();
     344    parentQuery.select(Selects.expression(position, "pos"));
     345    parentQuery.select(Selects.expression(m, "m"));
     346    parentQuery.select(Selects.expression(a, "a"));
     347    parentQuery.order(Orders.asc(position));
     348
     349    DynamicSpotQuery query = ba.getSpotData();
     350    query.select(Selects.expression(position, "pos"));
     351    query.select(Selects.expression(m, "m"));
     352    query.order(Orders.asc(position));
     353   
     354    // Add CF-data to the plot
     355   
     356    SqlResultIterator cf = new CFResultIterator(parentQuery.iterate(dc), query.iterate(dc));
     357   
     358    ScatterPlot plot = new ScatterPlot(xLabel, yLabel);
     359    plot.addData(cf, "ma", "cf");
     360    return plot.getChart();
     361  }
     362 
     363 
     364  private static class CFResultIterator
     365    implements SqlResultIterator
     366  {
     367   
     368    private final SqlResultIterator parent;
     369    private final SqlResultIterator child;
     370    private final CFSqlResult next;
     371   
     372    CFResultIterator(SqlResultIterator parent, SqlResultIterator child)
     373    {
     374      this.parent = parent;
     375      this.child = child;
     376      this.next = new CFSqlResult();
     377    }
     378
     379    /*
     380      From the Iterator interface
     381      -------------------------------------------
     382    */
     383    public boolean hasNext()
     384    {
     385      return child.hasNext();
     386    }
     387    public SqlResult next()
     388    {
     389      try
     390      {
     391        SqlResult nextChild = child.next();
     392        while (parent.hasNext())
     393        {
     394          SqlResult nextParent = parent.next();
     395          if (nextChild.getInt(1) == nextParent.getInt(1))
     396          {
     397            next.setResult(nextParent, nextChild);
     398            return next;
     399          }
     400        }
     401      }
     402      catch (SQLException ex)
     403      {
     404        throw new BaseException(ex);
     405      }
     406      return null;
     407    }
     408    /**
     409      Not supported.
     410      @throws UnsupportedOperationException Always
     411    */
     412    public void remove()
     413    {
     414      throw new UnsupportedOperationException("remove");
     415    }
     416    // -------------------------------------------
     417
     418    /*
     419      From the QueryResult interface
     420      -------------------------------------------
     421    */
     422    public int getTotalCount()
     423    {
     424      return child.getTotalCount();
     425    }
     426    // -------------------------------------------
     427
     428    /*
     429      From the ResultIterator interface
     430      -------------------------------------------
     431    */
     432    public void close()
     433    {
     434      parent.close();
     435      child.close();
     436    }
     437    public boolean isClosed()
     438    {
     439      return child.isClosed();
     440    }
     441    // -------------------------------------------
     442
     443    /*
     444      From the SqlResultIterator interface
     445      -------------------------------------------
     446    */
     447    public int getIndex(String name)
     448      throws SQLException
     449    {
     450      if ("a".equals(name))
     451      {
     452        return 1;
     453      }
     454      else if ("m".equals(name))
     455      {
     456        return 2;
     457      }
     458      else if ("cf".equals(name))
     459      {
     460        return 3;
     461      }
     462      throw new SQLException("Unknown column name: " + name);
     463    }
     464    // -------------------------------------------
     465   
     466  }
     467 
     468  private static class CFSqlResult
     469    implements SqlResult
     470  {
     471   
     472    private SqlResult parent;
     473    private SqlResult child;
     474   
     475    CFSqlResult()
     476    {}
     477
     478    void setResult(SqlResult parent, SqlResult child)
     479    {
     480      this.parent = parent;
     481      this.child = child;
     482    }
     483   
     484    public float getFloat(int index)
     485      throws SQLException
     486    {
     487      switch (index)
     488      {
     489        case 1:
     490          return parent.getFloat(3);
     491        case 2:
     492          return parent.getFloat(2);
     493        case 3:
     494          return parent.getFloat(2) - child.getFloat(2);
     495        default:
     496          throw new ArrayIndexOutOfBoundsException(index);
     497      }
     498    }
     499    public int getInt(int index)
     500      throws SQLException
     501    {
     502      return (int)getFloat(index);
     503    }
     504    public Object getObject(int index)
     505      throws SQLException
     506    {
     507      return getFloat(index);
     508    }
     509    public short getShort(int index)
     510      throws SQLException
     511    {
     512      return (short)getFloat(index);
     513    }
     514    public String getString(int index)
     515      throws SQLException
     516    {
     517      return String.valueOf(getFloat(index));
     518    }
     519   
     520  }
    303521}
  • trunk/src/core/net/sf/basedb/util/plot/HistogramPlot.java

    r2116 r2131  
    2525package net.sf.basedb.util.plot;
    2626
    27 import net.sf.basedb.core.DynamicResultIterator;
    2827import net.sf.basedb.core.query.SqlResult;
     28import net.sf.basedb.core.query.SqlResultIterator;
    2929
    3030import org.jfree.chart.JFreeChart;
     
    7070  }
    7171
    72   public void addData(DynamicResultIterator data, String name, float binSize, YAggregate yAggregate)
     72  public void addData(SqlResultIterator data, String name, float binSize, YAggregate yAggregate)
    7373    throws SQLException
    7474  {
  • trunk/src/core/net/sf/basedb/util/plot/ScatterPlot.java

    r2087 r2131  
    2525package net.sf.basedb.util.plot;
    2626
    27 import net.sf.basedb.core.DynamicResultIterator;
    2827import net.sf.basedb.core.query.SqlResult;
     28import net.sf.basedb.core.query.SqlResultIterator;
    2929
    3030import org.jfree.chart.JFreeChart;
    3131import org.jfree.chart.axis.NumberAxis;
     32import org.jfree.chart.plot.SeriesRenderingOrder;
    3233import org.jfree.chart.plot.XYPlot;
    3334import org.jfree.chart.renderer.xy.XYDotRenderer;
     
    6162
    6263      plot = new XYPlot(allSeries, domainAxis, rangeAxis, new XYDotRenderer());
     64      plot.setSeriesRenderingOrder(SeriesRenderingOrder.FORWARD);
    6365    chart = new JFreeChart(plot);
    6466    chart.setAntiAlias(false);
    6567  }
    6668
    67   public void addData(DynamicResultIterator data, String name)
     69  public void addData(SqlResultIterator data, String... names)
    6870    throws SQLException
    6971  {
    70     XYSeries series = new XYSeries(name);
     72    XYSeries[] series = new XYSeries[names.length];
     73    for (int i = 0; i < names.length; ++i)
     74    {
     75      series[i] = new XYSeries(names[i]);
     76    }
    7177    while (data.hasNext())
    7278    {
    7379      SqlResult r = data.next();
    74       series.add(r.getFloat(1), r.getFloat(2));
     80      float x = r.getFloat(1);
     81      for (int i = 0; i < series.length; ++i)
     82      {
     83        series[i].add(x, r.getFloat(2+i));
     84      }
    7585    }
    76     allSeries.addSeries(series);
     86    for (int i = 0; i < series.length; ++i)
     87    {
     88      allSeries.addSeries(series[i]);
     89    }
    7790  }
    7891 
  • trunk/src/web.xml.in

    r2113 r2131  
    9696  </servlet-mapping>
    9797
    98   <!-- The PlotServlet used to generate overiew and correction factor plots -->
    99   <servlet>
    100     <servlet-name>jfreeplot</servlet-name>
    101     <servlet-class>
    102       net.sf.basedb.clients.web.servlet.JFreePlotServlet
    103     </servlet-class>
    104   </servlet>
    105    <servlet-mapping>
    106     <servlet-name>jfreeplot</servlet-name>
    107     <url-pattern>/views/experiments/plotter/jfreeplot/*</url-pattern>
    108   </servlet-mapping>
    109  
    11098  <!-- The Generic PlotServlet for the HTML plot tool -->
    11199  <servlet>
  • trunk/www/include/scripts/tabcontrol.js

    r2111 r2131  
    9090    }
    9191  }
     92 
     93  this.isActive = function(tabControlId, tabId)
     94  {
     95    var realTabId = tabControlId+'.'+tabId;
     96    var tabControl = document.getElementById(tabControlId);
     97    var tab = document.getElementById(realTabId);
     98    return tabControl.activeTab == tab;
     99  }
    92100
    93101  this.mouseEnter = function(tabControlId, tabId)
  • trunk/www/views/experiments/bioassaysets/view_bioassayset.jsp

    r2102 r2131  
    150150    function switchTab(tabControlId, tabId)
    151151    {
     152      if (TabControl.isActive(tabControlId, tabId)) return;
    152153      if (tabId == 'bioassays')
    153154      {
     
    369370     
    370371      <t:tab id="overviewplots" title="Overview plots"
     372        tooltip="MA-plots of each bioassay in this bioassay set"
    371373        visible="<%=rawDataType.getChannels() == 2%>">
    372374      <%
    373375      if ("overviewplots".equals(tabId))
    374376      {
    375         /*
    376         TODO - replace jfreeplot with generic plot servlet
    377377        String M = HTML.urlEncode("log2(ch(1) / ch(2))");
    378378        String A = HTML.urlEncode("log(ch(1) * ch(2)) / 2");
    379         String xName = HTML.urlEncode("A, log10(ch1 * ch2) / 2");
    380         String yName = HTML.urlEncode("M, log2(ch1 / ch2)");
    381         */
     379        String xLabel = HTML.urlEncode("A, log10(ch1 * ch2) / 2");
     380        String yLabel = HTML.urlEncode("M, log2(ch1 / ch2)");
    382381        ItemQuery<BioAssay> bioAssayQuery = bioAssaySet.getBioAssays();
    383382        bioAssayQuery.order(Orders.asc(Hql.property("name")));
    384383        for (BioAssay bioAssay : bioAssayQuery.list(dc))
    385384        {
    386           /*
    387385          String url = "../plotter/plot?ID="+ID+"&bioassay_id="+bioAssay.getId();
    388386          url += "&type=scatter&width=400&height=300";
    389           url += "&x="+A+"&y="+M+"&xaxis="+xName+"&yaxis="+yName;
    390           */
    391           String url = "../plotter/jfreeplot/"+ID+"/"+bioAssaySet.getId()+"/"+bioAssay.getId()+"/overviewPlot";
     387          url += "&x="+A+"&y="+M+"&xLabel="+xLabel+"&yLabel="+yLabel;
     388          url += "&title="+HTML.urlEncode(bioAssay.getName());
    392389          %>
    393           <img src="<%=url%>">
     390          <img src="<%=url%>" width="400" height="300"
     391            title="MA-plot for bioassay <%=HTML.encodeTags(bioAssay.getName())%>">
    394392          <%
    395393        }
     
    399397     
    400398      <t:tab id="cfplots" title="Correction factor plots"
     399        tooltip="MA-plot of the parent bioassays together with a correction factor for each spot in the current bioassayset."
    401400        visible="<%=rawDataType.getChannels() == 2 && bioAssaySet.getTransformation().getSource() != null %>">
    402401      <%
     
    405404        ItemQuery<BioAssay> bioAssayQuery = bioAssaySet.getBioAssays();
    406405        bioAssayQuery.order(Orders.asc(Hql.property("name")));
     406        String xLabel = HTML.urlEncode("A, log10(ch1 * ch2) / 2");
     407        String yLabel = HTML.urlEncode("M, log2(ch1 / ch2)");
    407408        for (BioAssay bioAssay : bioAssayQuery.list(dc))
    408409        {
    409           String url = "../plotter/jfreeplot/"+ID+"/"+bioAssaySet.getId()+"/"+bioAssay.getId()+"/correctionFactorPlot";
     410          String url = "../plotter/plot?ID="+ID+"&bioassay_id="+bioAssay.getId();
     411          url += "&type=cfplot&width=400&height=300";
     412          url += "&xLabel="+xLabel+"&yLabel="+yLabel;
     413          url += "&title="+HTML.urlEncode(bioAssay.getName());
    410414          %>
    411           <img src="<%=url%>">
     415          <img src="<%=url%>" width="400" height="300"
     416            title="Correction factor plot for bioassay <%=HTML.encodeTags(bioAssay.getName())%>">
    412417          <%
    413418        }
Note: See TracChangeset for help on using the changeset viewer.