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.PDBEntry;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.Comparison;
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
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 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 sqinfo.put("SeqFeatures", sfeat);
64 sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId()
65 : new Vector<PDBEntry>());
66 sqinfo.put("datasetSequence",
67 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
68 : new Sequence("THISISAPLACEHOLDER", ""));
73 * Recover essential properties of a sequence from a hashtable TODO: replace
74 * these methods with something more elegant.
80 * @return boolean true if name was not updated from sqinfo Name entry
82 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
84 boolean namePresent = true;
89 String oldname = (String) sqinfo.get("Name");
90 Integer start = (Integer) sqinfo.get("Start");
91 Integer end = (Integer) sqinfo.get("End");
92 Vector sfeatures = (Vector) sqinfo.get("SeqFeatures");
93 Vector<PDBEntry> pdbid = (Vector<PDBEntry>) sqinfo.get("PdbId");
94 String description = (String) sqinfo.get("Description");
95 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
104 if (pdbid != null && pdbid.size() > 0)
109 if ((start != null) && (end != null))
111 sq.setStart(start.intValue());
112 sq.setEnd(end.intValue());
115 if ((sfeatures != null) && (sfeatures.size() > 0))
117 SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
118 for (int is = 0, isize = sfeatures.size(); is < isize; is++)
120 sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
122 sq.setSequenceFeatures(sfarray);
124 if (description != null)
126 sq.setDescription(description);
129 && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds
132 sq.setDatasetSequence(seqds);
139 * Form of the unique name used in uniquify for the i'th sequence in an
140 * ordered vector of sequences.
146 public static String unique_name(int i)
148 return new String("Sequence" + i);
152 * Generates a hash of SeqCharacterHash properties for each sequence in a
153 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
159 * boolean set this to rename each of the sequences to its
160 * unique_name(index) name
161 * @return Hashtable to be passed to
162 * @see deuniquify to recover original names (and properties) for renamed
165 public static Hashtable uniquify(SequenceI[] sequences,
168 // Generate a safely named sequence set and a hash to recover the sequence
170 Hashtable map = new Hashtable();
171 // String[] un_names = new String[sequences.length];
173 for (int i = 0; i < sequences.length; i++)
175 String safename = unique_name(i);
176 map.put(safename, SeqCharacterHash(sequences[i]));
180 sequences[i].setName(safename);
188 * recover unsafe sequence names and original properties for a sequence set
189 * using a map generated by
191 * @see uniquify(sequences,true)
198 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
200 return deuniquify(map, sequences, true);
204 * recover unsafe sequence names and original properties for a sequence set
205 * using a map generated by
207 * @see uniquify(sequences,true)
213 * when false, don't complain about sequences without any data in the
217 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
220 SequenceIdMatcher matcher = new SequenceIdMatcher(sequences);
221 SequenceI msq = null;
222 Enumeration keys = map.keys();
223 Vector unmatched = new Vector();
224 for (int i = 0, j = sequences.length; i < j; i++)
226 unmatched.addElement(sequences[i]);
228 while (keys.hasMoreElements())
230 Object key = keys.nextElement();
231 if (key instanceof String)
233 if ((msq = matcher.findIdMatch((String) key)) != null)
235 Hashtable sqinfo = (Hashtable) map.get(key);
236 unmatched.removeElement(msq);
237 SeqCharacterUnhash(msq, sqinfo);
243 System.err.println("Can't find '" + ((String) key)
244 + "' in uniquified alignment");
249 if (unmatched.size() > 0 && !quiet)
251 System.err.println("Did not find matches for :");
252 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out
253 .println(((SequenceI) i.nextElement()).getName()))
264 * returns a subset of the sequenceI seuqences, including only those that
265 * contain at least one residue.
269 * @return SequenceI[]
271 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
273 // Identify first row of alignment with residues for prediction
274 boolean ungapped[] = new boolean[sequences.length];
276 for (int i = 0, j = sequences.length; i < j; i++)
278 String tempseq = AlignSeq.extractGaps(Comparison.GapChars,
279 sequences[i].getSequenceAsString());
281 if (tempseq.length() == 0)
293 return null; // no minimal set
295 // compose minimal set
296 SequenceI[] mset = new SequenceI[msflen];
297 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
301 mset[k++] = sequences[i];