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 && alAnnotation!=null) {
169 AlignmentAnnotation[] sqann = seq.getAnnotation();
170 for (int i=0;i<sqann.length; i++)
172 boolean found = sqann == alAnnotation;
175 for (int apos = 0; !found && apos<alAnnotation.length; apos++)
177 found = (alAnnotation[apos] == sqann[i]);
180 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
181 addAlignmentAnnotation(newann);
184 if (seq.getPDBId()!=null) {
185 Vector ids = seq.getPDBId();
186 Enumeration e = ids.elements();
187 while (e.hasMoreElements()) {
188 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
196 * @param v DOCUMENT ME!
198 public void setSequenceFeatures(SequenceFeature[] features)
200 sequenceFeatures = features;
203 public synchronized void addSequenceFeature(SequenceFeature sf)
205 if (sequenceFeatures == null)
207 sequenceFeatures = new SequenceFeature[0];
210 for (int i = 0; i < sequenceFeatures.length; i++)
212 if (sequenceFeatures[i].equals(sf))
218 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
219 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
220 temp[sequenceFeatures.length] = sf;
222 sequenceFeatures = temp;
225 public void deleteFeature(SequenceFeature sf)
227 if(sequenceFeatures==null)
233 for (index = 0; index < sequenceFeatures.length; index++)
235 if (sequenceFeatures[index].equals(sf))
242 if(index==sequenceFeatures.length)
247 int sfLength = sequenceFeatures.length;
250 sequenceFeatures = null;
254 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
255 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
259 System.arraycopy(sequenceFeatures,
262 index, sequenceFeatures.length - index -1);
265 sequenceFeatures = temp;
272 * @return DOCUMENT ME!
274 public SequenceFeature[] getSequenceFeatures()
276 return sequenceFeatures;
279 public void addPDBId(PDBEntry entry)
283 pdbIds = new Vector();
286 pdbIds.addElement(entry);
292 * @param id DOCUMENT ME!
294 public void setPDBId(Vector id)
302 * @return DOCUMENT ME!
304 public Vector getPDBId()
312 * @return DOCUMENT ME!
314 public String getDisplayId(boolean jvsuffix)
316 StringBuffer result = new StringBuffer(name);
319 result.append("/" + start + "-" + end);
322 return result.toString();
328 * @param name DOCUMENT ME!
330 public void setName(String name)
339 * @return DOCUMENT ME!
341 public String getName()
349 * @param start DOCUMENT ME!
351 public void setStart(int start)
359 * @return DOCUMENT ME!
361 public int getStart()
369 * @param end DOCUMENT ME!
371 public void setEnd(int end)
379 * @return DOCUMENT ME!
389 * @return DOCUMENT ME!
391 public int getLength()
393 return this.sequence.length;
399 * @param seq DOCUMENT ME!
401 public void setSequence(String seq)
403 this.sequence = seq.toCharArray();
408 public String getSequenceAsString()
410 return new String(sequence);
413 public String getSequenceAsString(int start, int end)
415 return new String(getSequence(start, end));
419 public char [] getSequence()
427 * @param start DOCUMENT ME!
428 * @param end DOCUMENT ME!
430 * @return DOCUMENT ME!
432 public char [] getSequence(int start, int end)
436 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
437 if (start >= sequence.length)
442 if (end >= sequence.length)
444 end = sequence.length;
447 char [] reply = new char[end-start];
448 System.arraycopy(sequence, start, reply, 0, end-start);
455 * make a new Sequence object from start to end (including gaps) over this seqeunce
460 public SequenceI getSubSequence(int start, int end)
466 char [] seq = getSequence(start, end);
471 int nstart = findPosition(start);
472 int nend = findPosition(end) - 1;
473 // JBPNote - this is an incomplete copy.
474 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
475 nseq.setDescription(description);
476 if (datasetSequence!=null)
478 nseq.setDatasetSequence(datasetSequence);
482 nseq.setDatasetSequence(this);
490 * @param i DOCUMENT ME!
492 * @return DOCUMENT ME!
494 public char getCharAt(int i)
496 if (i < sequence.length)
509 * @param desc DOCUMENT ME!
511 public void setDescription(String desc)
513 this.description = desc;
519 * @return DOCUMENT ME!
521 public String getDescription()
523 return this.description;
527 * Return the alignment position for a sequence position
529 * @param pos lying from start to end
531 * @return aligned position of residue pos
533 public int findIndex(int pos)
535 // returns the alignment position for a residue
539 while ( (i < sequence.length) && (j <= end) && (j <= pos))
541 if (!jalview.util.Comparison.isGap(sequence[i]))
549 if ( (j == end) && (j < pos))
560 * Returns the sequence position for an alignment position
562 * @param i column index in alignment (from 1)
564 * @return residue number for residue (left of and) nearest ith column
566 public int findPosition(int i)
570 int seqlen = sequence.length;
571 while ( (j < i) && (j < seqlen))
573 if (!jalview.util.Comparison.isGap( sequence[j] ))
585 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
587 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
589 public int[] gapMap()
591 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
592 GapChars, new String(sequence));
593 int[] map = new int[seq.length()];
597 while (j < sequence.length)
599 if (!jalview.util.Comparison.isGap(sequence[j]))
613 * @param i DOCUMENT ME!
614 * @param j DOCUMENT ME!
616 public void deleteChars(int i, int j)
618 if (i >= sequence.length)
625 if (j >= sequence.length)
628 System.arraycopy(sequence,0,tmp,0,i);
632 tmp = new char[sequence.length-j+i];
633 System.arraycopy(sequence,0,tmp,0,i);
634 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
637 if (this.datasetSequence != null)
639 for (int s = i; s < j; s++)
641 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
644 Sequence ds = new Sequence(name,
645 AlignSeq.extractGaps(
646 jalview.util.Comparison.GapChars,
647 this.getSequenceAsString()
651 ds.setDescription(description);
665 * @param i DOCUMENT ME!
666 * @param c DOCUMENT ME!
667 * @param chop DOCUMENT ME!
669 public void insertCharAt(int i, int length, char c)
671 char [] tmp = new char[sequence.length+length];
673 if (i >= sequence.length)
675 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
680 System.arraycopy(sequence, 0, tmp, 0, i);
691 if (i < sequence.length)
693 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
699 public void insertCharAt(int i, char c)
701 insertCharAt(i, 1, c);
704 public String getVamsasId()
709 public void setVamsasId(String id)
714 public void setDBRef(DBRefEntry[] dbref)
719 public DBRefEntry[] getDBRef()
724 public void addDBRef(DBRefEntry entry)
728 dbrefs = new DBRefEntry[0];
731 int i, iSize = dbrefs.length;
733 for(i=0; i<iSize; i++)
735 if(dbrefs[i].equals(entry))
741 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
742 System.arraycopy(dbrefs, 0, temp, 0, iSize);
743 temp[temp.length - 1] = entry;
748 public void setDatasetSequence(SequenceI seq)
750 datasetSequence = seq;
753 public SequenceI getDatasetSequence()
755 return datasetSequence;
758 public AlignmentAnnotation[] getAnnotation()
760 if (annotation == null)
765 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
766 for (int r = 0; r < ret.length; r++)
768 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
774 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
776 if (this.annotation == null)
778 this.annotation = new Vector();
781 this.annotation.addElement(annotation);
782 annotation.setSequenceRef(this);
785 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
787 if(this.annotation!=null)
789 this.annotation.removeElement(annotation);
790 if(this.annotation.size()==0)
791 this.annotation = null;
797 * test if this is a valid candidate for another
798 * sequence's dataset sequence.
801 private boolean isValidDatasetSequence()
803 if (datasetSequence!=null)
807 for (int i=0;i<sequence.length; i++)
809 if (jalview.util.Comparison.isGap(sequence[i]))
817 * @see jalview.datamodel.SequenceI#deriveSequence()
819 public SequenceI deriveSequence()
821 SequenceI seq=new Sequence(this);
822 if (datasetSequence != null)
824 // duplicate current sequence with same dataset
825 seq.setDatasetSequence(datasetSequence);
829 if (isValidDatasetSequence())
831 // Use this as dataset sequence
832 seq.setDatasetSequence(this);
834 // Create a new, valid dataset sequence
836 ds.setSequence(AlignSeq.extractGaps(jalview.util.Comparison.GapChars, new String(sequence)));
837 setDatasetSequence(ds);
838 seq = this; // and return this sequence as the derived sequence.
844 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[] annotations)
846 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
848 if (annotation!=null) {
849 annotation.removeAllElements();
851 if (annotations!=null) {
852 for (int i=0; i<annotations.length; i++)
854 if (annotations[i]!=null)
855 addAlignmentAnnotation(annotations[i]);
861 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
863 public AlignmentAnnotation[] getAnnotation(String label)
865 if (annotation==null || annotation.size()==0)
870 Vector subset = new Vector();
871 Enumeration e = annotation.elements();
872 while (e.hasMoreElements())
874 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
875 if (ann.label!=null && ann.label.equals(label))
877 subset.addElement(ann);
880 if (subset.size()==0)
884 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
886 e = subset.elements();
887 while (e.hasMoreElements())
889 anns[i++] = (AlignmentAnnotation) e.nextElement();
891 subset.removeAllElements();