2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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;
23 import jalview.analysis.*;
27 * Implements the SequenceI interface for a char[] based sequence object.
32 public class Sequence implements SequenceI
34 SequenceI datasetSequence;
38 private char[] sequence;
53 * This annotation is displayed below the alignment but the positions are tied
54 * to the residues of this sequence
58 /** array of seuqence features - may not be null for a valid sequence object */
59 public SequenceFeature[] sequenceFeatures;
62 * Creates a new Sequence object.
67 * string to form a possibly gapped sequence out of
69 * first position of non-gap residue in the sequence
71 * last position of ungapped residues (nearly always only used
72 * for display purposes)
74 public Sequence(String name, String sequence, int start, int end)
77 this.sequence = sequence.toCharArray();
84 public Sequence(String name, char[] sequence, int start, int end)
87 this.sequence = sequence;
94 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
95 "[/][0-9]{1,}[-][0-9]{1,}$");
97 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
104 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
107 // Does sequence have the /start-end signiature?
108 if (limitrx.search(name))
110 name = limitrx.left();
111 endrx.search(limitrx.stringMatched());
112 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
113 endrx.matchedFrom() - 1)));
114 setEnd(Integer.parseInt(endrx.stringMatched()));
118 void checkValidRange()
123 for (int j = 0; j < sequence.length; j++)
125 if (!jalview.util.Comparison.isGap(sequence[j]))
141 * Creates a new Sequence object.
148 public Sequence(String name, String sequence)
150 this(name, sequence, 1, -1);
154 * Creates a new Sequence object with new features, DBRefEntries,
155 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
161 public Sequence(SequenceI seq)
163 this(seq, seq.getAnnotation());
167 * Create a new sequence object with new features, DBRefEntries, and PDBIds
168 * but inherits any existing dataset sequence reference, and duplicate of any
169 * annotation that is present in the given annotation array.
172 * the sequence to be copied
173 * @param alAnnotation
174 * an array of annotation including some associated with seq
176 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
178 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
179 description = seq.getDescription();
180 if (seq.getSequenceFeatures() != null)
182 SequenceFeature[] sf = seq.getSequenceFeatures();
183 for (int i = 0; i < sf.length; i++)
185 addSequenceFeature(new SequenceFeature(sf[i]));
188 if (seq.getDBRef() != null)
190 DBRefEntry[] dbr = seq.getDBRef();
191 for (int i = 0; i < dbr.length; i++)
193 addDBRef(new DBRefEntry(dbr[i]));
196 setDatasetSequence(seq.getDatasetSequence());
197 if (seq.getAnnotation() != null)
199 AlignmentAnnotation[] sqann = seq.getAnnotation();
200 for (int i = 0; i < sqann.length; i++)
202 if (sqann[i] == null)
206 boolean found = (alAnnotation == null);
209 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
211 found = (alAnnotation[apos] == sqann[i]);
216 // only copy the given annotation
217 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
218 addAlignmentAnnotation(newann);
222 if (seq.getPDBId() != null)
224 Vector ids = seq.getPDBId();
225 Enumeration e = ids.elements();
226 while (e.hasMoreElements())
228 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
239 public void setSequenceFeatures(SequenceFeature[] features)
241 sequenceFeatures = features;
244 public synchronized void addSequenceFeature(SequenceFeature sf)
246 if (sequenceFeatures == null)
248 sequenceFeatures = new SequenceFeature[0];
251 for (int i = 0; i < sequenceFeatures.length; i++)
253 if (sequenceFeatures[i].equals(sf))
259 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
260 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
261 temp[sequenceFeatures.length] = sf;
263 sequenceFeatures = temp;
266 public void deleteFeature(SequenceFeature sf)
268 if (sequenceFeatures == null)
274 for (index = 0; index < sequenceFeatures.length; index++)
276 if (sequenceFeatures[index].equals(sf))
282 if (index == sequenceFeatures.length)
287 int sfLength = sequenceFeatures.length;
290 sequenceFeatures = null;
294 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
295 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
297 if (index < sfLength)
299 System.arraycopy(sequenceFeatures, index + 1, temp, index,
300 sequenceFeatures.length - index - 1);
303 sequenceFeatures = temp;
310 * @return DOCUMENT ME!
312 public SequenceFeature[] getSequenceFeatures()
314 return sequenceFeatures;
317 public void addPDBId(PDBEntry entry)
321 pdbIds = new Vector();
323 if (!pdbIds.contains(entry))
325 pdbIds.addElement(entry);
335 public void setPDBId(Vector id)
343 * @return DOCUMENT ME!
345 public Vector getPDBId()
353 * @return DOCUMENT ME!
355 public String getDisplayId(boolean jvsuffix)
357 StringBuffer result = new StringBuffer(name);
360 result.append("/" + start + "-" + end);
363 return result.toString();
372 public void setName(String name)
381 * @return DOCUMENT ME!
383 public String getName()
394 public void setStart(int start)
402 * @return DOCUMENT ME!
404 public int getStart()
415 public void setEnd(int end)
423 * @return DOCUMENT ME!
433 * @return DOCUMENT ME!
435 public int getLength()
437 return this.sequence.length;
446 public void setSequence(String seq)
448 this.sequence = seq.toCharArray();
452 public String getSequenceAsString()
454 return new String(sequence);
457 public String getSequenceAsString(int start, int end)
459 return new String(getSequence(start, end));
462 public char[] getSequence()
470 * @see jalview.datamodel.SequenceI#getSequence(int, int)
472 public char[] getSequence(int start, int end)
476 // JBPNote - left to user to pad the result here (TODO:Decide on this
478 if (start >= sequence.length)
483 if (end >= sequence.length)
485 end = sequence.length;
488 char[] reply = new char[end - start];
489 System.arraycopy(sequence, start, reply, 0, end - start);
495 * make a new Sequence object from start to end (including gaps) over this
504 public SequenceI getSubSequence(int start, int end)
510 char[] seq = getSequence(start, end);
515 int nstart = findPosition(start);
516 int nend = findPosition(end) - 1;
517 // JBPNote - this is an incomplete copy.
518 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
519 nseq.setDescription(description);
520 if (datasetSequence != null)
522 nseq.setDatasetSequence(datasetSequence);
526 nseq.setDatasetSequence(this);
537 * @return DOCUMENT ME!
539 public char getCharAt(int i)
541 if (i < sequence.length)
557 public void setDescription(String desc)
559 this.description = desc;
565 * @return DOCUMENT ME!
567 public String getDescription()
569 return this.description;
573 * Return the alignment position for a sequence position
576 * lying from start to end
578 * @return aligned position of residue pos
580 public int findIndex(int pos)
582 // returns the alignment position for a residue
586 while ((i < sequence.length) && (j <= end) && (j <= pos))
588 if (!jalview.util.Comparison.isGap(sequence[i]))
596 if ((j == end) && (j < pos))
607 * Returns the sequence position for an alignment position
610 * column index in alignment (from 1)
612 * @return residue number for residue (left of and) nearest ith column
614 public int findPosition(int i)
618 int seqlen = sequence.length;
619 while ((j < i) && (j < seqlen))
621 if (!jalview.util.Comparison.isGap(sequence[j]))
633 * Returns an int array where indices correspond to each residue in the
634 * sequence and the element value gives its position in the alignment
636 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
637 * residues in SequenceI object
639 public int[] gapMap()
641 String seq = jalview.analysis.AlignSeq.extractGaps(
642 jalview.util.Comparison.GapChars, new String(sequence));
643 int[] map = new int[seq.length()];
647 while (j < sequence.length)
649 if (!jalview.util.Comparison.isGap(sequence[j]))
663 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
665 public void deleteChars(int i, int j)
667 int newstart = start, newend = end;
668 if (i >= sequence.length)
675 if (j >= sequence.length)
678 System.arraycopy(sequence, 0, tmp, 0, i);
682 tmp = new char[sequence.length - j + i];
683 System.arraycopy(sequence, 0, tmp, 0, i);
684 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
686 boolean createNewDs = false;
687 for (int s = i; s < j; s++)
689 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
697 int sindex = findIndex(start) - 1;
700 // delete characters including start of sequence
701 newstart = findPosition(j);
702 break; // don't need to search for any more residue characters.
706 // delete characters after start.
707 int eindex = findIndex(end) - 1;
710 // delete characters at end of sequence
711 newend = findPosition(i - 1);
712 break; // don't need to search for any more residue characters.
717 newend--; // decrease end position by one for the deleted residue
718 // and search further
724 // deletion occured in the middle of the sequence
725 if (createNewDs && this.datasetSequence != null)
727 // construct a new sequence
728 Sequence ds = new Sequence(datasetSequence);
729 // TODO: remove any non-inheritable properties ?
730 // TODO: create a sequence mapping (since there is a relation here ?)
731 ds.deleteChars(i, j);
732 datasetSequence = ds;
749 public void insertCharAt(int i, int length, char c)
751 char[] tmp = new char[sequence.length + length];
753 if (i >= sequence.length)
755 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
760 System.arraycopy(sequence, 0, tmp, 0, i);
770 if (i < sequence.length)
772 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
778 public void insertCharAt(int i, char c)
780 insertCharAt(i, 1, c);
783 public String getVamsasId()
788 public void setVamsasId(String id)
793 public void setDBRef(DBRefEntry[] dbref)
798 public DBRefEntry[] getDBRef()
803 public void addDBRef(DBRefEntry entry)
807 dbrefs = new DBRefEntry[0];
810 int i, iSize = dbrefs.length;
812 for (i = 0; i < iSize; i++)
814 if (dbrefs[i].equalRef(entry))
816 if (entry.getMap() != null)
818 if (dbrefs[i].getMap() == null)
820 // overwrite with 'superior' entry that contains a mapping.
828 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
829 System.arraycopy(dbrefs, 0, temp, 0, iSize);
830 temp[temp.length - 1] = entry;
835 public void setDatasetSequence(SequenceI seq)
837 datasetSequence = seq;
840 public SequenceI getDatasetSequence()
842 return datasetSequence;
845 public AlignmentAnnotation[] getAnnotation()
847 if (annotation == null)
852 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
853 for (int r = 0; r < ret.length; r++)
855 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
861 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
863 if (this.annotation == null)
865 this.annotation = new Vector();
868 this.annotation.addElement(annotation);
869 annotation.setSequenceRef(this);
872 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
874 if (this.annotation != null)
876 this.annotation.removeElement(annotation);
877 if (this.annotation.size() == 0)
878 this.annotation = null;
883 * test if this is a valid candidate for another sequence's dataset sequence.
886 private boolean isValidDatasetSequence()
888 if (datasetSequence != null)
892 for (int i = 0; i < sequence.length; i++)
894 if (jalview.util.Comparison.isGap(sequence[i]))
905 * @see jalview.datamodel.SequenceI#deriveSequence()
907 public SequenceI deriveSequence()
909 SequenceI seq = new Sequence(this);
910 if (datasetSequence != null)
912 // duplicate current sequence with same dataset
913 seq.setDatasetSequence(datasetSequence);
917 if (isValidDatasetSequence())
919 // Use this as dataset sequence
920 seq.setDatasetSequence(this);
924 // Create a new, valid dataset sequence
926 ds.setSequence(AlignSeq.extractGaps(
927 jalview.util.Comparison.GapChars, new String(sequence)));
928 setDatasetSequence(ds);
929 ds.setSequenceFeatures(getSequenceFeatures());
930 seq = this; // and return this sequence as the derived sequence.
939 * @see jalview.datamodel.SequenceI#createDatasetSequence()
941 public SequenceI createDatasetSequence()
943 if (datasetSequence == null)
945 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
946 jalview.util.Comparison.GapChars, getSequenceAsString()),
947 getStart(), getEnd());
948 datasetSequence.setSequenceFeatures(getSequenceFeatures());
949 datasetSequence.setDescription(getDescription());
950 setSequenceFeatures(null);
951 // move database references onto dataset sequence
952 datasetSequence.setDBRef(getDBRef());
954 datasetSequence.setPDBId(getPDBId());
956 datasetSequence.updatePDBIds();
958 return datasetSequence;
964 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
967 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
969 if (annotation != null)
971 annotation.removeAllElements();
973 if (annotations != null)
975 for (int i = 0; i < annotations.length; i++)
977 if (annotations[i] != null)
978 addAlignmentAnnotation(annotations[i]);
986 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
988 public AlignmentAnnotation[] getAnnotation(String label)
990 if (annotation == null || annotation.size() == 0)
995 Vector subset = new Vector();
996 Enumeration e = annotation.elements();
997 while (e.hasMoreElements())
999 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1000 if (ann.label != null && ann.label.equals(label))
1002 subset.addElement(ann);
1005 if (subset.size() == 0)
1009 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1011 e = subset.elements();
1012 while (e.hasMoreElements())
1014 anns[i++] = (AlignmentAnnotation) e.nextElement();
1016 subset.removeAllElements();
1020 public boolean updatePDBIds()
1022 if (datasetSequence != null)
1024 // TODO: could merge DBRefs
1025 return datasetSequence.updatePDBIds();
1027 if (dbrefs == null || dbrefs.length == 0)
1031 Vector newpdb = new Vector();
1032 for (int i = 0; i < dbrefs.length; i++)
1034 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1036 PDBEntry pdbe = new PDBEntry();
1037 pdbe.setId(dbrefs[i].getAccessionId());
1038 if (pdbIds == null || pdbIds.size() == 0)
1040 newpdb.addElement(pdbe);
1044 Enumeration en = pdbIds.elements();
1045 boolean matched = false;
1046 while (!matched && en.hasMoreElements())
1048 PDBEntry anentry = (PDBEntry) en.nextElement();
1049 if (anentry.getId().equals(pdbe.getId()))
1056 newpdb.addElement(pdbe);
1061 if (newpdb.size() > 0)
1063 Enumeration en = newpdb.elements();
1064 while (en.hasMoreElements())
1066 addPDBId((PDBEntry) en.nextElement());
1076 * @see jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1077 * jalview.datamodel.Mapping)
1079 public void transferAnnotation(SequenceI entry, Mapping mp)
1081 if (datasetSequence != null)
1083 datasetSequence.transferAnnotation(entry, mp);
1086 if (entry.getDatasetSequence() != null)
1088 transferAnnotation(entry.getDatasetSequence(), mp);
1091 // transfer any new features from entry onto sequence
1092 if (entry.getSequenceFeatures() != null)
1095 SequenceFeature[] sfs = entry.getSequenceFeatures();
1096 for (int si = 0; si < sfs.length; si++)
1098 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1099 : new SequenceFeature[]
1100 { new SequenceFeature(sfs[si]) };
1101 if (sf != null && sf.length > 0)
1103 for (int sfi = 0; sfi < sf.length; sfi++)
1105 addSequenceFeature(sf[sfi]);
1111 // transfer PDB entries
1112 if (entry.getPDBId() != null)
1114 Enumeration e = entry.getPDBId().elements();
1115 while (e.hasMoreElements())
1117 PDBEntry pdb = (PDBEntry) e.nextElement();
1121 // transfer database references
1122 DBRefEntry[] entryRefs = entry.getDBRef();
1123 if (entryRefs != null)
1125 for (int r = 0; r < entryRefs.length; r++)
1127 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1128 if (newref.getMap() != null && mp != null)
1130 // remap ref using our local mapping
1132 // we also assume all version string setting is done by dbSourceProxy
1134 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1135 * newref.setSource(dbSource); }