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.analysis;
23 import jalview.datamodel.ColumnSelection;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Vector;
34 * various methods for defining groups on an alignment based on some other
43 * Divide the given sequences based on the equivalence of their corresponding
44 * selectedChars string. If exgroups is provided, existing groups will be
48 * @param selectedChars
52 public static SequenceGroup[] makeGroupsFrom(SequenceI[] sequences,
53 String[] selectedChars, List<SequenceGroup> list)
55 // TODO: determine how to get/recover input data for group generation
56 Map<String, List<SequenceI>> gps = new HashMap<String, List<SequenceI>>();
58 Map<String, SequenceGroup> pgroup = new HashMap<String, SequenceGroup>();
61 for (SequenceGroup sg : list)
63 for (SequenceI sq : sg.getSequences(null))
65 pgroup.put(sq.toString(), sg);
69 for (i = 0; i < sequences.length; i++)
71 String schar = selectedChars[i];
72 SequenceGroup pgp = pgroup.get(((Object) sequences[i]).toString());
75 schar = pgp.getName() + ":" + schar;
77 List<SequenceI> svec = gps.get(schar);
80 svec = new ArrayList<SequenceI>();
83 if (width < sequences[i].getLength())
85 width = sequences[i].getLength();
87 svec.add(sequences[i]);
90 SequenceGroup[] groups = new SequenceGroup[gps.size()];
92 for (String key : gps.keySet())
94 SequenceGroup group = new SequenceGroup(gps.get(key),
95 "Subseq: " + key, null, true, true, false, 0, width - 1);
105 * Divide the given sequences based on the equivalence of characters at
106 * selected columns If exgroups is provided, existing groups will be
110 * @param columnSelection
114 public static SequenceGroup[] makeGroupsFromCols(SequenceI[] sequences,
115 ColumnSelection cs, List<SequenceGroup> list)
117 // TODO: determine how to get/recover input data for group generation
118 Map<String, List<SequenceI>> gps = new HashMap<String, List<SequenceI>>();
119 Map<String, SequenceGroup> pgroup = new HashMap<String, SequenceGroup>();
122 for (SequenceGroup sg : list)
124 for (SequenceI sq : sg.getSequences(null))
126 pgroup.put(sq.toString(), sg);
132 * get selected columns (in the order they were selected);
133 * note this could include right-to-left ranges
135 int[] spos = new int[cs.getSelected().size()];
138 for (Integer pos : cs.getSelected())
140 spos[i++] = pos.intValue();
143 for (i = 0; i < sequences.length; i++)
145 int slen = sequences[i].getLength();
151 SequenceGroup pgp = pgroup.get(((Object) sequences[i]).toString());
152 StringBuilder schar = new StringBuilder();
155 schar.append(pgp.getName() + ":");
165 schar.append(sequences[i].getCharAt(p));
168 List<SequenceI> svec = gps.get(schar.toString());
171 svec = new ArrayList<SequenceI>();
172 gps.put(schar.toString(), svec);
174 svec.add(sequences[i]);
177 SequenceGroup[] groups = new SequenceGroup[gps.size()];
179 for (String key : gps.keySet())
181 SequenceGroup group = new SequenceGroup(gps.get(key),
182 "Subseq: " + key, null, true, true, false, 0, width - 1);
192 * subdivide the given sequences based on the distribution of features
194 * @param featureLabels
195 * - null or one or more feature types to filter on.
197 * - null or set of groups to filter features on
199 * - range for feature filter
201 * - range for feature filter
203 * - sequences to be divided
205 * - existing groups to be subdivided
207 * - density, description, score
209 public static void divideByFeature(String[] featureLabels,
210 String[] groupLabels, int start, int stop, SequenceI[] sequences,
211 Vector exgroups, String method)
213 // TODO implement divideByFeature
215 * if (method!=AlignmentSorter.FEATURE_SCORE &&
216 * method!=AlignmentSorter.FEATURE_LABEL &&
217 * method!=AlignmentSorter.FEATURE_DENSITY) { throw newError(
218 * "Implementation Error - sortByFeature method must be one of FEATURE_SCORE, FEATURE_LABEL or FEATURE_DENSITY."
219 * ); } boolean ignoreScore=method!=AlignmentSorter.FEATURE_SCORE;
220 * StringBuffer scoreLabel = new StringBuffer();
221 * scoreLabel.append(start+stop+method); // This doesn't work yet - we'd
222 * like to have a canonical ordering that can be preserved from call to call
223 * for (int i=0;featureLabels!=null && i<featureLabels.length; i++) {
224 * scoreLabel.append(featureLabels[i]==null ? "null" : featureLabels[i]); }
225 * for (int i=0;groupLabels!=null && i<groupLabels.length; i++) {
226 * scoreLabel.append(groupLabels[i]==null ? "null" : groupLabels[i]); }
227 * SequenceI[] seqs = alignment.getSequencesArray();
229 * boolean[] hasScore = new boolean[seqs.length]; // per sequence score //
230 * presence int hasScores = 0; // number of scores present on set double[]
231 * scores = new double[seqs.length]; int[] seqScores = new int[seqs.length];
232 * Object[] feats = new Object[seqs.length]; double min = 0, max = 0; for
233 * (int i = 0; i < seqs.length; i++) { SequenceFeature[] sf =
234 * seqs[i].getSequenceFeatures(); if (sf==null &&
235 * seqs[i].getDatasetSequence()!=null) { sf =
236 * seqs[i].getDatasetSequence().getSequenceFeatures(); } if (sf==null) { sf
237 * = new SequenceFeature[0]; } else { SequenceFeature[] tmp = new
238 * SequenceFeature[sf.length]; for (int s=0; s<tmp.length;s++) { tmp[s] =
239 * sf[s]; } sf = tmp; } int sstart = (start==-1) ? start :
240 * seqs[i].findPosition(start); int sstop = (stop==-1) ? stop :
241 * seqs[i].findPosition(stop); seqScores[i]=0; scores[i]=0.0; int
242 * n=sf.length; for (int f=0;f<sf.length;f++) { // filter for selection
243 * criteria if ( // ignore features outwith alignment start-stop positions.
244 * (sf[f].end < sstart || sf[f].begin > sstop) || // or ignore based on
245 * selection criteria (featureLabels != null &&
246 * !AlignmentSorter.containsIgnoreCase(sf[f].type, featureLabels)) ||
247 * (groupLabels != null // problem here: we cannot eliminate null feature
248 * group features && (sf[f].getFeatureGroup() != null &&
249 * !AlignmentSorter.containsIgnoreCase(sf[f].getFeatureGroup(),
250 * groupLabels)))) { // forget about this feature sf[f] = null; n--; } else
251 * { // or, also take a look at the scores if necessary. if (!ignoreScore &&
252 * sf[f].getScore()!=Float.NaN) { if (seqScores[i]==0) { hasScores++; }
253 * seqScores[i]++; hasScore[i] = true; scores[i] += sf[f].getScore(); //
254 * take the first instance of this // score. } } } SequenceFeature[] fs;
255 * feats[i] = fs = new SequenceFeature[n]; if (n>0) { n=0; for (int
256 * f=0;f<sf.length;f++) { if (sf[f]!=null) { ((SequenceFeature[])
257 * feats[i])[n++] = sf[f]; } } if (method==FEATURE_LABEL) { // order the
258 * labels by alphabet String[] labs = new String[fs.length]; for (int
259 * l=0;l<labs.length; l++) { labs[l] = (fs[l].getDescription()!=null ?
260 * fs[l].getDescription() : fs[l].getType()); }
261 * jalview.util.QuickSort.sort(labs, ((Object[]) feats[i])); } } if
262 * (hasScore[i]) { // compute average score scores[i]/=seqScores[i]; //
263 * update the score bounds. if (hasScores == 1) { max = min = scores[i]; }
264 * else { if (max < scores[i]) { max = scores[i]; } if (min > scores[i]) {
265 * min = scores[i]; } } } }
267 * if (method==FEATURE_SCORE) { if (hasScores == 0) { return; // do nothing
268 * - no scores present to sort by. } // pad score matrix if (hasScores <
269 * seqs.length) { for (int i = 0; i < seqs.length; i++) { if (!hasScore[i])
270 * { scores[i] = (max + i); } else { int nf=(feats[i]==null) ? 0
271 * :((SequenceFeature[]) feats[i]).length;
272 * System.err.println("Sorting on Score: seq "+seqs[i].getName()+
273 * " Feats: "+nf+" Score : "+scores[i]); } } }
275 * jalview.util.QuickSort.sort(scores, seqs); } else if
276 * (method==FEATURE_DENSITY) {
278 * // break ties between equivalent numbers for adjacent sequences by adding
279 * 1/Nseq*i on the original order double fr = 0.9/(1.0*seqs.length); for
280 * (int i=0;i<seqs.length; i++) { double nf; scores[i] =
281 * (0.05+fr*i)+(nf=((feats[i]==null) ? 0.0 :1.0*((SequenceFeature[])
282 * feats[i]).length));
283 * System.err.println("Sorting on Density: seq "+seqs[i].getName()+
284 * " Feats: "+nf+" Score : "+scores[i]); }
285 * jalview.util.QuickSort.sort(scores, seqs); } else { if
286 * (method==FEATURE_LABEL) { throw new Error("Not yet implemented."); } } if
287 * (lastSortByFeatureScore ==null ||
288 * scoreLabel.equals(lastSortByFeatureScore)) { setOrder(alignment, seqs); }
289 * else { setReverseOrder(alignment, seqs); } lastSortByFeatureScore =
290 * scoreLabel.toString();