2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1)
3 * Copyright (C) 2015 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(viewport
90 .getSequenceSelection(), viewport.getAlignmentView(true)
91 .getSequenceStrings(viewport.getGapCharacter()), viewport
92 .getAlignment().getGroups());
98 gps = jalview.analysis.Grouping.makeGroupsFromCols(
99 (sg == null) ? viewport.getAlignment().getSequencesArray()
100 : sg.getSequences().toArray(new SequenceI[0]), cs,
101 viewport.getAlignment().getGroups());
106 viewport.getAlignment().deleteAllGroups();
107 viewport.clearSequenceColours();
108 viewport.setSelectionGroup(null);
109 // set view properties for each group
110 for (int g = 0; g < gps.length; g++)
112 // gps[g].setShowunconserved(viewport.getShowUnconserved());
113 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
114 viewport.getAlignment().addGroup(gps[g]);
115 Color col = new Color((int) (Math.random() * 255),
116 (int) (Math.random() * 255), (int) (Math.random() * 255));
117 col = col.brighter();
118 for (SequenceI sq : gps[g].getSequences(null))
120 viewport.setSequenceColour(sq, col);
129 public boolean createGroup()
132 SequenceGroup sg = viewport.getSelectionGroup();
135 viewport.getAlignment().addGroup(sg);
142 public boolean unGroup()
144 SequenceGroup sg = viewport.getSelectionGroup();
147 viewport.getAlignment().deleteGroup(sg);
154 public boolean deleteGroups()
156 if (viewport.getAlignment().getGroups() != null
157 && viewport.getAlignment().getGroups().size() > 0)
159 viewport.getAlignment().deleteAllGroups();
160 viewport.clearSequenceColours();
161 viewport.setSelectionGroup(null);
168 public boolean markColumnsContainingFeatures(boolean invert,
169 boolean extendCurrent, boolean toggle, String featureType)
171 // JBPNote this routine could also mark rows, not just columns.
172 // need a decent query structure to allow all types of feature searches
173 BitSet bs = new BitSet();
175 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null ? viewport
176 .getAlignment() : viewport.getSelectionGroup());
177 alStart = sqcol.getStartRes();
178 alw = sqcol.getEndRes() + 1;
179 List<SequenceI> seqs = sqcol.getSequences();
181 for (SequenceI sq : seqs)
186 SequenceFeature[] sf = sq.getSequenceFeatures();
189 int ist = sq.findIndex(sq.getStart());
190 int iend = sq.findIndex(sq.getEnd());
191 if (iend < alStart || ist > alw)
193 // sequence not in region
196 for (SequenceFeature sfpos : sf)
198 // future functionalty - featureType == null means mark columns
199 // containing all displayed features
200 if (sfpos != null && (featureType.equals(sfpos.getType())))
203 // optimisation - could consider 'spos,apos' like cursor argument
204 // - findIndex wastes time by starting from first character and
207 int i = sq.findIndex(sfpos.getBegin());
208 int j = sq.findIndex(sfpos.getEnd());
209 if (j < alStart || i > alw)
211 // feature is outside selected region
240 ColumnSelection cs = viewport.getColumnSelection();
241 if (bs.cardinality() > 0 || invert)
245 cs = new ColumnSelection();
256 // invert only in the currently selected sequence region
257 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
260 if (ibs < 0 || i < ibs)
262 if (toggle && cs.contains(i))
264 cs.removeElement(i++);
273 i = bs.nextClearBit(ibs);
274 ibs = bs.nextSetBit(i);
280 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
283 if (toggle && cs.contains(i))
293 viewport.setColumnSelection(cs);
294 alignPanel.paintAlignment(true);
295 avcg.setStatus(MessageManager.formatMessage(
296 "label.view_controller_toggled_marked",
298 (toggle ? MessageManager.getString("label.toggled")
299 : MessageManager.getString("label.marked")),
300 (invert ? (Integer.valueOf((alw - alStart)
301 - bs.cardinality()).toString()) : (Integer
302 .valueOf(bs.cardinality()).toString())),
303 featureType, Integer.valueOf(nseq).toString() }));
308 avcg.setStatus(MessageManager.formatMessage(
309 "label.no_feature_of_type_found",
310 new String[] { featureType }));
311 if (!extendCurrent && cs != null)
314 alignPanel.paintAlignment(true);
321 public void sortAlignmentByFeatureDensity(String[] typ)
323 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
326 protected void sortBy(String[] typ, String methodText, final String method)
328 FeatureRenderer fr = alignPanel.getFeatureRenderer();
331 typ = fr == null ? null : fr.getDisplayedFeatureTypes();
334 gps = fr == null ? null : fr.getDisplayedFeatureGroups();
337 ArrayList types = new ArrayList();
338 for (int i = 0; i < typ.length; i++)
344 typ = new String[types.size()];
350 ArrayList grps = new ArrayList();
352 for (int i = 0; i < gps.length; i++)
359 gps = new String[grps.size()];
362 AlignmentI al = viewport.getAlignment();
365 SequenceGroup sg = viewport.getSelectionGroup();
368 start = sg.getStartRes();
369 stop = sg.getEndRes();
374 stop = al.getWidth();
376 SequenceI[] oldOrder = al.getSequencesArray();
377 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
378 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
380 alignPanel.paintAlignment(true);
385 public void sortAlignmentByFeatureScore(String[] typ)
387 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
391 public boolean parseFeaturesFile(String file, String protocol,
392 boolean relaxedIdMatching)
394 boolean featuresFile = false;
397 featuresFile = new FeaturesFile(file, protocol).parse(viewport
398 .getAlignment().getDataset(), alignPanel.getFeatureRenderer()
399 .getFeatureColours(), false, relaxedIdMatching);
400 } catch (Exception ex)
402 ex.printStackTrace();
407 avcg.refreshFeatureUI(true);
408 if (alignPanel.getFeatureRenderer() != null)
410 // update the min/max ranges where necessary
411 alignPanel.getFeatureRenderer().findAllFeatures(true);
413 if (avcg.getFeatureSettingsUI() != null)
415 avcg.getFeatureSettingsUI().discoverAllFeatureData();
417 alignPanel.paintAlignment(true);