Formatted source
[jalview.git] / src / jalview / analysis / SeqsetUtils.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 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 */\r
19 package jalview.analysis;\r
20 \r
21 import jalview.datamodel.SequenceI;\r
22 \r
23 import java.util.Hashtable;\r
24 \r
25 \r
26 /**
27  * <p>Title: </p>
28  *
29  * <p>Description: </p>
30  *
31  * <p>Copyright: Copyright (c) 2004</p>
32  *
33  * <p>Company: Dundee University</p>
34  *
35  * @author not attributable
36  * @version 1.0
37  */\r
38 public class SeqsetUtils {\r
39     /**
40  * Store essential properties of a sequence in a hashtable for later recovery
41  *  Keys are Name, Start, End, SeqFeatures, PdbId
42  * @param seq SequenceI
43  * @return Hashtable
44  */\r
45     public static Hashtable SeqCharacterHash(SequenceI seq) {\r
46         Hashtable sqinfo = new Hashtable();\r
47         sqinfo.put("Name", seq.getName());\r
48         sqinfo.put("Start", new Integer(seq.getStart()));\r
49         sqinfo.put("End", new Integer(seq.getEnd()));\r
50         sqinfo.put("SeqFeatures", seq.getSequenceFeatures());\r
51         sqinfo.put("PdbId",\r
52             (seq.getPDBId() != null) ? seq.getPDBId() : new String(""));\r
53 \r
54         return sqinfo;\r
55     }\r
56 \r
57     /**
58  * Recover essential properties of a sequence from a hashtable
59  * TODO: replace these methods with something more elegant.
60  * @param sq SequenceI
61  * @param sqinfo Hashtable
62  * @return boolean
63  */\r
64     public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo) {\r
65         boolean namePresent = true;\r
66         String oldname = (String) sqinfo.get("Name");\r
67         Integer start = (Integer) sqinfo.get("Start");\r
68         Integer end = (Integer) sqinfo.get("End");\r
69         java.util.Vector sfeatures = (java.util.Vector) sqinfo.get(\r
70                 "SeqFeatures");\r
71         String pdbid = (String) sqinfo.get("PdbId");\r
72 \r
73         if (oldname == null) {\r
74             namePresent = false;\r
75         } else {\r
76             sq.setName(oldname);\r
77         }\r
78 \r
79         if (!pdbid.equals("")) {\r
80             sq.setPDBId(pdbid);\r
81         }\r
82 \r
83         if ((start != null) && (end != null)) {\r
84             sq.setStart(start.intValue());\r
85             sq.setEnd(end.intValue());\r
86         }\r
87 \r
88         if (sfeatures != null) {\r
89             sq.setSequenceFeatures(sfeatures);\r
90         }\r
91 \r
92         return namePresent;\r
93     }\r
94 \r
95     /**
96  * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.
97  * @param i int
98  * @return String
99  */\r
100     public static String unique_name(int i) {\r
101         return new String("Sequence" + i);\r
102     }\r
103 \r
104     public static Hashtable uniquify(SequenceI[] sequences, boolean write_names) {\r
105         // Generate a safely named sequence set and a hash to recover the sequence names\r
106         Hashtable map = new Hashtable();\r
107         String[] un_names = new String[sequences.length];\r
108 \r
109         if (!write_names) {\r
110             for (int i = 0; i < sequences.length; i++) {\r
111                 String safename = new String("Sequence" + i);\r
112                 map.put(safename, SeqCharacterHash(sequences[i]));\r
113 \r
114                 if (write_names) {\r
115                     sequences[i].setName(safename);\r
116                 }\r
117             }\r
118         }\r
119 \r
120         return map;\r
121     }\r
122 \r
123     public static boolean deuniquify(Hashtable map, SequenceI[] sequences) {\r
124         // recover unsafe sequence names for a sequence set\r
125         boolean allfound = true;\r
126 \r
127         for (int i = 0; i < sequences.length; i++) {\r
128             if (map.containsKey(sequences[i].getName())) {\r
129                 Hashtable sqinfo = (Hashtable) map.get(sequences[i].getName());\r
130                 SeqCharacterUnhash(sequences[i], sqinfo);\r
131             } else {\r
132                 allfound = false;\r
133             }\r
134         }\r
135 \r
136         return allfound;\r
137     }\r
138 }\r