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.Arrays;
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;
57 protected void finalize() throws Throwable
64 public AlignViewController(AlignViewControllerGuiI alignFrame,
65 AlignViewportI viewport, AlignmentViewPanel alignPanel)
67 this.avcg = alignFrame;
68 this.viewport = viewport;
69 this.alignPanel = alignPanel;
73 public void setViewportAndAlignmentPanel(AlignViewportI viewport,
74 AlignmentViewPanel alignPanel)
76 this.alignPanel = alignPanel;
77 this.viewport = viewport;
82 public boolean makeGroupsFromSelection()
84 SequenceGroup sg = viewport.getSelectionGroup();
85 ColumnSelection cs = viewport.getColumnSelection();
86 SequenceGroup[] gps = null;
87 if (sg != null && (cs == null || cs.isEmpty()))
89 gps = jalview.analysis.Grouping.makeGroupsFrom(viewport
90 .getSequenceSelection(), viewport.getAlignmentView(true)
91 .getSequenceStrings(viewport.getGapCharacter()), viewport
92 .getAlignment().getGroups());
98 gps = jalview.analysis.Grouping.makeGroupsFromCols(
99 (sg == null) ? viewport.getAlignment().getSequencesArray()
100 : sg.getSequences().toArray(new SequenceI[0]), cs,
101 viewport.getAlignment().getGroups());
106 showRandomColoursForGroups(Arrays.asList(gps));
114 public void showRandomColoursForGroups(List<SequenceGroup> gps)
116 viewport.getAlignment().deleteAllGroups();
117 viewport.clearSequenceColours();
118 viewport.setSelectionGroup(null);
119 // set view properties for each group
120 for (SequenceGroup sg : gps)
122 // gps[g].setShowunconserved(viewport.getShowUnconserved());
123 sg.setshowSequenceLogo(viewport.isShowSequenceLogo());
124 viewport.getAlignment().addGroup(sg);
125 Color col = new Color((int) (Math.random() * 255),
126 (int) (Math.random() * 255), (int) (Math.random() * 255));
127 col = col.brighter();
128 for (SequenceI sq : sg.getSequences(null))
130 viewport.setSequenceColour(sq, col);
136 public boolean createGroup()
139 SequenceGroup sg = viewport.getSelectionGroup();
142 viewport.getAlignment().addGroup(sg);
149 public boolean unGroup()
151 SequenceGroup sg = viewport.getSelectionGroup();
154 viewport.getAlignment().deleteGroup(sg);
161 public boolean deleteGroups()
163 if (viewport.getAlignment().getGroups() != null
164 && viewport.getAlignment().getGroups().size() > 0)
166 viewport.getAlignment().deleteAllGroups();
167 viewport.clearSequenceColours();
168 viewport.setSelectionGroup(null);
175 public boolean markColumnsContainingFeatures(boolean invert,
176 boolean extendCurrent, boolean toggle, String featureType)
178 // JBPNote this routine could also mark rows, not just columns.
179 // need a decent query structure to allow all types of feature searches
180 BitSet bs = new BitSet();
181 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null || extendCurrent) ? viewport
182 .getAlignment() : viewport.getSelectionGroup();
184 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
186 ColumnSelection cs = viewport.getColumnSelection();
189 cs = new ColumnSelection();
192 if (bs.cardinality() > 0 || invert)
194 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
195 sqcol.getEndRes(), invert, extendCurrent, toggle);
198 viewport.setColumnSelection(cs);
199 alignPanel.paintAlignment(true);
200 int columnCount = invert ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
203 avcg.setStatus(MessageManager.formatMessage(
204 "label.view_controller_toggled_marked",
206 toggle ? MessageManager.getString("label.toggled")
207 : MessageManager.getString("label.marked"),
208 String.valueOf(columnCount),
209 invert ? MessageManager
210 .getString("label.not_containing")
211 : MessageManager.getString("label.containing"),
212 featureType, Integer.valueOf(nseq).toString() }));
218 avcg.setStatus(MessageManager.formatMessage(
219 "label.no_feature_of_type_found",
220 new String[] { featureType }));
224 alignPanel.paintAlignment(true);
231 * Sets a bit in the BitSet for each column (base 0) in the sequence
232 * collection which includes the specified feature type. Returns the number of
233 * sequences which have the feature in the selected range.
240 static int findColumnsWithFeature(String featureType,
241 SequenceCollectionI sqcol, BitSet bs)
243 final int startPosition = sqcol.getStartRes() + 1; // converted to base 1
244 final int endPosition = sqcol.getEndRes() + 1;
245 List<SequenceI> seqs = sqcol.getSequences();
247 for (SequenceI sq : seqs)
249 boolean sequenceHasFeature = false;
252 SequenceFeature[] sfs = sq.getSequenceFeatures();
255 int ist = sq.findIndex(sq.getStart());
256 int iend = sq.findIndex(sq.getEnd());
257 if (iend < startPosition || ist > endPosition)
259 // sequence not in region
262 for (SequenceFeature sf : sfs)
264 // future functionality - featureType == null means mark columns
265 // containing all displayed features
266 if (sf != null && (featureType.equals(sf.getType())))
268 // optimisation - could consider 'spos,apos' like cursor argument
269 // - findIndex wastes time by starting from first character and
272 int sfStartCol = sq.findIndex(sf.getBegin());
273 int sfEndCol = sq.findIndex(sf.getEnd());
275 if (sf.isContactFeature())
278 * 'contact' feature - check for 'start' or 'end'
279 * position within the selected region
281 if (sfStartCol >= startPosition
282 && sfStartCol <= endPosition)
284 bs.set(sfStartCol - 1);
285 sequenceHasFeature = true;
287 if (sfEndCol >= startPosition && sfEndCol <= endPosition)
289 bs.set(sfEndCol - 1);
290 sequenceHasFeature = true;
296 * contiguous feature - select feature positions (if any)
297 * within the selected region
299 if (sfStartCol > endPosition || sfEndCol < startPosition)
301 // feature is outside selected region
304 sequenceHasFeature = true;
305 if (sfStartCol < startPosition)
307 sfStartCol = startPosition;
309 if (sfStartCol < ist)
313 if (sfEndCol > endPosition)
315 sfEndCol = endPosition;
317 for (; sfStartCol <= sfEndCol; sfStartCol++)
319 bs.set(sfStartCol - 1); // convert to base 0
325 if (sequenceHasFeature)
335 public void sortAlignmentByFeatureDensity(List<String> typ)
337 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
340 protected void sortBy(List<String> typ, String methodText,
343 FeatureRenderer fr = alignPanel.getFeatureRenderer();
344 if (typ == null && fr != null)
346 typ = fr.getDisplayedFeatureTypes();
348 List<String> gps = null;
351 gps = fr.getDisplayedFeatureGroups();
353 AlignmentI al = viewport.getAlignment();
356 SequenceGroup sg = viewport.getSelectionGroup();
359 start = sg.getStartRes();
360 stop = sg.getEndRes();
365 stop = al.getWidth();
367 SequenceI[] oldOrder = al.getSequencesArray();
368 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
369 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
371 alignPanel.paintAlignment(true);
376 public void sortAlignmentByFeatureScore(List<String> typ)
378 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
382 public boolean parseFeaturesFile(String file, DataSourceType protocol,
383 boolean relaxedIdMatching)
385 boolean featuresFile = false;
388 featuresFile = new FeaturesFile(false, file, protocol).parse(viewport
389 .getAlignment().getDataset(), alignPanel.getFeatureRenderer()
390 .getFeatureColours(), false, relaxedIdMatching);
391 } catch (Exception ex)
393 ex.printStackTrace();
398 avcg.refreshFeatureUI(true);
399 if (alignPanel.getFeatureRenderer() != null)
401 // update the min/max ranges where necessary
402 alignPanel.getFeatureRenderer().findAllFeatures(true);
404 if (avcg.getFeatureSettingsUI() != null)
406 avcg.getFeatureSettingsUI().discoverAllFeatureData();
408 alignPanel.paintAlignment(true);
416 public boolean markHighlightedColumns(boolean invert,
417 boolean extendCurrent, boolean toggle)
419 if (!viewport.hasSearchResults())
421 // do nothing if no selection exists
424 // JBPNote this routine could also mark rows, not just columns.
425 BitSet bs = new BitSet();
426 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null || extendCurrent) ? viewport
427 .getAlignment() : viewport.getSelectionGroup();
429 // this could be a lambda... - the remains of the method is boilerplate,
430 // except for the different messages for reporting selection.
431 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
433 ColumnSelection cs = viewport.getColumnSelection();
436 cs = new ColumnSelection();
439 if (bs.cardinality() > 0 || invert)
441 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
442 sqcol.getEndRes(), invert, extendCurrent, toggle);
445 viewport.setColumnSelection(cs);
446 alignPanel.paintAlignment(true);
447 int columnCount = invert ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
450 avcg.setStatus(MessageManager.formatMessage(
451 "label.view_controller_toggled_marked",
453 toggle ? MessageManager.getString("label.toggled")
454 : MessageManager.getString("label.marked"),
455 String.valueOf(columnCount),
456 invert ? MessageManager
457 .getString("label.not_containing")
458 : MessageManager.getString("label.containing"),
459 "Highlight", Integer.valueOf(nseq).toString() }));
465 avcg.setStatus(MessageManager
466 .formatMessage("No highlighted regions marked"));
470 alignPanel.paintAlignment(true);