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;
41 import jalview.util.MessageManager;
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))
103 viewport.setSequenceColour(sq, col);
111 public boolean createGroup()
114 SequenceGroup sg = viewport.getSelectionGroup();
117 viewport.getAlignment().addGroup(sg);
124 public boolean unGroup()
126 SequenceGroup sg = viewport.getSelectionGroup();
129 viewport.getAlignment().deleteGroup(sg);
136 public boolean deleteGroups()
138 if (viewport.getAlignment().getGroups() != null
139 && viewport.getAlignment().getGroups().size() > 0)
141 viewport.getAlignment().deleteAllGroups();
142 viewport.clearSequenceColours();
143 viewport.setSelectionGroup(null);
150 public boolean markColumnsContainingFeatures(boolean invert,
151 boolean extendCurrent, boolean toggle, String featureType)
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();
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();
163 for (SequenceI sq : seqs)
168 SequenceI dsq = sq.getDatasetSequence();
169 while (dsq.getDatasetSequence() != null)
171 dsq = dsq.getDatasetSequence();
174 SequenceFeature[] sf = dsq.getSequenceFeatures();
177 int ist = sq.findIndex(sq.getStart());
178 int iend = sq.findIndex(sq.getEnd());
179 if (iend < alStart || ist > alw)
181 // sequence not in region
184 for (SequenceFeature sfpos : sf)
186 // future functionalty - featureType == null means mark columns
187 // containing all displayed features
188 if (sfpos != null && (featureType.equals(sfpos.getType())))
191 // optimisation - could consider 'spos,apos' like cursor argument
192 // - findIndex wastes time by starting from first character and
195 int i = sq.findIndex(sfpos.getBegin());
196 int j = sq.findIndex(sfpos.getEnd());
197 if (j < alStart || i > alw)
199 // feature is outside selected region
228 ColumnSelection cs = viewport.getColumnSelection();
229 if (bs.cardinality() > 0 || invert)
233 cs = new ColumnSelection();
244 // invert only in the currently selected sequence region
245 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
248 if (ibs < 0 || i < ibs)
250 if (toggle && cs.contains(i))
252 cs.removeElement(i++);
261 i = bs.nextClearBit(ibs);
262 ibs = bs.nextSetBit(i);
268 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
271 if (toggle && cs.contains(i))
281 viewport.setColumnSelection(cs);
282 alignPanel.paintAlignment(true);
283 avcg.setStatus(MessageManager.formatMessage("label.view_controller_toggled_marked",
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()
293 avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
294 if (!extendCurrent && cs != null)
297 alignPanel.paintAlignment(true);
304 public void sortAlignmentByFeatureDensity(String[] typ)
306 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
309 protected void sortBy(String[] typ, String methodText, final String method)
311 FeatureRenderer fr = alignPanel.getFeatureRenderer();
314 typ = fr==null ? null : fr.getDisplayedFeatureTypes();
317 gps = fr==null ? null : fr.getDisplayedFeatureGroups();
320 ArrayList types = new ArrayList();
321 for (int i = 0; i < typ.length; i++)
327 typ = new String[types.size()];
333 ArrayList grps = new ArrayList();
335 for (int i = 0; i < gps.length; i++)
342 gps = new String[grps.size()];
345 AlignmentI al = viewport.getAlignment();
348 SequenceGroup sg = viewport.getSelectionGroup();
351 start = sg.getStartRes();
352 stop = sg.getEndRes();
357 stop = al.getWidth();
359 SequenceI[] oldOrder = al.getSequencesArray();
360 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
361 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
363 alignPanel.paintAlignment(true);
368 public void sortAlignmentByFeatureScore(String[] typ)
370 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);