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