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]))
662 * @see jalview.datamodel.SequenceI#findPositionMap()
664 public int[] findPositionMap()
666 int map[] = new int[sequence.length];
669 int seqlen = sequence.length;
673 if (!jalview.util.Comparison.isGap(sequence[j]))
685 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
687 public void deleteChars(int i, int j)
689 int newstart = start, newend = end;
690 if (i >= sequence.length)
697 if (j >= sequence.length)
700 System.arraycopy(sequence, 0, tmp, 0, i);
704 tmp = new char[sequence.length - j + i];
705 System.arraycopy(sequence, 0, tmp, 0, i);
706 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
708 boolean createNewDs = false;
709 for (int s = i; s < j; s++)
711 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
719 int sindex = findIndex(start) - 1;
722 // delete characters including start of sequence
723 newstart = findPosition(j);
724 break; // don't need to search for any more residue characters.
728 // delete characters after start.
729 int eindex = findIndex(end) - 1;
732 // delete characters at end of sequence
733 newend = findPosition(i - 1);
734 break; // don't need to search for any more residue characters.
739 newend--; // decrease end position by one for the deleted residue
740 // and search further
746 // deletion occured in the middle of the sequence
747 if (createNewDs && this.datasetSequence != null)
749 // construct a new sequence
750 Sequence ds = new Sequence(datasetSequence);
751 // TODO: remove any non-inheritable properties ?
752 // TODO: create a sequence mapping (since there is a relation here ?)
753 ds.deleteChars(i, j);
754 datasetSequence = ds;
771 public void insertCharAt(int i, int length, char c)
773 char[] tmp = new char[sequence.length + length];
775 if (i >= sequence.length)
777 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
782 System.arraycopy(sequence, 0, tmp, 0, i);
792 if (i < sequence.length)
794 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
800 public void insertCharAt(int i, char c)
802 insertCharAt(i, 1, c);
805 public String getVamsasId()
810 public void setVamsasId(String id)
815 public void setDBRef(DBRefEntry[] dbref)
820 public DBRefEntry[] getDBRef()
825 public void addDBRef(DBRefEntry entry)
829 dbrefs = new DBRefEntry[0];
832 int i, iSize = dbrefs.length;
834 for (i = 0; i < iSize; i++)
836 if (dbrefs[i].equalRef(entry))
838 if (entry.getMap() != null)
840 if (dbrefs[i].getMap() == null)
842 // overwrite with 'superior' entry that contains a mapping.
850 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
851 System.arraycopy(dbrefs, 0, temp, 0, iSize);
852 temp[temp.length - 1] = entry;
857 public void setDatasetSequence(SequenceI seq)
859 datasetSequence = seq;
862 public SequenceI getDatasetSequence()
864 return datasetSequence;
867 public AlignmentAnnotation[] getAnnotation()
869 if (annotation == null)
874 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
875 for (int r = 0; r < ret.length; r++)
877 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
883 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
885 if (this.annotation == null)
887 this.annotation = new Vector();
890 this.annotation.addElement(annotation);
891 annotation.setSequenceRef(this);
894 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
896 if (this.annotation != null)
898 this.annotation.removeElement(annotation);
899 if (this.annotation.size() == 0)
900 this.annotation = null;
905 * test if this is a valid candidate for another sequence's dataset sequence.
908 private boolean isValidDatasetSequence()
910 if (datasetSequence != null)
914 for (int i = 0; i < sequence.length; i++)
916 if (jalview.util.Comparison.isGap(sequence[i]))
927 * @see jalview.datamodel.SequenceI#deriveSequence()
929 public SequenceI deriveSequence()
931 SequenceI seq = new Sequence(this);
932 if (datasetSequence != null)
934 // duplicate current sequence with same dataset
935 seq.setDatasetSequence(datasetSequence);
939 if (isValidDatasetSequence())
941 // Use this as dataset sequence
942 seq.setDatasetSequence(this);
946 // Create a new, valid dataset sequence
948 ds.setSequence(AlignSeq.extractGaps(
949 jalview.util.Comparison.GapChars, new String(sequence)));
950 setDatasetSequence(ds);
951 ds.setSequenceFeatures(getSequenceFeatures());
952 seq = this; // and return this sequence as the derived sequence.
961 * @see jalview.datamodel.SequenceI#createDatasetSequence()
963 public SequenceI createDatasetSequence()
965 if (datasetSequence == null)
967 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
968 jalview.util.Comparison.GapChars, getSequenceAsString()),
969 getStart(), getEnd());
970 datasetSequence.setSequenceFeatures(getSequenceFeatures());
971 datasetSequence.setDescription(getDescription());
972 setSequenceFeatures(null);
973 // move database references onto dataset sequence
974 datasetSequence.setDBRef(getDBRef());
976 datasetSequence.setPDBId(getPDBId());
978 datasetSequence.updatePDBIds();
980 return datasetSequence;
986 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
989 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
991 if (annotation != null)
993 annotation.removeAllElements();
995 if (annotations != null)
997 for (int i = 0; i < annotations.length; i++)
999 if (annotations[i] != null)
1000 addAlignmentAnnotation(annotations[i]);
1008 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1010 public AlignmentAnnotation[] getAnnotation(String label)
1012 if (annotation == null || annotation.size() == 0)
1017 Vector subset = new Vector();
1018 Enumeration e = annotation.elements();
1019 while (e.hasMoreElements())
1021 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1022 if (ann.label != null && ann.label.equals(label))
1024 subset.addElement(ann);
1027 if (subset.size() == 0)
1031 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1033 e = subset.elements();
1034 while (e.hasMoreElements())
1036 anns[i++] = (AlignmentAnnotation) e.nextElement();
1038 subset.removeAllElements();
1042 public boolean updatePDBIds()
1044 if (datasetSequence != null)
1046 // TODO: could merge DBRefs
1047 return datasetSequence.updatePDBIds();
1049 if (dbrefs == null || dbrefs.length == 0)
1053 Vector newpdb = new Vector();
1054 for (int i = 0; i < dbrefs.length; i++)
1056 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1058 PDBEntry pdbe = new PDBEntry();
1059 pdbe.setId(dbrefs[i].getAccessionId());
1060 if (pdbIds == null || pdbIds.size() == 0)
1062 newpdb.addElement(pdbe);
1066 Enumeration en = pdbIds.elements();
1067 boolean matched = false;
1068 while (!matched && en.hasMoreElements())
1070 PDBEntry anentry = (PDBEntry) en.nextElement();
1071 if (anentry.getId().equals(pdbe.getId()))
1078 newpdb.addElement(pdbe);
1083 if (newpdb.size() > 0)
1085 Enumeration en = newpdb.elements();
1086 while (en.hasMoreElements())
1088 addPDBId((PDBEntry) en.nextElement());
1098 * @see jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1099 * jalview.datamodel.Mapping)
1101 public void transferAnnotation(SequenceI entry, Mapping mp)
1103 if (datasetSequence != null)
1105 datasetSequence.transferAnnotation(entry, mp);
1108 if (entry.getDatasetSequence() != null)
1110 transferAnnotation(entry.getDatasetSequence(), mp);
1113 // transfer any new features from entry onto sequence
1114 if (entry.getSequenceFeatures() != null)
1117 SequenceFeature[] sfs = entry.getSequenceFeatures();
1118 for (int si = 0; si < sfs.length; si++)
1120 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1121 : new SequenceFeature[]
1122 { new SequenceFeature(sfs[si]) };
1123 if (sf != null && sf.length > 0)
1125 for (int sfi = 0; sfi < sf.length; sfi++)
1127 addSequenceFeature(sf[sfi]);
1133 // transfer PDB entries
1134 if (entry.getPDBId() != null)
1136 Enumeration e = entry.getPDBId().elements();
1137 while (e.hasMoreElements())
1139 PDBEntry pdb = (PDBEntry) e.nextElement();
1143 // transfer database references
1144 DBRefEntry[] entryRefs = entry.getDBRef();
1145 if (entryRefs != null)
1147 for (int r = 0; r < entryRefs.length; r++)
1149 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1150 if (newref.getMap() != null && mp != null)
1152 // remap ref using our local mapping
1154 // we also assume all version string setting is done by dbSourceProxy
1156 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1157 * newref.setSource(dbSource); }