source: trunk/www/views/experiments/explorer/view/plotter.jsp @ 5138

Last change on this file since 5138 was 5138, checked in by Nicklas Nordborg, 13 years ago

References #1385 and #1386. Plot functions in experiment explorer

Both types of plots can now be generated and I think the percentile values are correctly calculated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 16.6 KB
Line 
1<%-- $Id: plotter.jsp 5138 2009-10-16 12:27:01Z nicklas $
2  ------------------------------------------------------------------
3  BioArray Software Environment (BASE) - http:// base.thep.lu.se/
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  BASE is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 3
11  of the License, or (at your option) any later version.
12
13  BASE is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with BASE. If not, see <http://www.gnu.org/licenses/>.
20  ------------------------------------------------------------------
21
22  @author Nicklas
23  @version 2.0
24--%>
25<%@ page session="false"
26  import="net.sf.basedb.core.SessionControl"
27  import="net.sf.basedb.core.DbControl"
28  import="net.sf.basedb.core.Experiment"
29  import="net.sf.basedb.core.BioAssaySet"
30  import="net.sf.basedb.core.BioAssay"
31  import="net.sf.basedb.core.RawDataType"
32  import="net.sf.basedb.core.RawDataProperty"
33  import="net.sf.basedb.core.Formula"
34  import="net.sf.basedb.core.IntensityTransform"
35  import="net.sf.basedb.core.AnnotationType"
36  import="net.sf.basedb.core.ItemQuery"
37  import="net.sf.basedb.core.ItemResultList"
38  import="net.sf.basedb.core.Include"
39  import="net.sf.basedb.core.Permission"
40  import="net.sf.basedb.core.Item"
41  import="net.sf.basedb.core.query.Orders"
42  import="net.sf.basedb.core.query.Hql"
43  import="net.sf.basedb.core.query.Restrictions"
44  import="net.sf.basedb.core.query.Restriction"
45  import="net.sf.basedb.core.query.Expressions"
46  import="net.sf.basedb.clients.web.ExperimentExplorer"
47  import="net.sf.basedb.clients.web.Base"
48  import="net.sf.basedb.clients.web.DynamicUtil"
49  import="net.sf.basedb.clients.web.util.HTML"
50  import="net.sf.basedb.util.Values"
51  import="net.sf.basedb.clients.web.WebException"
52  import="net.sf.basedb.clients.web.taglib.table.TableColumn"
53  import="java.util.List"
54  import="java.util.LinkedList"
55%>
56<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
57<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
58<%!
59private static void addFormulaOption(StringBuilder options, String formula, String title, String description)
60{
61  options.append("<option value=\"").append(HTML.encodeTags(formula)).append("\"");
62  options.append(" title=\"").append(HTML.encodeTags(description)).append("\"");
63  options.append(">").append(HTML.encodeTags(title)).append("\n");
64}
65%>
66<%
67final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
68final float scale = Base.getScale(sc);
69final String ID = sc.getId();
70final DbControl dc = sc.newDbControl();
71try
72{
73  final int bioAssaySetId = Values.getInt(request.getParameter("bioAssaySetId"));
74  final int reporterIndex = Values.getInt(request.getParameter("reporterIndex"));
75  final int positionIndex = Values.getInt(request.getParameter("positionIndex"));
76  final String plotType = Values.getString(request.getParameter("type"), "line");
77  final int annotationTypeId = Values.getInt(request.getParameter("annotationTypeId"));
78  final boolean needAverageMethod = positionIndex == ExperimentExplorer.SPOT_AVG;
79 
80  final BioAssaySet bas = BioAssaySet.getById(dc, bioAssaySetId);
81  final RawDataType rdt = bas.getRawDataType();
82  final Experiment experiment = bas.getExperiment();
83  final int maxRawMappings = bas.getMaxRawMappingsForSpot();
84  final IntensityTransform transform = bas.getIntensityTransform();
85  final ExperimentExplorer explorer = ExperimentExplorer.getExplorer(bas);
86  final List<AnnotationType> annotationTypes = explorer.getAnnotationTypes(dc, true);
87 
88  List<TableColumn> formulas = new LinkedList<TableColumn>();
89  DynamicUtil.addSpotColumns(formulas, dc, rdt.getChannels(), transform);
90  DynamicUtil.addFormulaColumns(formulas, dc, rdt, Formula.Type.COLUMN_EXPRESSION, 
91      transform, "", "", maxRawMappings == 1);
92  DynamicUtil.addExtraColumns(formulas, dc, bas, "ev", "#", "[Xtra] ");
93  if (maxRawMappings == 1)
94  {
95    DynamicUtil.addRawDataColumns(formulas, dc, rdt, "", "", "[Raw] ");
96  }
97 
98  StringBuilder formulaOptions = new StringBuilder();
99  for (TableColumn tc : formulas)
100  {
101    if (tc.getJepExpression() != null && tc.getDatatype().isNumerical())
102    {
103      if (!needAverageMethod || tc.getAverageMethod() != Formula.AverageMethod.NONE)
104      {
105        String jepExpression = tc.getJepExpression();
106        addFormulaOption(formulaOptions, jepExpression, tc.getTitle(), tc.getDescription());
107      }
108    }
109  }
110 
111  String title = HTML.encodeTags("Plot " + bas.getName());
112  final boolean hasCreateFilePermission = sc.hasPermission(Permission.CREATE, Item.FILE);
113  %>
114  <base:page type="popup" title="<%=title%>">
115  <base:head styles="tabcontrol.css" scripts="tabcontrol.js">
116  <script language="JavaScript">
117
118  var averageMethods = new Array();
119  <%
120  for (TableColumn tc : formulas)
121  {
122    if (tc.getJepExpression() != null && tc.getDatatype().isNumerical())
123    {
124      if (!needAverageMethod || tc.getAverageMethod() != Formula.AverageMethod.NONE)
125      {
126        %>
127        averageMethods[averageMethods.length] = '<%=tc.getAverageMethod().name()%>';
128        <%
129      }
130    }
131  }
132  %>
133 
134  function validate()
135  {
136    var plotType = getPlotType();
137    if (plotType == 'line')
138    {
139      return validateLinePlot();
140    }
141    else if (plotType == 'box')
142    {
143      return validateBoxPlot();
144    }
145    return false;
146  }
147 
148  function validateLinePlot()
149  {
150    var frm = document.forms['line'];
151    if (Main.trimString(frm.yFormula.value) == '')
152    {
153      alert("You must enter an expression for the Y axis");
154      frm.yFormula.focus();
155      return false;
156    }
157    return true;
158  }
159 
160  function validateBoxPlot()
161  {
162    var frm = document.forms['box'];
163    if (Main.trimString(frm.yFormula.value) == '')
164    {
165      alert("You must enter an expression for the Y axis");
166      frm.yFormula.focus();
167      return false;
168    }
169    return true;
170  }
171 
172  var plotType = '<%=plotType%>';
173  function getPlotType()
174  {
175    return plotType;
176  }
177
178  function switchTab(tabControlId, tabId)
179  {
180    if (tabId == 'line' || tabId == 'box')
181    {
182      plotType = tabId;
183    }
184    TabControl.setActiveTab(tabControlId, tabId);
185  }
186
187  function presetOnChange(list, formula, label)
188  {
189    var index = list.selectedIndex;
190    var frm = list.form;
191    if (frm.averageMethod)
192    {
193      var avgMethod = averageMethods[index-1];
194      Forms.selectListOption(frm.averageMethod, avgMethod);
195    }
196    formula.value = list[index].value;
197    if (label && list[index].value != '') label.value = list[index].text;
198    list.selectedIndex = 0;
199  }
200   
201  function generatePlotUrl(fullSize)
202  {
203    if (validate())
204    {
205      var plotFrm = document.forms['plot'];
206      var url = getRoot() + 'views/experiments/explorer/plot';
207      url += '?ID=<%=ID%>&bioAssaySetId=<%=bioAssaySetId%>';
208      url += '&reporterIndex=<%=reporterIndex%>&positionIndex=<%=positionIndex%>';
209      url += '&title='+Main.encodeURI(plotFrm.title.value);
210      url += '&subTitle='+Main.encodeURI(plotFrm.subTitle.value);
211      if (fullSize)
212      {
213        url += '&width='+plotFrm.width.value;
214        url += '&height='+plotFrm.height.value;
215      }
216      var plotType = getPlotType();
217      if (plotType == 'line')
218      {
219        var frm = document.forms['line'];
220        url += '&type=line';
221        url += '&y='+Main.encodeURI(frm.yFormula.value);
222        url += '&yLog='+(frm.yLog.checked ? 1 : 0);
223        url += '&yLabel='+Main.encodeURI(frm.yLabel.value);
224        url += '&showXLabels='+(frm.hideXLabels.checked ? 0 : 1);
225        if (frm.averageMethod)
226        {
227          url += '&averageMethod=' + frm.averageMethod[frm.averageMethod.selectedIndex].value;
228        }
229      }
230      else if (plotType == 'box')
231      {
232        var frm = document.forms['box'];
233        url += '&type=box';
234        url += '&y='+Main.encodeURI(frm.yFormula.value);
235        url += '&yLog='+(frm.yLog.checked ? 1 : 0);
236        url += '&yLabel='+Main.encodeURI(frm.yLabel.value);
237        url += '&annotationTypeId=' + frm.annotationTypeId[frm.annotationTypeId.selectedIndex].value;
238      }
239      url += '&' + new Date().getTime();
240      return url;
241    }
242  }
243 
244  function openExpressionBuilder(title, frmName, inputName, formulaType)
245  {
246    if (!document.forms[frmName][inputName].disabled)
247    {
248      var restrictions = formulaType == '<%=Formula.Type.COLUMN_RESTRICTION.name()%>';
249      Main.expressionBuilder('<%=ID%>', title, frmName, inputName, formulaType, '<%=rdt.getId()%>', <%=rdt.getChannels()%>, restrictions, <%=bas.getId()%>);
250    }
251  }
252 
253  function previewPlot()
254  {
255    var url = generatePlotUrl(false);
256    if (url)
257    {
258      url += '&width=600&height=400';
259      var image = document.getElementById('preview');
260      if (image.src.indexOf('plot_select') == -1)
261      {
262        var background = document.getElementById('background');
263        background.src = image.src;
264      }
265      image.src = getRoot()+'images/plot_generating.gif';
266      // Otherwise, the browser refuses to display the 'plot_generating.gif' while we are waiting.
267      //setTimeout('changePreviewImage()', 100);
268      image.realImg = new Image();
269      image.realImg.onload = changePreviewImage;
270      image.realImg.src = url;
271    }
272  }
273 
274  function changePreviewImage()
275  {
276    var image = document.getElementById('preview');
277    image.src = image.realImg.src;
278    image.realImg = null;
279  }
280 
281  function viewPlot()
282  {
283    var url = generatePlotUrl(true);
284    if (url)
285    {
286      var frm = document.forms['plot'];
287      var width = parseInt(frm.width.value);
288      var height = parseInt(frm.height.value);
289      if (!width || width < 600) width = 600;
290      if (!height || height < 400) height = 400;
291      Main.openPopup('../../plotter/view.jsp?ID=<%=ID%>&title='+Main.encodeURI(frm.title.value), 'ViewPlot', width+50, height+100);
292    }
293  }
294 
295  function downloadPlot()
296  {
297    var url = generatePlotUrl(true);
298    if (url)
299    {
300      Main.openPopup('../../plotter/download.jsp?ID=<%=ID%>', 'DownloadPlot', 500, 260);
301    }
302  }
303 
304  function savePlotAs()
305  {
306    var url = generatePlotUrl(true);
307    if (url)
308    {
309      Main.openPopup('../../plotter/save_as.jsp?ID=<%=ID%>', 'SavePlotAs', 500, 260);
310    }
311  }
312 
313  function init()
314  {
315   
316  }
317  </script>
318  </base:head>
319  <base:body onload="init()">
320 
321  <h3 class="docked"><%=title%> <base:help tabcontrol="plotType" /></h3>
322  <div class="boxed">
323 
324  <table border="0" cellspacing="0" cellpadding="2" width="100%">
325  <tr valign="bottom">
326    <td>
327    <form name="plot">
328    <table class="form">
329    <tr>
330      <td class="prompt">Plot title</td>
331      <td colspan="2"><input type="text" class="text" size="30" maxlength="255" name="title" 
332        value="<%=HTML.encodeTags(bas.getName())%>"></td>
333    </tr>
334    <tr>
335      <td class="prompt">Subtitle</td>
336      <td colspan="2"><input type="text" class="text" size="30" maxlength="255" name="subTitle" 
337        value=""></td>
338    </tr>
339    <tr>
340      <td class="prompt">Width</td>
341      <td><input type="text" class="text" size="12" maxlength="10" name="width" 
342        value="800" onkeypress="return Numbers.integerOnly(event)"></td>
343      <td rowspan="2" valign="center">(not used by preview)</td>
344    </tr>
345    <tr>
346      <td class="prompt">Height</td>
347      <td><input type="text" class="text" size="12" maxlength="10" name="height" 
348        value="600" onkeypress="return Numbers.integerOnly(event)"></td>
349    </tr>
350    </table>
351    </form>
352    <p>
353
354    <t:tabcontrol id="plotType" 
355      style="<%="width: "+(int)(scale*340)+"px;"%>" 
356      contentstyle="<%="height: "+(int)(scale*340)+"px;"%>"
357      active="<%=plotType%>" remember="false"
358      switch="switchTab">
359    <t:tab id="line" title="Line plot" helpid="explorer.lineplot"
360      tooltip="Create a line plot with bioassays along the x axis">
361      <form name="line">
362      <table border="0" cellspacing="0" cellpadding="2" class="form">
363      <tr>
364        <td class="prompt" colspan="3">Y-axis</td>
365      </tr>
366      <tr>
367        <td>&nbsp;Presets</td>
368        <td colspan="2">
369        <select name="yPresets" style="width: 20em;"
370          onchange="presetOnChange(this, this.form.yFormula, this.form.yLabel)" 
371          >
372          <option value="">- select from list or enter formula below -
373          <%=formulaOptions.toString()%>
374        </select>
375        </td>
376      </tr>
377      <tr>
378        <td>&nbsp;Expression</td>
379        <td><input type="text" class="text required" size="30" maxlength="255" name="yFormula"></td>
380        <td>
381          <base:button
382            title=""
383            image="expression_builder.gif"
384            tooltip="Use the Expression builder"
385            onclick="openExpressionBuilder('Y-axis expression', 'line', 'yFormula', 'COLUMN_EXPRESSION')"
386          />
387        </td>
388      </tr>
389      <tr>
390        <td>&nbsp;Label</td>
391        <td colspan="2"><input type="text" class="text" size="30" maxlength="255" name="yLabel"></td>
392      </tr>
393      <tr>
394        <td>&nbsp;Log scale</td>
395        <td colspan="2"><input type="checkbox" name="yLog" value="1"></td>
396      </tr>
397      <%
398      if (positionIndex == ExperimentExplorer.SPOT_AVG)
399      {
400        %>
401        <tr>
402          <td>&nbsp;Average</td>
403          <td>
404            <select name="averageMethod">
405            <%
406            for (Formula.AverageMethod method : Formula.AverageMethod.values())
407            {
408              if (method != Formula.AverageMethod.NONE)
409              {
410                %>
411                <option value="<%=method.name()%>"><%=method.toString()%></option>
412                <%
413              }
414            }
415            %>
416            </select>
417          </td>
418        <% 
419      }
420      %>
421      <tr>
422        <td class="prompt" colspan="3">X-axis</td>
423      </tr>
424      <tr>
425        <td>&nbsp;No labels</td>
426        <td><input type="checkbox" name="hideXLabels" value="1"></td>
427      </tr>
428      </table>
429      </form>
430    </t:tab>
431    <t:tab id="box" title="Box plot" helpid="explorer.boxplot"
432      tooltip="Create a box plot with annotation values along the x axis">
433      <form name="box">
434      <table border="0" cellspacing="0" cellpadding="2" class="form">
435      <tr>
436        <td class="prompt" colspan="3">Y-axis</td>
437      </tr>
438      <tr>
439        <td>&nbsp;Presets</td>
440        <td colspan="2">
441        <select name="yPresets" style="width: 20em;"
442          onchange="presetOnChange(this, this.form.yFormula, this.form.yLabel)" 
443          >
444          <option value="">- select from list or enter formula below -
445          <%=formulaOptions.toString()%>
446        </select>
447        </td>
448      </tr>
449      <tr>
450        <td>&nbsp;Expression</td>
451        <td><input type="text" class="text required" size="30" maxlength="255" name="yFormula"></td>
452        <td>
453          <base:button
454            title=""
455            image="expression_builder.gif"
456            tooltip="Use the Expression builder"
457            onclick="openExpressionBuilder('Y-axis expression', 'box', 'yFormula', 'COLUMN_EXPRESSION')"
458          />
459        </td>
460      </tr>
461      <tr>
462        <td>&nbsp;Label</td>
463        <td colspan="2"><input type="text" class="text" size="30" maxlength="255" name="yLabel"></td>
464      </tr>
465      <tr>
466        <td>&nbsp;Log scale</td>
467        <td colspan="2"><input type="checkbox" name="yLog" value="1"></td>
468      </tr>
469      <%
470      if (positionIndex == ExperimentExplorer.SPOT_AVG)
471      {
472        %>
473        <tr>
474          <td>&nbsp;Average</td>
475          <td>
476            <select name="averageMethod">
477            <%
478            for (Formula.AverageMethod method : Formula.AverageMethod.values())
479            {
480              if (method != Formula.AverageMethod.NONE)
481              {
482                %>
483                <option value="<%=method.name()%>"><%=method.toString()%></option>
484                <%
485              }
486            }
487            %>
488            </select>
489          </td>
490        <% 
491      }
492      %>
493      <tr>
494        <td class="prompt" colspan="3">X-axis</td>
495      </tr>
496      <tr>
497        <td>&nbsp;Annotation</td>
498        <td>
499          <select name="annotationTypeId">
500          <%
501          for (AnnotationType at : annotationTypes)
502          {
503            String selected = at.getId() == annotationTypeId ? "selected" : "";
504            %>
505            <option value="<%=at.getId()%>" <%=selected%>><%=HTML.encodeTags(at.getName())%>
506            <%
507          }
508          %>
509          </select>
510        </td>
511      </tr>
512      </table>
513      </form>
514    </t:tab>
515    </t:tabcontrol>
516    </td>
517    <td xstyle="width: 600px;">
518      <div style="position: relative; top: 0px; left: 0px; width: 600px; height: 400px; padding-bottom: 2px;">
519      <img src="../../../../images/plot_empty.png" id="background" 
520        style="position: absolute; top: 0px; left: 0px; z-index: 1; border: 1px solid #999999;">
521      <img src="../../../../images/plot_select.gif" id="preview" 
522        style="position: relative; top: 0px; left: 0px; z-index: 2; border: 1px solid #999999;">
523      </div>
524    </td>
525  </tr>
526  </table>
527  </div>
528    <p>
529    <div align="center">
530    <base:buttongroup>
531      <base:button title="Preview" onclick="previewPlot()" 
532        image="plotter_preview.gif" tooltip="Generate a preview of the plot" 
533      />
534      <base:button title="View&hellip;" onclick="viewPlot()" 
535        image="plotter.gif" tooltip="View a fullsized version of the plot (in a popup)" 
536      />
537      <base:button title="Download&hellip;" onclick="downloadPlot()" 
538        image="download.gif"
539        tooltip="Downlad a fullsized version of the plot to you computer" 
540      />
541      <base:button title="Save as&hellip;" onclick="savePlotAs()" 
542        image="<%=hasCreateFilePermission ? "saveas.gif" : "saveas_disabled.gif"%>"
543        disabled="<%=!hasCreateFilePermission%>"
544        tooltip="<%=hasCreateFilePermission ? 
545          "Save a fullsized version of the plot on the BASE server" :
546          "You don't have permission to create files" %>" 
547      />
548      <base:button onclick="window.close()" title="Close" />
549    </base:buttongroup>
550    </div>
551 
552  </base:body>
553  </base:page>
554  <%
555}
556finally
557{
558  if (dc != null) dc.close();
559}
560%>
Note: See TracBrowser for help on using the repository browser.