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;
31 public class Sequence implements SequenceI
33 SequenceI datasetSequence;
35 private char [] sequence;
43 /** This annotation is displayed below the alignment but the
44 * positions are tied to the residues of this sequence */
48 public SequenceFeature[] sequenceFeatures;
50 /** This array holds hidden sequences
51 * of which this sequence is the representitive member of a group
53 SequenceGroup hiddenSequences;
56 * Creates a new Sequence object.
58 * @param name DOCUMENT ME!
59 * @param sequence DOCUMENT ME!
60 * @param start DOCUMENT ME!
61 * @param end DOCUMENT ME!
63 public Sequence(String name, String sequence, int start, int end)
66 this.sequence = sequence.toCharArray();
73 public Sequence(String name, char [] sequence, int start, int end)
76 this.sequence = sequence;
83 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
84 "[/][0-9]{1,}[-][0-9]{1,}$");
85 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
90 // Does sequence have the /start-end signiature?
91 if (limitrx.search(name))
93 name = limitrx.left();
94 endrx.search(limitrx.stringMatched());
95 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
96 endrx.matchedFrom() - 1)));
97 setEnd(Integer.parseInt(endrx.stringMatched()));
101 void checkValidRange()
106 for (int j = 0; j < sequence.length; j++)
108 if (!jalview.util.Comparison.isGap( sequence[j] ))
124 * Creates a new Sequence object.
126 * @param name DOCUMENT ME!
127 * @param sequence DOCUMENT ME!
129 public Sequence(String name, String sequence)
131 this(name, sequence, 1, -1);
135 * Creates a new Sequence object.
137 * @param seq DOCUMENT ME!
139 public Sequence(SequenceI seq)
145 description = seq.getDescription();
152 * @param v DOCUMENT ME!
154 public void setSequenceFeatures(SequenceFeature[] features)
156 sequenceFeatures = features;
159 public synchronized void addSequenceFeature(SequenceFeature sf)
161 if (sequenceFeatures == null)
163 sequenceFeatures = new SequenceFeature[0];
166 for (int i = 0; i < sequenceFeatures.length; i++)
168 if (sequenceFeatures[i].equals(sf))
174 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
175 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
176 temp[sequenceFeatures.length] = sf;
178 sequenceFeatures = temp;
181 public void deleteFeature(SequenceFeature sf)
183 if(sequenceFeatures==null)
187 for (index = 0; index < sequenceFeatures.length; index++)
189 if (sequenceFeatures[index].equals(sf))
196 if(index==sequenceFeatures.length)
199 int sfLength = sequenceFeatures.length;
202 sequenceFeatures = null;
206 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
207 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
210 System.arraycopy(sequenceFeatures,
213 index, sequenceFeatures.length - index -1);
215 sequenceFeatures = temp;
222 * @return DOCUMENT ME!
224 public SequenceFeature[] getSequenceFeatures()
226 return sequenceFeatures;
229 public void addPDBId(PDBEntry entry)
232 pdbIds = new Vector();
234 pdbIds.addElement(entry);
240 * @param id DOCUMENT ME!
242 public void setPDBId(Vector id)
250 * @return DOCUMENT ME!
252 public Vector getPDBId()
260 * @return DOCUMENT ME!
262 public String getDisplayId(boolean jvsuffix)
264 StringBuffer result = new StringBuffer(name);
267 result.append("/" + start + "-" + end);
270 return result.toString();
276 * @param name DOCUMENT ME!
278 public void setName(String name)
287 * @return DOCUMENT ME!
289 public String getName()
297 * @param start DOCUMENT ME!
299 public void setStart(int start)
307 * @return DOCUMENT ME!
309 public int getStart()
317 * @param end DOCUMENT ME!
319 public void setEnd(int end)
327 * @return DOCUMENT ME!
337 * @return DOCUMENT ME!
339 public int getLength()
341 return this.sequence.length;
347 * @param seq DOCUMENT ME!
349 public void setSequence(String seq)
351 this.sequence = seq.toCharArray();
356 public String getSequenceAsString()
358 return new String(sequence);
361 public String getSequenceAsString(int start, int end)
363 return new String(getSequence(start, end));
367 public char [] getSequence()
375 * @param start DOCUMENT ME!
376 * @param end DOCUMENT ME!
378 * @return DOCUMENT ME!
380 public char [] getSequence(int start, int end)
382 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
383 if (start >= sequence.length)
388 if (end >= sequence.length)
390 end = sequence.length;
393 char [] reply = new char[end-start];
394 System.arraycopy(sequence, start, reply, 0, end-start);
401 * make a new Sequence object from start to end (including gaps) over this seqeunce
406 public SequenceI getSubSequence(int start, int end)
410 char [] seq = getSequence(start, end);
413 int nstart = findPosition(start);
414 int nend = findPosition(end) - 1;
415 // JBPNote - this is an incomplete copy.
416 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
417 nseq.setDescription(description);
418 nseq.setDatasetSequence(getDatasetSequence());
425 * @param i DOCUMENT ME!
427 * @return DOCUMENT ME!
429 public char getCharAt(int i)
431 if (i < sequence.length)
444 * @param desc DOCUMENT ME!
446 public void setDescription(String desc)
448 this.description = desc;
454 * @return DOCUMENT ME!
456 public String getDescription()
458 return this.description;
464 * @param pos DOCUMENT ME!
466 * @return DOCUMENT ME!
468 public int findIndex(int pos)
470 // returns the alignment position for a residue
474 while ( (i < sequence.length) && (j <= end) && (j <= pos))
476 if (!jalview.util.Comparison.isGap(sequence[i]))
484 if ( (j == end) && (j < pos))
495 * Returns the sequence position for an alignment position
497 * @param i column index in alignment (from 1)
499 * @return residue number for residue (left of and) nearest ith column
501 public int findPosition(int i)
505 int seqlen = sequence.length;
506 while ( (j < i) && (j < seqlen))
508 if (!jalview.util.Comparison.isGap( sequence[j] ))
520 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
522 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
524 public int[] gapMap()
526 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
527 GapChars, new String(sequence));
528 int[] map = new int[seq.length()];
532 while (j < sequence.length)
534 if (!jalview.util.Comparison.isGap(sequence[j]))
548 * @param i DOCUMENT ME!
549 * @param j DOCUMENT ME!
551 public void deleteChars(int i, int j)
553 if (i >= sequence.length)
560 if (j >= sequence.length)
563 System.arraycopy(sequence,0,tmp,0,i);
567 tmp = new char[sequence.length-j+i];
568 System.arraycopy(sequence,0,tmp,0,i);
569 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
578 * @param i DOCUMENT ME!
579 * @param c DOCUMENT ME!
580 * @param chop DOCUMENT ME!
582 public void insertCharAt(int i, int length, char c)
584 char [] tmp = new char[sequence.length+length];
586 if (i >= sequence.length)
588 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
593 System.arraycopy(sequence, 0, tmp, 0, i);
604 if (i < sequence.length)
606 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
612 public void insertCharAt(int i, char c)
614 insertCharAt(i, 1, c);
617 public String getVamsasId()
622 public void setVamsasId(String id)
627 public void setDBRef(DBRefEntry[] dbref)
632 public DBRefEntry[] getDBRef()
637 public void addDBRef(DBRefEntry entry)
640 dbrefs = new DBRefEntry[0];
642 int i, iSize = dbrefs.length;
644 for(i=0; i<iSize; i++)
645 if(dbrefs[i].getAccessionId().equals(entry.getAccessionId())
646 && dbrefs[i].getSource().equals(entry.getSource())
647 && dbrefs[i].getVersion().equals(entry.getVersion()))
652 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
653 System.arraycopy(dbrefs, 0, temp, 0, iSize);
654 temp[temp.length - 1] = entry;
659 public void setDatasetSequence(SequenceI seq)
661 datasetSequence = seq;
664 public SequenceI getDatasetSequence()
666 return datasetSequence;
669 public AlignmentAnnotation[] getAnnotation()
671 if (annotation == null)
674 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
675 for (int r = 0; r < ret.length; r++)
676 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
681 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
683 if (this.annotation == null)
684 this.annotation = new Vector();
686 this.annotation.addElement(annotation);
689 public SequenceGroup getHiddenSequences()
691 return hiddenSequences;
694 public void addHiddenSequence(SequenceI seq)
696 if (hiddenSequences == null)
698 hiddenSequences = new SequenceGroup();
700 hiddenSequences.addSequence(seq, false);
703 public void showHiddenSequence(SequenceI seq)
705 hiddenSequences.deleteSequence(seq, false);
706 if (hiddenSequences.getSize(false) < 1)
708 hiddenSequences = null;