3 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4 * Copyright (C) $$Year-Rel$$ The Jalview Authors
6 * This file is part of Jalview.
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.
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.
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.
23 <title>Extending Jalview with Groovy - Feature Counter Example</title>
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.
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>
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→Execute Groovy
40 Script</strong> from the alignment window's menu bar to run the script on
44 <strong>Please note: The 2.10.2 feature counting interface is not compatible with earlier versions.</strong><br/><br/>
46 href="http://www.jalview.org/examples/groovy/featuresCounter.groovy">http://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 <http://www.gnu.org/licenses/>.</span>
66 <span style="color: #408080; font-style: italic"> * The Jalview Authors are detailed in the 'AUTHORS' file.</span>
67 <span style="color: #408080; font-style: italic"> */</span>
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>
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>
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">'Phosphorylation'</span><span style="color: #666666">,</span> <span style="color: #BA2121">'Turn'</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">'Count of Phosphorylation features'</span><span style="color: #666666">,</span> <span style="color: #BA2121">'Count of Turn features'</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">-></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 'column 9' 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">'TURN'</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">'PHOSPHORYLATION'</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
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>