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