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 sourceDBRef = seq.getSourceDBRef();
223 if (seq.getSequenceFeatures() != null)
225 SequenceFeature[] sf = seq.getSequenceFeatures();
226 for (int i = 0; i < sf.length; i++)
228 addSequenceFeature(new SequenceFeature(sf[i]));
231 setDatasetSequence(seq.getDatasetSequence());
232 if (datasetSequence == null && seq.getDBRefs() != null)
234 // only copy DBRefs if we really are a dataset sequence
235 DBRefEntry[] dbr = seq.getDBRefs();
236 for (int i = 0; i < dbr.length; i++)
238 addDBRef(new DBRefEntry(dbr[i]));
241 if (seq.getAnnotation() != null)
243 AlignmentAnnotation[] sqann = seq.getAnnotation();
244 for (int i = 0; i < sqann.length; i++)
246 if (sqann[i] == null)
250 boolean found = (alAnnotation == null);
253 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
255 found = (alAnnotation[apos] == sqann[i]);
260 // only copy the given annotation
261 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
262 addAlignmentAnnotation(newann);
266 if (seq.getAllPDBEntries() != null)
268 Vector ids = seq.getAllPDBEntries();
269 Enumeration e = ids.elements();
270 while (e.hasMoreElements())
272 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
284 public void setSequenceFeatures(SequenceFeature[] features)
286 sequenceFeatures = features;
290 public synchronized void addSequenceFeature(SequenceFeature sf)
292 if (sequenceFeatures == null)
294 sequenceFeatures = new SequenceFeature[0];
297 for (int i = 0; i < sequenceFeatures.length; i++)
299 if (sequenceFeatures[i].equals(sf))
305 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
306 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
307 temp[sequenceFeatures.length] = sf;
309 sequenceFeatures = temp;
313 public void deleteFeature(SequenceFeature sf)
315 if (sequenceFeatures == null)
321 for (index = 0; index < sequenceFeatures.length; index++)
323 if (sequenceFeatures[index].equals(sf))
329 if (index == sequenceFeatures.length)
334 int sfLength = sequenceFeatures.length;
337 sequenceFeatures = null;
341 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
342 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
344 if (index < sfLength)
346 System.arraycopy(sequenceFeatures, index + 1, temp, index,
347 sequenceFeatures.length - index - 1);
350 sequenceFeatures = temp;
355 * Returns the sequence features (if any), looking first on the sequence, then
356 * on its dataset sequence, and so on until a non-null value is found (or
357 * none). This supports retrieval of sequence features stored on the sequence
358 * (as in the applet) or on the dataset sequence (as in the Desktop version).
363 public SequenceFeature[] getSequenceFeatures()
365 SequenceFeature[] features = sequenceFeatures;
367 SequenceI seq = this;
368 int count = 0; // failsafe against loop in sequence.datasetsequence...
369 while (features == null && seq.getDatasetSequence() != null
372 seq = seq.getDatasetSequence();
373 features = ((Sequence) seq).sequenceFeatures;
379 public void addPDBId(PDBEntry entry)
383 pdbIds = new Vector<PDBEntry>();
385 if (pdbIds.contains(entry))
387 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
391 pdbIds.addElement(entry);
395 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
397 if (newEntry.getFile() != null)
399 oldEntry.setFile(newEntry.getFile());
410 public void setPDBId(Vector<PDBEntry> id)
418 * @return DOCUMENT ME!
421 public Vector<PDBEntry> getAllPDBEntries()
429 * @return DOCUMENT ME!
432 public String getDisplayId(boolean jvsuffix)
434 StringBuffer result = new StringBuffer(name);
437 result.append("/" + start + "-" + end);
440 return result.toString();
450 public void setName(String name)
459 * @return DOCUMENT ME!
462 public String getName()
474 public void setStart(int start)
482 * @return DOCUMENT ME!
485 public int getStart()
497 public void setEnd(int end)
505 * @return DOCUMENT ME!
516 * @return DOCUMENT ME!
519 public int getLength()
521 return this.sequence.length;
531 public void setSequence(String seq)
533 this.sequence = seq.toCharArray();
538 public String getSequenceAsString()
540 return new String(sequence);
544 public String getSequenceAsString(int start, int end)
546 return new String(getSequence(start, end));
550 public char[] getSequence()
558 * @see jalview.datamodel.SequenceI#getSequence(int, int)
561 public char[] getSequence(int start, int end)
567 // JBPNote - left to user to pad the result here (TODO:Decide on this
569 if (start >= sequence.length)
574 if (end >= sequence.length)
576 end = sequence.length;
579 char[] reply = new char[end - start];
580 System.arraycopy(sequence, start, reply, 0, end - start);
586 public SequenceI getSubSequence(int start, int end)
592 char[] seq = getSequence(start, end);
597 int nstart = findPosition(start);
598 int nend = findPosition(end) - 1;
599 // JBPNote - this is an incomplete copy.
600 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
601 nseq.setDescription(description);
602 if (datasetSequence != null)
604 nseq.setDatasetSequence(datasetSequence);
608 nseq.setDatasetSequence(this);
619 * @return DOCUMENT ME!
622 public char getCharAt(int i)
624 if (i < sequence.length)
641 public void setDescription(String desc)
643 this.description = desc;
649 * @return DOCUMENT ME!
652 public String getDescription()
654 return this.description;
660 * @see jalview.datamodel.SequenceI#findIndex(int)
663 public int findIndex(int pos)
665 // returns the alignment position for a residue
668 // Rely on end being at least as long as the length of the sequence.
669 while ((i < sequence.length) && (j <= end) && (j <= pos))
671 if (!jalview.util.Comparison.isGap(sequence[i]))
679 if ((j == end) && (j < pos))
690 public int findPosition(int i)
694 int seqlen = sequence.length;
695 while ((j < i) && (j < seqlen))
697 if (!jalview.util.Comparison.isGap(sequence[j]))
709 * Returns an int array where indices correspond to each residue in the
710 * sequence and the element value gives its position in the alignment
712 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
713 * residues in SequenceI object
716 public int[] gapMap()
718 String seq = jalview.analysis.AlignSeq.extractGaps(
719 jalview.util.Comparison.GapChars, new String(sequence));
720 int[] map = new int[seq.length()];
724 while (j < sequence.length)
726 if (!jalview.util.Comparison.isGap(sequence[j]))
738 public int[] findPositionMap()
740 int map[] = new int[sequence.length];
743 int seqlen = sequence.length;
747 if (!jalview.util.Comparison.isGap(sequence[j]))
758 public List<int[]> getInsertions()
760 ArrayList<int[]> map = new ArrayList<int[]>();
761 int lastj = -1, j = 0;
763 int seqlen = sequence.length;
766 if (jalview.util.Comparison.isGap(sequence[j]))
777 map.add(new int[] { lastj, j - 1 });
785 map.add(new int[] { lastj, j - 1 });
792 public void deleteChars(int i, int j)
794 int newstart = start, newend = end;
795 if (i >= sequence.length || i < 0)
800 char[] tmp = StringUtils.deleteChars(sequence, i, j);
801 boolean createNewDs = false;
802 // TODO: take a (second look) at the dataset creation validation method for
803 // the very large sequence case
804 int eindex = -1, sindex = -1;
805 boolean ecalc = false, scalc = false;
806 for (int s = i; s < j; s++)
808 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
818 sindex = findIndex(start) - 1;
823 // delete characters including start of sequence
824 newstart = findPosition(j);
825 break; // don't need to search for any more residue characters.
829 // delete characters after start.
832 eindex = findIndex(end) - 1;
837 // delete characters at end of sequence
838 newend = findPosition(i - 1);
839 break; // don't need to search for any more residue characters.
844 newend--; // decrease end position by one for the deleted residue
845 // and search further
851 // deletion occured in the middle of the sequence
852 if (createNewDs && this.datasetSequence != null)
854 // construct a new sequence
855 Sequence ds = new Sequence(datasetSequence);
856 // TODO: remove any non-inheritable properties ?
857 // TODO: create a sequence mapping (since there is a relation here ?)
858 ds.deleteChars(i, j);
859 datasetSequence = ds;
867 public void insertCharAt(int i, int length, char c)
869 char[] tmp = new char[sequence.length + length];
871 if (i >= sequence.length)
873 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
878 System.arraycopy(sequence, 0, tmp, 0, i);
888 if (i < sequence.length)
890 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
897 public void insertCharAt(int i, char c)
899 insertCharAt(i, 1, c);
903 public String getVamsasId()
909 public void setVamsasId(String id)
915 public void setDBRef(DBRefEntry[] dbref)
921 public DBRefEntry[] getDBRefs()
923 if (dbrefs == null && datasetSequence != null
924 && this != datasetSequence)
926 return datasetSequence.getDBRefs();
932 public void addDBRef(DBRefEntry entry)
936 dbrefs = new DBRefEntry[0];
939 int i, iSize = dbrefs.length;
941 for (i = 0; i < iSize; i++)
943 if (dbrefs[i].equalRef(entry))
945 if (entry.getMap() != null)
947 if (dbrefs[i].getMap() == null)
949 // overwrite with 'superior' entry that contains a mapping.
957 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
958 System.arraycopy(dbrefs, 0, temp, 0, iSize);
959 temp[temp.length - 1] = entry;
965 public void setDatasetSequence(SequenceI seq)
967 datasetSequence = seq;
971 public SequenceI getDatasetSequence()
973 return datasetSequence;
977 public AlignmentAnnotation[] getAnnotation()
979 return annotation == null ? null : annotation
980 .toArray(new AlignmentAnnotation[annotation.size()]);
984 public boolean hasAnnotation(AlignmentAnnotation ann)
986 return annotation == null ? false : annotation.contains(ann);
990 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
992 if (this.annotation == null)
994 this.annotation = new Vector<AlignmentAnnotation>();
996 if (!this.annotation.contains(annotation))
998 this.annotation.addElement(annotation);
1000 annotation.setSequenceRef(this);
1004 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1006 if (this.annotation != null)
1008 this.annotation.removeElement(annotation);
1009 if (this.annotation.size() == 0)
1011 this.annotation = null;
1017 * test if this is a valid candidate for another sequence's dataset sequence.
1020 private boolean isValidDatasetSequence()
1022 if (datasetSequence != null)
1026 for (int i = 0; i < sequence.length; i++)
1028 if (jalview.util.Comparison.isGap(sequence[i]))
1037 public SequenceI deriveSequence()
1039 SequenceI seq = new Sequence(this);
1040 if (datasetSequence != null)
1042 // duplicate current sequence with same dataset
1043 seq.setDatasetSequence(datasetSequence);
1047 if (isValidDatasetSequence())
1049 // Use this as dataset sequence
1050 seq.setDatasetSequence(this);
1054 // Create a new, valid dataset sequence
1056 ds.setSequence(AlignSeq.extractGaps(
1057 jalview.util.Comparison.GapChars, new String(sequence)));
1058 setDatasetSequence(ds);
1059 ds.setSequenceFeatures(getSequenceFeatures());
1060 seq = this; // and return this sequence as the derived sequence.
1069 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1072 public SequenceI createDatasetSequence()
1074 if (datasetSequence == null)
1076 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1077 jalview.util.Comparison.GapChars, getSequenceAsString()),
1078 getStart(), getEnd());
1079 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1080 datasetSequence.setDescription(getDescription());
1081 setSequenceFeatures(null);
1082 // move database references onto dataset sequence
1083 datasetSequence.setDBRef(getDBRefs());
1085 datasetSequence.setPDBId(getAllPDBEntries());
1087 datasetSequence.updatePDBIds();
1088 if (annotation != null)
1090 for (AlignmentAnnotation aa : annotation)
1092 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1093 _aa.sequenceRef = datasetSequence;
1094 _aa.adjustForAlignment(); // uses annotation's own record of
1095 // sequence-column mapping
1096 datasetSequence.addAlignmentAnnotation(_aa);
1100 return datasetSequence;
1107 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1111 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1113 if (annotation != null)
1115 annotation.removeAllElements();
1117 if (annotations != null)
1119 for (int i = 0; i < annotations.length; i++)
1121 if (annotations[i] != null)
1123 addAlignmentAnnotation(annotations[i]);
1130 public AlignmentAnnotation[] getAnnotation(String label)
1132 if (annotation == null || annotation.size() == 0)
1137 Vector subset = new Vector();
1138 Enumeration e = annotation.elements();
1139 while (e.hasMoreElements())
1141 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1142 if (ann.label != null && ann.label.equals(label))
1144 subset.addElement(ann);
1147 if (subset.size() == 0)
1151 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1153 e = subset.elements();
1154 while (e.hasMoreElements())
1156 anns[i++] = (AlignmentAnnotation) e.nextElement();
1158 subset.removeAllElements();
1163 public boolean updatePDBIds()
1165 if (datasetSequence != null)
1167 // TODO: could merge DBRefs
1168 return datasetSequence.updatePDBIds();
1170 if (dbrefs == null || dbrefs.length == 0)
1174 Vector newpdb = new Vector();
1175 for (int i = 0; i < dbrefs.length; i++)
1177 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1179 PDBEntry pdbe = new PDBEntry();
1180 pdbe.setId(dbrefs[i].getAccessionId());
1181 if (pdbIds == null || pdbIds.size() == 0)
1183 newpdb.addElement(pdbe);
1187 Enumeration en = pdbIds.elements();
1188 boolean matched = false;
1189 while (!matched && en.hasMoreElements())
1191 PDBEntry anentry = (PDBEntry) en.nextElement();
1192 if (anentry.getId().equals(pdbe.getId()))
1199 newpdb.addElement(pdbe);
1204 if (newpdb.size() > 0)
1206 Enumeration en = newpdb.elements();
1207 while (en.hasMoreElements())
1209 addPDBId((PDBEntry) en.nextElement());
1217 public void transferAnnotation(SequenceI entry, Mapping mp)
1219 if (datasetSequence != null)
1221 datasetSequence.transferAnnotation(entry, mp);
1224 if (entry.getDatasetSequence() != null)
1226 transferAnnotation(entry.getDatasetSequence(), mp);
1229 // transfer any new features from entry onto sequence
1230 if (entry.getSequenceFeatures() != null)
1233 SequenceFeature[] sfs = entry.getSequenceFeatures();
1234 for (int si = 0; si < sfs.length; si++)
1236 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1237 : new SequenceFeature[] { new SequenceFeature(sfs[si]) };
1238 if (sf != null && sf.length > 0)
1240 for (int sfi = 0; sfi < sf.length; sfi++)
1242 addSequenceFeature(sf[sfi]);
1248 // transfer PDB entries
1249 if (entry.getAllPDBEntries() != null)
1251 Enumeration e = entry.getAllPDBEntries().elements();
1252 while (e.hasMoreElements())
1254 PDBEntry pdb = (PDBEntry) e.nextElement();
1258 // transfer database references
1259 DBRefEntry[] entryRefs = entry.getDBRefs();
1260 if (entryRefs != null)
1262 for (int r = 0; r < entryRefs.length; r++)
1264 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1265 if (newref.getMap() != null && mp != null)
1267 // remap ref using our local mapping
1269 // we also assume all version string setting is done by dbSourceProxy
1271 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1272 * newref.setSource(dbSource); }
1280 * @return The index (zero-based) on this sequence in the MSA. It returns
1281 * {@code -1} if this information is not available.
1284 public int getIndex()
1290 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1291 * if this information is undefined.
1294 * position for this sequence. This value is zero-based (zero for
1295 * this first sequence)
1298 public void setIndex(int value)
1304 public void setRNA(RNA r)
1316 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1319 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1320 if (this.annotation != null)
1322 for (AlignmentAnnotation ann : annotation)
1324 if (ann.calcId != null && ann.calcId.equals(calcId)
1325 && ann.label != null && ann.label.equals(label))
1335 public String toString()
1337 return getDisplayId(false);
1341 public PDBEntry getPDBEntry(String pdbIdStr)
1343 if (getDatasetSequence() == null
1344 || getDatasetSequence().getAllPDBEntries() == null)
1348 List<PDBEntry> entries = getDatasetSequence().getAllPDBEntries();
1349 for (PDBEntry entry : entries)
1351 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1360 public void setSourceDBRef(DBRefEntryI dbRef)
1362 this.sourceDBRef = dbRef;
1366 public DBRefEntryI getSourceDBRef()
1368 return this.sourceDBRef;