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],
111 viewport.getHiddenRepSequences()));
113 Color col = new Color((int) (Math.random() * 255),
114 (int) (Math.random() * 255), (int) (Math.random() * 255));
115 gps[g].idColour = col;
116 viewport.setUpdateStructures(true);
117 viewport.addSequenceGroup(gps[g]);
125 public boolean createGroup()
128 SequenceGroup sg = viewport.getSelectionGroup();
131 viewport.getAlignment().addGroup(sg);
138 public boolean unGroup()
140 SequenceGroup sg = viewport.getSelectionGroup();
143 viewport.getAlignment().deleteGroup(sg);
150 public boolean deleteGroups()
152 if (viewport.getAlignment().getGroups() != null
153 && viewport.getAlignment().getGroups().size() > 0)
155 viewport.getAlignment().deleteAllGroups();
156 viewport.clearSequenceColours();
157 viewport.setSelectionGroup(null);
164 public boolean markColumnsContainingFeatures(boolean invert,
165 boolean extendCurrent, boolean toggle, String featureType)
167 // JBPNote this routine could also mark rows, not just columns.
168 // need a decent query structure to allow all types of feature searches
169 BitSet bs = new BitSet();
170 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
171 || extendCurrent) ? viewport.getAlignment()
172 : viewport.getSelectionGroup();
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 avcg.setStatus(MessageManager
209 .formatMessage("label.no_feature_of_type_found", new String[]
214 alignPanel.paintAlignment(false, false);
221 * Sets a bit in the BitSet for each column (base 0) in the sequence
222 * collection which includes a visible feature of the specified feature type.
223 * Returns the number of sequences which have the feature visible in the
231 int findColumnsWithFeature(String featureType,
232 SequenceCollectionI sqcol, BitSet bs)
234 FeatureRenderer fr = alignPanel == null ? null : alignPanel
235 .getFeatureRenderer();
237 final int startColumn = sqcol.getStartRes() + 1; // converted to base 1
238 final int endColumn = sqcol.getEndRes() + 1;
239 List<SequenceI> seqs = sqcol.getSequences();
241 for (SequenceI sq : seqs)
245 // int ist = sq.findPosition(sqcol.getStartRes());
246 List<SequenceFeature> sfs = sq.findFeatures(startColumn,
247 endColumn, featureType);
249 boolean found = false;
250 for (SequenceFeature sf : sfs)
252 if (fr.getColour(sf) == null)
262 int sfStartCol = sq.findIndex(sf.getBegin());
263 int sfEndCol = sq.findIndex(sf.getEnd());
265 if (sf.isContactFeature())
268 * 'contact' feature - check for 'start' or 'end'
269 * position within the selected region
271 if (sfStartCol >= startColumn && sfStartCol <= endColumn)
273 bs.set(sfStartCol - 1);
275 if (sfEndCol >= startColumn && sfEndCol <= endColumn)
277 bs.set(sfEndCol - 1);
283 * contiguous feature - select feature positions (if any)
284 * within the selected region
286 if (sfStartCol < startColumn)
288 sfStartCol = startColumn;
290 // not sure what the point of this is
291 // if (sfStartCol < ist)
295 if (sfEndCol > endColumn)
297 sfEndCol = endColumn;
299 for (; sfStartCol <= sfEndCol; sfStartCol++)
301 bs.set(sfStartCol - 1); // convert to base 0
310 public void sortAlignmentByFeatureDensity(List<String> typ)
312 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
315 protected void sortBy(List<String> typ, String methodText,
318 FeatureRenderer fr = alignPanel.getFeatureRenderer();
319 if (typ == null && fr != null)
321 typ = fr.getDisplayedFeatureTypes();
323 List<String> gps = null;
326 gps = fr.getDisplayedFeatureGroups();
328 AlignmentI al = viewport.getAlignment();
331 SequenceGroup sg = viewport.getSelectionGroup();
334 start = sg.getStartRes();
335 stop = sg.getEndRes();
340 stop = al.getWidth();
342 SequenceI[] oldOrder = al.getSequencesArray();
343 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
344 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder,
345 viewport.getAlignment()));
346 alignPanel.paintAlignment(true, false);
351 public void sortAlignmentByFeatureScore(List<String> typ)
353 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
357 public boolean parseFeaturesFile(String file, DataSourceType protocol,
358 boolean relaxedIdMatching)
360 boolean featuresAdded = false;
361 FeatureRenderer fr = alignPanel.getFeatureRenderer();
364 featuresAdded = new FeaturesFile(false, file, protocol).parse(
365 viewport.getAlignment().getDataset(), fr.getFeatureColours(),
366 fr.getFeatureFilters(), false, relaxedIdMatching);
367 } catch (Exception ex)
369 ex.printStackTrace();
374 avcg.refreshFeatureUI(true);
377 // update the min/max ranges where necessary
378 fr.findAllFeatures(true);
380 if (avcg.getFeatureSettingsUI() != null)
382 avcg.getFeatureSettingsUI().discoverAllFeatureData();
384 alignPanel.paintAlignment(true, true);
387 return featuresAdded;
392 public boolean markHighlightedColumns(boolean invert,
393 boolean extendCurrent, boolean toggle)
395 if (!viewport.hasSearchResults())
397 // do nothing if no selection exists
400 // JBPNote this routine could also mark rows, not just columns.
401 BitSet bs = new BitSet();
402 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null
403 || extendCurrent) ? viewport.getAlignment()
404 : viewport.getSelectionGroup();
406 // this could be a lambda... - the remains of the method is boilerplate,
407 // except for the different messages for reporting selection.
408 int nseq = viewport.getSearchResults().markColumns(sqcol, bs);
410 ColumnSelection cs = viewport.getColumnSelection();
413 cs = new ColumnSelection();
416 if (bs.cardinality() > 0 || invert)
418 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
419 sqcol.getEndRes(), invert, extendCurrent, toggle);
422 viewport.setColumnSelection(cs);
423 alignPanel.paintAlignment(false, false);
424 int columnCount = invert
425 ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
428 avcg.setStatus(MessageManager.formatMessage(
429 "label.view_controller_toggled_marked", new String[]
430 { toggle ? MessageManager.getString("label.toggled")
431 : MessageManager.getString("label.marked"),
432 String.valueOf(columnCount),
433 invert ? MessageManager
434 .getString("label.not_containing")
435 : MessageManager.getString("label.containing"),
436 "Highlight", Integer.valueOf(nseq).toString() }));
442 avcg.setStatus(MessageManager
443 .formatMessage("No highlighted regions marked"));
447 alignPanel.paintAlignment(false, false);