2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 SequenceFeature[] sf = sq.getSequenceFeatures();
185 int ist = sq.findIndex(sq.getStart());
186 int iend = sq.findIndex(sq.getEnd());
187 if (iend < alStart || ist > alw)
189 // sequence not in region
192 for (SequenceFeature sfpos : sf)
194 // future functionalty - featureType == null means mark columns
195 // containing all displayed features
196 if (sfpos != null && (featureType.equals(sfpos.getType())))
199 // optimisation - could consider 'spos,apos' like cursor argument
200 // - findIndex wastes time by starting from first character and
203 int i = sq.findIndex(sfpos.getBegin());
204 int j = sq.findIndex(sfpos.getEnd());
205 if (j < alStart || i > alw)
207 // feature is outside selected region
236 ColumnSelection cs = viewport.getColumnSelection();
237 if (bs.cardinality() > 0 || invert)
241 cs = new ColumnSelection();
252 // invert only in the currently selected sequence region
253 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
256 if (ibs < 0 || i < ibs)
258 if (toggle && cs.contains(i))
260 cs.removeElement(i++);
269 i = bs.nextClearBit(ibs);
270 ibs = bs.nextSetBit(i);
276 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
279 if (toggle && cs.contains(i))
289 viewport.setColumnSelection(cs);
290 alignPanel.paintAlignment(true);
291 avcg.setStatus(MessageManager.formatMessage("label.view_controller_toggled_marked",
293 (toggle ? MessageManager.getString("label.toggled") : MessageManager.getString("label.marked")),
294 (invert ? (Integer.valueOf((alw - alStart) - bs.cardinality()).toString()):(Integer.valueOf(bs.cardinality()).toString())),
295 featureType, Integer.valueOf(nseq).toString()
301 avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
302 if (!extendCurrent && cs != null)
305 alignPanel.paintAlignment(true);
314 public void sortAlignmentByFeatureDensity(String[] typ)
316 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
319 protected void sortBy(String[] typ, String methodText, final String method)
321 FeatureRenderer fr = alignPanel.getFeatureRenderer();
324 typ = fr==null ? null : fr.getDisplayedFeatureTypes();
327 gps = fr==null ? null : fr.getDisplayedFeatureGroups();
330 ArrayList types = new ArrayList();
331 for (int i = 0; i < typ.length; i++)
337 typ = new String[types.size()];
343 ArrayList grps = new ArrayList();
345 for (int i = 0; i < gps.length; i++)
352 gps = new String[grps.size()];
355 AlignmentI al = viewport.getAlignment();
358 SequenceGroup sg = viewport.getSelectionGroup();
361 start = sg.getStartRes();
362 stop = sg.getEndRes();
367 stop = al.getWidth();
369 SequenceI[] oldOrder = al.getSequencesArray();
370 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
371 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
373 alignPanel.paintAlignment(true);
378 public void sortAlignmentByFeatureScore(String[] typ)
380 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);