JAL-966 @mungo TODO complete: List rather than bare int array
[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   @Override
518   public SequenceI getSubSequence(int start, int end)
519   {
520     if (start < 0)
521     {
522       start = 0;
523     }
524     char[] seq = getSequence(start, end);
525     if (seq.length == 0)
526     {
527       return null;
528     }
529     int nstart = findPosition(start);
530     int nend = findPosition(end) - 1;
531     // JBPNote - this is an incomplete copy.
532     SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
533     nseq.setDescription(description);
534     if (datasetSequence != null)
535     {
536       nseq.setDatasetSequence(datasetSequence);
537     }
538     else
539     {
540       nseq.setDatasetSequence(this);
541     }
542     return nseq;
543   }
544
545   /**
546    * DOCUMENT ME!
547    * 
548    * @param i
549    *          DOCUMENT ME!
550    * 
551    * @return DOCUMENT ME!
552    */
553   public char getCharAt(int i)
554   {
555     if (i < sequence.length)
556     {
557       return sequence[i];
558     }
559     else
560     {
561       return ' ';
562     }
563   }
564
565   /**
566    * DOCUMENT ME!
567    * 
568    * @param desc
569    *          DOCUMENT ME!
570    */
571   public void setDescription(String desc)
572   {
573     this.description = desc;
574   }
575
576   /**
577    * DOCUMENT ME!
578    * 
579    * @return DOCUMENT ME!
580    */
581   public String getDescription()
582   {
583     return this.description;
584   }
585
586   /*
587    * (non-Javadoc)
588    * 
589    * @see jalview.datamodel.SequenceI#findIndex(int)
590    */
591   public int findIndex(int pos)
592   {
593     // returns the alignment position for a residue
594     int j = start;
595     int i = 0;
596     // Rely on end being at least as long as the length of the sequence.
597     while ((i < sequence.length) && (j <= end) && (j <= pos))
598     {
599       if (!jalview.util.Comparison.isGap(sequence[i]))
600       {
601         j++;
602       }
603
604       i++;
605     }
606
607     if ((j == end) && (j < pos))
608     {
609       return end + 1;
610     }
611     else
612     {
613       return i;
614     }
615   }
616
617   @Override
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   @Override
665   public int[] findPositionMap()
666   {
667     int map[] = new int[sequence.length];
668     int j = 0;
669     int pos = start;
670     int seqlen = sequence.length;
671     while ((j < seqlen))
672     {
673       map[j] = pos;
674       if (!jalview.util.Comparison.isGap(sequence[j]))
675       {
676         pos++;
677       }
678
679       j++;
680     }
681     return map;
682   }
683
684   @Override
685   public List<int[]> getInsertions()
686   {
687     ArrayList<int[]> map = new ArrayList<int[]>();
688     int lastj = -1, j = 0;
689     int pos = start;
690     int seqlen = sequence.length;
691     while ((j < seqlen))
692     {
693       if (jalview.util.Comparison.isGap(sequence[j]))
694       {
695         if (lastj == -1)
696         {
697           lastj = j;
698         }
699       }
700       else
701       {
702         if (lastj != -1)
703         {
704           map.add(new int[]
705           { lastj, j - 1 });
706           lastj = -1;
707         }
708       }
709       j++;
710     }
711     if (lastj != -1)
712     {
713       map.add(new int[]
714       { lastj, j - 1 });
715       lastj = -1;
716     }
717     return map;
718   }
719
720   @Override
721   public void deleteChars(int i, int j)
722   {
723     int newstart = start, newend = end;
724     if (i >= sequence.length)
725     {
726       return;
727     }
728     // TODO use StringUtils.deleteChars
729
730     char[] tmp;
731
732     if (j >= sequence.length)
733     {
734       tmp = new char[i];
735       System.arraycopy(sequence, 0, tmp, 0, i);
736       j = sequence.length;
737     }
738     else
739     {
740       tmp = new char[sequence.length - j + i];
741       System.arraycopy(sequence, 0, tmp, 0, i);
742       System.arraycopy(sequence, j, tmp, i, sequence.length - j);
743     }
744     boolean createNewDs = false;
745     // TODO: take a look at the new dataset creation validation method below -
746     // this could become time comsuming for large sequences - consider making it
747     // more efficient
748     for (int s = i; s < j; s++)
749     {
750       if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
751       {
752         if (createNewDs)
753         {
754           newend--;
755         }
756         else
757         {
758           int sindex = findIndex(start) - 1;
759           if (sindex == s)
760           {
761             // delete characters including start of sequence
762             newstart = findPosition(j);
763             break; // don't need to search for any more residue characters.
764           }
765           else
766           {
767             // delete characters after start.
768             int eindex = findIndex(end) - 1;
769             if (eindex < j)
770             {
771               // delete characters at end of sequence
772               newend = findPosition(i - 1);
773               break; // don't need to search for any more residue characters.
774             }
775             else
776             {
777               createNewDs = true;
778               newend--; // decrease end position by one for the deleted residue
779               // and search further
780             }
781           }
782         }
783       }
784     }
785     // deletion occured in the middle of the sequence
786     if (createNewDs && this.datasetSequence != null)
787     {
788       // construct a new sequence
789       Sequence ds = new Sequence(datasetSequence);
790       // TODO: remove any non-inheritable properties ?
791       // TODO: create a sequence mapping (since there is a relation here ?)
792       ds.deleteChars(i, j);
793       datasetSequence = ds;
794     }
795     start = newstart;
796     end = newend;
797     sequence = tmp;
798   }
799
800   @Override
801   public void insertCharAt(int i, int length, char c)
802   {
803     char[] tmp = new char[sequence.length + length];
804
805     if (i >= sequence.length)
806     {
807       System.arraycopy(sequence, 0, tmp, 0, sequence.length);
808       i = sequence.length;
809     }
810     else
811     {
812       System.arraycopy(sequence, 0, tmp, 0, i);
813     }
814
815     int index = i;
816     while (length > 0)
817     {
818       tmp[index++] = c;
819       length--;
820     }
821
822     if (i < sequence.length)
823     {
824       System.arraycopy(sequence, i, tmp, index, sequence.length - i);
825     }
826
827     sequence = tmp;
828   }
829
830   @Override
831   public void insertCharAt(int i, char c)
832   {
833     insertCharAt(i, 1, c);
834   }
835
836   @Override
837   public String getVamsasId()
838   {
839     return vamsasId;
840   }
841
842   @Override
843   public void setVamsasId(String id)
844   {
845     vamsasId = id;
846   }
847
848   @Override
849   public void setDBRef(DBRefEntry[] dbref)
850   {
851     dbrefs = dbref;
852   }
853
854   @Override
855   public DBRefEntry[] getDBRef()
856   {
857     if (dbrefs == null && datasetSequence != null
858             && this != datasetSequence)
859     {
860       return datasetSequence.getDBRef();
861     }
862     return dbrefs;
863   }
864
865   @Override
866   public void addDBRef(DBRefEntry entry)
867   {
868     if (dbrefs == null)
869     {
870       dbrefs = new DBRefEntry[0];
871     }
872
873     int i, iSize = dbrefs.length;
874
875     for (i = 0; i < iSize; i++)
876     {
877       if (dbrefs[i].equalRef(entry))
878       {
879         if (entry.getMap() != null)
880         {
881           if (dbrefs[i].getMap() == null)
882           {
883             // overwrite with 'superior' entry that contains a mapping.
884             dbrefs[i] = entry;
885           }
886         }
887         return;
888       }
889     }
890
891     DBRefEntry[] temp = new DBRefEntry[iSize + 1];
892     System.arraycopy(dbrefs, 0, temp, 0, iSize);
893     temp[temp.length - 1] = entry;
894
895     dbrefs = temp;
896   }
897
898   @Override
899   public void setDatasetSequence(SequenceI seq)
900   {
901     datasetSequence = seq;
902   }
903
904   @Override
905   public SequenceI getDatasetSequence()
906   {
907     return datasetSequence;
908   }
909
910   @Override
911   public AlignmentAnnotation[] getAnnotation()
912   {
913     return annotation == null ? null : annotation
914             .toArray(new AlignmentAnnotation[annotation.size()]);
915   }
916
917
918   @Override
919   public boolean hasAnnotation(AlignmentAnnotation ann)
920   {
921     return annotation == null ? false : annotation.contains(ann);
922   }
923
924   @Override
925   public void addAlignmentAnnotation(AlignmentAnnotation annotation)
926   {
927     if (this.annotation == null)
928     {
929       this.annotation = new Vector();
930     }
931     if (!this.annotation.contains(annotation))
932     {
933       this.annotation.addElement(annotation);
934     }
935     annotation.setSequenceRef(this);
936   }
937
938   public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
939   {
940     if (this.annotation != null)
941     {
942       this.annotation.removeElement(annotation);
943       if (this.annotation.size() == 0)
944       {
945         this.annotation = null;
946       }
947     }
948   }
949
950   /**
951    * test if this is a valid candidate for another sequence's dataset sequence.
952    * 
953    */
954   private boolean isValidDatasetSequence()
955   {
956     if (datasetSequence != null)
957     {
958       return false;
959     }
960     for (int i = 0; i < sequence.length; i++)
961     {
962       if (jalview.util.Comparison.isGap(sequence[i]))
963       {
964         return false;
965       }
966     }
967     return true;
968   }
969
970   @Override
971   public SequenceI deriveSequence()
972   {
973     SequenceI seq = new Sequence(this);
974     if (datasetSequence != null)
975     {
976       // duplicate current sequence with same dataset
977       seq.setDatasetSequence(datasetSequence);
978     }
979     else
980     {
981       if (isValidDatasetSequence())
982       {
983         // Use this as dataset sequence
984         seq.setDatasetSequence(this);
985       }
986       else
987       {
988         // Create a new, valid dataset sequence
989         SequenceI ds = seq;
990         ds.setSequence(AlignSeq.extractGaps(
991                 jalview.util.Comparison.GapChars, new String(sequence)));
992         setDatasetSequence(ds);
993         ds.setSequenceFeatures(getSequenceFeatures());
994         seq = this; // and return this sequence as the derived sequence.
995       }
996     }
997     return seq;
998   }
999
1000   /*
1001    * (non-Javadoc)
1002    * 
1003    * @see jalview.datamodel.SequenceI#createDatasetSequence()
1004    */
1005   public SequenceI createDatasetSequence()
1006   {
1007     if (datasetSequence == null)
1008     {
1009       datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1010               jalview.util.Comparison.GapChars, getSequenceAsString()),
1011               getStart(), getEnd());
1012       datasetSequence.setSequenceFeatures(getSequenceFeatures());
1013       datasetSequence.setDescription(getDescription());
1014       setSequenceFeatures(null);
1015       // move database references onto dataset sequence
1016       datasetSequence.setDBRef(getDBRef());
1017       setDBRef(null);
1018       datasetSequence.setPDBId(getPDBId());
1019       setPDBId(null);
1020       datasetSequence.updatePDBIds();
1021       if (annotation != null)
1022       {
1023         for (AlignmentAnnotation aa : annotation)
1024         {
1025           AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1026           _aa.sequenceRef = datasetSequence;
1027           _aa.adjustForAlignment(); // uses annotation's own record of
1028                                     // sequence-column mapping
1029           datasetSequence.addAlignmentAnnotation(_aa);
1030         }
1031       }
1032     }
1033     return datasetSequence;
1034   }
1035
1036   /*
1037    * (non-Javadoc)
1038    * 
1039    * @see
1040    * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1041    * annotations)
1042    */
1043   public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1044   {
1045     if (annotation != null)
1046     {
1047       annotation.removeAllElements();
1048     }
1049     if (annotations != null)
1050     {
1051       for (int i = 0; i < annotations.length; i++)
1052       {
1053         if (annotations[i] != null)
1054         {
1055           addAlignmentAnnotation(annotations[i]);
1056         }
1057       }
1058     }
1059   }
1060
1061   @Override
1062   public AlignmentAnnotation[] getAnnotation(String label)
1063   {
1064     if (annotation == null || annotation.size() == 0)
1065     {
1066       return null;
1067     }
1068
1069     Vector subset = new Vector();
1070     Enumeration e = annotation.elements();
1071     while (e.hasMoreElements())
1072     {
1073       AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1074       if (ann.label != null && ann.label.equals(label))
1075       {
1076         subset.addElement(ann);
1077       }
1078     }
1079     if (subset.size() == 0)
1080     {
1081       return null;
1082     }
1083     AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1084     int i = 0;
1085     e = subset.elements();
1086     while (e.hasMoreElements())
1087     {
1088       anns[i++] = (AlignmentAnnotation) e.nextElement();
1089     }
1090     subset.removeAllElements();
1091     return anns;
1092   }
1093
1094   @Override
1095   public boolean updatePDBIds()
1096   {
1097     if (datasetSequence != null)
1098     {
1099       // TODO: could merge DBRefs
1100       return datasetSequence.updatePDBIds();
1101     }
1102     if (dbrefs == null || dbrefs.length == 0)
1103     {
1104       return false;
1105     }
1106     Vector newpdb = new Vector();
1107     for (int i = 0; i < dbrefs.length; i++)
1108     {
1109       if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1110       {
1111         PDBEntry pdbe = new PDBEntry();
1112         pdbe.setId(dbrefs[i].getAccessionId());
1113         if (pdbIds == null || pdbIds.size() == 0)
1114         {
1115           newpdb.addElement(pdbe);
1116         }
1117         else
1118         {
1119           Enumeration en = pdbIds.elements();
1120           boolean matched = false;
1121           while (!matched && en.hasMoreElements())
1122           {
1123             PDBEntry anentry = (PDBEntry) en.nextElement();
1124             if (anentry.getId().equals(pdbe.getId()))
1125             {
1126               matched = true;
1127             }
1128           }
1129           if (!matched)
1130           {
1131             newpdb.addElement(pdbe);
1132           }
1133         }
1134       }
1135     }
1136     if (newpdb.size() > 0)
1137     {
1138       Enumeration en = newpdb.elements();
1139       while (en.hasMoreElements())
1140       {
1141         addPDBId((PDBEntry) en.nextElement());
1142       }
1143       return true;
1144     }
1145     return false;
1146   }
1147
1148   @Override
1149   public void transferAnnotation(SequenceI entry, Mapping mp)
1150   {
1151     if (datasetSequence != null)
1152     {
1153       datasetSequence.transferAnnotation(entry, mp);
1154       return;
1155     }
1156     if (entry.getDatasetSequence() != null)
1157     {
1158       transferAnnotation(entry.getDatasetSequence(), mp);
1159       return;
1160     }
1161     // transfer any new features from entry onto sequence
1162     if (entry.getSequenceFeatures() != null)
1163     {
1164
1165       SequenceFeature[] sfs = entry.getSequenceFeatures();
1166       for (int si = 0; si < sfs.length; si++)
1167       {
1168         SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1169                 : new SequenceFeature[]
1170                 { new SequenceFeature(sfs[si]) };
1171         if (sf != null && sf.length > 0)
1172         {
1173           for (int sfi = 0; sfi < sf.length; sfi++)
1174           {
1175             addSequenceFeature(sf[sfi]);
1176           }
1177         }
1178       }
1179     }
1180
1181     // transfer PDB entries
1182     if (entry.getPDBId() != null)
1183     {
1184       Enumeration e = entry.getPDBId().elements();
1185       while (e.hasMoreElements())
1186       {
1187         PDBEntry pdb = (PDBEntry) e.nextElement();
1188         addPDBId(pdb);
1189       }
1190     }
1191     // transfer database references
1192     DBRefEntry[] entryRefs = entry.getDBRef();
1193     if (entryRefs != null)
1194     {
1195       for (int r = 0; r < entryRefs.length; r++)
1196       {
1197         DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1198         if (newref.getMap() != null && mp != null)
1199         {
1200           // remap ref using our local mapping
1201         }
1202         // we also assume all version string setting is done by dbSourceProxy
1203         /*
1204          * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1205          * newref.setSource(dbSource); }
1206          */
1207         addDBRef(newref);
1208       }
1209     }
1210   }
1211
1212   /**
1213    * @return The index (zero-based) on this sequence in the MSA. It returns
1214    *         {@code -1} if this information is not available.
1215    */
1216   public int getIndex()
1217   {
1218     return index;
1219   }
1220
1221   /**
1222    * Defines the position of this sequence in the MSA. Use the value {@code -1}
1223    * if this information is undefined.
1224    * 
1225    * @param The
1226    *          position for this sequence. This value is zero-based (zero for
1227    *          this first sequence)
1228    */
1229   public void setIndex(int value)
1230   {
1231     index = value;
1232   }
1233
1234   public void setRNA(RNA r)
1235   {
1236     rna = r;
1237   }
1238
1239   public RNA getRNA()
1240   {
1241     return rna;
1242   }
1243
1244   @Override
1245   public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1246           String label)
1247   {
1248     List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1249     if (this.annotation != null)
1250     {
1251       for (AlignmentAnnotation ann : annotation)
1252       {
1253         if (ann.calcId != null && ann.calcId.equals(calcId)
1254                 && ann.label != null && ann.label.equals(label))
1255         {
1256           result.add(ann);
1257         }
1258       }
1259     }
1260     return result;
1261   }
1262
1263 }