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.ColorI;
29 import jalview.api.FeatureRenderer;
30 import jalview.commands.OrderCommand;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.ColumnSelection;
33 import jalview.datamodel.SequenceCollectionI;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.datamodel.SequenceI;
37 import jalview.io.FeaturesFile;
38 import jalview.schemes.Colour;
39 import jalview.util.MessageManager;
41 import java.awt.Color;
42 import java.util.BitSet;
43 import java.util.List;
45 public class AlignViewController implements AlignViewControllerI
47 AlignViewportI viewport = null;
49 AlignmentViewPanel alignPanel = null;
52 * the GUI container that is handling interactions with the user
54 private AlignViewControllerGuiI avcg;
57 protected void finalize() throws Throwable
64 public AlignViewController(AlignViewControllerGuiI alignFrame,
65 AlignViewportI viewport, AlignmentViewPanel alignPanel)
67 this.avcg = alignFrame;
68 this.viewport = viewport;
69 this.alignPanel = alignPanel;
73 public void setViewportAndAlignmentPanel(AlignViewportI viewport,
74 AlignmentViewPanel alignPanel)
76 this.alignPanel = alignPanel;
77 this.viewport = viewport;
82 public boolean makeGroupsFromSelection()
84 SequenceGroup sg = viewport.getSelectionGroup();
85 ColumnSelection cs = viewport.getColumnSelection();
86 SequenceGroup[] gps = null;
87 if (sg != null && (cs == null || cs.isEmpty()))
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 ColorI newcol = new Colour(col.brighter());
118 for (SequenceI sq : gps[g].getSequences(null))
120 viewport.setSequenceColour(sq, newcol);
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();
174 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null || extendCurrent) ? viewport
175 .getAlignment() : viewport.getSelectionGroup();
177 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
179 ColumnSelection cs = viewport.getColumnSelection();
182 cs = new ColumnSelection();
185 if (bs.cardinality() > 0 || invert)
187 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
188 sqcol.getEndRes(), invert, extendCurrent, toggle);
191 viewport.setColumnSelection(cs);
192 alignPanel.paintAlignment(true);
193 int columnCount = invert ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
196 avcg.setStatus(MessageManager.formatMessage(
197 "label.view_controller_toggled_marked",
199 toggle ? MessageManager.getString("label.toggled")
200 : MessageManager.getString("label.marked"),
201 String.valueOf(columnCount),
202 invert ? MessageManager
203 .getString("label.not_containing")
204 : MessageManager.getString("label.containing"),
205 featureType, Integer.valueOf(nseq).toString() }));
211 avcg.setStatus(MessageManager.formatMessage(
212 "label.no_feature_of_type_found",
213 new String[] { featureType }));
217 alignPanel.paintAlignment(true);
224 * Sets a bit in the BitSet for each column (base 0) in the sequence
225 * collection which includes the specified feature type. Returns the number of
226 * sequences which have the feature in the selected range.
233 static int findColumnsWithFeature(String featureType,
234 SequenceCollectionI sqcol, BitSet bs)
236 final int startPosition = sqcol.getStartRes() + 1; // converted to base 1
237 final int endPosition = sqcol.getEndRes() + 1;
238 List<SequenceI> seqs = sqcol.getSequences();
240 for (SequenceI sq : seqs)
242 boolean sequenceHasFeature = false;
245 SequenceFeature[] sfs = sq.getSequenceFeatures();
249 * check whether the feature start/end (base 1)
250 * overlaps the selection start/end
252 int ist = sq.findIndex(sq.getStart());
253 int iend = sq.findIndex(sq.getEnd());
254 if (iend < startPosition || ist > endPosition)
256 // sequence not in region
259 for (SequenceFeature sf : sfs)
261 // future functionality - featureType == null means mark columns
262 // containing all displayed features
263 if (sf != null && (featureType.equals(sf.getType())))
265 // optimisation - could consider 'spos,apos' like cursor argument
266 // - findIndex wastes time by starting from first character and
269 int i = sq.findIndex(sf.getBegin());
270 int j = sq.findIndex(sf.getEnd());
271 if (j < startPosition || i > endPosition)
273 // feature is outside selected region
276 sequenceHasFeature = true;
277 if (i < startPosition)
291 bs.set(i - 1); // convert to base 0
297 if (sequenceHasFeature)
307 public void sortAlignmentByFeatureDensity(List<String> typ)
309 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
312 protected void sortBy(List<String> typ, String methodText,
315 FeatureRenderer fr = alignPanel.getFeatureRenderer();
316 if (typ == null && fr != null)
318 typ = fr.getDisplayedFeatureTypes();
320 List<String> gps = null;
323 gps = fr.getDisplayedFeatureGroups();
325 AlignmentI al = viewport.getAlignment();
328 SequenceGroup sg = viewport.getSelectionGroup();
331 start = sg.getStartRes();
332 stop = sg.getEndRes();
337 stop = al.getWidth();
339 SequenceI[] oldOrder = al.getSequencesArray();
340 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
341 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
343 alignPanel.paintAlignment(true);
348 public void sortAlignmentByFeatureScore(List<String> typ)
350 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
354 public boolean parseFeaturesFile(String file, String protocol,
355 boolean relaxedIdMatching)
357 boolean featuresFile = false;
360 featuresFile = new FeaturesFile(false, file, protocol).parse(viewport
361 .getAlignment().getDataset(), alignPanel.getFeatureRenderer()
362 .getFeatureColours(), false, relaxedIdMatching);
363 } catch (Exception ex)
365 ex.printStackTrace();
370 avcg.refreshFeatureUI(true);
371 if (alignPanel.getFeatureRenderer() != null)
373 // update the min/max ranges where necessary
374 alignPanel.getFeatureRenderer().findAllFeatures(true);
376 if (avcg.getFeatureSettingsUI() != null)
378 avcg.getFeatureSettingsUI().discoverAllFeatureData();
380 alignPanel.paintAlignment(true);