2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
332 * @return DOCUMENT ME!
334 public SequenceFeature[] getSequenceFeatures()
336 return sequenceFeatures;
339 public void addPDBId(PDBEntry entry)
343 pdbIds = new Vector();
345 if (!pdbIds.contains(entry))
347 pdbIds.addElement(entry);
357 public void setPDBId(Vector id)
365 * @return DOCUMENT ME!
367 public Vector getPDBId()
375 * @return DOCUMENT ME!
377 public String getDisplayId(boolean jvsuffix)
379 StringBuffer result = new StringBuffer(name);
382 result.append("/" + start + "-" + end);
385 return result.toString();
394 public void setName(String name)
403 * @return DOCUMENT ME!
405 public String getName()
416 public void setStart(int start)
424 * @return DOCUMENT ME!
426 public int getStart()
437 public void setEnd(int end)
445 * @return DOCUMENT ME!
455 * @return DOCUMENT ME!
457 public int getLength()
459 return this.sequence.length;
468 public void setSequence(String seq)
470 this.sequence = seq.toCharArray();
474 public String getSequenceAsString()
476 return new String(sequence);
479 public String getSequenceAsString(int start, int end)
481 return new String(getSequence(start, end));
484 public char[] getSequence()
492 * @see jalview.datamodel.SequenceI#getSequence(int, int)
494 public char[] getSequence(int start, int end)
500 // JBPNote - left to user to pad the result here (TODO:Decide on this
502 if (start >= sequence.length)
507 if (end >= sequence.length)
509 end = sequence.length;
512 char[] reply = new char[end - start];
513 System.arraycopy(sequence, start, reply, 0, end - start);
519 public SequenceI getSubSequence(int start, int end)
525 char[] seq = getSequence(start, end);
530 int nstart = findPosition(start);
531 int nend = findPosition(end) - 1;
532 // JBPNote - this is an incomplete copy.
533 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
534 nseq.setDescription(description);
535 if (datasetSequence != null)
537 nseq.setDatasetSequence(datasetSequence);
541 nseq.setDatasetSequence(this);
552 * @return DOCUMENT ME!
554 public char getCharAt(int i)
556 if (i < sequence.length)
572 public void setDescription(String desc)
574 this.description = desc;
580 * @return DOCUMENT ME!
582 public String getDescription()
584 return this.description;
590 * @see jalview.datamodel.SequenceI#findIndex(int)
592 public int findIndex(int pos)
594 // returns the alignment position for a residue
597 // Rely on end being at least as long as the length of the sequence.
598 while ((i < sequence.length) && (j <= end) && (j <= pos))
600 if (!jalview.util.Comparison.isGap(sequence[i]))
608 if ((j == end) && (j < pos))
619 public int findPosition(int i)
623 int seqlen = sequence.length;
624 while ((j < i) && (j < seqlen))
626 if (!jalview.util.Comparison.isGap(sequence[j]))
638 * Returns an int array where indices correspond to each residue in the
639 * sequence and the element value gives its position in the alignment
641 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
642 * residues in SequenceI object
644 public int[] gapMap()
646 String seq = jalview.analysis.AlignSeq.extractGaps(
647 jalview.util.Comparison.GapChars, new String(sequence));
648 int[] map = new int[seq.length()];
652 while (j < sequence.length)
654 if (!jalview.util.Comparison.isGap(sequence[j]))
666 public int[] findPositionMap()
668 int map[] = new int[sequence.length];
671 int seqlen = sequence.length;
675 if (!jalview.util.Comparison.isGap(sequence[j]))
686 public List<int[]> getInsertions()
688 ArrayList<int[]> map = new ArrayList<int[]>();
689 int lastj = -1, j = 0;
691 int seqlen = sequence.length;
694 if (jalview.util.Comparison.isGap(sequence[j]))
722 public void deleteChars(int i, int j)
724 int newstart = start, newend = end;
725 if (i >= sequence.length)
730 char[] tmp = StringUtils.deleteChars(sequence, i, j);
731 boolean createNewDs = false;
732 // TODO: take a look at the new dataset creation validation method below -
733 // this could become time comsuming for large sequences - consider making it
735 for (int s = i; s < j; s++)
737 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
745 int sindex = findIndex(start) - 1;
748 // delete characters including start of sequence
749 newstart = findPosition(j);
750 break; // don't need to search for any more residue characters.
754 // delete characters after start.
755 int eindex = findIndex(end) - 1;
758 // delete characters at end of sequence
759 newend = findPosition(i - 1);
760 break; // don't need to search for any more residue characters.
765 newend--; // decrease end position by one for the deleted residue
766 // and search further
772 // deletion occured in the middle of the sequence
773 if (createNewDs && this.datasetSequence != null)
775 // construct a new sequence
776 Sequence ds = new Sequence(datasetSequence);
777 // TODO: remove any non-inheritable properties ?
778 // TODO: create a sequence mapping (since there is a relation here ?)
779 ds.deleteChars(i, j);
780 datasetSequence = ds;
788 public void insertCharAt(int i, int length, char c)
790 char[] tmp = new char[sequence.length + length];
792 if (i >= sequence.length)
794 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
799 System.arraycopy(sequence, 0, tmp, 0, i);
809 if (i < sequence.length)
811 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
818 public void insertCharAt(int i, char c)
820 insertCharAt(i, 1, c);
824 public String getVamsasId()
830 public void setVamsasId(String id)
836 public void setDBRef(DBRefEntry[] dbref)
842 public DBRefEntry[] getDBRef()
844 if (dbrefs == null && datasetSequence != null
845 && this != datasetSequence)
847 return datasetSequence.getDBRef();
853 public void addDBRef(DBRefEntry entry)
857 dbrefs = new DBRefEntry[0];
860 int i, iSize = dbrefs.length;
862 for (i = 0; i < iSize; i++)
864 if (dbrefs[i].equalRef(entry))
866 if (entry.getMap() != null)
868 if (dbrefs[i].getMap() == null)
870 // overwrite with 'superior' entry that contains a mapping.
878 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
879 System.arraycopy(dbrefs, 0, temp, 0, iSize);
880 temp[temp.length - 1] = entry;
886 public void setDatasetSequence(SequenceI seq)
888 datasetSequence = seq;
892 public SequenceI getDatasetSequence()
894 return datasetSequence;
898 public AlignmentAnnotation[] getAnnotation()
900 return annotation == null ? null : annotation
901 .toArray(new AlignmentAnnotation[annotation.size()]);
906 public boolean hasAnnotation(AlignmentAnnotation ann)
908 return annotation == null ? false : annotation.contains(ann);
912 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
914 if (this.annotation == null)
916 this.annotation = new Vector();
918 if (!this.annotation.contains(annotation))
920 this.annotation.addElement(annotation);
922 annotation.setSequenceRef(this);
925 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
927 if (this.annotation != null)
929 this.annotation.removeElement(annotation);
930 if (this.annotation.size() == 0)
932 this.annotation = null;
938 * test if this is a valid candidate for another sequence's dataset sequence.
941 private boolean isValidDatasetSequence()
943 if (datasetSequence != null)
947 for (int i = 0; i < sequence.length; i++)
949 if (jalview.util.Comparison.isGap(sequence[i]))
958 public SequenceI deriveSequence()
960 SequenceI seq = new Sequence(this);
961 if (datasetSequence != null)
963 // duplicate current sequence with same dataset
964 seq.setDatasetSequence(datasetSequence);
968 if (isValidDatasetSequence())
970 // Use this as dataset sequence
971 seq.setDatasetSequence(this);
975 // Create a new, valid dataset sequence
977 ds.setSequence(AlignSeq.extractGaps(
978 jalview.util.Comparison.GapChars, new String(sequence)));
979 setDatasetSequence(ds);
980 ds.setSequenceFeatures(getSequenceFeatures());
981 seq = this; // and return this sequence as the derived sequence.
990 * @see jalview.datamodel.SequenceI#createDatasetSequence()
992 public SequenceI createDatasetSequence()
994 if (datasetSequence == null)
996 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
997 jalview.util.Comparison.GapChars, getSequenceAsString()),
998 getStart(), getEnd());
999 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1000 datasetSequence.setDescription(getDescription());
1001 setSequenceFeatures(null);
1002 // move database references onto dataset sequence
1003 datasetSequence.setDBRef(getDBRef());
1005 datasetSequence.setPDBId(getPDBId());
1007 datasetSequence.updatePDBIds();
1008 if (annotation != null)
1010 for (AlignmentAnnotation aa : annotation)
1012 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1013 _aa.sequenceRef = datasetSequence;
1014 _aa.adjustForAlignment(); // uses annotation's own record of
1015 // sequence-column mapping
1016 datasetSequence.addAlignmentAnnotation(_aa);
1020 return datasetSequence;
1027 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1030 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1032 if (annotation != null)
1034 annotation.removeAllElements();
1036 if (annotations != null)
1038 for (int i = 0; i < annotations.length; i++)
1040 if (annotations[i] != null)
1042 addAlignmentAnnotation(annotations[i]);
1049 public AlignmentAnnotation[] getAnnotation(String label)
1051 if (annotation == null || annotation.size() == 0)
1056 Vector subset = new Vector();
1057 Enumeration e = annotation.elements();
1058 while (e.hasMoreElements())
1060 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1061 if (ann.label != null && ann.label.equals(label))
1063 subset.addElement(ann);
1066 if (subset.size() == 0)
1070 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1072 e = subset.elements();
1073 while (e.hasMoreElements())
1075 anns[i++] = (AlignmentAnnotation) e.nextElement();
1077 subset.removeAllElements();
1082 public boolean updatePDBIds()
1084 if (datasetSequence != null)
1086 // TODO: could merge DBRefs
1087 return datasetSequence.updatePDBIds();
1089 if (dbrefs == null || dbrefs.length == 0)
1093 Vector newpdb = new Vector();
1094 for (int i = 0; i < dbrefs.length; i++)
1096 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1098 PDBEntry pdbe = new PDBEntry();
1099 pdbe.setId(dbrefs[i].getAccessionId());
1100 if (pdbIds == null || pdbIds.size() == 0)
1102 newpdb.addElement(pdbe);
1106 Enumeration en = pdbIds.elements();
1107 boolean matched = false;
1108 while (!matched && en.hasMoreElements())
1110 PDBEntry anentry = (PDBEntry) en.nextElement();
1111 if (anentry.getId().equals(pdbe.getId()))
1118 newpdb.addElement(pdbe);
1123 if (newpdb.size() > 0)
1125 Enumeration en = newpdb.elements();
1126 while (en.hasMoreElements())
1128 addPDBId((PDBEntry) en.nextElement());
1136 public void transferAnnotation(SequenceI entry, Mapping mp)
1138 if (datasetSequence != null)
1140 datasetSequence.transferAnnotation(entry, mp);
1143 if (entry.getDatasetSequence() != null)
1145 transferAnnotation(entry.getDatasetSequence(), mp);
1148 // transfer any new features from entry onto sequence
1149 if (entry.getSequenceFeatures() != null)
1152 SequenceFeature[] sfs = entry.getSequenceFeatures();
1153 for (int si = 0; si < sfs.length; si++)
1155 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1156 : new SequenceFeature[]
1157 { new SequenceFeature(sfs[si]) };
1158 if (sf != null && sf.length > 0)
1160 for (int sfi = 0; sfi < sf.length; sfi++)
1162 addSequenceFeature(sf[sfi]);
1168 // transfer PDB entries
1169 if (entry.getPDBId() != null)
1171 Enumeration e = entry.getPDBId().elements();
1172 while (e.hasMoreElements())
1174 PDBEntry pdb = (PDBEntry) e.nextElement();
1178 // transfer database references
1179 DBRefEntry[] entryRefs = entry.getDBRef();
1180 if (entryRefs != null)
1182 for (int r = 0; r < entryRefs.length; r++)
1184 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1185 if (newref.getMap() != null && mp != null)
1187 // remap ref using our local mapping
1189 // we also assume all version string setting is done by dbSourceProxy
1191 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1192 * newref.setSource(dbSource); }
1200 * @return The index (zero-based) on this sequence in the MSA. It returns
1201 * {@code -1} if this information is not available.
1203 public int getIndex()
1209 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1210 * if this information is undefined.
1213 * position for this sequence. This value is zero-based (zero for
1214 * this first sequence)
1216 public void setIndex(int value)
1221 public void setRNA(RNA r)
1232 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1235 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1236 if (this.annotation != null)
1238 for (AlignmentAnnotation ann : annotation)
1240 if (ann.calcId != null && ann.calcId.equals(calcId)
1241 && ann.label != null && ann.label.equals(label))