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.*;
28 * Implements the SequenceI interface for a char[] based sequence object.
36 SequenceI datasetSequence;
38 private char [] sequence;
46 /** This annotation is displayed below the alignment but the
47 * positions are tied to the residues of this sequence */
50 /** array of seuqence features - may not be null for a valid sequence object */
51 public SequenceFeature[] sequenceFeatures;
55 * Creates a new Sequence object.
57 * @param name display name string
58 * @param sequence string to form a possibly gapped sequence out of
59 * @param start first position of non-gap residue in the sequence
60 * @param end last position of ungapped residues (nearly always only used for display purposes)
62 public Sequence(String name, String sequence, int start, int end)
65 this.sequence = sequence.toCharArray();
72 public Sequence(String name, char [] sequence, int start, int end)
75 this.sequence = sequence;
82 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
83 "[/][0-9]{1,}[-][0-9]{1,}$");
84 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
89 // Does sequence have the /start-end signiature?
90 if (limitrx.search(name))
92 name = limitrx.left();
93 endrx.search(limitrx.stringMatched());
94 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
95 endrx.matchedFrom() - 1)));
96 setEnd(Integer.parseInt(endrx.stringMatched()));
100 void checkValidRange()
105 for (int j = 0; j < sequence.length; j++)
107 if (!jalview.util.Comparison.isGap( sequence[j] ))
123 * Creates a new Sequence object.
125 * @param name DOCUMENT ME!
126 * @param sequence DOCUMENT ME!
128 public Sequence(String name, String sequence)
130 this(name, sequence, 1, -1);
134 * Creates a new Sequence object with new features, DBRefEntries, AlignmentAnnotations, and PDBIds
135 * but inherits any existing dataset sequence reference.
136 * @param seq DOCUMENT ME!
138 public Sequence(SequenceI seq)
140 this(seq, seq.getAnnotation());
143 * Create a new sequence object with new features, DBRefEntries, and PDBIds
144 * but inherits any existing dataset sequence reference, and duplicate of
145 * any annotation that is present in the given annotation array.
146 * @param seq the sequence to be copied
147 * @param alAnnotation an array of annotation including some associated with seq
149 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
155 description = seq.getDescription();
156 if (seq.getSequenceFeatures()!=null) {
157 SequenceFeature[] sf = seq.getSequenceFeatures();
158 for (int i=0; i<sf.length; i++) {
159 addSequenceFeature(new SequenceFeature(sf[i]));
162 if (seq.getDBRef()!=null) {
163 DBRefEntry[] dbr = seq.getDBRef();
164 for (int i=0; i<dbr.length; i++) {
165 addDBRef(new DBRefEntry(dbr[i]));
168 setDatasetSequence(seq.getDatasetSequence());
169 if (seq.getAnnotation()!=null) {
170 AlignmentAnnotation[] sqann = seq.getAnnotation();
171 for (int i=0;i<sqann.length; i++)
177 boolean found = (alAnnotation==null);
180 for (int apos = 0; !found && apos<alAnnotation.length; apos++)
182 found = (alAnnotation[apos] == sqann[i]);
187 // only copy the given annotation
188 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
189 addAlignmentAnnotation(newann);
193 if (seq.getPDBId()!=null) {
194 Vector ids = seq.getPDBId();
195 Enumeration e = ids.elements();
196 while (e.hasMoreElements()) {
197 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
205 * @param v DOCUMENT ME!
207 public void setSequenceFeatures(SequenceFeature[] features)
209 sequenceFeatures = features;
212 public synchronized void addSequenceFeature(SequenceFeature sf)
214 if (sequenceFeatures == null)
216 sequenceFeatures = new SequenceFeature[0];
219 for (int i = 0; i < sequenceFeatures.length; i++)
221 if (sequenceFeatures[i].equals(sf))
227 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
228 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
229 temp[sequenceFeatures.length] = sf;
231 sequenceFeatures = temp;
234 public void deleteFeature(SequenceFeature sf)
236 if(sequenceFeatures==null)
242 for (index = 0; index < sequenceFeatures.length; index++)
244 if (sequenceFeatures[index].equals(sf))
251 if(index==sequenceFeatures.length)
256 int sfLength = sequenceFeatures.length;
259 sequenceFeatures = null;
263 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
264 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
268 System.arraycopy(sequenceFeatures,
271 index, sequenceFeatures.length - index -1);
274 sequenceFeatures = temp;
281 * @return DOCUMENT ME!
283 public SequenceFeature[] getSequenceFeatures()
285 return sequenceFeatures;
288 public void addPDBId(PDBEntry entry)
292 pdbIds = new Vector();
295 pdbIds.addElement(entry);
301 * @param id DOCUMENT ME!
303 public void setPDBId(Vector id)
311 * @return DOCUMENT ME!
313 public Vector getPDBId()
321 * @return DOCUMENT ME!
323 public String getDisplayId(boolean jvsuffix)
325 StringBuffer result = new StringBuffer(name);
328 result.append("/" + start + "-" + end);
331 return result.toString();
337 * @param name DOCUMENT ME!
339 public void setName(String name)
348 * @return DOCUMENT ME!
350 public String getName()
358 * @param start DOCUMENT ME!
360 public void setStart(int start)
368 * @return DOCUMENT ME!
370 public int getStart()
378 * @param end DOCUMENT ME!
380 public void setEnd(int end)
388 * @return DOCUMENT ME!
398 * @return DOCUMENT ME!
400 public int getLength()
402 return this.sequence.length;
408 * @param seq DOCUMENT ME!
410 public void setSequence(String seq)
412 this.sequence = seq.toCharArray();
417 public String getSequenceAsString()
419 return new String(sequence);
422 public String getSequenceAsString(int start, int end)
424 return new String(getSequence(start, end));
428 public char [] getSequence()
436 * @param start DOCUMENT ME!
437 * @param end DOCUMENT ME!
439 * @return DOCUMENT ME!
441 public char [] getSequence(int start, int end)
445 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
446 if (start >= sequence.length)
451 if (end >= sequence.length)
453 end = sequence.length;
456 char [] reply = new char[end-start];
457 System.arraycopy(sequence, start, reply, 0, end-start);
464 * make a new Sequence object from start to end (including gaps) over this seqeunce
469 public SequenceI getSubSequence(int start, int end)
475 char [] seq = getSequence(start, end);
480 int nstart = findPosition(start);
481 int nend = findPosition(end) - 1;
482 // JBPNote - this is an incomplete copy.
483 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
484 nseq.setDescription(description);
485 if (datasetSequence!=null)
487 nseq.setDatasetSequence(datasetSequence);
491 nseq.setDatasetSequence(this);
499 * @param i DOCUMENT ME!
501 * @return DOCUMENT ME!
503 public char getCharAt(int i)
505 if (i < sequence.length)
518 * @param desc DOCUMENT ME!
520 public void setDescription(String desc)
522 this.description = desc;
528 * @return DOCUMENT ME!
530 public String getDescription()
532 return this.description;
536 * Return the alignment position for a sequence position
538 * @param pos lying from start to end
540 * @return aligned position of residue pos
542 public int findIndex(int pos)
544 // returns the alignment position for a residue
548 while ( (i < sequence.length) && (j <= end) && (j <= pos))
550 if (!jalview.util.Comparison.isGap(sequence[i]))
558 if ( (j == end) && (j < pos))
569 * Returns the sequence position for an alignment position
571 * @param i column index in alignment (from 1)
573 * @return residue number for residue (left of and) nearest ith column
575 public int findPosition(int i)
579 int seqlen = sequence.length;
580 while ( (j < i) && (j < seqlen))
582 if (!jalview.util.Comparison.isGap( sequence[j] ))
594 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
596 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
598 public int[] gapMap()
600 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
601 GapChars, new String(sequence));
602 int[] map = new int[seq.length()];
606 while (j < sequence.length)
608 if (!jalview.util.Comparison.isGap(sequence[j]))
620 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
622 public void deleteChars(int i, int j)
624 int newstart=start,newend=end;
625 if (i >= sequence.length)
632 if (j >= sequence.length)
635 System.arraycopy(sequence,0,tmp,0,i);
639 tmp = new char[sequence.length-j+i];
640 System.arraycopy(sequence,0,tmp,0,i);
641 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
643 boolean createNewDs=false;
644 for (int s = i; s < j; s++)
646 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
652 int sindex = findIndex(start)-1;
655 // delete characters including start of sequence
656 newstart = findPosition(j);
657 break; // don't need to search for any more residue characters.
659 // delete characters after start.
660 int eindex = findIndex(end)-1;
663 // delete characters at end of sequence
664 newend = findPosition(i-1);
665 break; // don't need to search for any more residue characters.
668 newend--; // decrease end position by one for the deleted residue and search further
674 // deletion occured in the middle of the sequence
675 if (createNewDs && this.datasetSequence != null)
677 // construct a new sequence
678 Sequence ds = new Sequence(datasetSequence);
679 // TODO: remove any non-inheritable properties ?
680 // TODO: create a sequence mapping (since there is a relation here ?)
681 ds.deleteChars(i, j);
682 datasetSequence = ds;
693 * @param i DOCUMENT ME!
694 * @param c DOCUMENT ME!
695 * @param chop DOCUMENT ME!
697 public void insertCharAt(int i, int length, char c)
699 char [] tmp = new char[sequence.length+length];
701 if (i >= sequence.length)
703 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
708 System.arraycopy(sequence, 0, tmp, 0, i);
719 if (i < sequence.length)
721 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
727 public void insertCharAt(int i, char c)
729 insertCharAt(i, 1, c);
732 public String getVamsasId()
737 public void setVamsasId(String id)
742 public void setDBRef(DBRefEntry[] dbref)
747 public DBRefEntry[] getDBRef()
752 public void addDBRef(DBRefEntry entry)
756 dbrefs = new DBRefEntry[0];
759 int i, iSize = dbrefs.length;
761 for(i=0; i<iSize; i++)
763 if(dbrefs[i].equals(entry))
769 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
770 System.arraycopy(dbrefs, 0, temp, 0, iSize);
771 temp[temp.length - 1] = entry;
776 public void setDatasetSequence(SequenceI seq)
778 datasetSequence = seq;
781 public SequenceI getDatasetSequence()
783 return datasetSequence;
786 public AlignmentAnnotation[] getAnnotation()
788 if (annotation == null)
793 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
794 for (int r = 0; r < ret.length; r++)
796 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
802 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
804 if (this.annotation == null)
806 this.annotation = new Vector();
809 this.annotation.addElement(annotation);
810 annotation.setSequenceRef(this);
813 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
815 if(this.annotation!=null)
817 this.annotation.removeElement(annotation);
818 if(this.annotation.size()==0)
819 this.annotation = null;
825 * test if this is a valid candidate for another
826 * sequence's dataset sequence.
829 private boolean isValidDatasetSequence()
831 if (datasetSequence!=null)
835 for (int i=0;i<sequence.length; i++)
837 if (jalview.util.Comparison.isGap(sequence[i]))
845 * @see jalview.datamodel.SequenceI#deriveSequence()
847 public SequenceI deriveSequence()
849 SequenceI seq=new Sequence(this);
850 if (datasetSequence != null)
852 // duplicate current sequence with same dataset
853 seq.setDatasetSequence(datasetSequence);
857 if (isValidDatasetSequence())
859 // Use this as dataset sequence
860 seq.setDatasetSequence(this);
862 // Create a new, valid dataset sequence
864 ds.setSequence(AlignSeq.extractGaps(jalview.util.Comparison.GapChars, new String(sequence)));
865 setDatasetSequence(ds);
866 ds.setSequenceFeatures(getSequenceFeatures());
867 seq = this; // and return this sequence as the derived sequence.
873 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[] annotations)
875 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
877 if (annotation!=null) {
878 annotation.removeAllElements();
880 if (annotations!=null) {
881 for (int i=0; i<annotations.length; i++)
883 if (annotations[i]!=null)
884 addAlignmentAnnotation(annotations[i]);
890 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
892 public AlignmentAnnotation[] getAnnotation(String label)
894 if (annotation==null || annotation.size()==0)
899 Vector subset = new Vector();
900 Enumeration e = annotation.elements();
901 while (e.hasMoreElements())
903 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
904 if (ann.label!=null && ann.label.equals(label))
906 subset.addElement(ann);
909 if (subset.size()==0)
913 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
915 e = subset.elements();
916 while (e.hasMoreElements())
918 anns[i++] = (AlignmentAnnotation) e.nextElement();
920 subset.removeAllElements();