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.schemes.ResidueProperties;
25 import jalview.util.Comparison;
26 import jalview.util.StringUtils;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.List;
31 import java.util.Vector;
33 import com.stevesoft.pat.Regex;
35 import fr.orsay.lri.varna.models.rna.RNA;
39 * Implements the SequenceI interface for a char[] based sequence object.
44 public class Sequence extends ASequence implements SequenceI
46 SequenceI datasetSequence;
50 private char[] sequence;
58 Vector<PDBEntry> pdbIds;
68 * This annotation is displayed below the alignment but the positions are tied
69 * to the residues of this sequence
71 * TODO: change to List<>
73 Vector<AlignmentAnnotation> annotation;
76 * The index of the sequence in a MSA
80 /** array of sequence features - may not be null for a valid sequence object */
81 public SequenceFeature[] sequenceFeatures;
84 * Creates a new Sequence object.
89 * string to form a possibly gapped sequence out of
91 * first position of non-gap residue in the sequence
93 * last position of ungapped residues (nearly always only used for
96 public Sequence(String name, String sequence, int start, int end)
98 initSeqAndName(name, sequence.toCharArray(), start, end);
101 public Sequence(String name, char[] sequence, int start, int end)
103 initSeqAndName(name, sequence, start, end);
107 * Stage 1 constructor - assign name, sequence, and set start and end fields.
108 * start and end are updated values from name2 if it ends with /start-end
115 protected void initSeqAndName(String name2, char[] sequence2, int start2,
119 this.sequence = sequence2;
126 Regex limitrx = new Regex("[/][0-9]{1,}[-][0-9]{1,}$");
128 Regex endrx = new Regex("[0-9]{1,}$");
135 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
138 // Does sequence have the /start-end signature?
139 if (limitrx.search(name))
141 name = limitrx.left();
142 endrx.search(limitrx.stringMatched());
143 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
144 endrx.matchedFrom() - 1)));
145 setEnd(Integer.parseInt(endrx.stringMatched()));
149 void checkValidRange()
152 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
155 for (int j = 0; j < sequence.length; j++)
157 if (!Comparison.isGap(sequence[j]))
176 * Creates a new Sequence object.
183 public Sequence(String name, String sequence)
185 this(name, sequence, 1, -1);
189 * Creates a new Sequence object with new features, DBRefEntries,
190 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
196 public Sequence(SequenceI seq)
198 this(seq, seq.getAnnotation());
202 * Create a new sequence object with new features, DBRefEntries, and PDBIds
203 * but inherits any existing dataset sequence reference, and duplicate of any
204 * annotation that is present in the given annotation array.
207 * the sequence to be copied
208 * @param alAnnotation
209 * an array of annotation including some associated with seq
211 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
213 initSeqFrom(seq, alAnnotation);
217 protected void initSeqFrom(SequenceI seq,
218 AlignmentAnnotation[] alAnnotation)
220 initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(),
222 description = seq.getDescription();
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.getDBRef() != null)
234 // only copy DBRefs if we really are a dataset sequence
235 DBRefEntry[] dbr = seq.getDBRef();
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.getPDBId() != null)
268 Vector ids = seq.getPDBId();
269 Enumeration e = ids.elements();
270 while (e.hasMoreElements())
272 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
283 public void setSequenceFeatures(SequenceFeature[] features)
285 sequenceFeatures = features;
288 public synchronized void addSequenceFeature(SequenceFeature sf)
290 if (sequenceFeatures == null)
292 sequenceFeatures = new SequenceFeature[0];
295 for (int i = 0; i < sequenceFeatures.length; i++)
297 if (sequenceFeatures[i].equals(sf))
303 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
304 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
305 temp[sequenceFeatures.length] = sf;
307 sequenceFeatures = temp;
310 public void deleteFeature(SequenceFeature sf)
312 if (sequenceFeatures == null)
318 for (index = 0; index < sequenceFeatures.length; index++)
320 if (sequenceFeatures[index].equals(sf))
326 if (index == sequenceFeatures.length)
331 int sfLength = sequenceFeatures.length;
334 sequenceFeatures = null;
338 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
339 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
341 if (index < sfLength)
343 System.arraycopy(sequenceFeatures, index + 1, temp, index,
344 sequenceFeatures.length - index - 1);
347 sequenceFeatures = temp;
352 * Returns the sequence features (if any), looking first on the sequence, then
353 * on its dataset sequence, and so on until a non-null value is found (or
354 * none). This supports retrieval of sequence features stored on the sequence
355 * (as in the applet) or on the dataset sequence (as in the Desktop version).
359 public SequenceFeature[] getSequenceFeatures()
361 SequenceFeature[] features = sequenceFeatures;
363 SequenceI seq = this;
364 int count = 0; // failsafe against loop in sequence.datasetsequence...
365 while (features == null && seq.getDatasetSequence() != null
368 seq = seq.getDatasetSequence();
369 features = ((Sequence) seq).sequenceFeatures;
374 public void addPDBId(PDBEntry entry)
378 pdbIds = new Vector<PDBEntry>();
380 if (pdbIds.contains(entry))
382 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
386 pdbIds.addElement(entry);
390 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
392 if (newEntry.getFile() != null)
394 oldEntry.setFile(newEntry.getFile());
405 public void setPDBId(Vector<PDBEntry> id)
413 * @return DOCUMENT ME!
416 public Vector<PDBEntry> getPDBId()
424 * @return DOCUMENT ME!
426 public String getDisplayId(boolean jvsuffix)
428 StringBuffer result = new StringBuffer(name);
431 result.append("/" + start + "-" + end);
434 return result.toString();
443 public void setName(String name)
452 * @return DOCUMENT ME!
454 public String getName()
465 public void setStart(int start)
473 * @return DOCUMENT ME!
475 public int getStart()
486 public void setEnd(int end)
494 * @return DOCUMENT ME!
504 * @return DOCUMENT ME!
506 public int getLength()
508 return this.sequence.length;
517 public void setSequence(String seq)
519 this.sequence = seq.toCharArray();
523 public String getSequenceAsString()
525 return new String(sequence);
528 public String getSequenceAsString(int start, int end)
530 return new String(getSequence(start, end));
533 public char[] getSequence()
541 * @see jalview.datamodel.SequenceI#getSequence(int, int)
543 public char[] getSequence(int start, int end)
549 // JBPNote - left to user to pad the result here (TODO:Decide on this
551 if (start >= sequence.length)
556 if (end >= sequence.length)
558 end = sequence.length;
561 char[] reply = new char[end - start];
562 System.arraycopy(sequence, start, reply, 0, end - start);
568 public SequenceI getSubSequence(int start, int end)
574 char[] seq = getSequence(start, end);
579 int nstart = findPosition(start);
580 int nend = findPosition(end) - 1;
581 // JBPNote - this is an incomplete copy.
582 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
583 nseq.setDescription(description);
584 if (datasetSequence != null)
586 nseq.setDatasetSequence(datasetSequence);
590 nseq.setDatasetSequence(this);
601 * @return DOCUMENT ME!
603 public char getCharAt(int i)
605 if (i < sequence.length)
621 public void setDescription(String desc)
623 this.description = desc;
629 * @return DOCUMENT ME!
631 public String getDescription()
633 return this.description;
639 * @see jalview.datamodel.SequenceI#findIndex(int)
641 public int findIndex(int pos)
643 // returns the alignment position for a residue
646 // Rely on end being at least as long as the length of the sequence.
647 while ((i < sequence.length) && (j <= end) && (j <= pos))
649 if (!Comparison.isGap(sequence[i]))
657 if ((j == end) && (j < pos))
668 public int findPosition(int i)
672 int seqlen = sequence.length;
673 while ((j < i) && (j < seqlen))
675 if (!Comparison.isGap(sequence[j]))
687 * Returns an int array where indices correspond to each residue in the
688 * sequence and the element value gives its position in the alignment
690 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
691 * residues in SequenceI object
693 public int[] gapMap()
695 String seq = AlignSeq.extractGaps(
696 Comparison.GapChars, new String(sequence));
697 int[] map = new int[seq.length()];
701 while (j < sequence.length)
703 if (!Comparison.isGap(sequence[j]))
715 public int[] findPositionMap()
717 int map[] = new int[sequence.length];
720 int seqlen = sequence.length;
724 if (!Comparison.isGap(sequence[j]))
735 public List<int[]> getInsertions()
737 ArrayList<int[]> map = new ArrayList<int[]>();
738 int lastj = -1, j = 0;
740 int seqlen = sequence.length;
743 if (Comparison.isGap(sequence[j]))
771 public void deleteChars(int i, int j)
773 int newstart = start, newend = end;
774 if (i >= sequence.length || i < 0)
779 char[] tmp = StringUtils.deleteChars(sequence, i, j);
780 boolean createNewDs = false;
781 // TODO: take a (second look) at the dataset creation validation method for
782 // the very large sequence case
783 int eindex = -1, sindex = -1;
784 boolean ecalc = false, scalc = false;
785 for (int s = i; s < j; s++)
787 if (ResidueProperties.aaIndex[sequence[s]] != 23)
797 sindex = findIndex(start) - 1;
802 // delete characters including start of sequence
803 newstart = findPosition(j);
804 break; // don't need to search for any more residue characters.
808 // delete characters after start.
811 eindex = findIndex(end) - 1;
816 // delete characters at end of sequence
817 newend = findPosition(i - 1);
818 break; // don't need to search for any more residue characters.
823 newend--; // decrease end position by one for the deleted residue
824 // and search further
830 // deletion occured in the middle of the sequence
831 if (createNewDs && this.datasetSequence != null)
833 // construct a new sequence
834 Sequence ds = new Sequence(datasetSequence);
835 // TODO: remove any non-inheritable properties ?
836 // TODO: create a sequence mapping (since there is a relation here ?)
837 ds.deleteChars(i, j);
838 datasetSequence = ds;
846 public void insertCharAt(int i, int length, char c)
848 char[] tmp = new char[sequence.length + length];
850 if (i >= sequence.length)
852 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
857 System.arraycopy(sequence, 0, tmp, 0, i);
867 if (i < sequence.length)
869 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
876 public void insertCharAt(int i, char c)
878 insertCharAt(i, 1, c);
882 public String getVamsasId()
888 public void setVamsasId(String id)
894 public void setDBRef(DBRefEntry[] dbref)
900 public DBRefEntry[] getDBRef()
902 if (dbrefs == null && datasetSequence != null
903 && this != datasetSequence)
905 return datasetSequence.getDBRef();
911 public void addDBRef(DBRefEntry entry)
915 dbrefs = new DBRefEntry[0];
918 int i, iSize = dbrefs.length;
920 for (i = 0; i < iSize; i++)
922 if (dbrefs[i].equalRef(entry))
924 if (entry.getMap() != null)
926 if (dbrefs[i].getMap() == null)
928 // overwrite with 'superior' entry that contains a mapping.
936 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
937 System.arraycopy(dbrefs, 0, temp, 0, iSize);
938 temp[temp.length - 1] = entry;
944 public void setDatasetSequence(SequenceI seq)
946 datasetSequence = seq;
950 public SequenceI getDatasetSequence()
952 return datasetSequence;
956 public AlignmentAnnotation[] getAnnotation()
958 return annotation == null ? null : annotation
959 .toArray(new AlignmentAnnotation[annotation.size()]);
964 public boolean hasAnnotation(AlignmentAnnotation ann)
966 return annotation == null ? false : annotation.contains(ann);
970 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
972 if (this.annotation == null)
974 this.annotation = new Vector<AlignmentAnnotation>();
976 if (!this.annotation.contains(annotation))
978 this.annotation.addElement(annotation);
980 annotation.setSequenceRef(this);
983 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
985 if (this.annotation != null)
987 this.annotation.removeElement(annotation);
988 if (this.annotation.size() == 0)
990 this.annotation = null;
996 * test if this is a valid candidate for another sequence's dataset sequence.
999 private boolean isValidDatasetSequence()
1001 if (datasetSequence != null)
1005 for (int i = 0; i < sequence.length; i++)
1007 if (Comparison.isGap(sequence[i]))
1016 public SequenceI deriveSequence()
1018 SequenceI seq = new Sequence(this);
1019 if (datasetSequence != null)
1021 // duplicate current sequence with same dataset
1022 seq.setDatasetSequence(datasetSequence);
1026 if (isValidDatasetSequence())
1028 // Use this as dataset sequence
1029 seq.setDatasetSequence(this);
1033 // Create a new, valid dataset sequence
1035 ds.setSequence(AlignSeq.extractGaps(
1036 Comparison.GapChars, new String(sequence)));
1037 setDatasetSequence(ds);
1038 ds.setSequenceFeatures(getSequenceFeatures());
1039 seq = this; // and return this sequence as the derived sequence.
1048 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1050 public SequenceI createDatasetSequence()
1052 if (datasetSequence == null)
1054 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1055 Comparison.GapChars, getSequenceAsString()),
1056 getStart(), getEnd());
1057 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1058 datasetSequence.setDescription(getDescription());
1059 setSequenceFeatures(null);
1060 // move database references onto dataset sequence
1061 datasetSequence.setDBRef(getDBRef());
1063 datasetSequence.setPDBId(getPDBId());
1065 datasetSequence.updatePDBIds();
1066 if (annotation != null)
1068 for (AlignmentAnnotation aa : annotation)
1070 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1071 _aa.sequenceRef = datasetSequence;
1072 _aa.adjustForAlignment(); // uses annotation's own record of
1073 // sequence-column mapping
1074 datasetSequence.addAlignmentAnnotation(_aa);
1078 return datasetSequence;
1085 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1088 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1090 if (annotation != null)
1092 annotation.removeAllElements();
1094 if (annotations != null)
1096 for (int i = 0; i < annotations.length; i++)
1098 if (annotations[i] != null)
1100 addAlignmentAnnotation(annotations[i]);
1107 public AlignmentAnnotation[] getAnnotation(String label)
1109 if (annotation == null || annotation.size() == 0)
1114 Vector subset = new Vector();
1115 Enumeration e = annotation.elements();
1116 while (e.hasMoreElements())
1118 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1119 if (ann.label != null && ann.label.equals(label))
1121 subset.addElement(ann);
1124 if (subset.size() == 0)
1128 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1130 e = subset.elements();
1131 while (e.hasMoreElements())
1133 anns[i++] = (AlignmentAnnotation) e.nextElement();
1135 subset.removeAllElements();
1140 public boolean updatePDBIds()
1142 if (datasetSequence != null)
1144 // TODO: could merge DBRefs
1145 return datasetSequence.updatePDBIds();
1147 if (dbrefs == null || dbrefs.length == 0)
1151 Vector newpdb = new Vector();
1152 for (int i = 0; i < dbrefs.length; i++)
1154 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1156 PDBEntry pdbe = new PDBEntry();
1157 pdbe.setId(dbrefs[i].getAccessionId());
1158 if (pdbIds == null || pdbIds.size() == 0)
1160 newpdb.addElement(pdbe);
1164 Enumeration en = pdbIds.elements();
1165 boolean matched = false;
1166 while (!matched && en.hasMoreElements())
1168 PDBEntry anentry = (PDBEntry) en.nextElement();
1169 if (anentry.getId().equals(pdbe.getId()))
1176 newpdb.addElement(pdbe);
1181 if (newpdb.size() > 0)
1183 Enumeration en = newpdb.elements();
1184 while (en.hasMoreElements())
1186 addPDBId((PDBEntry) en.nextElement());
1194 public void transferAnnotation(SequenceI entry, Mapping mp)
1196 if (datasetSequence != null)
1198 datasetSequence.transferAnnotation(entry, mp);
1201 if (entry.getDatasetSequence() != null)
1203 transferAnnotation(entry.getDatasetSequence(), mp);
1206 // transfer any new features from entry onto sequence
1207 if (entry.getSequenceFeatures() != null)
1210 SequenceFeature[] sfs = entry.getSequenceFeatures();
1211 for (int si = 0; si < sfs.length; si++)
1213 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1214 : new SequenceFeature[]
1215 { new SequenceFeature(sfs[si]) };
1216 if (sf != null && sf.length > 0)
1218 for (int sfi = 0; sfi < sf.length; sfi++)
1220 addSequenceFeature(sf[sfi]);
1226 // transfer PDB entries
1227 if (entry.getPDBId() != null)
1229 Enumeration e = entry.getPDBId().elements();
1230 while (e.hasMoreElements())
1232 PDBEntry pdb = (PDBEntry) e.nextElement();
1236 // transfer database references
1237 DBRefEntry[] entryRefs = entry.getDBRef();
1238 if (entryRefs != null)
1240 for (int r = 0; r < entryRefs.length; r++)
1242 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1243 if (newref.getMap() != null && mp != null)
1245 // remap ref using our local mapping
1247 // we also assume all version string setting is done by dbSourceProxy
1249 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1250 * newref.setSource(dbSource); }
1258 * @return The index (zero-based) on this sequence in the MSA. It returns
1259 * {@code -1} if this information is not available.
1261 public int getIndex()
1267 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1268 * if this information is undefined.
1271 * position for this sequence. This value is zero-based (zero for
1272 * this first sequence)
1274 public void setIndex(int value)
1279 public void setRNA(RNA r)
1290 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1293 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1294 if (this.annotation != null)
1296 for (AlignmentAnnotation ann : annotation)
1298 if (ann.calcId != null && ann.calcId.equals(calcId)
1299 && ann.label != null && ann.label.equals(label))