JAL-1665 getSequenceFeatures; Desktop.getAlignFrames camel-cased
[jalview.git] / examples / groovy / selectColumnsByFeatureAndGroup.groovy
1 import jalview.analysis.*;
2 import jalview.datamodel.*;
3 import jalview.gui.AlignFrame;
4 import jalview.gui.AlignViewport;
5 import java.util.BitSet;
6 import javax.swing.JOptionPane;
7 import groovy.swing.SwingBuilder;
8 def toselect = getFeatureInput(); // change this to select the desired feature type
9
10 def nal=0;
11 def nfeat=0;
12 def nseq=0;
13
14 for (ala in Jalview.getAlignFrames()) {
15   def al = ala.viewport.alignment;
16     if (al!=null)
17     {
18       BitSet bs = new BitSet();
19       SequenceI[] seqs = al.getSequencesArray();
20       for (sq in seqs)
21       {
22           def tfeat=0;
23         if (sq!=null) {
24           SequenceFeature[] sf = sq.getSequenceFeatures();
25           for (sfpos in sf)
26           {
27             if (sfpos!=null && sfpos.getType().equals(toselect))
28             {
29               tfeat++;
30               int i=sq.findIndex(sfpos.getBegin());
31               int ist=sq.findIndex(sq.getStart());
32               if (i<ist)
33               {
34                 i=ist;
35               }
36               int j=sq.findIndex(sfpos.getEnd());
37               if (j>al.getWidth())
38               {
39                 j = al.getWidth();
40               }
41               for (; i<=j; i++)
42               {
43                 bs.set(i-1);
44               }
45             }
46           }
47         }
48         if (tfeat>0) {
49             nseq++;
50             nfeat+=tfeat;
51         }
52       }
53       if (bs.cardinality()>0)
54       {
55         nal ++;
56         ColumnSelection cs = ala.viewport.getColumnSelection();
57         if (cs == null) {
58           cs = new ColumnSelection();
59         }
60     for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
61         cs.addElement(i);
62         }
63       ala.viewport.setColumnSelection(cs);
64       ala.alignPanel.paintAlignment(true);
65       ala.statusBar.setText("Marked "+bs.cardinality()+" columns containing features of type "+toselect)
66       } else {
67         ala.statusBar.setText("No features of type "+toselect+" found.");
68       }
69     }
70 }
71 return "Found a total of ${nfeat} features across ${nseq} sequences in ${nal} alignments.";
72     
73 String getFeatureInput(){
74         def swingBuilder = new SwingBuilder();
75         def response = JOptionPane.showInputDialog(
76                    null, 'Select columns by feature by type','Enter type of feature', JOptionPane.OK_OPTION)
77
78         return response
79     }