JAL-1620 version bump and release notes
[jalview.git] / src / jalview / analysis / SeqsetUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.analysis;
22
23 import java.util.*;
24
25 import jalview.datamodel.*;
26
27 public class SeqsetUtils
28 {
29
30   /**
31    * Store essential properties of a sequence in a hashtable for later recovery
32    * Keys are Name, Start, End, SeqFeatures, PdbId
33    * 
34    * @param seq
35    *          SequenceI
36    * @return Hashtable
37    */
38   public static Hashtable SeqCharacterHash(SequenceI seq)
39   {
40     Hashtable sqinfo = new Hashtable();
41     sqinfo.put("Name", seq.getName());
42     sqinfo.put("Start", new Integer(seq.getStart()));
43     sqinfo.put("End", new Integer(seq.getEnd()));
44     if (seq.getDescription() != null)
45     {
46       sqinfo.put("Description", seq.getDescription());
47     }
48     Vector sfeat = new Vector();
49     jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures();
50     if (sfarray != null && sfarray.length > 0)
51     {
52       for (int i = 0; i < sfarray.length; i++)
53       {
54         sfeat.addElement(sfarray[i]);
55       }
56     }
57     sqinfo.put("SeqFeatures", sfeat);
58     sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId()
59             : new Vector());
60     sqinfo.put("datasetSequence",
61             (seq.getDatasetSequence() != null) ? seq.getDatasetSequence()
62                     : new Sequence("THISISAPLACEHOLDER", ""));
63     return sqinfo;
64   }
65
66   /**
67    * Recover essential properties of a sequence from a hashtable TODO: replace
68    * these methods with something more elegant.
69    * 
70    * @param sq
71    *          SequenceI
72    * @param sqinfo
73    *          Hashtable
74    * @return boolean true if name was not updated from sqinfo Name entry
75    */
76   public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
77   {
78     boolean namePresent = true;
79     if (sqinfo == null)
80     {
81       return false;
82     }
83     String oldname = (String) sqinfo.get("Name");
84     Integer start = (Integer) sqinfo.get("Start");
85     Integer end = (Integer) sqinfo.get("End");
86     Vector sfeatures = (Vector) sqinfo.get("SeqFeatures");
87     Vector pdbid = (Vector) sqinfo.get("PdbId");
88     String description = (String) sqinfo.get("Description");
89     Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
90     if (oldname == null)
91     {
92       namePresent = false;
93     }
94     else
95     {
96       sq.setName(oldname);
97     }
98     if (pdbid != null && pdbid.size() > 0)
99     {
100       sq.setPDBId(pdbid);
101     }
102
103     if ((start != null) && (end != null))
104     {
105       sq.setStart(start.intValue());
106       sq.setEnd(end.intValue());
107     }
108
109     if ((sfeatures != null) && (sfeatures.size() > 0))
110     {
111       SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()];
112       for (int is = 0, isize = sfeatures.size(); is < isize; is++)
113       {
114         sfarray[is] = (SequenceFeature) sfeatures.elementAt(is);
115       }
116       sq.setSequenceFeatures(sfarray);
117     }
118     if (description != null)
119     {
120       sq.setDescription(description);
121     }
122     if ((seqds != null)
123             && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds
124                     .getLength() == 0))
125     {
126       sq.setDatasetSequence(seqds);
127     }
128
129     return namePresent;
130   }
131
132   /**
133    * Form of the unique name used in uniquify for the i'th sequence in an
134    * ordered vector of sequences.
135    * 
136    * @param i
137    *          int
138    * @return String
139    */
140   public static String unique_name(int i)
141   {
142     return new String("Sequence" + i);
143   }
144
145   /**
146    * Generates a hash of SeqCharacterHash properties for each sequence in a
147    * sequence set, and optionally renames the sequences to an unambiguous 'safe'
148    * name.
149    * 
150    * @param sequences
151    *          SequenceI[]
152    * @param write_names
153    *          boolean set this to rename each of the sequences to its
154    *          unique_name(index) name
155    * @return Hashtable to be passed to
156    * @see deuniquify to recover original names (and properties) for renamed
157    *      sequences
158    */
159   public static Hashtable uniquify(SequenceI[] sequences,
160           boolean write_names)
161   {
162     // Generate a safely named sequence set and a hash to recover the sequence
163     // names
164     Hashtable map = new Hashtable();
165     // String[] un_names = new String[sequences.length];
166
167     for (int i = 0; i < sequences.length; i++)
168     {
169       String safename = unique_name(i);
170       map.put(safename, SeqCharacterHash(sequences[i]));
171
172       if (write_names)
173       {
174         sequences[i].setName(safename);
175       }
176     }
177
178     return map;
179   }
180
181   /**
182    * recover unsafe sequence names and original properties for a sequence set
183    * using a map generated by
184    * 
185    * @see uniquify(sequences,true)
186    * @param map
187    *          Hashtable
188    * @param sequences
189    *          SequenceI[]
190    * @return boolean
191    */
192   public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
193   {
194     return deuniquify(map, sequences, true);
195   }
196
197   /**
198    * recover unsafe sequence names and original properties for a sequence set
199    * using a map generated by
200    * 
201    * @see uniquify(sequences,true)
202    * @param map
203    *          Hashtable
204    * @param sequences
205    *          SequenceI[]
206    * @param quiet
207    *          when false, don't complain about sequences without any data in the
208    *          map.
209    * @return boolean
210    */
211   public static boolean deuniquify(Hashtable map, SequenceI[] sequences,
212           boolean quiet)
213   {
214     jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(
215             sequences);
216     SequenceI msq = null;
217     Enumeration keys = map.keys();
218     Vector unmatched = new Vector();
219     for (int i = 0, j = sequences.length; i < j; i++)
220     {
221       unmatched.addElement(sequences[i]);
222     }
223     while (keys.hasMoreElements())
224     {
225       Object key = keys.nextElement();
226       if (key instanceof String)
227       {
228         if ((msq = matcher.findIdMatch((String) key)) != null)
229         {
230           Hashtable sqinfo = (Hashtable) map.get(key);
231           unmatched.removeElement(msq);
232           SeqCharacterUnhash(msq, sqinfo);
233         }
234         else
235         {
236           if (!quiet)
237           {
238             System.err.println("Can't find '" + ((String) key)
239                     + "' in uniquified alignment");
240           }
241         }
242       }
243     }
244     if (unmatched.size() > 0 && !quiet)
245     {
246       System.err.println("Did not find matches for :");
247       for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out
248               .println(((SequenceI) i.nextElement()).getName()))
249       {
250         ;
251       }
252       return false;
253     }
254
255     return true;
256   }
257
258   /**
259    * returns a subset of the sequenceI seuqences, including only those that
260    * contain at least one residue.
261    * 
262    * @param sequences
263    *          SequenceI[]
264    * @return SequenceI[]
265    */
266   public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences)
267   {
268     // Identify first row of alignment with residues for prediction
269     boolean ungapped[] = new boolean[sequences.length];
270     int msflen = 0;
271     for (int i = 0, j = sequences.length; i < j; i++)
272     {
273       String tempseq = jalview.analysis.AlignSeq.extractGaps(
274               jalview.util.Comparison.GapChars,
275               sequences[i].getSequenceAsString());
276
277       if (tempseq.length() == 0)
278       {
279         ungapped[i] = false;
280       }
281       else
282       {
283         ungapped[i] = true;
284         msflen++;
285       }
286     }
287     if (msflen == 0)
288     {
289       return null; // no minimal set
290     }
291     // compose minimal set
292     SequenceI[] mset = new SequenceI[msflen];
293     for (int i = 0, j = sequences.length, k = 0; i < j; i++)
294     {
295       if (ungapped[i])
296       {
297         mset[k++] = sequences[i];
298       }
299     }
300     ungapped = null;
301     return mset;
302   }
303 }