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.SearchResultsI;
33 import jalview.datamodel.SequenceCollectionI;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.datamodel.SequenceI;
37 import jalview.io.DataSourceType;
38 import jalview.io.FeaturesFile;
39 import jalview.util.MessageManager;
41 import java.awt.Color;
42 import java.util.ArrayList;
43 import java.util.BitSet;
44 import java.util.List;
46 public class AlignViewController implements AlignViewControllerI
48 AlignViewportI viewport = null;
50 AlignmentViewPanel alignPanel = null;
53 * the GUI container that is handling interactions with the user
55 private AlignViewControllerGuiI avcg;
57 public AlignViewController(AlignViewControllerGuiI alignFrame,
58 AlignViewportI vp, AlignmentViewPanel ap)
60 this.avcg = alignFrame;
66 public void setViewportAndAlignmentPanel(AlignViewportI vp,
67 AlignmentViewPanel ap)
74 public boolean makeGroupsFromSelection()
76 SequenceGroup sg = viewport.getSelectionGroup();
77 ColumnSelection cs = viewport.getColumnSelection();
78 SequenceGroup[] gps = null;
79 if (sg != null && (cs == null || cs.isEmpty()))
81 gps = jalview.analysis.Grouping.makeGroupsFrom(
82 viewport.getSequenceSelection(),
83 viewport.getAlignmentView(true)
84 .getSequenceStrings(viewport.getGapCharacter()),
85 viewport.getAlignment().getGroups());
91 gps = jalview.analysis.Grouping.makeGroupsFromCols(
92 (sg == null) ? viewport.getAlignment().getSequencesArray()
93 : sg.getSequences().toArray(new SequenceI[0]),
94 cs, viewport.getAlignment().getGroups());
99 viewport.getAlignment().deleteAllGroups();
100 viewport.clearSequenceColours();
101 viewport.setSelectionGroup(null);
102 // set view properties for each group
103 for (int g = 0; g < gps.length; g++)
105 // gps[g].setShowunconserved(viewport.getShowUnconserved());
106 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
107 viewport.getAlignment().addGroup(gps[g]);
108 Color col = new Color((int) (Math.random() * 255),
109 (int) (Math.random() * 255), (int) (Math.random() * 255));
110 col = col.brighter();
111 for (SequenceI sq : gps[g].getSequences(null))
113 viewport.setSequenceColour(sq, col);
122 public boolean createGroup()
125 SequenceGroup sg = viewport.getSelectionGroup();
128 viewport.getAlignment().addGroup(sg);
135 public boolean unGroup()
137 SequenceGroup sg = viewport.getSelectionGroup();
140 viewport.getAlignment().deleteGroup(sg);
147 public boolean deleteGroups()
149 if (viewport.getAlignment().getGroups() != null
150 && viewport.getAlignment().getGroups().size() > 0)
152 viewport.getAlignment().deleteAllGroups();
153 viewport.clearSequenceColours();
154 viewport.setSelectionGroup(null);
161 public boolean markColumnsContainingFeatures(boolean invert,
162 boolean extendCurrent, boolean toggle, String featureType)
164 // JBPNote this routine could also mark rows, not just columns.
165 // need a decent query structure to allow all types of feature searches
166 BitSet bs = new BitSet();
167 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
168 || extendCurrent) ? viewport.getAlignment()
169 : viewport.getSelectionGroup();
171 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
173 ColumnSelection cs = viewport.getColumnSelection();
176 cs = new ColumnSelection();
179 if (bs.cardinality() > 0 || invert)
181 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
182 sqcol.getEndRes(), invert, extendCurrent, toggle);
185 viewport.setColumnSelection(cs);
186 alignPanel.paintAlignment(false, false);
187 int columnCount = invert
188 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
191 avcg.setStatus(MessageManager.formatMessage(
192 "label.view_controller_toggled_marked", new String[]
193 { toggle ? MessageManager.getString("label.toggled")
194 : MessageManager.getString("label.marked"),
195 String.valueOf(columnCount),
196 invert ? MessageManager
197 .getString("label.not_containing")
198 : MessageManager.getString("label.containing"),
199 featureType, Integer.valueOf(nseq).toString() }));
205 avcg.setStatus(MessageManager
206 .formatMessage("label.no_feature_of_type_found", new String[]
211 alignPanel.paintAlignment(false, false);
218 * Sets a bit in the BitSet for each column (base 0) in the sequence
219 * collection which includes a visible feature of the specified feature type.
220 * Returns the number of sequences which have the feature visible in the
228 int findColumnsWithFeature(String featureType,
229 SequenceCollectionI sqcol, BitSet bs)
231 FeatureRenderer fr = alignPanel == null ? null : alignPanel
232 .getFeatureRenderer();
234 final int startColumn = sqcol.getStartRes() + 1; // converted to base 1
235 final int endColumn = sqcol.getEndRes() + 1;
236 List<SequenceI> seqs = sqcol.getSequences();
238 for (SequenceI sq : seqs)
242 // int ist = sq.findPosition(sqcol.getStartRes());
243 List<SequenceFeature> sfs = sq.findFeatures(startColumn,
244 endColumn, featureType);
246 boolean found = false;
247 for (SequenceFeature sf : sfs)
249 if (fr.getColour(sf) == null)
259 int sfStartCol = sq.findIndex(sf.getBegin());
260 int sfEndCol = sq.findIndex(sf.getEnd());
262 if (sf.isContactFeature())
265 * 'contact' feature - check for 'start' or 'end'
266 * position within the selected region
268 if (sfStartCol >= startColumn && sfStartCol <= endColumn)
270 bs.set(sfStartCol - 1);
272 if (sfEndCol >= startColumn && sfEndCol <= endColumn)
274 bs.set(sfEndCol - 1);
280 * contiguous feature - select feature positions (if any)
281 * within the selected region
283 if (sfStartCol < startColumn)
285 sfStartCol = startColumn;
287 // not sure what the point of this is
288 // if (sfStartCol < ist)
292 if (sfEndCol > endColumn)
294 sfEndCol = endColumn;
296 for (; sfStartCol <= sfEndCol; sfStartCol++)
298 bs.set(sfStartCol - 1); // convert to base 0
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,
342 viewport.getAlignment()));
343 alignPanel.paintAlignment(true, false);
348 public void sortAlignmentByFeatureScore(List<String> typ)
350 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
354 public boolean parseFeaturesFile(String file, DataSourceType protocol,
355 boolean relaxedIdMatching)
357 boolean featuresAdded = false;
358 FeatureRenderer fr = alignPanel.getFeatureRenderer();
361 featuresAdded = new FeaturesFile(false, file, protocol).parse(
362 viewport.getAlignment().getDataset(), fr.getFeatureColours(),
363 fr.getFeatureFilters(), false, relaxedIdMatching);
364 } catch (Exception ex)
366 ex.printStackTrace();
371 avcg.refreshFeatureUI(true);
374 // update the min/max ranges where necessary
375 fr.findAllFeatures(true);
377 if (avcg.getFeatureSettingsUI() != null)
379 avcg.getFeatureSettingsUI().discoverAllFeatureData();
381 alignPanel.paintAlignment(true, true);
384 return featuresAdded;
390 public boolean selectHighlightedSequences(boolean invert,
391 boolean extendCurrent, boolean toggle)
393 List<SequenceI> results = alignPanel.getAlignViewport()
394 .getHighlightedSeqs();
396 SequenceGroup sq = (extendCurrent
397 && viewport.getSelectionGroup() != null)
398 ? viewport.getSelectionGroup()
399 : new SequenceGroup();
401 SearchResultsI searchResults = viewport.getSearchResults();
404 List<SequenceI> nothighlighted = new ArrayList();
405 for (SequenceI seq : alignPanel.getAlignViewport().getAlignment()
408 if (!results.contains(seq) && (searchResults == null
409 || !searchResults.involvesSequence(seq)))
411 nothighlighted.add(seq);
414 results = nothighlighted;
418 // copy list and add in search results
419 results = new ArrayList(results);
420 if (searchResults != null)
422 for (SequenceI seq : alignPanel.getAlignViewport().getAlignment()
425 if (searchResults.involvesSequence(seq))
433 if (results == null || results.size() == 0)
435 // do nothing if no selection exists
440 boolean changed = false;
442 for (SequenceI seq : results)
444 int size = sq.getSize();
447 sq.addOrRemove(seq, false);
451 sq.addSequence(seq, false);
453 changed |= size != sq.getSize();
456 if (sq.getSize() == 0)
458 viewport.setSelectionGroup(null);
462 if (sq != viewport.getSelectionGroup())
465 sq.setEndRes(viewport.getRanges().getAbsoluteAlignmentWidth());
467 viewport.setSelectionGroup(sq);
470 alignPanel.paintAlignment(false, false);
476 public boolean markHighlightedColumns(boolean invert,
477 boolean extendCurrent, boolean toggle)
479 if (!viewport.hasSearchResults())
481 // do nothing if no selection exists
484 // JBPNote this routine could also mark rows, not just columns.
485 BitSet bs = new BitSet();
486 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
487 || extendCurrent) ? viewport.getAlignment()
488 : viewport.getSelectionGroup();
490 // this could be a lambda... - the remains of the method is boilerplate,
491 // except for the different messages for reporting selection.
492 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
494 ColumnSelection cs = viewport.getColumnSelection();
497 cs = new ColumnSelection();
500 if (bs.cardinality() > 0 || invert)
502 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
503 sqcol.getEndRes(), invert, extendCurrent, toggle);
506 viewport.setColumnSelection(cs);
507 alignPanel.paintAlignment(false, false);
508 int columnCount = invert
509 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
512 avcg.setStatus(MessageManager.formatMessage(
513 "label.view_controller_toggled_marked", new String[]
514 { toggle ? MessageManager.getString("label.toggled")
515 : MessageManager.getString("label.marked"),
516 String.valueOf(columnCount),
517 invert ? MessageManager
518 .getString("label.not_containing")
519 : MessageManager.getString("label.containing"),
520 "Highlight", Integer.valueOf(nseq).toString() }));
526 avcg.setStatus(MessageManager
527 .formatMessage("No highlighted regions marked"));
531 alignPanel.paintAlignment(false, false);