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;
23 import jalview.analysis.*;
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;
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();
147 * @param v DOCUMENT ME!
149 public void setSequenceFeatures(SequenceFeature[] features)
151 sequenceFeatures = features;
154 public synchronized void addSequenceFeature(SequenceFeature sf)
156 if (sequenceFeatures == null)
158 sequenceFeatures = new SequenceFeature[0];
161 for (int i = 0; i < sequenceFeatures.length; i++)
163 if (sequenceFeatures[i].equals(sf))
169 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
170 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
171 temp[sequenceFeatures.length] = sf;
173 sequenceFeatures = temp;
176 public void deleteFeature(SequenceFeature sf)
178 if (sequenceFeatures == null)
184 for (index = 0; index < sequenceFeatures.length; index++)
186 if (sequenceFeatures[index].equals(sf))
192 if (index == sequenceFeatures.length)
197 int sfLength = sequenceFeatures.length;
200 sequenceFeatures = null;
204 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
205 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
207 if (index < sfLength)
209 System.arraycopy(sequenceFeatures,
212 index, sequenceFeatures.length - index - 1);
215 sequenceFeatures = temp;
222 * @return DOCUMENT ME!
224 public SequenceFeature[] getSequenceFeatures()
226 return sequenceFeatures;
229 public void addPDBId(PDBEntry entry)
233 pdbIds = new Vector();
236 pdbIds.addElement(entry);
242 * @param id DOCUMENT ME!
244 public void setPDBId(Vector id)
252 * @return DOCUMENT ME!
254 public Vector getPDBId()
262 * @return DOCUMENT ME!
264 public String getDisplayId(boolean jvsuffix)
266 StringBuffer result = new StringBuffer(name);
269 result.append("/" + start + "-" + end);
272 return result.toString();
278 * @param name DOCUMENT ME!
280 public void setName(String name)
289 * @return DOCUMENT ME!
291 public String getName()
299 * @param start DOCUMENT ME!
301 public void setStart(int start)
309 * @return DOCUMENT ME!
311 public int getStart()
319 * @param end DOCUMENT ME!
321 public void setEnd(int end)
329 * @return DOCUMENT ME!
339 * @return DOCUMENT ME!
341 public int getLength()
343 return this.sequence.length;
349 * @param seq DOCUMENT ME!
351 public void setSequence(String seq)
353 this.sequence = seq.toCharArray();
357 public String getSequenceAsString()
359 return new String(sequence);
362 public String getSequenceAsString(int start, int end)
364 return new String(getSequence(start, end));
367 public char[] getSequence()
375 * @param start DOCUMENT ME!
376 * @param end DOCUMENT ME!
378 * @return DOCUMENT ME!
380 public char[] getSequence(int start, int end)
382 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
383 if (start >= sequence.length)
388 if (end >= sequence.length)
390 end = sequence.length;
393 char[] reply = new char[end - start];
394 System.arraycopy(sequence, start, reply, 0, end - start);
400 * make a new Sequence object from start to end (including gaps) over this seqeunce
405 public SequenceI getSubSequence(int start, int end)
411 char[] seq = getSequence(start, end);
416 int nstart = findPosition(start);
417 int nend = findPosition(end) - 1;
418 // JBPNote - this is an incomplete copy.
419 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
420 nseq.setDescription(description);
421 if (datasetSequence != null)
423 nseq.setDatasetSequence(datasetSequence);
427 nseq.setDatasetSequence(this);
435 * @param i DOCUMENT ME!
437 * @return DOCUMENT ME!
439 public char getCharAt(int i)
441 if (i < sequence.length)
454 * @param desc DOCUMENT ME!
456 public void setDescription(String desc)
458 this.description = desc;
464 * @return DOCUMENT ME!
466 public String getDescription()
468 return this.description;
474 * @param pos DOCUMENT ME!
476 * @return DOCUMENT ME!
478 public int findIndex(int pos)
480 // returns the alignment position for a residue
484 while ( (i < sequence.length) && (j <= end) && (j <= pos))
486 if (!jalview.util.Comparison.isGap(sequence[i]))
494 if ( (j == end) && (j < pos))
505 * Returns the sequence position for an alignment position
507 * @param i column index in alignment (from 1)
509 * @return residue number for residue (left of and) nearest ith column
511 public int findPosition(int i)
515 int seqlen = sequence.length;
516 while ( (j < i) && (j < seqlen))
518 if (!jalview.util.Comparison.isGap(sequence[j]))
530 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
532 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
534 public int[] gapMap()
536 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
537 GapChars, new String(sequence));
538 int[] map = new int[seq.length()];
542 while (j < sequence.length)
544 if (!jalview.util.Comparison.isGap(sequence[j]))
558 * @param i DOCUMENT ME!
559 * @param j DOCUMENT ME!
561 public void deleteChars(int i, int j)
563 if (i >= sequence.length)
570 if (j >= sequence.length)
573 System.arraycopy(sequence, 0, tmp, 0, i);
577 tmp = new char[sequence.length - j + i];
578 System.arraycopy(sequence, 0, tmp, 0, i);
579 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
582 if (this.datasetSequence != null)
584 for (int s = i; s < j; s++)
586 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
589 Sequence ds = new Sequence(name,
590 AlignSeq.extractGaps(
591 jalview.util.Comparison.GapChars,
592 this.getSequenceAsString()
596 ds.setDescription(description);
609 * @param i DOCUMENT ME!
610 * @param c DOCUMENT ME!
611 * @param chop DOCUMENT ME!
613 public void insertCharAt(int i, int length, char c)
615 char[] tmp = new char[sequence.length + length];
617 if (i >= sequence.length)
619 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
624 System.arraycopy(sequence, 0, tmp, 0, i);
634 if (i < sequence.length)
636 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
642 public void insertCharAt(int i, char c)
644 insertCharAt(i, 1, c);
647 public String getVamsasId()
652 public void setVamsasId(String id)
657 public void setDBRef(DBRefEntry[] dbref)
662 public DBRefEntry[] getDBRef()
667 public void addDBRef(DBRefEntry entry)
671 dbrefs = new DBRefEntry[0];
674 int i, iSize = dbrefs.length;
676 for (i = 0; i < iSize; i++)
678 if (dbrefs[i].getAccessionId().equals(entry.getAccessionId())
679 && dbrefs[i].getSource().equals(entry.getSource())
680 && dbrefs[i].getVersion().equals(entry.getVersion()))
686 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
687 System.arraycopy(dbrefs, 0, temp, 0, iSize);
688 temp[temp.length - 1] = entry;
693 public void setDatasetSequence(SequenceI seq)
695 datasetSequence = seq;
698 public SequenceI getDatasetSequence()
700 return datasetSequence;
703 public AlignmentAnnotation[] getAnnotation()
705 if (annotation == null)
710 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
711 for (int r = 0; r < ret.length; r++)
713 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
719 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
721 if (this.annotation == null)
723 this.annotation = new Vector();
726 this.annotation.addElement(annotation);
730 * test if this is a valid candidate for another
731 * sequence's dataset sequence.
734 private boolean isValidDatasetSequence()
736 if (datasetSequence != null)
740 for (int i = 0; i < sequence.length; i++)
742 if (jalview.util.Comparison.isGap(sequence[i]))
751 * @see jalview.datamodel.SequenceI#deriveSequence()
753 public SequenceI deriveSequence()
755 SequenceI seq = new Sequence(name, sequence, start, end);
756 seq.setDescription(description);
757 if (datasetSequence != null)
759 seq.setDatasetSequence(datasetSequence);
763 if (isValidDatasetSequence())
765 seq.setDatasetSequence(this);