2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
35 * Copyright: Copyright (c) 2004
39 * Company: Dundee University
42 * @author not attributable
45 public class SeqsetUtils
49 * Store essential properties of a sequence in a hashtable for later recovery
50 * Keys are Name, Start, End, SeqFeatures, PdbId
56 public static Hashtable SeqCharacterHash(SequenceI seq)
58 Hashtable sqinfo = new Hashtable();
59 sqinfo.put("Name", seq.getName());
60 sqinfo.put("Start", new Integer(seq.getStart()));
61 sqinfo.put("End", new Integer(seq.getEnd()));
62 if (seq.getDescription() != null)
64 sqinfo.put("Description", seq.getDescription());
66 Vector sfeat = new Vector();
67 jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
68 if (sfarray != null && sfarray.length > 0)
70 for (int i = 0; i < sfarray.length; i++)
72 sfeat.addElement(sfarray[i]);
75 sqinfo.put("SeqFeatures", sfeat);
76 sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId()
78 sqinfo.put("datasetSequence", (seq.getDatasetSequence() != null) ? seq
79 .getDatasetSequence() : new Sequence("THISISAPLACEHOLDER", ""));
84 * Recover essential properties of a sequence from a hashtable TODO: replace
85 * these methods with something more elegant.
91 * @return boolean true if name was not updated from sqinfo Name entry
93 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
95 boolean namePresent = true;
100 String oldname = (String) sqinfo.get("Name");
101 Integer start = (Integer) sqinfo.get("Start");
102 Integer end = (Integer) sqinfo.get("End");
103 Vector sfeatures = (Vector) sqinfo.get("SeqFeatures");
104 Vector pdbid = (Vector) sqinfo.get("PdbId");
105 String description = (String) sqinfo.get("Description");
106 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
115 if (pdbid != null && pdbid.size() > 0)
120 if ((start != null) && (end != null))
122 sq.setStart(start.intValue());
123 sq.setEnd(end.intValue());
126 if ((sfeatures != null) && (sfeatures.size() > 0))
128 SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
129 for (int is = 0, isize = sfeatures.size(); is < isize; is++)
131 sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
133 sq.setSequenceFeatures(sfarray);
135 if (description != null)
137 sq.setDescription(description);
140 && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds
143 sq.setDatasetSequence(seqds);
150 * Form of the unique name used in uniquify for the i'th sequence in an
151 * ordered vector of sequences.
157 public static String unique_name(int i)
159 return new String("Sequence" + i);
163 * Generates a hash of SeqCharacterHash properties for each sequence in a
164 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
170 * boolean set this to rename each of the sequences to its
171 * unique_name(index) name
172 * @return Hashtable to be passed to
173 * @see deuniquify to recover original names (and properties) for renamed
176 public static Hashtable uniquify(SequenceI[] sequences,
179 // Generate a safely named sequence set and a hash to recover the sequence
181 Hashtable map = new Hashtable();
182 // String[] un_names = new String[sequences.length];
184 for (int i = 0; i < sequences.length; i++)
186 String safename = unique_name(i);
187 map.put(safename, SeqCharacterHash(sequences[i]));
191 sequences[i].setName(safename);
199 * recover unsafe sequence names and original properties for a sequence set
200 * using a map generated by
202 * @see uniquify(sequences,true)
209 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
211 return deuniquify(map, sequences, true);
215 * recover unsafe sequence names and original properties for a sequence set
216 * using a map generated by
218 * @see uniquify(sequences,true)
224 * when false, don't complain about sequences without any data
228 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
231 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
233 SequenceI msq = null;
234 Enumeration keys = map.keys();
235 Vector unmatched = new Vector();
236 for (int i = 0, j = sequences.length; i < j; i++)
238 unmatched.addElement(sequences[i]);
240 while (keys.hasMoreElements())
242 Object key = keys.nextElement();
243 if (key instanceof String)
245 if ((msq = matcher.findIdMatch((String) key)) != null)
247 Hashtable sqinfo = (Hashtable) map.get(key);
248 unmatched.removeElement(msq);
249 SeqCharacterUnhash(msq, sqinfo);
255 System.err.println("Can't find '" + ((String) key)
256 + "' in uniquified alignment");
261 if (unmatched.size() > 0 && !quiet)
263 System.err.println("Did not find matches for :");
264 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out
265 .println(((SequenceI) i.nextElement()).getName()))
276 * returns a subset of the sequenceI seuqences, including only those that
277 * contain at least one residue.
281 * @return SequenceI[]
283 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
285 // Identify first row of alignment with residues for prediction
286 boolean ungapped[] = new boolean[sequences.length];
288 for (int i = 0, j = sequences.length; i < j; i++)
290 String tempseq = jalview.analysis.AlignSeq.extractGaps(
291 jalview.util.Comparison.GapChars, sequences[i]
292 .getSequenceAsString());
294 if (tempseq.length() == 0)
306 return null; // no minimal set
308 // compose minimal set
309 SequenceI[] mset = new SequenceI[msflen];
310 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
314 mset[k++] = sequences[i];