2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.analysis;
\r
23 import jalview.datamodel.*;
\r
28 * <p>Description: </p>
\r
30 * <p>Copyright: Copyright (c) 2004</p>
\r
32 * <p>Company: Dundee University</p>
\r
34 * @author not attributable
\r
37 public class SeqsetUtils
\r
41 * Store essential properties of a sequence in a hashtable for later recovery
\r
42 * Keys are Name, Start, End, SeqFeatures, PdbId
\r
43 * @param seq SequenceI
\r
46 public static Hashtable SeqCharacterHash(SequenceI seq)
\r
48 Hashtable sqinfo = new Hashtable();
\r
49 sqinfo.put("Name", seq.getName());
\r
50 sqinfo.put("Start", new Integer(seq.getStart()));
\r
51 sqinfo.put("End", new Integer(seq.getEnd()));
\r
52 Vector sfeat = new Vector();
\r
53 jalview.datamodel.SequenceFeature[] sfarray=seq.getSequenceFeatures();
\r
54 if (sfarray!=null && sfarray.length>0) {
\r
55 for (int i=0;i<sfarray.length;i++)
\r
56 sfeat.add(sfarray[i]);
\r
58 sqinfo.put("SeqFeatures", sfeat);
\r
60 (seq.getPDBId() != null) ? seq.getPDBId() : new Vector());
\r
61 sqinfo.put("datasetSequence", (seq.getDatasetSequence() !=null) ? seq.getDatasetSequence() : new Sequence("THISISAPLACEHOLDER",""));
\r
66 * Recover essential properties of a sequence from a hashtable
\r
67 * TODO: replace these methods with something more elegant.
\r
68 * @param sq SequenceI
\r
69 * @param sqinfo Hashtable
\r
70 * @return boolean true if name was not updated from sqinfo Name entry
\r
72 public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
\r
74 boolean namePresent = true;
\r
77 String oldname = (String) sqinfo.get("Name");
\r
78 Integer start = (Integer) sqinfo.get("Start");
\r
79 Integer end = (Integer) sqinfo.get("End");
\r
80 Vector sfeatures = (Vector) sqinfo.get(
\r
82 Vector pdbid = (Vector) sqinfo.get("PdbId");
\r
83 Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
\r
84 if (oldname == null)
\r
86 namePresent = false;
\r
90 sq.setName(oldname);
\r
92 if (pdbid!=null && pdbid.size()>0)
\r
97 if ( (start != null) && (end != null))
\r
99 sq.setStart(start.intValue());
\r
100 sq.setEnd(end.intValue());
\r
103 if ((sfeatures != null) && (sfeatures.size()>0))
\r
105 SequenceFeature[] sfarray = (SequenceFeature[]) sfeatures.toArray();
\r
106 sq.setSequenceFeatures(sfarray);
\r
109 if ((seqds!=null) && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds.getLength()==0)) {
\r
110 sq.setDatasetSequence(seqds);
\r
113 return namePresent;
\r
117 * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.
\r
121 public static String unique_name(int i)
\r
123 return new String("Sequence" + i);
\r
127 * Generates a hash of SeqCharacterHash properties for each sequence
\r
128 * in a sequence set, and optionally renames the sequences to an
\r
129 * unambiguous 'safe' name.
\r
130 * @param sequences SequenceI[]
\r
131 * @param write_names boolean set this to rename each of the sequences to its unique_name(index) name
\r
132 * @return Hashtable to be passed to @see deuniquify to recover original names (and properties) for renamed sequences
\r
134 public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)
\r
136 // Generate a safely named sequence set and a hash to recover the sequence names
\r
137 Hashtable map = new Hashtable();
\r
138 //String[] un_names = new String[sequences.length];
\r
140 for (int i = 0; i < sequences.length; i++)
\r
142 String safename = unique_name(i);
\r
143 map.put(safename, SeqCharacterHash(sequences[i]));
\r
147 sequences[i].setName(safename);
\r
155 * recover unsafe sequence names and original properties for a sequence
\r
156 * set using a map generated by @see uniquify(sequences,true)
\r
157 * @param map Hashtable
\r
158 * @param sequences SequenceI[]
\r
161 public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
\r
163 jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(sequences);
\r
164 SequenceI msq = null;
\r
165 Enumeration keys = map.keys();
\r
166 Vector unmatched = new Vector();
\r
167 for (int i=0, j=sequences.length; i<j; i++)
\r
168 unmatched.add(sequences[i]);
\r
169 while (keys.hasMoreElements()) {
\r
170 Object key = keys.nextElement();
\r
171 if (key instanceof String) {
\r
172 if ((msq = matcher.findIdMatch((String) key))!=null) {
\r
173 Hashtable sqinfo = (Hashtable) map.get(key);
\r
174 unmatched.remove(msq);
\r
175 SeqCharacterUnhash(msq, sqinfo);
\r
179 System.err.println("Can't find '"+((String) key)+"' in uniquified alignment");
\r
183 if (unmatched.size()>0) {
\r
184 System.err.println("Did not find matches for :");
\r
185 for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out.println(((SequenceI) i.nextElement()).getName()))
\r
193 * returns a subset of the sequenceI seuqences,
\r
194 * including only those that contain at least one residue.
\r
195 * @param sequences SequenceI[]
\r
196 * @return SequenceI[]
\r
198 public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences) {
\r
199 // Identify first row of alignment with residues for prediction
\r
200 boolean ungapped[] = new boolean[sequences.length];
\r
202 for (int i=0,j=sequences.length; i<j;i++) {
\r
203 String tempseq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sequences[i].getSequence());
\r
204 if (tempseq.length()==0)
\r
212 return null; // no minimal set
\r
213 // compose minimal set
\r
214 SequenceI[] mset = new SequenceI[msflen];
\r
215 for (int i=0,j=sequences.length,k=0; i<j;i++) {
\r
217 mset[k++] = sequences[i];
\r