various loadTree bits for loading and saving trees.
[jalview.git] / src / jalview / analysis / CompareAlignments.java
1 package jalview.analysis;\r
2 \r
3 import jalview.datamodel.*;\r
4 import jalview.io.*;\r
5 \r
6 import java.util.*;\r
7 \r
8 public class CompareAlignments {\r
9 \r
10     Vector align;\r
11     String refId;\r
12     Vector ids;\r
13 \r
14     public CompareAlignments(Vector align) {\r
15 \r
16         this.align = align;\r
17 \r
18         ids = new Vector();\r
19 \r
20         Alignment al0 = (Alignment)align.elementAt(0);\r
21 \r
22         for (int i = 0; i < al0.getHeight(); i++) {\r
23             SequenceI seq = al0.getSequenceAt(i);\r
24 \r
25             ids.addElement(seq.getName());\r
26 \r
27             if (i == 0) {\r
28                 setReferenceId(seq.getName());\r
29             }\r
30         }\r
31 \r
32     }\r
33 \r
34     public void compare() {\r
35 \r
36         Hashtable positions = new Hashtable();\r
37 \r
38         for (int k = 0; k < ids.size(); k++) {\r
39 \r
40             String id = (String)ids.elementAt(k);\r
41 \r
42             System.out.println("Ids " + id + " " + refId);\r
43 \r
44             if (!id.equals(refId)) {\r
45 \r
46                 Hashtable fullhash = new Hashtable();\r
47 \r
48                 for (int i = 0; i < align.size(); i++) {\r
49                     System.out.println("Alignment " + i);\r
50 \r
51                     Alignment al = (Alignment)align.elementAt(i);\r
52 \r
53                     SequenceI refseq = null;\r
54 \r
55                     for (int j = 0; j < al.getHeight(); j++) {\r
56                         if (((SequenceI)al.getSequenceAt(j)).getName().equals(refId)) {\r
57                             refseq = (SequenceI)al.getSequenceAt(j);\r
58                         }\r
59                     }\r
60 \r
61                     if (refseq != null) {\r
62 \r
63                         System.out.println("Refseq " + refseq.getName());\r
64 \r
65                         for (int jj = 0; jj < al.getHeight(); jj++) {\r
66                             SequenceI seq = (SequenceI)al.getSequenceAt(jj);\r
67 \r
68                             if (seq.getName().equals(id)) {\r
69                                 Hashtable hash = getAlignPositions(seq,refseq);\r
70 \r
71                                 Enumeration keys = hash.keys();\r
72 \r
73                                 while (keys.hasMoreElements()) {\r
74                                     Integer key = (Integer)keys.nextElement();\r
75                                     //                          System.out.println(key + " " + hash.get(key));\r
76                                     if (fullhash.get(key) == null) {\r
77                                         fullhash.put(key,new Vector());\r
78                                     }\r
79 \r
80                                     Vector tmp = (Vector)fullhash.get(key);\r
81 \r
82                                     tmp.addElement(hash.get(key));\r
83                                 }\r
84                             }\r
85 \r
86                         }\r
87                     }\r
88                 }\r
89 \r
90                 System.out.println ("\nId " + id);\r
91 \r
92                 Enumeration keys = fullhash.keys();\r
93 \r
94 \r
95                 int totdiff = 0;\r
96                 while (keys.hasMoreElements()) {\r
97                     Integer key = (Integer)keys.nextElement();\r
98 \r
99                     Vector tmp = (Vector)fullhash.get(key);\r
100 \r
101 \r
102 \r
103                     int diff0 = ((Integer)tmp.elementAt(0)).intValue();;\r
104                     int diff = 0;\r
105 \r
106                     for (int l = 1; l < tmp.size(); l++) {\r
107                         diff += Math.abs(diff0 - ((Integer)tmp.elementAt(l)).intValue());\r
108 \r
109                     }\r
110 \r
111                     if (diff > 0) {\r
112                         totdiff++;\r
113                         System.out.print(id + " Ref pos " + key + " : " + diff0 + " " + diff + " : ");\r
114 \r
115                         for (int l = 1; l < tmp.size(); l++) {\r
116                             System.out.print(diff0 - ((Integer)tmp.elementAt(l)).intValue() + " ");\r
117                         }\r
118                         System.out.println();\r
119                     }\r
120                 }\r
121 \r
122                 System.out.println("Total " + id + " " + totdiff);\r
123 \r
124             }\r
125         }\r
126     }\r
127 \r
128 \r
129     public void setReferenceId(String id) {\r
130         this.refId = id;\r
131     }\r
132 \r
133     public Hashtable getAlignPositions(SequenceI seq1, SequenceI seq2) {\r
134 \r
135         Hashtable hash = new Hashtable();\r
136 \r
137         int i = 0;\r
138 \r
139         int pos1 = 0;\r
140         int pos2 = 0;\r
141 \r
142         while (i < seq1.getLength()) {\r
143 \r
144             char c1 = seq1.getCharAt(i);\r
145             char c2 = seq2.getCharAt(i);\r
146 \r
147             if (c1 != '-') {\r
148                 pos1++;\r
149             }\r
150 \r
151             if (c2 != '-') {\r
152                 pos2++;\r
153             }\r
154 \r
155             if (c1 != '-') {\r
156                 hash.put(new Integer(pos1),new Integer(pos2));\r
157             }\r
158 \r
159             i++;\r
160         }\r
161         return hash;\r
162     }\r
163 \r
164 \r
165 \r
166 }\r