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;
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);
343 * make a new Sequence object from start to end (including gaps) over this seqeunce
348 public SequenceI getSubSequence(int start, int end)
352 String seq = getSequence(start, end);
355 int nstart = findPosition(start);
356 int nend = findPosition(end) - 1;
357 // JBPNote - this is an incomplete copy.
358 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
359 nseq.setDatasetSequence(getDatasetSequence());
366 * @param i DOCUMENT ME!
368 * @return DOCUMENT ME!
370 public char getCharAt(int i)
372 if (i < sequence.length())
374 return sequence.charAt(i);
385 * @param desc DOCUMENT ME!
387 public void setDescription(String desc)
389 this.description = desc;
395 * @return DOCUMENT ME!
397 public String getDescription()
399 return this.description;
405 * @param pos DOCUMENT ME!
407 * @return DOCUMENT ME!
409 public int findIndex(int pos)
411 // returns the alignment position for a residue
415 while ( (i < sequence.length()) && (j <= end) && (j <= pos))
417 if (!jalview.util.Comparison.isGap(sequence.charAt(i)))
425 if ( (j == end) && (j < pos))
436 * Returns the sequence position for an alignment position
438 * @param i column index in alignment (from 1)
440 * @return residue number for residue (left of and) nearest ith column
442 public int findPosition(int i)
446 int seqlen = sequence.length();
447 while ( (j < i) && (j < seqlen))
449 if (!jalview.util.Comparison.isGap( (sequence.charAt(j))))
461 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
463 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
465 public int[] gapMap()
467 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
469 int[] map = new int[seq.length()];
473 while (j < sequence.length())
475 if (!jalview.util.Comparison.isGap(sequence.charAt(j)))
489 * @param i DOCUMENT ME!
491 public void deleteCharAt(int i)
493 if (i >= sequence.length())
498 sequence = sequence.substring(0, i) + sequence.substring(i + 1);
504 * @param i DOCUMENT ME!
505 * @param j DOCUMENT ME!
507 public void deleteChars(int i, int j)
509 if (i >= sequence.length())
514 if (j >= sequence.length())
516 sequence = sequence.substring(0, i);
520 sequence = sequence.substring(0, i) + sequence.substring(j);
527 * @param i DOCUMENT ME!
528 * @param c DOCUMENT ME!
529 * @param chop DOCUMENT ME!
531 public void insertCharAt(int i, int length, char c)
535 if (i >= sequence.length())
537 tmp = new StringBuffer(sequence);
540 tmp = new StringBuffer(sequence.substring(0, i));
548 if (i < sequence.length())
550 tmp.append(sequence.substring(i));
553 sequence = tmp.toString();
556 public void insertCharAt(int i, char c)
558 insertCharAt(i, 1, c);
564 * @param c DOCUMENT ME!
566 public void setColor(Color c)
574 * @return DOCUMENT ME!
576 public Color getColor()
581 public String getVamsasId()
586 public void setVamsasId(String id)
591 public void setDBRef(DBRefEntry[] dbref)
596 public DBRefEntry[] getDBRef()
601 public void addDBRef(DBRefEntry entry)
604 dbrefs = new DBRefEntry[0];
606 DBRefEntry[] temp = new DBRefEntry[dbrefs.length + 1];
607 System.arraycopy(dbrefs, 0, temp, 0, dbrefs.length);
609 temp[temp.length - 1] = entry;
614 public void setDatasetSequence(SequenceI seq)
616 datasetSequence = seq;
619 public SequenceI getDatasetSequence()
621 return datasetSequence;
624 public AlignmentAnnotation[] getAnnotation()
626 if (annotation == null)
629 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
630 for (int r = 0; r < ret.length; r++)
631 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
636 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
638 if (this.annotation == null)
639 this.annotation = new Vector();
641 this.annotation.addElement(annotation);
644 public SequenceGroup getHiddenSequences()
646 return hiddenSequences;
649 public void addHiddenSequence(SequenceI seq)
651 if (hiddenSequences == null)
653 hiddenSequences = new SequenceGroup();
655 hiddenSequences.addSequence(seq, false);
658 public void showHiddenSequence(SequenceI seq)
660 hiddenSequences.deleteSequence(seq, false);
661 if (hiddenSequences.getSize(false) < 1)
663 hiddenSequences = null;