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()
83 if (viewport.getSelectionGroup() != null)
85 SequenceGroup[] gps = jalview.analysis.Grouping.makeGroupsFrom(
86 viewport.getSequenceSelection(),
87 viewport.getAlignmentView(true).getSequenceStrings(
88 viewport.getGapCharacter()), viewport.getAlignment()
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++)
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))
104 viewport.setSequenceColour(sq, col);
113 public boolean createGroup()
116 SequenceGroup sg = viewport.getSelectionGroup();
119 viewport.getAlignment().addGroup(sg);
126 public boolean unGroup()
128 SequenceGroup sg = viewport.getSelectionGroup();
131 viewport.getAlignment().deleteGroup(sg);
138 public boolean deleteGroups()
140 if (viewport.getAlignment().getGroups() != null
141 && viewport.getAlignment().getGroups().size() > 0)
143 viewport.getAlignment().deleteAllGroups();
144 viewport.clearSequenceColours();
145 viewport.setSelectionGroup(null);
152 public boolean markColumnsContainingFeatures(boolean invert,
153 boolean extendCurrent, boolean toggle, String featureType)
155 // JBPNote this routine could also mark rows, not just columns.
156 // need a decent query structure to allow all types of feature searches
157 BitSet bs = new BitSet();
159 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null ? viewport
160 .getAlignment() : viewport.getSelectionGroup());
161 alStart = sqcol.getStartRes();
162 alw = sqcol.getEndRes() + 1;
163 List<SequenceI> seqs = sqcol.getSequences();
165 for (SequenceI sq : seqs)
170 SequenceI dsq = sq.getDatasetSequence();
171 while (dsq.getDatasetSequence() != null)
173 dsq = dsq.getDatasetSequence();
176 SequenceFeature[] sf = dsq.getSequenceFeatures();
179 int ist = sq.findIndex(sq.getStart());
180 int iend = sq.findIndex(sq.getEnd());
181 if (iend < alStart || ist > alw)
183 // sequence not in region
186 for (SequenceFeature sfpos : sf)
188 // future functionalty - featureType == null means mark columns
189 // containing all displayed features
190 if (sfpos != null && (featureType.equals(sfpos.getType())))
193 // optimisation - could consider 'spos,apos' like cursor argument
194 // - findIndex wastes time by starting from first character and
197 int i = sq.findIndex(sfpos.getBegin());
198 int j = sq.findIndex(sfpos.getEnd());
199 if (j < alStart || i > alw)
201 // feature is outside selected region
230 ColumnSelection cs = viewport.getColumnSelection();
231 if (bs.cardinality() > 0 || invert)
235 cs = new ColumnSelection();
246 // invert only in the currently selected sequence region
247 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
250 if (ibs < 0 || i < ibs)
252 if (toggle && cs.contains(i))
254 cs.removeElement(i++);
263 i = bs.nextClearBit(ibs);
264 ibs = bs.nextSetBit(i);
270 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
273 if (toggle && cs.contains(i))
283 viewport.setColumnSelection(cs);
284 alignPanel.paintAlignment(true);
285 avcg.setStatus(MessageManager.formatMessage("label.view_controller_toggled_marked",
287 (toggle ? MessageManager.getString("label.toggled") : MessageManager.getString("label.marked")),
288 (invert ? (Integer.valueOf((alw - alStart) - bs.cardinality()).toString()):(Integer.valueOf(bs.cardinality()).toString())),
289 featureType, Integer.valueOf(nseq).toString()
295 avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
296 if (!extendCurrent && cs != null)
299 alignPanel.paintAlignment(true);
308 public void sortAlignmentByFeatureDensity(String[] typ)
310 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
313 protected void sortBy(String[] typ, String methodText, final String method)
315 FeatureRenderer fr = alignPanel.getFeatureRenderer();
318 typ = fr==null ? null : fr.getDisplayedFeatureTypes();
321 gps = fr==null ? null : fr.getDisplayedFeatureGroups();
324 ArrayList types = new ArrayList();
325 for (int i = 0; i < typ.length; i++)
331 typ = new String[types.size()];
337 ArrayList grps = new ArrayList();
339 for (int i = 0; i < gps.length; i++)
346 gps = new String[grps.size()];
349 AlignmentI al = viewport.getAlignment();
352 SequenceGroup sg = viewport.getSelectionGroup();
355 start = sg.getStartRes();
356 stop = sg.getEndRes();
361 stop = al.getWidth();
363 SequenceI[] oldOrder = al.getSequencesArray();
364 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
365 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
367 alignPanel.paintAlignment(true);
372 public void sortAlignmentByFeatureScore(String[] typ)
374 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);