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.io.FeaturesFile;
37 import jalview.util.MessageManager;
39 import java.awt.Color;
40 import java.util.ArrayList;
41 import java.util.BitSet;
42 import java.util.List;
44 public class AlignViewController implements AlignViewControllerI
46 AlignViewportI viewport = null;
48 AlignmentViewPanel alignPanel = null;
51 * the GUI container that is handling interactions with the user
53 private AlignViewControllerGuiI avcg;
56 protected void finalize() throws Throwable
63 public AlignViewController(AlignViewControllerGuiI alignFrame,
64 AlignViewportI viewport, AlignmentViewPanel alignPanel)
66 this.avcg = alignFrame;
67 this.viewport = viewport;
68 this.alignPanel = alignPanel;
72 public void setViewportAndAlignmentPanel(AlignViewportI viewport,
73 AlignmentViewPanel alignPanel)
75 this.alignPanel = alignPanel;
76 this.viewport = viewport;
81 public boolean makeGroupsFromSelection()
83 SequenceGroup sg = viewport.getSelectionGroup();
84 ColumnSelection cs = viewport.getColumnSelection();
85 SequenceGroup[] gps = null;
87 && (cs == null || cs.getSelected() == null || cs.size() == 0))
89 gps = jalview.analysis.Grouping.makeGroupsFrom(
90 viewport.getSequenceSelection(),
91 viewport.getAlignmentView(true).getSequenceStrings(
92 viewport.getGapCharacter()), viewport.getAlignment()
96 gps = jalview.analysis.Grouping.makeGroupsFromCols(
97 (sg == null) ? viewport.getAlignment().getSequencesArray()
98 : sg.getSequences().toArray(new SequenceI[0]), cs,
99 viewport.getAlignment().getGroups());
103 viewport.getAlignment().deleteAllGroups();
104 viewport.clearSequenceColours();
105 viewport.setSelectionGroup(null);
106 // set view properties for each group
107 for (int g = 0; g < gps.length; g++)
109 // gps[g].setShowunconserved(viewport.getShowUnconserved());
110 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
111 viewport.getAlignment().addGroup(gps[g]);
112 Color col = new Color((int) (Math.random() * 255),
113 (int) (Math.random() * 255), (int) (Math.random() * 255));
114 col = col.brighter();
115 for (SequenceI sq : gps[g].getSequences(null))
117 viewport.setSequenceColour(sq, col);
126 public boolean createGroup()
129 SequenceGroup sg = viewport.getSelectionGroup();
132 viewport.getAlignment().addGroup(sg);
139 public boolean unGroup()
141 SequenceGroup sg = viewport.getSelectionGroup();
144 viewport.getAlignment().deleteGroup(sg);
151 public boolean deleteGroups()
153 if (viewport.getAlignment().getGroups() != null
154 && viewport.getAlignment().getGroups().size() > 0)
156 viewport.getAlignment().deleteAllGroups();
157 viewport.clearSequenceColours();
158 viewport.setSelectionGroup(null);
165 public boolean markColumnsContainingFeatures(boolean invert,
166 boolean extendCurrent, boolean toggle, String featureType)
168 // JBPNote this routine could also mark rows, not just columns.
169 // need a decent query structure to allow all types of feature searches
170 BitSet bs = new BitSet();
172 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null ? viewport
173 .getAlignment() : viewport.getSelectionGroup());
174 alStart = sqcol.getStartRes();
175 alw = sqcol.getEndRes() + 1;
176 List<SequenceI> seqs = sqcol.getSequences();
178 for (SequenceI sq : seqs)
183 SequenceFeature[] sf = sq.getSequenceFeatures();
186 int ist = sq.findIndex(sq.getStart());
187 int iend = sq.findIndex(sq.getEnd());
188 if (iend < alStart || ist > alw)
190 // sequence not in region
193 for (SequenceFeature sfpos : sf)
195 // future functionalty - featureType == null means mark columns
196 // containing all displayed features
197 if (sfpos != null && (featureType.equals(sfpos.getType())))
200 // optimisation - could consider 'spos,apos' like cursor argument
201 // - findIndex wastes time by starting from first character and
204 int i = sq.findIndex(sfpos.getBegin());
205 int j = sq.findIndex(sfpos.getEnd());
206 if (j < alStart || i > alw)
208 // feature is outside selected region
237 ColumnSelection cs = viewport.getColumnSelection();
238 if (bs.cardinality() > 0 || invert)
242 cs = new ColumnSelection();
253 // invert only in the currently selected sequence region
254 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
257 if (ibs < 0 || i < ibs)
259 if (toggle && cs.contains(i))
261 cs.removeElement(i++);
270 i = bs.nextClearBit(ibs);
271 ibs = bs.nextSetBit(i);
277 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
280 if (toggle && cs.contains(i))
290 viewport.setColumnSelection(cs);
291 alignPanel.paintAlignment(true);
292 avcg.setStatus(MessageManager.formatMessage("label.view_controller_toggled_marked",
294 (toggle ? MessageManager.getString("label.toggled") : MessageManager.getString("label.marked")),
295 (invert ? (Integer.valueOf((alw - alStart) - bs.cardinality()).toString()):(Integer.valueOf(bs.cardinality()).toString())),
296 featureType, Integer.valueOf(nseq).toString()
302 avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
303 if (!extendCurrent && cs != null)
306 alignPanel.paintAlignment(true);
315 public void sortAlignmentByFeatureDensity(String[] typ)
317 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
320 protected void sortBy(String[] typ, String methodText, final String method)
322 FeatureRenderer fr = alignPanel.getFeatureRenderer();
325 typ = fr==null ? null : fr.getDisplayedFeatureTypes();
328 gps = fr==null ? null : fr.getDisplayedFeatureGroups();
331 ArrayList types = new ArrayList();
332 for (int i = 0; i < typ.length; i++)
338 typ = new String[types.size()];
344 ArrayList grps = new ArrayList();
346 for (int i = 0; i < gps.length; i++)
353 gps = new String[grps.size()];
356 AlignmentI al = viewport.getAlignment();
359 SequenceGroup sg = viewport.getSelectionGroup();
362 start = sg.getStartRes();
363 stop = sg.getEndRes();
368 stop = al.getWidth();
370 SequenceI[] oldOrder = al.getSequencesArray();
371 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
372 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
374 alignPanel.paintAlignment(true);
379 public void sortAlignmentByFeatureScore(String[] typ)
381 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
385 public boolean parseFeaturesFile(String file, String protocol,
386 boolean relaxedIdMatching)
388 boolean featuresFile = false;
391 featuresFile = new FeaturesFile(file, protocol).parse(viewport
392 .getAlignment().getDataset(), alignPanel.getFeatureRenderer()
393 .getFeatureColours(), false, relaxedIdMatching);
394 } catch (Exception ex)
396 ex.printStackTrace();
401 avcg.refreshFeatureUI(true);
402 if (alignPanel.getFeatureRenderer() != null)
404 // update the min/max ranges where necessary
405 alignPanel.getFeatureRenderer().findAllFeatures(true);
407 if (avcg.getFeatureSettingsUI() != null)
409 avcg.getFeatureSettingsUI().discoverAllFeatureData();
411 alignPanel.paintAlignment(true);