Merge branch 'features/JAL-1605_html-svg-export' into develop
[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.ColumnSelection;
37 import jalview.datamodel.SequenceCollectionI;
38 import jalview.datamodel.SequenceFeature;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.datamodel.SequenceI;
41 import jalview.util.MessageManager;
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(MessageManager.formatMessage("label.view_controller_toggled_marked",
284                   new String[]{
285                                 (toggle ? MessageManager.getString("label.toggled") : MessageManager.getString("label.marked")),
286                                 (invert ? (Integer.valueOf((alw - alStart) - bs.cardinality()).toString()):(Integer.valueOf(bs.cardinality()).toString())),
287                                 featureType, Integer.valueOf(nseq).toString()
288                         }));
289       return true;
290     }
291     else
292     {
293       avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
294       if (!extendCurrent && cs != null)
295       {
296         cs.clear();
297         alignPanel.paintAlignment(true);
298       }
299       return false;
300     }
301   }
302
303   @Override
304   public void sortAlignmentByFeatureDensity(String[] typ)
305   {
306     sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
307   }
308
309   protected void sortBy(String[] typ, String methodText, final String method)
310   {
311     FeatureRenderer fr = alignPanel.getFeatureRenderer();
312     if (typ == null)
313     {
314       typ = fr==null ? null : fr.getDisplayedFeatureTypes();
315     }
316     String gps[] = null;
317     gps = fr==null ? null : fr.getDisplayedFeatureGroups();
318     if (typ != null)
319     {
320       ArrayList types = new ArrayList();
321       for (int i = 0; i < typ.length; i++)
322       {
323         if (typ[i] != null)
324         {
325           types.add(typ[i]);
326         }
327         typ = new String[types.size()];
328         types.toArray(typ);
329       }
330     }
331     if (gps != null)
332     {
333       ArrayList grps = new ArrayList();
334
335       for (int i = 0; i < gps.length; i++)
336       {
337         if (gps[i] != null)
338         {
339           grps.add(gps[i]);
340         }
341       }
342       gps = new String[grps.size()];
343       grps.toArray(gps);
344     }
345     AlignmentI al = viewport.getAlignment();
346
347     int start, stop;
348     SequenceGroup sg = viewport.getSelectionGroup();
349     if (sg != null)
350     {
351       start = sg.getStartRes();
352       stop = sg.getEndRes();
353     }
354     else
355     {
356       start = 0;
357       stop = al.getWidth();
358     }
359     SequenceI[] oldOrder = al.getSequencesArray();
360     AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
361     avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
362             .getAlignment()));
363     alignPanel.paintAlignment(true);
364
365   }
366
367   @Override
368   public void sortAlignmentByFeatureScore(String[] typ)
369   {
370     sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
371   }
372 }