2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
24 public class SeqsetUtils
28 * Store essential properties of a sequence in a hashtable for later recovery
29 * Keys are Name, Start, End, SeqFeatures, PdbId
35 public static Hashtable SeqCharacterHash(SequenceI seq)
37 Hashtable sqinfo = new Hashtable();
38 sqinfo.put("Name", seq.getName());
39 sqinfo.put("Start", new Integer(seq.getStart()));
40 sqinfo.put("End", new Integer(seq.getEnd()));
41 if (seq.getDescription() != null)
43 sqinfo.put("Description", seq.getDescription());
45 Vector sfeat = new Vector();
46 jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
47 if (sfarray != null && sfarray.length > 0)
49 for (int i = 0; i < sfarray.length; i++)
51 sfeat.addElement(sfarray[i]);
54 sqinfo.put("SeqFeatures", sfeat);
55 sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId()
57 sqinfo.put("datasetSequence",
58 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
59 : new Sequence("THISISAPLACEHOLDER", ""));
64 * Recover essential properties of a sequence from a hashtable TODO: replace
65 * these methods with something more elegant.
71 * @return boolean true if name was not updated from sqinfo Name entry
73 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
75 boolean namePresent = true;
80 String oldname = (String) sqinfo.get("Name");
81 Integer start = (Integer) sqinfo.get("Start");
82 Integer end = (Integer) sqinfo.get("End");
83 Vector sfeatures = (Vector) sqinfo.get("SeqFeatures");
84 Vector pdbid = (Vector) sqinfo.get("PdbId");
85 String description = (String) sqinfo.get("Description");
86 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
95 if (pdbid != null && pdbid.size() > 0)
100 if ((start != null) && (end != null))
102 sq.setStart(start.intValue());
103 sq.setEnd(end.intValue());
106 if ((sfeatures != null) && (sfeatures.size() > 0))
108 SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
109 for (int is = 0, isize = sfeatures.size(); is < isize; is++)
111 sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
113 sq.setSequenceFeatures(sfarray);
115 if (description != null)
117 sq.setDescription(description);
120 && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds
123 sq.setDatasetSequence(seqds);
130 * Form of the unique name used in uniquify for the i'th sequence in an
131 * ordered vector of sequences.
137 public static String unique_name(int i)
139 return new String("Sequence" + i);
143 * Generates a hash of SeqCharacterHash properties for each sequence in a
144 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
150 * boolean set this to rename each of the sequences to its
151 * unique_name(index) name
152 * @return Hashtable to be passed to
153 * @see deuniquify to recover original names (and properties) for renamed
156 public static Hashtable uniquify(SequenceI[] sequences,
159 // Generate a safely named sequence set and a hash to recover the sequence
161 Hashtable map = new Hashtable();
162 // String[] un_names = new String[sequences.length];
164 for (int i = 0; i < sequences.length; i++)
166 String safename = unique_name(i);
167 map.put(safename, SeqCharacterHash(sequences[i]));
171 sequences[i].setName(safename);
179 * recover unsafe sequence names and original properties for a sequence set
180 * using a map generated by
182 * @see uniquify(sequences,true)
189 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
191 return deuniquify(map, sequences, true);
195 * recover unsafe sequence names and original properties for a sequence set
196 * using a map generated by
198 * @see uniquify(sequences,true)
204 * when false, don't complain about sequences without any data in the
208 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
211 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
213 SequenceI msq = null;
214 Enumeration keys = map.keys();
215 Vector unmatched = new Vector();
216 for (int i = 0, j = sequences.length; i < j; i++)
218 unmatched.addElement(sequences[i]);
220 while (keys.hasMoreElements())
222 Object key = keys.nextElement();
223 if (key instanceof String)
225 if ((msq = matcher.findIdMatch((String) key)) != null)
227 Hashtable sqinfo = (Hashtable) map.get(key);
228 unmatched.removeElement(msq);
229 SeqCharacterUnhash(msq, sqinfo);
235 System.err.println("Can't find '" + ((String) key)
236 + "' in uniquified alignment");
241 if (unmatched.size() > 0 && !quiet)
243 System.err.println("Did not find matches for :");
244 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out
245 .println(((SequenceI) i.nextElement()).getName()))
256 * returns a subset of the sequenceI seuqences, including only those that
257 * contain at least one residue.
261 * @return SequenceI[]
263 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
265 // Identify first row of alignment with residues for prediction
266 boolean ungapped[] = new boolean[sequences.length];
268 for (int i = 0, j = sequences.length; i < j; i++)
270 String tempseq = jalview.analysis.AlignSeq.extractGaps(
271 jalview.util.Comparison.GapChars,
272 sequences[i].getSequenceAsString());
274 if (tempseq.length() == 0)
286 return null; // no minimal set
288 // compose minimal set
289 SequenceI[] mset = new SequenceI[msflen];
290 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
294 mset[k++] = sequences[i];