e348f9a5d5b485d0ddbcc5c4a9d3013d691a076b
[jalview.git] / src / jalview / binding / SequenceSet.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.binding;
20
21 // ---------------------------------/
22 // - Imported classes and packages -/
23 // ---------------------------------/
24
25 import org.exolab.castor.xml.Marshaller;
26 import org.exolab.castor.xml.Unmarshaller;
27
28 /**
29  * Class SequenceSet.
30  * 
31  * @version $Revision$ $Date$
32  */
33 public class SequenceSet implements java.io.Serializable
34 {
35
36   // --------------------------/
37   // - Class/Member Variables -/
38   // --------------------------/
39
40   /**
41    * Field _gapChar.
42    */
43   private java.lang.String _gapChar;
44
45   /**
46    * Field _aligned.
47    */
48   private boolean _aligned;
49
50   /**
51    * keeps track of state for field: _aligned
52    */
53   private boolean _has_aligned;
54
55   /**
56    * Field _sequenceList.
57    */
58   private java.util.Vector _sequenceList;
59
60   /**
61    * Field _annotationList.
62    */
63   private java.util.Vector _annotationList;
64
65   // ----------------/
66   // - Constructors -/
67   // ----------------/
68
69   public SequenceSet()
70   {
71     super();
72     this._sequenceList = new java.util.Vector();
73     this._annotationList = new java.util.Vector();
74   }
75
76   // -----------/
77   // - Methods -/
78   // -----------/
79
80   /**
81    * 
82    * 
83    * @param vAnnotation
84    * @throws java.lang.IndexOutOfBoundsException
85    *                 if the index given is outside the bounds of the collection
86    */
87   public void addAnnotation(final jalview.binding.Annotation vAnnotation)
88           throws java.lang.IndexOutOfBoundsException
89   {
90     this._annotationList.addElement(vAnnotation);
91   }
92
93   /**
94    * 
95    * 
96    * @param index
97    * @param vAnnotation
98    * @throws java.lang.IndexOutOfBoundsException
99    *                 if the index given is outside the bounds of the collection
100    */
101   public void addAnnotation(final int index,
102           final jalview.binding.Annotation vAnnotation)
103           throws java.lang.IndexOutOfBoundsException
104   {
105     this._annotationList.add(index, vAnnotation);
106   }
107
108   /**
109    * 
110    * 
111    * @param vSequence
112    * @throws java.lang.IndexOutOfBoundsException
113    *                 if the index given is outside the bounds of the collection
114    */
115   public void addSequence(final jalview.binding.Sequence vSequence)
116           throws java.lang.IndexOutOfBoundsException
117   {
118     this._sequenceList.addElement(vSequence);
119   }
120
121   /**
122    * 
123    * 
124    * @param index
125    * @param vSequence
126    * @throws java.lang.IndexOutOfBoundsException
127    *                 if the index given is outside the bounds of the collection
128    */
129   public void addSequence(final int index,
130           final jalview.binding.Sequence vSequence)
131           throws java.lang.IndexOutOfBoundsException
132   {
133     this._sequenceList.add(index, vSequence);
134   }
135
136   /**
137    */
138   public void deleteAligned()
139   {
140     this._has_aligned = false;
141   }
142
143   /**
144    * Method enumerateAnnotation.
145    * 
146    * @return an Enumeration over all jalview.binding.Annotation elements
147    */
148   public java.util.Enumeration enumerateAnnotation()
149   {
150     return this._annotationList.elements();
151   }
152
153   /**
154    * Method enumerateSequence.
155    * 
156    * @return an Enumeration over all jalview.binding.Sequence elements
157    */
158   public java.util.Enumeration enumerateSequence()
159   {
160     return this._sequenceList.elements();
161   }
162
163   /**
164    * Returns the value of field 'aligned'.
165    * 
166    * @return the value of field 'Aligned'.
167    */
168   public boolean getAligned()
169   {
170     return this._aligned;
171   }
172
173   /**
174    * Method getAnnotation.
175    * 
176    * @param index
177    * @throws java.lang.IndexOutOfBoundsException
178    *                 if the index given is outside the bounds of the collection
179    * @return the value of the jalview.binding.Annotation at the given index
180    */
181   public jalview.binding.Annotation getAnnotation(final int index)
182           throws java.lang.IndexOutOfBoundsException
183   {
184     // check bounds for index
185     if (index < 0 || index >= this._annotationList.size())
186     {
187       throw new IndexOutOfBoundsException("getAnnotation: Index value '"
188               + index + "' not in range [0.."
189               + (this._annotationList.size() - 1) + "]");
190     }
191
192     return (jalview.binding.Annotation) _annotationList.get(index);
193   }
194
195   /**
196    * Method getAnnotation.Returns the contents of the collection in an Array.
197    * <p>
198    * Note: Just in case the collection contents are changing in another thread,
199    * we pass a 0-length Array of the correct type into the API call. This way we
200    * <i>know</i> that the Array returned is of exactly the correct length.
201    * 
202    * @return this collection as an Array
203    */
204   public jalview.binding.Annotation[] getAnnotation()
205   {
206     jalview.binding.Annotation[] array = new jalview.binding.Annotation[0];
207     return (jalview.binding.Annotation[]) this._annotationList
208             .toArray(array);
209   }
210
211   /**
212    * Method getAnnotationCount.
213    * 
214    * @return the size of this collection
215    */
216   public int getAnnotationCount()
217   {
218     return this._annotationList.size();
219   }
220
221   /**
222    * Returns the value of field 'gapChar'.
223    * 
224    * @return the value of field 'GapChar'.
225    */
226   public java.lang.String getGapChar()
227   {
228     return this._gapChar;
229   }
230
231   /**
232    * Method getSequence.
233    * 
234    * @param index
235    * @throws java.lang.IndexOutOfBoundsException
236    *                 if the index given is outside the bounds of the collection
237    * @return the value of the jalview.binding.Sequence at the given index
238    */
239   public jalview.binding.Sequence getSequence(final int index)
240           throws java.lang.IndexOutOfBoundsException
241   {
242     // check bounds for index
243     if (index < 0 || index >= this._sequenceList.size())
244     {
245       throw new IndexOutOfBoundsException("getSequence: Index value '"
246               + index + "' not in range [0.."
247               + (this._sequenceList.size() - 1) + "]");
248     }
249
250     return (jalview.binding.Sequence) _sequenceList.get(index);
251   }
252
253   /**
254    * Method getSequence.Returns the contents of the collection in an Array.
255    * <p>
256    * Note: Just in case the collection contents are changing in another thread,
257    * we pass a 0-length Array of the correct type into the API call. This way we
258    * <i>know</i> that the Array returned is of exactly the correct length.
259    * 
260    * @return this collection as an Array
261    */
262   public jalview.binding.Sequence[] getSequence()
263   {
264     jalview.binding.Sequence[] array = new jalview.binding.Sequence[0];
265     return (jalview.binding.Sequence[]) this._sequenceList.toArray(array);
266   }
267
268   /**
269    * Method getSequenceCount.
270    * 
271    * @return the size of this collection
272    */
273   public int getSequenceCount()
274   {
275     return this._sequenceList.size();
276   }
277
278   /**
279    * Method hasAligned.
280    * 
281    * @return true if at least one Aligned has been added
282    */
283   public boolean hasAligned()
284   {
285     return this._has_aligned;
286   }
287
288   /**
289    * Returns the value of field 'aligned'.
290    * 
291    * @return the value of field 'Aligned'.
292    */
293   public boolean isAligned()
294   {
295     return this._aligned;
296   }
297
298   /**
299    * Method isValid.
300    * 
301    * @return true if this object is valid according to the schema
302    */
303   public boolean isValid()
304   {
305     try
306     {
307       validate();
308     } catch (org.exolab.castor.xml.ValidationException vex)
309     {
310       return false;
311     }
312     return true;
313   }
314
315   /**
316    * 
317    * 
318    * @param out
319    * @throws org.exolab.castor.xml.MarshalException
320    *                 if object is null or if any SAXException is thrown during
321    *                 marshaling
322    * @throws org.exolab.castor.xml.ValidationException
323    *                 if this object is an invalid instance according to the
324    *                 schema
325    */
326   public void marshal(final java.io.Writer out)
327           throws org.exolab.castor.xml.MarshalException,
328           org.exolab.castor.xml.ValidationException
329   {
330     Marshaller.marshal(this, out);
331   }
332
333   /**
334    * 
335    * 
336    * @param handler
337    * @throws java.io.IOException
338    *                 if an IOException occurs during marshaling
339    * @throws org.exolab.castor.xml.ValidationException
340    *                 if this object is an invalid instance according to the
341    *                 schema
342    * @throws org.exolab.castor.xml.MarshalException
343    *                 if object is null or if any SAXException is thrown during
344    *                 marshaling
345    */
346   public void marshal(final org.xml.sax.ContentHandler handler)
347           throws java.io.IOException,
348           org.exolab.castor.xml.MarshalException,
349           org.exolab.castor.xml.ValidationException
350   {
351     Marshaller.marshal(this, handler);
352   }
353
354   /**
355    */
356   public void removeAllAnnotation()
357   {
358     this._annotationList.clear();
359   }
360
361   /**
362    */
363   public void removeAllSequence()
364   {
365     this._sequenceList.clear();
366   }
367
368   /**
369    * Method removeAnnotation.
370    * 
371    * @param vAnnotation
372    * @return true if the object was removed from the collection.
373    */
374   public boolean removeAnnotation(
375           final jalview.binding.Annotation vAnnotation)
376   {
377     boolean removed = _annotationList.remove(vAnnotation);
378     return removed;
379   }
380
381   /**
382    * Method removeAnnotationAt.
383    * 
384    * @param index
385    * @return the element removed from the collection
386    */
387   public jalview.binding.Annotation removeAnnotationAt(final int index)
388   {
389     java.lang.Object obj = this._annotationList.remove(index);
390     return (jalview.binding.Annotation) obj;
391   }
392
393   /**
394    * Method removeSequence.
395    * 
396    * @param vSequence
397    * @return true if the object was removed from the collection.
398    */
399   public boolean removeSequence(final jalview.binding.Sequence vSequence)
400   {
401     boolean removed = _sequenceList.remove(vSequence);
402     return removed;
403   }
404
405   /**
406    * Method removeSequenceAt.
407    * 
408    * @param index
409    * @return the element removed from the collection
410    */
411   public jalview.binding.Sequence removeSequenceAt(final int index)
412   {
413     java.lang.Object obj = this._sequenceList.remove(index);
414     return (jalview.binding.Sequence) obj;
415   }
416
417   /**
418    * Sets the value of field 'aligned'.
419    * 
420    * @param aligned
421    *                the value of field 'aligned'.
422    */
423   public void setAligned(final boolean aligned)
424   {
425     this._aligned = aligned;
426     this._has_aligned = true;
427   }
428
429   /**
430    * 
431    * 
432    * @param index
433    * @param vAnnotation
434    * @throws java.lang.IndexOutOfBoundsException
435    *                 if the index given is outside the bounds of the collection
436    */
437   public void setAnnotation(final int index,
438           final jalview.binding.Annotation vAnnotation)
439           throws java.lang.IndexOutOfBoundsException
440   {
441     // check bounds for index
442     if (index < 0 || index >= this._annotationList.size())
443     {
444       throw new IndexOutOfBoundsException("setAnnotation: Index value '"
445               + index + "' not in range [0.."
446               + (this._annotationList.size() - 1) + "]");
447     }
448
449     this._annotationList.set(index, vAnnotation);
450   }
451
452   /**
453    * 
454    * 
455    * @param vAnnotationArray
456    */
457   public void setAnnotation(
458           final jalview.binding.Annotation[] vAnnotationArray)
459   {
460     // -- copy array
461     _annotationList.clear();
462
463     for (int i = 0; i < vAnnotationArray.length; i++)
464     {
465       this._annotationList.add(vAnnotationArray[i]);
466     }
467   }
468
469   /**
470    * Sets the value of field 'gapChar'.
471    * 
472    * @param gapChar
473    *                the value of field 'gapChar'.
474    */
475   public void setGapChar(final java.lang.String gapChar)
476   {
477     this._gapChar = gapChar;
478   }
479
480   /**
481    * 
482    * 
483    * @param index
484    * @param vSequence
485    * @throws java.lang.IndexOutOfBoundsException
486    *                 if the index given is outside the bounds of the collection
487    */
488   public void setSequence(final int index,
489           final jalview.binding.Sequence vSequence)
490           throws java.lang.IndexOutOfBoundsException
491   {
492     // check bounds for index
493     if (index < 0 || index >= this._sequenceList.size())
494     {
495       throw new IndexOutOfBoundsException("setSequence: Index value '"
496               + index + "' not in range [0.."
497               + (this._sequenceList.size() - 1) + "]");
498     }
499
500     this._sequenceList.set(index, vSequence);
501   }
502
503   /**
504    * 
505    * 
506    * @param vSequenceArray
507    */
508   public void setSequence(final jalview.binding.Sequence[] vSequenceArray)
509   {
510     // -- copy array
511     _sequenceList.clear();
512
513     for (int i = 0; i < vSequenceArray.length; i++)
514     {
515       this._sequenceList.add(vSequenceArray[i]);
516     }
517   }
518
519   /**
520    * Method unmarshal.
521    * 
522    * @param reader
523    * @throws org.exolab.castor.xml.MarshalException
524    *                 if object is null or if any SAXException is thrown during
525    *                 marshaling
526    * @throws org.exolab.castor.xml.ValidationException
527    *                 if this object is an invalid instance according to the
528    *                 schema
529    * @return the unmarshaled jalview.binding.SequenceSet
530    */
531   public static jalview.binding.SequenceSet unmarshal(
532           final java.io.Reader reader)
533           throws org.exolab.castor.xml.MarshalException,
534           org.exolab.castor.xml.ValidationException
535   {
536     return (jalview.binding.SequenceSet) Unmarshaller.unmarshal(
537             jalview.binding.SequenceSet.class, reader);
538   }
539
540   /**
541    * 
542    * 
543    * @throws org.exolab.castor.xml.ValidationException
544    *                 if this object is an invalid instance according to the
545    *                 schema
546    */
547   public void validate() throws org.exolab.castor.xml.ValidationException
548   {
549     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
550     validator.validate(this);
551   }
552
553 }