Features now array
[jalview.git] / src / jalview / analysis / SeqsetUtils.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\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
9  *\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
14  *\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
18  */\r
19 package jalview.analysis;\r
20 \r
21 import java.util.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 /**\r
26  * <p>Title: </p>\r
27  *\r
28  * <p>Description: </p>\r
29  *\r
30  * <p>Copyright: Copyright (c) 2004</p>\r
31  *\r
32  * <p>Company: Dundee University</p>\r
33  *\r
34  * @author not attributable\r
35  * @version 1.0\r
36  */\r
37 public class SeqsetUtils\r
38 {\r
39 \r
40   /**\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
44    * @return Hashtable\r
45    */\r
46   public static Hashtable SeqCharacterHash(SequenceI seq)\r
47   {\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     sqinfo.put("SeqFeatures", (seq.getSequenceFeatures() !=null) ? seq.getSequenceFeatures() : null);\r
53     sqinfo.put("PdbId",\r
54                (seq.getPDBId() != null) ? seq.getPDBId() : new Vector());\r
55     sqinfo.put("datasetSequence", (seq.getDatasetSequence() !=null) ? seq.getDatasetSequence() : new Sequence("THISISAPLACEHOLDER",""));\r
56     return sqinfo;\r
57   }\r
58 \r
59   /**\r
60    * Recover essential properties of a sequence from a hashtable\r
61    * TODO: replace these methods with something more elegant.\r
62    * @param sq SequenceI\r
63    * @param sqinfo Hashtable\r
64    * @return boolean\r
65    */\r
66   public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)\r
67   {\r
68     boolean namePresent = true;\r
69     if (sqinfo==null)\r
70       return false;\r
71     String oldname = (String) sqinfo.get("Name");\r
72     Integer start = (Integer) sqinfo.get("Start");\r
73     Integer end = (Integer) sqinfo.get("End");\r
74     SequenceFeature [] sfeatures = (SequenceFeature[]) sqinfo.get(\r
75         "SeqFeatures");\r
76     Vector pdbid = (Vector) sqinfo.get("PdbId");\r
77     Sequence seqds = (Sequence) sqinfo.get("datasetSequence");\r
78     if (oldname == null)\r
79     {\r
80       namePresent = false;\r
81     }\r
82     else\r
83     {\r
84       sq.setName(oldname);\r
85     }\r
86     if (pdbid!=null && pdbid.size()>0)\r
87     {\r
88       sq.setPDBId(pdbid);\r
89     }\r
90 \r
91     if ( (start != null) && (end != null))\r
92     {\r
93       sq.setStart(start.intValue());\r
94       sq.setEnd(end.intValue());\r
95     }\r
96 \r
97     if ((sfeatures != null) && (sfeatures.length>0))\r
98     {\r
99       sq.setSequenceFeatures(sfeatures);\r
100     }\r
101     if ((seqds!=null) && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds.getLength()==0)) {\r
102       sq.setDatasetSequence(seqds);\r
103     }\r
104 \r
105     return namePresent;\r
106   }\r
107 \r
108   /**\r
109    * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.\r
110    * @param i int\r
111    * @return String\r
112    */\r
113   public static String unique_name(int i)\r
114   {\r
115     return new String("Sequence" + i);\r
116   }\r
117 \r
118   /**\r
119    * Generates a hash of SeqCharacterHash properties for each sequence\r
120    * in a sequence set, and optionally renames the sequences to an\r
121    * unambiguous 'safe' name.\r
122    * @param sequences SequenceI[]\r
123    * @param write_names boolean set this to rename each of the sequences to its unique_name(index) name\r
124    * @return Hashtable to be passed to @see deuniquify to recover original names (and properties) for renamed sequences\r
125    */\r
126   public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)\r
127   {\r
128     // Generate a safely named sequence set and a hash to recover the sequence names\r
129     Hashtable map = new Hashtable();\r
130     //String[] un_names = new String[sequences.length];\r
131 \r
132     for (int i = 0; i < sequences.length; i++)\r
133     {\r
134       String safename = unique_name(i);\r
135       map.put(safename, SeqCharacterHash(sequences[i]));\r
136 \r
137       if (write_names)\r
138       {\r
139         sequences[i].setName(safename);\r
140       }\r
141     }\r
142 \r
143 \r
144     return map;\r
145   }\r
146   /**\r
147    * recover unsafe sequence names and original properties for a sequence\r
148    * set using a map generated by @see uniquify(sequences,true)\r
149    * @param map Hashtable\r
150    * @param sequences SequenceI[]\r
151    * @return boolean\r
152    */\r
153   public static boolean deuniquify(Hashtable map, SequenceI[] sequences)\r
154   {\r
155     jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher(sequences);\r
156     SequenceI msq = null;\r
157     Enumeration keys = map.keys();\r
158     Vector unmatched = new Vector();\r
159     for (int i=0, j=sequences.length; i<j; i++)\r
160       unmatched.add(sequences[i]);\r
161     while (keys.hasMoreElements()) {\r
162       Object key = keys.nextElement();\r
163       if (key instanceof String) {\r
164         if ((msq = matcher.findIdMatch((String) key))!=null) {\r
165           Hashtable sqinfo = (Hashtable) map.get(key);\r
166           unmatched.remove(msq);\r
167           SeqCharacterUnhash(msq, sqinfo);\r
168         }\r
169         else\r
170         {\r
171           System.err.println("Can't find '"+((String) key)+"' in uniquified alignment");\r
172         }\r
173       }\r
174     }\r
175     if (unmatched.size()>0) {\r
176       System.err.println("Did not find matches for :");\r
177       for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out.println(((SequenceI) i.nextElement()).getName()))\r
178            ;\r
179       return false;\r
180     }\r
181 \r
182     return true;\r
183   }\r
184   /**\r
185    * returns a subset of the sequenceI seuqences,\r
186    * including only those that contain at least one residue.\r
187    * @param sequences SequenceI[]\r
188    * @return SequenceI[]\r
189    */\r
190   public static SequenceI[] getNonEmptySequenceSet(SequenceI[] sequences) {\r
191       // Identify first row of alignment with residues for prediction\r
192       boolean ungapped[] = new boolean[sequences.length];\r
193       int msflen=0;\r
194       for (int i=0,j=sequences.length; i<j;i++) {\r
195         String tempseq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sequences[i].getSequence());\r
196         if (tempseq.length()==0)\r
197           ungapped[i]=false;\r
198         else {\r
199           ungapped[i]=true;\r
200           msflen++;\r
201         }\r
202       }\r
203       if (msflen==0)\r
204         return null; // no minimal set\r
205       // compose minimal set\r
206       SequenceI[] mset = new SequenceI[msflen];\r
207       for (int i=0,j=sequences.length,k=0; i<j;i++) {\r
208         if (ungapped[i])\r
209           mset[k++] = sequences[i];\r
210       }\r
211       ungapped = null;\r
212       return mset;\r
213   }\r
214 }\r