2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.controller;
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;
38 import java.awt.Color;
39 import java.util.ArrayList;
40 import java.util.BitSet;
41 import java.util.List;
43 public class AlignViewController implements AlignViewControllerI
45 AlignViewportI viewport = null;
47 AlignmentViewPanel alignPanel = null;
50 * the GUI container that is handling interactions with the user
52 private AlignViewControllerGuiI avcg;
55 protected void finalize() throws Throwable
62 public AlignViewController(AlignViewControllerGuiI alignFrame,
63 AlignViewportI viewport, AlignmentViewPanel alignPanel)
65 this.avcg = alignFrame;
66 this.viewport = viewport;
67 this.alignPanel = alignPanel;
71 public void setViewportAndAlignmentPanel(AlignViewportI viewport,
72 AlignmentViewPanel alignPanel)
74 this.alignPanel = alignPanel;
75 this.viewport = viewport;
80 public boolean makeGroupsFromSelection()
82 SequenceGroup sg = viewport.getSelectionGroup();
83 ColumnSelection cs = viewport.getColumnSelection();
84 SequenceGroup[] gps = null;
86 && (cs == null || cs.getSelected() == null || cs.size() == 0))
88 gps = jalview.analysis.Grouping.makeGroupsFrom(
89 viewport.getSequenceSelection(),
90 viewport.getAlignmentView(true).getSequenceStrings(
91 viewport.getGapCharacter()), viewport.getAlignment()
95 gps = jalview.analysis.Grouping.makeGroupsFromCols(
96 (sg == null) ? viewport.getAlignment().getSequencesArray()
97 : sg.getSequences().toArray(new SequenceI[0]), cs,
98 viewport.getAlignment().getGroups());
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++)
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))
116 viewport.setSequenceColour(sq, col);
125 public boolean createGroup()
128 SequenceGroup sg = viewport.getSelectionGroup();
131 viewport.getAlignment().addGroup(sg);
138 public boolean unGroup()
140 SequenceGroup sg = viewport.getSelectionGroup();
143 viewport.getAlignment().deleteGroup(sg);
150 public boolean deleteGroups()
152 if (viewport.getAlignment().getGroups() != null
153 && viewport.getAlignment().getGroups().size() > 0)
155 viewport.getAlignment().deleteAllGroups();
156 viewport.clearSequenceColours();
157 viewport.setSelectionGroup(null);
164 public boolean markColumnsContainingFeatures(boolean invert,
165 boolean extendCurrent, boolean toggle, String featureType)
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();
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();
177 for (SequenceI sq : seqs)
182 SequenceI dsq = sq.getDatasetSequence();
183 while (dsq.getDatasetSequence() != null)
185 dsq = dsq.getDatasetSequence();
188 SequenceFeature[] sf = dsq.getSequenceFeatures();
191 int ist = sq.findIndex(sq.getStart());
192 int iend = sq.findIndex(sq.getEnd());
193 if (iend < alStart || ist > alw)
195 // sequence not in region
198 for (SequenceFeature sfpos : sf)
200 // future functionalty - featureType == null means mark columns
201 // containing all displayed features
202 if (sfpos != null && (featureType.equals(sfpos.getType())))
205 // optimisation - could consider 'spos,apos' like cursor argument
206 // - findIndex wastes time by starting from first character and
209 int i = sq.findIndex(sfpos.getBegin());
210 int j = sq.findIndex(sfpos.getEnd());
211 if (j < alStart || i > alw)
213 // feature is outside selected region
242 ColumnSelection cs = viewport.getColumnSelection();
243 if (bs.cardinality() > 0 || invert)
247 cs = new ColumnSelection();
258 // invert only in the currently selected sequence region
259 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
262 if (ibs < 0 || i < ibs)
264 if (toggle && cs.contains(i))
266 cs.removeElement(i++);
275 i = bs.nextClearBit(ibs);
276 ibs = bs.nextSetBit(i);
282 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
285 if (toggle && cs.contains(i))
295 viewport.setColumnSelection(cs);
296 alignPanel.paintAlignment(true);
297 avcg.setStatus(MessageManager.formatMessage("label.view_controller_toggled_marked",
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()
307 avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
308 if (!extendCurrent && cs != null)
311 alignPanel.paintAlignment(true);
320 public void sortAlignmentByFeatureDensity(String[] typ)
322 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
325 protected void sortBy(String[] typ, String methodText, final String method)
327 FeatureRenderer fr = alignPanel.getFeatureRenderer();
330 typ = fr==null ? null : fr.getDisplayedFeatureTypes();
333 gps = fr==null ? null : fr.getDisplayedFeatureGroups();
336 ArrayList types = new ArrayList();
337 for (int i = 0; i < typ.length; i++)
343 typ = new String[types.size()];
349 ArrayList grps = new ArrayList();
351 for (int i = 0; i < gps.length; i++)
358 gps = new String[grps.size()];
361 AlignmentI al = viewport.getAlignment();
364 SequenceGroup sg = viewport.getSelectionGroup();
367 start = sg.getStartRes();
368 stop = sg.getEndRes();
373 stop = al.getWidth();
375 SequenceI[] oldOrder = al.getSequencesArray();
376 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
377 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
379 alignPanel.paintAlignment(true);
384 public void sortAlignmentByFeatureScore(String[] typ)
386 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);