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