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.datamodel.features.SequenceFeatures;
26 import jalview.datamodel.features.SequenceFeaturesI;
27 import jalview.util.Comparison;
28 import jalview.util.DBRefUtils;
29 import jalview.util.MapList;
30 import jalview.util.StringUtils;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.BitSet;
35 import java.util.Collections;
36 import java.util.Enumeration;
37 import java.util.List;
38 import java.util.ListIterator;
39 import java.util.Vector;
41 import com.stevesoft.pat.Regex;
43 import fr.orsay.lri.varna.models.rna.RNA;
47 * Implements the SequenceI interface for a char[] based sequence object
49 public class Sequence extends ASequence implements SequenceI
51 private static final Regex limitrx = new Regex(
52 "[/][0-9]{1,}[-][0-9]{1,}$");
54 private static final Regex endrx = new Regex("[0-9]{1,}$");
56 SequenceI datasetSequence;
60 private char[] sequence;
68 Vector<PDBEntry> pdbIds;
77 * This annotation is displayed below the alignment but the positions are tied
78 * to the residues of this sequence
80 * TODO: change to List<>
82 Vector<AlignmentAnnotation> annotation;
85 * The index of the sequence in a MSA
89 private SequenceFeatures sequenceFeatureStore;
92 * A cursor holding the approximate current view position to the sequence,
93 * as determined by findIndex or findPosition or findPositions.
94 * Using a cursor as a hint allows these methods to be more performant for
97 private SequenceCursor cursor;
100 * A number that should be incremented whenever the sequence is edited.
101 * If the value matches the cursor token, then we can trust the cursor,
102 * if not then it should be recomputed.
104 private int changeCount;
107 * Creates a new Sequence object.
110 * display name string
112 * string to form a possibly gapped sequence out of
114 * first position of non-gap residue in the sequence
116 * last position of ungapped residues (nearly always only used for
119 public Sequence(String name, String sequence, int start, int end)
122 initSeqAndName(name, sequence.toCharArray(), start, end);
125 public Sequence(String name, char[] sequence, int start, int end)
128 initSeqAndName(name, sequence, start, end);
132 * Stage 1 constructor - assign name, sequence, and set start and end fields.
133 * start and end are updated values from name2 if it ends with /start-end
140 protected void initSeqAndName(String name2, char[] sequence2, int start2,
144 this.sequence = sequence2;
156 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
159 // Does sequence have the /start-end signature?
160 if (limitrx.search(name))
162 name = limitrx.left();
163 endrx.search(limitrx.stringMatched());
164 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
165 endrx.matchedFrom() - 1)));
166 setEnd(Integer.parseInt(endrx.stringMatched()));
170 void checkValidRange()
173 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
176 for (int j = 0; j < sequence.length; j++)
178 if (!jalview.util.Comparison.isGap(sequence[j]))
197 * default constructor
201 sequenceFeatureStore = new SequenceFeatures();
205 * Creates a new Sequence object.
212 public Sequence(String name, String sequence)
214 this(name, sequence, 1, -1);
218 * Creates a new Sequence object with new AlignmentAnnotations but inherits
219 * any existing dataset sequence reference. If non exists, everything is
223 * if seq is a dataset sequence, behaves like a plain old copy
226 public Sequence(SequenceI seq)
228 this(seq, seq.getAnnotation());
232 * Create a new sequence object with new features, DBRefEntries, and PDBIds
233 * but inherits any existing dataset sequence reference, and duplicate of any
234 * annotation that is present in the given annotation array.
237 * the sequence to be copied
238 * @param alAnnotation
239 * an array of annotation including some associated with seq
241 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
244 initSeqFrom(seq, alAnnotation);
248 * does the heavy lifting when cloning a dataset sequence, or coping data from
249 * dataset to a new derived sequence.
252 * - source of attributes.
253 * @param alAnnotation
254 * - alignment annotation present on seq that should be copied onto
257 protected void initSeqFrom(SequenceI seq,
258 AlignmentAnnotation[] alAnnotation)
260 char[] oseq = seq.getSequence(); // returns a copy of the array
261 initSeqAndName(seq.getName(), oseq, seq.getStart(), seq.getEnd());
263 description = seq.getDescription();
264 if (seq != datasetSequence)
266 setDatasetSequence(seq.getDatasetSequence());
270 * only copy DBRefs and seqfeatures if we really are a dataset sequence
272 if (datasetSequence == null)
274 if (seq.getDBRefs() != null)
276 DBRefEntry[] dbr = seq.getDBRefs();
277 for (int i = 0; i < dbr.length; i++)
279 addDBRef(new DBRefEntry(dbr[i]));
284 * make copies of any sequence features
286 for (SequenceFeature sf : seq.getSequenceFeatures())
288 addSequenceFeature(new SequenceFeature(sf));
292 if (seq.getAnnotation() != null)
294 AlignmentAnnotation[] sqann = seq.getAnnotation();
295 for (int i = 0; i < sqann.length; i++)
297 if (sqann[i] == null)
301 boolean found = (alAnnotation == null);
304 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
306 found = (alAnnotation[apos] == sqann[i]);
311 // only copy the given annotation
312 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
313 addAlignmentAnnotation(newann);
317 if (seq.getAllPDBEntries() != null)
319 Vector<PDBEntry> ids = seq.getAllPDBEntries();
320 for (PDBEntry pdb : ids)
322 this.addPDBId(new PDBEntry(pdb));
328 public void setSequenceFeatures(List<SequenceFeature> features)
330 if (datasetSequence != null)
332 datasetSequence.setSequenceFeatures(features);
335 sequenceFeatureStore = new SequenceFeatures(features);
339 public synchronized boolean addSequenceFeature(SequenceFeature sf)
341 if (sf.getType() == null)
343 System.err.println("SequenceFeature type may not be null: "
348 if (datasetSequence != null)
350 return datasetSequence.addSequenceFeature(sf);
353 return sequenceFeatureStore.add(sf);
357 public void deleteFeature(SequenceFeature sf)
359 if (datasetSequence != null)
361 datasetSequence.deleteFeature(sf);
365 sequenceFeatureStore.delete(sf);
375 public List<SequenceFeature> getSequenceFeatures()
377 if (datasetSequence != null)
379 return datasetSequence.getSequenceFeatures();
381 return sequenceFeatureStore.getAllFeatures();
385 public SequenceFeaturesI getFeatures()
387 return datasetSequence != null ? datasetSequence.getFeatures()
388 : sequenceFeatureStore;
392 public boolean addPDBId(PDBEntry entry)
396 pdbIds = new Vector<PDBEntry>();
401 for (PDBEntry pdbe : pdbIds)
403 if (pdbe.updateFrom(entry))
408 pdbIds.addElement(entry);
419 public void setPDBId(Vector<PDBEntry> id)
427 * @return DOCUMENT ME!
430 public Vector<PDBEntry> getAllPDBEntries()
432 return pdbIds == null ? new Vector<PDBEntry>() : pdbIds;
438 * @return DOCUMENT ME!
441 public String getDisplayId(boolean jvsuffix)
443 StringBuffer result = new StringBuffer(name);
446 result.append("/" + start + "-" + end);
449 return result.toString();
459 public void setName(String name)
468 * @return DOCUMENT ME!
471 public String getName()
483 public void setStart(int start)
491 * @return DOCUMENT ME!
494 public int getStart()
506 public void setEnd(int end)
514 * @return DOCUMENT ME!
525 * @return DOCUMENT ME!
528 public int getLength()
530 return this.sequence.length;
540 public void setSequence(String seq)
542 this.sequence = seq.toCharArray();
548 public String getSequenceAsString()
550 return new String(sequence);
554 public String getSequenceAsString(int start, int end)
556 return new String(getSequence(start, end));
560 public char[] getSequence()
563 return sequence == null ? null : Arrays.copyOf(sequence,
570 * @see jalview.datamodel.SequenceI#getSequence(int, int)
573 public char[] getSequence(int start, int end)
579 // JBPNote - left to user to pad the result here (TODO:Decide on this
581 if (start >= sequence.length)
586 if (end >= sequence.length)
588 end = sequence.length;
591 char[] reply = new char[end - start];
592 System.arraycopy(sequence, start, reply, 0, end - start);
598 public SequenceI getSubSequence(int start, int end)
604 char[] seq = getSequence(start, end);
609 int nstart = findPosition(start);
610 int nend = findPosition(end) - 1;
611 // JBPNote - this is an incomplete copy.
612 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
613 nseq.setDescription(description);
614 if (datasetSequence != null)
616 nseq.setDatasetSequence(datasetSequence);
620 nseq.setDatasetSequence(this);
626 * Returns the character of the aligned sequence at the given position (base
627 * zero), or space if the position is not within the sequence's bounds
632 public char getCharAt(int i)
634 if (i >= 0 && i < sequence.length)
651 public void setDescription(String desc)
653 this.description = desc;
659 * @return DOCUMENT ME!
662 public String getDescription()
664 return this.description;
671 public int findIndex(int pos)
674 * use a valid, hopefully nearby, cursor if available
676 if (isValidCursor(cursor))
678 return findIndex(pos, cursor);
686 * traverse sequence from the start counting gaps; make a note of
687 * the column of the first residue to save in the cursor
689 while ((i < sequence.length) && (j <= end) && (j <= pos))
691 if (!Comparison.isGap(sequence[i]))
702 if (j == end && j < pos)
707 updateCursor(pos, i, startColumn);
712 * Updates the cursor to the latest found residue and column position
719 * column position of the first sequence residue
721 protected void updateCursor(int residuePos, int column, int startColumn)
724 * preserve end residue column provided cursor was valid
726 int endColumn = isValidCursor(cursor) ? cursor.lastResidueColumn : 0;
727 if (residuePos == this.end)
732 cursor = new SequenceCursor(this, residuePos, column, startColumn,
733 endColumn, this.changeCount);
737 * Answers the aligned column position (1..) for the given residue position
738 * (start..) given a 'hint' of a residue/column location in the neighbourhood.
739 * The hint may be left of, at, or to the right of the required position.
745 protected int findIndex(int pos, SequenceCursor curs)
747 if (!isValidCursor(curs))
750 * wrong or invalidated cursor, compute de novo
752 return findIndex(pos);
755 if (curs.residuePosition == pos)
757 return curs.columnPosition;
761 * move left or right to find pos from hint.position
763 int col = curs.columnPosition - 1; // convert from base 1 to base 0
764 int newPos = curs.residuePosition;
765 int delta = newPos > pos ? -1 : 1;
767 while (newPos != pos)
769 col += delta; // shift one column left or right
770 if (col < 0 || col == sequence.length)
774 if (!Comparison.isGap(sequence[col]))
780 col++; // convert back to base 1
781 updateCursor(pos, col, curs.firstResidueColumn);
790 public Range findPositions(int fromColumn, int toColumn)
792 if (toColumn < fromColumn || fromColumn < 1)
798 * find the first non-gapped position, if any
800 int firstPosition = 0;
801 int col = fromColumn - 1;
802 int length = sequence.length;
803 while (col < length && col < toColumn)
805 if (!Comparison.isGap(sequence[col]))
807 firstPosition = findPosition(col++);
813 if (firstPosition == 0)
819 * find the last non-gapped position
821 int lastPosition = firstPosition;
822 while (col < length && col < toColumn)
824 if (!Comparison.isGap(sequence[col++]))
830 return new Range(firstPosition, lastPosition);
837 public int findPosition(final int column)
840 * use a valid, hopefully nearby, cursor if available
842 if (isValidCursor(cursor))
844 return findPosition(column + 1, cursor);
847 // TODO recode this more naturally i.e. count residues only
848 // as they are found, not 'in anticipation'
851 * traverse the sequence counting gaps; note the column position
852 * of the first residue, to save in the cursor
854 int firstResidueColumn = 0;
855 int lastPosFound = 0;
856 int lastPosFoundColumn = 0;
857 int seqlen = sequence.length;
859 if (seqlen > 0 && !Comparison.isGap(sequence[0]))
861 lastPosFound = start;
862 lastPosFoundColumn = 0;
868 while (j < column && j < seqlen)
870 if (!Comparison.isGap(sequence[j]))
873 lastPosFoundColumn = j;
874 if (pos == this.start)
876 firstResidueColumn = j;
882 if (j < seqlen && !Comparison.isGap(sequence[j]))
885 lastPosFoundColumn = j;
886 if (pos == this.start)
888 firstResidueColumn = j;
893 * update the cursor to the last residue position found (if any)
894 * (converting column position to base 1)
896 if (lastPosFound != 0)
898 updateCursor(lastPosFound, lastPosFoundColumn + 1,
899 firstResidueColumn + 1);
906 * Answers true if the given cursor is not null, is for this sequence object,
907 * and has a token value that matches this object's changeCount, else false.
908 * This allows us to ignore a cursor as 'stale' if the sequence has been
909 * modified since the cursor was created.
914 protected boolean isValidCursor(SequenceCursor curs)
916 if (curs == null || curs.sequence != this || curs.token != changeCount)
921 * sanity check against range
923 if (curs.columnPosition < 0 || curs.columnPosition > sequence.length)
927 if (curs.residuePosition < start || curs.residuePosition > end)
935 * Answers the sequence position (start..) for the given aligned column
936 * position (1..), given a hint of a cursor in the neighbourhood. The cursor
937 * may lie left of, at, or to the right of the column position.
943 protected int findPosition(final int col, SequenceCursor curs)
945 if (!isValidCursor(curs))
948 * wrong or invalidated cursor, compute de novo
950 return findPosition(col - 1);// ugh back to base 0
953 if (curs.columnPosition == col)
955 cursor = curs; // in case this method becomes public
956 return curs.residuePosition; // easy case :-)
959 if (curs.lastResidueColumn > 0 && curs.lastResidueColumn < col)
962 * sequence lies entirely to the left of col
963 * - return last residue + 1
968 if (curs.firstResidueColumn > 0 && curs.firstResidueColumn > col)
971 * sequence lies entirely to the right of col
972 * - return first residue
977 // todo could choose closest to col out of column,
978 // firstColumnPosition, lastColumnPosition as a start point
981 * move left or right to find pos from cursor position
983 int firstResidueColumn = curs.firstResidueColumn;
984 int column = curs.columnPosition - 1; // to base 0
985 int newPos = curs.residuePosition;
986 int delta = curs.columnPosition > col ? -1 : 1;
987 boolean gapped = false;
988 int lastFoundPosition = curs.residuePosition;
989 int lastFoundPositionColumn = curs.columnPosition;
991 while (column != col - 1)
993 column += delta; // shift one column left or right
994 if (column < 0 || column == sequence.length)
998 gapped = Comparison.isGap(sequence[column]);
1002 lastFoundPosition = newPos;
1003 lastFoundPositionColumn = column + 1;
1004 if (lastFoundPosition == this.start)
1006 firstResidueColumn = column + 1;
1011 if (cursor == null || lastFoundPosition != cursor.residuePosition)
1013 updateCursor(lastFoundPosition, lastFoundPositionColumn,
1014 firstResidueColumn);
1018 * hack to give position to the right if on a gap
1019 * or beyond the length of the sequence (see JAL-2562)
1021 if (delta > 0 && (gapped || column >= sequence.length))
1030 * Returns an int array where indices correspond to each residue in the
1031 * sequence and the element value gives its position in the alignment
1033 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
1034 * residues in SequenceI object
1037 public int[] gapMap()
1039 String seq = jalview.analysis.AlignSeq.extractGaps(
1040 jalview.util.Comparison.GapChars, new String(sequence));
1041 int[] map = new int[seq.length()];
1045 while (j < sequence.length)
1047 if (!jalview.util.Comparison.isGap(sequence[j]))
1059 public int[] findPositionMap()
1061 int map[] = new int[sequence.length];
1064 int seqlen = sequence.length;
1065 while ((j < seqlen))
1068 if (!jalview.util.Comparison.isGap(sequence[j]))
1079 public List<int[]> getInsertions()
1081 ArrayList<int[]> map = new ArrayList<int[]>();
1082 int lastj = -1, j = 0;
1084 int seqlen = sequence.length;
1085 while ((j < seqlen))
1087 if (jalview.util.Comparison.isGap(sequence[j]))
1098 map.add(new int[] { lastj, j - 1 });
1106 map.add(new int[] { lastj, j - 1 });
1113 public BitSet getInsertionsAsBits()
1115 BitSet map = new BitSet();
1116 int lastj = -1, j = 0;
1118 int seqlen = sequence.length;
1119 while ((j < seqlen))
1121 if (jalview.util.Comparison.isGap(sequence[j]))
1147 public void deleteChars(int i, int j)
1149 int newstart = start, newend = end;
1150 if (i >= sequence.length || i < 0)
1155 char[] tmp = StringUtils.deleteChars(sequence, i, j);
1156 boolean createNewDs = false;
1157 // TODO: take a (second look) at the dataset creation validation method for
1158 // the very large sequence case
1159 int eindex = -1, sindex = -1;
1160 boolean ecalc = false, scalc = false;
1161 for (int s = i; s < j && s < sequence.length; s++)
1163 if (!Comparison.isGap(sequence[s]))
1173 sindex = findIndex(start) - 1;
1178 // delete characters including start of sequence
1179 newstart = findPosition(j);
1180 break; // don't need to search for any more residue characters.
1184 // delete characters after start.
1187 eindex = findIndex(end) - 1;
1192 // delete characters at end of sequence
1193 newend = findPosition(i - 1);
1194 break; // don't need to search for any more residue characters.
1199 newend--; // decrease end position by one for the deleted residue
1200 // and search further
1206 // deletion occured in the middle of the sequence
1207 if (createNewDs && this.datasetSequence != null)
1209 // construct a new sequence
1210 Sequence ds = new Sequence(datasetSequence);
1211 // TODO: remove any non-inheritable properties ?
1212 // TODO: create a sequence mapping (since there is a relation here ?)
1213 ds.deleteChars(i, j);
1214 datasetSequence = ds;
1223 public void insertCharAt(int i, int length, char c)
1225 char[] tmp = new char[sequence.length + length];
1227 if (i >= sequence.length)
1229 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
1230 i = sequence.length;
1234 System.arraycopy(sequence, 0, tmp, 0, i);
1244 if (i < sequence.length)
1246 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
1254 public void insertCharAt(int i, char c)
1256 insertCharAt(i, 1, c);
1260 public String getVamsasId()
1266 public void setVamsasId(String id)
1272 public void setDBRefs(DBRefEntry[] dbref)
1274 if (dbrefs == null && datasetSequence != null
1275 && this != datasetSequence)
1277 datasetSequence.setDBRefs(dbref);
1283 DBRefUtils.ensurePrimaries(this);
1288 public DBRefEntry[] getDBRefs()
1290 if (dbrefs == null && datasetSequence != null
1291 && this != datasetSequence)
1293 return datasetSequence.getDBRefs();
1299 public void addDBRef(DBRefEntry entry)
1301 if (datasetSequence != null)
1303 datasetSequence.addDBRef(entry);
1309 dbrefs = new DBRefEntry[0];
1312 for (DBRefEntryI dbr : dbrefs)
1314 if (dbr.updateFrom(entry))
1317 * found a dbref that either matched, or could be
1318 * updated from, the new entry - no need to add it
1325 * extend the array to make room for one more
1327 // TODO use an ArrayList instead
1328 int j = dbrefs.length;
1329 DBRefEntry[] temp = new DBRefEntry[j + 1];
1330 System.arraycopy(dbrefs, 0, temp, 0, j);
1331 temp[temp.length - 1] = entry;
1335 DBRefUtils.ensurePrimaries(this);
1339 public void setDatasetSequence(SequenceI seq)
1343 throw new IllegalArgumentException(
1344 "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
1346 if (seq != null && seq.getDatasetSequence() != null)
1348 throw new IllegalArgumentException(
1349 "Implementation error: cascading dataset sequences are not allowed.");
1351 datasetSequence = seq;
1355 public SequenceI getDatasetSequence()
1357 return datasetSequence;
1361 public AlignmentAnnotation[] getAnnotation()
1363 return annotation == null ? null : annotation
1364 .toArray(new AlignmentAnnotation[annotation.size()]);
1368 public boolean hasAnnotation(AlignmentAnnotation ann)
1370 return annotation == null ? false : annotation.contains(ann);
1374 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
1376 if (this.annotation == null)
1378 this.annotation = new Vector<AlignmentAnnotation>();
1380 if (!this.annotation.contains(annotation))
1382 this.annotation.addElement(annotation);
1384 annotation.setSequenceRef(this);
1388 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1390 if (this.annotation != null)
1392 this.annotation.removeElement(annotation);
1393 if (this.annotation.size() == 0)
1395 this.annotation = null;
1401 * test if this is a valid candidate for another sequence's dataset sequence.
1404 private boolean isValidDatasetSequence()
1406 if (datasetSequence != null)
1410 for (int i = 0; i < sequence.length; i++)
1412 if (jalview.util.Comparison.isGap(sequence[i]))
1421 public SequenceI deriveSequence()
1423 Sequence seq = null;
1424 if (datasetSequence == null)
1426 if (isValidDatasetSequence())
1428 // Use this as dataset sequence
1429 seq = new Sequence(getName(), "", 1, -1);
1430 seq.setDatasetSequence(this);
1431 seq.initSeqFrom(this, getAnnotation());
1436 // Create a new, valid dataset sequence
1437 createDatasetSequence();
1440 return new Sequence(this);
1443 private boolean _isNa;
1445 private int _seqhash = 0;
1448 * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
1452 public boolean isProtein()
1454 if (datasetSequence != null)
1456 return datasetSequence.isProtein();
1458 if (_seqhash != sequence.hashCode())
1460 _seqhash = sequence.hashCode();
1461 _isNa = Comparison.isNucleotide(this);
1469 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1472 public SequenceI createDatasetSequence()
1474 if (datasetSequence == null)
1476 Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps(
1477 jalview.util.Comparison.GapChars, getSequenceAsString()),
1478 getStart(), getEnd());
1480 datasetSequence = dsseq;
1482 dsseq.setDescription(description);
1483 // move features and database references onto dataset sequence
1484 dsseq.sequenceFeatureStore = sequenceFeatureStore;
1485 sequenceFeatureStore = null;
1486 dsseq.dbrefs = dbrefs;
1488 // TODO: search and replace any references to this sequence with
1489 // references to the dataset sequence in Mappings on dbref
1490 dsseq.pdbIds = pdbIds;
1492 datasetSequence.updatePDBIds();
1493 if (annotation != null)
1495 // annotation is cloned rather than moved, to preserve what's currently
1497 for (AlignmentAnnotation aa : annotation)
1499 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1500 _aa.sequenceRef = datasetSequence;
1501 _aa.adjustForAlignment(); // uses annotation's own record of
1502 // sequence-column mapping
1503 datasetSequence.addAlignmentAnnotation(_aa);
1507 return datasetSequence;
1514 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1518 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1520 if (annotation != null)
1522 annotation.removeAllElements();
1524 if (annotations != null)
1526 for (int i = 0; i < annotations.length; i++)
1528 if (annotations[i] != null)
1530 addAlignmentAnnotation(annotations[i]);
1537 public AlignmentAnnotation[] getAnnotation(String label)
1539 if (annotation == null || annotation.size() == 0)
1544 Vector<AlignmentAnnotation> subset = new Vector<AlignmentAnnotation>();
1545 Enumeration<AlignmentAnnotation> e = annotation.elements();
1546 while (e.hasMoreElements())
1548 AlignmentAnnotation ann = e.nextElement();
1549 if (ann.label != null && ann.label.equals(label))
1551 subset.addElement(ann);
1554 if (subset.size() == 0)
1558 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1560 e = subset.elements();
1561 while (e.hasMoreElements())
1563 anns[i++] = e.nextElement();
1565 subset.removeAllElements();
1570 public boolean updatePDBIds()
1572 if (datasetSequence != null)
1574 // TODO: could merge DBRefs
1575 return datasetSequence.updatePDBIds();
1577 if (dbrefs == null || dbrefs.length == 0)
1581 boolean added = false;
1582 for (DBRefEntry dbr : dbrefs)
1584 if (DBRefSource.PDB.equals(dbr.getSource()))
1587 * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the
1588 * PDB id is not already present in a 'matching' PDBEntry
1589 * Constructor parses out a chain code if appended to the accession id
1590 * (a fudge used to 'store' the chain code in the DBRef)
1592 PDBEntry pdbe = new PDBEntry(dbr);
1593 added |= addPDBId(pdbe);
1600 public void transferAnnotation(SequenceI entry, Mapping mp)
1602 if (datasetSequence != null)
1604 datasetSequence.transferAnnotation(entry, mp);
1607 if (entry.getDatasetSequence() != null)
1609 transferAnnotation(entry.getDatasetSequence(), mp);
1612 // transfer any new features from entry onto sequence
1613 if (entry.getSequenceFeatures() != null)
1616 List<SequenceFeature> sfs = entry.getSequenceFeatures();
1617 for (SequenceFeature feature : sfs)
1619 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
1620 : new SequenceFeature[] { new SequenceFeature(feature) };
1623 for (int sfi = 0; sfi < sf.length; sfi++)
1625 addSequenceFeature(sf[sfi]);
1631 // transfer PDB entries
1632 if (entry.getAllPDBEntries() != null)
1634 Enumeration<PDBEntry> e = entry.getAllPDBEntries().elements();
1635 while (e.hasMoreElements())
1637 PDBEntry pdb = e.nextElement();
1641 // transfer database references
1642 DBRefEntry[] entryRefs = entry.getDBRefs();
1643 if (entryRefs != null)
1645 for (int r = 0; r < entryRefs.length; r++)
1647 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1648 if (newref.getMap() != null && mp != null)
1650 // remap ref using our local mapping
1652 // we also assume all version string setting is done by dbSourceProxy
1654 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1655 * newref.setSource(dbSource); }
1663 * @return The index (zero-based) on this sequence in the MSA. It returns
1664 * {@code -1} if this information is not available.
1667 public int getIndex()
1673 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1674 * if this information is undefined.
1677 * position for this sequence. This value is zero-based (zero for
1678 * this first sequence)
1681 public void setIndex(int value)
1687 public void setRNA(RNA r)
1699 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1702 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1703 if (this.annotation != null)
1705 for (AlignmentAnnotation ann : annotation)
1707 if (ann.calcId != null && ann.calcId.equals(calcId)
1708 && ann.label != null && ann.label.equals(label))
1718 public String toString()
1720 return getDisplayId(false);
1724 public PDBEntry getPDBEntry(String pdbIdStr)
1726 if (getDatasetSequence() != null)
1728 return getDatasetSequence().getPDBEntry(pdbIdStr);
1734 List<PDBEntry> entries = getAllPDBEntries();
1735 for (PDBEntry entry : entries)
1737 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1746 public List<DBRefEntry> getPrimaryDBRefs()
1748 if (datasetSequence != null)
1750 return datasetSequence.getPrimaryDBRefs();
1752 if (dbrefs == null || dbrefs.length == 0)
1754 return Collections.emptyList();
1756 synchronized (dbrefs)
1758 List<DBRefEntry> primaries = new ArrayList<DBRefEntry>();
1759 DBRefEntry[] tmp = new DBRefEntry[1];
1760 for (DBRefEntry ref : dbrefs)
1762 if (!ref.isPrimaryCandidate())
1768 MapList mp = ref.getMap().getMap();
1769 if (mp.getFromLowest() > start || mp.getFromHighest() < end)
1771 // map only involves a subsequence, so cannot be primary
1775 // whilst it looks like it is a primary ref, we also sanity check type
1776 if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals(
1777 DBRefUtils.getCanonicalName(ref.getSource())))
1779 // PDB dbrefs imply there should be a PDBEntry associated
1780 // TODO: tighten PDB dbrefs
1781 // formally imply Jalview has actually downloaded and
1782 // parsed the pdb file. That means there should be a cached file
1783 // handle on the PDBEntry, and a real mapping between sequence and
1784 // extracted sequence from PDB file
1785 PDBEntry pdbentry = getPDBEntry(ref.getAccessionId());
1786 if (pdbentry != null && pdbentry.getFile() != null)
1792 // check standard protein or dna sources
1794 DBRefEntry[] res = DBRefUtils.selectDbRefs(!isProtein(), tmp);
1795 if (res != null && res[0] == tmp[0])
1809 public List<SequenceFeature> findFeatures(int fromColumn, int toColumn,
1812 int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
1813 int endPos = findPosition(toColumn - 1);
1815 List<SequenceFeature> result = new ArrayList<>();
1816 if (datasetSequence != null)
1818 result = datasetSequence.getFeatures().findFeatures(startPos, endPos,
1823 result = sequenceFeatureStore.findFeatures(startPos, endPos, types);
1827 * if the start or end column is gapped, startPos or endPos may be to the
1828 * left or right, and we may have included adjacent or enclosing features;
1829 * remove any that are not enclosing, non-contact features
1831 if (endPos > this.end || Comparison.isGap(sequence[fromColumn - 1])
1832 || Comparison.isGap(sequence[toColumn - 1]))
1834 ListIterator<SequenceFeature> it = result.listIterator();
1835 while (it.hasNext())
1837 SequenceFeature sf = it.next();
1838 int featureStartColumn = findIndex(sf.getBegin());
1839 int featureEndColumn = findIndex(sf.getEnd());
1840 boolean noOverlap = featureStartColumn > toColumn
1841 || featureEndColumn < fromColumn;
1844 * reject an 'enclosing' feature if it is actually a contact feature
1846 if (sf.isContactFeature() && featureStartColumn < fromColumn
1847 && featureEndColumn > toColumn)
1862 * Invalidates any stale cursors (forcing recalculation) by incrementing the
1863 * token that has to match the one presented by the cursor
1866 public void sequenceChanged()
1875 public int replace(char c1, char c2)
1882 synchronized (sequence)
1884 for (int c = 0; c < sequence.length; c++)
1886 if (sequence[c] == c1)