2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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 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;
34 * Implements the SequenceI interface for a char[] based sequence object.
39 public class Sequence implements SequenceI
41 SequenceI datasetSequence;
45 private char[] sequence;
62 * This annotation is displayed below the alignment but the positions are tied
63 * to the residues of this sequence
65 * TODO: change to List<>
67 Vector<AlignmentAnnotation> annotation;
70 * The index of the sequence in a MSA
74 /** array of sequence features - may not be null for a valid sequence object */
75 public SequenceFeature[] sequenceFeatures;
78 * Creates a new Sequence object.
83 * string to form a possibly gapped sequence out of
85 * first position of non-gap residue in the sequence
87 * last position of ungapped residues (nearly always only used for
90 public Sequence(String name, String sequence, int start, int end)
93 this.sequence = sequence.toCharArray();
100 public Sequence(String name, char[] sequence, int start, int end)
103 this.sequence = sequence;
110 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
111 "[/][0-9]{1,}[-][0-9]{1,}$");
113 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
120 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
123 // Does sequence have the /start-end signiature?
124 if (limitrx.search(name))
126 name = limitrx.left();
127 endrx.search(limitrx.stringMatched());
128 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
129 endrx.matchedFrom() - 1)));
130 setEnd(Integer.parseInt(endrx.stringMatched()));
134 void checkValidRange()
137 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
140 for (int j = 0; j < sequence.length; j++)
142 if (!jalview.util.Comparison.isGap(sequence[j]))
161 * Creates a new Sequence object.
168 public Sequence(String name, String sequence)
170 this(name, sequence, 1, -1);
174 * Creates a new Sequence object with new features, DBRefEntries,
175 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
181 public Sequence(SequenceI seq)
183 this(seq, seq.getAnnotation());
187 * Create a new sequence object with new features, DBRefEntries, and PDBIds
188 * but inherits any existing dataset sequence reference, and duplicate of any
189 * annotation that is present in the given annotation array.
192 * the sequence to be copied
193 * @param alAnnotation
194 * an array of annotation including some associated with seq
196 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
198 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
199 description = seq.getDescription();
200 if (seq.getSequenceFeatures() != null)
202 SequenceFeature[] sf = seq.getSequenceFeatures();
203 for (int i = 0; i < sf.length; i++)
205 addSequenceFeature(new SequenceFeature(sf[i]));
208 setDatasetSequence(seq.getDatasetSequence());
209 if (datasetSequence == null && seq.getDBRef() != null)
211 // only copy DBRefs if we really are a dataset sequence
212 DBRefEntry[] dbr = seq.getDBRef();
213 for (int i = 0; i < dbr.length; i++)
215 addDBRef(new DBRefEntry(dbr[i]));
218 if (seq.getAnnotation() != null)
220 AlignmentAnnotation[] sqann = seq.getAnnotation();
221 for (int i = 0; i < sqann.length; i++)
223 if (sqann[i] == null)
227 boolean found = (alAnnotation == null);
230 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
232 found = (alAnnotation[apos] == sqann[i]);
237 // only copy the given annotation
238 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
239 addAlignmentAnnotation(newann);
243 if (seq.getPDBId() != null)
245 Vector ids = seq.getPDBId();
246 Enumeration e = ids.elements();
247 while (e.hasMoreElements())
249 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
260 public void setSequenceFeatures(SequenceFeature[] features)
262 sequenceFeatures = features;
265 public synchronized void addSequenceFeature(SequenceFeature sf)
267 if (sequenceFeatures == null)
269 sequenceFeatures = new SequenceFeature[0];
272 for (int i = 0; i < sequenceFeatures.length; i++)
274 if (sequenceFeatures[i].equals(sf))
280 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
281 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
282 temp[sequenceFeatures.length] = sf;
284 sequenceFeatures = temp;
287 public void deleteFeature(SequenceFeature sf)
289 if (sequenceFeatures == null)
295 for (index = 0; index < sequenceFeatures.length; index++)
297 if (sequenceFeatures[index].equals(sf))
303 if (index == sequenceFeatures.length)
308 int sfLength = sequenceFeatures.length;
311 sequenceFeatures = null;
315 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
316 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
318 if (index < sfLength)
320 System.arraycopy(sequenceFeatures, index + 1, temp, index,
321 sequenceFeatures.length - index - 1);
324 sequenceFeatures = temp;
331 * @return DOCUMENT ME!
333 public SequenceFeature[] getSequenceFeatures()
335 return sequenceFeatures;
338 public void addPDBId(PDBEntry entry)
342 pdbIds = new Vector<PDBEntry>();
344 if (!pdbIds.contains(entry))
346 pdbIds.addElement(entry);
356 public void setPDBId(Vector id)
364 * @return DOCUMENT ME!
366 public Vector<PDBEntry> getPDBId()
374 * @return DOCUMENT ME!
376 public String getDisplayId(boolean jvsuffix)
378 StringBuffer result = new StringBuffer(name);
381 result.append("/" + start + "-" + end);
384 return result.toString();
393 public void setName(String name)
402 * @return DOCUMENT ME!
404 public String getName()
415 public void setStart(int start)
423 * @return DOCUMENT ME!
425 public int getStart()
436 public void setEnd(int end)
444 * @return DOCUMENT ME!
454 * @return DOCUMENT ME!
456 public int getLength()
458 return this.sequence.length;
467 public void setSequence(String seq)
469 this.sequence = seq.toCharArray();
473 public String getSequenceAsString()
475 return new String(sequence);
478 public String getSequenceAsString(int start, int end)
480 return new String(getSequence(start, end));
483 public char[] getSequence()
491 * @see jalview.datamodel.SequenceI#getSequence(int, int)
493 public char[] getSequence(int start, int end)
499 // JBPNote - left to user to pad the result here (TODO:Decide on this
501 if (start >= sequence.length)
506 if (end >= sequence.length)
508 end = sequence.length;
511 char[] reply = new char[end - start];
512 System.arraycopy(sequence, start, reply, 0, end - start);
518 public SequenceI getSubSequence(int start, int end)
524 char[] seq = getSequence(start, end);
529 int nstart = findPosition(start);
530 int nend = findPosition(end) - 1;
531 // JBPNote - this is an incomplete copy.
532 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
533 nseq.setDescription(description);
534 if (datasetSequence != null)
536 nseq.setDatasetSequence(datasetSequence);
540 nseq.setDatasetSequence(this);
551 * @return DOCUMENT ME!
553 public char getCharAt(int i)
555 if (i < sequence.length)
571 public void setDescription(String desc)
573 this.description = desc;
579 * @return DOCUMENT ME!
581 public String getDescription()
583 return this.description;
589 * @see jalview.datamodel.SequenceI#findIndex(int)
591 public int findIndex(int pos)
593 // returns the alignment position for a residue
596 // Rely on end being at least as long as the length of the sequence.
597 while ((i < sequence.length) && (j <= end) && (j <= pos))
599 if (!jalview.util.Comparison.isGap(sequence[i]))
607 if ((j == end) && (j < pos))
618 public int findPosition(int i)
622 int seqlen = sequence.length;
623 while ((j < i) && (j < seqlen))
625 if (!jalview.util.Comparison.isGap(sequence[j]))
637 * Returns an int array where indices correspond to each residue in the
638 * sequence and the element value gives its position in the alignment
640 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
641 * residues in SequenceI object
643 public int[] gapMap()
645 String seq = jalview.analysis.AlignSeq.extractGaps(
646 jalview.util.Comparison.GapChars, new String(sequence));
647 int[] map = new int[seq.length()];
651 while (j < sequence.length)
653 if (!jalview.util.Comparison.isGap(sequence[j]))
665 public int[] findPositionMap()
667 int map[] = new int[sequence.length];
670 int seqlen = sequence.length;
674 if (!jalview.util.Comparison.isGap(sequence[j]))
687 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
690 public void deleteChars(int i, int j)
692 int newstart = start, newend = end;
693 if (i >= sequence.length)
700 if (j >= sequence.length)
703 System.arraycopy(sequence, 0, tmp, 0, i);
708 tmp = new char[sequence.length - j + i];
709 System.arraycopy(sequence, 0, tmp, 0, i);
710 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
712 boolean createNewDs = false;
713 // TODO: take a look at the new dataset creation validation method below -
714 // this could become time comsuming for large sequences - consider making it
716 for (int s = i; s < j; s++)
718 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
726 int sindex = findIndex(start) - 1;
729 // delete characters including start of sequence
730 newstart = findPosition(j);
731 break; // don't need to search for any more residue characters.
735 // delete characters after start.
736 int eindex = findIndex(end) - 1;
739 // delete characters at end of sequence
740 newend = findPosition(i - 1);
741 break; // don't need to search for any more residue characters.
746 newend--; // decrease end position by one for the deleted residue
747 // and search further
753 // deletion occured in the middle of the sequence
754 if (createNewDs && this.datasetSequence != null)
756 // construct a new sequence
757 Sequence ds = new Sequence(datasetSequence);
758 // TODO: remove any non-inheritable properties ?
759 // TODO: create a sequence mapping (since there is a relation here ?)
760 ds.deleteChars(i, j);
761 datasetSequence = ds;
769 public void insertCharAt(int i, int length, char c)
771 char[] tmp = new char[sequence.length + length];
773 if (i >= sequence.length)
775 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
780 System.arraycopy(sequence, 0, tmp, 0, i);
790 if (i < sequence.length)
792 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
799 public void insertCharAt(int i, char c)
801 insertCharAt(i, 1, c);
805 public String getVamsasId()
811 public void setVamsasId(String id)
817 public void setDBRef(DBRefEntry[] dbref)
823 public DBRefEntry[] getDBRef()
825 if (dbrefs == null && datasetSequence != null
826 && this != datasetSequence)
828 return datasetSequence.getDBRef();
834 public void addDBRef(DBRefEntry entry)
838 dbrefs = new DBRefEntry[0];
841 int i, iSize = dbrefs.length;
843 for (i = 0; i < iSize; i++)
845 if (dbrefs[i].equalRef(entry))
847 if (entry.getMap() != null)
849 if (dbrefs[i].getMap() == null)
851 // overwrite with 'superior' entry that contains a mapping.
859 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
860 System.arraycopy(dbrefs, 0, temp, 0, iSize);
861 temp[temp.length - 1] = entry;
867 public void setDatasetSequence(SequenceI seq)
869 datasetSequence = seq;
873 public SequenceI getDatasetSequence()
875 return datasetSequence;
879 * Returns a new array containing this sequence's annotations, or null.
882 public AlignmentAnnotation[] getAnnotation()
884 return annotation == null ? null : annotation
885 .toArray(new AlignmentAnnotation[annotation.size()]);
889 * Returns true if this sequence has the given annotation (by object
893 public boolean hasAnnotation(AlignmentAnnotation ann)
895 return annotation == null ? false : annotation.contains(ann);
899 * Add the given annotation, if not already added, and set its sequence ref to
900 * be this sequence. Does nothing if this sequence's annotations already
901 * include this annotation (by identical object reference).
904 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
906 if (this.annotation == null)
908 this.annotation = new Vector();
910 if (!this.annotation.contains(annotation))
912 this.annotation.addElement(annotation);
914 annotation.setSequenceRef(this);
917 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
919 if (this.annotation != null)
921 this.annotation.removeElement(annotation);
922 if (this.annotation.size() == 0)
924 this.annotation = null;
930 * test if this is a valid candidate for another sequence's dataset sequence.
933 private boolean isValidDatasetSequence()
935 if (datasetSequence != null)
939 for (int i = 0; i < sequence.length; i++)
941 if (jalview.util.Comparison.isGap(sequence[i]))
952 * @see jalview.datamodel.SequenceI#deriveSequence()
955 public SequenceI deriveSequence()
957 SequenceI seq = new Sequence(this);
958 if (datasetSequence != null)
960 // duplicate current sequence with same dataset
961 seq.setDatasetSequence(datasetSequence);
965 if (isValidDatasetSequence())
967 // Use this as dataset sequence
968 seq.setDatasetSequence(this);
972 // Create a new, valid dataset sequence
974 ds.setSequence(AlignSeq.extractGaps(
975 jalview.util.Comparison.GapChars, new String(sequence)));
976 setDatasetSequence(ds);
977 ds.setSequenceFeatures(getSequenceFeatures());
978 seq = this; // and return this sequence as the derived sequence.
987 * @see jalview.datamodel.SequenceI#createDatasetSequence()
990 public SequenceI createDatasetSequence()
992 if (datasetSequence == null)
994 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
995 jalview.util.Comparison.GapChars, getSequenceAsString()),
996 getStart(), getEnd());
997 datasetSequence.setSequenceFeatures(getSequenceFeatures());
998 datasetSequence.setDescription(getDescription());
999 setSequenceFeatures(null);
1000 // move database references onto dataset sequence
1001 datasetSequence.setDBRef(getDBRef());
1003 datasetSequence.setPDBId(getPDBId());
1005 datasetSequence.updatePDBIds();
1006 if (annotation != null)
1008 for (AlignmentAnnotation aa : annotation)
1010 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1011 _aa.sequenceRef = datasetSequence;
1012 _aa.adjustForAlignment(); // uses annotation's own record of
1013 // sequence-column mapping
1014 datasetSequence.addAlignmentAnnotation(_aa);
1018 return datasetSequence;
1025 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1028 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1030 if (annotation != null)
1032 annotation.removeAllElements();
1034 if (annotations != null)
1036 for (int i = 0; i < annotations.length; i++)
1038 if (annotations[i] != null)
1040 addAlignmentAnnotation(annotations[i]);
1049 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1052 public AlignmentAnnotation[] getAnnotation(String label)
1054 if (annotation == null || annotation.size() == 0)
1059 Vector subset = new Vector();
1060 Enumeration e = annotation.elements();
1061 while (e.hasMoreElements())
1063 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1064 if (ann.label != null && ann.label.equals(label))
1066 subset.addElement(ann);
1069 if (subset.size() == 0)
1073 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1075 e = subset.elements();
1076 while (e.hasMoreElements())
1078 anns[i++] = (AlignmentAnnotation) e.nextElement();
1080 subset.removeAllElements();
1085 public boolean updatePDBIds()
1087 if (datasetSequence != null)
1089 // TODO: could merge DBRefs
1090 return datasetSequence.updatePDBIds();
1092 if (dbrefs == null || dbrefs.length == 0)
1096 Vector newpdb = new Vector();
1097 for (int i = 0; i < dbrefs.length; i++)
1099 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1101 PDBEntry pdbe = new PDBEntry();
1102 pdbe.setId(dbrefs[i].getAccessionId());
1103 if (pdbIds == null || pdbIds.size() == 0)
1105 newpdb.addElement(pdbe);
1109 Enumeration en = pdbIds.elements();
1110 boolean matched = false;
1111 while (!matched && en.hasMoreElements())
1113 PDBEntry anentry = (PDBEntry) en.nextElement();
1114 if (anentry.getId().equals(pdbe.getId()))
1121 newpdb.addElement(pdbe);
1126 if (newpdb.size() > 0)
1128 Enumeration en = newpdb.elements();
1129 while (en.hasMoreElements())
1131 addPDBId((PDBEntry) en.nextElement());
1142 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1143 * jalview.datamodel.Mapping)
1146 public void transferAnnotation(SequenceI entry, Mapping mp)
1148 if (datasetSequence != null)
1150 datasetSequence.transferAnnotation(entry, mp);
1153 if (entry.getDatasetSequence() != null)
1155 transferAnnotation(entry.getDatasetSequence(), mp);
1158 // transfer any new features from entry onto sequence
1159 if (entry.getSequenceFeatures() != null)
1162 SequenceFeature[] sfs = entry.getSequenceFeatures();
1163 for (int si = 0; si < sfs.length; si++)
1165 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1166 : new SequenceFeature[]
1167 { new SequenceFeature(sfs[si]) };
1168 if (sf != null && sf.length > 0)
1170 for (int sfi = 0; sfi < sf.length; sfi++)
1172 addSequenceFeature(sf[sfi]);
1178 // transfer PDB entries
1179 if (entry.getPDBId() != null)
1181 Enumeration e = entry.getPDBId().elements();
1182 while (e.hasMoreElements())
1184 PDBEntry pdb = (PDBEntry) e.nextElement();
1188 // transfer database references
1189 DBRefEntry[] entryRefs = entry.getDBRef();
1190 if (entryRefs != null)
1192 for (int r = 0; r < entryRefs.length; r++)
1194 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1195 if (newref.getMap() != null && mp != null)
1197 // remap ref using our local mapping
1199 // we also assume all version string setting is done by dbSourceProxy
1201 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1202 * newref.setSource(dbSource); }
1210 * @return The index (zero-based) on this sequence in the MSA. It returns
1211 * {@code -1} if this information is not available.
1213 public int getIndex()
1219 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1220 * if this information is undefined.
1223 * position for this sequence. This value is zero-based (zero for
1224 * this first sequence)
1226 public void setIndex(int value)
1231 public void setRNA(RNA r)
1242 * Returns a (possibly empty) list of any annotations that match on given
1243 * calcId (source) and label (type). Null values do not match.
1249 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1252 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1253 if (this.annotation != null) {
1254 for (AlignmentAnnotation ann : annotation) {
1255 if (ann.calcId != null && ann.calcId.equals(calcId)
1256 && ann.label != null && ann.label.equals(label))