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.api.DBRefEntryI;
25 import jalview.util.StringUtils;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.Vector;
32 import fr.orsay.lri.varna.models.rna.RNA;
36 * Implements the SequenceI interface for a char[] based sequence object.
41 public class Sequence extends ASequence implements SequenceI
43 SequenceI datasetSequence;
47 private char[] sequence;
55 Vector<PDBEntry> pdbIds;
59 DBRefEntryI sourceDBRef;
66 * This annotation is displayed below the alignment but the positions are tied
67 * to the residues of this sequence
69 * TODO: change to List<>
71 Vector<AlignmentAnnotation> annotation;
74 * The index of the sequence in a MSA
78 /** array of sequence features - may not be null for a valid sequence object */
79 public SequenceFeature[] sequenceFeatures;
82 * Creates a new Sequence object.
87 * string to form a possibly gapped sequence out of
89 * first position of non-gap residue in the sequence
91 * last position of ungapped residues (nearly always only used for
94 public Sequence(String name, String sequence, int start, int end)
96 initSeqAndName(name, sequence.toCharArray(), start, end);
99 public Sequence(String name, char[] sequence, int start, int end)
101 initSeqAndName(name, sequence, start, end);
105 * Stage 1 constructor - assign name, sequence, and set start and end fields.
106 * start and end are updated values from name2 if it ends with /start-end
113 protected void initSeqAndName(String name2, char[] sequence2, int start2,
117 this.sequence = sequence2;
124 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
125 "[/][0-9]{1,}[-][0-9]{1,}$");
127 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
134 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
137 // Does sequence have the /start-end signature?
138 if (limitrx.search(name))
140 name = limitrx.left();
141 endrx.search(limitrx.stringMatched());
142 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
143 endrx.matchedFrom() - 1)));
144 setEnd(Integer.parseInt(endrx.stringMatched()));
148 void checkValidRange()
151 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
154 for (int j = 0; j < sequence.length; j++)
156 if (!jalview.util.Comparison.isGap(sequence[j]))
175 * Creates a new Sequence object.
182 public Sequence(String name, String sequence)
184 this(name, sequence, 1, -1);
188 * Creates a new Sequence object with new features, DBRefEntries,
189 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
195 public Sequence(SequenceI seq)
197 this(seq, seq.getAnnotation());
201 * Create a new sequence object with new features, DBRefEntries, and PDBIds
202 * but inherits any existing dataset sequence reference, and duplicate of any
203 * annotation that is present in the given annotation array.
206 * the sequence to be copied
207 * @param alAnnotation
208 * an array of annotation including some associated with seq
210 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
212 initSeqFrom(seq, alAnnotation);
216 protected void initSeqFrom(SequenceI seq,
217 AlignmentAnnotation[] alAnnotation)
219 initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(),
221 description = seq.getDescription();
222 if (seq.getSequenceFeatures() != null)
224 SequenceFeature[] sf = seq.getSequenceFeatures();
225 for (int i = 0; i < sf.length; i++)
227 addSequenceFeature(new SequenceFeature(sf[i]));
230 setDatasetSequence(seq.getDatasetSequence());
231 if (datasetSequence == null && seq.getDBRefs() != null)
233 // only copy DBRefs if we really are a dataset sequence
234 DBRefEntry[] dbr = seq.getDBRefs();
235 for (int i = 0; i < dbr.length; i++)
237 addDBRef(new DBRefEntry(dbr[i]));
240 if (seq.getAnnotation() != null)
242 AlignmentAnnotation[] sqann = seq.getAnnotation();
243 for (int i = 0; i < sqann.length; i++)
245 if (sqann[i] == null)
249 boolean found = (alAnnotation == null);
252 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
254 found = (alAnnotation[apos] == sqann[i]);
259 // only copy the given annotation
260 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
261 addAlignmentAnnotation(newann);
265 if (seq.getAllPDBEntries() != null)
267 Vector ids = seq.getAllPDBEntries();
268 Enumeration e = ids.elements();
269 while (e.hasMoreElements())
271 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
283 public void setSequenceFeatures(SequenceFeature[] features)
285 sequenceFeatures = features;
289 public synchronized void addSequenceFeature(SequenceFeature sf)
291 if (sequenceFeatures == null)
293 sequenceFeatures = new SequenceFeature[0];
296 for (int i = 0; i < sequenceFeatures.length; i++)
298 if (sequenceFeatures[i].equals(sf))
304 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
305 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
306 temp[sequenceFeatures.length] = sf;
308 sequenceFeatures = temp;
312 public void deleteFeature(SequenceFeature sf)
314 if (sequenceFeatures == null)
320 for (index = 0; index < sequenceFeatures.length; index++)
322 if (sequenceFeatures[index].equals(sf))
328 if (index == sequenceFeatures.length)
333 int sfLength = sequenceFeatures.length;
336 sequenceFeatures = null;
340 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
341 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
343 if (index < sfLength)
345 System.arraycopy(sequenceFeatures, index + 1, temp, index,
346 sequenceFeatures.length - index - 1);
349 sequenceFeatures = temp;
354 * Returns the sequence features (if any), looking first on the sequence, then
355 * on its dataset sequence, and so on until a non-null value is found (or
356 * none). This supports retrieval of sequence features stored on the sequence
357 * (as in the applet) or on the dataset sequence (as in the Desktop version).
362 public SequenceFeature[] getSequenceFeatures()
364 SequenceFeature[] features = sequenceFeatures;
366 SequenceI seq = this;
367 int count = 0; // failsafe against loop in sequence.datasetsequence...
368 while (features == null && seq.getDatasetSequence() != null
371 seq = seq.getDatasetSequence();
372 features = ((Sequence) seq).sequenceFeatures;
378 public void addPDBId(PDBEntry entry)
382 pdbIds = new Vector<PDBEntry>();
384 if (pdbIds.contains(entry))
386 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
390 pdbIds.addElement(entry);
394 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
396 if (newEntry.getFile() != null)
398 oldEntry.setFile(newEntry.getFile());
409 public void setPDBId(Vector<PDBEntry> id)
417 * @return DOCUMENT ME!
420 public Vector<PDBEntry> getAllPDBEntries()
428 * @return DOCUMENT ME!
431 public String getDisplayId(boolean jvsuffix)
433 StringBuffer result = new StringBuffer(name);
436 result.append("/" + start + "-" + end);
439 return result.toString();
449 public void setName(String name)
458 * @return DOCUMENT ME!
461 public String getName()
473 public void setStart(int start)
481 * @return DOCUMENT ME!
484 public int getStart()
496 public void setEnd(int end)
504 * @return DOCUMENT ME!
515 * @return DOCUMENT ME!
518 public int getLength()
520 return this.sequence.length;
530 public void setSequence(String seq)
532 this.sequence = seq.toCharArray();
537 public String getSequenceAsString()
539 return new String(sequence);
543 public String getSequenceAsString(int start, int end)
545 return new String(getSequence(start, end));
549 public char[] getSequence()
557 * @see jalview.datamodel.SequenceI#getSequence(int, int)
560 public char[] getSequence(int start, int end)
566 // JBPNote - left to user to pad the result here (TODO:Decide on this
568 if (start >= sequence.length)
573 if (end >= sequence.length)
575 end = sequence.length;
578 char[] reply = new char[end - start];
579 System.arraycopy(sequence, start, reply, 0, end - start);
585 public SequenceI getSubSequence(int start, int end)
591 char[] seq = getSequence(start, end);
596 int nstart = findPosition(start);
597 int nend = findPosition(end) - 1;
598 // JBPNote - this is an incomplete copy.
599 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
600 nseq.setDescription(description);
601 if (datasetSequence != null)
603 nseq.setDatasetSequence(datasetSequence);
607 nseq.setDatasetSequence(this);
618 * @return DOCUMENT ME!
621 public char getCharAt(int i)
623 if (i < sequence.length)
640 public void setDescription(String desc)
642 this.description = desc;
648 * @return DOCUMENT ME!
651 public String getDescription()
653 return this.description;
659 * @see jalview.datamodel.SequenceI#findIndex(int)
662 public int findIndex(int pos)
664 // returns the alignment position for a residue
667 // Rely on end being at least as long as the length of the sequence.
668 while ((i < sequence.length) && (j <= end) && (j <= pos))
670 if (!jalview.util.Comparison.isGap(sequence[i]))
678 if ((j == end) && (j < pos))
689 public int findPosition(int i)
693 int seqlen = sequence.length;
694 while ((j < i) && (j < seqlen))
696 if (!jalview.util.Comparison.isGap(sequence[j]))
708 * Returns an int array where indices correspond to each residue in the
709 * sequence and the element value gives its position in the alignment
711 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
712 * residues in SequenceI object
715 public int[] gapMap()
717 String seq = jalview.analysis.AlignSeq.extractGaps(
718 jalview.util.Comparison.GapChars, new String(sequence));
719 int[] map = new int[seq.length()];
723 while (j < sequence.length)
725 if (!jalview.util.Comparison.isGap(sequence[j]))
737 public int[] findPositionMap()
739 int map[] = new int[sequence.length];
742 int seqlen = sequence.length;
746 if (!jalview.util.Comparison.isGap(sequence[j]))
757 public List<int[]> getInsertions()
759 ArrayList<int[]> map = new ArrayList<int[]>();
760 int lastj = -1, j = 0;
762 int seqlen = sequence.length;
765 if (jalview.util.Comparison.isGap(sequence[j]))
776 map.add(new int[] { lastj, j - 1 });
784 map.add(new int[] { lastj, j - 1 });
791 public void deleteChars(int i, int j)
793 int newstart = start, newend = end;
794 if (i >= sequence.length || i < 0)
799 char[] tmp = StringUtils.deleteChars(sequence, i, j);
800 boolean createNewDs = false;
801 // TODO: take a (second look) at the dataset creation validation method for
802 // the very large sequence case
803 int eindex = -1, sindex = -1;
804 boolean ecalc = false, scalc = false;
805 for (int s = i; s < j; s++)
807 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
817 sindex = findIndex(start) - 1;
822 // delete characters including start of sequence
823 newstart = findPosition(j);
824 break; // don't need to search for any more residue characters.
828 // delete characters after start.
831 eindex = findIndex(end) - 1;
836 // delete characters at end of sequence
837 newend = findPosition(i - 1);
838 break; // don't need to search for any more residue characters.
843 newend--; // decrease end position by one for the deleted residue
844 // and search further
850 // deletion occured in the middle of the sequence
851 if (createNewDs && this.datasetSequence != null)
853 // construct a new sequence
854 Sequence ds = new Sequence(datasetSequence);
855 // TODO: remove any non-inheritable properties ?
856 // TODO: create a sequence mapping (since there is a relation here ?)
857 ds.deleteChars(i, j);
858 datasetSequence = ds;
866 public void insertCharAt(int i, int length, char c)
868 char[] tmp = new char[sequence.length + length];
870 if (i >= sequence.length)
872 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
877 System.arraycopy(sequence, 0, tmp, 0, i);
887 if (i < sequence.length)
889 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
896 public void insertCharAt(int i, char c)
898 insertCharAt(i, 1, c);
902 public String getVamsasId()
908 public void setVamsasId(String id)
914 public void setDBRef(DBRefEntry[] dbref)
920 public DBRefEntry[] getDBRefs()
922 if (dbrefs == null && datasetSequence != null
923 && this != datasetSequence)
925 return datasetSequence.getDBRefs();
931 public void addDBRef(DBRefEntry entry)
935 dbrefs = new DBRefEntry[0];
938 int i, iSize = dbrefs.length;
940 for (i = 0; i < iSize; i++)
942 if (dbrefs[i].equalRef(entry))
944 if (entry.getMap() != null)
946 if (dbrefs[i].getMap() == null)
948 // overwrite with 'superior' entry that contains a mapping.
956 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
957 System.arraycopy(dbrefs, 0, temp, 0, iSize);
958 temp[temp.length - 1] = entry;
964 public void setDatasetSequence(SequenceI seq)
966 datasetSequence = seq;
970 public SequenceI getDatasetSequence()
972 return datasetSequence;
976 public AlignmentAnnotation[] getAnnotation()
978 return annotation == null ? null : annotation
979 .toArray(new AlignmentAnnotation[annotation.size()]);
983 public boolean hasAnnotation(AlignmentAnnotation ann)
985 return annotation == null ? false : annotation.contains(ann);
989 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
991 if (this.annotation == null)
993 this.annotation = new Vector<AlignmentAnnotation>();
995 if (!this.annotation.contains(annotation))
997 this.annotation.addElement(annotation);
999 annotation.setSequenceRef(this);
1003 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1005 if (this.annotation != null)
1007 this.annotation.removeElement(annotation);
1008 if (this.annotation.size() == 0)
1010 this.annotation = null;
1016 * test if this is a valid candidate for another sequence's dataset sequence.
1019 private boolean isValidDatasetSequence()
1021 if (datasetSequence != null)
1025 for (int i = 0; i < sequence.length; i++)
1027 if (jalview.util.Comparison.isGap(sequence[i]))
1036 public SequenceI deriveSequence()
1038 SequenceI seq = new Sequence(this);
1039 if (datasetSequence != null)
1041 // duplicate current sequence with same dataset
1042 seq.setDatasetSequence(datasetSequence);
1046 if (isValidDatasetSequence())
1048 // Use this as dataset sequence
1049 seq.setDatasetSequence(this);
1053 // Create a new, valid dataset sequence
1055 ds.setSequence(AlignSeq.extractGaps(
1056 jalview.util.Comparison.GapChars, new String(sequence)));
1057 setDatasetSequence(ds);
1058 ds.setSequenceFeatures(getSequenceFeatures());
1059 seq = this; // and return this sequence as the derived sequence.
1068 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1071 public SequenceI createDatasetSequence()
1073 if (datasetSequence == null)
1075 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1076 jalview.util.Comparison.GapChars, getSequenceAsString()),
1077 getStart(), getEnd());
1078 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1079 datasetSequence.setDescription(getDescription());
1080 setSequenceFeatures(null);
1081 // move database references onto dataset sequence
1082 datasetSequence.setDBRef(getDBRefs());
1084 datasetSequence.setPDBId(getAllPDBEntries());
1086 datasetSequence.updatePDBIds();
1087 if (annotation != null)
1089 for (AlignmentAnnotation aa : annotation)
1091 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1092 _aa.sequenceRef = datasetSequence;
1093 _aa.adjustForAlignment(); // uses annotation's own record of
1094 // sequence-column mapping
1095 datasetSequence.addAlignmentAnnotation(_aa);
1099 return datasetSequence;
1106 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1110 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1112 if (annotation != null)
1114 annotation.removeAllElements();
1116 if (annotations != null)
1118 for (int i = 0; i < annotations.length; i++)
1120 if (annotations[i] != null)
1122 addAlignmentAnnotation(annotations[i]);
1129 public AlignmentAnnotation[] getAnnotation(String label)
1131 if (annotation == null || annotation.size() == 0)
1136 Vector subset = new Vector();
1137 Enumeration e = annotation.elements();
1138 while (e.hasMoreElements())
1140 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1141 if (ann.label != null && ann.label.equals(label))
1143 subset.addElement(ann);
1146 if (subset.size() == 0)
1150 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1152 e = subset.elements();
1153 while (e.hasMoreElements())
1155 anns[i++] = (AlignmentAnnotation) e.nextElement();
1157 subset.removeAllElements();
1162 public boolean updatePDBIds()
1164 if (datasetSequence != null)
1166 // TODO: could merge DBRefs
1167 return datasetSequence.updatePDBIds();
1169 if (dbrefs == null || dbrefs.length == 0)
1173 Vector newpdb = new Vector();
1174 for (int i = 0; i < dbrefs.length; i++)
1176 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1178 PDBEntry pdbe = new PDBEntry();
1179 pdbe.setId(dbrefs[i].getAccessionId());
1180 if (pdbIds == null || pdbIds.size() == 0)
1182 newpdb.addElement(pdbe);
1186 Enumeration en = pdbIds.elements();
1187 boolean matched = false;
1188 while (!matched && en.hasMoreElements())
1190 PDBEntry anentry = (PDBEntry) en.nextElement();
1191 if (anentry.getId().equals(pdbe.getId()))
1198 newpdb.addElement(pdbe);
1203 if (newpdb.size() > 0)
1205 Enumeration en = newpdb.elements();
1206 while (en.hasMoreElements())
1208 addPDBId((PDBEntry) en.nextElement());
1216 public void transferAnnotation(SequenceI entry, Mapping mp)
1218 if (datasetSequence != null)
1220 datasetSequence.transferAnnotation(entry, mp);
1223 if (entry.getDatasetSequence() != null)
1225 transferAnnotation(entry.getDatasetSequence(), mp);
1228 // transfer any new features from entry onto sequence
1229 if (entry.getSequenceFeatures() != null)
1232 SequenceFeature[] sfs = entry.getSequenceFeatures();
1233 for (int si = 0; si < sfs.length; si++)
1235 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1236 : new SequenceFeature[] { new SequenceFeature(sfs[si]) };
1237 if (sf != null && sf.length > 0)
1239 for (int sfi = 0; sfi < sf.length; sfi++)
1241 addSequenceFeature(sf[sfi]);
1247 // transfer PDB entries
1248 if (entry.getAllPDBEntries() != null)
1250 Enumeration e = entry.getAllPDBEntries().elements();
1251 while (e.hasMoreElements())
1253 PDBEntry pdb = (PDBEntry) e.nextElement();
1257 // transfer database references
1258 DBRefEntry[] entryRefs = entry.getDBRefs();
1259 if (entryRefs != null)
1261 for (int r = 0; r < entryRefs.length; r++)
1263 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1264 if (newref.getMap() != null && mp != null)
1266 // remap ref using our local mapping
1268 // we also assume all version string setting is done by dbSourceProxy
1270 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1271 * newref.setSource(dbSource); }
1279 * @return The index (zero-based) on this sequence in the MSA. It returns
1280 * {@code -1} if this information is not available.
1283 public int getIndex()
1289 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1290 * if this information is undefined.
1293 * position for this sequence. This value is zero-based (zero for
1294 * this first sequence)
1297 public void setIndex(int value)
1303 public void setRNA(RNA r)
1315 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1318 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1319 if (this.annotation != null)
1321 for (AlignmentAnnotation ann : annotation)
1323 if (ann.calcId != null && ann.calcId.equals(calcId)
1324 && ann.label != null && ann.label.equals(label))
1334 public String toString()
1336 return getDisplayId(false);
1340 public PDBEntry getPDBEntry(String pdbIdStr)
1342 if (getDatasetSequence() == null
1343 || getDatasetSequence().getAllPDBEntries() == null)
1347 List<PDBEntry> entries = getDatasetSequence().getAllPDBEntries();
1348 for (PDBEntry entry : entries)
1350 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1359 public void setSourceDBRef(DBRefEntryI dbRef)
1361 this.sourceDBRef = dbRef;
1365 public DBRefEntryI getSourceDBRef()
1367 return this.sourceDBRef;