fixed uniquify seq[]
[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());\r
53     sqinfo.put("PdbId",\r
54                (seq.getPDBId() != null) ? seq.getPDBId() : new String(""));\r
55 \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     String oldname = (String) sqinfo.get("Name");\r
70     Integer start = (Integer) sqinfo.get("Start");\r
71     Integer end = (Integer) sqinfo.get("End");\r
72     java.util.Vector sfeatures = (java.util.Vector) sqinfo.get(\r
73         "SeqFeatures");\r
74     String pdbid = (String) sqinfo.get("PdbId");\r
75 \r
76     if (oldname == null)\r
77     {\r
78       namePresent = false;\r
79     }\r
80     else\r
81     {\r
82       sq.setName(oldname);\r
83     }\r
84 \r
85     if (!pdbid.equals(""))\r
86     {\r
87       sq.setPDBId(pdbid);\r
88     }\r
89 \r
90     if ( (start != null) && (end != null))\r
91     {\r
92       sq.setStart(start.intValue());\r
93       sq.setEnd(end.intValue());\r
94     }\r
95 \r
96     if (sfeatures != null)\r
97     {\r
98       sq.setSequenceFeatures(sfeatures);\r
99     }\r
100 \r
101     return namePresent;\r
102   }\r
103 \r
104   /**\r
105    * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.\r
106    * @param i int\r
107    * @return String\r
108    */\r
109   public static String unique_name(int i)\r
110   {\r
111     return new String("Sequence" + i);\r
112   }\r
113 \r
114   public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)\r
115   {\r
116     // Generate a safely named sequence set and a hash to recover the sequence names\r
117     Hashtable map = new Hashtable();\r
118     String[] un_names = new String[sequences.length];\r
119 \r
120     for (int i = 0; i < sequences.length; i++)\r
121     {\r
122       String safename = unique_name(i);\r
123       map.put(safename, SeqCharacterHash(sequences[i]));\r
124 \r
125       if (write_names)\r
126       {\r
127         sequences[i].setName(safename);\r
128       }\r
129     }\r
130 \r
131 \r
132     return map;\r
133   }\r
134 \r
135   public static boolean deuniquify(Hashtable map, SequenceI[] sequences)\r
136   {\r
137     // recover unsafe sequence names for a sequence set\r
138     boolean allfound = true;\r
139 \r
140     for (int i = 0; i < sequences.length; i++)\r
141     {\r
142       if (map.containsKey(sequences[i].getName()))\r
143       {\r
144         Hashtable sqinfo = (Hashtable) map.get(sequences[i].getName());\r
145         SeqCharacterUnhash(sequences[i], sqinfo);\r
146       }\r
147       else\r
148       {\r
149         allfound = false;\r
150       }\r
151     }\r
152 \r
153     return allfound;\r
154   }\r
155 }\r