2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
28 * <p>Description: </p>
30 * <p>Copyright: Copyright (c) 2004</p>
32 * <p>Company: Dundee University</p>
34 * @author not attributable
37 public class SeqsetUtils
41 * Store essential properties of a sequence in a hashtable for later recovery
42 * Keys are Name, Start, End, SeqFeatures, PdbId
43 * @param seq SequenceI
46 public static Hashtable SeqCharacterHash(SequenceI seq)
48 Hashtable sqinfo = new Hashtable();
49 sqinfo.put("Name", seq.getName());
50 sqinfo.put("Start", new Integer(seq.getStart()));
51 sqinfo.put("End", new Integer(seq.getEnd()));
52 if (seq.getDescription() != null)
54 sqinfo.put("Description", seq.getDescription());
56 Vector sfeat = new Vector();
57 jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
58 if (sfarray != null && sfarray.length > 0)
60 for (int i = 0; i < sfarray.length; i++)
62 sfeat.addElement(sfarray[i]);
65 sqinfo.put("SeqFeatures", sfeat);
67 (seq.getPDBId() != null) ? seq.getPDBId() : new Vector());
68 sqinfo.put("datasetSequence",
69 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence() :
70 new Sequence("THISISAPLACEHOLDER", ""));
75 * Recover essential properties of a sequence from a hashtable
76 * TODO: replace these methods with something more elegant.
78 * @param sqinfo Hashtable
79 * @return boolean true if name was not updated from sqinfo Name entry
81 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
83 boolean namePresent = true;
88 String oldname = (String) sqinfo.get("Name");
89 Integer start = (Integer) sqinfo.get("Start");
90 Integer end = (Integer) sqinfo.get("End");
91 Vector sfeatures = (Vector) sqinfo.get(
93 Vector pdbid = (Vector) 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);
128 if ( (seqds != null) &&
129 ! (seqds.getName().equals("THISISAPLACEHOLDER") &&
130 seqds.getLength() == 0))
132 sq.setDatasetSequence(seqds);
139 * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.
143 public static String unique_name(int i)
145 return new String("Sequence" + i);
149 * Generates a hash of SeqCharacterHash properties for each sequence
150 * in a sequence set, and optionally renames the sequences to an
151 * unambiguous 'safe' name.
152 * @param sequences SequenceI[]
153 * @param write_names boolean set this to rename each of the sequences to its unique_name(index) name
154 * @return Hashtable to be passed to @see deuniquify to recover original names (and properties) for renamed sequences
156 public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)
158 // Generate a safely named sequence set and a hash to recover the sequence names
159 Hashtable map = new Hashtable();
160 //String[] un_names = new String[sequences.length];
162 for (int i = 0; i < sequences.length; i++)
164 String safename = unique_name(i);
165 map.put(safename, SeqCharacterHash(sequences[i]));
169 sequences[i].setName(safename);
177 * recover unsafe sequence names and original properties for a sequence
178 * set using a map generated by @see uniquify(sequences,true)
179 * @param map Hashtable
180 * @param sequences SequenceI[]
183 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
185 return deuniquify(map, sequences, true);
188 * recover unsafe sequence names and original properties for a sequence
189 * set using a map generated by @see uniquify(sequences,true)
190 * @param map Hashtable
191 * @param sequences SequenceI[]
192 * @param quiet when false, don't complain about sequences without any data in the map.
195 public static boolean deuniquify(Hashtable map, SequenceI[] sequences, boolean quiet)
197 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
199 SequenceI msq = null;
200 Enumeration keys = map.keys();
201 Vector unmatched = new Vector();
202 for (int i = 0, j = sequences.length; i < j; i++)
204 unmatched.addElement(sequences[i]);
206 while (keys.hasMoreElements())
208 Object key = keys.nextElement();
209 if (key instanceof String)
211 if ( (msq = matcher.findIdMatch( (String) key)) != null)
213 Hashtable sqinfo = (Hashtable) map.get(key);
214 unmatched.removeElement(msq);
215 SeqCharacterUnhash(msq, sqinfo);
221 System.err.println("Can't find '" + ( (String) key) +
222 "' in uniquified alignment");
227 if (unmatched.size() > 0 && !quiet)
229 System.err.println("Did not find matches for :");
230 for (Enumeration i = unmatched.elements(); i.hasMoreElements();
231 System.out.println( ( (SequenceI) i.nextElement()).getName()))
242 * returns a subset of the sequenceI seuqences,
243 * including only those that contain at least one residue.
244 * @param sequences SequenceI[]
245 * @return SequenceI[]
247 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
249 // Identify first row of alignment with residues for prediction
250 boolean ungapped[] = new boolean[sequences.length];
252 for (int i = 0, j = sequences.length; i < j; i++)
254 String tempseq = jalview.analysis.AlignSeq.extractGaps(
255 jalview.util.Comparison.GapChars,
256 sequences[i].getSequenceAsString());
258 if (tempseq.length() == 0)
270 return null; // no minimal set
272 // compose minimal set
273 SequenceI[] mset = new SequenceI[msflen];
274 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
278 mset[k++] = sequences[i];