2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
24 import jalview.analysis.*;
35 SequenceI datasetSequence;
37 private char [] sequence;
45 /** This annotation is displayed below the alignment but the
46 * positions are tied to the residues of this sequence */
50 public SequenceFeature[] sequenceFeatures;
54 * Creates a new Sequence object.
56 * @param name DOCUMENT ME!
57 * @param sequence DOCUMENT ME!
58 * @param start DOCUMENT ME!
59 * @param end DOCUMENT ME!
61 public Sequence(String name, String sequence, int start, int end)
64 this.sequence = sequence.toCharArray();
71 public Sequence(String name, char [] sequence, int start, int end)
74 this.sequence = sequence;
81 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
82 "[/][0-9]{1,}[-][0-9]{1,}$");
83 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
88 // Does sequence have the /start-end signiature?
89 if (limitrx.search(name))
91 name = limitrx.left();
92 endrx.search(limitrx.stringMatched());
93 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
94 endrx.matchedFrom() - 1)));
95 setEnd(Integer.parseInt(endrx.stringMatched()));
99 void checkValidRange()
104 for (int j = 0; j < sequence.length; j++)
106 if (!jalview.util.Comparison.isGap( sequence[j] ))
122 * Creates a new Sequence object.
124 * @param name DOCUMENT ME!
125 * @param sequence DOCUMENT ME!
127 public Sequence(String name, String sequence)
129 this(name, sequence, 1, -1);
133 * Creates a new Sequence object with new features, DBRefEntries, AlignmentAnnotations, and PDBIds
134 * but inherits any existing dataset sequence reference.
135 * @param seq DOCUMENT ME!
137 public Sequence(SequenceI seq)
139 this(seq, seq.getAnnotation());
142 * Create a new sequence object with new features, DBRefEntries, and PDBIds
143 * but inherits any existing dataset sequence reference, and duplicate of
144 * any annotation that is present in the given annotation array.
145 * @param seq the sequence to be copied
146 * @param alAnnotation an array of annotation including some associated with seq
148 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
154 description = seq.getDescription();
155 if (seq.getSequenceFeatures()!=null) {
156 SequenceFeature[] sf = seq.getSequenceFeatures();
157 for (int i=0; i<sf.length; i++) {
158 addSequenceFeature(new SequenceFeature(sf[i]));
161 if (seq.getDBRef()!=null) {
162 DBRefEntry[] dbr = seq.getDBRef();
163 for (int i=0; i<dbr.length; i++) {
164 addDBRef(new DBRefEntry(dbr[i]));
167 setDatasetSequence(seq.getDatasetSequence());
168 if (seq.getAnnotation()!=null) {
169 AlignmentAnnotation[] sqann = seq.getAnnotation();
170 for (int i=0;i<sqann.length; i++)
176 boolean found = (alAnnotation==null);
179 for (int apos = 0; !found && apos<alAnnotation.length; apos++)
181 found = (alAnnotation[apos] == sqann[i]);
186 // only copy the given annotation
187 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
188 addAlignmentAnnotation(newann);
192 if (seq.getPDBId()!=null) {
193 Vector ids = seq.getPDBId();
194 Enumeration e = ids.elements();
195 while (e.hasMoreElements()) {
196 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
204 * @param v DOCUMENT ME!
206 public void setSequenceFeatures(SequenceFeature[] features)
208 sequenceFeatures = features;
211 public synchronized void addSequenceFeature(SequenceFeature sf)
213 if (sequenceFeatures == null)
215 sequenceFeatures = new SequenceFeature[0];
218 for (int i = 0; i < sequenceFeatures.length; i++)
220 if (sequenceFeatures[i].equals(sf))
226 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
227 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
228 temp[sequenceFeatures.length] = sf;
230 sequenceFeatures = temp;
233 public void deleteFeature(SequenceFeature sf)
235 if(sequenceFeatures==null)
241 for (index = 0; index < sequenceFeatures.length; index++)
243 if (sequenceFeatures[index].equals(sf))
250 if(index==sequenceFeatures.length)
255 int sfLength = sequenceFeatures.length;
258 sequenceFeatures = null;
262 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
263 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
267 System.arraycopy(sequenceFeatures,
270 index, sequenceFeatures.length - index -1);
273 sequenceFeatures = temp;
280 * @return DOCUMENT ME!
282 public SequenceFeature[] getSequenceFeatures()
284 return sequenceFeatures;
287 public void addPDBId(PDBEntry entry)
291 pdbIds = new Vector();
294 pdbIds.addElement(entry);
300 * @param id DOCUMENT ME!
302 public void setPDBId(Vector id)
310 * @return DOCUMENT ME!
312 public Vector getPDBId()
320 * @return DOCUMENT ME!
322 public String getDisplayId(boolean jvsuffix)
324 StringBuffer result = new StringBuffer(name);
327 result.append("/" + start + "-" + end);
330 return result.toString();
336 * @param name DOCUMENT ME!
338 public void setName(String name)
347 * @return DOCUMENT ME!
349 public String getName()
357 * @param start DOCUMENT ME!
359 public void setStart(int start)
367 * @return DOCUMENT ME!
369 public int getStart()
377 * @param end DOCUMENT ME!
379 public void setEnd(int end)
387 * @return DOCUMENT ME!
397 * @return DOCUMENT ME!
399 public int getLength()
401 return this.sequence.length;
407 * @param seq DOCUMENT ME!
409 public void setSequence(String seq)
411 this.sequence = seq.toCharArray();
416 public String getSequenceAsString()
418 return new String(sequence);
421 public String getSequenceAsString(int start, int end)
423 return new String(getSequence(start, end));
427 public char [] getSequence()
435 * @param start DOCUMENT ME!
436 * @param end DOCUMENT ME!
438 * @return DOCUMENT ME!
440 public char [] getSequence(int start, int end)
444 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
445 if (start >= sequence.length)
450 if (end >= sequence.length)
452 end = sequence.length;
455 char [] reply = new char[end-start];
456 System.arraycopy(sequence, start, reply, 0, end-start);
463 * make a new Sequence object from start to end (including gaps) over this seqeunce
468 public SequenceI getSubSequence(int start, int end)
474 char [] seq = getSequence(start, end);
479 int nstart = findPosition(start);
480 int nend = findPosition(end) - 1;
481 // JBPNote - this is an incomplete copy.
482 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
483 nseq.setDescription(description);
484 if (datasetSequence!=null)
486 nseq.setDatasetSequence(datasetSequence);
490 nseq.setDatasetSequence(this);
498 * @param i DOCUMENT ME!
500 * @return DOCUMENT ME!
502 public char getCharAt(int i)
504 if (i < sequence.length)
517 * @param desc DOCUMENT ME!
519 public void setDescription(String desc)
521 this.description = desc;
527 * @return DOCUMENT ME!
529 public String getDescription()
531 return this.description;
535 * Return the alignment position for a sequence position
537 * @param pos lying from start to end
539 * @return aligned position of residue pos
541 public int findIndex(int pos)
543 // returns the alignment position for a residue
547 while ( (i < sequence.length) && (j <= end) && (j <= pos))
549 if (!jalview.util.Comparison.isGap(sequence[i]))
557 if ( (j == end) && (j < pos))
568 * Returns the sequence position for an alignment position
570 * @param i column index in alignment (from 1)
572 * @return residue number for residue (left of and) nearest ith column
574 public int findPosition(int i)
578 int seqlen = sequence.length;
579 while ( (j < i) && (j < seqlen))
581 if (!jalview.util.Comparison.isGap( sequence[j] ))
593 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
595 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
597 public int[] gapMap()
599 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
600 GapChars, new String(sequence));
601 int[] map = new int[seq.length()];
605 while (j < sequence.length)
607 if (!jalview.util.Comparison.isGap(sequence[j]))
621 * @param i DOCUMENT ME!
622 * @param j DOCUMENT ME!
624 public void deleteChars(int i, int j)
626 if (i >= sequence.length)
633 if (j >= sequence.length)
636 System.arraycopy(sequence,0,tmp,0,i);
640 tmp = new char[sequence.length-j+i];
641 System.arraycopy(sequence,0,tmp,0,i);
642 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
645 if (this.datasetSequence != null)
647 for (int s = i; s < j; s++)
649 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
652 Sequence ds = new Sequence(name,
653 AlignSeq.extractGaps(
654 jalview.util.Comparison.GapChars,
655 this.getSequenceAsString()
659 ds.setDescription(description);
673 * @param i DOCUMENT ME!
674 * @param c DOCUMENT ME!
675 * @param chop DOCUMENT ME!
677 public void insertCharAt(int i, int length, char c)
679 char [] tmp = new char[sequence.length+length];
681 if (i >= sequence.length)
683 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
688 System.arraycopy(sequence, 0, tmp, 0, i);
699 if (i < sequence.length)
701 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
707 public void insertCharAt(int i, char c)
709 insertCharAt(i, 1, c);
712 public String getVamsasId()
717 public void setVamsasId(String id)
722 public void setDBRef(DBRefEntry[] dbref)
727 public DBRefEntry[] getDBRef()
732 public void addDBRef(DBRefEntry entry)
736 dbrefs = new DBRefEntry[0];
739 int i, iSize = dbrefs.length;
741 for(i=0; i<iSize; i++)
743 if(dbrefs[i].equals(entry))
749 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
750 System.arraycopy(dbrefs, 0, temp, 0, iSize);
751 temp[temp.length - 1] = entry;
756 public void setDatasetSequence(SequenceI seq)
758 datasetSequence = seq;
761 public SequenceI getDatasetSequence()
763 return datasetSequence;
766 public AlignmentAnnotation[] getAnnotation()
768 if (annotation == null)
773 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
774 for (int r = 0; r < ret.length; r++)
776 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
782 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
784 if (this.annotation == null)
786 this.annotation = new Vector();
789 this.annotation.addElement(annotation);
790 annotation.setSequenceRef(this);
793 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
795 if(this.annotation!=null)
797 this.annotation.removeElement(annotation);
798 if(this.annotation.size()==0)
799 this.annotation = null;
805 * test if this is a valid candidate for another
806 * sequence's dataset sequence.
809 private boolean isValidDatasetSequence()
811 if (datasetSequence!=null)
815 for (int i=0;i<sequence.length; i++)
817 if (jalview.util.Comparison.isGap(sequence[i]))
825 * @see jalview.datamodel.SequenceI#deriveSequence()
827 public SequenceI deriveSequence()
829 SequenceI seq=new Sequence(this);
830 if (datasetSequence != null)
832 // duplicate current sequence with same dataset
833 seq.setDatasetSequence(datasetSequence);
837 if (isValidDatasetSequence())
839 // Use this as dataset sequence
840 seq.setDatasetSequence(this);
842 // Create a new, valid dataset sequence
844 ds.setSequence(AlignSeq.extractGaps(jalview.util.Comparison.GapChars, new String(sequence)));
845 setDatasetSequence(ds);
846 seq = this; // and return this sequence as the derived sequence.
852 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[] annotations)
854 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
856 if (annotation!=null) {
857 annotation.removeAllElements();
859 if (annotations!=null) {
860 for (int i=0; i<annotations.length; i++)
862 if (annotations[i]!=null)
863 addAlignmentAnnotation(annotations[i]);
869 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
871 public AlignmentAnnotation[] getAnnotation(String label)
873 if (annotation==null || annotation.size()==0)
878 Vector subset = new Vector();
879 Enumeration e = annotation.elements();
880 while (e.hasMoreElements())
882 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
883 if (ann.label!=null && ann.label.equals(label))
885 subset.addElement(ann);
888 if (subset.size()==0)
892 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
894 e = subset.elements();
895 while (e.hasMoreElements())
897 anns[i++] = (AlignmentAnnotation) e.nextElement();
899 subset.removeAllElements();