2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3 * Copyright (C) 2015 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;
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)
93 initSeqAndName(name, sequence.toCharArray(), start, end);
96 public Sequence(String name, char[] sequence, int start, int end)
98 initSeqAndName(name, sequence, start, end);
102 * Stage 1 constructor - assign name, sequence, and set start and end fields.
103 * start and end are updated values from name2 if it ends with /start-end
110 protected void initSeqAndName(String name2, char[] sequence2, int start2,
114 this.sequence = sequence2;
121 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
122 "[/][0-9]{1,}[-][0-9]{1,}$");
124 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
131 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
134 // Does sequence have the /start-end signature?
135 if (limitrx.search(name))
137 name = limitrx.left();
138 endrx.search(limitrx.stringMatched());
139 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
140 endrx.matchedFrom() - 1)));
141 setEnd(Integer.parseInt(endrx.stringMatched()));
145 void checkValidRange()
148 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
151 for (int j = 0; j < sequence.length; j++)
153 if (!jalview.util.Comparison.isGap(sequence[j]))
172 * Creates a new Sequence object.
179 public Sequence(String name, String sequence)
181 this(name, sequence, 1, -1);
185 * Creates a new Sequence object with new features, DBRefEntries,
186 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
192 public Sequence(SequenceI seq)
194 this(seq, seq.getAnnotation());
198 * Create a new sequence object with new features, DBRefEntries, and PDBIds
199 * but inherits any existing dataset sequence reference, and duplicate of any
200 * annotation that is present in the given annotation array.
203 * the sequence to be copied
204 * @param alAnnotation
205 * an array of annotation including some associated with seq
207 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
209 initSeqFrom(seq, alAnnotation);
213 protected void initSeqFrom(SequenceI seq,
214 AlignmentAnnotation[] alAnnotation)
216 initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(),
218 description = seq.getDescription();
219 if (seq.getSequenceFeatures() != null)
221 SequenceFeature[] sf = seq.getSequenceFeatures();
222 for (int i = 0; i < sf.length; i++)
224 addSequenceFeature(new SequenceFeature(sf[i]));
227 setDatasetSequence(seq.getDatasetSequence());
228 if (datasetSequence == null && seq.getDBRef() != null)
230 // only copy DBRefs if we really are a dataset sequence
231 DBRefEntry[] dbr = seq.getDBRef();
232 for (int i = 0; i < dbr.length; i++)
234 addDBRef(new DBRefEntry(dbr[i]));
237 if (seq.getAnnotation() != null)
239 AlignmentAnnotation[] sqann = seq.getAnnotation();
240 for (int i = 0; i < sqann.length; i++)
242 if (sqann[i] == null)
246 boolean found = (alAnnotation == null);
249 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
251 found = (alAnnotation[apos] == sqann[i]);
256 // only copy the given annotation
257 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
258 addAlignmentAnnotation(newann);
262 if (seq.getAllPDBEntries() != null)
264 Vector ids = seq.getAllPDBEntries();
265 Enumeration e = ids.elements();
266 while (e.hasMoreElements())
268 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
279 public void setSequenceFeatures(SequenceFeature[] features)
281 sequenceFeatures = features;
284 public synchronized void addSequenceFeature(SequenceFeature sf)
286 if (sequenceFeatures == null)
288 sequenceFeatures = new SequenceFeature[0];
291 for (int i = 0; i < sequenceFeatures.length; i++)
293 if (sequenceFeatures[i].equals(sf))
299 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
300 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
301 temp[sequenceFeatures.length] = sf;
303 sequenceFeatures = temp;
306 public void deleteFeature(SequenceFeature sf)
308 if (sequenceFeatures == null)
314 for (index = 0; index < sequenceFeatures.length; index++)
316 if (sequenceFeatures[index].equals(sf))
322 if (index == sequenceFeatures.length)
327 int sfLength = sequenceFeatures.length;
330 sequenceFeatures = null;
334 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
335 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
337 if (index < sfLength)
339 System.arraycopy(sequenceFeatures, index + 1, temp, index,
340 sequenceFeatures.length - index - 1);
343 sequenceFeatures = temp;
348 * Returns the sequence features (if any), looking first on the sequence, then
349 * on its dataset sequence, and so on until a non-null value is found (or
350 * none). This supports retrieval of sequence features stored on the sequence
351 * (as in the applet) or on the dataset sequence (as in the Desktop version).
355 public SequenceFeature[] getSequenceFeatures()
357 SequenceFeature[] features = sequenceFeatures;
359 SequenceI seq = this;
360 int count = 0; // failsafe against loop in sequence.datasetsequence...
361 while (features == null && seq.getDatasetSequence() != null
364 seq = seq.getDatasetSequence();
365 features = ((Sequence) seq).sequenceFeatures;
370 public void addPDBId(PDBEntry entry)
374 pdbIds = new Vector<PDBEntry>();
376 if (pdbIds.contains(entry))
378 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
382 pdbIds.addElement(entry);
386 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
388 if (newEntry.getFile() != null)
390 oldEntry.setFile(newEntry.getFile());
401 public void setPDBId(Vector<PDBEntry> id)
409 * @return DOCUMENT ME!
412 public Vector<PDBEntry> getAllPDBEntries()
420 * @return DOCUMENT ME!
422 public String getDisplayId(boolean jvsuffix)
424 StringBuffer result = new StringBuffer(name);
427 result.append("/" + start + "-" + end);
430 return result.toString();
439 public void setName(String name)
448 * @return DOCUMENT ME!
450 public String getName()
461 public void setStart(int start)
469 * @return DOCUMENT ME!
471 public int getStart()
482 public void setEnd(int end)
490 * @return DOCUMENT ME!
500 * @return DOCUMENT ME!
502 public int getLength()
504 return this.sequence.length;
513 public void setSequence(String seq)
515 this.sequence = seq.toCharArray();
519 public String getSequenceAsString()
521 return new String(sequence);
524 public String getSequenceAsString(int start, int end)
526 return new String(getSequence(start, end));
529 public char[] getSequence()
537 * @see jalview.datamodel.SequenceI#getSequence(int, int)
539 public char[] getSequence(int start, int end)
545 // JBPNote - left to user to pad the result here (TODO:Decide on this
547 if (start >= sequence.length)
552 if (end >= sequence.length)
554 end = sequence.length;
557 char[] reply = new char[end - start];
558 System.arraycopy(sequence, start, reply, 0, end - start);
564 public SequenceI getSubSequence(int start, int end)
570 char[] seq = getSequence(start, end);
575 int nstart = findPosition(start);
576 int nend = findPosition(end) - 1;
577 // JBPNote - this is an incomplete copy.
578 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
579 nseq.setDescription(description);
580 if (datasetSequence != null)
582 nseq.setDatasetSequence(datasetSequence);
586 nseq.setDatasetSequence(this);
597 * @return DOCUMENT ME!
599 public char getCharAt(int i)
601 if (i < sequence.length)
617 public void setDescription(String desc)
619 this.description = desc;
625 * @return DOCUMENT ME!
627 public String getDescription()
629 return this.description;
635 * @see jalview.datamodel.SequenceI#findIndex(int)
637 public int findIndex(int pos)
639 // returns the alignment position for a residue
642 // Rely on end being at least as long as the length of the sequence.
643 while ((i < sequence.length) && (j <= end) && (j <= pos))
645 if (!jalview.util.Comparison.isGap(sequence[i]))
653 if ((j == end) && (j < pos))
664 public int findPosition(int i)
668 int seqlen = sequence.length;
669 while ((j < i) && (j < seqlen))
671 if (!jalview.util.Comparison.isGap(sequence[j]))
683 * Returns an int array where indices correspond to each residue in the
684 * sequence and the element value gives its position in the alignment
686 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
687 * residues in SequenceI object
689 public int[] gapMap()
691 String seq = jalview.analysis.AlignSeq.extractGaps(
692 jalview.util.Comparison.GapChars, new String(sequence));
693 int[] map = new int[seq.length()];
697 while (j < sequence.length)
699 if (!jalview.util.Comparison.isGap(sequence[j]))
711 public int[] findPositionMap()
713 int map[] = new int[sequence.length];
716 int seqlen = sequence.length;
720 if (!jalview.util.Comparison.isGap(sequence[j]))
731 public List<int[]> getInsertions()
733 ArrayList<int[]> map = new ArrayList<int[]>();
734 int lastj = -1, j = 0;
736 int seqlen = sequence.length;
739 if (jalview.util.Comparison.isGap(sequence[j]))
750 map.add(new int[] { lastj, j - 1 });
758 map.add(new int[] { lastj, j - 1 });
765 public void deleteChars(int i, int j)
767 int newstart = start, newend = end;
768 if (i >= sequence.length || i < 0)
773 char[] tmp = StringUtils.deleteChars(sequence, i, j);
774 boolean createNewDs = false;
775 // TODO: take a (second look) at the dataset creation validation method for
776 // the very large sequence case
777 int eindex = -1, sindex = -1;
778 boolean ecalc = false, scalc = false;
779 for (int s = i; s < j; s++)
781 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
791 sindex = findIndex(start) - 1;
796 // delete characters including start of sequence
797 newstart = findPosition(j);
798 break; // don't need to search for any more residue characters.
802 // delete characters after start.
805 eindex = findIndex(end) - 1;
810 // delete characters at end of sequence
811 newend = findPosition(i - 1);
812 break; // don't need to search for any more residue characters.
817 newend--; // decrease end position by one for the deleted residue
818 // and search further
824 // deletion occured in the middle of the sequence
825 if (createNewDs && this.datasetSequence != null)
827 // construct a new sequence
828 Sequence ds = new Sequence(datasetSequence);
829 // TODO: remove any non-inheritable properties ?
830 // TODO: create a sequence mapping (since there is a relation here ?)
831 ds.deleteChars(i, j);
832 datasetSequence = ds;
840 public void insertCharAt(int i, int length, char c)
842 char[] tmp = new char[sequence.length + length];
844 if (i >= sequence.length)
846 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
851 System.arraycopy(sequence, 0, tmp, 0, i);
861 if (i < sequence.length)
863 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
870 public void insertCharAt(int i, char c)
872 insertCharAt(i, 1, c);
876 public String getVamsasId()
882 public void setVamsasId(String id)
888 public void setDBRef(DBRefEntry[] dbref)
894 public DBRefEntry[] getDBRef()
896 if (dbrefs == null && datasetSequence != null
897 && this != datasetSequence)
899 return datasetSequence.getDBRef();
905 public void addDBRef(DBRefEntry entry)
909 dbrefs = new DBRefEntry[0];
912 int i, iSize = dbrefs.length;
914 for (i = 0; i < iSize; i++)
916 if (dbrefs[i].equalRef(entry))
918 if (entry.getMap() != null)
920 if (dbrefs[i].getMap() == null)
922 // overwrite with 'superior' entry that contains a mapping.
930 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
931 System.arraycopy(dbrefs, 0, temp, 0, iSize);
932 temp[temp.length - 1] = entry;
938 public void setDatasetSequence(SequenceI seq)
940 datasetSequence = seq;
944 public SequenceI getDatasetSequence()
946 return datasetSequence;
950 public AlignmentAnnotation[] getAnnotation()
952 return annotation == null ? null : annotation
953 .toArray(new AlignmentAnnotation[annotation.size()]);
957 public boolean hasAnnotation(AlignmentAnnotation ann)
959 return annotation == null ? false : annotation.contains(ann);
963 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
965 if (this.annotation == null)
967 this.annotation = new Vector<AlignmentAnnotation>();
969 if (!this.annotation.contains(annotation))
971 this.annotation.addElement(annotation);
973 annotation.setSequenceRef(this);
976 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
978 if (this.annotation != null)
980 this.annotation.removeElement(annotation);
981 if (this.annotation.size() == 0)
983 this.annotation = null;
989 * test if this is a valid candidate for another sequence's dataset sequence.
992 private boolean isValidDatasetSequence()
994 if (datasetSequence != null)
998 for (int i = 0; i < sequence.length; i++)
1000 if (jalview.util.Comparison.isGap(sequence[i]))
1009 public SequenceI deriveSequence()
1011 SequenceI seq = new Sequence(this);
1012 if (datasetSequence != null)
1014 // duplicate current sequence with same dataset
1015 seq.setDatasetSequence(datasetSequence);
1019 if (isValidDatasetSequence())
1021 // Use this as dataset sequence
1022 seq.setDatasetSequence(this);
1026 // Create a new, valid dataset sequence
1028 ds.setSequence(AlignSeq.extractGaps(
1029 jalview.util.Comparison.GapChars, new String(sequence)));
1030 setDatasetSequence(ds);
1031 ds.setSequenceFeatures(getSequenceFeatures());
1032 seq = this; // and return this sequence as the derived sequence.
1041 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1043 public SequenceI createDatasetSequence()
1045 if (datasetSequence == null)
1047 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1048 jalview.util.Comparison.GapChars, getSequenceAsString()),
1049 getStart(), getEnd());
1050 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1051 datasetSequence.setDescription(getDescription());
1052 setSequenceFeatures(null);
1053 // move database references onto dataset sequence
1054 datasetSequence.setDBRef(getDBRef());
1056 datasetSequence.setPDBId(getAllPDBEntries());
1058 datasetSequence.updatePDBIds();
1059 if (annotation != null)
1061 for (AlignmentAnnotation aa : annotation)
1063 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1064 _aa.sequenceRef = datasetSequence;
1065 _aa.adjustForAlignment(); // uses annotation's own record of
1066 // sequence-column mapping
1067 datasetSequence.addAlignmentAnnotation(_aa);
1071 return datasetSequence;
1078 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1081 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1083 if (annotation != null)
1085 annotation.removeAllElements();
1087 if (annotations != null)
1089 for (int i = 0; i < annotations.length; i++)
1091 if (annotations[i] != null)
1093 addAlignmentAnnotation(annotations[i]);
1100 public AlignmentAnnotation[] getAnnotation(String label)
1102 if (annotation == null || annotation.size() == 0)
1107 Vector subset = new Vector();
1108 Enumeration e = annotation.elements();
1109 while (e.hasMoreElements())
1111 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1112 if (ann.label != null && ann.label.equals(label))
1114 subset.addElement(ann);
1117 if (subset.size() == 0)
1121 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1123 e = subset.elements();
1124 while (e.hasMoreElements())
1126 anns[i++] = (AlignmentAnnotation) e.nextElement();
1128 subset.removeAllElements();
1133 public boolean updatePDBIds()
1135 if (datasetSequence != null)
1137 // TODO: could merge DBRefs
1138 return datasetSequence.updatePDBIds();
1140 if (dbrefs == null || dbrefs.length == 0)
1144 Vector newpdb = new Vector();
1145 for (int i = 0; i < dbrefs.length; i++)
1147 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1149 PDBEntry pdbe = new PDBEntry();
1150 pdbe.setId(dbrefs[i].getAccessionId());
1151 if (pdbIds == null || pdbIds.size() == 0)
1153 newpdb.addElement(pdbe);
1157 Enumeration en = pdbIds.elements();
1158 boolean matched = false;
1159 while (!matched && en.hasMoreElements())
1161 PDBEntry anentry = (PDBEntry) en.nextElement();
1162 if (anentry.getId().equals(pdbe.getId()))
1169 newpdb.addElement(pdbe);
1174 if (newpdb.size() > 0)
1176 Enumeration en = newpdb.elements();
1177 while (en.hasMoreElements())
1179 addPDBId((PDBEntry) en.nextElement());
1187 public void transferAnnotation(SequenceI entry, Mapping mp)
1189 if (datasetSequence != null)
1191 datasetSequence.transferAnnotation(entry, mp);
1194 if (entry.getDatasetSequence() != null)
1196 transferAnnotation(entry.getDatasetSequence(), mp);
1199 // transfer any new features from entry onto sequence
1200 if (entry.getSequenceFeatures() != null)
1203 SequenceFeature[] sfs = entry.getSequenceFeatures();
1204 for (int si = 0; si < sfs.length; si++)
1206 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1207 : new SequenceFeature[] { new SequenceFeature(sfs[si]) };
1208 if (sf != null && sf.length > 0)
1210 for (int sfi = 0; sfi < sf.length; sfi++)
1212 addSequenceFeature(sf[sfi]);
1218 // transfer PDB entries
1219 if (entry.getAllPDBEntries() != null)
1221 Enumeration e = entry.getAllPDBEntries().elements();
1222 while (e.hasMoreElements())
1224 PDBEntry pdb = (PDBEntry) e.nextElement();
1228 // transfer database references
1229 DBRefEntry[] entryRefs = entry.getDBRef();
1230 if (entryRefs != null)
1232 for (int r = 0; r < entryRefs.length; r++)
1234 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1235 if (newref.getMap() != null && mp != null)
1237 // remap ref using our local mapping
1239 // we also assume all version string setting is done by dbSourceProxy
1241 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1242 * newref.setSource(dbSource); }
1250 * @return The index (zero-based) on this sequence in the MSA. It returns
1251 * {@code -1} if this information is not available.
1253 public int getIndex()
1259 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1260 * if this information is undefined.
1263 * position for this sequence. This value is zero-based (zero for
1264 * this first sequence)
1266 public void setIndex(int value)
1271 public void setRNA(RNA r)
1282 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1285 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1286 if (this.annotation != null)
1288 for (AlignmentAnnotation ann : annotation)
1290 if (ann.calcId != null && ann.calcId.equals(calcId)
1291 && ann.label != null && ann.label.equals(label))
1300 public String toString()
1302 return getDisplayId(false);
1306 public PDBEntry getPDBEntry(String pdbIdStr)
1308 if (getDatasetSequence() == null
1309 || getDatasetSequence().getAllPDBEntries() == null)
1313 List<PDBEntry> entries = getDatasetSequence().getAllPDBEntries();
1314 for (PDBEntry entry : entries)
1316 if (entry.getId().equalsIgnoreCase(pdbIdStr))