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