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

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

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

The current reporter name is used as a default subtitle.

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