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