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.List;
32 import java.util.Vector;
34 public class SeqsetUtils
38 * Store essential properties of a sequence in a hashtable for later recovery
39 * Keys are Name, Start, End, SeqFeatures, PdbId, HMM
45 public static Hashtable SeqCharacterHash(SequenceI seq)
47 Hashtable sqinfo = new Hashtable();
48 sqinfo.put("Name", seq.getName());
49 sqinfo.put("Start", new Integer(seq.getStart()));
50 sqinfo.put("End", new Integer(seq.getEnd()));
51 if (seq.getDescription() != null)
53 sqinfo.put("Description", seq.getDescription());
56 Vector<SequenceFeature> sfeat = new Vector<>();
57 List<SequenceFeature> sfs = seq.getFeatures().getAllFeatures();
60 if (seq.getDatasetSequence() == null)
62 sqinfo.put("SeqFeatures", sfeat);
64 (seq.getAllPDBEntries() != null) ? seq.getAllPDBEntries()
65 : new Vector<PDBEntry>());
69 sqinfo.put("datasetSequence",
70 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
71 : new Sequence("THISISAPLACEHOLDER", ""));
73 if (seq.hasHMMProfile())
75 sqinfo.put("HMM", seq.getHMM());
81 * Recover essential properties of a sequence from a hashtable TODO: replace
82 * these methods with something more elegant.
88 * @return boolean true if name was not updated from sqinfo Name entry
90 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
92 boolean namePresent = true;
97 String oldname = (String) sqinfo.get("Name");
98 Integer start = (Integer) sqinfo.get("Start");
99 Integer end = (Integer) sqinfo.get("End");
100 Vector<SequenceFeature> sfeatures = (Vector<SequenceFeature>) sqinfo
102 Vector<PDBEntry> pdbid = (Vector<PDBEntry>) sqinfo.get("PdbId");
103 String description = (String) sqinfo.get("Description");
104 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
105 HiddenMarkovModel hmm = (HiddenMarkovModel) sqinfo.get("HMM");
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.isEmpty())
127 sq.setSequenceFeatures(sfeatures);
129 if (description != null)
131 sq.setDescription(description);
133 if ((seqds != null) && !(seqds.getName().equals("THISISAPLACEHOLDER")
134 && seqds.getLength() == 0))
136 if (sfeatures != null)
139 "Implementation error: setting dataset sequence for a sequence which has sequence features.\n\tDataset sequence features will not be visible.");
141 sq.setDatasetSequence(seqds);
146 sq.setHMM(new HiddenMarkovModel(hmm, sq));
152 * Form of the unique name used in uniquify for the i'th sequence in an
153 * ordered vector of sequences.
159 public static String unique_name(int i)
161 return new String("Sequence" + i);
165 * Generates a hash of SeqCharacterHash properties for each sequence in a
166 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
172 * boolean set this to rename each of the sequences to its
173 * unique_name(index) name
174 * @return Hashtable to be passed to
175 * @see deuniquify to recover original names (and properties) for renamed
178 public static Hashtable uniquify(SequenceI[] sequences,
181 // Generate a safely named sequence set and a hash to recover the sequence
183 Hashtable map = new Hashtable();
184 // String[] un_names = new String[sequences.length];
186 for (int i = 0; i < sequences.length; i++)
188 String safename = unique_name(i);
189 map.put(safename, SeqCharacterHash(sequences[i]));
193 sequences[i].setName(safename);
201 * recover unsafe sequence names and original properties for a sequence set
202 * using a map generated by
204 * @see uniquify(sequences,true)
211 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
213 return deuniquify(map, sequences, true);
217 * recover unsafe sequence names and original properties for a sequence set
218 * using a map generated by
220 * @see uniquify(sequences,true)
226 * when false, don't complain about sequences without any data in the
230 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
233 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
235 SequenceI msq = null;
236 Enumeration keys = map.keys();
237 Vector unmatched = new Vector();
238 for (int i = 0, j = sequences.length; i < j; i++)
240 unmatched.addElement(sequences[i]);
242 while (keys.hasMoreElements())
244 Object key = keys.nextElement();
245 if (key instanceof String)
247 if ((msq = matcher.findIdMatch((String) key)) != null)
249 Hashtable sqinfo = (Hashtable) map.get(key);
250 unmatched.removeElement(msq);
251 SeqCharacterUnhash(msq, sqinfo);
257 System.err.println("Can't find '" + ((String) key)
258 + "' in uniquified alignment");
263 if (unmatched.size() > 0 && !quiet)
265 System.err.println("Did not find matches for :");
266 for (Enumeration i = unmatched.elements(); i
267 .hasMoreElements(); System.out
268 .println(((SequenceI) i.nextElement()).getName()))
279 * returns a subset of the sequenceI seuqences, including only those that
280 * contain at least one residue.
284 * @return SequenceI[]
286 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
288 // Identify first row of alignment with residues for prediction
289 boolean ungapped[] = new boolean[sequences.length];
291 for (int i = 0, j = sequences.length; i < j; i++)
293 String tempseq = jalview.analysis.AlignSeq.extractGaps(
294 jalview.util.Comparison.GapChars,
295 sequences[i].getSequenceAsString());
297 if (tempseq.length() == 0)
309 return null; // no minimal set
311 // compose minimal set
312 SequenceI[] mset = new SequenceI[msflen];
313 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
317 mset[k++] = sequences[i];