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 * Add highlighted sequences to selected rows. Exclude highlighted sequences
391 * from selected rows. toggle inclusion or exclusion of highlighted sequences.
392 * or add/exclude/toggle for sequences not highlighted.
395 * - when true, sequences that are not highlighted are added/removed
397 * @param extendCurrent
398 * - normally true , the current selected group is modified.
399 * Otherwise a new selection is created
401 * - toggle selection status for highlighted/not highlighted sequence
404 public boolean selectHighlightedSequences(boolean invert,
405 boolean extendCurrent, boolean toggle)
407 List<SequenceI> results = alignPanel.getAlignViewport()
408 .getHighlightedSeqs();
410 SequenceGroup sq = (extendCurrent
411 && viewport.getSelectionGroup() != null)
412 ? viewport.getSelectionGroup()
413 : new SequenceGroup();
415 SearchResultsI searchResults = viewport.getSearchResults();
418 List<SequenceI> nothighlighted = new ArrayList();
419 for (SequenceI seq : alignPanel.getAlignViewport().getAlignment()
422 if (!results.contains(seq) && (searchResults == null
423 || !searchResults.involvesSequence(seq)))
425 nothighlighted.add(seq);
428 results = nothighlighted;
432 // copy list and add in search results
433 results = new ArrayList(results);
434 if (searchResults != null)
436 for (SequenceI seq : alignPanel.getAlignViewport().getAlignment()
439 if (searchResults.involvesSequence(seq))
447 if (results == null || results.size() == 0)
449 // do nothing if no selection exists
454 boolean changed = false;
456 for (SequenceI seq : results)
458 int size = sq.getSize();
461 sq.addOrRemove(seq, false);
465 sq.addSequence(seq, false);
467 changed |= size != sq.getSize();
470 if (sq.getSize() == 0)
472 viewport.setSelectionGroup(null);
476 if (sq != viewport.getSelectionGroup())
479 sq.setEndRes(viewport.getRanges().getAbsoluteAlignmentWidth());
481 viewport.setSelectionGroup(sq);
484 alignPanel.paintAlignment(false, false);
490 public boolean markHighlightedColumns(boolean invert,
491 boolean extendCurrent, boolean toggle)
493 if (!viewport.hasSearchResults())
495 // do nothing if no selection exists
498 // JBPNote this routine could also mark rows, not just columns.
499 BitSet bs = new BitSet();
500 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
501 || extendCurrent) ? viewport.getAlignment()
502 : viewport.getSelectionGroup();
504 // this could be a lambda... - the remains of the method is boilerplate,
505 // except for the different messages for reporting selection.
506 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
508 ColumnSelection cs = viewport.getColumnSelection();
511 cs = new ColumnSelection();
514 if (bs.cardinality() > 0 || invert)
516 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
517 sqcol.getEndRes(), invert, extendCurrent, toggle);
520 viewport.setColumnSelection(cs);
521 alignPanel.paintAlignment(false, false);
522 int columnCount = invert
523 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
526 avcg.setStatus(MessageManager.formatMessage(
527 "label.view_controller_toggled_marked", new String[]
528 { toggle ? MessageManager.getString("label.toggled")
529 : MessageManager.getString("label.marked"),
530 String.valueOf(columnCount),
531 invert ? MessageManager
532 .getString("label.not_containing")
533 : MessageManager.getString("label.containing"),
534 "Highlight", Integer.valueOf(nseq).toString() }));
540 avcg.setStatus(MessageManager
541 .formatMessage("No highlighted regions marked"));
545 alignPanel.paintAlignment(false, false);