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
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;
51 /** This array holds hidden sequences
52 * of which this sequence is the representitive member of a group
54 SequenceGroup hiddenSequences;
57 * Creates a new Sequence object.
59 * @param name DOCUMENT ME!
60 * @param sequence DOCUMENT ME!
61 * @param start DOCUMENT ME!
62 * @param end DOCUMENT ME!
64 public Sequence(String name, String sequence, int start, int end)
67 this.sequence = sequence.toCharArray();
74 public Sequence(String name, char [] sequence, int start, int end)
77 this.sequence = sequence;
84 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
85 "[/][0-9]{1,}[-][0-9]{1,}$");
86 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
91 // Does sequence have the /start-end signiature?
92 if (limitrx.search(name))
94 name = limitrx.left();
95 endrx.search(limitrx.stringMatched());
96 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
97 endrx.matchedFrom() - 1)));
98 setEnd(Integer.parseInt(endrx.stringMatched()));
102 void checkValidRange()
107 for (int j = 0; j < sequence.length; j++)
109 if (!jalview.util.Comparison.isGap( sequence[j] ))
125 * Creates a new Sequence object.
127 * @param name DOCUMENT ME!
128 * @param sequence DOCUMENT ME!
130 public Sequence(String name, String sequence)
132 this(name, sequence, 1, -1);
136 * Creates a new Sequence object.
138 * @param seq DOCUMENT ME!
140 public Sequence(SequenceI seq)
146 description = seq.getDescription();
153 * @param v DOCUMENT ME!
155 public void setSequenceFeatures(SequenceFeature[] features)
157 sequenceFeatures = features;
160 public synchronized void addSequenceFeature(SequenceFeature sf)
162 if (sequenceFeatures == null)
164 sequenceFeatures = new SequenceFeature[0];
167 for (int i = 0; i < sequenceFeatures.length; i++)
169 if (sequenceFeatures[i].equals(sf))
175 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
176 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
177 temp[sequenceFeatures.length] = sf;
179 sequenceFeatures = temp;
182 public void deleteFeature(SequenceFeature sf)
184 if(sequenceFeatures==null)
188 for (index = 0; index < sequenceFeatures.length; index++)
190 if (sequenceFeatures[index].equals(sf))
197 if(index==sequenceFeatures.length)
200 int sfLength = sequenceFeatures.length;
203 sequenceFeatures = null;
207 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
208 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
211 System.arraycopy(sequenceFeatures,
214 index, sequenceFeatures.length - index -1);
216 sequenceFeatures = temp;
223 * @return DOCUMENT ME!
225 public SequenceFeature[] getSequenceFeatures()
227 return sequenceFeatures;
230 public void addPDBId(PDBEntry entry)
233 pdbIds = new Vector();
235 pdbIds.addElement(entry);
241 * @param id DOCUMENT ME!
243 public void setPDBId(Vector id)
251 * @return DOCUMENT ME!
253 public Vector getPDBId()
261 * @return DOCUMENT ME!
263 public String getDisplayId(boolean jvsuffix)
265 StringBuffer result = new StringBuffer(name);
268 result.append("/" + start + "-" + end);
271 return result.toString();
277 * @param name DOCUMENT ME!
279 public void setName(String name)
288 * @return DOCUMENT ME!
290 public String getName()
298 * @param start DOCUMENT ME!
300 public void setStart(int start)
308 * @return DOCUMENT ME!
310 public int getStart()
318 * @param end DOCUMENT ME!
320 public void setEnd(int end)
328 * @return DOCUMENT ME!
338 * @return DOCUMENT ME!
340 public int getLength()
342 return this.sequence.length;
348 * @param seq DOCUMENT ME!
350 public void setSequence(String seq)
352 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));
368 public char [] getSequence()
376 * @param start DOCUMENT ME!
377 * @param end DOCUMENT ME!
379 * @return DOCUMENT ME!
381 public char [] getSequence(int start, int end)
383 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
384 if (start >= sequence.length)
389 if (end >= sequence.length)
391 end = sequence.length;
394 char [] reply = new char[end-start];
395 System.arraycopy(sequence, start, reply, 0, end-start);
402 * make a new Sequence object from start to end (including gaps) over this seqeunce
407 public SequenceI getSubSequence(int start, int end)
411 char [] seq = getSequence(start, end);
414 int nstart = findPosition(start);
415 int nend = findPosition(end) - 1;
416 // JBPNote - this is an incomplete copy.
417 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
418 nseq.setDescription(description);
419 if (datasetSequence!=null)
420 nseq.setDatasetSequence(datasetSequence);
422 nseq.setDatasetSequence(this);
429 * @param i DOCUMENT ME!
431 * @return DOCUMENT ME!
433 public char getCharAt(int i)
435 if (i < sequence.length)
448 * @param desc DOCUMENT ME!
450 public void setDescription(String desc)
452 this.description = desc;
458 * @return DOCUMENT ME!
460 public String getDescription()
462 return this.description;
468 * @param pos DOCUMENT ME!
470 * @return DOCUMENT ME!
472 public int findIndex(int pos)
474 // returns the alignment position for a residue
478 while ( (i < sequence.length) && (j <= end) && (j <= pos))
480 if (!jalview.util.Comparison.isGap(sequence[i]))
488 if ( (j == end) && (j < pos))
499 * Returns the sequence position for an alignment position
501 * @param i column index in alignment (from 1)
503 * @return residue number for residue (left of and) nearest ith column
505 public int findPosition(int i)
509 int seqlen = sequence.length;
510 while ( (j < i) && (j < seqlen))
512 if (!jalview.util.Comparison.isGap( sequence[j] ))
524 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
526 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
528 public int[] gapMap()
530 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
531 GapChars, new String(sequence));
532 int[] map = new int[seq.length()];
536 while (j < sequence.length)
538 if (!jalview.util.Comparison.isGap(sequence[j]))
552 * @param i DOCUMENT ME!
553 * @param j DOCUMENT ME!
555 public void deleteChars(int i, int j)
557 if (i >= sequence.length)
564 if (j >= sequence.length)
567 System.arraycopy(sequence,0,tmp,0,i);
571 tmp = new char[sequence.length-j+i];
572 System.arraycopy(sequence,0,tmp,0,i);
573 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
582 * @param i DOCUMENT ME!
583 * @param c DOCUMENT ME!
584 * @param chop DOCUMENT ME!
586 public void insertCharAt(int i, int length, char c)
588 char [] tmp = new char[sequence.length+length];
590 if (i >= sequence.length)
592 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
597 System.arraycopy(sequence, 0, tmp, 0, i);
608 if (i < sequence.length)
610 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
616 public void insertCharAt(int i, char c)
618 insertCharAt(i, 1, c);
621 public String getVamsasId()
626 public void setVamsasId(String id)
631 public void setDBRef(DBRefEntry[] dbref)
636 public DBRefEntry[] getDBRef()
641 public void addDBRef(DBRefEntry entry)
644 dbrefs = new DBRefEntry[0];
646 int i, iSize = dbrefs.length;
648 for(i=0; i<iSize; i++)
649 if(dbrefs[i].getAccessionId().equals(entry.getAccessionId())
650 && dbrefs[i].getSource().equals(entry.getSource())
651 && dbrefs[i].getVersion().equals(entry.getVersion()))
656 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
657 System.arraycopy(dbrefs, 0, temp, 0, iSize);
658 temp[temp.length - 1] = entry;
663 public void setDatasetSequence(SequenceI seq)
665 datasetSequence = seq;
668 public SequenceI getDatasetSequence()
670 return datasetSequence;
673 public AlignmentAnnotation[] getAnnotation()
675 if (annotation == null)
678 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
679 for (int r = 0; r < ret.length; r++)
680 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
685 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
687 if (this.annotation == null)
688 this.annotation = new Vector();
690 this.annotation.addElement(annotation);
693 public SequenceGroup getHiddenSequences()
695 return hiddenSequences;
698 public void addHiddenSequence(SequenceI seq)
700 if (hiddenSequences == null)
702 hiddenSequences = new SequenceGroup();
704 hiddenSequences.addSequence(seq, false);
707 public void showHiddenSequence(SequenceI seq)
709 hiddenSequences.deleteSequence(seq, false);
710 if (hiddenSequences.getSize(false) < 1)
712 hiddenSequences = null;
716 * test if this is a valid candidate for another
717 * sequence's dataset sequence.
720 private boolean isValidDatasetSequence() {
721 if (datasetSequence!=null)
723 for (int i=0;i<sequence.length; i++)
724 if (jalview.util.Comparison.isGap(sequence[i]))
729 * @see jalview.datamodel.SequenceI#deriveSequence()
731 public SequenceI deriveSequence() {
732 SequenceI seq = new Sequence(name, sequence, start, end);
733 seq.setDescription(description);
734 if (datasetSequence!=null) {
735 seq.setDatasetSequence(datasetSequence);
737 if (isValidDatasetSequence())
738 seq.setDatasetSequence(this);