2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.analysis.AlignSeq;
24 import jalview.util.StringUtils;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.List;
29 import java.util.Vector;
31 import fr.orsay.lri.varna.models.rna.RNA;
35 * Implements the SequenceI interface for a char[] based sequence object.
40 public class Sequence implements SequenceI
42 SequenceI datasetSequence;
46 private char[] sequence;
63 * This annotation is displayed below the alignment but the positions are tied
64 * to the residues of this sequence
66 * TODO: change to List<>
68 Vector<AlignmentAnnotation> annotation;
71 * The index of the sequence in a MSA
75 /** array of sequence features - may not be null for a valid sequence object */
76 public SequenceFeature[] sequenceFeatures;
79 * Creates a new Sequence object.
84 * string to form a possibly gapped sequence out of
86 * first position of non-gap residue in the sequence
88 * last position of ungapped residues (nearly always only used for
91 public Sequence(String name, String sequence, int start, int end)
94 this.sequence = sequence.toCharArray();
101 public Sequence(String name, char[] sequence, int start, int end)
104 this.sequence = sequence;
111 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
112 "[/][0-9]{1,}[-][0-9]{1,}$");
114 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
121 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
124 // Does sequence have the /start-end signiature?
125 if (limitrx.search(name))
127 name = limitrx.left();
128 endrx.search(limitrx.stringMatched());
129 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
130 endrx.matchedFrom() - 1)));
131 setEnd(Integer.parseInt(endrx.stringMatched()));
135 void checkValidRange()
138 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
141 for (int j = 0; j < sequence.length; j++)
143 if (!jalview.util.Comparison.isGap(sequence[j]))
162 * Creates a new Sequence object.
169 public Sequence(String name, String sequence)
171 this(name, sequence, 1, -1);
175 * Creates a new Sequence object with new features, DBRefEntries,
176 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
182 public Sequence(SequenceI seq)
184 this(seq, seq.getAnnotation());
188 * Create a new sequence object with new features, DBRefEntries, and PDBIds
189 * but inherits any existing dataset sequence reference, and duplicate of any
190 * annotation that is present in the given annotation array.
193 * the sequence to be copied
194 * @param alAnnotation
195 * an array of annotation including some associated with seq
197 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
199 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
200 description = seq.getDescription();
201 if (seq.getSequenceFeatures() != null)
203 SequenceFeature[] sf = seq.getSequenceFeatures();
204 for (int i = 0; i < sf.length; i++)
206 addSequenceFeature(new SequenceFeature(sf[i]));
209 setDatasetSequence(seq.getDatasetSequence());
210 if (datasetSequence == null && seq.getDBRef() != null)
212 // only copy DBRefs if we really are a dataset sequence
213 DBRefEntry[] dbr = seq.getDBRef();
214 for (int i = 0; i < dbr.length; i++)
216 addDBRef(new DBRefEntry(dbr[i]));
219 if (seq.getAnnotation() != null)
221 AlignmentAnnotation[] sqann = seq.getAnnotation();
222 for (int i = 0; i < sqann.length; i++)
224 if (sqann[i] == null)
228 boolean found = (alAnnotation == null);
231 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
233 found = (alAnnotation[apos] == sqann[i]);
238 // only copy the given annotation
239 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
240 addAlignmentAnnotation(newann);
244 if (seq.getPDBId() != null)
246 Vector ids = seq.getPDBId();
247 Enumeration e = ids.elements();
248 while (e.hasMoreElements())
250 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
261 public void setSequenceFeatures(SequenceFeature[] features)
263 sequenceFeatures = features;
266 public synchronized void addSequenceFeature(SequenceFeature sf)
268 if (sequenceFeatures == null)
270 sequenceFeatures = new SequenceFeature[0];
273 for (int i = 0; i < sequenceFeatures.length; i++)
275 if (sequenceFeatures[i].equals(sf))
281 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
282 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
283 temp[sequenceFeatures.length] = sf;
285 sequenceFeatures = temp;
288 public void deleteFeature(SequenceFeature sf)
290 if (sequenceFeatures == null)
296 for (index = 0; index < sequenceFeatures.length; index++)
298 if (sequenceFeatures[index].equals(sf))
304 if (index == sequenceFeatures.length)
309 int sfLength = sequenceFeatures.length;
312 sequenceFeatures = null;
316 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
317 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
319 if (index < sfLength)
321 System.arraycopy(sequenceFeatures, index + 1, temp, index,
322 sequenceFeatures.length - index - 1);
325 sequenceFeatures = temp;
330 * Returns the sequence features (if any), looking first on the sequence, then
331 * on its dataset sequence, and so on until a non-null value is found (or
332 * none). This supports retrieval of sequence features stored on the sequence
333 * (as in the applet) or on the dataset sequence (as in the Desktop version).
337 public SequenceFeature[] getSequenceFeatures()
339 SequenceFeature[] features = sequenceFeatures;
341 SequenceI seq = this;
342 int count = 0; // failsafe against loop in sequence.datasetsequence...
343 while (features == null && seq.getDatasetSequence() != null
346 seq = seq.getDatasetSequence();
347 features = ((Sequence) seq).sequenceFeatures;
352 public void addPDBId(PDBEntry entry)
356 pdbIds = new Vector();
358 if (!pdbIds.contains(entry))
360 pdbIds.addElement(entry);
370 public void setPDBId(Vector id)
378 * @return DOCUMENT ME!
380 public Vector getPDBId()
388 * @return DOCUMENT ME!
390 public String getDisplayId(boolean jvsuffix)
392 StringBuffer result = new StringBuffer(name);
395 result.append("/" + start + "-" + end);
398 return result.toString();
407 public void setName(String name)
416 * @return DOCUMENT ME!
418 public String getName()
429 public void setStart(int start)
437 * @return DOCUMENT ME!
439 public int getStart()
450 public void setEnd(int end)
458 * @return DOCUMENT ME!
468 * @return DOCUMENT ME!
470 public int getLength()
472 return this.sequence.length;
481 public void setSequence(String seq)
483 this.sequence = seq.toCharArray();
487 public String getSequenceAsString()
489 return new String(sequence);
492 public String getSequenceAsString(int start, int end)
494 return new String(getSequence(start, end));
497 public char[] getSequence()
505 * @see jalview.datamodel.SequenceI#getSequence(int, int)
507 public char[] getSequence(int start, int end)
513 // JBPNote - left to user to pad the result here (TODO:Decide on this
515 if (start >= sequence.length)
520 if (end >= sequence.length)
522 end = sequence.length;
525 char[] reply = new char[end - start];
526 System.arraycopy(sequence, start, reply, 0, end - start);
532 public SequenceI getSubSequence(int start, int end)
538 char[] seq = getSequence(start, end);
543 int nstart = findPosition(start);
544 int nend = findPosition(end) - 1;
545 // JBPNote - this is an incomplete copy.
546 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
547 nseq.setDescription(description);
548 if (datasetSequence != null)
550 nseq.setDatasetSequence(datasetSequence);
554 nseq.setDatasetSequence(this);
565 * @return DOCUMENT ME!
567 public char getCharAt(int i)
569 if (i < sequence.length)
585 public void setDescription(String desc)
587 this.description = desc;
593 * @return DOCUMENT ME!
595 public String getDescription()
597 return this.description;
603 * @see jalview.datamodel.SequenceI#findIndex(int)
605 public int findIndex(int pos)
607 // returns the alignment position for a residue
610 // Rely on end being at least as long as the length of the sequence.
611 while ((i < sequence.length) && (j <= end) && (j <= pos))
613 if (!jalview.util.Comparison.isGap(sequence[i]))
621 if ((j == end) && (j < pos))
632 public int findPosition(int i)
636 int seqlen = sequence.length;
637 while ((j < i) && (j < seqlen))
639 if (!jalview.util.Comparison.isGap(sequence[j]))
651 * Returns an int array where indices correspond to each residue in the
652 * sequence and the element value gives its position in the alignment
654 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
655 * residues in SequenceI object
657 public int[] gapMap()
659 String seq = jalview.analysis.AlignSeq.extractGaps(
660 jalview.util.Comparison.GapChars, new String(sequence));
661 int[] map = new int[seq.length()];
665 while (j < sequence.length)
667 if (!jalview.util.Comparison.isGap(sequence[j]))
679 public int[] findPositionMap()
681 int map[] = new int[sequence.length];
684 int seqlen = sequence.length;
688 if (!jalview.util.Comparison.isGap(sequence[j]))
699 public List<int[]> getInsertions()
701 ArrayList<int[]> map = new ArrayList<int[]>();
702 int lastj = -1, j = 0;
704 int seqlen = sequence.length;
707 if (jalview.util.Comparison.isGap(sequence[j]))
735 public void deleteChars(int i, int j)
737 int newstart = start, newend = end;
738 if (i >= sequence.length)
743 char[] tmp = StringUtils.deleteChars(sequence, i, j);
744 boolean createNewDs = false;
745 // TODO: take a (second look) at the dataset creation validation method for
746 // the very large sequence case
747 int eindex = -1, sindex = -1;
748 boolean ecalc = false, scalc = false;
749 for (int s = i; s < j; s++)
751 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
761 sindex = findIndex(start) - 1;
766 // delete characters including start of sequence
767 newstart = findPosition(j);
768 break; // don't need to search for any more residue characters.
772 // delete characters after start.
775 eindex = findIndex(end) - 1;
780 // delete characters at end of sequence
781 newend = findPosition(i - 1);
782 break; // don't need to search for any more residue characters.
787 newend--; // decrease end position by one for the deleted residue
788 // and search further
794 // deletion occured in the middle of the sequence
795 if (createNewDs && this.datasetSequence != null)
797 // construct a new sequence
798 Sequence ds = new Sequence(datasetSequence);
799 // TODO: remove any non-inheritable properties ?
800 // TODO: create a sequence mapping (since there is a relation here ?)
801 ds.deleteChars(i, j);
802 datasetSequence = ds;
810 public void insertCharAt(int i, int length, char c)
812 char[] tmp = new char[sequence.length + length];
814 if (i >= sequence.length)
816 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
821 System.arraycopy(sequence, 0, tmp, 0, i);
831 if (i < sequence.length)
833 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
840 public void insertCharAt(int i, char c)
842 insertCharAt(i, 1, c);
846 public String getVamsasId()
852 public void setVamsasId(String id)
858 public void setDBRef(DBRefEntry[] dbref)
864 public DBRefEntry[] getDBRef()
866 if (dbrefs == null && datasetSequence != null
867 && this != datasetSequence)
869 return datasetSequence.getDBRef();
875 public void addDBRef(DBRefEntry entry)
879 dbrefs = new DBRefEntry[0];
882 int i, iSize = dbrefs.length;
884 for (i = 0; i < iSize; i++)
886 if (dbrefs[i].equalRef(entry))
888 if (entry.getMap() != null)
890 if (dbrefs[i].getMap() == null)
892 // overwrite with 'superior' entry that contains a mapping.
900 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
901 System.arraycopy(dbrefs, 0, temp, 0, iSize);
902 temp[temp.length - 1] = entry;
908 public void setDatasetSequence(SequenceI seq)
910 datasetSequence = seq;
914 public SequenceI getDatasetSequence()
916 return datasetSequence;
920 public AlignmentAnnotation[] getAnnotation()
922 return annotation == null ? null : annotation
923 .toArray(new AlignmentAnnotation[annotation.size()]);
928 public boolean hasAnnotation(AlignmentAnnotation ann)
930 return annotation == null ? false : annotation.contains(ann);
934 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
936 if (this.annotation == null)
938 this.annotation = new Vector();
940 if (!this.annotation.contains(annotation))
942 this.annotation.addElement(annotation);
944 annotation.setSequenceRef(this);
947 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
949 if (this.annotation != null)
951 this.annotation.removeElement(annotation);
952 if (this.annotation.size() == 0)
954 this.annotation = null;
960 * test if this is a valid candidate for another sequence's dataset sequence.
963 private boolean isValidDatasetSequence()
965 if (datasetSequence != null)
969 for (int i = 0; i < sequence.length; i++)
971 if (jalview.util.Comparison.isGap(sequence[i]))
980 public SequenceI deriveSequence()
982 SequenceI seq = new Sequence(this);
983 if (datasetSequence != null)
985 // duplicate current sequence with same dataset
986 seq.setDatasetSequence(datasetSequence);
990 if (isValidDatasetSequence())
992 // Use this as dataset sequence
993 seq.setDatasetSequence(this);
997 // Create a new, valid dataset sequence
999 ds.setSequence(AlignSeq.extractGaps(
1000 jalview.util.Comparison.GapChars, new String(sequence)));
1001 setDatasetSequence(ds);
1002 ds.setSequenceFeatures(getSequenceFeatures());
1003 seq = this; // and return this sequence as the derived sequence.
1012 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1014 public SequenceI createDatasetSequence()
1016 if (datasetSequence == null)
1018 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1019 jalview.util.Comparison.GapChars, getSequenceAsString()),
1020 getStart(), getEnd());
1021 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1022 datasetSequence.setDescription(getDescription());
1023 setSequenceFeatures(null);
1024 // move database references onto dataset sequence
1025 datasetSequence.setDBRef(getDBRef());
1027 datasetSequence.setPDBId(getPDBId());
1029 datasetSequence.updatePDBIds();
1030 if (annotation != null)
1032 for (AlignmentAnnotation aa : annotation)
1034 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1035 _aa.sequenceRef = datasetSequence;
1036 _aa.adjustForAlignment(); // uses annotation's own record of
1037 // sequence-column mapping
1038 datasetSequence.addAlignmentAnnotation(_aa);
1042 return datasetSequence;
1049 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1052 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1054 if (annotation != null)
1056 annotation.removeAllElements();
1058 if (annotations != null)
1060 for (int i = 0; i < annotations.length; i++)
1062 if (annotations[i] != null)
1064 addAlignmentAnnotation(annotations[i]);
1071 public AlignmentAnnotation[] getAnnotation(String label)
1073 if (annotation == null || annotation.size() == 0)
1078 Vector subset = new Vector();
1079 Enumeration e = annotation.elements();
1080 while (e.hasMoreElements())
1082 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1083 if (ann.label != null && ann.label.equals(label))
1085 subset.addElement(ann);
1088 if (subset.size() == 0)
1092 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1094 e = subset.elements();
1095 while (e.hasMoreElements())
1097 anns[i++] = (AlignmentAnnotation) e.nextElement();
1099 subset.removeAllElements();
1104 public boolean updatePDBIds()
1106 if (datasetSequence != null)
1108 // TODO: could merge DBRefs
1109 return datasetSequence.updatePDBIds();
1111 if (dbrefs == null || dbrefs.length == 0)
1115 Vector newpdb = new Vector();
1116 for (int i = 0; i < dbrefs.length; i++)
1118 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1120 PDBEntry pdbe = new PDBEntry();
1121 pdbe.setId(dbrefs[i].getAccessionId());
1122 if (pdbIds == null || pdbIds.size() == 0)
1124 newpdb.addElement(pdbe);
1128 Enumeration en = pdbIds.elements();
1129 boolean matched = false;
1130 while (!matched && en.hasMoreElements())
1132 PDBEntry anentry = (PDBEntry) en.nextElement();
1133 if (anentry.getId().equals(pdbe.getId()))
1140 newpdb.addElement(pdbe);
1145 if (newpdb.size() > 0)
1147 Enumeration en = newpdb.elements();
1148 while (en.hasMoreElements())
1150 addPDBId((PDBEntry) en.nextElement());
1158 public void transferAnnotation(SequenceI entry, Mapping mp)
1160 if (datasetSequence != null)
1162 datasetSequence.transferAnnotation(entry, mp);
1165 if (entry.getDatasetSequence() != null)
1167 transferAnnotation(entry.getDatasetSequence(), mp);
1170 // transfer any new features from entry onto sequence
1171 if (entry.getSequenceFeatures() != null)
1174 SequenceFeature[] sfs = entry.getSequenceFeatures();
1175 for (int si = 0; si < sfs.length; si++)
1177 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1178 : new SequenceFeature[]
1179 { new SequenceFeature(sfs[si]) };
1180 if (sf != null && sf.length > 0)
1182 for (int sfi = 0; sfi < sf.length; sfi++)
1184 addSequenceFeature(sf[sfi]);
1190 // transfer PDB entries
1191 if (entry.getPDBId() != null)
1193 Enumeration e = entry.getPDBId().elements();
1194 while (e.hasMoreElements())
1196 PDBEntry pdb = (PDBEntry) e.nextElement();
1200 // transfer database references
1201 DBRefEntry[] entryRefs = entry.getDBRef();
1202 if (entryRefs != null)
1204 for (int r = 0; r < entryRefs.length; r++)
1206 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1207 if (newref.getMap() != null && mp != null)
1209 // remap ref using our local mapping
1211 // we also assume all version string setting is done by dbSourceProxy
1213 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1214 * newref.setSource(dbSource); }
1222 * @return The index (zero-based) on this sequence in the MSA. It returns
1223 * {@code -1} if this information is not available.
1225 public int getIndex()
1231 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1232 * if this information is undefined.
1235 * position for this sequence. This value is zero-based (zero for
1236 * this first sequence)
1238 public void setIndex(int value)
1243 public void setRNA(RNA r)
1254 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1257 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1258 if (this.annotation != null)
1260 for (AlignmentAnnotation ann : annotation)
1262 if (ann.calcId != null && ann.calcId.equals(calcId)
1263 && ann.label != null && ann.label.equals(label))