2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.datamodel;
\r
30 * @version $Revision$
\r
32 public class Sequence implements SequenceI
\r
34 SequenceI datasetSequence;
\r
36 private String sequence;
\r
40 Color color = Color.white;
\r
43 DBRefEntry [] dbrefs;
\r
45 /** This annotation is displayed below the alignment but the
\r
46 * positions are tied to the residues of this sequence */
\r
49 /** DOCUMENT ME!! */
\r
50 public SequenceFeature [] sequenceFeatures;
\r
52 /** This array holds hidden sequences
\r
53 * of which this sequence is the representitive member of a group
\r
55 SequenceGroup hiddenSequences;
\r
58 * Creates a new Sequence object.
\r
60 * @param name DOCUMENT ME!
\r
61 * @param sequence DOCUMENT ME!
\r
62 * @param start DOCUMENT ME!
\r
63 * @param end DOCUMENT ME!
\r
65 public Sequence(String name, String sequence, int start, int end)
\r
68 this.sequence = sequence;
\r
77 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
\r
78 "[/][0-9]{1,}[-][0-9]{1,}$");
\r
79 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
\r
84 // Does sequence have the /start-end signiature?
\r
85 if(limitrx.search(name))
\r
87 name = limitrx.left();
\r
88 endrx.search(limitrx.stringMatched());
\r
89 setStart( Integer.parseInt( limitrx.stringMatched().substring(1,endrx.matchedFrom()-1 )));
\r
90 setEnd( Integer.parseInt( endrx.stringMatched() ));
\r
94 void checkValidRange()
\r
100 for (int j = 0; j < sequence.length(); j++)
\r
102 ch = sequence.charAt(j);
\r
103 if (!jalview.util.Comparison.isGap( (ch)))
\r
110 endRes += start - 1;
\r
119 * Creates a new Sequence object.
\r
121 * @param name DOCUMENT ME!
\r
122 * @param sequence DOCUMENT ME!
\r
124 public Sequence(String name, String sequence)
\r
126 this(name, sequence, 1, -1);
\r
130 * Creates a new Sequence object.
\r
132 * @param seq DOCUMENT ME!
\r
134 public Sequence(SequenceI seq)
\r
136 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
\r
142 * @param v DOCUMENT ME!
\r
144 public void setSequenceFeatures(SequenceFeature [] features)
\r
146 sequenceFeatures = features;
\r
149 public synchronized void addSequenceFeature(SequenceFeature sf)
\r
151 if(sequenceFeatures==null)
\r
153 sequenceFeatures = new SequenceFeature[0];
\r
156 for(int i=0; i<sequenceFeatures.length; i++)
\r
158 if(sequenceFeatures[i].equals(sf))
\r
164 SequenceFeature [] temp = new SequenceFeature[sequenceFeatures.length+1];
\r
165 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
\r
166 temp[sequenceFeatures.length] = sf;
\r
168 sequenceFeatures = temp;
\r
175 * @return DOCUMENT ME!
\r
177 public SequenceFeature [] getSequenceFeatures()
\r
179 return sequenceFeatures;
\r
182 public void addPDBId(PDBEntry entry)
\r
185 pdbIds = new Vector();
\r
187 pdbIds.addElement(entry);
\r
193 * @param id DOCUMENT ME!
\r
195 public void setPDBId(Vector id)
\r
203 * @return DOCUMENT ME!
\r
205 public Vector getPDBId()
\r
213 * @return DOCUMENT ME!
\r
215 public String getDisplayId(boolean jvsuffix)
\r
217 StringBuffer result = new StringBuffer(name);
\r
220 result.append("/" + start + "-" + end);
\r
223 return result.toString();
\r
229 * @param name DOCUMENT ME!
\r
231 public void setName(String name)
\r
240 * @return DOCUMENT ME!
\r
242 public String getName()
\r
250 * @param start DOCUMENT ME!
\r
252 public void setStart(int start)
\r
254 this.start = start;
\r
260 * @return DOCUMENT ME!
\r
262 public int getStart()
\r
270 * @param end DOCUMENT ME!
\r
272 public void setEnd(int end)
\r
280 * @return DOCUMENT ME!
\r
282 public int getEnd()
\r
290 * @return DOCUMENT ME!
\r
292 public int getLength()
\r
294 return this.sequence.length();
\r
300 * @param seq DOCUMENT ME!
\r
302 public void setSequence(String seq)
\r
304 this.sequence = seq;
\r
311 * @return DOCUMENT ME!
\r
313 public String getSequence()
\r
315 return this.sequence;
\r
321 * @param start DOCUMENT ME!
\r
322 * @param end DOCUMENT ME!
\r
324 * @return DOCUMENT ME!
\r
326 public String getSequence(int start, int end)
\r
328 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
\r
329 if (start >= sequence.length())
\r
334 if (end >= sequence.length())
\r
336 end = sequence.length();
\r
339 return this.sequence.substring(start, end);
\r
345 * @param i DOCUMENT ME!
\r
347 * @return DOCUMENT ME!
\r
349 public char getCharAt(int i)
\r
351 if (i < sequence.length())
\r
353 return sequence.charAt(i);
\r
364 * @param desc DOCUMENT ME!
\r
366 public void setDescription(String desc)
\r
368 this.description = desc;
\r
374 * @return DOCUMENT ME!
\r
376 public String getDescription()
\r
378 return this.description;
\r
384 * @param pos DOCUMENT ME!
\r
386 * @return DOCUMENT ME!
\r
388 public int findIndex(int pos)
\r
390 // returns the alignment position for a residue
\r
394 while ((i < sequence.length()) && (j <= end) && (j <= pos))
\r
396 if (!jalview.util.Comparison.isGap(sequence.charAt(i)))
\r
404 if ((j == end) && (j < pos))
\r
417 * @param i DOCUMENT ME!
\r
419 * @return DOCUMENT ME!
\r
421 public int findPosition(int i)
\r
423 // Returns the sequence position for an alignment position
\r
427 while ((j < i) && (j < sequence.length()))
\r
429 if (!jalview.util.Comparison.isGap((sequence.charAt(j))))
\r
443 * @return DOCUMENT ME!
\r
445 public int[] gapMap()
\r
447 // Returns an int array giving the position of each residue in the sequence in the alignment
\r
448 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sequence);
\r
449 int[] map = new int[seq.length()];
\r
453 while (j < sequence.length())
\r
455 if (!jalview.util.Comparison.isGap(sequence.charAt(j)))
\r
469 * @param i DOCUMENT ME!
\r
471 public void deleteCharAt(int i)
\r
473 if (i >= sequence.length())
\r
478 sequence = sequence.substring(0, i) + sequence.substring(i + 1);
\r
484 * @param i DOCUMENT ME!
\r
485 * @param j DOCUMENT ME!
\r
487 public void deleteChars(int i, int j)
\r
489 if (i >= sequence.length())
\r
494 if (j >= sequence.length())
\r
496 sequence = sequence.substring(0, i);
\r
500 sequence = sequence.substring(0, i) + sequence.substring(j);
\r
508 * @param i DOCUMENT ME!
\r
509 * @param c DOCUMENT ME!
\r
510 * @param chop DOCUMENT ME!
\r
512 public void insertCharAt(int i, char c)
\r
514 String tmp = new String(sequence);
\r
516 if (i < sequence.length())
\r
518 sequence = tmp.substring(0, i) + String.valueOf(c) +
\r
523 // JBPNote : padding char at end of sequence. We'll not get away with this when we insert residues, I bet!
\r
524 char[] ch = new char[(1 + i) - sequence.length()];
\r
526 for (int j = 0, k = ch.length; j < k; j++)
\r
529 sequence = tmp + String.valueOf(ch);
\r
536 * @param c DOCUMENT ME!
\r
538 public void setColor(Color c)
\r
546 * @return DOCUMENT ME!
\r
548 public Color getColor()
\r
553 public String getVamsasId()
\r
558 public void setVamsasId(String id)
\r
563 public void setDBRef(DBRefEntry [] dbref)
\r
568 public DBRefEntry [] getDBRef()
\r
573 public void addDBRef(DBRefEntry entry)
\r
576 dbrefs = new DBRefEntry[0];
\r
578 DBRefEntry [] temp = new DBRefEntry[dbrefs.length+1];
\r
579 System.arraycopy(dbrefs, 0, temp, 0, dbrefs.length);
\r
581 temp[temp.length-1] = entry;
\r
586 public void setDatasetSequence(SequenceI seq)
\r
588 datasetSequence = seq;
\r
591 public SequenceI getDatasetSequence()
\r
593 return datasetSequence;
\r
596 public AlignmentAnnotation [] getAnnotation()
\r
598 if(annotation==null)
\r
601 AlignmentAnnotation [] ret = new AlignmentAnnotation[annotation.size()];
\r
602 for(int r = 0; r<ret.length; r++)
\r
603 ret[r] = (AlignmentAnnotation)annotation.elementAt(r);
\r
608 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
\r
610 if(this.annotation==null)
\r
611 this.annotation = new Vector();
\r
613 this.annotation.addElement( annotation );
\r
616 public SequenceGroup getHiddenSequences()
\r
618 return hiddenSequences;
\r
621 public void addHiddenSequence(SequenceI seq)
\r
623 if(hiddenSequences==null)
\r
625 hiddenSequences = new SequenceGroup();
\r
627 hiddenSequences.addSequence(seq, false);
\r
630 public void showHiddenSequence(SequenceI seq)
\r
632 hiddenSequences.deleteSequence(seq, false);
\r
633 if (hiddenSequences.getSize(false) < 1)
\r
635 hiddenSequences = null;
\r
639 public void changeCase(boolean toUpper, int start, int end)
\r
641 StringBuffer newSeq = new StringBuffer();
\r
643 if(end>sequence.length())
\r
644 end = sequence.length();
\r
648 newSeq.append(sequence.substring(0, start));
\r
652 newSeq.append(sequence.substring(start, end).toUpperCase());
\r
654 newSeq.append(sequence.substring(start, end).toLowerCase());
\r
656 if (end < sequence.length())
\r
657 newSeq.append(sequence.substring(end));
\r
659 sequence = newSeq.toString();
\r
662 public void toggleCase(int start, int end)
\r
664 StringBuffer newSeq = new StringBuffer();
\r
666 if(end>sequence.length())
\r
667 end = sequence.length();
\r
671 newSeq.append(sequence.substring(0, start));
\r
675 for(int c=start; c<end; c++)
\r
677 nextChar = sequence.charAt(c);
\r
678 if(Character.isLetter(nextChar))
\r
680 if(Character.isUpperCase(nextChar))
\r
681 nextChar = Character.toLowerCase(nextChar);
\r
683 nextChar = Character.toUpperCase(nextChar);
\r
687 newSeq.append(nextChar);
\r
690 if (end < sequence.length())
\r
691 newSeq.append(sequence.substring(end));
\r
693 sequence = newSeq.toString();
\r