2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 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)-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))))
460 * @return DOCUMENT ME!
462 public int[] gapMap()
464 // Returns an int array giving the position of each residue in the sequence in the alignment
465 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sequence);
466 int[] map = new int[seq.length()];
470 while (j < sequence.length())
472 if (!jalview.util.Comparison.isGap(sequence.charAt(j)))
486 * @param i DOCUMENT ME!
488 public void deleteCharAt(int i)
490 if (i >= sequence.length())
495 sequence = sequence.substring(0, i) + sequence.substring(i + 1);
501 * @param i DOCUMENT ME!
502 * @param j DOCUMENT ME!
504 public void deleteChars(int i, int j)
506 if (i >= sequence.length())
511 if (j >= sequence.length())
513 sequence = sequence.substring(0, i);
517 sequence = sequence.substring(0, i) + sequence.substring(j);
525 * @param i DOCUMENT ME!
526 * @param c DOCUMENT ME!
527 * @param chop DOCUMENT ME!
529 public void insertCharAt(int i, char c)
531 String tmp = new String(sequence);
533 if (i < sequence.length())
535 sequence = tmp.substring(0, i) + String.valueOf(c) +
540 // JBPNote : padding char at end of sequence. We'll not get away with this when we insert residues, I bet!
541 char[] ch = new char[(1 + i) - sequence.length()];
543 for (int j = 0, k = ch.length; j < k; j++)
546 sequence = tmp + String.valueOf(ch);
553 * @param c DOCUMENT ME!
555 public void setColor(Color c)
563 * @return DOCUMENT ME!
565 public Color getColor()
570 public String getVamsasId()
575 public void setVamsasId(String id)
580 public void setDBRef(DBRefEntry [] dbref)
585 public DBRefEntry [] getDBRef()
590 public void addDBRef(DBRefEntry entry)
593 dbrefs = new DBRefEntry[0];
595 DBRefEntry [] temp = new DBRefEntry[dbrefs.length+1];
596 System.arraycopy(dbrefs, 0, temp, 0, dbrefs.length);
598 temp[temp.length-1] = entry;
603 public void setDatasetSequence(SequenceI seq)
605 datasetSequence = seq;
608 public SequenceI getDatasetSequence()
610 return datasetSequence;
613 public AlignmentAnnotation [] getAnnotation()
618 AlignmentAnnotation [] ret = new AlignmentAnnotation[annotation.size()];
619 for(int r = 0; r<ret.length; r++)
620 ret[r] = (AlignmentAnnotation)annotation.elementAt(r);
625 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
627 if(this.annotation==null)
628 this.annotation = new Vector();
630 this.annotation.addElement( annotation );
633 public SequenceGroup getHiddenSequences()
635 return hiddenSequences;
638 public void addHiddenSequence(SequenceI seq)
640 if(hiddenSequences==null)
642 hiddenSequences = new SequenceGroup();
644 hiddenSequences.addSequence(seq, false);
647 public void showHiddenSequence(SequenceI seq)
649 hiddenSequences.deleteSequence(seq, false);
650 if (hiddenSequences.getSize(false) < 1)
652 hiddenSequences = null;
656 public void changeCase(boolean toUpper, int start, int end)
658 StringBuffer newSeq = new StringBuffer();
660 if(end>sequence.length())
661 end = sequence.length();
665 newSeq.append(sequence.substring(0, start));
669 newSeq.append(sequence.substring(start, end).toUpperCase());
671 newSeq.append(sequence.substring(start, end).toLowerCase());
673 if (end < sequence.length())
674 newSeq.append(sequence.substring(end));
676 sequence = newSeq.toString();
679 public void toggleCase(int start, int end)
681 StringBuffer newSeq = new StringBuffer();
683 if(end>sequence.length())
684 end = sequence.length();
688 newSeq.append(sequence.substring(0, start));
692 for(int c=start; c<end; c++)
694 nextChar = sequence.charAt(c);
695 if(Character.isLetter(nextChar))
697 if(Character.isUpperCase(nextChar))
698 nextChar = Character.toLowerCase(nextChar);
700 nextChar = Character.toUpperCase(nextChar);
704 newSeq.append(nextChar);
707 if (end < sequence.length())
708 newSeq.append(sequence.substring(end));
710 sequence = newSeq.toString();