2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
34 * Copyright: Copyright (c) 2004
38 * Company: Dundee University
41 * @author not attributable
44 public class SeqsetUtils
48 * Store essential properties of a sequence in a hashtable for later recovery
49 * Keys are Name, Start, End, SeqFeatures, PdbId
55 public static Hashtable SeqCharacterHash(SequenceI seq)
57 Hashtable sqinfo = new Hashtable();
58 sqinfo.put("Name", seq.getName());
59 sqinfo.put("Start", new Integer(seq.getStart()));
60 sqinfo.put("End", new Integer(seq.getEnd()));
61 if (seq.getDescription() != null)
63 sqinfo.put("Description", seq.getDescription());
65 Vector sfeat = new Vector();
66 jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
67 if (sfarray != null && sfarray.length > 0)
69 for (int i = 0; i < sfarray.length; i++)
71 sfeat.addElement(sfarray[i]);
74 sqinfo.put("SeqFeatures", sfeat);
75 sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId()
77 sqinfo.put("datasetSequence", (seq.getDatasetSequence() != null) ? seq
78 .getDatasetSequence() : new Sequence("THISISAPLACEHOLDER", ""));
83 * Recover essential properties of a sequence from a hashtable TODO: replace
84 * these methods with something more elegant.
90 * @return boolean true if name was not updated from sqinfo Name entry
92 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
94 boolean namePresent = true;
99 String oldname = (String) sqinfo.get("Name");
100 Integer start = (Integer) sqinfo.get("Start");
101 Integer end = (Integer) sqinfo.get("End");
102 Vector sfeatures = (Vector) sqinfo.get("SeqFeatures");
103 Vector pdbid = (Vector) sqinfo.get("PdbId");
104 String description = (String) sqinfo.get("Description");
105 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
114 if (pdbid != null && pdbid.size() > 0)
119 if ((start != null) && (end != null))
121 sq.setStart(start.intValue());
122 sq.setEnd(end.intValue());
125 if ((sfeatures != null) && (sfeatures.size() > 0))
127 SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
128 for (int is = 0, isize = sfeatures.size(); is < isize; is++)
130 sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
132 sq.setSequenceFeatures(sfarray);
134 if (description != null)
136 sq.setDescription(description);
139 && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds
142 sq.setDatasetSequence(seqds);
149 * Form of the unique name used in uniquify for the i'th sequence in an
150 * ordered vector of sequences.
156 public static String unique_name(int i)
158 return new String("Sequence" + i);
162 * Generates a hash of SeqCharacterHash properties for each sequence in a
163 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
169 * boolean set this to rename each of the sequences to its
170 * unique_name(index) name
171 * @return Hashtable to be passed to
172 * @see deuniquify to recover original names (and properties) for renamed
175 public static Hashtable uniquify(SequenceI[] sequences,
178 // Generate a safely named sequence set and a hash to recover the sequence
180 Hashtable map = new Hashtable();
181 // String[] un_names = new String[sequences.length];
183 for (int i = 0; i < sequences.length; i++)
185 String safename = unique_name(i);
186 map.put(safename, SeqCharacterHash(sequences[i]));
190 sequences[i].setName(safename);
198 * recover unsafe sequence names and original properties for a sequence set
199 * using a map generated by
201 * @see uniquify(sequences,true)
208 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
210 return deuniquify(map, sequences, true);
214 * recover unsafe sequence names and original properties for a sequence set
215 * using a map generated by
217 * @see uniquify(sequences,true)
223 * when false, don't complain about sequences without any data in the
227 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
230 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
232 SequenceI msq = null;
233 Enumeration keys = map.keys();
234 Vector unmatched = new Vector();
235 for (int i = 0, j = sequences.length; i < j; i++)
237 unmatched.addElement(sequences[i]);
239 while (keys.hasMoreElements())
241 Object key = keys.nextElement();
242 if (key instanceof String)
244 if ((msq = matcher.findIdMatch((String) key)) != null)
246 Hashtable sqinfo = (Hashtable) map.get(key);
247 unmatched.removeElement(msq);
248 SeqCharacterUnhash(msq, sqinfo);
254 System.err.println("Can't find '" + ((String) key)
255 + "' in uniquified alignment");
260 if (unmatched.size() > 0 && !quiet)
262 System.err.println("Did not find matches for :");
263 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out
264 .println(((SequenceI) i.nextElement()).getName()))
275 * returns a subset of the sequenceI seuqences, including only those that
276 * contain at least one residue.
280 * @return SequenceI[]
282 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
284 // Identify first row of alignment with residues for prediction
285 boolean ungapped[] = new boolean[sequences.length];
287 for (int i = 0, j = sequences.length; i < j; i++)
289 String tempseq = jalview.analysis.AlignSeq.extractGaps(
290 jalview.util.Comparison.GapChars, sequences[i]
291 .getSequenceAsString());
293 if (tempseq.length() == 0)
305 return null; // no minimal set
307 // compose minimal set
308 SequenceI[] mset = new SequenceI[msflen];
309 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
313 mset[k++] = sequences[i];