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 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
170 || extendCurrent) ? viewport.getAlignment()
171 : viewport.getSelectionGroup();
173 int nseq = findColumnsWithFeature(sqcol, bs, featureType);
175 ColumnSelection cs = viewport.getColumnSelection();
178 cs = new ColumnSelection();
181 String featureTypeString = featureType.length == 1 ? featureType[0]
182 : featureType.toString();
184 if (bs.cardinality() > 0 || invert)
186 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
187 sqcol.getEndRes(), invert, extendCurrent, toggle);
190 viewport.setColumnSelection(cs);
191 alignPanel.paintAlignment(false, false);
192 int columnCount = invert
193 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
196 avcg.setStatus(MessageManager.formatMessage(
197 "label.view_controller_toggled_marked", new String[]
198 { toggle ? MessageManager.getString("label.toggled")
199 : MessageManager.getString("label.marked"),
200 String.valueOf(columnCount),
201 invert ? MessageManager
202 .getString("label.not_containing")
203 : MessageManager.getString("label.containing"),
204 featureTypeString, Integer.valueOf(nseq).toString() }));
210 avcg.setStatus(MessageManager
211 .formatMessage("label.no_feature_of_type_found", new String[]
212 { featureTypeString }));
216 alignPanel.paintAlignment(false, false);
223 * Sets a bit in the BitSet for each column (base 0) in the sequence
224 * collection which includes a visible feature of the specified feature
225 * type(s). Returns the number of sequences which have the feature(s) visible
226 * in the selected range.
234 int findColumnsWithFeature(SequenceCollectionI sqcol,
235 BitSet bs, String... featureType)
237 FeatureRenderer fr = alignPanel == null ? null : alignPanel
238 .getFeatureRenderer();
240 final int startColumn = sqcol.getStartRes() + 1; // converted to base 1
241 final int endColumn = sqcol.getEndRes() + 1;
242 List<SequenceI> seqs = sqcol.getSequences();
244 for (SequenceI sq : seqs)
248 // int ist = sq.findPosition(sqcol.getStartRes());
249 List<SequenceFeature> sfs = sq.findFeatures(startColumn,
250 endColumn, featureType);
252 boolean found = false;
253 for (SequenceFeature sf : sfs)
255 if (fr.getColour(sf) == null)
265 int sfStartCol = sq.findIndex(sf.getBegin());
266 int sfEndCol = sq.findIndex(sf.getEnd());
268 if (sf.isContactFeature())
271 * 'contact' feature - check for 'start' or 'end'
272 * position within the selected region
274 if (sfStartCol >= startColumn && sfStartCol <= endColumn)
276 bs.set(sfStartCol - 1);
278 if (sfEndCol >= startColumn && sfEndCol <= endColumn)
280 bs.set(sfEndCol - 1);
286 * contiguous feature - select feature positions (if any)
287 * within the selected region
289 if (sfStartCol < startColumn)
291 sfStartCol = startColumn;
293 // not sure what the point of this is
294 // if (sfStartCol < ist)
298 if (sfEndCol > endColumn)
300 sfEndCol = endColumn;
302 for (; sfStartCol <= sfEndCol; sfStartCol++)
304 bs.set(sfStartCol - 1); // convert to base 0
313 public void sortAlignmentByFeatureDensity(List<String> typ)
315 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
318 protected void sortBy(List<String> typ, String methodText,
321 FeatureRenderer fr = alignPanel.getFeatureRenderer();
322 if (typ == null && fr != null)
324 typ = fr.getDisplayedFeatureTypes();
326 List<String> gps = null;
329 gps = fr.getDisplayedFeatureGroups();
331 AlignmentI al = viewport.getAlignment();
334 SequenceGroup sg = viewport.getSelectionGroup();
337 start = sg.getStartRes();
338 stop = sg.getEndRes();
343 stop = al.getWidth();
345 SequenceI[] oldOrder = al.getSequencesArray();
346 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
347 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder,
348 viewport.getAlignment()));
349 alignPanel.paintAlignment(true, false);
354 public void sortAlignmentByFeatureScore(List<String> typ)
356 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
360 public boolean parseFeaturesFile(String file, DataSourceType protocol,
361 boolean relaxedIdMatching)
363 boolean featuresAdded = false;
364 FeatureRenderer fr = alignPanel.getFeatureRenderer();
367 featuresAdded = new FeaturesFile(false, file, protocol).parse(
368 viewport.getAlignment().getDataset(), fr.getFeatureColours(),
369 fr.getFeatureFilters(), false, relaxedIdMatching);
370 } catch (Exception ex)
372 ex.printStackTrace();
377 avcg.refreshFeatureUI(true);
380 // update the min/max ranges where necessary
381 fr.findAllFeatures(true);
383 if (avcg.getFeatureSettingsUI() != null)
385 avcg.getFeatureSettingsUI().discoverAllFeatureData();
387 alignPanel.paintAlignment(true, true);
390 return featuresAdded;
395 public boolean markHighlightedColumns(boolean invert,
396 boolean extendCurrent, boolean toggle)
398 if (!viewport.hasSearchResults())
400 // do nothing if no selection exists
403 // JBPNote this routine could also mark rows, not just columns.
404 BitSet bs = new BitSet();
405 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
406 || extendCurrent) ? viewport.getAlignment()
407 : viewport.getSelectionGroup();
409 // this could be a lambda... - the remains of the method is boilerplate,
410 // except for the different messages for reporting selection.
411 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
413 ColumnSelection cs = viewport.getColumnSelection();
416 cs = new ColumnSelection();
419 if (bs.cardinality() > 0 || invert)
421 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
422 sqcol.getEndRes(), invert, extendCurrent, toggle);
425 viewport.setColumnSelection(cs);
426 alignPanel.paintAlignment(false, false);
427 int columnCount = invert
428 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
431 avcg.setStatus(MessageManager.formatMessage(
432 "label.view_controller_toggled_marked", new String[]
433 { toggle ? MessageManager.getString("label.toggled")
434 : MessageManager.getString("label.marked"),
435 String.valueOf(columnCount),
436 invert ? MessageManager
437 .getString("label.not_containing")
438 : MessageManager.getString("label.containing"),
439 "Highlight", Integer.valueOf(nseq).toString() }));
445 avcg.setStatus(MessageManager
446 .formatMessage("No highlighted regions marked"));
450 alignPanel.paintAlignment(false, false);