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;
32 public class Sequence implements SequenceI
34 SequenceI datasetSequence;
36 private String sequence;
40 Color color = Color.white;
45 /** This annotation is displayed below the alignment but the
46 * positions are tied to the residues of this sequence */
50 public SequenceFeature [] sequenceFeatures;
52 /** This array holds hidden sequences
53 * of which this sequence is the representitive member of a group
55 SequenceGroup hiddenSequences;
58 * Creates a new Sequence object.
60 * @param name DOCUMENT ME!
61 * @param sequence DOCUMENT ME!
62 * @param start DOCUMENT ME!
63 * @param end DOCUMENT ME!
65 public Sequence(String name, String sequence, int start, int end)
68 this.sequence = sequence;
77 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
78 "[/][0-9]{1,}[-][0-9]{1,}$");
79 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
84 // Does sequence have the /start-end signiature?
85 if(limitrx.search(name))
87 name = limitrx.left();
88 endrx.search(limitrx.stringMatched());
89 setStart( Integer.parseInt( limitrx.stringMatched().substring(1,endrx.matchedFrom()-1 )));
90 setEnd( Integer.parseInt( endrx.stringMatched() ));
94 void checkValidRange()
100 for (int j = 0; j < sequence.length(); j++)
102 ch = sequence.charAt(j);
103 if (!jalview.util.Comparison.isGap( (ch)))
119 * Creates a new Sequence object.
121 * @param name DOCUMENT ME!
122 * @param sequence DOCUMENT ME!
124 public Sequence(String name, String sequence)
126 this(name, sequence, 1, -1);
130 * Creates a new Sequence object.
132 * @param seq DOCUMENT ME!
134 public Sequence(SequenceI seq)
136 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
142 * @param v DOCUMENT ME!
144 public void setSequenceFeatures(SequenceFeature [] features)
146 sequenceFeatures = features;
149 public synchronized void addSequenceFeature(SequenceFeature sf)
151 if(sequenceFeatures==null)
153 sequenceFeatures = new SequenceFeature[0];
156 for(int i=0; i<sequenceFeatures.length; i++)
158 if(sequenceFeatures[i].equals(sf))
164 SequenceFeature [] temp = new SequenceFeature[sequenceFeatures.length+1];
165 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
166 temp[sequenceFeatures.length] = sf;
168 sequenceFeatures = temp;
175 * @return DOCUMENT ME!
177 public SequenceFeature [] getSequenceFeatures()
179 return sequenceFeatures;
182 public void addPDBId(PDBEntry entry)
185 pdbIds = new Vector();
187 pdbIds.addElement(entry);
193 * @param id DOCUMENT ME!
195 public void setPDBId(Vector id)
203 * @return DOCUMENT ME!
205 public Vector getPDBId()
213 * @return DOCUMENT ME!
215 public String getDisplayId(boolean jvsuffix)
217 StringBuffer result = new StringBuffer(name);
220 result.append("/" + start + "-" + end);
223 return result.toString();
229 * @param name DOCUMENT ME!
231 public void setName(String name)
240 * @return DOCUMENT ME!
242 public String getName()
250 * @param start DOCUMENT ME!
252 public void setStart(int start)
260 * @return DOCUMENT ME!
262 public int getStart()
270 * @param end DOCUMENT ME!
272 public void setEnd(int end)
280 * @return DOCUMENT ME!
290 * @return DOCUMENT ME!
292 public int getLength()
294 return this.sequence.length();
300 * @param seq DOCUMENT ME!
302 public void setSequence(String seq)
311 * @return DOCUMENT ME!
313 public String getSequence()
315 return this.sequence;
321 * @param start DOCUMENT ME!
322 * @param end DOCUMENT ME!
324 * @return DOCUMENT ME!
326 public String getSequence(int start, int end)
328 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
329 if (start >= sequence.length())
334 if (end >= sequence.length())
336 end = sequence.length();
339 return this.sequence.substring(start, end);
342 * make a new Sequence object from start to end (including gaps) over this seqeunce
347 public SequenceI getSubSequence(int start, int end) {
350 String seq = getSequence(start, end);
353 int nstart = findPosition(start);
354 int nend=findPosition(end)-1;
355 // JBPNote - this is an incomplete copy.
356 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
357 nseq.setDatasetSequence(getDatasetSequence());
363 * @param i DOCUMENT ME!
365 * @return DOCUMENT ME!
367 public char getCharAt(int i)
369 if (i < sequence.length())
371 return sequence.charAt(i);
382 * @param desc DOCUMENT ME!
384 public void setDescription(String desc)
386 this.description = desc;
392 * @return DOCUMENT ME!
394 public String getDescription()
396 return this.description;
402 * @param pos DOCUMENT ME!
404 * @return DOCUMENT ME!
406 public int findIndex(int pos)
408 // returns the alignment position for a residue
412 while ((i < sequence.length()) && (j <= end) && (j <= pos))
414 if (!jalview.util.Comparison.isGap(sequence.charAt(i)))
422 if ((j == end) && (j < pos))
433 * Returns the sequence position for an alignment position
435 * @param i column index in alignment (from 1)
437 * @return residue number for residue (left of and) nearest ith column
439 public int findPosition(int i)
443 int seqlen=sequence.length();
444 while ((j < i) && (j < seqlen))
446 if (!jalview.util.Comparison.isGap((sequence.charAt(j))))
458 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
460 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
462 public int[] gapMap()
464 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sequence);
465 int[] map = new int[seq.length()];
469 while (j < sequence.length())
471 if (!jalview.util.Comparison.isGap(sequence.charAt(j)))
485 * @param i DOCUMENT ME!
487 public void deleteCharAt(int i)
489 if (i >= sequence.length())
494 sequence = sequence.substring(0, i) + sequence.substring(i + 1);
500 * @param i DOCUMENT ME!
501 * @param j DOCUMENT ME!
503 public void deleteChars(int i, int j)
505 if (i >= sequence.length())
510 if (j >= sequence.length())
512 sequence = sequence.substring(0, i);
516 sequence = sequence.substring(0, i) + sequence.substring(j);
524 * @param i DOCUMENT ME!
525 * @param c DOCUMENT ME!
526 * @param chop DOCUMENT ME!
528 public void insertCharAt(int i, char c)
530 String tmp = new String(sequence);
532 if (i < sequence.length())
534 sequence = tmp.substring(0, i) + String.valueOf(c) +
539 // JBPNote : padding char at end of sequence. We'll not get away with this when we insert residues, I bet!
540 char[] ch = new char[(1 + i) - sequence.length()];
542 for (int j = 0, k = ch.length; j < k; j++)
545 sequence = tmp + String.valueOf(ch);
552 * @param c DOCUMENT ME!
554 public void setColor(Color c)
562 * @return DOCUMENT ME!
564 public Color getColor()
569 public String getVamsasId()
574 public void setVamsasId(String id)
579 public void setDBRef(DBRefEntry [] dbref)
584 public DBRefEntry [] getDBRef()
589 public void addDBRef(DBRefEntry entry)
592 dbrefs = new DBRefEntry[0];
594 DBRefEntry [] temp = new DBRefEntry[dbrefs.length+1];
595 System.arraycopy(dbrefs, 0, temp, 0, dbrefs.length);
597 temp[temp.length-1] = entry;
602 public void setDatasetSequence(SequenceI seq)
604 datasetSequence = seq;
607 public SequenceI getDatasetSequence()
609 return datasetSequence;
612 public AlignmentAnnotation [] getAnnotation()
617 AlignmentAnnotation [] ret = new AlignmentAnnotation[annotation.size()];
618 for(int r = 0; r<ret.length; r++)
619 ret[r] = (AlignmentAnnotation)annotation.elementAt(r);
624 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
626 if(this.annotation==null)
627 this.annotation = new Vector();
629 this.annotation.addElement( annotation );
632 public SequenceGroup getHiddenSequences()
634 return hiddenSequences;
637 public void addHiddenSequence(SequenceI seq)
639 if(hiddenSequences==null)
641 hiddenSequences = new SequenceGroup();
643 hiddenSequences.addSequence(seq, false);
646 public void showHiddenSequence(SequenceI seq)
648 hiddenSequences.deleteSequence(seq, false);
649 if (hiddenSequences.getSize(false) < 1)
651 hiddenSequences = null;
655 public void changeCase(boolean toUpper, int start, int end)
657 StringBuffer newSeq = new StringBuffer();
659 if(end>sequence.length())
660 end = sequence.length();
664 newSeq.append(sequence.substring(0, start));
668 newSeq.append(sequence.substring(start, end).toUpperCase());
670 newSeq.append(sequence.substring(start, end).toLowerCase());
672 if (end < sequence.length())
673 newSeq.append(sequence.substring(end));
675 sequence = newSeq.toString();
678 public void toggleCase(int start, int end)
680 StringBuffer newSeq = new StringBuffer();
682 if(end>sequence.length())
683 end = sequence.length();
687 newSeq.append(sequence.substring(0, start));
691 for(int c=start; c<end; c++)
693 nextChar = sequence.charAt(c);
694 if(Character.isLetter(nextChar))
696 if(Character.isUpperCase(nextChar))
697 nextChar = Character.toLowerCase(nextChar);
699 nextChar = Character.toUpperCase(nextChar);
703 newSeq.append(nextChar);
706 if (end < sequence.length())
707 newSeq.append(sequence.substring(end));
709 sequence = newSeq.toString();
712 public SequenceI getSubSequence(int start)
717 return getSubSequence(start, getLength());
720 public int removeGaps() {
722 return removeGaps(0, getLength());
726 public int removeGaps(int start, int end) {
727 int jSize = getLength();
734 // Removing a range is much quicker than removing gaps
735 // one by one for long sequences
737 int rangeStart=-1, rangeEnd=-1;
741 if (jalview.util.Comparison.isGap(getCharAt(j)))
758 deleteChars(rangeStart, rangeEnd);
759 j-=rangeEnd-rangeStart;
760 jSize-=rangeEnd-rangeStart;
768 while (j < end && j < jSize);
771 deleteChars(rangeStart, rangeEnd);
772 jSize-=rangeEnd-rangeStart;
774 return oSize-jSize; // number of deleted characters.