JAL-1355
[jalview.git] / src / jalview / datamodel / AlignmentOrder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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 jalview.util.MessageManager;
24
25 import java.util.*;
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 Vector 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    *          Vector
70    */
71   public AlignmentOrder(Vector anOrder)
72   {
73     Order = anOrder;
74   }
75
76   /**
77    * AlignmentOrder
78    * 
79    * @param orderFrom
80    *          AlignmentI
81    */
82   public AlignmentOrder(AlignmentI orderFrom)
83   {
84     Order = new Vector();
85
86     for (int i = 0, ns = orderFrom.getHeight(); i < ns; i++)
87     {
88       Order.addElement(orderFrom.getSequenceAt(i));
89     }
90   }
91
92   /**
93    * Creates a new AlignmentOrder object.
94    * 
95    * @param orderFrom
96    *          DOCUMENT ME!
97    */
98   public AlignmentOrder(SequenceI[] orderFrom)
99   {
100     Order = new Vector();
101
102     for (int i = 0, ns = orderFrom.length; i < ns; i++)
103     {
104       Order.addElement(orderFrom[i]);
105     }
106   }
107
108   /**
109    * DOCUMENT ME!
110    * 
111    * @param Type
112    *          DOCUMENT ME!
113    */
114   public void setType(int Type)
115   {
116     this.Type = Type;
117   }
118
119   /**
120    * DOCUMENT ME!
121    * 
122    * @return DOCUMENT ME!
123    */
124   public int getType()
125   {
126     return Type;
127   }
128
129   /**
130    * DOCUMENT ME!
131    * 
132    * @param Name
133    *          DOCUMENT ME!
134    */
135   public void setName(String Name)
136   {
137     this.Name = Name;
138   }
139
140   /**
141    * DOCUMENT ME!
142    * 
143    * @return DOCUMENT ME!
144    */
145   public String getName()
146   {
147     return Name;
148   }
149
150   /**
151    * DOCUMENT ME!
152    * 
153    * @param Order
154    *          DOCUMENT ME!
155    */
156   public void setOrder(Vector Order)
157   {
158     this.Order = Order;
159   }
160
161   /**
162    * DOCUMENT ME!
163    * 
164    * @return DOCUMENT ME!
165    */
166   public Vector getOrder()
167   {
168     return Order;
169   }
170
171   /**
172    * replaces oldref with newref in the alignment order.
173    * 
174    * @param oldref
175    * @param newref
176    * @return true if oldref was contained in order and replaced with newref
177    */
178   public boolean updateSequence(SequenceI oldref, SequenceI newref)
179   {
180     int found = Order.indexOf(oldref);
181     if (found > -1)
182     {
183       Order.setElementAt(newref, found);
184     }
185     return found > -1;
186   }
187
188   /**
189    * Exact equivalence of two AlignmentOrders
190    * 
191    * @param o
192    * @return true if o orders the same sequenceI objects in the same way
193    */
194   public boolean equals(AlignmentOrder o)
195   {
196     return equals(o, true);
197   }
198
199   /**
200    * Exact equivalence of two AlignmentOrders // TODO: Weak SequenceI
201    * equivalence - will throw Error at moment
202    * 
203    * @param o
204    * @param identity
205    *          - false - use weak equivalence (refers to same or different parts
206    *          of same sequence)
207    * @return true if o orders equivalent sequenceI objects in the same way
208    */
209   public boolean equals(AlignmentOrder o, boolean identity)
210   {
211     if (o != this)
212     {
213       if (o == null)
214       {
215         return false;
216       }
217       if (Order != null && o.Order != null
218               && Order.size() == o.Order.size())
219       {
220         if (!identity)
221         {
222           throw new Error(MessageManager.getString("error.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.elementAt(i) != o.Order.elementAt(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         Vector 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(MessageManager.getString("error.weak_sequencei_equivalence_not_yet_implemented"));
290         }
291         else
292         {
293           // test if c contains s and order in s is conserved in c
294           int last = -1;
295           for (int i = 0, j = s.size(); i < j; i++)
296           {
297             int pos = c.indexOf(s.elementAt(i)); // JBPNote - optimize by
298             // incremental position search
299             if (pos > last)
300             {
301               last = pos;
302             }
303             else
304             {
305               return false;
306             }
307           }
308         }
309       }
310       else
311       {
312         return false;
313       }
314     }
315     return true;
316   }
317   /**
318    * AlignmentOrder
319    * 
320    * @param orderThis
321    *          AlignmentI
322    * @param byThat
323    *          AlignmentI
324    */
325
326   /*
327    * public AlignmentOrder(AlignmentI orderThis, AlignmentI byThat) { // Vector
328    * is an ordering of this alignment using the order of sequence objects in
329    * byThat, // where ids and unaligned sequences must match }
330    */
331 }