JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / datamodel / AlignmentOrder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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("Weak sequenceI equivalence not yet implemented.");
222         }
223         else
224         {
225           for (int i = 0, j = o.Order.size(); i < j; i++)
226           {
227             if (Order.get(i) != o.Order.get(i))
228             {
229               return false;
230             }
231           }
232         }
233       }
234       else
235       {
236         return false;
237       }
238     }
239     return true;
240   }
241
242   /**
243    * Consistency test for alignmentOrders
244    * 
245    * @param o
246    * @return true if o contains or is contained by this and the common SequenceI
247    *         objects are ordered in the same way
248    */
249   public boolean isConsistent(AlignmentOrder o)
250   {
251     return isConsistent(o, true);
252   }
253
254   /**
255    * Consistency test for alignmentOrders
256    * 
257    * @param o
258    *          // TODO: Weak SequenceI equivalence - will throw Error at moment
259    * @param identity
260    *          - false - use weak equivalence (refers to same or different parts
261    *          of same sequence)
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, boolean identity)
266   {
267     if (o != this)
268     {
269       if (o == null)
270       {
271         return false;
272       }
273       if (Order != null && o.Order != null)
274       {
275         List<SequenceI> c, s;
276         if (o.Order.size() > Order.size())
277         {
278           c = o.Order;
279           s = Order;
280         }
281         else
282         {
283           c = Order;
284           s = o.Order;
285         }
286         if (!identity)
287         {
288           throw new Error("Weak sequenceI equivalence not yet implemented.");
289         }
290         else
291         {
292           // test if c contains s and order in s is conserved in c
293           int last = -1;
294           for (int i = 0, j = s.size(); i < j; i++)
295           {
296             int pos = c.indexOf(s.get(i)); // JBPNote - optimize by
297             // incremental position search
298             if (pos > last)
299             {
300               last = pos;
301             }
302             else
303             {
304               return false;
305             }
306           }
307         }
308       }
309       else
310       {
311         return false;
312       }
313     }
314     return true;
315   }
316   /**
317    * AlignmentOrder
318    * 
319    * @param orderThis
320    *          AlignmentI
321    * @param byThat
322    *          AlignmentI
323    */
324
325   /*
326    * public AlignmentOrder(AlignmentI orderThis, AlignmentI byThat) { // Vector
327    * is an ordering of this alignment using the order of sequence objects in
328    * byThat, // where ids and unaligned sequences must match }
329    */
330 }