2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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;
23 import jalview.analysis.AlignSeq;
32 public class Sequence implements SequenceI
34 SequenceI datasetSequence;
36 private char [] sequence;
44 /** This annotation is displayed below the alignment but the
45 * positions are tied to the residues of this sequence */
49 public SequenceFeature[] sequenceFeatures;
53 * Creates a new Sequence object.
55 * @param name DOCUMENT ME!
56 * @param sequence DOCUMENT ME!
57 * @param start DOCUMENT ME!
58 * @param end DOCUMENT ME!
60 public Sequence(String name, String sequence, int start, int end)
63 this.sequence = sequence.toCharArray();
70 public Sequence(String name, char [] sequence, int start, int end)
73 this.sequence = sequence;
80 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
81 "[/][0-9]{1,}[-][0-9]{1,}$");
82 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
87 // Does sequence have the /start-end signiature?
88 if (limitrx.search(name))
90 name = limitrx.left();
91 endrx.search(limitrx.stringMatched());
92 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
93 endrx.matchedFrom() - 1)));
94 setEnd(Integer.parseInt(endrx.stringMatched()));
98 void checkValidRange()
103 for (int j = 0; j < sequence.length; j++)
105 if (!jalview.util.Comparison.isGap( sequence[j] ))
121 * Creates a new Sequence object.
123 * @param name DOCUMENT ME!
124 * @param sequence DOCUMENT ME!
126 public Sequence(String name, String sequence)
128 this(name, sequence, 1, -1);
132 * Creates a new Sequence object.
134 * @param seq DOCUMENT ME!
136 public Sequence(SequenceI seq)
142 description = seq.getDescription();
149 * @param v DOCUMENT ME!
151 public void setSequenceFeatures(SequenceFeature[] features)
153 sequenceFeatures = features;
156 public synchronized void addSequenceFeature(SequenceFeature sf)
158 if (sequenceFeatures == null)
160 sequenceFeatures = new SequenceFeature[0];
163 for (int i = 0; i < sequenceFeatures.length; i++)
165 if (sequenceFeatures[i].equals(sf))
171 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
172 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
173 temp[sequenceFeatures.length] = sf;
175 sequenceFeatures = temp;
178 public void deleteFeature(SequenceFeature sf)
180 if(sequenceFeatures==null)
184 for (index = 0; index < sequenceFeatures.length; index++)
186 if (sequenceFeatures[index].equals(sf))
193 if(index==sequenceFeatures.length)
196 int sfLength = sequenceFeatures.length;
199 sequenceFeatures = null;
203 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
204 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
207 System.arraycopy(sequenceFeatures,
210 index, sequenceFeatures.length - index -1);
212 sequenceFeatures = temp;
219 * @return DOCUMENT ME!
221 public SequenceFeature[] getSequenceFeatures()
223 return sequenceFeatures;
226 public void addPDBId(PDBEntry entry)
229 pdbIds = new Vector();
231 pdbIds.addElement(entry);
237 * @param id DOCUMENT ME!
239 public void setPDBId(Vector id)
247 * @return DOCUMENT ME!
249 public Vector getPDBId()
257 * @return DOCUMENT ME!
259 public String getDisplayId(boolean jvsuffix)
261 StringBuffer result = new StringBuffer(name);
264 result.append("/" + start + "-" + end);
267 return result.toString();
273 * @param name DOCUMENT ME!
275 public void setName(String name)
284 * @return DOCUMENT ME!
286 public String getName()
294 * @param start DOCUMENT ME!
296 public void setStart(int start)
304 * @return DOCUMENT ME!
306 public int getStart()
314 * @param end DOCUMENT ME!
316 public void setEnd(int end)
324 * @return DOCUMENT ME!
334 * @return DOCUMENT ME!
336 public int getLength()
338 return this.sequence.length;
344 * @param seq DOCUMENT ME!
346 public void setSequence(String seq)
348 this.sequence = seq.toCharArray();
353 public String getSequenceAsString()
355 return new String(sequence);
358 public String getSequenceAsString(int start, int end)
360 return new String(getSequence(start, end));
364 public char [] getSequence()
372 * @param start DOCUMENT ME!
373 * @param end DOCUMENT ME!
375 * @return DOCUMENT ME!
377 public char [] getSequence(int start, int end)
379 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
380 if (start >= sequence.length)
385 if (end >= sequence.length)
387 end = sequence.length;
390 char [] reply = new char[end-start];
391 System.arraycopy(sequence, start, reply, 0, end-start);
398 * make a new Sequence object from start to end (including gaps) over this seqeunce
403 public SequenceI getSubSequence(int start, int end)
407 char [] seq = getSequence(start, end);
410 int nstart = findPosition(start);
411 int nend = findPosition(end) - 1;
412 // JBPNote - this is an incomplete copy.
413 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
414 nseq.setDescription(description);
415 if (datasetSequence!=null)
416 nseq.setDatasetSequence(datasetSequence);
418 nseq.setDatasetSequence(this);
425 * @param i DOCUMENT ME!
427 * @return DOCUMENT ME!
429 public char getCharAt(int i)
431 if (i < sequence.length)
444 * @param desc DOCUMENT ME!
446 public void setDescription(String desc)
448 this.description = desc;
454 * @return DOCUMENT ME!
456 public String getDescription()
458 return this.description;
464 * @param pos DOCUMENT ME!
466 * @return DOCUMENT ME!
468 public int findIndex(int pos)
470 // returns the alignment position for a residue
474 while ( (i < sequence.length) && (j <= end) && (j <= pos))
476 if (!jalview.util.Comparison.isGap(sequence[i]))
484 if ( (j == end) && (j < pos))
495 * Returns the sequence position for an alignment position
497 * @param i column index in alignment (from 1)
499 * @return residue number for residue (left of and) nearest ith column
501 public int findPosition(int i)
505 int seqlen = sequence.length;
506 while ( (j < i) && (j < seqlen))
508 if (!jalview.util.Comparison.isGap( sequence[j] ))
520 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
522 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
524 public int[] gapMap()
526 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
527 GapChars, new String(sequence));
528 int[] map = new int[seq.length()];
532 while (j < sequence.length)
534 if (!jalview.util.Comparison.isGap(sequence[j]))
548 * @param i DOCUMENT ME!
549 * @param j DOCUMENT ME!
551 public void deleteChars(int i, int j)
553 if (i >= sequence.length)
560 if (j >= sequence.length)
563 System.arraycopy(sequence,0,tmp,0,i);
567 tmp = new char[sequence.length-j+i];
568 System.arraycopy(sequence,0,tmp,0,i);
569 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
572 if (this.datasetSequence != null)
574 for (int s = i; s < j; s++)
576 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
579 Sequence ds = new Sequence(name,
580 AlignSeq.extractGaps(
581 jalview.util.Comparison.GapChars,
582 this.getSequenceAsString()
586 ds.setDescription(description);
600 * @param i DOCUMENT ME!
601 * @param c DOCUMENT ME!
602 * @param chop DOCUMENT ME!
604 public void insertCharAt(int i, int length, char c)
606 char [] tmp = new char[sequence.length+length];
608 if (i >= sequence.length)
610 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
615 System.arraycopy(sequence, 0, tmp, 0, i);
626 if (i < sequence.length)
628 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
634 public void insertCharAt(int i, char c)
636 insertCharAt(i, 1, c);
639 public String getVamsasId()
644 public void setVamsasId(String id)
649 public void setDBRef(DBRefEntry[] dbref)
654 public DBRefEntry[] getDBRef()
659 public void addDBRef(DBRefEntry entry)
662 dbrefs = new DBRefEntry[0];
664 int i, iSize = dbrefs.length;
666 for(i=0; i<iSize; i++)
667 if(dbrefs[i].getAccessionId().equals(entry.getAccessionId())
668 && dbrefs[i].getSource().equals(entry.getSource())
669 && dbrefs[i].getVersion().equals(entry.getVersion()))
674 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
675 System.arraycopy(dbrefs, 0, temp, 0, iSize);
676 temp[temp.length - 1] = entry;
681 public void setDatasetSequence(SequenceI seq)
683 datasetSequence = seq;
686 public SequenceI getDatasetSequence()
688 return datasetSequence;
691 public AlignmentAnnotation[] getAnnotation()
693 if (annotation == null)
696 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
697 for (int r = 0; r < ret.length; r++)
698 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
703 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
705 if (this.annotation == null)
706 this.annotation = new Vector();
708 this.annotation.addElement(annotation);
713 * test if this is a valid candidate for another
714 * sequence's dataset sequence.
717 private boolean isValidDatasetSequence() {
718 if (datasetSequence!=null)
720 for (int i=0;i<sequence.length; i++)
721 if (jalview.util.Comparison.isGap(sequence[i]))
726 * @see jalview.datamodel.SequenceI#deriveSequence()
728 public SequenceI deriveSequence() {
729 SequenceI seq = new Sequence(name, sequence, start, end);
730 seq.setDescription(description);
731 if (datasetSequence!=null) {
732 seq.setDatasetSequence(datasetSequence);
734 if (isValidDatasetSequence())
735 seq.setDatasetSequence(this);