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 java.awt.Color;
24 import java.util.ArrayList;
25 import java.util.BitSet;
26 import java.util.List;
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;
42 public class AlignViewController implements AlignViewControllerI
44 AlignViewportI viewport = null;
46 AlignmentViewPanel alignPanel = null;
49 * the GUI container that is handling interactions with the user
51 private AlignViewControllerGuiI avcg;
54 protected void finalize() throws Throwable
61 public AlignViewController(AlignViewControllerGuiI alignFrame,
62 AlignViewportI viewport, AlignmentViewPanel alignPanel)
64 this.avcg = alignFrame;
65 this.viewport = viewport;
66 this.alignPanel = alignPanel;
70 public void setViewportAndAlignmentPanel(AlignViewportI viewport,
71 AlignmentViewPanel alignPanel)
73 this.alignPanel = alignPanel;
74 this.viewport = viewport;
79 public boolean makeGroupsFromSelection()
82 if (viewport.getSelectionGroup() != null)
84 SequenceGroup[] gps = jalview.analysis.Grouping.makeGroupsFrom(
85 viewport.getSequenceSelection(),
86 viewport.getAlignmentView(true).getSequenceStrings(
87 viewport.getGapCharacter()), viewport.getAlignment()
89 viewport.getAlignment().deleteAllGroups();
90 viewport.clearSequenceColours();
91 viewport.setSelectionGroup(null);
92 // set view properties for each group
93 for (int g = 0; g < gps.length; g++)
95 // gps[g].setShowunconserved(viewport.getShowUnconserved());
96 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
97 viewport.getAlignment().addGroup(gps[g]);
98 Color col = new Color((int) (Math.random() * 255),
99 (int) (Math.random() * 255), (int) (Math.random() * 255));
100 col = col.brighter();
101 for (SequenceI sq : gps[g].getSequences(null))
102 viewport.setSequenceColour(sq, col);
110 public boolean createGroup()
113 SequenceGroup sg = viewport.getSelectionGroup();
116 viewport.getAlignment().addGroup(sg);
123 public boolean unGroup()
125 SequenceGroup sg = viewport.getSelectionGroup();
128 viewport.getAlignment().deleteGroup(sg);
135 public boolean deleteGroups()
137 if (viewport.getAlignment().getGroups() != null
138 && viewport.getAlignment().getGroups().size() > 0)
140 viewport.getAlignment().deleteAllGroups();
141 viewport.clearSequenceColours();
142 viewport.setSelectionGroup(null);
149 public boolean markColumnsContainingFeatures(boolean invert,
150 boolean extendCurrent, boolean toggle, String featureType)
152 // JBPNote this routine could also mark rows, not just columns.
153 // need a decent query structure to allow all types of feature searches
154 BitSet bs = new BitSet();
156 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null ? viewport
157 .getAlignment() : viewport.getSelectionGroup());
158 alStart = sqcol.getStartRes();
159 alw = sqcol.getEndRes() + 1;
160 List<SequenceI> seqs = sqcol.getSequences();
162 for (SequenceI sq : seqs)
167 SequenceI dsq = sq.getDatasetSequence();
168 while (dsq.getDatasetSequence() != null)
170 dsq = dsq.getDatasetSequence();
173 SequenceFeature[] sf = dsq.getSequenceFeatures();
176 int ist = sq.findIndex(sq.getStart());
177 int iend = sq.findIndex(sq.getEnd());
178 if (iend < alStart || ist > alw)
180 // sequence not in region
183 for (SequenceFeature sfpos : sf)
185 // future functionalty - featureType == null means mark columns
186 // containing all displayed features
187 if (sfpos != null && (featureType.equals(sfpos.getType())))
190 // optimisation - could consider 'spos,apos' like cursor argument
191 // - findIndex wastes time by starting from first character and
194 int i = sq.findIndex(sfpos.getBegin());
195 int j = sq.findIndex(sfpos.getEnd());
196 if (j < alStart || i > alw)
198 // feature is outside selected region
227 ColumnSelection cs = viewport.getColumnSelection();
228 if (bs.cardinality() > 0 || invert)
232 cs = new ColumnSelection();
243 // invert only in the currently selected sequence region
244 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
247 if (ibs < 0 || i < ibs)
249 if (toggle && cs.contains(i))
251 cs.removeElement(i++);
260 i = bs.nextClearBit(ibs);
261 ibs = bs.nextSetBit(i);
267 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
270 if (toggle && cs.contains(i))
280 viewport.setColumnSelection(cs);
281 alignPanel.paintAlignment(true);
282 avcg.setStatus((toggle ? "Toggled " : "Marked ")
283 + (invert ? (alw - alStart) - bs.cardinality() : bs
284 .cardinality()) + " columns "
285 + (invert ? "not " : "") + "containing features of type "
286 + featureType + " across " + nseq + " sequence(s)");
291 avcg.setStatus("No features of type " + featureType + " found.");
292 if (!extendCurrent && cs != null)
295 alignPanel.paintAlignment(true);
302 public void sortAlignmentByFeatureDensity(String[] typ)
304 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
307 protected void sortBy(String[] typ, String methodText, final String method)
309 FeatureRenderer fr = alignPanel.getFeatureRenderer();
312 typ = fr==null ? null : fr.getDisplayedFeatureTypes();
315 gps = fr==null ? null : fr.getDisplayedFeatureGroups();
318 ArrayList types = new ArrayList();
319 for (int i = 0; i < typ.length; i++)
325 typ = new String[types.size()];
331 ArrayList grps = new ArrayList();
333 for (int i = 0; i < gps.length; i++)
340 gps = new String[grps.size()];
343 AlignmentI al = viewport.getAlignment();
346 SequenceGroup sg = viewport.getSelectionGroup();
349 start = sg.getStartRes();
350 stop = sg.getEndRes();
355 stop = al.getWidth();
357 SequenceI[] oldOrder = al.getSequencesArray();
358 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
359 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
361 alignPanel.paintAlignment(true);
366 public void sortAlignmentByFeatureScore(String[] typ)
368 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);