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.BitSet;
42 import java.util.List;
44 public class AlignViewController implements AlignViewControllerI
46 AlignViewportI viewport = null;
48 AlignmentViewPanel alignPanel = null;
51 * the GUI container that is handling interactions with the user
53 private AlignViewControllerGuiI avcg;
55 public AlignViewController(AlignViewControllerGuiI alignFrame,
56 AlignViewportI vp, AlignmentViewPanel ap)
58 this.avcg = alignFrame;
64 public void setViewportAndAlignmentPanel(AlignViewportI vp,
65 AlignmentViewPanel ap)
72 public boolean makeGroupsFromSelection()
74 SequenceGroup sg = viewport.getSelectionGroup();
75 ColumnSelection cs = viewport.getColumnSelection();
76 SequenceGroup[] gps = null;
77 if (sg != null && (cs == null || cs.isEmpty()))
79 gps = jalview.analysis.Grouping.makeGroupsFrom(
80 viewport.getSequenceSelection(),
81 viewport.getAlignmentView(true)
82 .getSequenceStrings(viewport.getGapCharacter()),
83 viewport.getAlignment().getGroups());
89 gps = jalview.analysis.Grouping.makeGroupsFromCols(
90 (sg == null) ? viewport.getAlignment().getSequencesArray()
91 : sg.getSequences().toArray(new SequenceI[0]),
92 cs, viewport.getAlignment().getGroups());
97 viewport.getAlignment().deleteAllGroups();
98 viewport.clearSequenceColours();
99 viewport.setSelectionGroup(null);
100 // set view properties for each group
101 for (int g = 0; g < gps.length; g++)
103 // gps[g].setShowunconserved(viewport.getShowUnconserved());
104 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
105 viewport.getAlignment().addGroup(gps[g]);
106 Color col = new Color((int) (Math.random() * 255),
107 (int) (Math.random() * 255), (int) (Math.random() * 255));
108 col = col.brighter();
109 for (SequenceI sq : gps[g].getSequences(null))
111 viewport.setSequenceColour(sq, col);
120 public boolean createGroup()
123 SequenceGroup sg = viewport.getSelectionGroup();
126 viewport.getAlignment().addGroup(sg);
133 public boolean unGroup()
135 SequenceGroup sg = viewport.getSelectionGroup();
138 viewport.getAlignment().deleteGroup(sg);
145 public boolean deleteGroups()
147 if (viewport.getAlignment().getGroups() != null
148 && viewport.getAlignment().getGroups().size() > 0)
150 viewport.getAlignment().deleteAllGroups();
151 viewport.clearSequenceColours();
152 viewport.setSelectionGroup(null);
159 public boolean markColumnsContainingFeatures(boolean invert,
160 boolean extendCurrent, boolean toggle, String featureType)
162 // JBPNote this routine could also mark rows, not just columns.
163 // need a decent query structure to allow all types of feature searches
164 BitSet bs = new BitSet();
165 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
166 || extendCurrent) ? viewport.getAlignment()
167 : viewport.getSelectionGroup();
169 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
171 ColumnSelection cs = viewport.getColumnSelection();
174 cs = new ColumnSelection();
177 if (bs.cardinality() > 0 || invert)
179 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
180 sqcol.getEndRes(), invert, extendCurrent, toggle);
183 viewport.setColumnSelection(cs);
184 alignPanel.paintAlignment(false, false);
185 int columnCount = invert
186 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
189 avcg.setStatus(MessageManager.formatMessage(
190 "label.view_controller_toggled_marked", new String[]
191 { toggle ? MessageManager.getString("label.toggled")
192 : MessageManager.getString("label.marked"),
193 String.valueOf(columnCount),
194 invert ? MessageManager
195 .getString("label.not_containing")
196 : MessageManager.getString("label.containing"),
197 featureType, Integer.valueOf(nseq).toString() }));
203 avcg.setStatus(MessageManager
204 .formatMessage("label.no_feature_of_type_found", new String[]
209 alignPanel.paintAlignment(false, false);
216 * Sets a bit in the BitSet for each column (base 0) in the sequence
217 * collection which includes a visible feature of the specified feature type.
218 * Returns the number of sequences which have the feature visible in the
226 int findColumnsWithFeature(String featureType,
227 SequenceCollectionI sqcol, BitSet bs)
229 FeatureRenderer fr = alignPanel == null ? null : alignPanel
230 .getFeatureRenderer();
232 final int startColumn = sqcol.getStartRes() + 1; // converted to base 1
233 final int endColumn = sqcol.getEndRes() + 1;
234 List<SequenceI> seqs = sqcol.getSequences();
236 for (SequenceI sq : seqs)
240 // int ist = sq.findPosition(sqcol.getStartRes());
241 List<SequenceFeature> sfs = sq.findFeatures(startColumn,
242 endColumn, featureType);
244 boolean found = false;
245 for (SequenceFeature sf : sfs)
247 if (fr.getColour(sf) == null)
257 int sfStartCol = sq.findIndex(sf.getBegin());
258 int sfEndCol = sq.findIndex(sf.getEnd());
260 if (sf.isContactFeature())
263 * 'contact' feature - check for 'start' or 'end'
264 * position within the selected region
266 if (sfStartCol >= startColumn && sfStartCol <= endColumn)
268 bs.set(sfStartCol - 1);
270 if (sfEndCol >= startColumn && sfEndCol <= endColumn)
272 bs.set(sfEndCol - 1);
278 * contiguous feature - select feature positions (if any)
279 * within the selected region
281 if (sfStartCol < startColumn)
283 sfStartCol = startColumn;
285 // not sure what the point of this is
286 // if (sfStartCol < ist)
290 if (sfEndCol > endColumn)
292 sfEndCol = endColumn;
294 for (; sfStartCol <= sfEndCol; sfStartCol++)
296 bs.set(sfStartCol - 1); // convert to base 0
305 public void sortAlignmentByFeatureDensity(List<String> typ)
307 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
310 protected void sortBy(List<String> typ, String methodText,
313 FeatureRenderer fr = alignPanel.getFeatureRenderer();
314 if (typ == null && fr != null)
316 typ = fr.getDisplayedFeatureTypes();
318 List<String> gps = null;
321 gps = fr.getDisplayedFeatureGroups();
323 AlignmentI al = viewport.getAlignment();
326 SequenceGroup sg = viewport.getSelectionGroup();
329 start = sg.getStartRes();
330 stop = sg.getEndRes();
335 stop = al.getWidth();
337 SequenceI[] oldOrder = al.getSequencesArray();
338 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
339 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder,
340 viewport.getAlignment()));
341 alignPanel.paintAlignment(true, false);
346 public void sortAlignmentByFeatureScore(List<String> typ)
348 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
352 public boolean parseFeaturesFile(String file, DataSourceType protocol,
353 boolean relaxedIdMatching)
355 boolean featuresAdded = false;
356 FeatureRenderer fr = alignPanel.getFeatureRenderer();
359 featuresAdded = new FeaturesFile(false, file, protocol).parse(
360 viewport.getAlignment().getDataset(), fr.getFeatureColours(),
361 fr.getFeatureFilters(), false, relaxedIdMatching);
362 } catch (Exception ex)
364 ex.printStackTrace();
369 avcg.refreshFeatureUI(true);
372 // update the min/max ranges where necessary
373 fr.findAllFeatures(true);
375 if (avcg.getFeatureSettingsUI() != null)
377 avcg.getFeatureSettingsUI().discoverAllFeatureData();
379 alignPanel.paintAlignment(true, true);
382 return featuresAdded;
387 public boolean markHighlightedColumns(boolean invert,
388 boolean extendCurrent, boolean toggle)
390 if (!viewport.hasSearchResults())
392 // do nothing if no selection exists
395 // JBPNote this routine could also mark rows, not just columns.
396 BitSet bs = new BitSet();
397 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
398 || extendCurrent) ? viewport.getAlignment()
399 : viewport.getSelectionGroup();
401 // this could be a lambda... - the remains of the method is boilerplate,
402 // except for the different messages for reporting selection.
403 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
405 ColumnSelection cs = viewport.getColumnSelection();
408 cs = new ColumnSelection();
411 if (bs.cardinality() > 0 || invert)
413 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
414 sqcol.getEndRes(), invert, extendCurrent, toggle);
417 viewport.setColumnSelection(cs);
418 alignPanel.paintAlignment(false, false);
419 int columnCount = invert
420 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
423 avcg.setStatus(MessageManager.formatMessage(
424 "label.view_controller_toggled_marked", new String[]
425 { toggle ? MessageManager.getString("label.toggled")
426 : MessageManager.getString("label.marked"),
427 String.valueOf(columnCount),
428 invert ? MessageManager
429 .getString("label.not_containing")
430 : MessageManager.getString("label.containing"),
431 "Highlight", Integer.valueOf(nseq).toString() }));
437 avcg.setStatus(MessageManager
438 .formatMessage("No highlighted regions marked"));
442 alignPanel.paintAlignment(false, false);