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.DataSourceType;
37 import jalview.io.FeaturesFile;
38 import jalview.util.MessageManager;
40 import java.awt.Color;
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;
86 if (sg != null && (cs == null || cs.isEmpty()))
88 gps = jalview.analysis.Grouping.makeGroupsFrom(viewport
89 .getSequenceSelection(), viewport.getAlignmentView(true)
90 .getSequenceStrings(viewport.getGapCharacter()), viewport
91 .getAlignment().getGroups());
97 gps = jalview.analysis.Grouping.makeGroupsFromCols(
98 (sg == null) ? viewport.getAlignment().getSequencesArray()
99 : sg.getSequences().toArray(new SequenceI[0]), cs,
100 viewport.getAlignment().getGroups());
105 viewport.getAlignment().deleteAllGroups();
106 viewport.clearSequenceColours();
107 viewport.setSelectionGroup(null);
108 // set view properties for each group
109 for (int g = 0; g < gps.length; g++)
111 // gps[g].setShowunconserved(viewport.getShowUnconserved());
112 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
113 viewport.getAlignment().addGroup(gps[g]);
114 Color col = new Color((int) (Math.random() * 255),
115 (int) (Math.random() * 255), (int) (Math.random() * 255));
116 col = col.brighter();
117 for (SequenceI sq : gps[g].getSequences(null))
119 viewport.setSequenceColour(sq, col);
128 public boolean createGroup()
131 SequenceGroup sg = viewport.getSelectionGroup();
134 viewport.getAlignment().addGroup(sg);
141 public boolean unGroup()
143 SequenceGroup sg = viewport.getSelectionGroup();
146 viewport.getAlignment().deleteGroup(sg);
153 public boolean deleteGroups()
155 if (viewport.getAlignment().getGroups() != null
156 && viewport.getAlignment().getGroups().size() > 0)
158 viewport.getAlignment().deleteAllGroups();
159 viewport.clearSequenceColours();
160 viewport.setSelectionGroup(null);
167 public boolean markColumnsContainingFeatures(boolean invert,
168 boolean extendCurrent, boolean toggle, String featureType)
170 // JBPNote this routine could also mark rows, not just columns.
171 // need a decent query structure to allow all types of feature searches
172 BitSet bs = new BitSet();
173 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null || extendCurrent) ? viewport
174 .getAlignment() : viewport.getSelectionGroup();
176 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
178 ColumnSelection cs = viewport.getColumnSelection();
181 cs = new ColumnSelection();
184 if (bs.cardinality() > 0 || invert)
186 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
187 sqcol.getEndRes(), invert, extendCurrent, toggle);
190 viewport.setColumnSelection(cs);
191 alignPanel.paintAlignment(true);
192 int columnCount = invert ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
195 avcg.setStatus(MessageManager.formatMessage(
196 "label.view_controller_toggled_marked",
198 toggle ? MessageManager.getString("label.toggled")
199 : MessageManager.getString("label.marked"),
200 String.valueOf(columnCount),
201 invert ? MessageManager
202 .getString("label.not_containing")
203 : MessageManager.getString("label.containing"),
204 featureType, Integer.valueOf(nseq).toString() }));
210 avcg.setStatus(MessageManager.formatMessage(
211 "label.no_feature_of_type_found",
212 new String[] { featureType }));
216 alignPanel.paintAlignment(true);
223 * Sets a bit in the BitSet for each column (base 0) in the sequence
224 * collection which includes the specified feature type. Returns the number of
225 * sequences which have the feature in the selected range.
232 static int findColumnsWithFeature(String featureType,
233 SequenceCollectionI sqcol, BitSet bs)
235 final int startPosition = sqcol.getStartRes() + 1; // converted to base 1
236 final int endPosition = sqcol.getEndRes() + 1;
237 List<SequenceI> seqs = sqcol.getSequences();
239 for (SequenceI sq : seqs)
241 boolean sequenceHasFeature = false;
244 SequenceFeature[] sfs = sq.getSequenceFeatures();
247 int ist = sq.findIndex(sq.getStart());
248 int iend = sq.findIndex(sq.getEnd());
249 if (iend < startPosition || ist > endPosition)
251 // sequence not in region
254 for (SequenceFeature sf : sfs)
256 // future functionality - featureType == null means mark columns
257 // containing all displayed features
258 if (sf != null && (featureType.equals(sf.getType())))
260 // optimisation - could consider 'spos,apos' like cursor argument
261 // - findIndex wastes time by starting from first character and
264 int sfStartCol = sq.findIndex(sf.getBegin());
265 int sfEndCol = sq.findIndex(sf.getEnd());
267 if (sf.isContactFeature())
270 * 'contact' feature - check for 'start' or 'end'
271 * position within the selected region
273 if (sfStartCol >= startPosition
274 && sfStartCol <= endPosition)
276 bs.set(sfStartCol - 1);
277 sequenceHasFeature = true;
279 if (sfEndCol >= startPosition && sfEndCol <= endPosition)
281 bs.set(sfEndCol - 1);
282 sequenceHasFeature = true;
288 * contiguous feature - select feature positions (if any)
289 * within the selected region
291 if (sfStartCol > endPosition || sfEndCol < startPosition)
293 // feature is outside selected region
296 sequenceHasFeature = true;
297 if (sfStartCol < startPosition)
299 sfStartCol = startPosition;
301 if (sfStartCol < ist)
305 if (sfEndCol > endPosition)
307 sfEndCol = endPosition;
309 for (; sfStartCol <= sfEndCol; sfStartCol++)
311 bs.set(sfStartCol - 1); // convert to base 0
317 if (sequenceHasFeature)
327 public void sortAlignmentByFeatureDensity(List<String> typ)
329 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
332 protected void sortBy(List<String> typ, String methodText,
335 FeatureRenderer fr = alignPanel.getFeatureRenderer();
336 if (typ == null && fr != null)
338 typ = fr.getDisplayedFeatureTypes();
340 List<String> gps = null;
343 gps = fr.getDisplayedFeatureGroups();
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(List<String> typ)
370 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
374 public boolean parseFeaturesFile(String file, DataSourceType protocol,
375 boolean relaxedIdMatching)
377 boolean featuresFile = false;
380 featuresFile = new FeaturesFile(false, file, protocol).parse(viewport
381 .getAlignment().getDataset(), alignPanel.getFeatureRenderer()
382 .getFeatureColours(), false, relaxedIdMatching);
383 } catch (Exception ex)
385 ex.printStackTrace();
390 avcg.refreshFeatureUI(true);
391 if (alignPanel.getFeatureRenderer() != null)
393 // update the min/max ranges where necessary
394 alignPanel.getFeatureRenderer().findAllFeatures(true);
396 if (avcg.getFeatureSettingsUI() != null)
398 avcg.getFeatureSettingsUI().discoverAllFeatureData();
400 alignPanel.paintAlignment(true);
408 public boolean markHighlightedColumns(boolean invert,
409 boolean extendCurrent, boolean toggle)
411 if (!viewport.hasSearchResults())
413 // do nothing if no selection exists
416 // JBPNote this routine could also mark rows, not just columns.
417 BitSet bs = new BitSet();
418 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null || extendCurrent) ? viewport
419 .getAlignment() : viewport.getSelectionGroup();
421 // this could be a lambda... - the remains of the method is boilerplate,
422 // except for the different messages for reporting selection.
423 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
425 ColumnSelection cs = viewport.getColumnSelection();
428 cs = new ColumnSelection();
431 if (bs.cardinality() > 0 || invert)
433 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
434 sqcol.getEndRes(), invert, extendCurrent, toggle);
437 viewport.setColumnSelection(cs);
438 alignPanel.paintAlignment(true);
439 int columnCount = invert ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
442 avcg.setStatus(MessageManager.formatMessage(
443 "label.view_controller_toggled_marked",
445 toggle ? MessageManager.getString("label.toggled")
446 : MessageManager.getString("label.marked"),
447 String.valueOf(columnCount),
448 invert ? MessageManager
449 .getString("label.not_containing")
450 : MessageManager.getString("label.containing"),
451 "Highlight", Integer.valueOf(nseq).toString() }));
457 avcg.setStatus(MessageManager
458 .formatMessage("No highlighted regions marked"));
462 alignPanel.paintAlignment(true);