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;
31 public class Sequence implements SequenceI
33 SequenceI datasetSequence;
35 private char [] sequence;
43 /** This annotation is displayed below the alignment but the
44 * positions are tied to the residues of this sequence */
48 public SequenceFeature[] sequenceFeatures;
52 * Creates a new Sequence object.
54 * @param name DOCUMENT ME!
55 * @param sequence DOCUMENT ME!
56 * @param start DOCUMENT ME!
57 * @param end DOCUMENT ME!
59 public Sequence(String name, String sequence, int start, int end)
62 this.sequence = sequence.toCharArray();
69 public Sequence(String name, char [] sequence, int start, int end)
72 this.sequence = sequence;
79 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
80 "[/][0-9]{1,}[-][0-9]{1,}$");
81 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
86 // Does sequence have the /start-end signiature?
87 if (limitrx.search(name))
89 name = limitrx.left();
90 endrx.search(limitrx.stringMatched());
91 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
92 endrx.matchedFrom() - 1)));
93 setEnd(Integer.parseInt(endrx.stringMatched()));
97 void checkValidRange()
102 for (int j = 0; j < sequence.length; j++)
104 if (!jalview.util.Comparison.isGap( sequence[j] ))
120 * Creates a new Sequence object.
122 * @param name DOCUMENT ME!
123 * @param sequence DOCUMENT ME!
125 public Sequence(String name, String sequence)
127 this(name, sequence, 1, -1);
131 * Creates a new Sequence object.
133 * @param seq DOCUMENT ME!
135 public Sequence(SequenceI seq)
141 description = seq.getDescription();
148 * @param v DOCUMENT ME!
150 public void setSequenceFeatures(SequenceFeature[] features)
152 sequenceFeatures = features;
155 public synchronized void addSequenceFeature(SequenceFeature sf)
157 if (sequenceFeatures == null)
159 sequenceFeatures = new SequenceFeature[0];
162 for (int i = 0; i < sequenceFeatures.length; i++)
164 if (sequenceFeatures[i].equals(sf))
170 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
171 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
172 temp[sequenceFeatures.length] = sf;
174 sequenceFeatures = temp;
177 public void deleteFeature(SequenceFeature sf)
179 if(sequenceFeatures==null)
183 for (index = 0; index < sequenceFeatures.length; index++)
185 if (sequenceFeatures[index].equals(sf))
192 if(index==sequenceFeatures.length)
195 int sfLength = sequenceFeatures.length;
198 sequenceFeatures = null;
202 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
203 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
206 System.arraycopy(sequenceFeatures,
209 index, sequenceFeatures.length - index -1);
211 sequenceFeatures = temp;
218 * @return DOCUMENT ME!
220 public SequenceFeature[] getSequenceFeatures()
222 return sequenceFeatures;
225 public void addPDBId(PDBEntry entry)
228 pdbIds = new Vector();
230 pdbIds.addElement(entry);
236 * @param id DOCUMENT ME!
238 public void setPDBId(Vector id)
246 * @return DOCUMENT ME!
248 public Vector getPDBId()
256 * @return DOCUMENT ME!
258 public String getDisplayId(boolean jvsuffix)
260 StringBuffer result = new StringBuffer(name);
263 result.append("/" + start + "-" + end);
266 return result.toString();
272 * @param name DOCUMENT ME!
274 public void setName(String name)
283 * @return DOCUMENT ME!
285 public String getName()
293 * @param start DOCUMENT ME!
295 public void setStart(int start)
303 * @return DOCUMENT ME!
305 public int getStart()
313 * @param end DOCUMENT ME!
315 public void setEnd(int end)
323 * @return DOCUMENT ME!
333 * @return DOCUMENT ME!
335 public int getLength()
337 return this.sequence.length;
343 * @param seq DOCUMENT ME!
345 public void setSequence(String seq)
347 this.sequence = seq.toCharArray();
352 public String getSequenceAsString()
354 return new String(sequence);
357 public String getSequenceAsString(int start, int end)
359 return new String(getSequence(start, end));
363 public char [] getSequence()
371 * @param start DOCUMENT ME!
372 * @param end DOCUMENT ME!
374 * @return DOCUMENT ME!
376 public char [] getSequence(int start, int end)
378 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
379 if (start >= sequence.length)
384 if (end >= sequence.length)
386 end = sequence.length;
389 char [] reply = new char[end-start];
390 System.arraycopy(sequence, start, reply, 0, end-start);
397 * make a new Sequence object from start to end (including gaps) over this seqeunce
402 public SequenceI getSubSequence(int start, int end)
406 char [] seq = getSequence(start, end);
409 int nstart = findPosition(start);
410 int nend = findPosition(end) - 1;
411 // JBPNote - this is an incomplete copy.
412 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
413 nseq.setDescription(description);
414 if (datasetSequence!=null)
415 nseq.setDatasetSequence(datasetSequence);
417 nseq.setDatasetSequence(this);
424 * @param i DOCUMENT ME!
426 * @return DOCUMENT ME!
428 public char getCharAt(int i)
430 if (i < sequence.length)
443 * @param desc DOCUMENT ME!
445 public void setDescription(String desc)
447 this.description = desc;
453 * @return DOCUMENT ME!
455 public String getDescription()
457 return this.description;
463 * @param pos DOCUMENT ME!
465 * @return DOCUMENT ME!
467 public int findIndex(int pos)
469 // returns the alignment position for a residue
473 while ( (i < sequence.length) && (j <= end) && (j <= pos))
475 if (!jalview.util.Comparison.isGap(sequence[i]))
483 if ( (j == end) && (j < pos))
494 * Returns the sequence position for an alignment position
496 * @param i column index in alignment (from 1)
498 * @return residue number for residue (left of and) nearest ith column
500 public int findPosition(int i)
504 int seqlen = sequence.length;
505 while ( (j < i) && (j < seqlen))
507 if (!jalview.util.Comparison.isGap( sequence[j] ))
519 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
521 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
523 public int[] gapMap()
525 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
526 GapChars, new String(sequence));
527 int[] map = new int[seq.length()];
531 while (j < sequence.length)
533 if (!jalview.util.Comparison.isGap(sequence[j]))
547 * @param i DOCUMENT ME!
548 * @param j DOCUMENT ME!
550 public void deleteChars(int i, int j)
552 if (i >= sequence.length)
559 if (j >= sequence.length)
562 System.arraycopy(sequence,0,tmp,0,i);
566 tmp = new char[sequence.length-j+i];
567 System.arraycopy(sequence,0,tmp,0,i);
568 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
577 * @param i DOCUMENT ME!
578 * @param c DOCUMENT ME!
579 * @param chop DOCUMENT ME!
581 public void insertCharAt(int i, int length, char c)
583 char [] tmp = new char[sequence.length+length];
585 if (i >= sequence.length)
587 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
592 System.arraycopy(sequence, 0, tmp, 0, i);
603 if (i < sequence.length)
605 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
611 public void insertCharAt(int i, char c)
613 insertCharAt(i, 1, c);
616 public String getVamsasId()
621 public void setVamsasId(String id)
626 public void setDBRef(DBRefEntry[] dbref)
631 public DBRefEntry[] getDBRef()
636 public void addDBRef(DBRefEntry entry)
639 dbrefs = new DBRefEntry[0];
641 int i, iSize = dbrefs.length;
643 for(i=0; i<iSize; i++)
644 if(dbrefs[i].getAccessionId().equals(entry.getAccessionId())
645 && dbrefs[i].getSource().equals(entry.getSource())
646 && dbrefs[i].getVersion().equals(entry.getVersion()))
651 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
652 System.arraycopy(dbrefs, 0, temp, 0, iSize);
653 temp[temp.length - 1] = entry;
658 public void setDatasetSequence(SequenceI seq)
660 datasetSequence = seq;
663 public SequenceI getDatasetSequence()
665 return datasetSequence;
668 public AlignmentAnnotation[] getAnnotation()
670 if (annotation == null)
673 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
674 for (int r = 0; r < ret.length; r++)
675 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
680 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
682 if (this.annotation == null)
683 this.annotation = new Vector();
685 this.annotation.addElement(annotation);
690 * test if this is a valid candidate for another
691 * sequence's dataset sequence.
694 private boolean isValidDatasetSequence() {
695 if (datasetSequence!=null)
697 for (int i=0;i<sequence.length; i++)
698 if (jalview.util.Comparison.isGap(sequence[i]))
703 * @see jalview.datamodel.SequenceI#deriveSequence()
705 public SequenceI deriveSequence() {
706 SequenceI seq = new Sequence(name, sequence, start, end);
707 seq.setDescription(description);
708 if (datasetSequence!=null) {
709 seq.setDatasetSequence(datasetSequence);
711 if (isValidDatasetSequence())
712 seq.setDatasetSequence(this);