7e41f7703979316644dedf29b892dc0b2cc7685d
[jalview.git] / src / jalview / controller / AlignViewController.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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 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         SequenceI dsq = sq.getDatasetSequence();
183         while (dsq.getDatasetSequence() != null)
184         {
185           dsq = dsq.getDatasetSequence();
186         }
187         ;
188         SequenceFeature[] sf = dsq.getSequenceFeatures();
189         if (sf != null)
190         {
191           int ist = sq.findIndex(sq.getStart());
192           int iend = sq.findIndex(sq.getEnd());
193           if (iend < alStart || ist > alw)
194           {
195             // sequence not in region
196             continue;
197           }
198           for (SequenceFeature sfpos : sf)
199           {
200             // future functionalty - featureType == null means mark columns
201             // containing all displayed features
202             if (sfpos != null && (featureType.equals(sfpos.getType())))
203             {
204               tfeat++;
205               // optimisation - could consider 'spos,apos' like cursor argument
206               // - findIndex wastes time by starting from first character and
207               // counting
208
209               int i = sq.findIndex(sfpos.getBegin());
210               int j = sq.findIndex(sfpos.getEnd());
211               if (j < alStart || i > alw)
212               {
213                 // feature is outside selected region
214                 continue;
215               }
216               if (i < alStart)
217               {
218                 i = alStart;
219               }
220               if (i < ist)
221               {
222                 i = ist;
223               }
224               if (j > alw)
225               {
226                 j = alw;
227               }
228               for (; i <= j; i++)
229               {
230                 bs.set(i - 1);
231               }
232             }
233           }
234         }
235
236         if (tfeat > 0)
237         {
238           nseq++;
239         }
240       }
241     }
242     ColumnSelection cs = viewport.getColumnSelection();
243     if (bs.cardinality() > 0 || invert)
244     {
245       if (cs == null)
246       {
247         cs = new ColumnSelection();
248       }
249       else
250       {
251         if (!extendCurrent)
252         {
253           cs.clear();
254         }
255       }
256       if (invert)
257       {
258         // invert only in the currently selected sequence region
259         for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
260                 && i < (alw);)
261         {
262           if (ibs < 0 || i < ibs)
263           {
264             if (toggle && cs.contains(i))
265             {
266               cs.removeElement(i++);
267             }
268             else
269             {
270               cs.addElement(i++);
271             }
272           }
273           else
274           {
275             i = bs.nextClearBit(ibs);
276             ibs = bs.nextSetBit(i);
277           }
278         }
279       }
280       else
281       {
282         for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
283                 .nextSetBit(i + 1))
284         {
285           if (toggle && cs.contains(i))
286           {
287             cs.removeElement(i);
288           }
289           else
290           {
291             cs.addElement(i);
292           }
293         }
294       }
295       viewport.setColumnSelection(cs);
296       alignPanel.paintAlignment(true);
297       avcg.setStatus(MessageManager.formatMessage("label.view_controller_toggled_marked",
298                   new String[]{
299                                 (toggle ? MessageManager.getString("label.toggled") : MessageManager.getString("label.marked")),
300                                 (invert ? (Integer.valueOf((alw - alStart) - bs.cardinality()).toString()):(Integer.valueOf(bs.cardinality()).toString())),
301                                 featureType, Integer.valueOf(nseq).toString()
302                         }));
303       return true;
304     }
305     else
306     {
307       avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
308       if (!extendCurrent && cs != null)
309       {
310         cs.clear();
311         alignPanel.paintAlignment(true);
312       }
313       return false;
314     }
315   }
316
317
318
319   @Override
320   public void sortAlignmentByFeatureDensity(String[] typ)
321   {
322     sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
323   }
324
325   protected void sortBy(String[] typ, String methodText, final String method)
326   {
327     FeatureRenderer fr = alignPanel.getFeatureRenderer();
328     if (typ == null)
329     {
330       typ = fr==null ? null : fr.getDisplayedFeatureTypes();
331     }
332     String gps[] = null;
333     gps = fr==null ? null : fr.getDisplayedFeatureGroups();
334     if (typ != null)
335     {
336       ArrayList types = new ArrayList();
337       for (int i = 0; i < typ.length; i++)
338       {
339         if (typ[i] != null)
340         {
341           types.add(typ[i]);
342         }
343         typ = new String[types.size()];
344         types.toArray(typ);
345       }
346     }
347     if (gps != null)
348     {
349       ArrayList grps = new ArrayList();
350
351       for (int i = 0; i < gps.length; i++)
352       {
353         if (gps[i] != null)
354         {
355           grps.add(gps[i]);
356         }
357       }
358       gps = new String[grps.size()];
359       grps.toArray(gps);
360     }
361     AlignmentI al = viewport.getAlignment();
362
363     int start, stop;
364     SequenceGroup sg = viewport.getSelectionGroup();
365     if (sg != null)
366     {
367       start = sg.getStartRes();
368       stop = sg.getEndRes();
369     }
370     else
371     {
372       start = 0;
373       stop = al.getWidth();
374     }
375     SequenceI[] oldOrder = al.getSequencesArray();
376     AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
377     avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
378             .getAlignment()));
379     alignPanel.paintAlignment(true);
380
381   }
382
383   @Override
384   public void sortAlignmentByFeatureScore(String[] typ)
385   {
386     sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
387   }
388 }