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.FeaturesFile;
37 import jalview.util.MessageManager;
39 import java.awt.Color;
40 import java.util.BitSet;
41 import java.util.List;
43 public class AlignViewController implements AlignViewControllerI
45 AlignViewportI viewport = null;
47 AlignmentViewPanel alignPanel = null;
50 * the GUI container that is handling interactions with the user
52 private AlignViewControllerGuiI avcg;
55 protected void finalize() throws Throwable
62 public AlignViewController(AlignViewControllerGuiI alignFrame,
63 AlignViewportI viewport, AlignmentViewPanel alignPanel)
65 this.avcg = alignFrame;
66 this.viewport = viewport;
67 this.alignPanel = alignPanel;
71 public void setViewportAndAlignmentPanel(AlignViewportI viewport,
72 AlignmentViewPanel alignPanel)
74 this.alignPanel = alignPanel;
75 this.viewport = viewport;
80 public boolean makeGroupsFromSelection()
82 SequenceGroup sg = viewport.getSelectionGroup();
83 ColumnSelection cs = viewport.getColumnSelection();
84 SequenceGroup[] gps = null;
85 if (sg != null && (cs == null || cs.isEmpty()))
87 gps = jalview.analysis.Grouping.makeGroupsFrom(viewport
88 .getSequenceSelection(), viewport.getAlignmentView(true)
89 .getSequenceStrings(viewport.getGapCharacter()), viewport
90 .getAlignment().getGroups());
96 gps = jalview.analysis.Grouping.makeGroupsFromCols(
97 (sg == null) ? viewport.getAlignment().getSequencesArray()
98 : sg.getSequences().toArray(new SequenceI[0]), cs,
99 viewport.getAlignment().getGroups());
104 viewport.getAlignment().deleteAllGroups();
105 viewport.clearSequenceColours();
106 viewport.setSelectionGroup(null);
107 // set view properties for each group
108 for (int g = 0; g < gps.length; g++)
110 // gps[g].setShowunconserved(viewport.getShowUnconserved());
111 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
112 viewport.getAlignment().addGroup(gps[g]);
113 Color col = new Color((int) (Math.random() * 255),
114 (int) (Math.random() * 255), (int) (Math.random() * 255));
115 col = col.brighter();
116 for (SequenceI sq : gps[g].getSequences(null))
118 viewport.setSequenceColour(sq, col);
127 public boolean createGroup()
130 SequenceGroup sg = viewport.getSelectionGroup();
133 viewport.getAlignment().addGroup(sg);
140 public boolean unGroup()
142 SequenceGroup sg = viewport.getSelectionGroup();
145 viewport.getAlignment().deleteGroup(sg);
152 public boolean deleteGroups()
154 if (viewport.getAlignment().getGroups() != null
155 && viewport.getAlignment().getGroups().size() > 0)
157 viewport.getAlignment().deleteAllGroups();
158 viewport.clearSequenceColours();
159 viewport.setSelectionGroup(null);
166 public boolean markColumnsContainingFeatures(boolean invert,
167 boolean extendCurrent, boolean toggle, String featureType)
169 // JBPNote this routine could also mark rows, not just columns.
170 // need a decent query structure to allow all types of feature searches
171 BitSet bs = new BitSet();
172 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null || extendCurrent) ? viewport
173 .getAlignment() : viewport.getSelectionGroup();
175 int nseq = findColumnsWithFeature(featureType, sqcol, bs);
177 ColumnSelection cs = viewport.getColumnSelection();
180 cs = new ColumnSelection();
183 if (bs.cardinality() > 0 || invert)
185 boolean changed = cs.markColumns(bs, sqcol.getStartRes(),
186 sqcol.getEndRes(), invert, extendCurrent, toggle);
189 viewport.setColumnSelection(cs);
190 alignPanel.paintAlignment(true);
191 int columnCount = invert ? (sqcol.getEndRes() - sqcol.getStartRes() + 1)
194 avcg.setStatus(MessageManager.formatMessage(
195 "label.view_controller_toggled_marked",
197 toggle ? MessageManager.getString("label.toggled")
198 : MessageManager.getString("label.marked"),
199 String.valueOf(columnCount),
200 invert ? MessageManager
201 .getString("label.not_containing")
202 : MessageManager.getString("label.containing"),
203 featureType, Integer.valueOf(nseq).toString() }));
209 avcg.setStatus(MessageManager.formatMessage(
210 "label.no_feature_of_type_found",
211 new String[] { featureType }));
215 alignPanel.paintAlignment(true);
222 * Sets a bit in the BitSet for each column (base 0) in the sequence
223 * collection which includes the specified feature type. Returns the number of
224 * sequences which have the feature in the selected range.
231 static int findColumnsWithFeature(String featureType,
232 SequenceCollectionI sqcol, BitSet bs)
234 final int startPosition = sqcol.getStartRes() + 1; // converted to base 1
235 final int endPosition = sqcol.getEndRes() + 1;
236 List<SequenceI> seqs = sqcol.getSequences();
238 for (SequenceI sq : seqs)
240 boolean sequenceHasFeature = false;
243 SequenceFeature[] sfs = sq.getSequenceFeatures();
246 int ist = sq.findIndex(sq.getStart());
247 int iend = sq.findIndex(sq.getEnd());
248 if (iend < startPosition || ist > endPosition)
250 // sequence not in region
253 for (SequenceFeature sf : sfs)
255 // future functionality - featureType == null means mark columns
256 // containing all displayed features
257 if (sf != null && (featureType.equals(sf.getType())))
259 // optimisation - could consider 'spos,apos' like cursor argument
260 // - findIndex wastes time by starting from first character and
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 >= startPosition
273 && sfStartCol <= endPosition)
275 bs.set(sfStartCol - 1);
276 sequenceHasFeature = true;
278 if (sfEndCol >= startPosition && sfEndCol <= endPosition)
280 bs.set(sfEndCol - 1);
281 sequenceHasFeature = true;
287 * contiguous feature - select feature positions (if any)
288 * within the selected region
290 if (sfStartCol > endPosition || sfEndCol < startPosition)
292 // feature is outside selected region
295 sequenceHasFeature = true;
296 if (sfStartCol < startPosition)
298 sfStartCol = startPosition;
300 if (sfStartCol < ist)
304 if (sfEndCol > endPosition)
306 sfEndCol = endPosition;
308 for (; sfStartCol <= sfEndCol; sfStartCol++)
310 bs.set(sfStartCol - 1); // convert to base 0
316 if (sequenceHasFeature)
326 public void sortAlignmentByFeatureDensity(List<String> typ)
328 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
331 protected void sortBy(List<String> typ, String methodText,
334 FeatureRenderer fr = alignPanel.getFeatureRenderer();
335 if (typ == null && fr != null)
337 typ = fr.getDisplayedFeatureTypes();
339 List<String> gps = null;
342 gps = fr.getDisplayedFeatureGroups();
344 AlignmentI al = viewport.getAlignment();
347 SequenceGroup sg = viewport.getSelectionGroup();
350 start = sg.getStartRes();
351 stop = sg.getEndRes();
356 stop = al.getWidth();
358 SequenceI[] oldOrder = al.getSequencesArray();
359 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
360 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
362 alignPanel.paintAlignment(true);
367 public void sortAlignmentByFeatureScore(List<String> typ)
369 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
373 public boolean parseFeaturesFile(String file, String protocol,
374 boolean relaxedIdMatching)
376 boolean featuresFile = false;
379 featuresFile = new FeaturesFile(false, file, protocol).parse(viewport
380 .getAlignment().getDataset(), alignPanel.getFeatureRenderer()
381 .getFeatureColours(), false, relaxedIdMatching);
382 } catch (Exception ex)
384 ex.printStackTrace();
389 avcg.refreshFeatureUI(true);
390 if (alignPanel.getFeatureRenderer() != null)
392 // update the min/max ranges where necessary
393 alignPanel.getFeatureRenderer().findAllFeatures(true);
395 if (avcg.getFeatureSettingsUI() != null)
397 avcg.getFeatureSettingsUI().discoverAllFeatureData();
399 alignPanel.paintAlignment(true);