applied copyright 2008
[jalview.git] / src / jalview / datamodel / AlignmentOrder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.datamodel;
20
21 import java.util.*;
22
23 /**
24  * <p>Title: </p>
25  *
26  * <p>Description: </p>
27  *
28  * <p>Copyright: Copyright (c) 2004</p>
29  *
30  * <p>Company: Dundee University</p>
31  *
32  * @author not attributable
33  * @version 1.0
34  */
35 public class AlignmentOrder
36 {
37   // JBPNote : this method would return a vector containing all sequences in seqset
38   // with those also contained in order at the beginning of the vector in the order
39   // given by order. AlignmentSorter.vectorSubsetToArray already does this, but that method
40   // should be here for completeness.
41
42   /*  public Vector getOrder(AlignmentI seqset)
43      {
44        Vector perm = new Vector(seqset.getHeight());
45        for (i=0, o = 0, n=seqset.getHeight(), p = Order.size(); i<n; i++)
46     perm.setElement(i,...).
47        return Order;
48      }
49    */
50
51   /** DOCUMENT ME!! */
52   public static final int FILE = 0;
53
54   /** DOCUMENT ME!! */
55   public static final int MSA = 1;
56
57   /** DOCUMENT ME!! */
58   public static final int USER = 2;
59   private int Type = 0;
60   private String Name;
61   private Vector Order = null;
62
63   /**
64    * Creates a new AlignmentOrder object.
65    */
66   public AlignmentOrder()
67   {
68   }
69
70   /**
71    * AlignmentOrder
72    *
73    * @param anOrder Vector
74    */
75   public AlignmentOrder(Vector anOrder)
76   {
77     Order = anOrder;
78   }
79
80   /**
81    * AlignmentOrder
82    *
83    * @param orderFrom AlignmentI
84    */
85   public AlignmentOrder(AlignmentI orderFrom)
86   {
87     Order = new Vector();
88
89     for (int i = 0, ns = orderFrom.getHeight(); i < ns; i++)
90     {
91       Order.addElement(orderFrom.getSequenceAt(i));
92     }
93   }
94
95   /**
96    * Creates a new AlignmentOrder object.
97    *
98    * @param orderFrom DOCUMENT ME!
99    */
100   public AlignmentOrder(SequenceI[] orderFrom)
101   {
102     Order = new Vector();
103
104     for (int i = 0, ns = orderFrom.length; i < ns; i++)
105     {
106       Order.addElement(orderFrom[i]);
107     }
108   }
109
110   /**
111    * DOCUMENT ME!
112    *
113    * @param Type DOCUMENT ME!
114    */
115   public void setType(int Type)
116   {
117     this.Type = Type;
118   }
119
120   /**
121    * DOCUMENT ME!
122    *
123    * @return DOCUMENT ME!
124    */
125   public int getType()
126   {
127     return Type;
128   }
129
130   /**
131    * DOCUMENT ME!
132    *
133    * @param Name 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 DOCUMENT ME!
154    */
155   public void setOrder(Vector Order)
156   {
157     this.Order = Order;
158   }
159
160   /**
161    * DOCUMENT ME!
162    *
163    * @return DOCUMENT ME!
164    */
165   public Vector getOrder()
166   {
167     return Order;
168   }
169
170   /**
171    * replaces oldref with newref in the alignment order.
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    * @param o
189    * @return true if o orders the same sequenceI objects in the same way
190    */
191   public boolean equals(AlignmentOrder o)
192   {
193     return equals(o, true);
194   }
195
196   /**
197    * Exact equivalence of two AlignmentOrders
198    *  // TODO: Weak SequenceI equivalence - will throw Error at moment
199    * @param o
200    * @param identity - false - use weak equivalence (refers to same or different parts of same sequence)
201    * @return true if o orders equivalent sequenceI objects in the same way
202    */
203   public boolean equals(AlignmentOrder o, boolean identity)
204   {
205     if (o != this)
206     {
207       if (o == null)
208       {
209         return false;
210       }
211       if (Order != null && o.Order != null && Order.size() == o.Order.size())
212       {
213         if (!identity)
214         {
215           throw new Error("Weak sequenceI equivalence not yet implemented.");
216         }
217         else
218         {
219           for (int i = 0, j = o.Order.size(); i < j; i++)
220           {
221             if (Order.elementAt(i) != o.Order.elementAt(i))
222             {
223               return false;
224             }
225           }
226         }
227       }
228       else
229       {
230         return false;
231       }
232     }
233     return true;
234   }
235
236   /**
237    * Consistency test for alignmentOrders
238    * @param o
239    * @return true if o contains or is contained by this and the common SequenceI objects are ordered in the same way
240    */
241   public boolean isConsistent(AlignmentOrder o)
242   {
243     return isConsistent(o, true);
244   }
245
246   /**
247    * Consistency test for alignmentOrders
248    * @param o
249    *  // TODO: Weak SequenceI equivalence - will throw Error at moment
250    * @param identity - false - use weak equivalence (refers to same or different parts of same sequence)
251    * @return true if o contains or is contained by this and the common SequenceI objects are ordered in the same way
252    */
253   public boolean isConsistent(AlignmentOrder o, boolean identity)
254   {
255     if (o != this)
256     {
257       if (o == null)
258       {
259         return false;
260       }
261       if (Order != null && o.Order != null)
262       {
263         Vector c, s;
264         if (o.Order.size() > Order.size())
265         {
266           c = o.Order;
267           s = Order;
268         }
269         else
270         {
271           c = Order;
272           s = o.Order;
273         }
274         if (!identity)
275         {
276           throw new Error("Weak sequenceI equivalence not yet implemented.");
277         }
278         else
279         {
280           // test if c contains s and order in s is conserved in c
281           int last = -1;
282           for (int i = 0, j = s.size(); i < j; i++)
283           {
284             int pos = c.indexOf(s.elementAt(i)); // JBPNote - optimize by incremental position search
285             if (pos > last)
286             {
287               last = pos;
288             }
289             else
290             {
291               return false;
292             }
293           }
294         }
295       }
296       else
297       {
298         return false;
299       }
300     }
301     return true;
302   }
303   /**
304    * AlignmentOrder
305    *
306    * @param orderThis AlignmentI
307    * @param byThat AlignmentI
308    */
309
310   /* public AlignmentOrder(AlignmentI orderThis, AlignmentI byThat)
311    {
312      // Vector is an ordering of this alignment using the order of sequence objects in byThat,
313      // where ids and unaligned sequences must match
314
315    } */
316 }