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 java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.Vector;
28 import fr.orsay.lri.varna.models.rna.RNA;
30 import jalview.analysis.AlignSeq;
31 import jalview.util.StringUtils;
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 signature?
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());
383 public void setPDBId(Vector<PDBEntry> id)
391 * @return DOCUMENT ME!
394 public Vector<PDBEntry> getPDBId()
402 * @return DOCUMENT ME!
404 public String getDisplayId(boolean jvsuffix)
406 StringBuffer result = new StringBuffer(name);
409 result.append("/" + start + "-" + end);
412 return result.toString();
421 public void setName(String name)
430 * @return DOCUMENT ME!
432 public String getName()
443 public void setStart(int start)
451 * @return DOCUMENT ME!
453 public int getStart()
464 public void setEnd(int end)
472 * @return DOCUMENT ME!
482 * @return DOCUMENT ME!
484 public int getLength()
486 return this.sequence.length;
495 public void setSequence(String seq)
497 this.sequence = seq.toCharArray();
501 public String getSequenceAsString()
503 return new String(sequence);
506 public String getSequenceAsString(int start, int end)
508 return new String(getSequence(start, end));
511 public char[] getSequence()
519 * @see jalview.datamodel.SequenceI#getSequence(int, int)
521 public char[] getSequence(int start, int end)
527 // JBPNote - left to user to pad the result here (TODO:Decide on this
529 if (start >= sequence.length)
534 if (end >= sequence.length)
536 end = sequence.length;
539 char[] reply = new char[end - start];
540 System.arraycopy(sequence, start, reply, 0, end - start);
546 public SequenceI getSubSequence(int start, int end)
552 char[] seq = getSequence(start, end);
557 int nstart = findPosition(start);
558 int nend = findPosition(end) - 1;
559 // JBPNote - this is an incomplete copy.
560 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
561 nseq.setDescription(description);
562 if (datasetSequence != null)
564 nseq.setDatasetSequence(datasetSequence);
568 nseq.setDatasetSequence(this);
579 * @return DOCUMENT ME!
581 public char getCharAt(int i)
583 if (i < sequence.length)
599 public void setDescription(String desc)
601 this.description = desc;
607 * @return DOCUMENT ME!
609 public String getDescription()
611 return this.description;
617 * @see jalview.datamodel.SequenceI#findIndex(int)
619 public int findIndex(int pos)
621 // returns the alignment position for a residue
624 // Rely on end being at least as long as the length of the sequence.
625 while ((i < sequence.length) && (j <= end) && (j <= pos))
627 if (!jalview.util.Comparison.isGap(sequence[i]))
635 if ((j == end) && (j < pos))
646 public int findPosition(int i)
650 int seqlen = sequence.length;
651 while ((j < i) && (j < seqlen))
653 if (!jalview.util.Comparison.isGap(sequence[j]))
665 * Returns an int array where indices correspond to each residue in the
666 * sequence and the element value gives its position in the alignment
668 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
669 * residues in SequenceI object
671 public int[] gapMap()
673 String seq = jalview.analysis.AlignSeq.extractGaps(
674 jalview.util.Comparison.GapChars, new String(sequence));
675 int[] map = new int[seq.length()];
679 while (j < sequence.length)
681 if (!jalview.util.Comparison.isGap(sequence[j]))
693 public int[] findPositionMap()
695 int map[] = new int[sequence.length];
698 int seqlen = sequence.length;
702 if (!jalview.util.Comparison.isGap(sequence[j]))
713 public List<int[]> getInsertions()
715 ArrayList<int[]> map = new ArrayList<int[]>();
716 int lastj = -1, j = 0;
718 int seqlen = sequence.length;
721 if (jalview.util.Comparison.isGap(sequence[j]))
749 public void deleteChars(int i, int j)
751 int newstart = start, newend = end;
752 if (i >= sequence.length)
757 char[] tmp = StringUtils.deleteChars(sequence, i, j);
758 boolean createNewDs = false;
759 // TODO: take a (second look) at the dataset creation validation method for
760 // the very large sequence case
761 int eindex = -1, sindex = -1;
762 boolean ecalc = false, scalc = false;
763 for (int s = i; s < j; s++)
765 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
775 sindex = findIndex(start) - 1;
780 // delete characters including start of sequence
781 newstart = findPosition(j);
782 break; // don't need to search for any more residue characters.
786 // delete characters after start.
789 eindex = findIndex(end) - 1;
794 // delete characters at end of sequence
795 newend = findPosition(i - 1);
796 break; // don't need to search for any more residue characters.
801 newend--; // decrease end position by one for the deleted residue
802 // and search further
808 // deletion occured in the middle of the sequence
809 if (createNewDs && this.datasetSequence != null)
811 // construct a new sequence
812 Sequence ds = new Sequence(datasetSequence);
813 // TODO: remove any non-inheritable properties ?
814 // TODO: create a sequence mapping (since there is a relation here ?)
815 ds.deleteChars(i, j);
816 datasetSequence = ds;
824 public void insertCharAt(int i, int length, char c)
826 char[] tmp = new char[sequence.length + length];
828 if (i >= sequence.length)
830 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
835 System.arraycopy(sequence, 0, tmp, 0, i);
845 if (i < sequence.length)
847 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
854 public void insertCharAt(int i, char c)
856 insertCharAt(i, 1, c);
860 public String getVamsasId()
866 public void setVamsasId(String id)
872 public void setDBRef(DBRefEntry[] dbref)
878 public DBRefEntry[] getDBRef()
880 if (dbrefs == null && datasetSequence != null
881 && this != datasetSequence)
883 return datasetSequence.getDBRef();
889 public void addDBRef(DBRefEntry entry)
893 dbrefs = new DBRefEntry[0];
896 int i, iSize = dbrefs.length;
898 for (i = 0; i < iSize; i++)
900 if (dbrefs[i].equalRef(entry))
902 if (entry.getMap() != null)
904 if (dbrefs[i].getMap() == null)
906 // overwrite with 'superior' entry that contains a mapping.
914 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
915 System.arraycopy(dbrefs, 0, temp, 0, iSize);
916 temp[temp.length - 1] = entry;
922 public void setDatasetSequence(SequenceI seq)
924 datasetSequence = seq;
928 public SequenceI getDatasetSequence()
930 return datasetSequence;
934 public AlignmentAnnotation[] getAnnotation()
936 return annotation == null ? null : annotation
937 .toArray(new AlignmentAnnotation[annotation.size()]);
942 public boolean hasAnnotation(AlignmentAnnotation ann)
944 return annotation == null ? false : annotation.contains(ann);
948 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
950 if (this.annotation == null)
952 this.annotation = new Vector<AlignmentAnnotation>();
954 if (!this.annotation.contains(annotation))
956 this.annotation.addElement(annotation);
958 annotation.setSequenceRef(this);
961 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
963 if (this.annotation != null)
965 this.annotation.removeElement(annotation);
966 if (this.annotation.size() == 0)
968 this.annotation = null;
974 * test if this is a valid candidate for another sequence's dataset sequence.
977 private boolean isValidDatasetSequence()
979 if (datasetSequence != null)
983 for (int i = 0; i < sequence.length; i++)
985 if (jalview.util.Comparison.isGap(sequence[i]))
994 public SequenceI deriveSequence()
996 SequenceI seq = new Sequence(this);
997 if (datasetSequence != null)
999 // duplicate current sequence with same dataset
1000 seq.setDatasetSequence(datasetSequence);
1004 if (isValidDatasetSequence())
1006 // Use this as dataset sequence
1007 seq.setDatasetSequence(this);
1011 // Create a new, valid dataset sequence
1013 ds.setSequence(AlignSeq.extractGaps(
1014 jalview.util.Comparison.GapChars, new String(sequence)));
1015 setDatasetSequence(ds);
1016 ds.setSequenceFeatures(getSequenceFeatures());
1017 seq = this; // and return this sequence as the derived sequence.
1026 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1028 public SequenceI createDatasetSequence()
1030 if (datasetSequence == null)
1032 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1033 jalview.util.Comparison.GapChars, getSequenceAsString()),
1034 getStart(), getEnd());
1035 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1036 datasetSequence.setDescription(getDescription());
1037 setSequenceFeatures(null);
1038 // move database references onto dataset sequence
1039 datasetSequence.setDBRef(getDBRef());
1041 datasetSequence.setPDBId(getPDBId());
1043 datasetSequence.updatePDBIds();
1044 if (annotation != null)
1046 for (AlignmentAnnotation aa : annotation)
1048 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1049 _aa.sequenceRef = datasetSequence;
1050 _aa.adjustForAlignment(); // uses annotation's own record of
1051 // sequence-column mapping
1052 datasetSequence.addAlignmentAnnotation(_aa);
1056 return datasetSequence;
1063 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1066 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1068 if (annotation != null)
1070 annotation.removeAllElements();
1072 if (annotations != null)
1074 for (int i = 0; i < annotations.length; i++)
1076 if (annotations[i] != null)
1078 addAlignmentAnnotation(annotations[i]);
1085 public AlignmentAnnotation[] getAnnotation(String label)
1087 if (annotation == null || annotation.size() == 0)
1092 Vector subset = new Vector();
1093 Enumeration e = annotation.elements();
1094 while (e.hasMoreElements())
1096 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1097 if (ann.label != null && ann.label.equals(label))
1099 subset.addElement(ann);
1102 if (subset.size() == 0)
1106 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1108 e = subset.elements();
1109 while (e.hasMoreElements())
1111 anns[i++] = (AlignmentAnnotation) e.nextElement();
1113 subset.removeAllElements();
1118 public boolean updatePDBIds()
1120 if (datasetSequence != null)
1122 // TODO: could merge DBRefs
1123 return datasetSequence.updatePDBIds();
1125 if (dbrefs == null || dbrefs.length == 0)
1129 Vector newpdb = new Vector();
1130 for (int i = 0; i < dbrefs.length; i++)
1132 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1134 PDBEntry pdbe = new PDBEntry();
1135 pdbe.setId(dbrefs[i].getAccessionId());
1136 if (pdbIds == null || pdbIds.size() == 0)
1138 newpdb.addElement(pdbe);
1142 Enumeration en = pdbIds.elements();
1143 boolean matched = false;
1144 while (!matched && en.hasMoreElements())
1146 PDBEntry anentry = (PDBEntry) en.nextElement();
1147 if (anentry.getId().equals(pdbe.getId()))
1154 newpdb.addElement(pdbe);
1159 if (newpdb.size() > 0)
1161 Enumeration en = newpdb.elements();
1162 while (en.hasMoreElements())
1164 addPDBId((PDBEntry) en.nextElement());
1172 public void transferAnnotation(SequenceI entry, Mapping mp)
1174 if (datasetSequence != null)
1176 datasetSequence.transferAnnotation(entry, mp);
1179 if (entry.getDatasetSequence() != null)
1181 transferAnnotation(entry.getDatasetSequence(), mp);
1184 // transfer any new features from entry onto sequence
1185 if (entry.getSequenceFeatures() != null)
1188 SequenceFeature[] sfs = entry.getSequenceFeatures();
1189 for (int si = 0; si < sfs.length; si++)
1191 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1192 : new SequenceFeature[]
1193 { new SequenceFeature(sfs[si]) };
1194 if (sf != null && sf.length > 0)
1196 for (int sfi = 0; sfi < sf.length; sfi++)
1198 addSequenceFeature(sf[sfi]);
1204 // transfer PDB entries
1205 if (entry.getPDBId() != null)
1207 Enumeration e = entry.getPDBId().elements();
1208 while (e.hasMoreElements())
1210 PDBEntry pdb = (PDBEntry) e.nextElement();
1214 // transfer database references
1215 DBRefEntry[] entryRefs = entry.getDBRef();
1216 if (entryRefs != null)
1218 for (int r = 0; r < entryRefs.length; r++)
1220 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1221 if (newref.getMap() != null && mp != null)
1223 // remap ref using our local mapping
1225 // we also assume all version string setting is done by dbSourceProxy
1227 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1228 * newref.setSource(dbSource); }
1236 * @return The index (zero-based) on this sequence in the MSA. It returns
1237 * {@code -1} if this information is not available.
1239 public int getIndex()
1245 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1246 * if this information is undefined.
1249 * position for this sequence. This value is zero-based (zero for
1250 * this first sequence)
1252 public void setIndex(int value)
1257 public void setRNA(RNA r)
1268 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1271 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1272 if (this.annotation != null)
1274 for (AlignmentAnnotation ann : annotation)
1276 if (ann.calcId != null && ann.calcId.equals(calcId)
1277 && ann.label != null && ann.label.equals(label))