Ugly but improved recovery of sequence names, starts/ends and other SequenceI propert...
[jalview.git] / src / jalview / analysis / SeqsetUtils.java
1 package jalview.analysis;
2
3 import jalview.datamodel.SequenceI;
4 import java.util.Hashtable;
5
6 /**
7  * <p>Title: </p>
8  *
9  * <p>Description: </p>
10  *
11  * <p>Copyright: Copyright (c) 2004</p>
12  *
13  * <p>Company: Dundee University</p>
14  *
15  * @author not attributable
16  * @version 1.0
17  */
18 public class SeqsetUtils
19 {
20     /**
21      * Store essential properties of a sequence in a hashtable for later recovery
22      *  Keys are Name, Start, End, SeqFeatures, PdbId
23      * @param seq SequenceI
24      * @return Hashtable
25      */
26     public static Hashtable SeqCharacterHash(SequenceI seq) {
27     Hashtable sqinfo = new Hashtable();
28     sqinfo.put("Name", seq.getName());
29     sqinfo.put("Start", new Integer(seq.getStart()));
30     sqinfo.put("End", new Integer(seq.getEnd()));
31     sqinfo.put("SeqFeatures", seq.getSequenceFeatures());
32     sqinfo.put("PdbId", (seq.getPDBId()!=null) ? seq.getPDBId() : new String("") );
33     return sqinfo;
34   }
35   /**
36    * Recover essential properties of a sequence from a hashtable
37    * TODO: replace these methods with something more elegant.
38    * @param sq SequenceI
39    * @param sqinfo Hashtable
40    * @return boolean
41    */
42   public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo) {
43     boolean namePresent = true;
44     String oldname = (String) sqinfo.get("Name");
45     Integer start = (Integer) sqinfo.get("Start");
46     Integer end = (Integer) sqinfo.get("End");
47     java.util.Vector sfeatures = (java.util.Vector) sqinfo.get("SeqFeatures");
48     String pdbid = (String) sqinfo.get("PdbId");
49     if (oldname==null)
50       namePresent = false;
51     else
52       sq.setName(oldname);
53     if (!pdbid.equals(""))
54       sq.setPDBId(pdbid);
55
56     if ((start!=null) && (end!=null)) {
57       sq.setStart(start.intValue());
58       sq.setEnd(end.intValue());
59     }
60     if (sfeatures!=null)
61       sq.setSequenceFeatures(sfeatures);
62     return namePresent;
63   }
64
65   /**
66    * Form of the unique name used in uniquify for the i'th sequence in an ordered vector of sequences.
67    * @param i int
68    * @return String
69    */
70   public static String unique_name(int i) {
71       return new String("Sequence"+i);
72   }
73
74   public static Hashtable uniquify(SequenceI[] sequences, boolean write_names)
75   {
76     // Generate a safely named sequence set and a hash to recover the sequence names
77     Hashtable map = new Hashtable();
78     String[] un_names = new String[sequences.length];
79     if (!write_names)
80
81     for (int i = 0; i < sequences.length; i++)
82     {
83       String safename = new String("Sequence" + i);
84       map.put(safename, SeqCharacterHash(sequences[i]));
85       if (write_names)
86         sequences[i].setName(safename);
87     }
88     return map;
89   }
90
91   public static boolean deuniquify(Hashtable map, SequenceI[] sequences)
92   {
93     // recover unsafe sequence names for a sequence set
94     boolean allfound = true;
95     for (int i = 0; i < sequences.length; i++)
96     {
97       if (map.containsKey(sequences[i].getName()))
98       {
99         Hashtable sqinfo = (Hashtable) map.get(sequences[i].getName());
100         SeqCharacterUnhash(sequences[i], sqinfo);
101       }
102       else
103       {
104         allfound = false;
105       }
106     }
107     return allfound;
108   }
109
110 }