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 extends ASequence implements SequenceI
42 SequenceI datasetSequence;
46 private char[] sequence;
54 Vector<PDBEntry> pdbIds;
64 * This annotation is displayed below the alignment but the positions are tied
65 * to the residues of this sequence
67 * TODO: change to List<>
69 Vector<AlignmentAnnotation> annotation;
72 * The index of the sequence in a MSA
76 /** array of sequence features - may not be null for a valid sequence object */
77 public SequenceFeature[] sequenceFeatures;
80 * Creates a new Sequence object.
85 * string to form a possibly gapped sequence out of
87 * first position of non-gap residue in the sequence
89 * last position of ungapped residues (nearly always only used for
92 public Sequence(String name, String sequence, int start, int end)
94 initSeqAndName(name, sequence.toCharArray(), start, end);
97 public Sequence(String name, char[] sequence, int start, int end)
99 initSeqAndName(name, sequence, start, end);
103 * Stage 1 constructor - assign name, sequence, and set start and end fields.
104 * start and end are updated values from name2 if it ends with /start-end
111 protected void initSeqAndName(String name2, char[] sequence2, int start2,
115 this.sequence = sequence2;
122 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
123 "[/][0-9]{1,}[-][0-9]{1,}$");
125 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
132 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
135 // Does sequence have the /start-end signature?
136 if (limitrx.search(name))
138 name = limitrx.left();
139 endrx.search(limitrx.stringMatched());
140 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
141 endrx.matchedFrom() - 1)));
142 setEnd(Integer.parseInt(endrx.stringMatched()));
146 void checkValidRange()
149 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
152 for (int j = 0; j < sequence.length; j++)
154 if (!jalview.util.Comparison.isGap(sequence[j]))
173 * Creates a new Sequence object.
180 public Sequence(String name, String sequence)
182 this(name, sequence, 1, -1);
186 * Creates a new Sequence object with new features, DBRefEntries,
187 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
193 public Sequence(SequenceI seq)
195 this(seq, seq.getAnnotation());
199 * Create a new sequence object with new features, DBRefEntries, and PDBIds
200 * but inherits any existing dataset sequence reference, and duplicate of any
201 * annotation that is present in the given annotation array.
204 * the sequence to be copied
205 * @param alAnnotation
206 * an array of annotation including some associated with seq
208 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
210 initSeqFrom(seq, alAnnotation);
214 protected void initSeqFrom(SequenceI seq,
215 AlignmentAnnotation[] alAnnotation)
217 initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(),
219 description = seq.getDescription();
220 if (seq.getSequenceFeatures() != null)
222 SequenceFeature[] sf = seq.getSequenceFeatures();
223 for (int i = 0; i < sf.length; i++)
225 addSequenceFeature(new SequenceFeature(sf[i]));
228 setDatasetSequence(seq.getDatasetSequence());
229 if (datasetSequence == null && seq.getDBRef() != null)
231 // only copy DBRefs if we really are a dataset sequence
232 DBRefEntry[] dbr = seq.getDBRef();
233 for (int i = 0; i < dbr.length; i++)
235 addDBRef(new DBRefEntry(dbr[i]));
238 if (seq.getAnnotation() != null)
240 AlignmentAnnotation[] sqann = seq.getAnnotation();
241 for (int i = 0; i < sqann.length; i++)
243 if (sqann[i] == null)
247 boolean found = (alAnnotation == null);
250 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
252 found = (alAnnotation[apos] == sqann[i]);
257 // only copy the given annotation
258 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
259 addAlignmentAnnotation(newann);
263 if (seq.getAllPDBEntries() != null)
265 Vector ids = seq.getAllPDBEntries();
266 Enumeration e = ids.elements();
267 while (e.hasMoreElements())
269 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
280 public void setSequenceFeatures(SequenceFeature[] features)
282 sequenceFeatures = features;
285 public synchronized void addSequenceFeature(SequenceFeature sf)
287 if (sequenceFeatures == null)
289 sequenceFeatures = new SequenceFeature[0];
292 for (int i = 0; i < sequenceFeatures.length; i++)
294 if (sequenceFeatures[i].equals(sf))
300 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
301 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
302 temp[sequenceFeatures.length] = sf;
304 sequenceFeatures = temp;
307 public void deleteFeature(SequenceFeature sf)
309 if (sequenceFeatures == null)
315 for (index = 0; index < sequenceFeatures.length; index++)
317 if (sequenceFeatures[index].equals(sf))
323 if (index == sequenceFeatures.length)
328 int sfLength = sequenceFeatures.length;
331 sequenceFeatures = null;
335 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
336 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
338 if (index < sfLength)
340 System.arraycopy(sequenceFeatures, index + 1, temp, index,
341 sequenceFeatures.length - index - 1);
344 sequenceFeatures = temp;
349 * Returns the sequence features (if any), looking first on the sequence, then
350 * on its dataset sequence, and so on until a non-null value is found (or
351 * none). This supports retrieval of sequence features stored on the sequence
352 * (as in the applet) or on the dataset sequence (as in the Desktop version).
356 public SequenceFeature[] getSequenceFeatures()
358 SequenceFeature[] features = sequenceFeatures;
360 SequenceI seq = this;
361 int count = 0; // failsafe against loop in sequence.datasetsequence...
362 while (features == null && seq.getDatasetSequence() != null
365 seq = seq.getDatasetSequence();
366 features = ((Sequence) seq).sequenceFeatures;
371 public void addPDBId(PDBEntry entry)
375 pdbIds = new Vector<PDBEntry>();
377 if (pdbIds.contains(entry))
379 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
383 pdbIds.addElement(entry);
387 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
389 if (newEntry.getFile() != null)
391 oldEntry.setFile(newEntry.getFile());
402 public void setPDBId(Vector<PDBEntry> id)
410 * @return DOCUMENT ME!
413 public Vector<PDBEntry> getAllPDBEntries()
421 * @return DOCUMENT ME!
423 public String getDisplayId(boolean jvsuffix)
425 StringBuffer result = new StringBuffer(name);
428 result.append("/" + start + "-" + end);
431 return result.toString();
440 public void setName(String name)
449 * @return DOCUMENT ME!
451 public String getName()
462 public void setStart(int start)
470 * @return DOCUMENT ME!
472 public int getStart()
483 public void setEnd(int end)
491 * @return DOCUMENT ME!
501 * @return DOCUMENT ME!
503 public int getLength()
505 return this.sequence.length;
514 public void setSequence(String seq)
516 this.sequence = seq.toCharArray();
520 public String getSequenceAsString()
522 return new String(sequence);
525 public String getSequenceAsString(int start, int end)
527 return new String(getSequence(start, end));
530 public char[] getSequence()
538 * @see jalview.datamodel.SequenceI#getSequence(int, int)
540 public char[] getSequence(int start, int end)
546 // JBPNote - left to user to pad the result here (TODO:Decide on this
548 if (start >= sequence.length)
553 if (end >= sequence.length)
555 end = sequence.length;
558 char[] reply = new char[end - start];
559 System.arraycopy(sequence, start, reply, 0, end - start);
565 public SequenceI getSubSequence(int start, int end)
571 char[] seq = getSequence(start, end);
576 int nstart = findPosition(start);
577 int nend = findPosition(end) - 1;
578 // JBPNote - this is an incomplete copy.
579 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
580 nseq.setDescription(description);
581 if (datasetSequence != null)
583 nseq.setDatasetSequence(datasetSequence);
587 nseq.setDatasetSequence(this);
598 * @return DOCUMENT ME!
600 public char getCharAt(int i)
602 if (i < sequence.length)
618 public void setDescription(String desc)
620 this.description = desc;
626 * @return DOCUMENT ME!
628 public String getDescription()
630 return this.description;
636 * @see jalview.datamodel.SequenceI#findIndex(int)
638 public int findIndex(int pos)
640 // returns the alignment position for a residue
643 // Rely on end being at least as long as the length of the sequence.
644 while ((i < sequence.length) && (j <= end) && (j <= pos))
646 if (!jalview.util.Comparison.isGap(sequence[i]))
654 if ((j == end) && (j < pos))
665 public int findPosition(int i)
669 int seqlen = sequence.length;
670 while ((j < i) && (j < seqlen))
672 if (!jalview.util.Comparison.isGap(sequence[j]))
684 * Returns an int array where indices correspond to each residue in the
685 * sequence and the element value gives its position in the alignment
687 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
688 * residues in SequenceI object
690 public int[] gapMap()
692 String seq = jalview.analysis.AlignSeq.extractGaps(
693 jalview.util.Comparison.GapChars, new String(sequence));
694 int[] map = new int[seq.length()];
698 while (j < sequence.length)
700 if (!jalview.util.Comparison.isGap(sequence[j]))
712 public int[] findPositionMap()
714 int map[] = new int[sequence.length];
717 int seqlen = sequence.length;
721 if (!jalview.util.Comparison.isGap(sequence[j]))
732 public List<int[]> getInsertions()
734 ArrayList<int[]> map = new ArrayList<int[]>();
735 int lastj = -1, j = 0;
737 int seqlen = sequence.length;
740 if (jalview.util.Comparison.isGap(sequence[j]))
768 public void deleteChars(int i, int j)
770 int newstart = start, newend = end;
771 if (i >= sequence.length || i < 0)
776 char[] tmp = StringUtils.deleteChars(sequence, i, j);
777 boolean createNewDs = false;
778 // TODO: take a (second look) at the dataset creation validation method for
779 // the very large sequence case
780 int eindex = -1, sindex = -1;
781 boolean ecalc = false, scalc = false;
782 for (int s = i; s < j; s++)
784 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
794 sindex = findIndex(start) - 1;
799 // delete characters including start of sequence
800 newstart = findPosition(j);
801 break; // don't need to search for any more residue characters.
805 // delete characters after start.
808 eindex = findIndex(end) - 1;
813 // delete characters at end of sequence
814 newend = findPosition(i - 1);
815 break; // don't need to search for any more residue characters.
820 newend--; // decrease end position by one for the deleted residue
821 // and search further
827 // deletion occured in the middle of the sequence
828 if (createNewDs && this.datasetSequence != null)
830 // construct a new sequence
831 Sequence ds = new Sequence(datasetSequence);
832 // TODO: remove any non-inheritable properties ?
833 // TODO: create a sequence mapping (since there is a relation here ?)
834 ds.deleteChars(i, j);
835 datasetSequence = ds;
843 public void insertCharAt(int i, int length, char c)
845 char[] tmp = new char[sequence.length + length];
847 if (i >= sequence.length)
849 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
854 System.arraycopy(sequence, 0, tmp, 0, i);
864 if (i < sequence.length)
866 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
873 public void insertCharAt(int i, char c)
875 insertCharAt(i, 1, c);
879 public String getVamsasId()
885 public void setVamsasId(String id)
891 public void setDBRef(DBRefEntry[] dbref)
897 public DBRefEntry[] getDBRef()
899 if (dbrefs == null && datasetSequence != null
900 && this != datasetSequence)
902 return datasetSequence.getDBRef();
908 public void addDBRef(DBRefEntry entry)
912 dbrefs = new DBRefEntry[0];
915 int i, iSize = dbrefs.length;
917 for (i = 0; i < iSize; i++)
919 if (dbrefs[i].equalRef(entry))
921 if (entry.getMap() != null)
923 if (dbrefs[i].getMap() == null)
925 // overwrite with 'superior' entry that contains a mapping.
933 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
934 System.arraycopy(dbrefs, 0, temp, 0, iSize);
935 temp[temp.length - 1] = entry;
941 public void setDatasetSequence(SequenceI seq)
943 datasetSequence = seq;
947 public SequenceI getDatasetSequence()
949 return datasetSequence;
953 public AlignmentAnnotation[] getAnnotation()
955 return annotation == null ? null : annotation
956 .toArray(new AlignmentAnnotation[annotation.size()]);
961 public boolean hasAnnotation(AlignmentAnnotation ann)
963 return annotation == null ? false : annotation.contains(ann);
967 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
969 if (this.annotation == null)
971 this.annotation = new Vector<AlignmentAnnotation>();
973 if (!this.annotation.contains(annotation))
975 this.annotation.addElement(annotation);
977 annotation.setSequenceRef(this);
980 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
982 if (this.annotation != null)
984 this.annotation.removeElement(annotation);
985 if (this.annotation.size() == 0)
987 this.annotation = null;
993 * test if this is a valid candidate for another sequence's dataset sequence.
996 private boolean isValidDatasetSequence()
998 if (datasetSequence != null)
1002 for (int i = 0; i < sequence.length; i++)
1004 if (jalview.util.Comparison.isGap(sequence[i]))
1013 public SequenceI deriveSequence()
1015 SequenceI seq = new Sequence(this);
1016 if (datasetSequence != null)
1018 // duplicate current sequence with same dataset
1019 seq.setDatasetSequence(datasetSequence);
1023 if (isValidDatasetSequence())
1025 // Use this as dataset sequence
1026 seq.setDatasetSequence(this);
1030 // Create a new, valid dataset sequence
1032 ds.setSequence(AlignSeq.extractGaps(
1033 jalview.util.Comparison.GapChars, new String(sequence)));
1034 setDatasetSequence(ds);
1035 ds.setSequenceFeatures(getSequenceFeatures());
1036 seq = this; // and return this sequence as the derived sequence.
1045 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1047 public SequenceI createDatasetSequence()
1049 if (datasetSequence == null)
1051 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1052 jalview.util.Comparison.GapChars, getSequenceAsString()),
1053 getStart(), getEnd());
1054 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1055 datasetSequence.setDescription(getDescription());
1056 setSequenceFeatures(null);
1057 // move database references onto dataset sequence
1058 datasetSequence.setDBRef(getDBRef());
1060 datasetSequence.setPDBId(getAllPDBEntries());
1062 datasetSequence.updatePDBIds();
1063 if (annotation != null)
1065 for (AlignmentAnnotation aa : annotation)
1067 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1068 _aa.sequenceRef = datasetSequence;
1069 _aa.adjustForAlignment(); // uses annotation's own record of
1070 // sequence-column mapping
1071 datasetSequence.addAlignmentAnnotation(_aa);
1075 return datasetSequence;
1082 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1085 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1087 if (annotation != null)
1089 annotation.removeAllElements();
1091 if (annotations != null)
1093 for (int i = 0; i < annotations.length; i++)
1095 if (annotations[i] != null)
1097 addAlignmentAnnotation(annotations[i]);
1104 public AlignmentAnnotation[] getAnnotation(String label)
1106 if (annotation == null || annotation.size() == 0)
1111 Vector subset = new Vector();
1112 Enumeration e = annotation.elements();
1113 while (e.hasMoreElements())
1115 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1116 if (ann.label != null && ann.label.equals(label))
1118 subset.addElement(ann);
1121 if (subset.size() == 0)
1125 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1127 e = subset.elements();
1128 while (e.hasMoreElements())
1130 anns[i++] = (AlignmentAnnotation) e.nextElement();
1132 subset.removeAllElements();
1137 public boolean updatePDBIds()
1139 if (datasetSequence != null)
1141 // TODO: could merge DBRefs
1142 return datasetSequence.updatePDBIds();
1144 if (dbrefs == null || dbrefs.length == 0)
1148 Vector newpdb = new Vector();
1149 for (int i = 0; i < dbrefs.length; i++)
1151 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1153 PDBEntry pdbe = new PDBEntry();
1154 pdbe.setId(dbrefs[i].getAccessionId());
1155 if (pdbIds == null || pdbIds.size() == 0)
1157 newpdb.addElement(pdbe);
1161 Enumeration en = pdbIds.elements();
1162 boolean matched = false;
1163 while (!matched && en.hasMoreElements())
1165 PDBEntry anentry = (PDBEntry) en.nextElement();
1166 if (anentry.getId().equals(pdbe.getId()))
1173 newpdb.addElement(pdbe);
1178 if (newpdb.size() > 0)
1180 Enumeration en = newpdb.elements();
1181 while (en.hasMoreElements())
1183 addPDBId((PDBEntry) en.nextElement());
1191 public void transferAnnotation(SequenceI entry, Mapping mp)
1193 if (datasetSequence != null)
1195 datasetSequence.transferAnnotation(entry, mp);
1198 if (entry.getDatasetSequence() != null)
1200 transferAnnotation(entry.getDatasetSequence(), mp);
1203 // transfer any new features from entry onto sequence
1204 if (entry.getSequenceFeatures() != null)
1207 SequenceFeature[] sfs = entry.getSequenceFeatures();
1208 for (int si = 0; si < sfs.length; si++)
1210 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1211 : new SequenceFeature[]
1212 { new SequenceFeature(sfs[si]) };
1213 if (sf != null && sf.length > 0)
1215 for (int sfi = 0; sfi < sf.length; sfi++)
1217 addSequenceFeature(sf[sfi]);
1223 // transfer PDB entries
1224 if (entry.getAllPDBEntries() != null)
1226 Enumeration e = entry.getAllPDBEntries().elements();
1227 while (e.hasMoreElements())
1229 PDBEntry pdb = (PDBEntry) e.nextElement();
1233 // transfer database references
1234 DBRefEntry[] entryRefs = entry.getDBRef();
1235 if (entryRefs != null)
1237 for (int r = 0; r < entryRefs.length; r++)
1239 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1240 if (newref.getMap() != null && mp != null)
1242 // remap ref using our local mapping
1244 // we also assume all version string setting is done by dbSourceProxy
1246 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1247 * newref.setSource(dbSource); }
1255 * @return The index (zero-based) on this sequence in the MSA. It returns
1256 * {@code -1} if this information is not available.
1258 public int getIndex()
1264 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1265 * if this information is undefined.
1268 * position for this sequence. This value is zero-based (zero for
1269 * this first sequence)
1271 public void setIndex(int value)
1276 public void setRNA(RNA r)
1287 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1290 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1291 if (this.annotation != null)
1293 for (AlignmentAnnotation ann : annotation)
1295 if (ann.calcId != null && ann.calcId.equals(calcId)
1296 && ann.label != null && ann.label.equals(label))
1305 public String toString()
1307 return getDisplayId(false);
1311 public PDBEntry getPDBEntry(String pdbIdStr)
1313 if (getDatasetSequence() == null
1314 || getDatasetSequence().getAllPDBEntries() == null)
1318 List<PDBEntry> entries = getDatasetSequence().getAllPDBEntries();
1319 for (PDBEntry entry : entries)
1321 if (entry.getId().equalsIgnoreCase(pdbIdStr))