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