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