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