2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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 Vector sfeat = new Vector();
53 jalview.datamodel.SequenceFeature[] sfarray=seq.getSequenceFeatures();
54 if (sfarray!=null && sfarray.length>0) {
55 for (int i=0;i<sfarray.length;i++)
56 sfeat.add(sfarray[i]);
58 sqinfo.put("SeqFeatures", sfeat);
60 (seq.getPDBId() != null) ? seq.getPDBId() : new Vector());
61 sqinfo.put("datasetSequence", (seq.getDatasetSequence() !=null) ? seq.getDatasetSequence() : new Sequence("THISISAPLACEHOLDER",""));
66 * Recover essential properties of a sequence from a hashtable
67 * TODO: replace these methods with something more elegant.
69 * @param sqinfo Hashtable
70 * @return boolean true if name was not updated from sqinfo Name entry
72 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
74 boolean namePresent = true;
77 String oldname = (String) sqinfo.get("Name");
78 Integer start = (Integer) sqinfo.get("Start");
79 Integer end = (Integer) sqinfo.get("End");
80 Vector sfeatures = (Vector) sqinfo.get(
82 Vector pdbid = (Vector) sqinfo.get("PdbId");
83 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
92 if (pdbid!=null && pdbid.size()>0)
97 if ( (start != null) && (end != null))
99 sq.setStart(start.intValue());
100 sq.setEnd(end.intValue());
103 if ((sfeatures != null) && (sfeatures.size()>0))
105 SequenceFeature[] sfarray = (SequenceFeature[]) sfeatures.toArray();
106 sq.setSequenceFeatures(sfarray);
109 if ((seqds!=null) && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds.getLength()==0)) {
110 sq.setDatasetSequence(seqds);
117 * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.
121 public static String unique_name(int i)
123 return new String("Sequence" + i);
127 * Generates a hash of SeqCharacterHash properties for each sequence
128 * in a sequence set, and optionally renames the sequences to an
129 * unambiguous 'safe' name.
130 * @param sequences SequenceI[]
131 * @param write_names boolean set this to rename each of the sequences to its unique_name(index) name
132 * @return Hashtable to be passed to @see deuniquify to recover original names (and properties) for renamed sequences
134 public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)
136 // Generate a safely named sequence set and a hash to recover the sequence names
137 Hashtable map = new Hashtable();
138 //String[] un_names = new String[sequences.length];
140 for (int i = 0; i < sequences.length; i++)
142 String safename = unique_name(i);
143 map.put(safename, SeqCharacterHash(sequences[i]));
147 sequences[i].setName(safename);
155 * recover unsafe sequence names and original properties for a sequence
156 * set using a map generated by @see uniquify(sequences,true)
157 * @param map Hashtable
158 * @param sequences SequenceI[]
161 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
163 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(sequences);
164 SequenceI msq = null;
165 Enumeration keys = map.keys();
166 Vector unmatched = new Vector();
167 for (int i=0, j=sequences.length; i<j; i++)
168 unmatched.add(sequences[i]);
169 while (keys.hasMoreElements()) {
170 Object key = keys.nextElement();
171 if (key instanceof String) {
172 if ((msq = matcher.findIdMatch((String) key))!=null) {
173 Hashtable sqinfo = (Hashtable) map.get(key);
174 unmatched.remove(msq);
175 SeqCharacterUnhash(msq, sqinfo);
179 System.err.println("Can't find '"+((String) key)+"' in uniquified alignment");
183 if (unmatched.size()>0) {
184 System.err.println("Did not find matches for :");
185 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out.println(((SequenceI) i.nextElement()).getName()))
193 * returns a subset of the sequenceI seuqences,
194 * including only those that contain at least one residue.
195 * @param sequences SequenceI[]
196 * @return SequenceI[]
198 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences) {
199 // Identify first row of alignment with residues for prediction
200 boolean ungapped[] = new boolean[sequences.length];
202 for (int i=0,j=sequences.length; i<j;i++) {
203 String tempseq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sequences[i].getSequence());
204 if (tempseq.length()==0)
212 return null; // no minimal set
213 // compose minimal set
214 SequenceI[] mset = new SequenceI[msflen];
215 for (int i=0,j=sequences.length,k=0; i<j;i++) {
217 mset[k++] = sequences[i];