2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
23 import jalview.datamodel.*;
25 public class SeqsetUtils
29 * Store essential properties of a sequence in a hashtable for later recovery
30 * Keys are Name, Start, End, SeqFeatures, PdbId
36 public static Hashtable SeqCharacterHash(SequenceI seq)
38 Hashtable sqinfo = new Hashtable();
39 sqinfo.put("Name", seq.getName());
40 sqinfo.put("Start", new Integer(seq.getStart()));
41 sqinfo.put("End", new Integer(seq.getEnd()));
42 if (seq.getDescription() != null)
44 sqinfo.put("Description", seq.getDescription());
46 Vector sfeat = new Vector();
47 jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
48 if (sfarray != null && sfarray.length > 0)
50 for (int i = 0; i < sfarray.length; i++)
52 sfeat.addElement(sfarray[i]);
55 sqinfo.put("SeqFeatures", sfeat);
56 sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId()
58 sqinfo.put("datasetSequence",
59 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
60 : new Sequence("THISISAPLACEHOLDER", ""));
65 * Recover essential properties of a sequence from a hashtable TODO: replace
66 * these methods with something more elegant.
72 * @return boolean true if name was not updated from sqinfo Name entry
74 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
76 boolean namePresent = true;
81 String oldname = (String) sqinfo.get("Name");
82 Integer start = (Integer) sqinfo.get("Start");
83 Integer end = (Integer) sqinfo.get("End");
84 Vector sfeatures = (Vector) sqinfo.get("SeqFeatures");
85 Vector pdbid = (Vector) sqinfo.get("PdbId");
86 String description = (String) sqinfo.get("Description");
87 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
96 if (pdbid != null && pdbid.size() > 0)
101 if ((start != null) && (end != null))
103 sq.setStart(start.intValue());
104 sq.setEnd(end.intValue());
107 if ((sfeatures != null) && (sfeatures.size() > 0))
109 SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
110 for (int is = 0, isize = sfeatures.size(); is < isize; is++)
112 sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
114 sq.setSequenceFeatures(sfarray);
116 if (description != null)
118 sq.setDescription(description);
121 && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds
124 sq.setDatasetSequence(seqds);
131 * Form of the unique name used in uniquify for the i'th sequence in an
132 * ordered vector of sequences.
138 public static String unique_name(int i)
140 return new String("Sequence" + i);
144 * Generates a hash of SeqCharacterHash properties for each sequence in a
145 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
151 * boolean set this to rename each of the sequences to its
152 * unique_name(index) name
153 * @return Hashtable to be passed to
154 * @see deuniquify to recover original names (and properties) for renamed
157 public static Hashtable uniquify(SequenceI[] sequences,
160 // Generate a safely named sequence set and a hash to recover the sequence
162 Hashtable map = new Hashtable();
163 // String[] un_names = new String[sequences.length];
165 for (int i = 0; i < sequences.length; i++)
167 String safename = unique_name(i);
168 map.put(safename, SeqCharacterHash(sequences[i]));
172 sequences[i].setName(safename);
180 * recover unsafe sequence names and original properties for a sequence set
181 * using a map generated by
183 * @see uniquify(sequences,true)
190 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
192 return deuniquify(map, sequences, true);
196 * recover unsafe sequence names and original properties for a sequence set
197 * using a map generated by
199 * @see uniquify(sequences,true)
205 * when false, don't complain about sequences without any data in the
209 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
212 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
214 SequenceI msq = null;
215 Enumeration keys = map.keys();
216 Vector unmatched = new Vector();
217 for (int i = 0, j = sequences.length; i < j; i++)
219 unmatched.addElement(sequences[i]);
221 while (keys.hasMoreElements())
223 Object key = keys.nextElement();
224 if (key instanceof String)
226 if ((msq = matcher.findIdMatch((String) key)) != null)
228 Hashtable sqinfo = (Hashtable) map.get(key);
229 unmatched.removeElement(msq);
230 SeqCharacterUnhash(msq, sqinfo);
236 System.err.println("Can't find '" + ((String) key)
237 + "' in uniquified alignment");
242 if (unmatched.size() > 0 && !quiet)
244 System.err.println("Did not find matches for :");
245 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out
246 .println(((SequenceI) i.nextElement()).getName()))
257 * returns a subset of the sequenceI seuqences, including only those that
258 * contain at least one residue.
262 * @return SequenceI[]
264 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
266 // Identify first row of alignment with residues for prediction
267 boolean ungapped[] = new boolean[sequences.length];
269 for (int i = 0, j = sequences.length; i < j; i++)
271 String tempseq = jalview.analysis.AlignSeq.extractGaps(
272 jalview.util.Comparison.GapChars,
273 sequences[i].getSequenceAsString());
275 if (tempseq.length() == 0)
287 return null; // no minimal set
289 // compose minimal set
290 SequenceI[] mset = new SequenceI[msflen];
291 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
295 mset[k++] = sequences[i];