7bf20424892c1f0963c789ae8b33c417ee2d724f
[jalview.git] / src / jalview / datamodel / AlignmentOrder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.datamodel;
19
20 import java.util.*;
21
22 /**
23  * <p>
24  * Title:
25  * </p>
26  * 
27  * <p>
28  * Description:
29  * </p>
30  * 
31  * <p>
32  * Copyright: Copyright (c) 2004
33  * </p>
34  * 
35  * <p>
36  * Company: Dundee University
37  * </p>
38  * 
39  * @author not attributable
40  * @version 1.0
41  */
42 public class AlignmentOrder
43 {
44   // JBPNote : this method would return a vector containing all sequences in
45   // seqset
46   // with those also contained in order at the beginning of the vector in the
47   // order
48   // given by order. AlignmentSorter.vectorSubsetToArray already does this, but
49   // that method
50   // should be here for completeness.
51
52   /*
53    * public Vector getOrder(AlignmentI seqset) { Vector perm = new
54    * Vector(seqset.getHeight()); for (i=0, o = 0, n=seqset.getHeight(), p =
55    * Order.size(); i<n; i++) perm.setElement(i,...). return Order; }
56    */
57
58   /** DOCUMENT ME!! */
59   public static final int FILE = 0;
60
61   /** DOCUMENT ME!! */
62   public static final int MSA = 1;
63
64   /** DOCUMENT ME!! */
65   public static final int USER = 2;
66
67   private int Type = 0;
68
69   private String Name;
70
71   private Vector Order = null;
72
73   /**
74    * Creates a new AlignmentOrder object.
75    */
76   public AlignmentOrder()
77   {
78   }
79
80   /**
81    * AlignmentOrder
82    * 
83    * @param anOrder
84    *          Vector
85    */
86   public AlignmentOrder(Vector anOrder)
87   {
88     Order = anOrder;
89   }
90
91   /**
92    * AlignmentOrder
93    * 
94    * @param orderFrom
95    *          AlignmentI
96    */
97   public AlignmentOrder(AlignmentI orderFrom)
98   {
99     Order = new Vector();
100
101     for (int i = 0, ns = orderFrom.getHeight(); i < ns; i++)
102     {
103       Order.addElement(orderFrom.getSequenceAt(i));
104     }
105   }
106
107   /**
108    * Creates a new AlignmentOrder object.
109    * 
110    * @param orderFrom
111    *          DOCUMENT ME!
112    */
113   public AlignmentOrder(SequenceI[] orderFrom)
114   {
115     Order = new Vector();
116
117     for (int i = 0, ns = orderFrom.length; i < ns; i++)
118     {
119       Order.addElement(orderFrom[i]);
120     }
121   }
122
123   /**
124    * DOCUMENT ME!
125    * 
126    * @param Type
127    *          DOCUMENT ME!
128    */
129   public void setType(int Type)
130   {
131     this.Type = Type;
132   }
133
134   /**
135    * DOCUMENT ME!
136    * 
137    * @return DOCUMENT ME!
138    */
139   public int getType()
140   {
141     return Type;
142   }
143
144   /**
145    * DOCUMENT ME!
146    * 
147    * @param Name
148    *          DOCUMENT ME!
149    */
150   public void setName(String Name)
151   {
152     this.Name = Name;
153   }
154
155   /**
156    * DOCUMENT ME!
157    * 
158    * @return DOCUMENT ME!
159    */
160   public String getName()
161   {
162     return Name;
163   }
164
165   /**
166    * DOCUMENT ME!
167    * 
168    * @param Order
169    *          DOCUMENT ME!
170    */
171   public void setOrder(Vector Order)
172   {
173     this.Order = Order;
174   }
175
176   /**
177    * DOCUMENT ME!
178    * 
179    * @return DOCUMENT ME!
180    */
181   public Vector getOrder()
182   {
183     return Order;
184   }
185
186   /**
187    * replaces oldref with newref in the alignment order.
188    * 
189    * @param oldref
190    * @param newref
191    * @return true if oldref was contained in order and replaced with newref
192    */
193   public boolean updateSequence(SequenceI oldref, SequenceI newref)
194   {
195     int found = Order.indexOf(oldref);
196     if (found > -1)
197     {
198       Order.setElementAt(newref, found);
199     }
200     return found > -1;
201   }
202
203   /**
204    * Exact equivalence of two AlignmentOrders
205    * 
206    * @param o
207    * @return true if o orders the same sequenceI objects in the same way
208    */
209   public boolean equals(AlignmentOrder o)
210   {
211     return equals(o, true);
212   }
213
214   /**
215    * Exact equivalence of two AlignmentOrders // TODO: Weak SequenceI
216    * equivalence - will throw Error at moment
217    * 
218    * @param o
219    * @param identity
220    *          - false - use weak equivalence (refers to same or different parts
221    *          of same sequence)
222    * @return true if o orders equivalent sequenceI objects in the same way
223    */
224   public boolean equals(AlignmentOrder o, boolean identity)
225   {
226     if (o != this)
227     {
228       if (o == null)
229       {
230         return false;
231       }
232       if (Order != null && o.Order != null
233               && Order.size() == o.Order.size())
234       {
235         if (!identity)
236         {
237           throw new Error("Weak sequenceI equivalence not yet implemented.");
238         }
239         else
240         {
241           for (int i = 0, j = o.Order.size(); i < j; i++)
242           {
243             if (Order.elementAt(i) != o.Order.elementAt(i))
244             {
245               return false;
246             }
247           }
248         }
249       }
250       else
251       {
252         return false;
253       }
254     }
255     return true;
256   }
257
258   /**
259    * Consistency test for alignmentOrders
260    * 
261    * @param o
262    * @return true if o contains or is contained by this and the common SequenceI
263    *         objects are ordered in the same way
264    */
265   public boolean isConsistent(AlignmentOrder o)
266   {
267     return isConsistent(o, true);
268   }
269
270   /**
271    * Consistency test for alignmentOrders
272    * 
273    * @param o
274    *          // TODO: Weak SequenceI equivalence - will throw Error at moment
275    * @param identity
276    *          - false - use weak equivalence (refers to same or different parts
277    *          of same sequence)
278    * @return true if o contains or is contained by this and the common SequenceI
279    *         objects are ordered in the same way
280    */
281   public boolean isConsistent(AlignmentOrder o, boolean identity)
282   {
283     if (o != this)
284     {
285       if (o == null)
286       {
287         return false;
288       }
289       if (Order != null && o.Order != null)
290       {
291         Vector c, s;
292         if (o.Order.size() > Order.size())
293         {
294           c = o.Order;
295           s = Order;
296         }
297         else
298         {
299           c = Order;
300           s = o.Order;
301         }
302         if (!identity)
303         {
304           throw new Error("Weak sequenceI equivalence not yet implemented.");
305         }
306         else
307         {
308           // test if c contains s and order in s is conserved in c
309           int last = -1;
310           for (int i = 0, j = s.size(); i < j; i++)
311           {
312             int pos = c.indexOf(s.elementAt(i)); // JBPNote - optimize by
313             // incremental position search
314             if (pos > last)
315             {
316               last = pos;
317             }
318             else
319             {
320               return false;
321             }
322           }
323         }
324       }
325       else
326       {
327         return false;
328       }
329     }
330     return true;
331   }
332   /**
333    * AlignmentOrder
334    * 
335    * @param orderThis
336    *          AlignmentI
337    * @param byThat
338    *          AlignmentI
339    */
340
341   /*
342    * public AlignmentOrder(AlignmentI orderThis, AlignmentI byThat) { // Vector
343    * is an ordering of this alignment using the order of sequence objects in
344    * byThat, // where ids and unaligned sequences must match }
345    */
346 }