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.
135 * @param seq DOCUMENT ME!
137 public Sequence(SequenceI seq)
143 description = seq.getDescription();
150 * @param v DOCUMENT ME!
152 public void setSequenceFeatures(SequenceFeature[] features)
154 sequenceFeatures = features;
157 public synchronized void addSequenceFeature(SequenceFeature sf)
159 if (sequenceFeatures == null)
161 sequenceFeatures = new SequenceFeature[0];
164 for (int i = 0; i < sequenceFeatures.length; i++)
166 if (sequenceFeatures[i].equals(sf))
172 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
173 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
174 temp[sequenceFeatures.length] = sf;
176 sequenceFeatures = temp;
179 public void deleteFeature(SequenceFeature sf)
181 if(sequenceFeatures==null)
187 for (index = 0; index < sequenceFeatures.length; index++)
189 if (sequenceFeatures[index].equals(sf))
196 if(index==sequenceFeatures.length)
201 int sfLength = sequenceFeatures.length;
204 sequenceFeatures = null;
208 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
209 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
213 System.arraycopy(sequenceFeatures,
216 index, sequenceFeatures.length - index -1);
219 sequenceFeatures = temp;
226 * @return DOCUMENT ME!
228 public SequenceFeature[] getSequenceFeatures()
230 return sequenceFeatures;
233 public void addPDBId(PDBEntry entry)
237 pdbIds = new Vector();
240 pdbIds.addElement(entry);
246 * @param id DOCUMENT ME!
248 public void setPDBId(Vector id)
256 * @return DOCUMENT ME!
258 public Vector getPDBId()
266 * @return DOCUMENT ME!
268 public String getDisplayId(boolean jvsuffix)
270 StringBuffer result = new StringBuffer(name);
273 result.append("/" + start + "-" + end);
276 return result.toString();
282 * @param name DOCUMENT ME!
284 public void setName(String name)
293 * @return DOCUMENT ME!
295 public String getName()
303 * @param start DOCUMENT ME!
305 public void setStart(int start)
313 * @return DOCUMENT ME!
315 public int getStart()
323 * @param end DOCUMENT ME!
325 public void setEnd(int end)
333 * @return DOCUMENT ME!
343 * @return DOCUMENT ME!
345 public int getLength()
347 return this.sequence.length;
353 * @param seq DOCUMENT ME!
355 public void setSequence(String seq)
357 this.sequence = seq.toCharArray();
362 public String getSequenceAsString()
364 return new String(sequence);
367 public String getSequenceAsString(int start, int end)
369 return new String(getSequence(start, end));
373 public char [] getSequence()
381 * @param start DOCUMENT ME!
382 * @param end DOCUMENT ME!
384 * @return DOCUMENT ME!
386 public char [] getSequence(int start, int end)
388 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
389 if (start >= sequence.length)
394 if (end >= sequence.length)
396 end = sequence.length;
399 char [] reply = new char[end-start];
400 System.arraycopy(sequence, start, reply, 0, end-start);
407 * make a new Sequence object from start to end (including gaps) over this seqeunce
412 public SequenceI getSubSequence(int start, int end)
418 char [] seq = getSequence(start, end);
423 int nstart = findPosition(start);
424 int nend = findPosition(end) - 1;
425 // JBPNote - this is an incomplete copy.
426 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
427 nseq.setDescription(description);
428 if (datasetSequence!=null)
430 nseq.setDatasetSequence(datasetSequence);
434 nseq.setDatasetSequence(this);
442 * @param i DOCUMENT ME!
444 * @return DOCUMENT ME!
446 public char getCharAt(int i)
448 if (i < sequence.length)
461 * @param desc DOCUMENT ME!
463 public void setDescription(String desc)
465 this.description = desc;
471 * @return DOCUMENT ME!
473 public String getDescription()
475 return this.description;
481 * @param pos DOCUMENT ME!
483 * @return DOCUMENT ME!
485 public int findIndex(int pos)
487 // returns the alignment position for a residue
491 while ( (i < sequence.length) && (j <= end) && (j <= pos))
493 if (!jalview.util.Comparison.isGap(sequence[i]))
501 if ( (j == end) && (j < pos))
512 * Returns the sequence position for an alignment position
514 * @param i column index in alignment (from 1)
516 * @return residue number for residue (left of and) nearest ith column
518 public int findPosition(int i)
522 int seqlen = sequence.length;
523 while ( (j < i) && (j < seqlen))
525 if (!jalview.util.Comparison.isGap( sequence[j] ))
537 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
539 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
541 public int[] gapMap()
543 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
544 GapChars, new String(sequence));
545 int[] map = new int[seq.length()];
549 while (j < sequence.length)
551 if (!jalview.util.Comparison.isGap(sequence[j]))
565 * @param i DOCUMENT ME!
566 * @param j DOCUMENT ME!
568 public void deleteChars(int i, int j)
570 if (i >= sequence.length)
577 if (j >= sequence.length)
580 System.arraycopy(sequence,0,tmp,0,i);
584 tmp = new char[sequence.length-j+i];
585 System.arraycopy(sequence,0,tmp,0,i);
586 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
589 if (this.datasetSequence != null)
591 for (int s = i; s < j; s++)
593 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
596 Sequence ds = new Sequence(name,
597 AlignSeq.extractGaps(
598 jalview.util.Comparison.GapChars,
599 this.getSequenceAsString()
603 ds.setDescription(description);
617 * @param i DOCUMENT ME!
618 * @param c DOCUMENT ME!
619 * @param chop DOCUMENT ME!
621 public void insertCharAt(int i, int length, char c)
623 char [] tmp = new char[sequence.length+length];
625 if (i >= sequence.length)
627 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
632 System.arraycopy(sequence, 0, tmp, 0, i);
643 if (i < sequence.length)
645 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
651 public void insertCharAt(int i, char c)
653 insertCharAt(i, 1, c);
656 public String getVamsasId()
661 public void setVamsasId(String id)
666 public void setDBRef(DBRefEntry[] dbref)
671 public DBRefEntry[] getDBRef()
676 public void addDBRef(DBRefEntry entry)
680 dbrefs = new DBRefEntry[0];
683 int i, iSize = dbrefs.length;
685 for(i=0; i<iSize; i++)
687 if(dbrefs[i].equals(entry))
693 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
694 System.arraycopy(dbrefs, 0, temp, 0, iSize);
695 temp[temp.length - 1] = entry;
700 public void setDatasetSequence(SequenceI seq)
702 datasetSequence = seq;
705 public SequenceI getDatasetSequence()
707 return datasetSequence;
710 public AlignmentAnnotation[] getAnnotation()
712 if (annotation == null)
717 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
718 for (int r = 0; r < ret.length; r++)
720 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
726 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
728 if (this.annotation == null)
730 this.annotation = new Vector();
733 this.annotation.addElement(annotation);
738 * test if this is a valid candidate for another
739 * sequence's dataset sequence.
742 private boolean isValidDatasetSequence()
744 if (datasetSequence!=null)
748 for (int i=0;i<sequence.length; i++)
750 if (jalview.util.Comparison.isGap(sequence[i]))
758 * @see jalview.datamodel.SequenceI#deriveSequence()
760 public SequenceI deriveSequence()
762 SequenceI seq = new Sequence(name, sequence, start, end);
763 seq.setDescription(description);
764 if (datasetSequence != null)
766 seq.setDatasetSequence(datasetSequence);
770 if (isValidDatasetSequence())
772 seq.setDatasetSequence(this);