header updated
[jalview.git] / src / jalview / datamodel / AlignmentOrder.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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.datamodel;\r
20 \r
21 import java.util.*;\r
22 \r
23 \r
24 /**\r
25  * <p>Title: </p>\r
26  *\r
27  * <p>Description: </p>\r
28  *\r
29  * <p>Copyright: Copyright (c) 2004</p>\r
30  *\r
31  * <p>Company: Dundee University</p>\r
32  *\r
33  * @author not attributable\r
34  * @version 1.0\r
35  */\r
36 public class AlignmentOrder\r
37 {\r
38     // JBPNote : this method would return a vector containing all sequences in seqset\r
39     // with those also contained in order at the beginning of the vector in the order\r
40     // given by order. AlignmentSorter.vectorSubsetToArray already does this, but that method\r
41     // should be here for completeness.\r
42 \r
43     /*  public Vector getOrder(AlignmentI seqset)\r
44        {\r
45          Vector perm = new Vector(seqset.getHeight());\r
46          for (i=0, o = 0, n=seqset.getHeight(), p = Order.size(); i<n; i++)\r
47       perm.setElement(i,...).\r
48          return Order;\r
49        }\r
50      */\r
51 \r
52     /** DOCUMENT ME!! */\r
53     public static final int FILE = 0;\r
54 \r
55     /** DOCUMENT ME!! */\r
56     public static final int MSA = 1;\r
57 \r
58     /** DOCUMENT ME!! */\r
59     public static final int USER = 2;\r
60     private int Type = 0;\r
61     private String Name;\r
62     private Vector Order = null;\r
63 \r
64     /**\r
65      * Creates a new AlignmentOrder object.\r
66      */\r
67     public AlignmentOrder()\r
68     {\r
69     }\r
70 \r
71     /**\r
72      * AlignmentOrder\r
73      *\r
74      * @param anOrder Vector\r
75      */\r
76     public AlignmentOrder(Vector anOrder)\r
77     {\r
78         Order = anOrder;\r
79     }\r
80 \r
81     /**\r
82      * AlignmentOrder\r
83      *\r
84      * @param orderFrom AlignmentI\r
85      */\r
86     public AlignmentOrder(AlignmentI orderFrom)\r
87     {\r
88         Order = new Vector();\r
89 \r
90         for (int i = 0, ns = orderFrom.getHeight(); i < ns; i++)\r
91         {\r
92             Order.addElement(orderFrom.getSequenceAt(i));\r
93         }\r
94     }\r
95 \r
96     /**\r
97      * Creates a new AlignmentOrder object.\r
98      *\r
99      * @param orderFrom DOCUMENT ME!\r
100      */\r
101     public AlignmentOrder(SequenceI[] orderFrom)\r
102     {\r
103         Order = new Vector();\r
104 \r
105         for (int i = 0, ns = orderFrom.length; i < ns; i++)\r
106         {\r
107             Order.addElement(orderFrom[i]);\r
108         }\r
109     }\r
110 \r
111     /**\r
112      * DOCUMENT ME!\r
113      *\r
114      * @param Type DOCUMENT ME!\r
115      */\r
116     public void setType(int Type)\r
117     {\r
118         this.Type = Type;\r
119     }\r
120 \r
121     /**\r
122      * DOCUMENT ME!\r
123      *\r
124      * @return DOCUMENT ME!\r
125      */\r
126     public int getType()\r
127     {\r
128         return Type;\r
129     }\r
130 \r
131     /**\r
132      * DOCUMENT ME!\r
133      *\r
134      * @param Name DOCUMENT ME!\r
135      */\r
136     public void setName(String Name)\r
137     {\r
138         this.Name = Name;\r
139     }\r
140 \r
141     /**\r
142      * DOCUMENT ME!\r
143      *\r
144      * @return DOCUMENT ME!\r
145      */\r
146     public String getName()\r
147     {\r
148         return Name;\r
149     }\r
150 \r
151     /**\r
152      * DOCUMENT ME!\r
153      *\r
154      * @param Order DOCUMENT ME!\r
155      */\r
156     public void setOrder(Vector Order)\r
157     {\r
158         this.Order = Order;\r
159     }\r
160 \r
161     /**\r
162      * DOCUMENT ME!\r
163      *\r
164      * @return DOCUMENT ME!\r
165      */\r
166     public Vector getOrder()\r
167     {\r
168         return Order;\r
169     }\r
170     /**\r
171      * replaces oldref with newref in the alignment order.\r
172      * @param oldref\r
173      * @param newref\r
174      * @return true if oldref was contained in order and replaced with newref\r
175      */\r
176     public boolean updateSequence(SequenceI oldref, SequenceI newref) {\r
177       int found=Order.indexOf(oldref);\r
178       if (found>-1) {\r
179         Order.setElementAt(newref, found);\r
180       }\r
181       return found>-1;\r
182     }\r
183     /**\r
184      * Exact equivalence of two AlignmentOrders\r
185      * @param o\r
186      * @return true if o orders the same sequenceI objects in the same way\r
187      */\r
188     public boolean equals(AlignmentOrder o) {\r
189       return equals(o, true);\r
190     }\r
191     /**\r
192      * Exact equivalence of two AlignmentOrders\r
193      *  // TODO: Weak SequenceI equivalence - will throw Error at moment\r
194      * @param o\r
195      * @param identity - false - use weak equivalence (refers to same or different parts of same sequence)\r
196      * @return true if o orders equivalent sequenceI objects in the same way\r
197      */\r
198     public boolean equals(AlignmentOrder o, boolean identity) {\r
199       if (o!=this) {\r
200         if (o==null)\r
201           return false;\r
202         if (Order!=null && o.Order!=null && Order.size()==o.Order.size()) {\r
203           if (!identity) {\r
204             throw new Error("Weak sequenceI equivalence not yet implemented.");\r
205           } else {\r
206             for (int i=0,j=o.Order.size(); i<j; i++) {\r
207             if (Order.elementAt(i)!=o.Order.elementAt(i))\r
208               return false;\r
209             }\r
210           }\r
211         } else\r
212           return false;\r
213       }\r
214       return true;\r
215     }\r
216     /**\r
217      * Consistency test for alignmentOrders\r
218      * @param o\r
219      * @return true if o contains or is contained by this and the common SequenceI objects are ordered in the same way\r
220      */\r
221     public boolean isConsistent(AlignmentOrder o) {\r
222       return isConsistent(o, true);\r
223     }\r
224     /**\r
225      * Consistency test for alignmentOrders\r
226      * @param o\r
227      *  // TODO: Weak SequenceI equivalence - will throw Error at moment\r
228      * @param identity - false - use weak equivalence (refers to same or different parts of same sequence)\r
229      * @return true if o contains or is contained by this and the common SequenceI objects are ordered in the same way\r
230      */\r
231     public boolean isConsistent(AlignmentOrder o, boolean identity) {\r
232       if (o!=this) {\r
233         if (o==null)\r
234           return false;\r
235         if (Order!=null && o.Order!=null) {\r
236           Vector c,s;\r
237           if (o.Order.size()>Order.size()) {\r
238             c = o.Order;\r
239             s = Order;\r
240           } else {\r
241             c = Order;\r
242             s = o.Order;\r
243           }\r
244           if (!identity) {\r
245             throw new Error("Weak sequenceI equivalence not yet implemented.");\r
246           } else {\r
247             // test if c contains s and order in s is conserved in c\r
248             int last=-1;\r
249             for (int i=0,j=s.size(); i<j; i++) {\r
250               int pos=c.indexOf(s.elementAt(i)); // JBPNote - optimize by incremental position search\r
251               if (pos>last) {\r
252                 last=pos;\r
253               } else\r
254                 return false;\r
255             }\r
256           }\r
257         } else\r
258           return false;\r
259       }\r
260       return true;\r
261     }\r
262     /**\r
263      * AlignmentOrder\r
264      *\r
265      * @param orderThis AlignmentI\r
266      * @param byThat AlignmentI\r
267      */\r
268 \r
269     /* public AlignmentOrder(AlignmentI orderThis, AlignmentI byThat)\r
270      {\r
271        // Vector is an ordering of this alignment using the order of sequence objects in byThat,\r
272        // where ids and unaligned sequences must match\r
273 \r
274      } */\r
275 }\r