updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / analysis / SeqsetUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.analysis;
20
21 import java.util.*;
22
23 import jalview.datamodel.*;
24
25 /**
26  * <p>Title: </p>
27  *
28  * <p>Description: </p>
29  *
30  * <p>Copyright: Copyright (c) 2004</p>
31  *
32  * <p>Company: Dundee University</p>
33  *
34  * @author not attributable
35  * @version 1.0
36  */
37 public class SeqsetUtils
38 {
39
40   /**
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
44    * @return Hashtable
45    */
46   public static Hashtable SeqCharacterHash(SequenceI seq)
47   {
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]);
57     }
58     sqinfo.put("SeqFeatures", sfeat);
59     sqinfo.put("PdbId",
60                (seq.getPDBId() != null) ? seq.getPDBId() : new Vector());
61     sqinfo.put("datasetSequence", (seq.getDatasetSequence() !=null) ? seq.getDatasetSequence() : new Sequence("THISISAPLACEHOLDER",""));
62     return sqinfo;
63   }
64
65   /**
66    * Recover essential properties of a sequence from a hashtable
67    * TODO: replace these methods with something more elegant.
68    * @param sq SequenceI
69    * @param sqinfo Hashtable
70    * @return boolean true if name was not updated from sqinfo Name entry
71    */
72   public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)
73   {
74     boolean namePresent = true;
75     if (sqinfo==null)
76       return false;
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(
81         "SeqFeatures");
82     Vector pdbid = (Vector) sqinfo.get("PdbId");
83     Sequence seqds = (Sequence) sqinfo.get("datasetSequence");
84     if (oldname == null)
85     {
86       namePresent = false;
87     }
88     else
89     {
90       sq.setName(oldname);
91     }
92     if (pdbid!=null && pdbid.size()>0)
93     {
94       sq.setPDBId(pdbid);
95     }
96
97     if ( (start != null) && (end != null))
98     {
99       sq.setStart(start.intValue());
100       sq.setEnd(end.intValue());
101     }
102
103     if ((sfeatures != null) && (sfeatures.size()>0))
104     {
105       SequenceFeature[] sfarray = (SequenceFeature[]) sfeatures.toArray();
106       sq.setSequenceFeatures(sfarray);
107     }
108
109     if ((seqds!=null) && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds.getLength()==0)) {
110       sq.setDatasetSequence(seqds);
111     }
112
113     return namePresent;
114   }
115
116   /**
117    * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.
118    * @param i int
119    * @return String
120    */
121   public static String unique_name(int i)
122   {
123     return new String("Sequence" + i);
124   }
125
126   /**
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
133    */
134   public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)
135   {
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];
139
140     for (int i = 0; i < sequences.length; i++)
141     {
142       String safename = unique_name(i);
143       map.put(safename, SeqCharacterHash(sequences[i]));
144
145       if (write_names)
146       {
147         sequences[i].setName(safename);
148       }
149     }
150
151
152     return map;
153   }
154   /**
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[]
159    * @return boolean
160    */
161   public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
162   {
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);
176         }
177         else
178         {
179           System.err.println("Can't find '"+((String) key)+"' in uniquified alignment");
180         }
181       }
182     }
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()))
186            ;
187       return false;
188     }
189
190     return true;
191   }
192   /**
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[]
197    */
198   public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences) {
199       // Identify first row of alignment with residues for prediction
200       boolean ungapped[] = new boolean[sequences.length];
201       int msflen=0;
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)
205           ungapped[i]=false;
206         else {
207           ungapped[i]=true;
208           msflen++;
209         }
210       }
211       if (msflen==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++) {
216         if (ungapped[i])
217           mset[k++] = sequences[i];
218       }
219       ungapped = null;
220       return mset;
221   }
222 }