a5c10940000c1a182b7aaa8bd7cbebbb77c47b47
[jalview.git] / src / jalview / datamodel / AlignmentOrder.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2005 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 /**
25  * <p>Title: </p>
26  *
27  * <p>Description: </p>
28  *
29  * <p>Copyright: Copyright (c) 2004</p>
30  *
31  * <p>Company: Dundee University</p>
32  *
33  * @author not attributable
34  * @version 1.0
35  */
36 public class AlignmentOrder
37 {
38     // JBPNote : this method would return a vector containing all sequences in seqset
39     // with those also contained in order at the beginning of the vector in the order
40     // given by order. AlignmentSorter.vectorSubsetToArray already does this, but that method
41     // should be here for completeness.
42
43     /*  public Vector getOrder(AlignmentI seqset)
44        {
45          Vector perm = new Vector(seqset.getHeight());
46          for (i=0, o = 0, n=seqset.getHeight(), p = Order.size(); i<n; i++)
47       perm.setElement(i,...).
48          return Order;
49        }
50      */
51
52     /** DOCUMENT ME!! */
53     public static final int FILE = 0;
54
55     /** DOCUMENT ME!! */
56     public static final int MSA = 1;
57
58     /** DOCUMENT ME!! */
59     public static final int USER = 2;
60     private int Type = 0;
61     private String Name;
62     private Vector Order = null;
63
64     /**
65      * Creates a new AlignmentOrder object.
66      */
67     public AlignmentOrder()
68     {
69     }
70
71     /**
72      * AlignmentOrder
73      *
74      * @param anOrder Vector
75      */
76     public AlignmentOrder(Vector anOrder)
77     {
78         Order = anOrder;
79     }
80
81     /**
82      * AlignmentOrder
83      *
84      * @param orderFrom AlignmentI
85      */
86     public AlignmentOrder(AlignmentI orderFrom)
87     {
88         Order = new Vector();
89
90         for (int i = 0, ns = orderFrom.getHeight(); i < ns; i++)
91         {
92             Order.addElement(orderFrom.getSequenceAt(i));
93         }
94     }
95
96     /**
97      * Creates a new AlignmentOrder object.
98      *
99      * @param orderFrom DOCUMENT ME!
100      */
101     public AlignmentOrder(SequenceI[] orderFrom)
102     {
103         Order = new Vector();
104
105         for (int i = 0, ns = orderFrom.length; i < ns; i++)
106         {
107             Order.addElement(orderFrom[i]);
108         }
109     }
110
111     /**
112      * DOCUMENT ME!
113      *
114      * @param Type DOCUMENT ME!
115      */
116     public void setType(int Type)
117     {
118         this.Type = Type;
119     }
120
121     /**
122      * DOCUMENT ME!
123      *
124      * @return DOCUMENT ME!
125      */
126     public int getType()
127     {
128         return Type;
129     }
130
131     /**
132      * DOCUMENT ME!
133      *
134      * @param Name DOCUMENT ME!
135      */
136     public void setName(String Name)
137     {
138         this.Name = Name;
139     }
140
141     /**
142      * DOCUMENT ME!
143      *
144      * @return DOCUMENT ME!
145      */
146     public String getName()
147     {
148         return Name;
149     }
150
151     /**
152      * DOCUMENT ME!
153      *
154      * @param Order 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      * replaces oldref with newref in the alignment order.
172      * @param oldref
173      * @param newref
174      * @return
175      */
176     public boolean updateSequence(SequenceI oldref, SequenceI newref) {
177       int found=Order.indexOf(oldref);
178       if (found>-1) {
179         Order.setElementAt(newref, found);
180       }
181       return found>-1;
182     }
183     /**
184      * Exact equivalence of two AlignmentOrders
185      * @param o
186      * @return true if o orders the same sequenceI objects in the same way
187      */
188     public boolean equals(AlignmentOrder o) {
189       return equals(o, true);
190     }
191     /**
192      * Exact equivalence of two AlignmentOrders
193      *  // TODO: Weak SequenceI equivalence - will throw Error at moment
194      * @param o
195      * @param identity - false - use weak equivalence (refers to same or different parts of same sequence)
196      * @return true if o orders equivalent sequenceI objects in the same way
197      */
198     public boolean equals(AlignmentOrder o, boolean identity) {
199       if (o!=this) {
200         if (o==null)
201           return false;
202         if (Order!=null && o.Order!=null && Order.size()==o.Order.size()) {
203           if (!identity) {
204             throw new Error("Weak sequenceI equivalence not yet implemented."); 
205           } else {
206             for (int i=0,j=o.Order.size(); i<j; i++) {
207             if (Order.get(i)!=o.Order.get(i))
208               return false;
209             }
210           }
211         } else
212           return false;
213       }
214       return true;
215     }
216     /**
217      * Consistency test for alignmentOrders
218      * @param o
219      * @return true if o contains or is contained by this and the common SequenceI objects are ordered in the same way
220      */
221     public boolean isConsistent(AlignmentOrder o) {
222       return isConsistent(o, true);
223     }
224     /**
225      * Consistency test for alignmentOrders
226      * @param o
227      *  // TODO: Weak SequenceI equivalence - will throw Error at moment
228      * @param identity - false - use weak equivalence (refers to same or different parts of same sequence)
229      * @return true if o contains or is contained by this and the common SequenceI objects are ordered in the same way
230      */
231     public boolean isConsistent(AlignmentOrder o, boolean identity) {
232       if (o!=this) {
233         if (o==null)
234           return false;
235         if (Order!=null && o.Order!=null) {
236           Vector c,s;
237           if (o.Order.size()>Order.size()) {
238             c = o.Order;
239             s = Order;
240           } else {
241             c = Order;
242             s = o.Order;
243           }
244           if (!identity) {
245             throw new Error("Weak sequenceI equivalence not yet implemented.");  
246           } else {
247             // test if c contains s and order in s is conserved in c
248             int last=-1;
249             for (int i=0,j=s.size(); i<j; i++) {
250               int pos=c.indexOf(s.get(i)); // JBPNote - optimize by incremental position search
251               if (pos>last) {
252                 last=pos;
253               } else
254                 return false;
255             }
256           }
257         } else
258           return false;
259       }
260       return true;
261     }
262     /**
263      * AlignmentOrder
264      *
265      * @param orderThis AlignmentI
266      * @param byThat AlignmentI
267      */
268
269     /* public AlignmentOrder(AlignmentI orderThis, AlignmentI byThat)
270      {
271        // Vector is an ordering of this alignment using the order of sequence objects in byThat,
272        // where ids and unaligned sequences must match
273
274      } */
275 }