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.HiddenMarkovModel;
24 import jalview.datamodel.PDBEntry;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
29 import java.util.Enumeration;
30 import java.util.Hashtable;
31 import java.util.Vector;
33 public class SeqsetUtils
37 * Store essential properties of a sequence in a hashtable for later recovery
38 * Keys are Name, Start, End, SeqFeatures, PdbId, HMM
44 public static Hashtable SeqCharacterHash(SequenceI seq)
46 Hashtable sqinfo = new Hashtable();
47 sqinfo.put("Name", seq.getName());
48 sqinfo.put("Start", new Integer(seq.getStart()));
49 sqinfo.put("End", new Integer(seq.getEnd()));
50 if (seq.getDescription() != null)
52 sqinfo.put("Description", seq.getDescription());
54 Vector sfeat = new Vector();
55 jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
56 if (sfarray != null && sfarray.length > 0)
58 for (int i = 0; i < sfarray.length; i++)
60 sfeat.addElement(sfarray[i]);
63 if (seq.getDatasetSequence() == null)
65 sqinfo.put("SeqFeatures", sfeat);
67 (seq.getAllPDBEntries() != null) ? seq.getAllPDBEntries()
68 : new Vector<PDBEntry>());
72 sqinfo.put("datasetSequence",
73 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
74 : new Sequence("THISISAPLACEHOLDER", ""));
76 if (seq.isHMMConsensusSequence())
78 sqinfo.put("HMM", seq.getHMM());
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<PDBEntry> pdbid = (Vector<PDBEntry>) sqinfo.get("PdbId");
105 String description = (String) sqinfo.get("Description");
106 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
107 HiddenMarkovModel hmm = (HiddenMarkovModel) sqinfo.get("HMM");
116 if (pdbid != null && pdbid.size() > 0)
121 if ((start != null) && (end != null))
123 sq.setStart(start.intValue());
124 sq.setEnd(end.intValue());
127 if ((sfeatures != null) && (sfeatures.size() > 0))
129 SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
130 for (int is = 0, isize = sfeatures.size(); is < isize; is++)
132 sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
134 sq.setSequenceFeatures(sfarray);
136 if (description != null)
138 sq.setDescription(description);
140 if ((seqds != null) && !(seqds.getName().equals("THISISAPLACEHOLDER")
141 && seqds.getLength() == 0))
143 if (sfeatures != null)
146 "Implementation error: setting dataset sequence for a sequence which has sequence features.\n\tDataset sequence features will not be visible.");
148 sq.setDatasetSequence(seqds);
153 sq.setHMM(new HiddenMarkovModel(hmm));
154 sq.setIsHMMConsensusSequence(true);
160 * Form of the unique name used in uniquify for the i'th sequence in an
161 * ordered vector of sequences.
167 public static String unique_name(int i)
169 return new String("Sequence" + i);
173 * Generates a hash of SeqCharacterHash properties for each sequence in a
174 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
180 * boolean set this to rename each of the sequences to its
181 * unique_name(index) name
182 * @return Hashtable to be passed to
183 * @see deuniquify to recover original names (and properties) for renamed
186 public static Hashtable uniquify(SequenceI[] sequences,
189 // Generate a safely named sequence set and a hash to recover the sequence
191 Hashtable map = new Hashtable();
192 // String[] un_names = new String[sequences.length];
194 for (int i = 0; i < sequences.length; i++)
196 String safename = unique_name(i);
197 map.put(safename, SeqCharacterHash(sequences[i]));
201 sequences[i].setName(safename);
209 * recover unsafe sequence names and original properties for a sequence set
210 * using a map generated by
212 * @see uniquify(sequences,true)
219 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
221 return deuniquify(map, sequences, true);
225 * recover unsafe sequence names and original properties for a sequence set
226 * using a map generated by
228 * @see uniquify(sequences,true)
234 * when false, don't complain about sequences without any data in the
238 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
241 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
243 SequenceI msq = null;
244 Enumeration keys = map.keys();
245 Vector unmatched = new Vector();
246 for (int i = 0, j = sequences.length; i < j; i++)
248 unmatched.addElement(sequences[i]);
250 while (keys.hasMoreElements())
252 Object key = keys.nextElement();
253 if (key instanceof String)
255 if ((msq = matcher.findIdMatch((String) key)) != null)
257 Hashtable sqinfo = (Hashtable) map.get(key);
258 unmatched.removeElement(msq);
259 SeqCharacterUnhash(msq, sqinfo);
265 System.err.println("Can't find '" + ((String) key)
266 + "' in uniquified alignment");
271 if (unmatched.size() > 0 && !quiet)
273 System.err.println("Did not find matches for :");
274 for (Enumeration i = unmatched.elements(); i
275 .hasMoreElements(); System.out
276 .println(((SequenceI) i.nextElement()).getName()))
287 * returns a subset of the sequenceI seuqences, including only those that
288 * contain at least one residue.
292 * @return SequenceI[]
294 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
296 // Identify first row of alignment with residues for prediction
297 boolean ungapped[] = new boolean[sequences.length];
299 for (int i = 0, j = sequences.length; i < j; i++)
301 String tempseq = jalview.analysis.AlignSeq.extractGaps(
302 jalview.util.Comparison.GapChars,
303 sequences[i].getSequenceAsString());
305 if (tempseq.length() == 0)
317 return null; // no minimal set
319 // compose minimal set
320 SequenceI[] mset = new SequenceI[msflen];
321 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
325 mset[k++] = sequences[i];