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;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.List;
28 import java.util.Vector;
30 import fr.orsay.lri.varna.models.rna.RNA;
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();
344 if (!pdbIds.contains(entry))
346 pdbIds.addElement(entry);
356 public void setPDBId(Vector id)
364 * @return DOCUMENT ME!
366 public Vector 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 * make a new Sequence object from start to end (including gaps) over this
527 public SequenceI getSubSequence(int start, int end)
533 char[] seq = getSequence(start, end);
538 int nstart = findPosition(start);
539 int nend = findPosition(end) - 1;
540 // JBPNote - this is an incomplete copy.
541 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
542 nseq.setDescription(description);
543 if (datasetSequence != null)
545 nseq.setDatasetSequence(datasetSequence);
549 nseq.setDatasetSequence(this);
560 * @return DOCUMENT ME!
562 public char getCharAt(int i)
564 if (i < sequence.length)
580 public void setDescription(String desc)
582 this.description = desc;
588 * @return DOCUMENT ME!
590 public String getDescription()
592 return this.description;
598 * @see jalview.datamodel.SequenceI#findIndex(int)
600 public int findIndex(int pos)
602 // returns the alignment position for a residue
605 // Rely on end being at least as long as the length of the sequence.
606 while ((i < sequence.length) && (j <= end) && (j <= pos))
608 if (!jalview.util.Comparison.isGap(sequence[i]))
616 if ((j == end) && (j < pos))
627 * Returns the sequence position for an alignment position
630 * column index in alignment (from 1)
632 * @return residue number for residue (left of and) nearest ith column
634 public int findPosition(int i)
638 int seqlen = sequence.length;
639 while ((j < i) && (j < seqlen))
641 if (!jalview.util.Comparison.isGap(sequence[j]))
653 * Returns an int array where indices correspond to each residue in the
654 * sequence and the element value gives its position in the alignment
656 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
657 * residues in SequenceI object
659 public int[] gapMap()
661 String seq = jalview.analysis.AlignSeq.extractGaps(
662 jalview.util.Comparison.GapChars, new String(sequence));
663 int[] map = new int[seq.length()];
667 while (j < sequence.length)
669 if (!jalview.util.Comparison.isGap(sequence[j]))
683 * @see jalview.datamodel.SequenceI#findPositionMap()
685 public int[] findPositionMap()
687 int map[] = new int[sequence.length];
690 int seqlen = sequence.length;
694 if (!jalview.util.Comparison.isGap(sequence[j]))
707 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
709 public void deleteChars(int i, int j)
711 int newstart = start, newend = end;
712 if (i >= sequence.length)
719 if (j >= sequence.length)
722 System.arraycopy(sequence, 0, tmp, 0, i);
727 tmp = new char[sequence.length - j + i];
728 System.arraycopy(sequence, 0, tmp, 0, i);
729 System.arraycopy(sequence, j, tmp, i, sequence.length - 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;
797 public void insertCharAt(int i, int length, char c)
799 char[] tmp = new char[sequence.length + length];
801 if (i >= sequence.length)
803 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
808 System.arraycopy(sequence, 0, tmp, 0, i);
818 if (i < sequence.length)
820 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
826 public void insertCharAt(int i, char c)
828 insertCharAt(i, 1, c);
831 public String getVamsasId()
836 public void setVamsasId(String id)
841 public void setDBRef(DBRefEntry[] dbref)
846 public DBRefEntry[] getDBRef()
848 if (dbrefs == null && datasetSequence != null
849 && this != datasetSequence)
851 return datasetSequence.getDBRef();
856 public void addDBRef(DBRefEntry entry)
860 dbrefs = new DBRefEntry[0];
863 int i, iSize = dbrefs.length;
865 for (i = 0; i < iSize; i++)
867 if (dbrefs[i].equalRef(entry))
869 if (entry.getMap() != null)
871 if (dbrefs[i].getMap() == null)
873 // overwrite with 'superior' entry that contains a mapping.
881 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
882 System.arraycopy(dbrefs, 0, temp, 0, iSize);
883 temp[temp.length - 1] = entry;
888 public void setDatasetSequence(SequenceI seq)
890 datasetSequence = seq;
893 public SequenceI getDatasetSequence()
895 return datasetSequence;
898 public AlignmentAnnotation[] getAnnotation()
900 if (annotation == null)
905 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
906 for (int r = 0; r < ret.length; r++)
908 ret[r] = annotation.elementAt(r);
914 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
916 if (this.annotation == null)
918 this.annotation = new Vector();
920 if (!this.annotation.contains(annotation))
922 this.annotation.addElement(annotation);
924 annotation.setSequenceRef(this);
927 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
929 if (this.annotation != null)
931 this.annotation.removeElement(annotation);
932 if (this.annotation.size() == 0)
934 this.annotation = null;
940 * test if this is a valid candidate for another sequence's dataset sequence.
943 private boolean isValidDatasetSequence()
945 if (datasetSequence != null)
949 for (int i = 0; i < sequence.length; i++)
951 if (jalview.util.Comparison.isGap(sequence[i]))
962 * @see jalview.datamodel.SequenceI#deriveSequence()
964 public SequenceI deriveSequence()
966 SequenceI seq = new Sequence(this);
967 if (datasetSequence != null)
969 // duplicate current sequence with same dataset
970 seq.setDatasetSequence(datasetSequence);
974 if (isValidDatasetSequence())
976 // Use this as dataset sequence
977 seq.setDatasetSequence(this);
981 // Create a new, valid dataset sequence
983 ds.setSequence(AlignSeq.extractGaps(
984 jalview.util.Comparison.GapChars, new String(sequence)));
985 setDatasetSequence(ds);
986 ds.setSequenceFeatures(getSequenceFeatures());
987 seq = this; // and return this sequence as the derived sequence.
996 * @see jalview.datamodel.SequenceI#createDatasetSequence()
998 public SequenceI createDatasetSequence()
1000 if (datasetSequence == null)
1002 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1003 jalview.util.Comparison.GapChars, getSequenceAsString()),
1004 getStart(), getEnd());
1005 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1006 datasetSequence.setDescription(getDescription());
1007 setSequenceFeatures(null);
1008 // move database references onto dataset sequence
1009 datasetSequence.setDBRef(getDBRef());
1011 datasetSequence.setPDBId(getPDBId());
1013 datasetSequence.updatePDBIds();
1014 if (annotation != null)
1016 for (AlignmentAnnotation aa : annotation)
1018 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1019 _aa.sequenceRef = datasetSequence;
1020 _aa.adjustForAlignment(); // uses annotation's own record of
1021 // sequence-column mapping
1022 datasetSequence.addAlignmentAnnotation(_aa);
1026 return datasetSequence;
1033 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1036 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1038 if (annotation != null)
1040 annotation.removeAllElements();
1042 if (annotations != null)
1044 for (int i = 0; i < annotations.length; i++)
1046 if (annotations[i] != null)
1048 addAlignmentAnnotation(annotations[i]);
1057 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1059 public AlignmentAnnotation[] getAnnotation(String label)
1061 if (annotation == null || annotation.size() == 0)
1066 Vector subset = new Vector();
1067 Enumeration e = annotation.elements();
1068 while (e.hasMoreElements())
1070 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1071 if (ann.label != null && ann.label.equals(label))
1073 subset.addElement(ann);
1076 if (subset.size() == 0)
1080 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1082 e = subset.elements();
1083 while (e.hasMoreElements())
1085 anns[i++] = (AlignmentAnnotation) e.nextElement();
1087 subset.removeAllElements();
1091 public boolean updatePDBIds()
1093 if (datasetSequence != null)
1095 // TODO: could merge DBRefs
1096 return datasetSequence.updatePDBIds();
1098 if (dbrefs == null || dbrefs.length == 0)
1102 Vector newpdb = new Vector();
1103 for (int i = 0; i < dbrefs.length; i++)
1105 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1107 PDBEntry pdbe = new PDBEntry();
1108 pdbe.setId(dbrefs[i].getAccessionId());
1109 if (pdbIds == null || pdbIds.size() == 0)
1111 newpdb.addElement(pdbe);
1115 Enumeration en = pdbIds.elements();
1116 boolean matched = false;
1117 while (!matched && en.hasMoreElements())
1119 PDBEntry anentry = (PDBEntry) en.nextElement();
1120 if (anentry.getId().equals(pdbe.getId()))
1127 newpdb.addElement(pdbe);
1132 if (newpdb.size() > 0)
1134 Enumeration en = newpdb.elements();
1135 while (en.hasMoreElements())
1137 addPDBId((PDBEntry) en.nextElement());
1148 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1149 * jalview.datamodel.Mapping)
1151 public void transferAnnotation(SequenceI entry, Mapping mp)
1153 if (datasetSequence != null)
1155 datasetSequence.transferAnnotation(entry, mp);
1158 if (entry.getDatasetSequence() != null)
1160 transferAnnotation(entry.getDatasetSequence(), mp);
1163 // transfer any new features from entry onto sequence
1164 if (entry.getSequenceFeatures() != null)
1167 SequenceFeature[] sfs = entry.getSequenceFeatures();
1168 for (int si = 0; si < sfs.length; si++)
1170 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1171 : new SequenceFeature[]
1172 { new SequenceFeature(sfs[si]) };
1173 if (sf != null && sf.length > 0)
1175 for (int sfi = 0; sfi < sf.length; sfi++)
1177 addSequenceFeature(sf[sfi]);
1183 // transfer PDB entries
1184 if (entry.getPDBId() != null)
1186 Enumeration e = entry.getPDBId().elements();
1187 while (e.hasMoreElements())
1189 PDBEntry pdb = (PDBEntry) e.nextElement();
1193 // transfer database references
1194 DBRefEntry[] entryRefs = entry.getDBRef();
1195 if (entryRefs != null)
1197 for (int r = 0; r < entryRefs.length; r++)
1199 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1200 if (newref.getMap() != null && mp != null)
1202 // remap ref using our local mapping
1204 // we also assume all version string setting is done by dbSourceProxy
1206 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1207 * newref.setSource(dbSource); }
1215 * @return The index (zero-based) on this sequence in the MSA. It returns
1216 * {@code -1} if this information is not available.
1218 public int getIndex()
1224 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1225 * if this information is undefined.
1228 * position for this sequence. This value is zero-based (zero for
1229 * this first sequence)
1231 public void setIndex(int value)
1236 public void setRNA(RNA r)
1247 * Returns a (possibly empty) list of any annotations that match on given
1248 * calcId (source) and label (type). Null values do not match.
1254 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1257 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1258 if (this.annotation != null) {
1259 for (AlignmentAnnotation ann : annotation) {
1260 if (ann.calcId != null && ann.calcId.equals(calcId)
1261 && ann.label != null && ann.label.equals(label))