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(
91 System.err.println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
94 // Does sequence have the /start-end signiature?
95 if (limitrx.search(name))
97 name = limitrx.left();
98 endrx.search(limitrx.stringMatched());
99 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
100 endrx.matchedFrom() - 1)));
101 setEnd(Integer.parseInt(endrx.stringMatched()));
105 void checkValidRange()
110 for (int j = 0; j < sequence.length; j++)
112 if (!jalview.util.Comparison.isGap( sequence[j] ))
128 * Creates a new Sequence object.
130 * @param name DOCUMENT ME!
131 * @param sequence DOCUMENT ME!
133 public Sequence(String name, String sequence)
135 this(name, sequence, 1, -1);
139 * Creates a new Sequence object with new features, DBRefEntries, AlignmentAnnotations, and PDBIds
140 * but inherits any existing dataset sequence reference.
141 * @param seq DOCUMENT ME!
143 public Sequence(SequenceI seq)
145 this(seq, seq.getAnnotation());
148 * Create a new sequence object with new features, DBRefEntries, and PDBIds
149 * but inherits any existing dataset sequence reference, and duplicate of
150 * any annotation that is present in the given annotation array.
151 * @param seq the sequence to be copied
152 * @param alAnnotation an array of annotation including some associated with seq
154 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
160 description = seq.getDescription();
161 if (seq.getSequenceFeatures()!=null) {
162 SequenceFeature[] sf = seq.getSequenceFeatures();
163 for (int i=0; i<sf.length; i++) {
164 addSequenceFeature(new SequenceFeature(sf[i]));
167 if (seq.getDBRef()!=null) {
168 DBRefEntry[] dbr = seq.getDBRef();
169 for (int i=0; i<dbr.length; i++) {
170 addDBRef(new DBRefEntry(dbr[i]));
173 setDatasetSequence(seq.getDatasetSequence());
174 if (seq.getAnnotation()!=null) {
175 AlignmentAnnotation[] sqann = seq.getAnnotation();
176 for (int i=0;i<sqann.length; i++)
182 boolean found = (alAnnotation==null);
185 for (int apos = 0; !found && apos<alAnnotation.length; apos++)
187 found = (alAnnotation[apos] == sqann[i]);
192 // only copy the given annotation
193 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
194 addAlignmentAnnotation(newann);
198 if (seq.getPDBId()!=null) {
199 Vector ids = seq.getPDBId();
200 Enumeration e = ids.elements();
201 while (e.hasMoreElements()) {
202 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
210 * @param v DOCUMENT ME!
212 public void setSequenceFeatures(SequenceFeature[] features)
214 sequenceFeatures = features;
217 public synchronized void addSequenceFeature(SequenceFeature sf)
219 if (sequenceFeatures == null)
221 sequenceFeatures = new SequenceFeature[0];
224 for (int i = 0; i < sequenceFeatures.length; i++)
226 if (sequenceFeatures[i].equals(sf))
232 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
233 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
234 temp[sequenceFeatures.length] = sf;
236 sequenceFeatures = temp;
239 public void deleteFeature(SequenceFeature sf)
241 if(sequenceFeatures==null)
247 for (index = 0; index < sequenceFeatures.length; index++)
249 if (sequenceFeatures[index].equals(sf))
256 if(index==sequenceFeatures.length)
261 int sfLength = sequenceFeatures.length;
264 sequenceFeatures = null;
268 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
269 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
273 System.arraycopy(sequenceFeatures,
276 index, sequenceFeatures.length - index -1);
279 sequenceFeatures = temp;
286 * @return DOCUMENT ME!
288 public SequenceFeature[] getSequenceFeatures()
290 return sequenceFeatures;
293 public void addPDBId(PDBEntry entry)
297 pdbIds = new Vector();
300 pdbIds.addElement(entry);
306 * @param id DOCUMENT ME!
308 public void setPDBId(Vector id)
316 * @return DOCUMENT ME!
318 public Vector getPDBId()
326 * @return DOCUMENT ME!
328 public String getDisplayId(boolean jvsuffix)
330 StringBuffer result = new StringBuffer(name);
333 result.append("/" + start + "-" + end);
336 return result.toString();
342 * @param name DOCUMENT ME!
344 public void setName(String name)
353 * @return DOCUMENT ME!
355 public String getName()
363 * @param start DOCUMENT ME!
365 public void setStart(int start)
373 * @return DOCUMENT ME!
375 public int getStart()
383 * @param end DOCUMENT ME!
385 public void setEnd(int end)
393 * @return DOCUMENT ME!
403 * @return DOCUMENT ME!
405 public int getLength()
407 return this.sequence.length;
413 * @param seq DOCUMENT ME!
415 public void setSequence(String seq)
417 this.sequence = seq.toCharArray();
422 public String getSequenceAsString()
424 return new String(sequence);
427 public String getSequenceAsString(int start, int end)
429 return new String(getSequence(start, end));
433 public char [] getSequence()
441 * @param start DOCUMENT ME!
442 * @param end DOCUMENT ME!
444 * @return DOCUMENT ME!
446 public char [] getSequence(int start, int end)
450 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
451 if (start >= sequence.length)
456 if (end >= sequence.length)
458 end = sequence.length;
461 char [] reply = new char[end-start];
462 System.arraycopy(sequence, start, reply, 0, end-start);
469 * make a new Sequence object from start to end (including gaps) over this seqeunce
474 public SequenceI getSubSequence(int start, int end)
480 char [] seq = getSequence(start, end);
485 int nstart = findPosition(start);
486 int nend = findPosition(end) - 1;
487 // JBPNote - this is an incomplete copy.
488 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
489 nseq.setDescription(description);
490 if (datasetSequence!=null)
492 nseq.setDatasetSequence(datasetSequence);
496 nseq.setDatasetSequence(this);
504 * @param i DOCUMENT ME!
506 * @return DOCUMENT ME!
508 public char getCharAt(int i)
510 if (i < sequence.length)
523 * @param desc DOCUMENT ME!
525 public void setDescription(String desc)
527 this.description = desc;
533 * @return DOCUMENT ME!
535 public String getDescription()
537 return this.description;
541 * Return the alignment position for a sequence position
543 * @param pos lying from start to end
545 * @return aligned position of residue pos
547 public int findIndex(int pos)
549 // returns the alignment position for a residue
553 while ( (i < sequence.length) && (j <= end) && (j <= pos))
555 if (!jalview.util.Comparison.isGap(sequence[i]))
563 if ( (j == end) && (j < pos))
574 * Returns the sequence position for an alignment position
576 * @param i column index in alignment (from 1)
578 * @return residue number for residue (left of and) nearest ith column
580 public int findPosition(int i)
584 int seqlen = sequence.length;
585 while ( (j < i) && (j < seqlen))
587 if (!jalview.util.Comparison.isGap( sequence[j] ))
599 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
601 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
603 public int[] gapMap()
605 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
606 GapChars, new String(sequence));
607 int[] map = new int[seq.length()];
611 while (j < sequence.length)
613 if (!jalview.util.Comparison.isGap(sequence[j]))
625 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
627 public void deleteChars(int i, int j)
629 int newstart=start,newend=end;
630 if (i >= sequence.length)
637 if (j >= sequence.length)
640 System.arraycopy(sequence,0,tmp,0,i);
644 tmp = new char[sequence.length-j+i];
645 System.arraycopy(sequence,0,tmp,0,i);
646 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
648 boolean createNewDs=false;
649 for (int s = i; s < j; s++)
651 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
657 int sindex = findIndex(start)-1;
660 // delete characters including start of sequence
661 newstart = findPosition(j);
662 break; // don't need to search for any more residue characters.
664 // delete characters after start.
665 int eindex = findIndex(end)-1;
668 // delete characters at end of sequence
669 newend = findPosition(i-1);
670 break; // don't need to search for any more residue characters.
673 newend--; // decrease end position by one for the deleted residue and search further
679 // deletion occured in the middle of the sequence
680 if (createNewDs && this.datasetSequence != null)
682 // construct a new sequence
683 Sequence ds = new Sequence(datasetSequence);
684 // TODO: remove any non-inheritable properties ?
685 // TODO: create a sequence mapping (since there is a relation here ?)
686 ds.deleteChars(i, j);
687 datasetSequence = ds;
698 * @param i DOCUMENT ME!
699 * @param c DOCUMENT ME!
700 * @param chop DOCUMENT ME!
702 public void insertCharAt(int i, int length, char c)
704 char [] tmp = new char[sequence.length+length];
706 if (i >= sequence.length)
708 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
713 System.arraycopy(sequence, 0, tmp, 0, i);
724 if (i < sequence.length)
726 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
732 public void insertCharAt(int i, char c)
734 insertCharAt(i, 1, c);
737 public String getVamsasId()
742 public void setVamsasId(String id)
747 public void setDBRef(DBRefEntry[] dbref)
752 public DBRefEntry[] getDBRef()
757 public void addDBRef(DBRefEntry entry)
761 dbrefs = new DBRefEntry[0];
764 int i, iSize = dbrefs.length;
766 for(i=0; i<iSize; i++)
768 if(dbrefs[i].equalRef(entry))
770 if (entry.getMap()!=null)
772 if (dbrefs[i].getMap()==null)
774 // overwrite with 'superior' entry that contains a mapping.
782 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
783 System.arraycopy(dbrefs, 0, temp, 0, iSize);
784 temp[temp.length - 1] = entry;
789 public void setDatasetSequence(SequenceI seq)
791 datasetSequence = seq;
794 public SequenceI getDatasetSequence()
796 return datasetSequence;
799 public AlignmentAnnotation[] getAnnotation()
801 if (annotation == null)
806 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
807 for (int r = 0; r < ret.length; r++)
809 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
815 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
817 if (this.annotation == null)
819 this.annotation = new Vector();
822 this.annotation.addElement(annotation);
823 annotation.setSequenceRef(this);
826 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
828 if(this.annotation!=null)
830 this.annotation.removeElement(annotation);
831 if(this.annotation.size()==0)
832 this.annotation = null;
838 * test if this is a valid candidate for another
839 * sequence's dataset sequence.
842 private boolean isValidDatasetSequence()
844 if (datasetSequence!=null)
848 for (int i=0;i<sequence.length; i++)
850 if (jalview.util.Comparison.isGap(sequence[i]))
858 * @see jalview.datamodel.SequenceI#deriveSequence()
860 public SequenceI deriveSequence()
862 SequenceI seq=new Sequence(this);
863 if (datasetSequence != null)
865 // duplicate current sequence with same dataset
866 seq.setDatasetSequence(datasetSequence);
870 if (isValidDatasetSequence())
872 // Use this as dataset sequence
873 seq.setDatasetSequence(this);
875 // Create a new, valid dataset sequence
877 ds.setSequence(AlignSeq.extractGaps(jalview.util.Comparison.GapChars, new String(sequence)));
878 setDatasetSequence(ds);
879 ds.setSequenceFeatures(getSequenceFeatures());
880 seq = this; // and return this sequence as the derived sequence.
886 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[] annotations)
888 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
890 if (annotation!=null) {
891 annotation.removeAllElements();
893 if (annotations!=null) {
894 for (int i=0; i<annotations.length; i++)
896 if (annotations[i]!=null)
897 addAlignmentAnnotation(annotations[i]);
903 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
905 public AlignmentAnnotation[] getAnnotation(String label)
907 if (annotation==null || annotation.size()==0)
912 Vector subset = new Vector();
913 Enumeration e = annotation.elements();
914 while (e.hasMoreElements())
916 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
917 if (ann.label!=null && ann.label.equals(label))
919 subset.addElement(ann);
922 if (subset.size()==0)
926 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
928 e = subset.elements();
929 while (e.hasMoreElements())
931 anns[i++] = (AlignmentAnnotation) e.nextElement();
933 subset.removeAllElements();