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;
44 /** This annotation is displayed below the alignment but the
45 * positions are tied to the residues of this sequence */
49 public SequenceFeature[] sequenceFeatures;
51 /** This array holds hidden sequences
52 * of which this sequence is the representitive member of a group
54 SequenceGroup hiddenSequences;
57 * Creates a new Sequence object.
59 * @param name DOCUMENT ME!
60 * @param sequence DOCUMENT ME!
61 * @param start DOCUMENT ME!
62 * @param end DOCUMENT ME!
64 public Sequence(String name, String sequence, int start, int end)
67 this.sequence = sequence;
76 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
77 "[/][0-9]{1,}[-][0-9]{1,}$");
78 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
83 // Does sequence have the /start-end signiature?
84 if (limitrx.search(name))
86 name = limitrx.left();
87 endrx.search(limitrx.stringMatched());
88 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
89 endrx.matchedFrom() - 1)));
90 setEnd(Integer.parseInt(endrx.stringMatched()));
94 void checkValidRange()
100 for (int j = 0; j < sequence.length(); j++)
102 ch = sequence.charAt(j);
103 if (!jalview.util.Comparison.isGap( (ch)))
119 * Creates a new Sequence object.
121 * @param name DOCUMENT ME!
122 * @param sequence DOCUMENT ME!
124 public Sequence(String name, String sequence)
126 this(name, sequence, 1, -1);
130 * Creates a new Sequence object.
132 * @param seq DOCUMENT ME!
134 public Sequence(SequenceI seq)
136 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
142 * @param v DOCUMENT ME!
144 public void setSequenceFeatures(SequenceFeature[] features)
146 sequenceFeatures = features;
149 public synchronized void addSequenceFeature(SequenceFeature sf)
151 if (sequenceFeatures == null)
153 sequenceFeatures = new SequenceFeature[0];
156 for (int i = 0; i < sequenceFeatures.length; i++)
158 if (sequenceFeatures[i].equals(sf))
164 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
165 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
166 temp[sequenceFeatures.length] = sf;
168 sequenceFeatures = temp;
171 public void deleteFeature(SequenceFeature sf)
173 if(sequenceFeatures==null)
177 for (index = 0; index < sequenceFeatures.length; index++)
179 if (sequenceFeatures[index].equals(sf))
186 if(index==sequenceFeatures.length)
189 int sfLength = sequenceFeatures.length;
192 sequenceFeatures = null;
196 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
197 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
200 System.arraycopy(sequenceFeatures,
203 index, sequenceFeatures.length - index -1);
205 sequenceFeatures = temp;
212 * @return DOCUMENT ME!
214 public SequenceFeature[] getSequenceFeatures()
216 return sequenceFeatures;
219 public void addPDBId(PDBEntry entry)
222 pdbIds = new Vector();
224 pdbIds.addElement(entry);
230 * @param id DOCUMENT ME!
232 public void setPDBId(Vector id)
240 * @return DOCUMENT ME!
242 public Vector getPDBId()
250 * @return DOCUMENT ME!
252 public String getDisplayId(boolean jvsuffix)
254 StringBuffer result = new StringBuffer(name);
257 result.append("/" + start + "-" + end);
260 return result.toString();
266 * @param name DOCUMENT ME!
268 public void setName(String name)
277 * @return DOCUMENT ME!
279 public String getName()
287 * @param start DOCUMENT ME!
289 public void setStart(int start)
297 * @return DOCUMENT ME!
299 public int getStart()
307 * @param end DOCUMENT ME!
309 public void setEnd(int end)
317 * @return DOCUMENT ME!
327 * @return DOCUMENT ME!
329 public int getLength()
331 return this.sequence.length();
337 * @param seq DOCUMENT ME!
339 public void setSequence(String seq)
348 * @return DOCUMENT ME!
350 public String getSequence()
352 return this.sequence;
358 * @param start DOCUMENT ME!
359 * @param end DOCUMENT ME!
361 * @return DOCUMENT ME!
363 public String getSequence(int start, int end)
365 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
366 if (start >= sequence.length())
371 if (end >= sequence.length())
373 end = sequence.length();
376 return this.sequence.substring(start, end);
380 * make a new Sequence object from start to end (including gaps) over this seqeunce
385 public SequenceI getSubSequence(int start, int end)
389 String seq = getSequence(start, end);
392 int nstart = findPosition(start);
393 int nend = findPosition(end) - 1;
394 // JBPNote - this is an incomplete copy.
395 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
396 nseq.setDescription(description);
397 nseq.setDatasetSequence(getDatasetSequence());
404 * @param i DOCUMENT ME!
406 * @return DOCUMENT ME!
408 public char getCharAt(int i)
410 if (i < sequence.length())
412 return sequence.charAt(i);
423 * @param desc DOCUMENT ME!
425 public void setDescription(String desc)
427 this.description = desc;
433 * @return DOCUMENT ME!
435 public String getDescription()
437 return this.description;
443 * @param pos DOCUMENT ME!
445 * @return DOCUMENT ME!
447 public int findIndex(int pos)
449 // returns the alignment position for a residue
453 while ( (i < sequence.length()) && (j <= end) && (j <= pos))
455 if (!jalview.util.Comparison.isGap(sequence.charAt(i)))
463 if ( (j == end) && (j < pos))
474 * Returns the sequence position for an alignment position
476 * @param i column index in alignment (from 1)
478 * @return residue number for residue (left of and) nearest ith column
480 public int findPosition(int i)
484 int seqlen = sequence.length();
485 while ( (j < i) && (j < seqlen))
487 if (!jalview.util.Comparison.isGap( (sequence.charAt(j))))
499 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
501 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
503 public int[] gapMap()
505 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
507 int[] map = new int[seq.length()];
511 while (j < sequence.length())
513 if (!jalview.util.Comparison.isGap(sequence.charAt(j)))
527 * @param i DOCUMENT ME!
529 public void deleteCharAt(int i)
531 if (i >= sequence.length())
536 sequence = sequence.substring(0, i) + sequence.substring(i + 1);
542 * @param i DOCUMENT ME!
543 * @param j DOCUMENT ME!
545 public void deleteChars(int i, int j)
547 if (i >= sequence.length())
552 if (j >= sequence.length())
554 sequence = sequence.substring(0, i);
558 sequence = sequence.substring(0, i) + sequence.substring(j);
565 * @param i DOCUMENT ME!
566 * @param c DOCUMENT ME!
567 * @param chop DOCUMENT ME!
569 public void insertCharAt(int i, int length, char c)
573 if (i >= sequence.length())
575 tmp = new StringBuffer(sequence);
578 tmp = new StringBuffer(sequence.substring(0, i));
586 if (i < sequence.length())
588 tmp.append(sequence.substring(i));
591 sequence = tmp.toString();
594 public void insertCharAt(int i, char c)
596 insertCharAt(i, 1, c);
599 public String getVamsasId()
604 public void setVamsasId(String id)
609 public void setDBRef(DBRefEntry[] dbref)
614 public DBRefEntry[] getDBRef()
619 public void addDBRef(DBRefEntry entry)
622 dbrefs = new DBRefEntry[0];
624 int i, iSize = dbrefs.length;
626 for(i=0; i<iSize; i++)
627 if(dbrefs[i].getAccessionId().equals(entry.getAccessionId())
628 && dbrefs[i].getSource().equals(entry.getSource())
629 && dbrefs[i].getVersion().equals(entry.getVersion()))
634 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
635 System.arraycopy(dbrefs, 0, temp, 0, iSize);
636 temp[temp.length - 1] = entry;
641 public void setDatasetSequence(SequenceI seq)
643 datasetSequence = seq;
646 public SequenceI getDatasetSequence()
648 return datasetSequence;
651 public AlignmentAnnotation[] getAnnotation()
653 if (annotation == null)
656 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
657 for (int r = 0; r < ret.length; r++)
658 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
663 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
665 if (this.annotation == null)
666 this.annotation = new Vector();
668 this.annotation.addElement(annotation);
671 public SequenceGroup getHiddenSequences()
673 return hiddenSequences;
676 public void addHiddenSequence(SequenceI seq)
678 if (hiddenSequences == null)
680 hiddenSequences = new SequenceGroup();
682 hiddenSequences.addSequence(seq, false);
685 public void showHiddenSequence(SequenceI seq)
687 hiddenSequences.deleteSequence(seq, false);
688 if (hiddenSequences.getSize(false) < 1)
690 hiddenSequences = null;