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.ArrayList;
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;
56 public AlignViewController(AlignViewControllerGuiI alignFrame,
57 AlignViewportI vp, AlignmentViewPanel ap)
59 this.avcg = alignFrame;
65 public void setViewportAndAlignmentPanel(AlignViewportI vp,
66 AlignmentViewPanel ap)
73 public boolean makeGroupsFromSelection()
75 SequenceGroup sg = viewport.getSelectionGroup();
76 ColumnSelection cs = viewport.getColumnSelection();
77 SequenceGroup[] gps = null;
78 if (sg != null && (cs == null || cs.isEmpty()))
80 gps = jalview.analysis.Grouping.makeGroupsFrom(
81 viewport.getSequenceSelection(),
82 viewport.getAlignmentView(true)
83 .getSequenceStrings(viewport.getGapCharacter()),
84 viewport.getAlignment().getGroups());
90 gps = jalview.analysis.Grouping.makeGroupsFromCols(
91 (sg == null) ? viewport.getAlignment().getSequencesArray()
92 : sg.getSequences().toArray(new SequenceI[0]),
93 cs, viewport.getAlignment().getGroups());
98 viewport.getAlignment().deleteAllGroups();
99 viewport.clearSequenceColours();
100 viewport.setSelectionGroup(null);
101 // set view properties for each group
102 for (int g = 0; g < gps.length; g++)
104 // gps[g].setShowunconserved(viewport.getShowUnconserved());
105 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
106 viewport.getAlignment().addGroup(gps[g]);
107 Color col = new Color((int) (Math.random() * 255),
108 (int) (Math.random() * 255), (int) (Math.random() * 255));
109 col = col.brighter();
110 for (SequenceI sq : gps[g].getSequences(null))
112 viewport.setSequenceColour(sq, col);
121 public boolean createGroup()
124 SequenceGroup sg = viewport.getSelectionGroup();
127 viewport.getAlignment().addGroup(sg);
134 public boolean unGroup()
136 SequenceGroup sg = viewport.getSelectionGroup();
139 viewport.getAlignment().deleteGroup(sg);
146 public boolean deleteGroups()
148 if (viewport.getAlignment().getGroups() != null
149 && viewport.getAlignment().getGroups().size() > 0)
151 viewport.getAlignment().deleteAllGroups();
152 viewport.clearSequenceColours();
153 viewport.setSelectionGroup(null);
160 public boolean markColumnsContainingFeatures(boolean invert,
161 boolean extendCurrent, boolean toggle, String featureType)
163 // JBPNote this routine could also mark rows, not just columns.
164 // need a decent query structure to allow all types of feature searches
165 BitSet bs = new BitSet();
166 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
167 || extendCurrent) ? viewport.getAlignment()
168 : viewport.getSelectionGroup();
170 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
172 ColumnSelection cs = viewport.getColumnSelection();
175 cs = new ColumnSelection();
178 if (bs.cardinality() > 0 || invert)
180 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
181 sqcol.getEndRes(), invert, extendCurrent, toggle);
184 viewport.setColumnSelection(cs);
185 alignPanel.paintAlignment(false, false);
186 int columnCount = invert
187 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
190 avcg.setStatus(MessageManager.formatMessage(
191 "label.view_controller_toggled_marked", new String[]
192 { toggle ? MessageManager.getString("label.toggled")
193 : MessageManager.getString("label.marked"),
194 String.valueOf(columnCount),
195 invert ? MessageManager
196 .getString("label.not_containing")
197 : MessageManager.getString("label.containing"),
198 featureType, Integer.valueOf(nseq).toString() }));
204 avcg.setStatus(MessageManager
205 .formatMessage("label.no_feature_of_type_found", new String[]
210 alignPanel.paintAlignment(false, false);
217 * Sets a bit in the BitSet for each column (base 0) in the sequence
218 * collection which includes a visible feature of the specified feature type.
219 * Returns the number of sequences which have the feature visible in the
227 int findColumnsWithFeature(String featureType,
228 SequenceCollectionI sqcol, BitSet bs)
230 FeatureRenderer fr = alignPanel == null ? null : alignPanel
231 .getFeatureRenderer();
233 final int startColumn = sqcol.getStartRes() + 1; // converted to base 1
234 final int endColumn = sqcol.getEndRes() + 1;
235 List<SequenceI> seqs = sqcol.getSequences();
237 for (SequenceI sq : seqs)
241 // int ist = sq.findPosition(sqcol.getStartRes());
242 List<SequenceFeature> sfs = sq.findFeatures(startColumn,
243 endColumn, featureType);
245 boolean found = false;
246 for (SequenceFeature sf : sfs)
248 if (fr.getColour(sf) == null)
258 int sfStartCol = sq.findIndex(sf.getBegin());
259 int sfEndCol = sq.findIndex(sf.getEnd());
261 if (sf.isContactFeature())
264 * 'contact' feature - check for 'start' or 'end'
265 * position within the selected region
267 if (sfStartCol >= startColumn && sfStartCol <= endColumn)
269 bs.set(sfStartCol - 1);
271 if (sfEndCol >= startColumn && sfEndCol <= endColumn)
273 bs.set(sfEndCol - 1);
279 * contiguous feature - select feature positions (if any)
280 * within the selected region
282 if (sfStartCol < startColumn)
284 sfStartCol = startColumn;
286 // not sure what the point of this is
287 // if (sfStartCol < ist)
291 if (sfEndCol > endColumn)
293 sfEndCol = endColumn;
295 for (; sfStartCol <= sfEndCol; sfStartCol++)
297 bs.set(sfStartCol - 1); // convert to base 0
306 public void sortAlignmentByFeatureDensity(List<String> typ)
308 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
311 protected void sortBy(List<String> typ, String methodText,
314 FeatureRenderer fr = alignPanel.getFeatureRenderer();
315 if (typ == null && fr != null)
317 typ = fr.getDisplayedFeatureTypes();
319 List<String> gps = null;
322 gps = fr.getDisplayedFeatureGroups();
324 AlignmentI al = viewport.getAlignment();
327 SequenceGroup sg = viewport.getSelectionGroup();
330 start = sg.getStartRes();
331 stop = sg.getEndRes();
336 stop = al.getWidth();
338 SequenceI[] oldOrder = al.getSequencesArray();
339 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
340 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder,
341 viewport.getAlignment()));
342 alignPanel.paintAlignment(true, false);
347 public void sortAlignmentByFeatureScore(List<String> typ)
349 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
353 public boolean parseFeaturesFile(String file, DataSourceType protocol,
354 boolean relaxedIdMatching)
356 boolean featuresAdded = false;
357 FeatureRenderer fr = alignPanel.getFeatureRenderer();
360 featuresAdded = new FeaturesFile(false, file, protocol).parse(
361 viewport.getAlignment().getDataset(), fr.getFeatureColours(),
362 fr.getFeatureFilters(), false, relaxedIdMatching);
363 } catch (Exception ex)
365 ex.printStackTrace();
370 avcg.refreshFeatureUI(true);
373 // update the min/max ranges where necessary
374 fr.findAllFeatures(true);
376 if (avcg.getFeatureSettingsUI() != null)
378 avcg.getFeatureSettingsUI().discoverAllFeatureData();
380 alignPanel.paintAlignment(true, true);
383 return featuresAdded;
389 * Add highlighted sequences to selected rows. Exclude highlighted sequences
390 * from selected rows. toggle inclusion or exclusion of highlighted sequences.
391 * or add/exclude/toggle for sequences not highlighted.
394 * - when true, sequences that are not highlighted are used to modify
396 * @param extendCurrent
397 * - normally true , the current selected group is modified.
402 public boolean selectHighlightedSequences(boolean invert,
403 boolean extendCurrent, boolean toggle)
405 List<SequenceI> results = alignPanel.getAlignViewport()
406 .getHighlightedSeqs();
408 SequenceGroup sq = (extendCurrent
409 && viewport.getSelectionGroup() != null)
410 ? viewport.getSelectionGroup()
411 : new SequenceGroup();
415 List<SequenceI> nothighlighted = new ArrayList();
416 for (SequenceI seq : alignPanel.getAlignViewport().getAlignment()
419 if (!results.contains(seq))
421 nothighlighted.add(seq);
424 results = nothighlighted;
427 if (results == null || results.size() == 0)
429 // do nothing if no selection exists
434 boolean changed = false;
436 for (SequenceI seq : results)
438 int size = sq.getSize();
441 sq.addOrRemove(seq, false);
445 sq.addSequence(seq, false);
447 changed |= size != sq.getSize();
450 if (sq.getSize() == 0)
452 viewport.setSelectionGroup(null);
456 if (sq != viewport.getSelectionGroup())
459 sq.setEndRes(viewport.getRanges().getAbsoluteAlignmentWidth());
461 viewport.setSelectionGroup(sq);
464 alignPanel.paintAlignment(false, false);
470 public boolean markHighlightedColumns(boolean invert,
471 boolean extendCurrent, boolean toggle)
473 if (!viewport.hasSearchResults())
475 // do nothing if no selection exists
478 // JBPNote this routine could also mark rows, not just columns.
479 BitSet bs = new BitSet();
480 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
481 || extendCurrent) ? viewport.getAlignment()
482 : viewport.getSelectionGroup();
484 // this could be a lambda... - the remains of the method is boilerplate,
485 // except for the different messages for reporting selection.
486 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
488 ColumnSelection cs = viewport.getColumnSelection();
491 cs = new ColumnSelection();
494 if (bs.cardinality() > 0 || invert)
496 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
497 sqcol.getEndRes(), invert, extendCurrent, toggle);
500 viewport.setColumnSelection(cs);
501 alignPanel.paintAlignment(false, false);
502 int columnCount = invert
503 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
506 avcg.setStatus(MessageManager.formatMessage(
507 "label.view_controller_toggled_marked", new String[]
508 { toggle ? MessageManager.getString("label.toggled")
509 : MessageManager.getString("label.marked"),
510 String.valueOf(columnCount),
511 invert ? MessageManager
512 .getString("label.not_containing")
513 : MessageManager.getString("label.containing"),
514 "Highlight", Integer.valueOf(nseq).toString() }));
520 avcg.setStatus(MessageManager
521 .formatMessage("No highlighted regions marked"));
525 alignPanel.paintAlignment(false, false);