21b877d4083609affa599393782ab5f92ad741a6
[jalview.git] / src / jalview / datamodel / Sequence.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 jalview.analysis.AlignSeq;
21
22 import java.util.Enumeration;
23 import java.util.Vector;
24
25 /**
26  * 
27  * Implements the SequenceI interface for a char[] based sequence object.
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class Sequence implements SequenceI
33 {
34   SequenceI datasetSequence;
35
36   String name;
37
38   private char[] sequence;
39
40   String description;
41
42   int start;
43
44   int end;
45
46   Vector pdbIds;
47
48   String vamsasId;
49
50   DBRefEntry[] dbrefs;
51
52   /**
53    * This annotation is displayed below the alignment but the positions are tied
54    * to the residues of this sequence
55    */
56   Vector annotation;
57   
58   /**
59    * The index of the sequence in a MSA 
60    */
61   int index = -1;
62
63   /** array of seuqence features - may not be null for a valid sequence object */
64   public SequenceFeature[] sequenceFeatures;
65
66   /**
67    * Creates a new Sequence object.
68    * 
69    * @param name
70    *          display name string
71    * @param sequence
72    *          string to form a possibly gapped sequence out of
73    * @param start
74    *          first position of non-gap residue in the sequence
75    * @param end
76    *          last position of ungapped residues (nearly always only used for
77    *          display purposes)
78    */
79   public Sequence(String name, String sequence, int start, int end)
80   {
81     this.name = name;
82     this.sequence = sequence.toCharArray();
83     this.start = start;
84     this.end = end;
85     parseId();
86     checkValidRange();
87   }
88
89   public Sequence(String name, char[] sequence, int start, int end)
90   {
91     this.name = name;
92     this.sequence = sequence;
93     this.start = start;
94     this.end = end;
95     parseId();
96     checkValidRange();
97   }
98
99   com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
100           "[/][0-9]{1,}[-][0-9]{1,}$");
101
102   com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
103
104   void parseId()
105   {
106     if (name == null)
107     {
108       System.err
109               .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
110       name = "";
111     }
112     // Does sequence have the /start-end signiature?
113     if (limitrx.search(name))
114     {
115       name = limitrx.left();
116       endrx.search(limitrx.stringMatched());
117       setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
118               endrx.matchedFrom() - 1)));
119       setEnd(Integer.parseInt(endrx.stringMatched()));
120     }
121   }
122
123   void checkValidRange()
124   {
125     // Note: JAL-774 : http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
126     {
127       int endRes = 0;
128       for (int j = 0; j < sequence.length; j++)
129       {
130         if (!jalview.util.Comparison.isGap(sequence[j]))
131         {
132           endRes++;
133         }
134       }
135       if (endRes > 0)
136       {
137         endRes += start - 1;
138       }
139
140       if (end<endRes) { 
141         end = endRes;
142       }
143     }
144
145   }
146
147   /**
148    * Creates a new Sequence object.
149    * 
150    * @param name
151    *          DOCUMENT ME!
152    * @param sequence
153    *          DOCUMENT ME!
154    */
155   public Sequence(String name, String sequence)
156   {
157     this(name, sequence, 1, -1);
158   }
159
160   /**
161    * Creates a new Sequence object with new features, DBRefEntries,
162    * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
163    * reference.
164    * 
165    * @param seq
166    *          DOCUMENT ME!
167    */
168   public Sequence(SequenceI seq)
169   {
170     this(seq, seq.getAnnotation());
171   }
172
173   /**
174    * Create a new sequence object with new features, DBRefEntries, and PDBIds
175    * but inherits any existing dataset sequence reference, and duplicate of any
176    * annotation that is present in the given annotation array.
177    * 
178    * @param seq
179    *          the sequence to be copied
180    * @param alAnnotation
181    *          an array of annotation including some associated with seq
182    */
183   public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
184   {
185     this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
186     description = seq.getDescription();
187     if (seq.getSequenceFeatures() != null)
188     {
189       SequenceFeature[] sf = seq.getSequenceFeatures();
190       for (int i = 0; i < sf.length; i++)
191       {
192         addSequenceFeature(new SequenceFeature(sf[i]));
193       }
194     }
195     setDatasetSequence(seq.getDatasetSequence());
196     if (datasetSequence == null && seq.getDBRef() != null)
197     {
198       // only copy DBRefs if we really are a dataset sequence
199       DBRefEntry[] dbr = seq.getDBRef();
200       for (int i = 0; i < dbr.length; i++)
201       {
202         addDBRef(new DBRefEntry(dbr[i]));
203       }
204     }
205     if (seq.getAnnotation() != null)
206     {
207       AlignmentAnnotation[] sqann = seq.getAnnotation();
208       for (int i = 0; i < sqann.length; i++)
209       {
210         if (sqann[i] == null)
211         {
212           continue;
213         }
214         boolean found = (alAnnotation == null);
215         if (!found)
216         {
217           for (int apos = 0; !found && apos < alAnnotation.length; apos++)
218           {
219             found = (alAnnotation[apos] == sqann[i]);
220           }
221         }
222         if (found)
223         {
224           // only copy the given annotation
225           AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
226           addAlignmentAnnotation(newann);
227         }
228       }
229     }
230     if (seq.getPDBId() != null)
231     {
232       Vector ids = seq.getPDBId();
233       Enumeration e = ids.elements();
234       while (e.hasMoreElements())
235       {
236         this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
237       }
238     }
239   }
240
241   /**
242    * DOCUMENT ME!
243    * 
244    * @param v
245    *          DOCUMENT ME!
246    */
247   public void setSequenceFeatures(SequenceFeature[] features)
248   {
249     sequenceFeatures = features;
250   }
251
252   public synchronized void addSequenceFeature(SequenceFeature sf)
253   {
254     if (sequenceFeatures == null)
255     {
256       sequenceFeatures = new SequenceFeature[0];
257     }
258
259     for (int i = 0; i < sequenceFeatures.length; i++)
260     {
261       if (sequenceFeatures[i].equals(sf))
262       {
263         return;
264       }
265     }
266
267     SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
268     System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
269     temp[sequenceFeatures.length] = sf;
270
271     sequenceFeatures = temp;
272   }
273
274   public void deleteFeature(SequenceFeature sf)
275   {
276     if (sequenceFeatures == null)
277     {
278       return;
279     }
280
281     int index = 0;
282     for (index = 0; index < sequenceFeatures.length; index++)
283     {
284       if (sequenceFeatures[index].equals(sf))
285       {
286         break;
287       }
288     }
289
290     if (index == sequenceFeatures.length)
291     {
292       return;
293     }
294
295     int sfLength = sequenceFeatures.length;
296     if (sfLength < 2)
297     {
298       sequenceFeatures = null;
299     }
300     else
301     {
302       SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
303       System.arraycopy(sequenceFeatures, 0, temp, 0, index);
304
305       if (index < sfLength)
306       {
307         System.arraycopy(sequenceFeatures, index + 1, temp, index,
308                 sequenceFeatures.length - index - 1);
309       }
310
311       sequenceFeatures = temp;
312     }
313   }
314
315   /**
316    * DOCUMENT ME!
317    * 
318    * @return DOCUMENT ME!
319    */
320   public SequenceFeature[] getSequenceFeatures()
321   {
322     return sequenceFeatures;
323   }
324
325   public void addPDBId(PDBEntry entry)
326   {
327     if (pdbIds == null)
328     {
329       pdbIds = new Vector();
330     }
331     if (!pdbIds.contains(entry))
332     {
333       pdbIds.addElement(entry);
334     }
335   }
336
337   /**
338    * DOCUMENT ME!
339    * 
340    * @param id
341    *          DOCUMENT ME!
342    */
343   public void setPDBId(Vector id)
344   {
345     pdbIds = id;
346   }
347
348   /**
349    * DOCUMENT ME!
350    * 
351    * @return DOCUMENT ME!
352    */
353   public Vector getPDBId()
354   {
355     return pdbIds;
356   }
357
358   /**
359    * DOCUMENT ME!
360    * 
361    * @return DOCUMENT ME!
362    */
363   public String getDisplayId(boolean jvsuffix)
364   {
365     StringBuffer result = new StringBuffer(name);
366     if (jvsuffix)
367     {
368       result.append("/" + start + "-" + end);
369     }
370
371     return result.toString();
372   }
373
374   /**
375    * DOCUMENT ME!
376    * 
377    * @param name
378    *          DOCUMENT ME!
379    */
380   public void setName(String name)
381   {
382     this.name = name;
383     this.parseId();
384   }
385
386   /**
387    * DOCUMENT ME!
388    * 
389    * @return DOCUMENT ME!
390    */
391   public String getName()
392   {
393     return this.name;
394   }
395
396   /**
397    * DOCUMENT ME!
398    * 
399    * @param start
400    *          DOCUMENT ME!
401    */
402   public void setStart(int start)
403   {
404     this.start = start;
405   }
406
407   /**
408    * DOCUMENT ME!
409    * 
410    * @return DOCUMENT ME!
411    */
412   public int getStart()
413   {
414     return this.start;
415   }
416
417   /**
418    * DOCUMENT ME!
419    * 
420    * @param end
421    *          DOCUMENT ME!
422    */
423   public void setEnd(int end)
424   {
425     this.end = end;
426   }
427
428   /**
429    * DOCUMENT ME!
430    * 
431    * @return DOCUMENT ME!
432    */
433   public int getEnd()
434   {
435     return this.end;
436   }
437
438   /**
439    * DOCUMENT ME!
440    * 
441    * @return DOCUMENT ME!
442    */
443   public int getLength()
444   {
445     return this.sequence.length;
446   }
447
448   /**
449    * DOCUMENT ME!
450    * 
451    * @param seq
452    *          DOCUMENT ME!
453    */
454   public void setSequence(String seq)
455   {
456     this.sequence = seq.toCharArray();
457     checkValidRange();
458   }
459
460   public String getSequenceAsString()
461   {
462     return new String(sequence);
463   }
464
465   public String getSequenceAsString(int start, int end)
466   {
467     return new String(getSequence(start, end));
468   }
469
470   public char[] getSequence()
471   {
472     return sequence;
473   }
474
475   /*
476    * (non-Javadoc)
477    * 
478    * @see jalview.datamodel.SequenceI#getSequence(int, int)
479    */
480   public char[] getSequence(int start, int end)
481   {
482     if (start < 0)
483       start = 0;
484     // JBPNote - left to user to pad the result here (TODO:Decide on this
485     // policy)
486     if (start >= sequence.length)
487     {
488       return new char[0];
489     }
490
491     if (end >= sequence.length)
492     {
493       end = sequence.length;
494     }
495
496     char[] reply = new char[end - start];
497     System.arraycopy(sequence, start, reply, 0, end - start);
498
499     return reply;
500   }
501
502   /**
503    * make a new Sequence object from start to end (including gaps) over this
504    * seqeunce
505    * 
506    * @param start
507    *          int
508    * @param end
509    *          int
510    * @return SequenceI
511    */
512   public SequenceI getSubSequence(int start, int end)
513   {
514     if (start < 0)
515     {
516       start = 0;
517     }
518     char[] seq = getSequence(start, end);
519     if (seq.length == 0)
520     {
521       return null;
522     }
523     int nstart = findPosition(start);
524     int nend = findPosition(end) - 1;
525     // JBPNote - this is an incomplete copy.
526     SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
527     nseq.setDescription(description);
528     if (datasetSequence != null)
529     {
530       nseq.setDatasetSequence(datasetSequence);
531     }
532     else
533     {
534       nseq.setDatasetSequence(this);
535     }
536     return nseq;
537   }
538
539   /**
540    * DOCUMENT ME!
541    * 
542    * @param i
543    *          DOCUMENT ME!
544    * 
545    * @return DOCUMENT ME!
546    */
547   public char getCharAt(int i)
548   {
549     if (i < sequence.length)
550     {
551       return sequence[i];
552     }
553     else
554     {
555       return ' ';
556     }
557   }
558
559   /**
560    * DOCUMENT ME!
561    * 
562    * @param desc
563    *          DOCUMENT ME!
564    */
565   public void setDescription(String desc)
566   {
567     this.description = desc;
568   }
569
570   /**
571    * DOCUMENT ME!
572    * 
573    * @return DOCUMENT ME!
574    */
575   public String getDescription()
576   {
577     return this.description;
578   }
579
580   
581   /* (non-Javadoc)
582    * @see jalview.datamodel.SequenceI#findIndex(int)
583    */
584   public int findIndex(int pos)
585   {
586     // returns the alignment position for a residue
587     int j = start;
588     int i = 0;
589     // Rely on end being at least as long as the length of the sequence.
590     while ((i < sequence.length) && (j <= end) && (j <= pos))
591     {
592       if (!jalview.util.Comparison.isGap(sequence[i]))
593       {
594         j++;
595       }
596
597       i++;
598     }
599
600     if ((j == end) && (j < pos))
601     {
602       return end + 1;
603     }
604     else
605     {
606       return i;
607     }
608   }
609
610   /**
611    * Returns the sequence position for an alignment position
612    * 
613    * @param i
614    *          column index in alignment (from 1)
615    * 
616    * @return residue number for residue (left of and) nearest ith column
617    */
618   public int findPosition(int i)
619   {
620     int j = 0;
621     int pos = start;
622     int seqlen = sequence.length;
623     while ((j < i) && (j < seqlen))
624     {
625       if (!jalview.util.Comparison.isGap(sequence[j]))
626       {
627         pos++;
628       }
629
630       j++;
631     }
632
633     return pos;
634   }
635
636   /**
637    * Returns an int array where indices correspond to each residue in the
638    * sequence and the element value gives its position in the alignment
639    * 
640    * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
641    *         residues in SequenceI object
642    */
643   public int[] gapMap()
644   {
645     String seq = jalview.analysis.AlignSeq.extractGaps(
646             jalview.util.Comparison.GapChars, new String(sequence));
647     int[] map = new int[seq.length()];
648     int j = 0;
649     int p = 0;
650
651     while (j < sequence.length)
652     {
653       if (!jalview.util.Comparison.isGap(sequence[j]))
654       {
655         map[p++] = j;
656       }
657
658       j++;
659     }
660
661     return map;
662   }
663
664   /*
665    * (non-Javadoc)
666    * 
667    * @see jalview.datamodel.SequenceI#findPositionMap()
668    */
669   public int[] findPositionMap()
670   {
671     int map[] = new int[sequence.length];
672     int j = 0;
673     int pos = start;
674     int seqlen = sequence.length;
675     while ((j < seqlen))
676     {
677       map[j] = pos;
678       if (!jalview.util.Comparison.isGap(sequence[j]))
679       {
680         pos++;
681       }
682
683       j++;
684     }
685     return map;
686   }
687
688   /*
689    * (non-Javadoc)
690    * 
691    * @see jalview.datamodel.SequenceI#deleteChars(int, int)
692    */
693   public void deleteChars(int i, int j)
694   {
695     int newstart = start, newend = end;
696     if (i >= sequence.length)
697     {
698       return;
699     }
700
701     char[] tmp;
702
703     if (j >= sequence.length)
704     {
705       tmp = new char[i];
706       System.arraycopy(sequence, 0, tmp, 0, i);
707     }
708     else
709     {
710       tmp = new char[sequence.length - j + i];
711       System.arraycopy(sequence, 0, tmp, 0, i);
712       System.arraycopy(sequence, j, tmp, i, sequence.length - j);
713     }
714     boolean createNewDs = false;
715     for (int s = i; s < j; s++)
716     {
717       if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
718       {
719         if (createNewDs)
720         {
721           newend--;
722         }
723         else
724         {
725           int sindex = findIndex(start) - 1;
726           if (sindex == s)
727           {
728             // delete characters including start of sequence
729             newstart = findPosition(j);
730             break; // don't need to search for any more residue characters.
731           }
732           else
733           {
734             // delete characters after start.
735             int eindex = findIndex(end) - 1;
736             if (eindex < j)
737             {
738               // delete characters at end of sequence
739               newend = findPosition(i - 1);
740               break; // don't need to search for any more residue characters.
741             }
742             else
743             {
744               createNewDs = true;
745               newend--; // decrease end position by one for the deleted residue
746               // and search further
747             }
748           }
749         }
750       }
751     }
752     // deletion occured in the middle of the sequence
753     if (createNewDs && this.datasetSequence != null)
754     {
755       // construct a new sequence
756       Sequence ds = new Sequence(datasetSequence);
757       // TODO: remove any non-inheritable properties ?
758       // TODO: create a sequence mapping (since there is a relation here ?)
759       ds.deleteChars(i, j);
760       datasetSequence = ds;
761     }
762     start = newstart;
763     end = newend;
764     sequence = tmp;
765   }
766
767   /**
768    * DOCUMENT ME!
769    * 
770    * @param i
771    *          DOCUMENT ME!
772    * @param c
773    *          DOCUMENT ME!
774    * @param chop
775    *          DOCUMENT ME!
776    */
777   public void insertCharAt(int i, int length, char c)
778   {
779     char[] tmp = new char[sequence.length + length];
780
781     if (i >= sequence.length)
782     {
783       System.arraycopy(sequence, 0, tmp, 0, sequence.length);
784       i = sequence.length;
785     }
786     else
787     {
788       System.arraycopy(sequence, 0, tmp, 0, i);
789     }
790
791     int index = i;
792     while (length > 0)
793     {
794       tmp[index++] = c;
795       length--;
796     }
797
798     if (i < sequence.length)
799     {
800       System.arraycopy(sequence, i, tmp, index, sequence.length - i);
801     }
802
803     sequence = tmp;
804   }
805
806   public void insertCharAt(int i, char c)
807   {
808     insertCharAt(i, 1, c);
809   }
810
811   public String getVamsasId()
812   {
813     return vamsasId;
814   }
815
816   public void setVamsasId(String id)
817   {
818     vamsasId = id;
819   }
820
821   public void setDBRef(DBRefEntry[] dbref)
822   {
823     dbrefs = dbref;
824   }
825
826   public DBRefEntry[] getDBRef()
827   {
828     if (dbrefs == null && datasetSequence != null
829             && this != datasetSequence)
830     {
831       return datasetSequence.getDBRef();
832     }
833     return dbrefs;
834   }
835
836   public void addDBRef(DBRefEntry entry)
837   {
838     if (dbrefs == null)
839     {
840       dbrefs = new DBRefEntry[0];
841     }
842
843     int i, iSize = dbrefs.length;
844
845     for (i = 0; i < iSize; i++)
846     {
847       if (dbrefs[i].equalRef(entry))
848       {
849         if (entry.getMap() != null)
850         {
851           if (dbrefs[i].getMap() == null)
852           {
853             // overwrite with 'superior' entry that contains a mapping.
854             dbrefs[i] = entry;
855           }
856         }
857         return;
858       }
859     }
860
861     DBRefEntry[] temp = new DBRefEntry[iSize + 1];
862     System.arraycopy(dbrefs, 0, temp, 0, iSize);
863     temp[temp.length - 1] = entry;
864
865     dbrefs = temp;
866   }
867
868   public void setDatasetSequence(SequenceI seq)
869   {
870     datasetSequence = seq;
871   }
872
873   public SequenceI getDatasetSequence()
874   {
875     return datasetSequence;
876   }
877
878   public AlignmentAnnotation[] getAnnotation()
879   {
880     if (annotation == null)
881     {
882       return null;
883     }
884
885     AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
886     for (int r = 0; r < ret.length; r++)
887     {
888       ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
889     }
890
891     return ret;
892   }
893
894   public void addAlignmentAnnotation(AlignmentAnnotation annotation)
895   {
896     if (this.annotation == null)
897     {
898       this.annotation = new Vector();
899     }
900     if (!this.annotation.contains(annotation))
901     {
902       this.annotation.addElement(annotation);
903     }
904     annotation.setSequenceRef(this);
905   }
906
907   public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
908   {
909     if (this.annotation != null)
910     {
911       this.annotation.removeElement(annotation);
912       if (this.annotation.size() == 0)
913         this.annotation = null;
914     }
915   }
916
917   /**
918    * test if this is a valid candidate for another sequence's dataset sequence.
919    * 
920    */
921   private boolean isValidDatasetSequence()
922   {
923     if (datasetSequence != null)
924     {
925       return false;
926     }
927     for (int i = 0; i < sequence.length; i++)
928     {
929       if (jalview.util.Comparison.isGap(sequence[i]))
930       {
931         return false;
932       }
933     }
934     return true;
935   }
936
937   /*
938    * (non-Javadoc)
939    * 
940    * @see jalview.datamodel.SequenceI#deriveSequence()
941    */
942   public SequenceI deriveSequence()
943   {
944     SequenceI seq = new Sequence(this);
945     if (datasetSequence != null)
946     {
947       // duplicate current sequence with same dataset
948       seq.setDatasetSequence(datasetSequence);
949     }
950     else
951     {
952       if (isValidDatasetSequence())
953       {
954         // Use this as dataset sequence
955         seq.setDatasetSequence(this);
956       }
957       else
958       {
959         // Create a new, valid dataset sequence
960         SequenceI ds = seq;
961         ds.setSequence(AlignSeq.extractGaps(
962                 jalview.util.Comparison.GapChars, new String(sequence)));
963         setDatasetSequence(ds);
964         ds.setSequenceFeatures(getSequenceFeatures());
965         seq = this; // and return this sequence as the derived sequence.
966       }
967     }
968     return seq;
969   }
970
971   /*
972    * (non-Javadoc)
973    * 
974    * @see jalview.datamodel.SequenceI#createDatasetSequence()
975    */
976   public SequenceI createDatasetSequence()
977   {
978     if (datasetSequence == null)
979     {
980       datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
981               jalview.util.Comparison.GapChars, getSequenceAsString()),
982               getStart(), getEnd());
983       datasetSequence.setSequenceFeatures(getSequenceFeatures());
984       datasetSequence.setDescription(getDescription());
985       setSequenceFeatures(null);
986       // move database references onto dataset sequence
987       datasetSequence.setDBRef(getDBRef());
988       setDBRef(null);
989       datasetSequence.setPDBId(getPDBId());
990       setPDBId(null);
991       datasetSequence.updatePDBIds();
992     }
993     return datasetSequence;
994   }
995
996   /*
997    * (non-Javadoc)
998    * 
999    * @see
1000    * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1001    * annotations)
1002    */
1003   public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1004   {
1005     if (annotation != null)
1006     {
1007       annotation.removeAllElements();
1008     }
1009     if (annotations != null)
1010     {
1011       for (int i = 0; i < annotations.length; i++)
1012       {
1013         if (annotations[i] != null)
1014           addAlignmentAnnotation(annotations[i]);
1015       }
1016     }
1017   }
1018
1019   /*
1020    * (non-Javadoc)
1021    * 
1022    * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1023    */
1024   public AlignmentAnnotation[] getAnnotation(String label)
1025   {
1026     if (annotation == null || annotation.size() == 0)
1027     {
1028       return null;
1029     }
1030
1031     Vector subset = new Vector();
1032     Enumeration e = annotation.elements();
1033     while (e.hasMoreElements())
1034     {
1035       AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1036       if (ann.label != null && ann.label.equals(label))
1037       {
1038         subset.addElement(ann);
1039       }
1040     }
1041     if (subset.size() == 0)
1042     {
1043       return null;
1044     }
1045     AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1046     int i = 0;
1047     e = subset.elements();
1048     while (e.hasMoreElements())
1049     {
1050       anns[i++] = (AlignmentAnnotation) e.nextElement();
1051     }
1052     subset.removeAllElements();
1053     return anns;
1054   }
1055
1056   public boolean updatePDBIds()
1057   {
1058     if (datasetSequence != null)
1059     {
1060       // TODO: could merge DBRefs
1061       return datasetSequence.updatePDBIds();
1062     }
1063     if (dbrefs == null || dbrefs.length == 0)
1064     {
1065       return false;
1066     }
1067     Vector newpdb = new Vector();
1068     for (int i = 0; i < dbrefs.length; i++)
1069     {
1070       if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1071       {
1072         PDBEntry pdbe = new PDBEntry();
1073         pdbe.setId(dbrefs[i].getAccessionId());
1074         if (pdbIds == null || pdbIds.size() == 0)
1075         {
1076           newpdb.addElement(pdbe);
1077         }
1078         else
1079         {
1080           Enumeration en = pdbIds.elements();
1081           boolean matched = false;
1082           while (!matched && en.hasMoreElements())
1083           {
1084             PDBEntry anentry = (PDBEntry) en.nextElement();
1085             if (anentry.getId().equals(pdbe.getId()))
1086             {
1087               matched = true;
1088             }
1089           }
1090           if (!matched)
1091           {
1092             newpdb.addElement(pdbe);
1093           }
1094         }
1095       }
1096     }
1097     if (newpdb.size() > 0)
1098     {
1099       Enumeration en = newpdb.elements();
1100       while (en.hasMoreElements())
1101       {
1102         addPDBId((PDBEntry) en.nextElement());
1103       }
1104       return true;
1105     }
1106     return false;
1107   }
1108
1109   /*
1110    * (non-Javadoc)
1111    * 
1112    * @see
1113    * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1114    * jalview.datamodel.Mapping)
1115    */
1116   public void transferAnnotation(SequenceI entry, Mapping mp)
1117   {
1118     if (datasetSequence != null)
1119     {
1120       datasetSequence.transferAnnotation(entry, mp);
1121       return;
1122     }
1123     if (entry.getDatasetSequence() != null)
1124     {
1125       transferAnnotation(entry.getDatasetSequence(), mp);
1126       return;
1127     }
1128     // transfer any new features from entry onto sequence
1129     if (entry.getSequenceFeatures() != null)
1130     {
1131
1132       SequenceFeature[] sfs = entry.getSequenceFeatures();
1133       for (int si = 0; si < sfs.length; si++)
1134       {
1135         SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1136                 : new SequenceFeature[]
1137                 { new SequenceFeature(sfs[si]) };
1138         if (sf != null && sf.length > 0)
1139         {
1140           for (int sfi = 0; sfi < sf.length; sfi++)
1141           {
1142             addSequenceFeature(sf[sfi]);
1143           }
1144         }
1145       }
1146     }
1147
1148     // transfer PDB entries
1149     if (entry.getPDBId() != null)
1150     {
1151       Enumeration e = entry.getPDBId().elements();
1152       while (e.hasMoreElements())
1153       {
1154         PDBEntry pdb = (PDBEntry) e.nextElement();
1155         addPDBId(pdb);
1156       }
1157     }
1158     // transfer database references
1159     DBRefEntry[] entryRefs = entry.getDBRef();
1160     if (entryRefs != null)
1161     {
1162       for (int r = 0; r < entryRefs.length; r++)
1163       {
1164         DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1165         if (newref.getMap() != null && mp != null)
1166         {
1167           // remap ref using our local mapping
1168         }
1169         // we also assume all version string setting is done by dbSourceProxy
1170         /*
1171          * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1172          * newref.setSource(dbSource); }
1173          */
1174         addDBRef(newref);
1175       }
1176     }
1177   }
1178
1179   /**
1180    * @return The index (zero-based) on this sequence in the MSA. 
1181    * It returns {@code -1} if this information is not available.
1182    */
1183   public int getIndex() { return index; }
1184   
1185   /**
1186    * Defines the position of this sequence in the MSA. 
1187    * Use the value {@code -1} if this information is undefined.
1188    * 
1189    * @param The position for this sequence. This value is zero-based (zero for this first sequence)
1190    */
1191   public void setIndex(int value) { index = value; }
1192 }