2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
21 import jalview.datamodel.SequenceGroup;
22 import jalview.datamodel.SequenceI;
24 import java.util.Hashtable;
25 import java.util.List;
26 import java.util.Vector;
29 * various methods for defining groups on an alignment based on some other
38 * Divide the given sequences based on the equivalence of their corresponding
39 * selectedChars string. If exgroups is provided, existing groups will be
43 * @param selectedChars
47 public static SequenceGroup[] makeGroupsFrom(SequenceI[] sequences,
48 String[] selectedChars, List<SequenceGroup> list)
50 // TODO: determine how to get/recover input data for group generation
51 Hashtable gps = new Hashtable();
53 Hashtable pgroup = new Hashtable();
56 for (SequenceGroup sg : list)
58 for (SequenceI sq : sg.getSequences(null))
60 pgroup.put(sq.toString(), sg);
64 for (i = 0; i < sequences.length; i++)
66 String schar = selectedChars[i];
67 SequenceGroup pgp = (SequenceGroup) pgroup
68 .get(((Object) sequences[i]).toString());
71 schar = pgp.getName() + ":" + schar;
73 Vector svec = (Vector) gps.get(schar);
79 if (width < sequences[i].getLength())
81 width = sequences[i].getLength();
83 svec.addElement(sequences[i]);
86 java.util.Enumeration sge = gps.keys();
87 SequenceGroup[] groups = new SequenceGroup[gps.size()];
89 while (sge.hasMoreElements())
91 String key = (String) sge.nextElement();
92 SequenceGroup group = new SequenceGroup((Vector) gps.get(key),
93 "Subseq: " + key, null, true, true, false, 0, width - 1);
103 * subdivide the given sequences based on the distribution of features
105 * @param featureLabels
106 * - null or one or more feature types to filter on.
108 * - null or set of groups to filter features on
110 * - range for feature filter
112 * - range for feature filter
114 * - sequences to be divided
116 * - existing groups to be subdivided
118 * - density, description, score
120 public static void divideByFeature(String[] featureLabels,
121 String[] groupLabels, int start, int stop, SequenceI[] sequences,
122 Vector exgroups, String method)
124 // TODO implement divideByFeature
126 * if (method!=AlignmentSorter.FEATURE_SCORE &&
127 * method!=AlignmentSorter.FEATURE_LABEL &&
128 * method!=AlignmentSorter.FEATURE_DENSITY) { throw newError(
129 * "Implementation Error - sortByFeature method must be one of FEATURE_SCORE, FEATURE_LABEL or FEATURE_DENSITY."
130 * ); } boolean ignoreScore=method!=AlignmentSorter.FEATURE_SCORE;
131 * StringBuffer scoreLabel = new StringBuffer();
132 * scoreLabel.append(start+stop+method); // This doesn't work yet - we'd
133 * like to have a canonical ordering that can be preserved from call to call
134 * for (int i=0;featureLabels!=null && i<featureLabels.length; i++) {
135 * scoreLabel.append(featureLabels[i]==null ? "null" : featureLabels[i]); }
136 * for (int i=0;groupLabels!=null && i<groupLabels.length; i++) {
137 * scoreLabel.append(groupLabels[i]==null ? "null" : groupLabels[i]); }
138 * SequenceI[] seqs = alignment.getSequencesArray();
140 * boolean[] hasScore = new boolean[seqs.length]; // per sequence score //
141 * presence int hasScores = 0; // number of scores present on set double[]
142 * scores = new double[seqs.length]; int[] seqScores = new int[seqs.length];
143 * Object[] feats = new Object[seqs.length]; double min = 0, max = 0; for
144 * (int i = 0; i < seqs.length; i++) { SequenceFeature[] sf =
145 * seqs[i].getSequenceFeatures(); if (sf==null &&
146 * seqs[i].getDatasetSequence()!=null) { sf =
147 * seqs[i].getDatasetSequence().getSequenceFeatures(); } if (sf==null) { sf
148 * = new SequenceFeature[0]; } else { SequenceFeature[] tmp = new
149 * SequenceFeature[sf.length]; for (int s=0; s<tmp.length;s++) { tmp[s] =
150 * sf[s]; } sf = tmp; } int sstart = (start==-1) ? start :
151 * seqs[i].findPosition(start); int sstop = (stop==-1) ? stop :
152 * seqs[i].findPosition(stop); seqScores[i]=0; scores[i]=0.0; int
153 * n=sf.length; for (int f=0;f<sf.length;f++) { // filter for selection
154 * criteria if ( // ignore features outwith alignment start-stop positions.
155 * (sf[f].end < sstart || sf[f].begin > sstop) || // or ignore based on
156 * selection criteria (featureLabels != null &&
157 * !AlignmentSorter.containsIgnoreCase(sf[f].type, featureLabels)) ||
158 * (groupLabels != null // problem here: we cannot eliminate null feature
159 * group features && (sf[f].getFeatureGroup() != null &&
160 * !AlignmentSorter.containsIgnoreCase(sf[f].getFeatureGroup(),
161 * groupLabels)))) { // forget about this feature sf[f] = null; n--; } else
162 * { // or, also take a look at the scores if necessary. if (!ignoreScore &&
163 * sf[f].getScore()!=Float.NaN) { if (seqScores[i]==0) { hasScores++; }
164 * seqScores[i]++; hasScore[i] = true; scores[i] += sf[f].getScore(); //
165 * take the first instance of this // score. } } } SequenceFeature[] fs;
166 * feats[i] = fs = new SequenceFeature[n]; if (n>0) { n=0; for (int
167 * f=0;f<sf.length;f++) { if (sf[f]!=null) { ((SequenceFeature[])
168 * feats[i])[n++] = sf[f]; } } if (method==FEATURE_LABEL) { // order the
169 * labels by alphabet String[] labs = new String[fs.length]; for (int
170 * l=0;l<labs.length; l++) { labs[l] = (fs[l].getDescription()!=null ?
171 * fs[l].getDescription() : fs[l].getType()); }
172 * jalview.util.QuickSort.sort(labs, ((Object[]) feats[i])); } } if
173 * (hasScore[i]) { // compute average score scores[i]/=seqScores[i]; //
174 * update the score bounds. if (hasScores == 1) { max = min = scores[i]; }
175 * else { if (max < scores[i]) { max = scores[i]; } if (min > scores[i]) {
176 * min = scores[i]; } } } }
178 * if (method==FEATURE_SCORE) { if (hasScores == 0) { return; // do nothing
179 * - no scores present to sort by. } // pad score matrix if (hasScores <
180 * seqs.length) { for (int i = 0; i < seqs.length; i++) { if (!hasScore[i])
181 * { scores[i] = (max + i); } else { int nf=(feats[i]==null) ? 0
182 * :((SequenceFeature[]) feats[i]).length;
183 * System.err.println("Sorting on Score: seq "+seqs[i].getName()+
184 * " Feats: "+nf+" Score : "+scores[i]); } } }
186 * jalview.util.QuickSort.sort(scores, seqs); } else if
187 * (method==FEATURE_DENSITY) {
189 * // break ties between equivalent numbers for adjacent sequences by adding
190 * 1/Nseq*i on the original order double fr = 0.9/(1.0*seqs.length); for
191 * (int i=0;i<seqs.length; i++) { double nf; scores[i] =
192 * (0.05+fr*i)+(nf=((feats[i]==null) ? 0.0 :1.0*((SequenceFeature[])
193 * feats[i]).length));
194 * System.err.println("Sorting on Density: seq "+seqs[i].getName()+
195 * " Feats: "+nf+" Score : "+scores[i]); }
196 * jalview.util.QuickSort.sort(scores, seqs); } else { if
197 * (method==FEATURE_LABEL) { throw new Error("Not yet implemented."); } } if
198 * (lastSortByFeatureScore ==null ||
199 * scoreLabel.equals(lastSortByFeatureScore)) { setOrder(alignment, seqs); }
200 * else { setReverseOrder(alignment, seqs); } lastSortByFeatureScore =
201 * scoreLabel.toString();