JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / examples / groovy / selectColumnsByFeatureAndGroup.groovy
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 import jalview.analysis.*;
22 import jalview.datamodel.*;
23 import jalview.gui.AlignFrame;
24 import jalview.gui.AlignViewport;
25 import java.util.BitSet;
26 import javax.swing.JOptionPane;
27 import groovy.swing.SwingBuilder;
28 def toselect = getFeatureInput(); // change this to select the desired feature type
29
30 def nal=0;
31 def nfeat=0;
32 def nseq=0;
33
34 for (ala in Jalview.getAlignFrames()) {
35   def al = ala.viewport.alignment;
36     if (al!=null)
37     {
38       BitSet bs = new BitSet();
39       SequenceI[] seqs = al.getSequencesArray();
40       for (sq in seqs)
41       {
42           def tfeat=0;
43         if (sq!=null) {
44           SequenceFeature[] sf = sq.getSequenceFeatures();
45           for (sfpos in sf)
46           {
47             if (sfpos!=null && sfpos.getType().equals(toselect))
48             {
49               tfeat++;
50               int i=sq.findIndex(sfpos.getBegin());
51               int ist=sq.findIndex(sq.getStart());
52               if (i<ist)
53               {
54                 i=ist;
55               }
56               int j=sq.findIndex(sfpos.getEnd());
57               if (j>al.getWidth())
58               {
59                 j = al.getWidth();
60               }
61               for (; i<=j; i++)
62               {
63                 bs.set(i-1);
64               }
65             }
66           }
67         }
68         if (tfeat>0) {
69             nseq++;
70             nfeat+=tfeat;
71         }
72       }
73       if (bs.cardinality()>0)
74       {
75         nal ++;
76         ColumnSelection cs = ala.viewport.getColumnSelection();
77         if (cs == null) {
78           cs = new ColumnSelection();
79         }
80     for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
81         cs.addElement(i);
82         }
83       ala.viewport.setColumnSelection(cs);
84       ala.alignPanel.paintAlignment(true);
85       ala.statusBar.setText("Marked "+bs.cardinality()+" columns containing features of type "+toselect)
86       } else {
87         ala.statusBar.setText("No features of type "+toselect+" found.");
88       }
89     }
90 }
91 return "Found a total of ${nfeat} features across ${nseq} sequences in ${nal} alignments.";
92     
93 String getFeatureInput(){
94         def swingBuilder = new SwingBuilder();
95         def response = JOptionPane.showInputDialog(
96                    null, 'Select columns by feature by type','Enter type of feature', JOptionPane.OK_OPTION)
97
98         return response
99     }