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