From cd340d96daf7a9f885cc1ec9061156f44ecc9fd4 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Mon, 17 Feb 2014 16:29:17 +0000 Subject: [PATCH] JAL-1445 groovy script demonstrating how to select columns according to a particular feature type --- .../groovy/selectColumnsByFeatureAndGroup.groovy | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 examples/groovy/selectColumnsByFeatureAndGroup.groovy diff --git a/examples/groovy/selectColumnsByFeatureAndGroup.groovy b/examples/groovy/selectColumnsByFeatureAndGroup.groovy new file mode 100644 index 0000000..0a16391 --- /dev/null +++ b/examples/groovy/selectColumnsByFeatureAndGroup.groovy @@ -0,0 +1,79 @@ +import jalview.analysis.*; +import jalview.datamodel.*; +import jalview.gui.AlignFrame; +import jalview.gui.AlignViewport; +import java.util.BitSet; +import javax.swing.JOptionPane; +import groovy.swing.SwingBuilder; +def toselect = getFeatureInput(); // change this to select the desired feature type + +def nal=0; +def nfeat=0; +def nseq=0; + +for (ala in Jalview.getAlignframes()) { + def al = ala.viewport.alignment; + if (al!=null) + { + BitSet bs = new BitSet(); + SequenceI[] seqs = al.getSequencesArray(); + for (sq in seqs) + { + def tfeat=0; + if (sq!=null) { + SequenceFeature[] sf = sq.getDatasetSequence().getSequenceFeatures(); + for (sfpos in sf) + { + if (sfpos!=null && sfpos.getType().equals(toselect)) + { + tfeat++; + int i=sq.findIndex(sfpos.getBegin()); + int ist=sq.findIndex(sq.getStart()); + if (ial.getWidth()) + { + j = al.getWidth(); + } + for (; i<=j; i++) + { + bs.set(i-1); + } + } + } + } + if (tfeat>0) { + nseq++; + nfeat+=tfeat; + } + } + if (bs.cardinality()>0) + { + nal ++; + ColumnSelection cs = ala.viewport.getColumnSelection(); + if (cs == null) { + cs = new ColumnSelection(); + } + for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { + cs.addElement(i); + } + ala.viewport.setColumnSelection(cs); + ala.alignPanel.paintAlignment(true); + ala.statusBar.setText("Marked "+bs.cardinality()+" columns containing features of type "+toselect) + } else { + ala.statusBar.setText("No features of type "+toselect+" found."); + } + } +} +return "Found a total of ${nfeat} features across ${nseq} sequences in ${nal} alignments."; + +String getFeatureInput(){ + def swingBuilder = new SwingBuilder(); + def response = JOptionPane.showInputDialog( + null, 'Select columns by feature by type','Enter type of feature', JOptionPane.OK_OPTION) + + return response + } \ No newline at end of file -- 1.7.10.2