bd7144e59f8c9bd8e83e0439522aab6c2026391a
[jalview.git] / help / help / html / groovy / featuresCounter.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   <strong>Please note: The 2.10.2 feature counting interface is not compatible with earlier versions.</strong><br/><br/>
45   <em><a
46     href="https://www.jalview.org/examples/groovy/featuresCounter.groovy">https://www.jalview.org/examples/groovy/featuresCounter.groovy</a>
47     - rendered with <a href="http://hilite.me">hilite.me</a></em>
48   <!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #408080; font-style: italic">/*</span>
49 <span style="color: #408080; font-style: italic"> * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)</span>
50 <span style="color: #408080; font-style: italic"> * Copyright (C) $$Year-Rel$$ The Jalview Authors</span>
51 <span style="color: #408080; font-style: italic"> * </span>
52 <span style="color: #408080; font-style: italic"> * This file is part of Jalview.</span>
53 <span style="color: #408080; font-style: italic"> * </span>
54 <span style="color: #408080; font-style: italic"> * Jalview is free software: you can redistribute it and/or</span>
55 <span style="color: #408080; font-style: italic"> * modify it under the terms of the GNU General Public License </span>
56 <span style="color: #408080; font-style: italic"> * as published by the Free Software Foundation, either version 3</span>
57 <span style="color: #408080; font-style: italic"> * of the License, or (at your option) any later version.</span>
58 <span style="color: #408080; font-style: italic"> *  </span>
59 <span style="color: #408080; font-style: italic"> * Jalview is distributed in the hope that it will be useful, but </span>
60 <span style="color: #408080; font-style: italic"> * WITHOUT ANY WARRANTY; without even the implied warranty </span>
61 <span style="color: #408080; font-style: italic"> * of MERCHANTABILITY or FITNESS FOR A PARTICULAR </span>
62 <span style="color: #408080; font-style: italic"> * PURPOSE.  See the GNU General Public License for more details.</span>
63 <span style="color: #408080; font-style: italic"> * </span>
64 <span style="color: #408080; font-style: italic"> * You should have received a copy of the GNU General Public License</span>
65 <span style="color: #408080; font-style: italic"> * along with Jalview.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
66 <span style="color: #408080; font-style: italic"> * The Jalview Authors are detailed in the &#39;AUTHORS&#39; file.</span>
67 <span style="color: #408080; font-style: italic"> */</span>
68
69 <span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">jalview.workers.AlignmentAnnotationFactory</span><span style="color: #666666">;</span>
70 <span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">jalview.workers.FeatureSetCounterI</span><span style="color: #666666">;</span>
71
72 <span style="color: #408080; font-style: italic">/*</span>
73 <span style="color: #408080; font-style: italic"> * Example script to compute two alignment annotations</span>
74 <span style="color: #408080; font-style: italic"> * - count of Phosphorylation features</span>
75 <span style="color: #408080; font-style: italic"> * - count of Turn features</span>
76 <span style="color: #408080; font-style: italic"> * To try this, first load example file uniref50.fa and load on features file</span>
77 <span style="color: #408080; font-style: italic"> * exampleFeatures.txt, before running this script</span>
78 <span style="color: #408080; font-style: italic"> *</span>
79 <span style="color: #408080; font-style: italic"> * The script only needs to be run once - it will be registered by Jalview</span>
80 <span style="color: #408080; font-style: italic"> * and recalculated automatically when the alignment changes.</span>
81 <span style="color: #408080; font-style: italic"> * </span>
82 <span style="color: #408080; font-style: italic"> * Note: The feature api provided by 2.10.2 is not compatible with scripts</span>
83 <span style="color: #408080; font-style: italic"> * that worked with earlier Jalview versions. Apologies for the inconvenience.</span>
84 <span style="color: #408080; font-style: italic"> */</span>
85  
86 <span style="color: #B00040">def</span> annotator <span style="color: #666666">=</span> 
87     <span style="color: #666666">[</span>
88      <span style="color: #A0A000">getNames:</span> <span style="color: #666666">{</span> <span style="color: #666666">[</span><span style="color: #BA2121">&#39;Phosphorylation&#39;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&#39;Turn&#39;</span><span style="color: #666666">]</span> <span style="color: #008000; font-weight: bold">as</span> String<span style="color: #666666">[]</span> <span style="color: #666666">},</span> 
89      <span style="color: #A0A000">getDescriptions:</span>  <span style="color: #666666">{</span> <span style="color: #666666">[</span><span style="color: #BA2121">&#39;Count of Phosphorylation features&#39;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&#39;Count of Turn features&#39;</span><span style="color: #666666">]</span> <span style="color: #008000; font-weight: bold">as</span> String<span style="color: #666666">[]</span> <span style="color: #666666">},</span>
90      <span style="color: #A0A000">getMinColour:</span> <span style="color: #666666">{</span> <span style="color: #666666">[0,</span> <span style="color: #666666">255,</span> <span style="color: #666666">255]</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #B00040">int</span><span style="color: #666666">[]</span> <span style="color: #666666">},</span> <span style="color: #408080; font-style: italic">// cyan</span>
91      <span style="color: #A0A000">getMaxColour:</span> <span style="color: #666666">{</span> <span style="color: #666666">[0,</span> <span style="color: #666666">0,</span> <span style="color: #666666">255]</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #B00040">int</span><span style="color: #666666">[]</span> <span style="color: #666666">},</span> <span style="color: #408080; font-style: italic">// blue</span>
92      <span style="color: #A0A000">count:</span> 
93          <span style="color: #666666">{</span> res<span style="color: #666666">,</span> feats <span style="color: #666666">-&gt;</span> 
94                 <span style="color: #B00040">int</span> phos
95                 <span style="color: #B00040">int</span> turn
96                 <span style="color: #0000FF">for</span> <span style="color: #666666">(</span>sf <span style="color: #008000; font-weight: bold">in</span> feats<span style="color: #666666">)</span>
97                 <span style="color: #666666">{</span>
98                           <span style="color: #408080; font-style: italic">/*</span>
99 <span style="color: #408080; font-style: italic">                          * Here we inspect the type of the sequence feature.</span>
100 <span style="color: #408080; font-style: italic">                          * You can also test sf.description, sf.score, sf.featureGroup,</span>
101 <span style="color: #408080; font-style: italic">                          * sf.strand, sf.phase, sf.begin, sf.end</span>
102 <span style="color: #408080; font-style: italic">                          * or sf.getValue(attributeName) for GFF &#39;column 9&#39; properties</span>
103 <span style="color: #408080; font-style: italic">                          */</span>
104                            <span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">(</span>sf<span style="color: #666666">.</span><span style="color: #7D9029">type</span><span style="color: #666666">.</span><span style="color: #7D9029">contains</span><span style="color: #666666">(</span><span style="color: #BA2121">&#39;TURN&#39;</span><span style="color: #666666">))</span>
105                    <span style="color: #666666">{</span>
106                       turn<span style="color: #666666">++</span>
107                    <span style="color: #666666">}</span>
108                    <span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">(</span>sf<span style="color: #666666">.</span><span style="color: #7D9029">type</span><span style="color: #666666">.</span><span style="color: #7D9029">contains</span><span style="color: #666666">(</span><span style="color: #BA2121">&#39;PHOSPHORYLATION&#39;</span><span style="color: #666666">))</span>
109                    <span style="color: #666666">{</span>
110                       phos<span style="color: #666666">++</span>
111                    <span style="color: #666666">}</span>
112                 <span style="color: #666666">}</span>
113                 <span style="color: #666666">[</span>phos<span style="color: #666666">,</span> turn<span style="color: #666666">]</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #B00040">int</span><span style="color: #666666">[]</span>
114          <span style="color: #666666">}</span>
115      <span style="color: #666666">]</span> <span style="color: #008000; font-weight: bold">as</span> FeatureSetCounterI
116     
117 <span style="color: #408080; font-style: italic">/*</span>
118 <span style="color: #408080; font-style: italic"> * Register the annotation calculator with Jalview</span>
119 <span style="color: #408080; font-style: italic"> */</span>
120 AlignmentAnnotationFactory<span style="color: #666666">.</span><span style="color: #7D9029">newCalculator</span><span style="color: #666666">(</span>annotator<span style="color: #666666">)</span> 
121 </pre></div>
122 </body>
123 </html>