2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.Annotation;
32 import jalview.datamodel.ColumnSelection;
33 import jalview.datamodel.SequenceCollectionI;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.datamodel.SequenceI;
37 import jalview.util.MessageManager;
38 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
39 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField;
41 import java.awt.Color;
42 import java.util.ArrayList;
43 import java.util.BitSet;
44 import java.util.List;
46 public class AlignViewController implements AlignViewControllerI
48 AlignViewportI viewport = null;
50 AlignmentViewPanel alignPanel = null;
53 * the GUI container that is handling interactions with the user
55 private AlignViewControllerGuiI avcg;
58 protected void finalize() throws Throwable
65 public AlignViewController(AlignViewControllerGuiI alignFrame,
66 AlignViewportI viewport, AlignmentViewPanel alignPanel)
68 this.avcg = alignFrame;
69 this.viewport = viewport;
70 this.alignPanel = alignPanel;
74 public void setViewportAndAlignmentPanel(AlignViewportI viewport,
75 AlignmentViewPanel alignPanel)
77 this.alignPanel = alignPanel;
78 this.viewport = viewport;
83 public boolean makeGroupsFromSelection()
86 if (viewport.getSelectionGroup() != null)
88 SequenceGroup[] gps = jalview.analysis.Grouping.makeGroupsFrom(
89 viewport.getSequenceSelection(),
90 viewport.getAlignmentView(true).getSequenceStrings(
91 viewport.getGapCharacter()), viewport.getAlignment()
93 viewport.getAlignment().deleteAllGroups();
94 viewport.clearSequenceColours();
95 viewport.setSelectionGroup(null);
96 // set view properties for each group
97 for (int g = 0; g < gps.length; g++)
99 // gps[g].setShowunconserved(viewport.getShowUnconserved());
100 gps[g].setshowSequenceLogo(viewport.isShowSequenceLogo());
101 viewport.getAlignment().addGroup(gps[g]);
102 Color col = new Color((int) (Math.random() * 255),
103 (int) (Math.random() * 255), (int) (Math.random() * 255));
104 col = col.brighter();
105 for (SequenceI sq : gps[g].getSequences(null))
107 viewport.setSequenceColour(sq, col);
116 public boolean createGroup()
119 SequenceGroup sg = viewport.getSelectionGroup();
122 viewport.getAlignment().addGroup(sg);
129 public boolean unGroup()
131 SequenceGroup sg = viewport.getSelectionGroup();
134 viewport.getAlignment().deleteGroup(sg);
141 public boolean deleteGroups()
143 if (viewport.getAlignment().getGroups() != null
144 && viewport.getAlignment().getGroups().size() > 0)
146 viewport.getAlignment().deleteAllGroups();
147 viewport.clearSequenceColours();
148 viewport.setSelectionGroup(null);
155 public boolean markColumnsContainingFeatures(boolean invert,
156 boolean extendCurrent, boolean toggle, String featureType)
158 // JBPNote this routine could also mark rows, not just columns.
159 // need a decent query structure to allow all types of feature searches
160 BitSet bs = new BitSet();
162 SequenceCollectionI sqcol = (viewport.getSelectionGroup() == null ? viewport
163 .getAlignment() : viewport.getSelectionGroup());
164 alStart = sqcol.getStartRes();
165 alw = sqcol.getEndRes() + 1;
166 List<SequenceI> seqs = sqcol.getSequences();
168 for (SequenceI sq : seqs)
173 SequenceI dsq = sq.getDatasetSequence();
174 while (dsq.getDatasetSequence() != null)
176 dsq = dsq.getDatasetSequence();
179 SequenceFeature[] sf = dsq.getSequenceFeatures();
182 int ist = sq.findIndex(sq.getStart());
183 int iend = sq.findIndex(sq.getEnd());
184 if (iend < alStart || ist > alw)
186 // sequence not in region
189 for (SequenceFeature sfpos : sf)
191 // future functionalty - featureType == null means mark columns
192 // containing all displayed features
193 if (sfpos != null && (featureType.equals(sfpos.getType())))
196 // optimisation - could consider 'spos,apos' like cursor argument
197 // - findIndex wastes time by starting from first character and
200 int i = sq.findIndex(sfpos.getBegin());
201 int j = sq.findIndex(sfpos.getEnd());
202 if (j < alStart || i > alw)
204 // feature is outside selected region
233 ColumnSelection cs = viewport.getColumnSelection();
234 if (bs.cardinality() > 0 || invert)
238 cs = new ColumnSelection();
249 // invert only in the currently selected sequence region
250 for (int i = bs.nextClearBit(alStart), ibs = bs.nextSetBit(alStart); i >= alStart
253 if (ibs < 0 || i < ibs)
255 if (toggle && cs.contains(i))
257 cs.removeElement(i++);
266 i = bs.nextClearBit(ibs);
267 ibs = bs.nextSetBit(i);
273 for (int i = bs.nextSetBit(alStart); i >= alStart; i = bs
276 if (toggle && cs.contains(i))
286 viewport.setColumnSelection(cs);
287 alignPanel.paintAlignment(true);
288 avcg.setStatus(MessageManager.formatMessage("label.view_controller_toggled_marked",
290 (toggle ? MessageManager.getString("label.toggled") : MessageManager.getString("label.marked")),
291 (invert ? (Integer.valueOf((alw - alStart) - bs.cardinality()).toString()):(Integer.valueOf(bs.cardinality()).toString())),
292 featureType, Integer.valueOf(nseq).toString()
298 avcg.setStatus(MessageManager.formatMessage("label.no_feature_of_type_found", new String[]{featureType}));
299 if (!extendCurrent && cs != null)
302 alignPanel.paintAlignment(true);
308 public static boolean filterAnnotations(Annotation[] annotations,
309 AnnotationFilterParameter filterParams, ColumnSelection cs)
311 cs.revealAllHiddenColumns();
316 if (annotations[count] != null)
319 boolean itemMatched = false;
321 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
322 && annotations[count].value > filterParams
323 .getThresholdValue())
327 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
328 && annotations[count].value < filterParams
329 .getThresholdValue())
334 if (filterParams.isFilterAlphaHelix()
335 && annotations[count].secondaryStructure == 'H')
340 if (filterParams.isFilterBetaSheet()
341 && annotations[count].secondaryStructure == 'E')
346 if (filterParams.isFilterTurn()
347 && annotations[count].secondaryStructure == 'S')
352 String regexSearchString = filterParams.getRegexString();
353 if (regexSearchString != null
354 && !filterParams.getRegexSearchFields().isEmpty())
356 List<SearchableAnnotationField> fields = filterParams
357 .getRegexSearchFields();
360 if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
361 && annotations[count].displayCharacter
362 .matches(regexSearchString))
366 } catch (java.util.regex.PatternSyntaxException pse)
368 if (annotations[count].displayCharacter
369 .equals(regexSearchString))
374 if (fields.contains(SearchableAnnotationField.DESCRIPTION)
375 && annotations[count].description != null
376 && annotations[count].description
377 .matches(regexSearchString))
385 cs.addElement(count);
389 } while (count < annotations.length);
394 public void sortAlignmentByFeatureDensity(String[] typ)
396 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
399 protected void sortBy(String[] typ, String methodText, final String method)
401 FeatureRenderer fr = alignPanel.getFeatureRenderer();
404 typ = fr==null ? null : fr.getDisplayedFeatureTypes();
407 gps = fr==null ? null : fr.getDisplayedFeatureGroups();
410 ArrayList types = new ArrayList();
411 for (int i = 0; i < typ.length; i++)
417 typ = new String[types.size()];
423 ArrayList grps = new ArrayList();
425 for (int i = 0; i < gps.length; i++)
432 gps = new String[grps.size()];
435 AlignmentI al = viewport.getAlignment();
438 SequenceGroup sg = viewport.getSelectionGroup();
441 start = sg.getStartRes();
442 stop = sg.getEndRes();
447 stop = al.getWidth();
449 SequenceI[] oldOrder = al.getSequencesArray();
450 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
451 avcg.addHistoryItem(new OrderCommand(methodText, oldOrder, viewport
453 alignPanel.paintAlignment(true);
458 public void sortAlignmentByFeatureScore(String[] typ)
460 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);