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)
141 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
147 * @param v DOCUMENT ME!
149 public void setSequenceFeatures(SequenceFeature[] features)
151 sequenceFeatures = features;
154 public synchronized void addSequenceFeature(SequenceFeature sf)
156 if (sequenceFeatures == null)
158 sequenceFeatures = new SequenceFeature[0];
161 for (int i = 0; i < sequenceFeatures.length; i++)
163 if (sequenceFeatures[i].equals(sf))
169 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
170 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
171 temp[sequenceFeatures.length] = sf;
173 sequenceFeatures = temp;
176 public void deleteFeature(SequenceFeature sf)
178 if(sequenceFeatures==null)
182 for (index = 0; index < sequenceFeatures.length; index++)
184 if (sequenceFeatures[index].equals(sf))
191 if(index==sequenceFeatures.length)
194 int sfLength = sequenceFeatures.length;
197 sequenceFeatures = null;
201 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
202 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
205 System.arraycopy(sequenceFeatures,
208 index, sequenceFeatures.length - index -1);
210 sequenceFeatures = temp;
217 * @return DOCUMENT ME!
219 public SequenceFeature[] getSequenceFeatures()
221 return sequenceFeatures;
224 public void addPDBId(PDBEntry entry)
227 pdbIds = new Vector();
229 pdbIds.addElement(entry);
235 * @param id DOCUMENT ME!
237 public void setPDBId(Vector id)
245 * @return DOCUMENT ME!
247 public Vector getPDBId()
255 * @return DOCUMENT ME!
257 public String getDisplayId(boolean jvsuffix)
259 StringBuffer result = new StringBuffer(name);
262 result.append("/" + start + "-" + end);
265 return result.toString();
271 * @param name DOCUMENT ME!
273 public void setName(String name)
282 * @return DOCUMENT ME!
284 public String getName()
292 * @param start DOCUMENT ME!
294 public void setStart(int start)
302 * @return DOCUMENT ME!
304 public int getStart()
312 * @param end DOCUMENT ME!
314 public void setEnd(int end)
322 * @return DOCUMENT ME!
332 * @return DOCUMENT ME!
334 public int getLength()
336 return this.sequence.length;
342 * @param seq DOCUMENT ME!
344 public void setSequence(String seq)
346 this.sequence = seq.toCharArray();
351 public String getSequenceAsString()
353 return new String(sequence);
356 public String getSequenceAsString(int start, int end)
358 return new String(getSequence(start, end));
362 public char [] getSequence()
370 * @param start DOCUMENT ME!
371 * @param end DOCUMENT ME!
373 * @return DOCUMENT ME!
375 public char [] getSequence(int start, int end)
377 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
378 if (start >= sequence.length)
380 System.out.println("DOES THIS GET CALLED???");
383 if (end >= sequence.length)
385 end = sequence.length;
388 char [] reply = new char[end-start];
389 System.arraycopy(sequence, start, reply, 0, end-start);
396 * make a new Sequence object from start to end (including gaps) over this seqeunce
401 public SequenceI getSubSequence(int start, int end)
405 char [] seq = getSequence(start, end);
408 int nstart = findPosition(start);
409 int nend = findPosition(end) - 1;
410 // JBPNote - this is an incomplete copy.
411 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
412 nseq.setDescription(description);
413 nseq.setDatasetSequence(getDatasetSequence());
420 * @param i DOCUMENT ME!
422 * @return DOCUMENT ME!
424 public char getCharAt(int i)
426 if (i < sequence.length)
439 * @param desc DOCUMENT ME!
441 public void setDescription(String desc)
443 this.description = desc;
449 * @return DOCUMENT ME!
451 public String getDescription()
453 return this.description;
459 * @param pos DOCUMENT ME!
461 * @return DOCUMENT ME!
463 public int findIndex(int pos)
465 // returns the alignment position for a residue
469 while ( (i < sequence.length) && (j <= end) && (j <= pos))
471 if (!jalview.util.Comparison.isGap(sequence[i]))
479 if ( (j == end) && (j < pos))
490 * Returns the sequence position for an alignment position
492 * @param i column index in alignment (from 1)
494 * @return residue number for residue (left of and) nearest ith column
496 public int findPosition(int i)
500 int seqlen = sequence.length;
501 while ( (j < i) && (j < seqlen))
503 if (!jalview.util.Comparison.isGap( sequence[j] ))
515 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
517 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
519 public int[] gapMap()
521 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
522 GapChars, new String(sequence));
523 int[] map = new int[seq.length()];
527 while (j < sequence.length)
529 if (!jalview.util.Comparison.isGap(sequence[j]))
543 * @param i DOCUMENT ME!
544 * @param j DOCUMENT ME!
546 public void deleteChars(int i, int j)
548 if (i >= sequence.length)
555 if (j >= sequence.length)
558 System.arraycopy(sequence,0,tmp,0,i);
562 tmp = new char[sequence.length-j+i];
563 System.arraycopy(sequence,0,tmp,0,i);
564 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
573 * @param i DOCUMENT ME!
574 * @param c DOCUMENT ME!
575 * @param chop DOCUMENT ME!
577 public void insertCharAt(int i, int length, char c)
579 char [] tmp = new char[sequence.length+length];
581 if (i >= sequence.length)
583 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
588 System.arraycopy(sequence, 0, tmp, 0, i);
599 if (i < sequence.length)
601 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
607 public void insertCharAt(int i, char c)
609 insertCharAt(i, 1, c);
612 public String getVamsasId()
617 public void setVamsasId(String id)
622 public void setDBRef(DBRefEntry[] dbref)
627 public DBRefEntry[] getDBRef()
632 public void addDBRef(DBRefEntry entry)
635 dbrefs = new DBRefEntry[0];
637 int i, iSize = dbrefs.length;
639 for(i=0; i<iSize; i++)
640 if(dbrefs[i].getAccessionId().equals(entry.getAccessionId())
641 && dbrefs[i].getSource().equals(entry.getSource())
642 && dbrefs[i].getVersion().equals(entry.getVersion()))
647 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
648 System.arraycopy(dbrefs, 0, temp, 0, iSize);
649 temp[temp.length - 1] = entry;
654 public void setDatasetSequence(SequenceI seq)
656 datasetSequence = seq;
659 public SequenceI getDatasetSequence()
661 return datasetSequence;
664 public AlignmentAnnotation[] getAnnotation()
666 if (annotation == null)
669 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
670 for (int r = 0; r < ret.length; r++)
671 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
676 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
678 if (this.annotation == null)
679 this.annotation = new Vector();
681 this.annotation.addElement(annotation);
684 public SequenceGroup getHiddenSequences()
686 return hiddenSequences;
689 public void addHiddenSequence(SequenceI seq)
691 if (hiddenSequences == null)
693 hiddenSequences = new SequenceGroup();
695 hiddenSequences.addSequence(seq, false);
698 public void showHiddenSequence(SequenceI seq)
700 hiddenSequences.deleteSequence(seq, false);
701 if (hiddenSequences.getSize(false) < 1)
703 hiddenSequences = null;