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;
28 import java.util.Enumeration;
29 import java.util.Hashtable;
30 import java.util.List;
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", Integer.valueOf(seq.getStart()));
49 sqinfo.put("End", Integer.valueOf(seq.getEnd()));
50 if (seq.getDescription() != null)
52 sqinfo.put("Description", seq.getDescription());
55 if (seq.getDatasetSequence() == null)
57 Vector<SequenceFeature> sfeat = new Vector<>();
58 List<SequenceFeature> sfs = seq.getFeatures().getAllFeatures();
60 sqinfo.put("SeqFeatures", sfeat);
62 (seq.getAllPDBEntries() != null) ? seq.getAllPDBEntries()
63 : new Vector<PDBEntry>());
67 sqinfo.put("datasetSequence",
68 (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
69 : new Sequence("THISISAPLACEHOLDER", ""));
75 * Recover essential properties of a sequence from a hashtable TODO: replace
76 * these methods with something more elegant.
82 * @return boolean true if name was not updated from sqinfo Name entry
84 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
86 boolean namePresent = true;
91 String oldname = (String) sqinfo.get("Name");
92 Integer start = (Integer) sqinfo.get("Start");
93 Integer end = (Integer) sqinfo.get("End");
94 Vector<SequenceFeature> sfeatures = (Vector<SequenceFeature>) sqinfo
96 Vector<PDBEntry> pdbid = (Vector<PDBEntry>) sqinfo.get("PdbId");
97 String description = (String) sqinfo.get("Description");
98 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
107 if (pdbid != null && pdbid.size() > 0)
112 if ((start != null) && (end != null))
114 sq.setStart(start.intValue());
115 sq.setEnd(end.intValue());
118 if (sfeatures != null && !sfeatures.isEmpty())
120 sq.setSequenceFeatures(sfeatures);
122 if (description != null)
124 sq.setDescription(description);
126 if ((seqds != null) && !(seqds.getName().equals("THISISAPLACEHOLDER")
127 && seqds.getLength() == 0))
129 if (sfeatures != null)
132 "Implementation error: setting dataset sequence for a sequence which has sequence features.\n\tDataset sequence features will not be visible.");
134 sq.setDatasetSequence(seqds);
141 * Form of the unique name used in uniquify for the i'th sequence in an
142 * ordered vector of sequences.
148 public static String unique_name(int i)
150 return new String("Sequence" + i);
154 * Generates a hash of SeqCharacterHash properties for each sequence in a
155 * sequence set, and optionally renames the sequences to an unambiguous 'safe'
161 * boolean set this to rename each of the sequences to its
162 * unique_name(index) name
163 * @return Hashtable to be passed to
164 * @see deuniquify to recover original names (and properties) for renamed
167 public static Hashtable uniquify(SequenceI[] sequences,
170 // Generate a safely named sequence set and a hash to recover the sequence
172 Hashtable map = new Hashtable();
173 // String[] un_names = new String[sequences.length];
175 for (int i = 0; i < sequences.length; i++)
177 String safename = unique_name(i);
178 map.put(safename, SeqCharacterHash(sequences[i]));
182 sequences[i].setName(safename);
190 * recover unsafe sequence names and original properties for a sequence set
191 * using a map generated by
193 * @see uniquify(sequences,true)
200 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
202 return deuniquify(map, sequences, true);
206 * recover unsafe sequence names and original properties for a sequence set
207 * using a map generated by
209 * @see uniquify(sequences,true)
215 * when false, don't complain about sequences without any data in the
219 public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
222 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
224 SequenceI msq = null;
225 Enumeration keys = map.keys();
226 Vector unmatched = new Vector();
227 for (int i = 0, j = sequences.length; i < j; i++)
229 unmatched.addElement(sequences[i]);
231 while (keys.hasMoreElements())
233 Object key = keys.nextElement();
234 if (key instanceof String)
236 if ((msq = matcher.findIdMatch((String) key)) != null)
238 Hashtable sqinfo = (Hashtable) map.get(key);
239 unmatched.removeElement(msq);
240 SeqCharacterUnhash(msq, sqinfo);
246 System.err.println("Can't find '" + ((String) key)
247 + "' in uniquified alignment");
252 if (unmatched.size() > 0 && !quiet)
254 System.err.println("Did not find matches for :");
255 for (Enumeration i = unmatched.elements(); i
256 .hasMoreElements(); System.out
257 .println(((SequenceI) i.nextElement()).getName()))
268 * returns a subset of the sequenceI seuqences, including only those that
269 * contain at least one residue.
273 * @return SequenceI[]
275 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
277 // Identify first row of alignment with residues for prediction
278 boolean ungapped[] = new boolean[sequences.length];
280 for (int i = 0, j = sequences.length; i < j; i++)
282 String tempseq = jalview.analysis.AlignSeq.extractGaps(
283 jalview.util.Comparison.GapChars,
284 sequences[i].getSequenceAsString());
286 if (tempseq.length() == 0)
298 return null; // no minimal set
300 // compose minimal set
301 SequenceI[] mset = new SequenceI[msflen];
302 for (int i = 0, j = sequences.length, k = 0; i < j; i++)
306 mset[k++] = sequences[i];