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.schemes.ColourSchemeI;
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;
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 ColourSchemeI colours = viewport.getGlobalColourScheme();
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]);
110 gps[g].setColourScheme(colours.getInstance(viewport, gps[g]));
112 Color col = new Color((int) (Math.random() * 255),
113 (int) (Math.random() * 255), (int) (Math.random() * 255));
114 gps[g].idColour = col;
115 viewport.setUpdateStructures(true);
116 viewport.addSequenceGroup(gps[g]);
124 public boolean createGroup()
127 SequenceGroup sg = viewport.getSelectionGroup();
130 viewport.getAlignment().addGroup(sg);
137 public boolean unGroup()
139 SequenceGroup sg = viewport.getSelectionGroup();
142 viewport.getAlignment().deleteGroup(sg);
149 public boolean deleteGroups()
151 if (viewport.getAlignment().getGroups() != null
152 && viewport.getAlignment().getGroups().size() > 0)
154 viewport.getAlignment().deleteAllGroups();
155 viewport.clearSequenceColours();
156 viewport.setSelectionGroup(null);
163 public boolean markColumnsContainingFeatures(boolean invert,
164 boolean extendCurrent, boolean toggle, String featureType)
166 // JBPNote this routine could also mark rows, not just columns.
167 // need a decent query structure to allow all types of feature searches
168 BitSet bs = new BitSet();
169 boolean searchSelection = viewport.getSelectionGroup() != null
171 SequenceCollectionI sqcol = searchSelection ? viewport
172 .getSelectionGroup() : viewport.getAlignment();
174 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
176 ColumnSelection cs = viewport.getColumnSelection();
179 cs = new ColumnSelection();
182 if (bs.cardinality() > 0 || invert)
184 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
185 sqcol.getEndRes(), invert, extendCurrent, toggle);
188 viewport.setColumnSelection(cs);
189 alignPanel.paintAlignment(false, false);
190 int columnCount = invert
191 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
194 avcg.setStatus(MessageManager.formatMessage(
195 "label.view_controller_toggled_marked", new String[]
196 { toggle ? MessageManager.getString("label.toggled")
197 : MessageManager.getString("label.marked"),
198 String.valueOf(columnCount),
199 invert ? MessageManager
200 .getString("label.not_containing")
201 : MessageManager.getString("label.containing"),
202 featureType, Integer.valueOf(nseq).toString() }));
208 String key = searchSelection ? "label.no_feature_found_selection"
209 : "label.no_feature_of_type_found";
210 avcg.setStatus(MessageManager.formatMessage(key,
211 new String[] { featureType }));
215 alignPanel.paintAlignment(false, false);
222 * Sets a bit in the BitSet for each column (base 0) in the sequence
223 * collection which includes a visible feature of the specified feature type.
224 * Returns the number of sequences which have the feature visible in the
232 int findColumnsWithFeature(String featureType,
233 SequenceCollectionI sqcol, BitSet bs)
235 FeatureRenderer fr = alignPanel == null ? null : alignPanel
236 .getFeatureRenderer();
238 final int startColumn = sqcol.getStartRes() + 1; // converted to base 1
239 final int endColumn = sqcol.getEndRes() + 1;
240 List<SequenceI> seqs = sqcol.getSequences();
242 for (SequenceI sq : seqs)
246 // int ist = sq.findPosition(sqcol.getStartRes());
247 List<SequenceFeature> sfs = sq.findFeatures(startColumn,
248 endColumn, featureType);
250 boolean found = false;
251 for (SequenceFeature sf : sfs)
253 if (fr.getColour(sf) == null)
263 int sfStartCol = sq.findIndex(sf.getBegin());
264 int sfEndCol = sq.findIndex(sf.getEnd());
266 if (sf.isContactFeature())
269 * 'contact' feature - check for 'start' or 'end'
270 * position within the selected region
272 if (sfStartCol >= startColumn && sfStartCol <= endColumn)
274 bs.set(sfStartCol - 1);
276 if (sfEndCol >= startColumn && sfEndCol <= endColumn)
278 bs.set(sfEndCol - 1);
284 * contiguous feature - select feature positions (if any)
285 * within the selected region
287 if (sfStartCol < startColumn)
289 sfStartCol = startColumn;
291 // not sure what the point of this is
292 // if (sfStartCol < ist)
296 if (sfEndCol > endColumn)
298 sfEndCol = endColumn;
300 for (; sfStartCol <= sfEndCol; sfStartCol++)
302 bs.set(sfStartCol - 1); // convert to base 0
311 public void sortAlignmentByFeatureDensity(List<String> typ)
313 String methodText = MessageManager.getString("label.sort_by_density");
314 sortByFeatures(typ, methodText, AlignmentSorter.FEATURE_DENSITY);
318 * Sorts the alignment (or current selection) by either average score or density
319 * of the specified feature types, and adds to the command history. If
320 * {@code types} is null, all visible feature types are used for the sort. If no
321 * feature types apply, does nothing.
325 * - text shown in Undo/Redo command
328 * jalview.analysis.AlignmentSorter.sortByFeatures()
330 protected void sortByFeatures(List<String> types, String methodText,
333 FeatureRenderer fr = alignPanel.getFeatureRenderer();
334 if (types == null && fr != null)
336 types = fr.getDisplayedFeatureTypes();
340 return; // nothing to do
342 List<String> gps = null;
345 gps = fr.getDisplayedFeatureGroups();
347 AlignmentI al = viewport.getAlignment();
350 SequenceGroup sg = viewport.getSelectionGroup();
353 start = sg.getStartRes();
354 stop = sg.getEndRes();
359 stop = al.getWidth();
361 SequenceI[] oldOrder = al.getSequencesArray();
362 AlignmentSorter.sortByFeature(types, gps, start, stop, al, method);
363 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder,
364 viewport.getAlignment()));
365 alignPanel.paintAlignment(true, false);
370 public void sortAlignmentByFeatureScore(List<String> typ)
372 String methodText = MessageManager.getString("label.sort_by_score");
373 sortByFeatures(typ, methodText, AlignmentSorter.FEATURE_SCORE);
377 public boolean parseFeaturesFile(Object file, DataSourceType protocol,
378 boolean relaxedIdMatching)
380 boolean featuresAdded = false;
381 FeatureRenderer fr = alignPanel.getFeatureRenderer();
384 featuresAdded = new FeaturesFile(false, file, protocol).parse(
385 viewport.getAlignment().getDataset(), fr.getFeatureColours(),
386 fr.getFeatureFilters(), false, relaxedIdMatching);
387 } catch (Exception ex)
389 ex.printStackTrace();
394 avcg.refreshFeatureUI(true);
397 // update the min/max ranges where necessary
398 fr.findAllFeatures(true);
400 if (avcg.getFeatureSettingsUI() != null)
402 avcg.getFeatureSettingsUI().discoverAllFeatureData();
404 alignPanel.paintAlignment(true, true);
407 return featuresAdded;
412 public boolean markHighlightedColumns(boolean invert,
413 boolean extendCurrent, boolean toggle)
415 if (!viewport.hasSearchResults())
417 // do nothing if no selection exists
420 // JBPNote this routine could also mark rows, not just columns.
421 BitSet bs = new BitSet();
422 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
423 || extendCurrent) ? viewport.getAlignment()
424 : viewport.getSelectionGroup();
426 // this could be a lambda... - the remains of the method is boilerplate,
427 // except for the different messages for reporting selection.
428 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
430 ColumnSelection cs = viewport.getColumnSelection();
433 cs = new ColumnSelection();
436 if (bs.cardinality() > 0 || invert)
438 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
439 sqcol.getEndRes(), invert, extendCurrent, toggle);
442 viewport.setColumnSelection(cs);
443 alignPanel.paintAlignment(false, false);
444 int columnCount = invert
445 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
448 avcg.setStatus(MessageManager.formatMessage(
449 "label.view_controller_toggled_marked", new String[]
450 { toggle ? MessageManager.getString("label.toggled")
451 : MessageManager.getString("label.marked"),
452 String.valueOf(columnCount),
453 invert ? MessageManager
454 .getString("label.not_containing")
455 : MessageManager.getString("label.containing"),
456 "Highlight", Integer.valueOf(nseq).toString() }));
462 avcg.setStatus(MessageManager.getString("label.no_highlighted_regions_marked"));
466 alignPanel.paintAlignment(false, false);