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