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;
54 Vector<PDBEntry> pdbIds;
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<PDBEntry>();
358 if (pdbIds.contains(entry))
360 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
364 pdbIds.addElement(entry);
368 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
370 if (newEntry.getFile() != null)
372 oldEntry.setFile(newEntry.getFile());
382 public void setPDBId(Vector id)
390 * @return DOCUMENT ME!
392 public Vector getPDBId()
400 * @return DOCUMENT ME!
402 public String getDisplayId(boolean jvsuffix)
404 StringBuffer result = new StringBuffer(name);
407 result.append("/" + start + "-" + end);
410 return result.toString();
419 public void setName(String name)
428 * @return DOCUMENT ME!
430 public String getName()
441 public void setStart(int start)
449 * @return DOCUMENT ME!
451 public int getStart()
462 public void setEnd(int end)
470 * @return DOCUMENT ME!
480 * @return DOCUMENT ME!
482 public int getLength()
484 return this.sequence.length;
493 public void setSequence(String seq)
495 this.sequence = seq.toCharArray();
499 public String getSequenceAsString()
501 return new String(sequence);
504 public String getSequenceAsString(int start, int end)
506 return new String(getSequence(start, end));
509 public char[] getSequence()
517 * @see jalview.datamodel.SequenceI#getSequence(int, int)
519 public char[] getSequence(int start, int end)
525 // JBPNote - left to user to pad the result here (TODO:Decide on this
527 if (start >= sequence.length)
532 if (end >= sequence.length)
534 end = sequence.length;
537 char[] reply = new char[end - start];
538 System.arraycopy(sequence, start, reply, 0, end - start);
544 public SequenceI getSubSequence(int start, int end)
550 char[] seq = getSequence(start, end);
555 int nstart = findPosition(start);
556 int nend = findPosition(end) - 1;
557 // JBPNote - this is an incomplete copy.
558 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
559 nseq.setDescription(description);
560 if (datasetSequence != null)
562 nseq.setDatasetSequence(datasetSequence);
566 nseq.setDatasetSequence(this);
577 * @return DOCUMENT ME!
579 public char getCharAt(int i)
581 if (i < sequence.length)
597 public void setDescription(String desc)
599 this.description = desc;
605 * @return DOCUMENT ME!
607 public String getDescription()
609 return this.description;
615 * @see jalview.datamodel.SequenceI#findIndex(int)
617 public int findIndex(int pos)
619 // returns the alignment position for a residue
622 // Rely on end being at least as long as the length of the sequence.
623 while ((i < sequence.length) && (j <= end) && (j <= pos))
625 if (!jalview.util.Comparison.isGap(sequence[i]))
633 if ((j == end) && (j < pos))
644 public int findPosition(int i)
648 int seqlen = sequence.length;
649 while ((j < i) && (j < seqlen))
651 if (!jalview.util.Comparison.isGap(sequence[j]))
663 * Returns an int array where indices correspond to each residue in the
664 * sequence and the element value gives its position in the alignment
666 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
667 * residues in SequenceI object
669 public int[] gapMap()
671 String seq = jalview.analysis.AlignSeq.extractGaps(
672 jalview.util.Comparison.GapChars, new String(sequence));
673 int[] map = new int[seq.length()];
677 while (j < sequence.length)
679 if (!jalview.util.Comparison.isGap(sequence[j]))
691 public int[] findPositionMap()
693 int map[] = new int[sequence.length];
696 int seqlen = sequence.length;
700 if (!jalview.util.Comparison.isGap(sequence[j]))
711 public List<int[]> getInsertions()
713 ArrayList<int[]> map = new ArrayList<int[]>();
714 int lastj = -1, j = 0;
716 int seqlen = sequence.length;
719 if (jalview.util.Comparison.isGap(sequence[j]))
747 public void deleteChars(int i, int j)
749 int newstart = start, newend = end;
750 if (i >= sequence.length)
755 char[] tmp = StringUtils.deleteChars(sequence, i, j);
756 boolean createNewDs = false;
757 // TODO: take a (second look) at the dataset creation validation method for
758 // the very large sequence case
759 int eindex = -1, sindex = -1;
760 boolean ecalc = false, scalc = false;
761 for (int s = i; s < j; s++)
763 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
773 sindex = findIndex(start) - 1;
778 // delete characters including start of sequence
779 newstart = findPosition(j);
780 break; // don't need to search for any more residue characters.
784 // delete characters after start.
787 eindex = findIndex(end) - 1;
792 // delete characters at end of sequence
793 newend = findPosition(i - 1);
794 break; // don't need to search for any more residue characters.
799 newend--; // decrease end position by one for the deleted residue
800 // and search further
806 // deletion occured in the middle of the sequence
807 if (createNewDs && this.datasetSequence != null)
809 // construct a new sequence
810 Sequence ds = new Sequence(datasetSequence);
811 // TODO: remove any non-inheritable properties ?
812 // TODO: create a sequence mapping (since there is a relation here ?)
813 ds.deleteChars(i, j);
814 datasetSequence = ds;
822 public void insertCharAt(int i, int length, char c)
824 char[] tmp = new char[sequence.length + length];
826 if (i >= sequence.length)
828 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
833 System.arraycopy(sequence, 0, tmp, 0, i);
843 if (i < sequence.length)
845 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
852 public void insertCharAt(int i, char c)
854 insertCharAt(i, 1, c);
858 public String getVamsasId()
864 public void setVamsasId(String id)
870 public void setDBRef(DBRefEntry[] dbref)
876 public DBRefEntry[] getDBRef()
878 if (dbrefs == null && datasetSequence != null
879 && this != datasetSequence)
881 return datasetSequence.getDBRef();
887 public void addDBRef(DBRefEntry entry)
891 dbrefs = new DBRefEntry[0];
894 int i, iSize = dbrefs.length;
896 for (i = 0; i < iSize; i++)
898 if (dbrefs[i].equalRef(entry))
900 if (entry.getMap() != null)
902 if (dbrefs[i].getMap() == null)
904 // overwrite with 'superior' entry that contains a mapping.
912 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
913 System.arraycopy(dbrefs, 0, temp, 0, iSize);
914 temp[temp.length - 1] = entry;
920 public void setDatasetSequence(SequenceI seq)
922 datasetSequence = seq;
926 public SequenceI getDatasetSequence()
928 return datasetSequence;
932 public AlignmentAnnotation[] getAnnotation()
934 return annotation == null ? null : annotation
935 .toArray(new AlignmentAnnotation[annotation.size()]);
940 public boolean hasAnnotation(AlignmentAnnotation ann)
942 return annotation == null ? false : annotation.contains(ann);
946 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
948 if (this.annotation == null)
950 this.annotation = new Vector();
952 if (!this.annotation.contains(annotation))
954 this.annotation.addElement(annotation);
956 annotation.setSequenceRef(this);
959 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
961 if (this.annotation != null)
963 this.annotation.removeElement(annotation);
964 if (this.annotation.size() == 0)
966 this.annotation = null;
972 * test if this is a valid candidate for another sequence's dataset sequence.
975 private boolean isValidDatasetSequence()
977 if (datasetSequence != null)
981 for (int i = 0; i < sequence.length; i++)
983 if (jalview.util.Comparison.isGap(sequence[i]))
992 public SequenceI deriveSequence()
994 SequenceI seq = new Sequence(this);
995 if (datasetSequence != null)
997 // duplicate current sequence with same dataset
998 seq.setDatasetSequence(datasetSequence);
1002 if (isValidDatasetSequence())
1004 // Use this as dataset sequence
1005 seq.setDatasetSequence(this);
1009 // Create a new, valid dataset sequence
1011 ds.setSequence(AlignSeq.extractGaps(
1012 jalview.util.Comparison.GapChars, new String(sequence)));
1013 setDatasetSequence(ds);
1014 ds.setSequenceFeatures(getSequenceFeatures());
1015 seq = this; // and return this sequence as the derived sequence.
1024 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1026 public SequenceI createDatasetSequence()
1028 if (datasetSequence == null)
1030 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1031 jalview.util.Comparison.GapChars, getSequenceAsString()),
1032 getStart(), getEnd());
1033 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1034 datasetSequence.setDescription(getDescription());
1035 setSequenceFeatures(null);
1036 // move database references onto dataset sequence
1037 datasetSequence.setDBRef(getDBRef());
1039 datasetSequence.setPDBId(getPDBId());
1041 datasetSequence.updatePDBIds();
1042 if (annotation != null)
1044 for (AlignmentAnnotation aa : annotation)
1046 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1047 _aa.sequenceRef = datasetSequence;
1048 _aa.adjustForAlignment(); // uses annotation's own record of
1049 // sequence-column mapping
1050 datasetSequence.addAlignmentAnnotation(_aa);
1054 return datasetSequence;
1061 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1064 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1066 if (annotation != null)
1068 annotation.removeAllElements();
1070 if (annotations != null)
1072 for (int i = 0; i < annotations.length; i++)
1074 if (annotations[i] != null)
1076 addAlignmentAnnotation(annotations[i]);
1083 public AlignmentAnnotation[] getAnnotation(String label)
1085 if (annotation == null || annotation.size() == 0)
1090 Vector subset = new Vector();
1091 Enumeration e = annotation.elements();
1092 while (e.hasMoreElements())
1094 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1095 if (ann.label != null && ann.label.equals(label))
1097 subset.addElement(ann);
1100 if (subset.size() == 0)
1104 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1106 e = subset.elements();
1107 while (e.hasMoreElements())
1109 anns[i++] = (AlignmentAnnotation) e.nextElement();
1111 subset.removeAllElements();
1116 public boolean updatePDBIds()
1118 if (datasetSequence != null)
1120 // TODO: could merge DBRefs
1121 return datasetSequence.updatePDBIds();
1123 if (dbrefs == null || dbrefs.length == 0)
1127 Vector newpdb = new Vector();
1128 for (int i = 0; i < dbrefs.length; i++)
1130 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1132 PDBEntry pdbe = new PDBEntry();
1133 pdbe.setId(dbrefs[i].getAccessionId());
1134 if (pdbIds == null || pdbIds.size() == 0)
1136 newpdb.addElement(pdbe);
1140 Enumeration en = pdbIds.elements();
1141 boolean matched = false;
1142 while (!matched && en.hasMoreElements())
1144 PDBEntry anentry = (PDBEntry) en.nextElement();
1145 if (anentry.getId().equals(pdbe.getId()))
1152 newpdb.addElement(pdbe);
1157 if (newpdb.size() > 0)
1159 Enumeration en = newpdb.elements();
1160 while (en.hasMoreElements())
1162 addPDBId((PDBEntry) en.nextElement());
1170 public void transferAnnotation(SequenceI entry, Mapping mp)
1172 if (datasetSequence != null)
1174 datasetSequence.transferAnnotation(entry, mp);
1177 if (entry.getDatasetSequence() != null)
1179 transferAnnotation(entry.getDatasetSequence(), mp);
1182 // transfer any new features from entry onto sequence
1183 if (entry.getSequenceFeatures() != null)
1186 SequenceFeature[] sfs = entry.getSequenceFeatures();
1187 for (int si = 0; si < sfs.length; si++)
1189 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1190 : new SequenceFeature[]
1191 { new SequenceFeature(sfs[si]) };
1192 if (sf != null && sf.length > 0)
1194 for (int sfi = 0; sfi < sf.length; sfi++)
1196 addSequenceFeature(sf[sfi]);
1202 // transfer PDB entries
1203 if (entry.getPDBId() != null)
1205 Enumeration e = entry.getPDBId().elements();
1206 while (e.hasMoreElements())
1208 PDBEntry pdb = (PDBEntry) e.nextElement();
1212 // transfer database references
1213 DBRefEntry[] entryRefs = entry.getDBRef();
1214 if (entryRefs != null)
1216 for (int r = 0; r < entryRefs.length; r++)
1218 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1219 if (newref.getMap() != null && mp != null)
1221 // remap ref using our local mapping
1223 // we also assume all version string setting is done by dbSourceProxy
1225 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1226 * newref.setSource(dbSource); }
1234 * @return The index (zero-based) on this sequence in the MSA. It returns
1235 * {@code -1} if this information is not available.
1237 public int getIndex()
1243 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1244 * if this information is undefined.
1247 * position for this sequence. This value is zero-based (zero for
1248 * this first sequence)
1250 public void setIndex(int value)
1255 public void setRNA(RNA r)
1266 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1269 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1270 if (this.annotation != null)
1272 for (AlignmentAnnotation ann : annotation)
1274 if (ann.calcId != null && ann.calcId.equals(calcId)
1275 && ann.label != null && ann.label.equals(label))