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;
25 import jalview.datamodel.*;
27 public class SeqsetUtils
31 * Store essential properties of a sequence in a hashtable for later recovery
32 * Keys are Name, Start, End, SeqFeatures, PdbId
38 public static Hashtable SeqCharacterHash(SequenceI seq)
40 Hashtable sqinfo = new Hashtable();
41 sqinfo.put("Name", seq.getName());
42 sqinfo.put("Start", new Integer(seq.getStart()));
43 sqinfo.put("End", new Integer(seq.getEnd()));
44 if (seq.getDescription() != null)
46 sqinfo.put("Description", seq.getDescription());
48 Vector sfeat = new Vector();
49 jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
50 if (sfarray != null && sfarray.length > 0)
52 for (int i = 0; i < sfarray.length; i++)
54 sfeat.addElement(sfarray[i]);
57 sqinfo.put("SeqFeatures", sfeat);
58 sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId()
60 sqinfo.put("datasetSequence",
61 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
62 : new Sequence("THISISAPLACEHOLDER", ""));
67 * Recover essential properties of a sequence from a hashtable TODO: replace
68 * these methods with something more elegant.
74 * @return boolean true if name was not updated from sqinfo Name entry
76 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
78 boolean namePresent = true;
83 String oldname = (String) sqinfo.get("Name");
84 Integer start = (Integer) sqinfo.get("Start");
85 Integer end = (Integer) sqinfo.get("End");
86 Vector sfeatures = (Vector) sqinfo.get("SeqFeatures");
87 Vector pdbid = (Vector) sqinfo.get("PdbId");
88 String description = (String) sqinfo.get("Description");
89 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
98 if (pdbid != null && pdbid.size() > 0)
103 if ((start != null) && (end != null))
105 sq.setStart(start.intValue());
106 sq.setEnd(end.intValue());
109 if ((sfeatures != null) && (sfeatures.size() > 0))
111 SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
112 for (int is = 0, isize = sfeatures.size(); is < isize; is++)
114 sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
116 sq.setSequenceFeatures(sfarray);
118 if (description != null)
120 sq.setDescription(description);
123 && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds
126 sq.setDatasetSequence(seqds);
133 * Form of the unique name used in uniquify for the i'th sequence in an
134 * ordered vector of sequences.
140 public static String unique_name(int i)
142 return new String("Sequence" + i);
146 * Generates a hash of SeqCharacterHash properties for each sequence in a
147 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
153 * boolean set this to rename each of the sequences to its
154 * unique_name(index) name
155 * @return Hashtable to be passed to
156 * @see deuniquify to recover original names (and properties) for renamed
159 public static Hashtable uniquify(SequenceI[] sequences,
162 // Generate a safely named sequence set and a hash to recover the sequence
164 Hashtable map = new Hashtable();
165 // String[] un_names = new String[sequences.length];
167 for (int i = 0; i < sequences.length; i++)
169 String safename = unique_name(i);
170 map.put(safename, SeqCharacterHash(sequences[i]));
174 sequences[i].setName(safename);
182 * recover unsafe sequence names and original properties for a sequence set
183 * using a map generated by
185 * @see uniquify(sequences,true)
192 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
194 return deuniquify(map, sequences, true);
198 * recover unsafe sequence names and original properties for a sequence set
199 * using a map generated by
201 * @see uniquify(sequences,true)
207 * when false, don't complain about sequences without any data in the
211 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
214 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
216 SequenceI msq = null;
217 Enumeration keys = map.keys();
218 Vector unmatched = new Vector();
219 for (int i = 0, j = sequences.length; i < j; i++)
221 unmatched.addElement(sequences[i]);
223 while (keys.hasMoreElements())
225 Object key = keys.nextElement();
226 if (key instanceof String)
228 if ((msq = matcher.findIdMatch((String) key)) != null)
230 Hashtable sqinfo = (Hashtable) map.get(key);
231 unmatched.removeElement(msq);
232 SeqCharacterUnhash(msq, sqinfo);
238 System.err.println("Can't find '" + ((String) key)
239 + "' in uniquified alignment");
244 if (unmatched.size() > 0 && !quiet)
246 System.err.println("Did not find matches for :");
247 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out
248 .println(((SequenceI) i.nextElement()).getName()))
259 * returns a subset of the sequenceI seuqences, including only those that
260 * contain at least one residue.
264 * @return SequenceI[]
266 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
268 // Identify first row of alignment with residues for prediction
269 boolean ungapped[] = new boolean[sequences.length];
271 for (int i = 0, j = sequences.length; i < j; i++)
273 String tempseq = jalview.analysis.AlignSeq.extractGaps(
274 jalview.util.Comparison.GapChars,
275 sequences[i].getSequenceAsString());
277 if (tempseq.length() == 0)
289 return null; // no minimal set
291 // compose minimal set
292 SequenceI[] mset = new SequenceI[msflen];
293 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
297 mset[k++] = sequences[i];