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