JAL-2189 format help
[jalview.git] / help / html / groovy / featureCounter.html
1 <html>
2 <!--
3  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4  * Copyright (C) $$Year-Rel$$ The Jalview Authors
5  * 
6  * This file is part of Jalview.
7  * 
8  * Jalview 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  * Jalview is distributed in the hope that it will be useful, but 
14  * WITHOUT ANY WARRANTY; without even the implied warranty 
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
16  * PURPOSE.  See the GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
20  * The Jalview Authors are detailed in the 'AUTHORS' file.
21  -->
22 <head>
23 <title>Extending Jalview with Groovy - Feature Counter Example</title>
24 </head>
25 <body>
26   <p>
27     <strong>Extending Jalview with Groovy - A customisable
28       feature counter</strong><br /> <br />The groovy script below shows how to
29     add a new calculation track to a Jalview alignment window.
30   </p>
31   <p>As currently written, it will add two tracks to a protein
32     alignment view which count Pfam features in each column, and ones
33     where a charge residue also occur.</p>
34   <p>To try it for yourself:</p>
35   <ol>
36     <li>Copy and paste it into the groovy script console</li>
37     <li>Load the example Feredoxin project (the one that opens by
38       default when you first launched Jalview)</li>
39     <li>Select <strong>Calculations&#8594;Execute Groovy
40         Script</strong> from the alignment window's menu bar to run the script on
41       the current view.
42     </li>
43   </ol>
44   <em><a
45     href="http://www.jalview.org/examples/groovy/featureCounter.groovy">http://www.jalview.org/examples/groovy/featureCounter.groovy</a>
46     - rendered with <a href="http://hilite.me">hilite.me</a></em>
47   <!-- HTML generated using hilite.me -->
48   <div
49     style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
50     <pre style="margin: 0; line-height: 125%">
51 <span style="color: #888888">/*</span>
52 <span style="color: #888888"> * Jalview - A Sequence Alignment Editor and Viewer (Version 2.10)</span>
53 <span style="color: #888888"> * Copyright (C) 2016 The Jalview Authors</span>
54 <span style="color: #888888"> * </span>
55 <span style="color: #888888"> * This file is part of Jalview.</span>
56 <span style="color: #888888"> * </span>
57 <span style="color: #888888"> * Jalview is free software: you can redistribute it and/or</span>
58 <span style="color: #888888"> * modify it under the terms of the GNU General Public License </span>
59 <span style="color: #888888"> * as published by the Free Software Foundation, either version 3</span>
60 <span style="color: #888888"> * of the License, or (at your option) any later version.</span>
61 <span style="color: #888888"> *  </span>
62 <span style="color: #888888"> * Jalview is distributed in the hope that it will be useful, but </span>
63 <span style="color: #888888"> * WITHOUT ANY WARRANTY; without even the implied warranty </span>
64 <span style="color: #888888"> * of MERCHANTABILITY or FITNESS FOR A PARTICULAR </span>
65 <span style="color: #888888"> * PURPOSE.  See the GNU General Public License for more details.</span>
66 <span style="color: #888888"> * </span>
67 <span style="color: #888888"> * You should have received a copy of the GNU General Public License</span>
68 <span style="color: #888888"> * along with Jalview.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
69 <span style="color: #888888"> * The Jalview Authors are detailed in the &#39;AUTHORS&#39; file.</span>
70 <span style="color: #888888"> */</span>
71  
72 <span style="color: #008800; font-weight: bold">import</span> <span
73         style="color: #0e84b5; font-weight: bold">jalview.workers.FeatureCounterI</span><span
74         style="color: #333333">;</span>
75 <span style="color: #008800; font-weight: bold">import</span> <span
76         style="color: #0e84b5; font-weight: bold">jalview.workers.AlignmentAnnotationFactory</span><span
77         style="color: #333333">;</span>
78
79 <span style="color: #888888">/*</span>
80 <span style="color: #888888"> * Example script that registers two alignment annotation calculators</span>
81 <span style="color: #888888"> * - one that counts residues in a column with Pfam annotation</span>
82 <span style="color: #888888"> * - one that counts only charged residues with Pfam annotation</span>
83 <span style="color: #888888"> *</span>
84 <span style="color: #888888"> * To try:</span>
85 <span style="color: #888888"> * 1. load uniref50.fa from the examples folder</span>
86 <span style="color: #888888"> * 2. load features onto it from from examples/exampleFeatures.txt</span>
87 <span style="color: #888888"> * 3. Open this script in the Groovy console.</span>
88 <span style="color: #888888"> * 4. Either execute this script from the console, or via Calculate-&gt;Run Groovy Script</span>
89 <span style="color: #888888"> </span>
90 <span style="color: #888888"> * To explore further, try changing this script to count other kinds of occurrences of </span>
91 <span style="color: #888888"> * residue and sequence features at columns in an alignment.</span>
92 <span style="color: #888888"> */</span>
93
94 <span style="color: #888888">/*</span>
95 <span style="color: #888888"> * A closure that returns true for any Charged residue</span>
96 <span style="color: #888888"> */</span>
97 <span style="color: #333399; font-weight: bold">def</span> isCharged <span
98         style="color: #333333">=</span> <span style="color: #333333">{</span> residue <span
99         style="color: #333333">-&gt;</span>
100     <span style="color: #008800; font-weight: bold">switch</span><span
101         style="color: #333333">(</span>residue<span
102         style="color: #333333">)</span> <span style="color: #333333">{</span>
103         <span style="color: #008800; font-weight: bold">case</span> <span
104         style="color: #333333">[</span><span
105         style="background-color: #fff0f0">&#39;D&#39;</span><span
106         style="color: #333333">,</span> <span
107         style="background-color: #fff0f0">&#39;d&#39;</span><span
108         style="color: #333333">,</span> <span
109         style="background-color: #fff0f0">&#39;E&#39;</span><span
110         style="color: #333333">,</span> <span
111         style="background-color: #fff0f0">&#39;e&#39;</span><span
112         style="color: #333333">,</span> <span
113         style="background-color: #fff0f0">&#39;H&#39;</span><span
114         style="color: #333333">,</span> <span
115         style="background-color: #fff0f0">&#39;h&#39;</span><span
116         style="color: #333333">,</span> <span
117         style="background-color: #fff0f0">&#39;K&#39;</span><span
118         style="color: #333333">,</span> <span
119         style="background-color: #fff0f0">&#39;k&#39;</span><span
120         style="color: #333333">,</span> <span
121         style="background-color: #fff0f0">&#39;R&#39;</span><span
122         style="color: #333333">,</span> <span
123         style="background-color: #fff0f0">&#39;r&#39;</span><span
124         style="color: #333333">]:</span>
125             <span style="color: #008800; font-weight: bold">return</span> <span
126         style="color: #008800; font-weight: bold">true</span>
127     <span style="color: #333333">}</span>
128     <span style="color: #008800; font-weight: bold">false</span>
129 <span style="color: #333333">}</span> 
130
131 <span style="color: #888888">/*</span>
132 <span style="color: #888888"> * A closure that returns 1 if sequence features include type &#39;Pfam&#39;, else 0</span>
133 <span style="color: #888888"> * Argument should be a list of SequenceFeature </span>
134 <span style="color: #888888"> */</span>
135 <span style="color: #333399; font-weight: bold">def</span> hasPfam <span
136         style="color: #333333">=</span> <span style="color: #333333">{</span> features <span
137         style="color: #333333">-&gt;</span> 
138     <span style="color: #008800; font-weight: bold">for</span> <span
139         style="color: #333333">(</span>sf <span
140         style="color: #008800; font-weight: bold">in</span> features<span
141         style="color: #333333">)</span>
142     <span style="color: #333333">{</span>
143         <span style="color: #888888">/*</span>
144 <span style="color: #888888">         * Here we inspect the type of the sequence feature.</span>
145 <span style="color: #888888">         * You can also test sf.description, sf.score, sf.featureGroup,</span>
146 <span style="color: #888888">         * sf.strand, sf.phase, sf.begin, sf.end</span>
147 <span style="color: #888888">         * or sf.getValue(attributeName) for GFF &#39;column 9&#39; properties</span>
148 <span style="color: #888888">         */</span>
149         <span style="color: #008800; font-weight: bold">if</span> <span
150         style="color: #333333">(</span><span
151         style="background-color: #fff0f0">&quot;Pfam&quot;</span><span
152         style="color: #333333">.</span><span style="color: #0000CC">equals</span><span
153         style="color: #333333">(</span>sf<span style="color: #333333">.</span><span
154         style="color: #0000CC">type</span><span style="color: #333333">))</span>
155         <span style="color: #333333">{</span>
156             <span style="color: #008800; font-weight: bold">return</span> <span
157         style="color: #008800; font-weight: bold">true</span>
158         <span style="color: #333333">}</span>
159     <span style="color: #333333">}</span>
160     <span style="color: #008800; font-weight: bold">false</span>
161 <span style="color: #333333">}</span>
162
163 <span style="color: #888888">/*</span>
164 <span style="color: #888888"> * Closure that computes an annotation based on </span>
165 <span style="color: #888888"> * presence of particular residues and features</span>
166 <span style="color: #888888"> * Parameters are</span>
167 <span style="color: #888888"> * - the name (label) for the alignment annotation</span>
168 <span style="color: #888888"> * - the description (tooltip) for the annotation</span>
169 <span style="color: #888888"> * - a closure (groovy function) that tests whether to include a residue</span>
170 <span style="color: #888888"> * - a closure that tests whether to increment count based on sequence features  </span>
171 <span style="color: #888888"> */</span>
172 <span style="color: #333399; font-weight: bold">def</span> getColumnCounter <span
173         style="color: #333333">=</span> <span style="color: #333333">{</span> name<span
174         style="color: #333333">,</span> desc<span style="color: #333333">,</span> acceptResidue<span
175         style="color: #333333">,</span> acceptFeatures <span
176         style="color: #333333">-&gt;</span>
177     <span style="color: #333333">[</span>
178      <span style="color: #997700; font-weight: bold">getName:</span> <span
179         style="color: #333333">{</span> name <span
180         style="color: #333333">},</span> 
181      <span style="color: #997700; font-weight: bold">getDescription:</span> <span
182         style="color: #333333">{</span> desc <span
183         style="color: #333333">},</span>
184      <span style="color: #997700; font-weight: bold">getMinColour:</span> <span
185         style="color: #333333">{</span> <span style="color: #333333">[</span><span
186         style="color: #0000DD; font-weight: bold">0</span><span
187         style="color: #333333">,</span> <span
188         style="color: #0000DD; font-weight: bold">255</span><span
189         style="color: #333333">,</span> <span
190         style="color: #0000DD; font-weight: bold">255</span><span
191         style="color: #333333">]</span> <span style="color: #333333">},</span> <span
192         style="color: #888888">// cyan</span>
193      <span style="color: #997700; font-weight: bold">getMaxColour:</span> <span
194         style="color: #333333">{</span> <span style="color: #333333">[</span><span
195         style="color: #0000DD; font-weight: bold">0</span><span
196         style="color: #333333">,</span> <span
197         style="color: #0000DD; font-weight: bold">0</span><span
198         style="color: #333333">,</span> <span
199         style="color: #0000DD; font-weight: bold">255</span><span
200         style="color: #333333">]</span> <span style="color: #333333">},</span> <span
201         style="color: #888888">// blue</span>
202      <span style="color: #997700; font-weight: bold">count:</span> 
203          <span style="color: #333333">{</span> res<span
204         style="color: #333333">,</span> feats <span
205         style="color: #333333">-&gt;</span> 
206             <span style="color: #333399; font-weight: bold">def</span> c <span
207         style="color: #333333">=</span> <span
208         style="color: #0000DD; font-weight: bold">0</span>
209             <span style="color: #008800; font-weight: bold">if</span> <span
210         style="color: #333333">(</span>acceptResidue<span
211         style="color: #333333">.</span><span style="color: #0000CC">call</span><span
212         style="color: #333333">(</span>res<span style="color: #333333">))</span>
213             <span style="color: #333333">{</span>
214                 <span style="color: #008800; font-weight: bold">if</span> <span
215         style="color: #333333">(</span>acceptFeatures<span
216         style="color: #333333">.</span><span style="color: #0000CC">call</span><span
217         style="color: #333333">(</span>feats<span style="color: #333333">))</span>
218                 <span style="color: #333333">{</span>
219                     c<span style="color: #333333">++</span>
220                 <span style="color: #333333">}</span>
221             <span style="color: #333333">}</span>
222             c
223          <span style="color: #333333">}</span>
224      <span style="color: #333333">]</span> <span
225         style="color: #008800; font-weight: bold">as</span> FeatureCounterI
226 <span style="color: #333333">}</span>
227
228 <span style="color: #888888">/*</span>
229 <span style="color: #888888"> * Define an annotation row that counts any residue with Pfam domain annotation</span>
230 <span style="color: #888888"> */</span>
231 <span style="color: #333399; font-weight: bold">def</span> pfamAnnotation <span
232         style="color: #333333">=</span> getColumnCounter<span
233         style="color: #333333">(</span><span
234         style="background-color: #fff0f0">&quot;Pfam&quot;</span><span
235         style="color: #333333">,</span> <span
236         style="background-color: #fff0f0">&quot;Count of residues with Pfam domain annotation&quot;</span><span
237         style="color: #333333">,</span> <span style="color: #333333">{</span><span
238         style="color: #008800; font-weight: bold">true</span><span
239         style="color: #333333">},</span> hasPfam<span
240         style="color: #333333">)</span>
241
242 <span style="color: #888888">/*</span>
243 <span style="color: #888888"> * Define an annotation row that counts charged residues with Pfam domain annotation</span>
244 <span style="color: #888888"> */</span>
245 <span style="color: #333399; font-weight: bold">def</span> chargedPfamAnnotation <span
246         style="color: #333333">=</span> getColumnCounter<span
247         style="color: #333333">(</span><span
248         style="background-color: #fff0f0">&quot;Pfam charged&quot;</span><span
249         style="color: #333333">,</span> <span
250         style="background-color: #fff0f0">&quot;Count of charged residues with Pfam domain annotation&quot;</span><span
251         style="color: #333333">,</span> isCharged<span
252         style="color: #333333">,</span> hasPfam<span
253         style="color: #333333">)</span>
254
255 <span style="color: #888888">/*</span>
256 <span style="color: #888888"> * Register the annotations</span>
257 <span style="color: #888888"> */</span>
258 AlignmentAnnotationFactory<span style="color: #333333">.</span><span
259         style="color: #0000CC">newCalculator</span><span
260         style="color: #333333">(</span>pfamAnnotation<span
261         style="color: #333333">)</span> 
262 AlignmentAnnotationFactory<span style="color: #333333">.</span><span
263         style="color: #0000CC">newCalculator</span><span
264         style="color: #333333">(</span>chargedPfamAnnotation<span
265         style="color: #333333">)</span>
266 </pre>
267   </div>
268 </body>
269 </html>