JAL-1483 sort by feature score/density made first class alignment view controller...
[jalview.git] / src / jalview / controller / AlignViewController.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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 package jalview.controller;
22
23 import java.awt.Color;
24 import java.util.ArrayList;
25 import java.util.BitSet;
26 import java.util.List;
27
28 import jalview.analysis.AlignmentSorter;
29 import jalview.api.AlignViewControllerGuiI;
30 import jalview.api.AlignViewControllerI;
31 import jalview.api.AlignViewportI;
32 import jalview.api.AlignmentViewPanel;
33 import jalview.api.FeatureRenderer;
34 import jalview.commands.OrderCommand;
35 import jalview.datamodel.AlignmentI;
36 import jalview.datamodel.AnnotatedCollectionI;
37 import jalview.datamodel.ColumnSelection;
38 import jalview.datamodel.SequenceCollectionI;
39 import jalview.datamodel.SequenceFeature;
40 import jalview.datamodel.SequenceGroup;
41 import jalview.datamodel.SequenceI;
42
43 public class AlignViewController implements AlignViewControllerI
44 {
45   AlignViewportI viewport = null;
46
47   AlignmentViewPanel alignPanel = null;
48
49   /**
50    * the GUI container that is handling interactions with the user
51    */
52   private AlignViewControllerGuiI avcg;
53
54   @Override
55   protected void finalize() throws Throwable
56   {
57     viewport = null;
58     alignPanel = null;
59     avcg = null;
60   };
61
62   public AlignViewController(AlignViewControllerGuiI alignFrame,
63           AlignViewportI viewport, AlignmentViewPanel alignPanel)
64   {
65     this.avcg = alignFrame;
66     this.viewport = viewport;
67     this.alignPanel = alignPanel;
68   }
69
70   @Override
71   public void setViewportAndAlignmentPanel(AlignViewportI viewport,
72           AlignmentViewPanel alignPanel)
73   {
74     this.alignPanel = alignPanel;
75     this.viewport = viewport;
76
77   }
78
79   @Override
80   public boolean makeGroupsFromSelection()
81   {
82
83     if (viewport.getSelectionGroup() != null)
84     {
85       SequenceGroup[] gps = jalview.analysis.Grouping.makeGroupsFrom(
86               viewport.getSequenceSelection(),
87               viewport.getAlignmentView(true).getSequenceStrings(
88                       viewport.getGapCharacter()), viewport.getAlignment()
89                       .getGroups());
90       viewport.getAlignment().deleteAllGroups();
91       viewport.clearSequenceColours();
92       viewport.setSelectionGroup(null);
93       // set view properties for each group
94       for (int g = 0; g < gps.length; g++)
95       {
96         // gps[g].setShowunconserved(viewport.getShowUnconserved());
97         gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
98         viewport.getAlignment().addGroup(gps[g]);
99         Color col = new Color((int) (Math.random() * 255),
100                 (int) (Math.random() * 255), (int) (Math.random() * 255));
101         col = col.brighter();
102         for (SequenceI sq : gps[g].getSequences(null))
103           viewport.setSequenceColour(sq, col);
104       }
105       return true;
106     }
107     return false;
108   }
109
110   @Override
111   public boolean createGroup()
112   {
113
114     SequenceGroup sg = viewport.getSelectionGroup();
115     if (sg != null)
116     {
117       viewport.getAlignment().addGroup(sg);
118       return true;
119     }
120     return false;
121   }
122
123   @Override
124   public boolean unGroup()
125   {
126     SequenceGroup sg = viewport.getSelectionGroup();
127     if (sg != null)
128     {
129       viewport.getAlignment().deleteGroup(sg);
130       return true;
131     }
132     return false;
133   }
134
135   @Override
136   public boolean deleteGroups()
137   {
138     if (viewport.getAlignment().getGroups() != null
139             && viewport.getAlignment().getGroups().size() > 0)
140     {
141       viewport.getAlignment().deleteAllGroups();
142       viewport.clearSequenceColours();
143       viewport.setSelectionGroup(null);
144       return true;
145     }
146     return false;
147   }
148
149   @Override
150   public boolean markColumnsContainingFeatures(boolean invert,
151           boolean extendCurrent, boolean toggle, String featureType)
152   {
153     // JBPNote this routine could also mark rows, not just columns.
154     // need a decent query structure to allow all types of feature searches
155     BitSet bs = new BitSet();
156     int alw, alStart;
157     SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null ? viewport
158             .getAlignment() : viewport.getSelectionGroup());
159     alStart = sqcol.getStartRes();
160     alw = sqcol.getEndRes() + 1;
161     List<SequenceI> seqs = sqcol.getSequences();
162     int nseq = 0;
163     for (SequenceI sq : seqs)
164     {
165       int tfeat = 0;
166       if (sq != null)
167       {
168         SequenceI dsq = sq.getDatasetSequence();
169         while (dsq.getDatasetSequence() != null)
170         {
171           dsq = dsq.getDatasetSequence();
172         }
173         ;
174         SequenceFeature[] sf = dsq.getSequenceFeatures();
175         if (sf != null)
176         {
177           int ist = sq.findIndex(sq.getStart());
178           int iend = sq.findIndex(sq.getEnd());
179           if (iend < alStart || ist > alw)
180           {
181             // sequence not in region
182             continue;
183           }
184           for (SequenceFeature sfpos : sf)
185           {
186             // future functionalty - featureType == null means mark columns
187             // containing all displayed features
188             if (sfpos != null && (featureType.equals(sfpos.getType())))
189             {
190               tfeat++;
191               // optimisation - could consider 'spos,apos' like cursor argument
192               // - findIndex wastes time by starting from first character and
193               // counting
194
195               int i = sq.findIndex(sfpos.getBegin());
196               int j = sq.findIndex(sfpos.getEnd());
197               if (j < alStart || i > alw)
198               {
199                 // feature is outside selected region
200                 continue;
201               }
202               if (i < alStart)
203               {
204                 i = alStart;
205               }
206               if (i < ist)
207               {
208                 i = ist;
209               }
210               if (j > alw)
211               {
212                 j = alw;
213               }
214               for (; i <= j; i++)
215               {
216                 bs.set(i - 1);
217               }
218             }
219           }
220         }
221
222         if (tfeat > 0)
223         {
224           nseq++;
225         }
226       }
227     }
228     ColumnSelection cs = viewport.getColumnSelection();
229     if (bs.cardinality() > 0 || invert)
230     {
231       if (cs == null)
232       {
233         cs = new ColumnSelection();
234       }
235       else
236       {
237         if (!extendCurrent)
238         {
239           cs.clear();
240         }
241       }
242       if (invert)
243       {
244         // invert only in the currently selected sequence region
245         for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
246                 && i < (alw);)
247         {
248           if (ibs < 0 || i < ibs)
249           {
250             if (toggle && cs.contains(i))
251             {
252               cs.removeElement(i++);
253             }
254             else
255             {
256               cs.addElement(i++);
257             }
258           }
259           else
260           {
261             i = bs.nextClearBit(ibs);
262             ibs = bs.nextSetBit(i);
263           }
264         }
265       }
266       else
267       {
268         for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
269                 .nextSetBit(i + 1))
270         {
271           if (toggle && cs.contains(i))
272           {
273             cs.removeElement(i);
274           }
275           else
276           {
277             cs.addElement(i);
278           }
279         }
280       }
281       viewport.setColumnSelection(cs);
282       alignPanel.paintAlignment(true);
283       avcg.setStatus((toggle ? "Toggled " : "Marked ")
284               + (invert ? (alw - alStart) - bs.cardinality() : bs
285                       .cardinality()) + " columns "
286               + (invert ? "not " : "") + "containing features of type "
287               + featureType + " across " + nseq + " sequence(s)");
288       return true;
289     }
290     else
291     {
292       avcg.setStatus("No features of type " + featureType + " found.");
293       if (!extendCurrent && cs != null)
294       {
295         cs.clear();
296         alignPanel.paintAlignment(true);
297       }
298       return false;
299     }
300   }
301
302   @Override
303   public void sortAlignmentByFeatureDensity(String[] typ)
304   {
305     sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
306   }
307
308   protected void sortBy(String[] typ, String methodText, final String method)
309   {
310     FeatureRenderer fr = alignPanel.getFeatureRenderer();
311     if (typ == null)
312     {
313       typ = fr==null ? null : fr.getDisplayedFeatureTypes();
314     }
315     String gps[] = null;
316     gps = fr==null ? null : fr.getDisplayedFeatureGroups();
317     if (typ != null)
318     {
319       ArrayList types = new ArrayList();
320       for (int i = 0; i < typ.length; i++)
321       {
322         if (typ[i] != null)
323         {
324           types.add(typ[i]);
325         }
326         typ = new String[types.size()];
327         types.toArray(typ);
328       }
329     }
330     if (gps != null)
331     {
332       ArrayList grps = new ArrayList();
333
334       for (int i = 0; i < gps.length; i++)
335       {
336         if (gps[i] != null)
337         {
338           grps.add(gps[i]);
339         }
340       }
341       gps = new String[grps.size()];
342       grps.toArray(gps);
343     }
344     AlignmentI al = viewport.getAlignment();
345
346     int start, stop;
347     SequenceGroup sg = viewport.getSelectionGroup();
348     if (sg != null)
349     {
350       start = sg.getStartRes();
351       stop = sg.getEndRes();
352     }
353     else
354     {
355       start = 0;
356       stop = al.getWidth();
357     }
358     SequenceI[] oldOrder = al.getSequencesArray();
359     AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
360     avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
361             .getAlignment()));
362     alignPanel.paintAlignment(true);
363
364   }
365
366   @Override
367   public void sortAlignmentByFeatureScore(String[] typ)
368   {
369     sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
370   }
371 }