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