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.jsdev.api.RegExpInterface;
25 import jalview.jsdev.api.VarnaRNA;
26 import jalview.schemes.ResidueProperties;
27 import jalview.util.Comparison;
28 import jalview.util.StringUtils;
30 import java.util.ArrayList;
31 import java.util.Enumeration;
32 import java.util.List;
33 import java.util.Vector;
35 import jalview.jsdev.RegExp;
37 //import fr.orsay.lri.varna.models.rna.RNA;
41 * Implements the SequenceI interface for a char[] based sequence object.
46 public class Sequence extends ASequence implements SequenceI
48 SequenceI datasetSequence;
52 private char[] sequence;
60 Vector<PDBEntry> pdbIds;
70 * This annotation is displayed below the alignment but the positions are tied
71 * to the residues of this sequence
73 * TODO: change to List<>
75 Vector<AlignmentAnnotation> annotation;
78 * The index of the sequence in a MSA
82 /** array of sequence features - may not be null for a valid sequence object */
83 public SequenceFeature[] sequenceFeatures;
86 * Creates a new Sequence object.
91 * string to form a possibly gapped sequence out of
93 * first position of non-gap residue in the sequence
95 * last position of ungapped residues (nearly always only used for
98 public Sequence(String name, String sequence, int start, int end)
100 initSeqAndName(name, sequence.toCharArray(), start, end);
103 public Sequence(String name, char[] sequence, int start, int end)
105 initSeqAndName(name, sequence, start, end);
109 * Stage 1 constructor - assign name, sequence, and set start and end fields.
110 * start and end are updated values from name2 if it ends with /start-end
117 protected void initSeqAndName(String name2, char[] sequence2, int start2,
121 this.sequence = sequence2;
128 RegExpInterface limitrx = RegExp.newRegex("[/][0-9]{1,}[-][0-9]{1,}$");
130 RegExpInterface endrx = RegExp.newRegex("[0-9]{1,}$");
137 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
140 // Does sequence have the /start-end signature?
141 if (limitrx.search(name))
143 name = limitrx.left();
144 endrx.search(limitrx.stringMatched());
145 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
146 endrx.matchedFrom() - 1)));
147 setEnd(Integer.parseInt(endrx.stringMatched()));
151 void checkValidRange()
154 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
157 for (int j = 0; j < sequence.length; j++)
159 if (!Comparison.isGap(sequence[j]))
178 * Creates a new Sequence object.
185 public Sequence(String name, String sequence)
187 this(name, sequence, 1, -1);
191 * Creates a new Sequence object with new features, DBRefEntries,
192 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
198 public Sequence(SequenceI seq)
200 this(seq, seq.getAnnotation());
204 * Create a new sequence object with new features, DBRefEntries, and PDBIds
205 * but inherits any existing dataset sequence reference, and duplicate of any
206 * annotation that is present in the given annotation array.
209 * the sequence to be copied
210 * @param alAnnotation
211 * an array of annotation including some associated with seq
213 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
215 initSeqFrom(seq, alAnnotation);
219 protected void initSeqFrom(SequenceI seq,
220 AlignmentAnnotation[] alAnnotation)
222 initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(),
224 description = seq.getDescription();
225 if (seq.getSequenceFeatures() != null)
227 SequenceFeature[] sf = seq.getSequenceFeatures();
228 for (int i = 0; i < sf.length; i++)
230 addSequenceFeature(new SequenceFeature(sf[i]));
233 setDatasetSequence(seq.getDatasetSequence());
234 if (datasetSequence == null && seq.getDBRef() != null)
236 // only copy DBRefs if we really are a dataset sequence
237 DBRefEntry[] dbr = seq.getDBRef();
238 for (int i = 0; i < dbr.length; i++)
240 addDBRef(new DBRefEntry(dbr[i]));
243 if (seq.getAnnotation() != null)
245 AlignmentAnnotation[] sqann = seq.getAnnotation();
246 for (int i = 0; i < sqann.length; i++)
248 if (sqann[i] == null)
252 boolean found = (alAnnotation == null);
255 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
257 found = (alAnnotation[apos] == sqann[i]);
262 // only copy the given annotation
263 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
264 addAlignmentAnnotation(newann);
268 if (seq.getPDBId() != null)
270 Vector ids = seq.getPDBId();
271 Enumeration e = ids.elements();
272 while (e.hasMoreElements())
274 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
285 public void setSequenceFeatures(SequenceFeature[] features)
287 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;
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).
361 public SequenceFeature[] getSequenceFeatures()
363 SequenceFeature[] features = sequenceFeatures;
365 SequenceI seq = this;
366 int count = 0; // failsafe against loop in sequence.datasetsequence...
367 while (features == null && seq.getDatasetSequence() != null
370 seq = seq.getDatasetSequence();
371 features = ((Sequence) seq).sequenceFeatures;
376 public void addPDBId(PDBEntry entry)
380 pdbIds = new Vector<PDBEntry>();
382 if (pdbIds.contains(entry))
384 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
388 pdbIds.addElement(entry);
392 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
394 if (newEntry.getFile() != null)
396 oldEntry.setFile(newEntry.getFile());
407 public void setPDBId(Vector<PDBEntry> id)
415 * @return DOCUMENT ME!
418 public Vector<PDBEntry> getPDBId()
426 * @return DOCUMENT ME!
428 public String getDisplayId(boolean jvsuffix)
430 StringBuffer result = new StringBuffer(name);
433 result.append("/" + start + "-" + end);
436 return result.toString();
445 public void setName(String name)
454 * @return DOCUMENT ME!
456 public String getName()
467 public void setStart(int start)
475 * @return DOCUMENT ME!
477 public int getStart()
488 public void setEnd(int end)
496 * @return DOCUMENT ME!
506 * @return DOCUMENT ME!
508 public int getLength()
510 return this.sequence.length;
519 public void setSequence(String seq)
521 this.sequence = seq.toCharArray();
525 public String getSequenceAsString()
527 return new String(sequence);
530 public String getSequenceAsString(int start, int end)
532 return new String(getSequence(start, end));
535 public char[] getSequence()
543 * @see jalview.datamodel.SequenceI#getSequence(int, int)
545 public char[] getSequence(int start, int end)
551 // JBPNote - left to user to pad the result here (TODO:Decide on this
553 if (start >= sequence.length)
558 if (end >= sequence.length)
560 end = sequence.length;
563 char[] reply = new char[end - start];
564 System.arraycopy(sequence, start, reply, 0, end - start);
570 public SequenceI getSubSequence(int start, int end)
576 char[] seq = getSequence(start, end);
581 int nstart = findPosition(start);
582 int nend = findPosition(end) - 1;
583 // JBPNote - this is an incomplete copy.
584 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
585 nseq.setDescription(description);
586 if (datasetSequence != null)
588 nseq.setDatasetSequence(datasetSequence);
592 nseq.setDatasetSequence(this);
603 * @return DOCUMENT ME!
605 public char getCharAt(int i)
607 if (i < sequence.length)
623 public void setDescription(String desc)
625 this.description = desc;
631 * @return DOCUMENT ME!
633 public String getDescription()
635 return this.description;
641 * @see jalview.datamodel.SequenceI#findIndex(int)
643 public int findIndex(int pos)
645 // returns the alignment position for a residue
648 // Rely on end being at least as long as the length of the sequence.
649 while ((i < sequence.length) && (j <= end) && (j <= pos))
651 if (!Comparison.isGap(sequence[i]))
659 if ((j == end) && (j < pos))
670 public int findPosition(int i)
674 int seqlen = sequence.length;
675 while ((j < i) && (j < seqlen))
677 if (!Comparison.isGap(sequence[j]))
689 * Returns an int array where indices correspond to each residue in the
690 * sequence and the element value gives its position in the alignment
692 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
693 * residues in SequenceI object
695 public int[] gapMap()
697 String seq = AlignSeq.extractGaps(
698 Comparison.GapChars, new String(sequence));
699 int[] map = new int[seq.length()];
703 while (j < sequence.length)
705 if (!Comparison.isGap(sequence[j]))
717 public int[] findPositionMap()
719 int map[] = new int[sequence.length];
722 int seqlen = sequence.length;
726 if (!Comparison.isGap(sequence[j]))
737 public List<int[]> getInsertions()
739 ArrayList<int[]> map = new ArrayList<int[]>();
740 int lastj = -1, j = 0;
742 int seqlen = sequence.length;
745 if (Comparison.isGap(sequence[j]))
773 public void deleteChars(int i, int j)
775 int newstart = start, newend = end;
776 if (i >= sequence.length || i < 0)
781 char[] tmp = StringUtils.deleteChars(sequence, i, j);
782 boolean createNewDs = false;
783 // TODO: take a (second look) at the dataset creation validation method for
784 // the very large sequence case
785 int eindex = -1, sindex = -1;
786 boolean ecalc = false, scalc = false;
787 for (int s = i; s < j; s++)
789 if (ResidueProperties.aaIndex[sequence[s]] != 23)
799 sindex = findIndex(start) - 1;
804 // delete characters including start of sequence
805 newstart = findPosition(j);
806 break; // don't need to search for any more residue characters.
810 // delete characters after start.
813 eindex = findIndex(end) - 1;
818 // delete characters at end of sequence
819 newend = findPosition(i - 1);
820 break; // don't need to search for any more residue characters.
825 newend--; // decrease end position by one for the deleted residue
826 // and search further
832 // deletion occured in the middle of the sequence
833 if (createNewDs && this.datasetSequence != null)
835 // construct a new sequence
836 Sequence ds = new Sequence(datasetSequence);
837 // TODO: remove any non-inheritable properties ?
838 // TODO: create a sequence mapping (since there is a relation here ?)
839 ds.deleteChars(i, j);
840 datasetSequence = ds;
848 public void insertCharAt(int i, int length, char c)
850 char[] tmp = new char[sequence.length + length];
852 if (i >= sequence.length)
854 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
859 System.arraycopy(sequence, 0, tmp, 0, i);
869 if (i < sequence.length)
871 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
878 public void insertCharAt(int i, char c)
880 insertCharAt(i, 1, c);
884 public String getVamsasId()
890 public void setVamsasId(String id)
896 public void setDBRef(DBRefEntry[] dbref)
902 public DBRefEntry[] getDBRef()
904 if (dbrefs == null && datasetSequence != null
905 && this != datasetSequence)
907 return datasetSequence.getDBRef();
913 public void addDBRef(DBRefEntry entry)
917 dbrefs = new DBRefEntry[0];
920 int i, iSize = dbrefs.length;
922 for (i = 0; i < iSize; i++)
924 if (dbrefs[i].equalRef(entry))
926 if (entry.getMap() != null)
928 if (dbrefs[i].getMap() == null)
930 // overwrite with 'superior' entry that contains a mapping.
938 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
939 System.arraycopy(dbrefs, 0, temp, 0, iSize);
940 temp[temp.length - 1] = entry;
946 public void setDatasetSequence(SequenceI seq)
948 datasetSequence = seq;
952 public SequenceI getDatasetSequence()
954 return datasetSequence;
958 public AlignmentAnnotation[] getAnnotation()
960 return annotation == null ? null : annotation
961 .toArray(new AlignmentAnnotation[annotation.size()]);
966 public boolean hasAnnotation(AlignmentAnnotation ann)
968 return annotation == null ? false : annotation.contains(ann);
972 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
974 if (this.annotation == null)
976 this.annotation = new Vector<AlignmentAnnotation>();
978 if (!this.annotation.contains(annotation))
980 this.annotation.addElement(annotation);
982 annotation.setSequenceRef(this);
985 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
987 if (this.annotation != null)
989 this.annotation.removeElement(annotation);
990 if (this.annotation.size() == 0)
992 this.annotation = null;
998 * test if this is a valid candidate for another sequence's dataset sequence.
1001 private boolean isValidDatasetSequence()
1003 if (datasetSequence != null)
1007 for (int i = 0; i < sequence.length; i++)
1009 if (Comparison.isGap(sequence[i]))
1018 public SequenceI deriveSequence()
1020 SequenceI seq = new Sequence(this);
1021 if (datasetSequence != null)
1023 // duplicate current sequence with same dataset
1024 seq.setDatasetSequence(datasetSequence);
1028 if (isValidDatasetSequence())
1030 // Use this as dataset sequence
1031 seq.setDatasetSequence(this);
1035 // Create a new, valid dataset sequence
1037 ds.setSequence(AlignSeq.extractGaps(
1038 Comparison.GapChars, new String(sequence)));
1039 setDatasetSequence(ds);
1040 ds.setSequenceFeatures(getSequenceFeatures());
1041 seq = this; // and return this sequence as the derived sequence.
1050 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1052 public SequenceI createDatasetSequence()
1054 if (datasetSequence == null)
1056 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1057 Comparison.GapChars, getSequenceAsString()),
1058 getStart(), getEnd());
1059 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1060 datasetSequence.setDescription(getDescription());
1061 setSequenceFeatures(null);
1062 // move database references onto dataset sequence
1063 datasetSequence.setDBRef(getDBRef());
1065 datasetSequence.setPDBId(getPDBId());
1067 datasetSequence.updatePDBIds();
1068 if (annotation != null)
1070 for (AlignmentAnnotation aa : annotation)
1072 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1073 _aa.sequenceRef = datasetSequence;
1074 _aa.adjustForAlignment(); // uses annotation's own record of
1075 // sequence-column mapping
1076 datasetSequence.addAlignmentAnnotation(_aa);
1080 return datasetSequence;
1087 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1090 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1092 if (annotation != null)
1094 annotation.removeAllElements();
1096 if (annotations != null)
1098 for (int i = 0; i < annotations.length; i++)
1100 if (annotations[i] != null)
1102 addAlignmentAnnotation(annotations[i]);
1109 public AlignmentAnnotation[] getAnnotation(String label)
1111 if (annotation == null || annotation.size() == 0)
1116 Vector subset = new Vector();
1117 Enumeration e = annotation.elements();
1118 while (e.hasMoreElements())
1120 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1121 if (ann.label != null && ann.label.equals(label))
1123 subset.addElement(ann);
1126 if (subset.size() == 0)
1130 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1132 e = subset.elements();
1133 while (e.hasMoreElements())
1135 anns[i++] = (AlignmentAnnotation) e.nextElement();
1137 subset.removeAllElements();
1142 public boolean updatePDBIds()
1144 if (datasetSequence != null)
1146 // TODO: could merge DBRefs
1147 return datasetSequence.updatePDBIds();
1149 if (dbrefs == null || dbrefs.length == 0)
1153 Vector newpdb = new Vector();
1154 for (int i = 0; i < dbrefs.length; i++)
1156 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1158 PDBEntry pdbe = new PDBEntry();
1159 pdbe.setId(dbrefs[i].getAccessionId());
1160 if (pdbIds == null || pdbIds.size() == 0)
1162 newpdb.addElement(pdbe);
1166 Enumeration en = pdbIds.elements();
1167 boolean matched = false;
1168 while (!matched && en.hasMoreElements())
1170 PDBEntry anentry = (PDBEntry) en.nextElement();
1171 if (anentry.getId().equals(pdbe.getId()))
1178 newpdb.addElement(pdbe);
1183 if (newpdb.size() > 0)
1185 Enumeration en = newpdb.elements();
1186 while (en.hasMoreElements())
1188 addPDBId((PDBEntry) en.nextElement());
1196 public void transferAnnotation(SequenceI entry, Mapping mp)
1198 if (datasetSequence != null)
1200 datasetSequence.transferAnnotation(entry, mp);
1203 if (entry.getDatasetSequence() != null)
1205 transferAnnotation(entry.getDatasetSequence(), mp);
1208 // transfer any new features from entry onto sequence
1209 if (entry.getSequenceFeatures() != null)
1212 SequenceFeature[] sfs = entry.getSequenceFeatures();
1213 for (int si = 0; si < sfs.length; si++)
1215 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1216 : new SequenceFeature[]
1217 { new SequenceFeature(sfs[si]) };
1218 if (sf != null && sf.length > 0)
1220 for (int sfi = 0; sfi < sf.length; sfi++)
1222 addSequenceFeature(sf[sfi]);
1228 // transfer PDB entries
1229 if (entry.getPDBId() != null)
1231 Enumeration e = entry.getPDBId().elements();
1232 while (e.hasMoreElements())
1234 PDBEntry pdb = (PDBEntry) e.nextElement();
1238 // transfer database references
1239 DBRefEntry[] entryRefs = entry.getDBRef();
1240 if (entryRefs != null)
1242 for (int r = 0; r < entryRefs.length; r++)
1244 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1245 if (newref.getMap() != null && mp != null)
1247 // remap ref using our local mapping
1249 // we also assume all version string setting is done by dbSourceProxy
1251 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1252 * newref.setSource(dbSource); }
1260 * @return The index (zero-based) on this sequence in the MSA. It returns
1261 * {@code -1} if this information is not available.
1263 public int getIndex()
1269 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1270 * if this information is undefined.
1273 * position for this sequence. This value is zero-based (zero for
1274 * this first sequence)
1276 public void setIndex(int value)
1281 public void setRNA(VarnaRNA r)
1286 public VarnaRNA getRNA()
1292 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1295 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1296 if (this.annotation != null)
1298 for (AlignmentAnnotation ann : annotation)
1300 if (ann.calcId != null && ann.calcId.equals(calcId)
1301 && ann.label != null && ann.label.equals(label))