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