Formatting changes
[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    * Store essential properties of a sequence in a hashtable for later recovery\r
41    *  Keys are Name, Start, End, SeqFeatures, PdbId\r
42    * @param seq SequenceI\r
43    * @return Hashtable\r
44    */\r
45   public static Hashtable SeqCharacterHash(SequenceI seq)\r
46   {\r
47     Hashtable sqinfo = new Hashtable();\r
48     sqinfo.put("Name", seq.getName());\r
49     sqinfo.put("Start", new Integer(seq.getStart()));\r
50     sqinfo.put("End", new Integer(seq.getEnd()));\r
51     sqinfo.put("SeqFeatures", seq.getSequenceFeatures());\r
52     sqinfo.put("PdbId",\r
53                (seq.getPDBId() != null) ? seq.getPDBId() : new String(""));\r
54 \r
55     return sqinfo;\r
56   }\r
57 \r
58   /**\r
59    * Recover essential properties of a sequence from a hashtable\r
60    * TODO: replace these methods with something more elegant.\r
61    * @param sq SequenceI\r
62    * @param sqinfo Hashtable\r
63    * @return boolean\r
64    */\r
65   public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo)\r
66   {\r
67     boolean namePresent = true;\r
68     String oldname = (String) sqinfo.get("Name");\r
69     Integer start = (Integer) sqinfo.get("Start");\r
70     Integer end = (Integer) sqinfo.get("End");\r
71     java.util.Vector sfeatures = (java.util.Vector) sqinfo.get(\r
72         "SeqFeatures");\r
73     String pdbid = (String) sqinfo.get("PdbId");\r
74 \r
75     if (oldname == null)\r
76     {\r
77       namePresent = false;\r
78     }\r
79     else\r
80     {\r
81       sq.setName(oldname);\r
82     }\r
83 \r
84     if (!pdbid.equals(""))\r
85     {\r
86       sq.setPDBId(pdbid);\r
87     }\r
88 \r
89     if ( (start != null) && (end != null))\r
90     {\r
91       sq.setStart(start.intValue());\r
92       sq.setEnd(end.intValue());\r
93     }\r
94 \r
95     if (sfeatures != null)\r
96     {\r
97       sq.setSequenceFeatures(sfeatures);\r
98     }\r
99 \r
100     return namePresent;\r
101   }\r
102 \r
103   /**\r
104    * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.\r
105    * @param i int\r
106    * @return String\r
107    */\r
108   public static String unique_name(int i)\r
109   {\r
110     return new String("Sequence" + i);\r
111   }\r
112 \r
113   public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)\r
114   {\r
115     // Generate a safely named sequence set and a hash to recover the sequence names\r
116     Hashtable map = new Hashtable();\r
117     String[] un_names = new String[sequences.length];\r
118 \r
119     if (!write_names)\r
120     {\r
121       for (int i = 0; i < sequences.length; i++)\r
122       {\r
123         String safename = new String("Sequence" + i);\r
124         map.put(safename, SeqCharacterHash(sequences[i]));\r
125 \r
126         if (write_names)\r
127         {\r
128           sequences[i].setName(safename);\r
129         }\r
130       }\r
131     }\r
132 \r
133     return map;\r
134   }\r
135 \r
136   public static boolean deuniquify(Hashtable map, SequenceI[] sequences)\r
137   {\r
138     // recover unsafe sequence names for a sequence set\r
139     boolean allfound = true;\r
140 \r
141     for (int i = 0; i < sequences.length; i++)\r
142     {\r
143       if (map.containsKey(sequences[i].getName()))\r
144       {\r
145         Hashtable sqinfo = (Hashtable) map.get(sequences[i].getName());\r
146         SeqCharacterUnhash(sequences[i], sqinfo);\r
147       }\r
148       else\r
149       {\r
150         allfound = false;\r
151       }\r
152     }\r
153 \r
154     return allfound;\r
155   }\r
156 }\r