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