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,
90 endrx.matchedFrom() - 1)));
91 setEnd(Integer.parseInt(endrx.stringMatched()));
95 void checkValidRange()
101 for (int j = 0; j < sequence.length(); j++)
103 ch = sequence.charAt(j);
104 if (!jalview.util.Comparison.isGap( (ch)))
120 * Creates a new Sequence object.
122 * @param name DOCUMENT ME!
123 * @param sequence DOCUMENT ME!
125 public Sequence(String name, String sequence)
127 this(name, sequence, 1, -1);
131 * Creates a new Sequence object.
133 * @param seq DOCUMENT ME!
135 public Sequence(SequenceI seq)
137 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
143 * @param v DOCUMENT ME!
145 public void setSequenceFeatures(SequenceFeature[] features)
147 sequenceFeatures = features;
150 public synchronized void addSequenceFeature(SequenceFeature sf)
152 if (sequenceFeatures == null)
154 sequenceFeatures = new SequenceFeature[0];
157 for (int i = 0; i < sequenceFeatures.length; i++)
159 if (sequenceFeatures[i].equals(sf))
165 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
166 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
167 temp[sequenceFeatures.length] = sf;
169 sequenceFeatures = temp;
172 public void deleteFeature(SequenceFeature sf)
174 if(sequenceFeatures==null)
178 for (index = 0; index < sequenceFeatures.length; index++)
180 if (sequenceFeatures[index].equals(sf))
187 if(index==sequenceFeatures.length)
190 int sfLength = sequenceFeatures.length;
193 sequenceFeatures = null;
197 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
198 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
201 System.arraycopy(sequenceFeatures,
204 index, sequenceFeatures.length - index -1);
206 sequenceFeatures = temp;
213 * @return DOCUMENT ME!
215 public SequenceFeature[] getSequenceFeatures()
217 return sequenceFeatures;
220 public void addPDBId(PDBEntry entry)
223 pdbIds = new Vector();
225 pdbIds.addElement(entry);
231 * @param id DOCUMENT ME!
233 public void setPDBId(Vector id)
241 * @return DOCUMENT ME!
243 public Vector getPDBId()
251 * @return DOCUMENT ME!
253 public String getDisplayId(boolean jvsuffix)
255 StringBuffer result = new StringBuffer(name);
258 result.append("/" + start + "-" + end);
261 return result.toString();
267 * @param name DOCUMENT ME!
269 public void setName(String name)
278 * @return DOCUMENT ME!
280 public String getName()
288 * @param start DOCUMENT ME!
290 public void setStart(int start)
298 * @return DOCUMENT ME!
300 public int getStart()
308 * @param end DOCUMENT ME!
310 public void setEnd(int end)
318 * @return DOCUMENT ME!
328 * @return DOCUMENT ME!
330 public int getLength()
332 return this.sequence.length();
338 * @param seq DOCUMENT ME!
340 public void setSequence(String seq)
349 * @return DOCUMENT ME!
351 public String getSequence()
353 return this.sequence;
359 * @param start DOCUMENT ME!
360 * @param end DOCUMENT ME!
362 * @return DOCUMENT ME!
364 public String getSequence(int start, int end)
366 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
367 if (start >= sequence.length())
372 if (end >= sequence.length())
374 end = sequence.length();
377 return this.sequence.substring(start, end);
381 * make a new Sequence object from start to end (including gaps) over this seqeunce
386 public SequenceI getSubSequence(int start, int end)
390 String seq = getSequence(start, end);
393 int nstart = findPosition(start);
394 int nend = findPosition(end) - 1;
395 // JBPNote - this is an incomplete copy.
396 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
397 nseq.setDescription(description);
398 nseq.setDatasetSequence(getDatasetSequence());
405 * @param i DOCUMENT ME!
407 * @return DOCUMENT ME!
409 public char getCharAt(int i)
411 if (i < sequence.length())
413 return sequence.charAt(i);
424 * @param desc DOCUMENT ME!
426 public void setDescription(String desc)
428 this.description = desc;
434 * @return DOCUMENT ME!
436 public String getDescription()
438 return this.description;
444 * @param pos DOCUMENT ME!
446 * @return DOCUMENT ME!
448 public int findIndex(int pos)
450 // returns the alignment position for a residue
454 while ( (i < sequence.length()) && (j <= end) && (j <= pos))
456 if (!jalview.util.Comparison.isGap(sequence.charAt(i)))
464 if ( (j == end) && (j < pos))
475 * Returns the sequence position for an alignment position
477 * @param i column index in alignment (from 1)
479 * @return residue number for residue (left of and) nearest ith column
481 public int findPosition(int i)
485 int seqlen = sequence.length();
486 while ( (j < i) && (j < seqlen))
488 if (!jalview.util.Comparison.isGap( (sequence.charAt(j))))
500 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
502 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
504 public int[] gapMap()
506 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
508 int[] map = new int[seq.length()];
512 while (j < sequence.length())
514 if (!jalview.util.Comparison.isGap(sequence.charAt(j)))
528 * @param i DOCUMENT ME!
530 public void deleteCharAt(int i)
532 if (i >= sequence.length())
537 sequence = sequence.substring(0, i) + sequence.substring(i + 1);
543 * @param i DOCUMENT ME!
544 * @param j DOCUMENT ME!
546 public void deleteChars(int i, int j)
548 if (i >= sequence.length())
553 if (j >= sequence.length())
555 sequence = sequence.substring(0, i);
559 sequence = sequence.substring(0, i) + sequence.substring(j);
566 * @param i DOCUMENT ME!
567 * @param c DOCUMENT ME!
568 * @param chop DOCUMENT ME!
570 public void insertCharAt(int i, int length, char c)
574 if (i >= sequence.length())
576 tmp = new StringBuffer(sequence);
579 tmp = new StringBuffer(sequence.substring(0, i));
587 if (i < sequence.length())
589 tmp.append(sequence.substring(i));
592 sequence = tmp.toString();
595 public void insertCharAt(int i, char c)
597 insertCharAt(i, 1, c);
603 * @param c DOCUMENT ME!
605 public void setColor(Color c)
613 * @return DOCUMENT ME!
615 public Color getColor()
620 public String getVamsasId()
625 public void setVamsasId(String id)
630 public void setDBRef(DBRefEntry[] dbref)
635 public DBRefEntry[] getDBRef()
640 public void addDBRef(DBRefEntry entry)
643 dbrefs = new DBRefEntry[0];
645 DBRefEntry[] temp = new DBRefEntry[dbrefs.length + 1];
646 System.arraycopy(dbrefs, 0, temp, 0, dbrefs.length);
648 temp[temp.length - 1] = entry;
653 public void setDatasetSequence(SequenceI seq)
655 datasetSequence = seq;
658 public SequenceI getDatasetSequence()
660 return datasetSequence;
663 public AlignmentAnnotation[] getAnnotation()
665 if (annotation == null)
668 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
669 for (int r = 0; r < ret.length; r++)
670 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
675 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
677 if (this.annotation == null)
678 this.annotation = new Vector();
680 this.annotation.addElement(annotation);
683 public SequenceGroup getHiddenSequences()
685 return hiddenSequences;
688 public void addHiddenSequence(SequenceI seq)
690 if (hiddenSequences == null)
692 hiddenSequences = new SequenceGroup();
694 hiddenSequences.addSequence(seq, false);
697 public void showHiddenSequence(SequenceI seq)
699 hiddenSequences.deleteSequence(seq, false);
700 if (hiddenSequences.getSize(false) < 1)
702 hiddenSequences = null;