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.
52 public class Sequence extends ASequence implements SequenceI
54 private static final Regex limitrx = new Regex(
55 "[/][0-9]{1,}[-][0-9]{1,}$");
57 private static final Regex endrx = new Regex("[0-9]{1,}$");
59 SequenceI datasetSequence;
63 private char[] sequence;
71 Vector<PDBEntry> pdbIds;
80 * This annotation is displayed below the alignment but the positions are tied
81 * to the residues of this sequence
83 * TODO: change to List<>
85 Vector<AlignmentAnnotation> annotation;
88 * The index of the sequence in a MSA
92 private SequenceFeatures sequenceFeatureStore;
95 * A cursor holding the approximate current view position to the sequence,
96 * as determined by findIndex or findPosition or findPositions.
97 * Using a cursor as a hint allows these methods to be more performant for
100 private SequenceCursor cursor;
103 * A number that should be incremented whenever the sequence is edited.
104 * If the value matches the cursor token, then we can trust the cursor,
105 * if not then it should be recomputed.
107 private int changeCount;
110 * Creates a new Sequence object.
113 * display name string
115 * string to form a possibly gapped sequence out of
117 * first position of non-gap residue in the sequence
119 * last position of ungapped residues (nearly always only used for
122 public Sequence(String name, String sequence, int start, int end)
125 initSeqAndName(name, sequence.toCharArray(), start, end);
128 public Sequence(String name, char[] sequence, int start, int end)
131 initSeqAndName(name, sequence, start, end);
135 * Stage 1 constructor - assign name, sequence, and set start and end fields.
136 * start and end are updated values from name2 if it ends with /start-end
143 protected void initSeqAndName(String name2, char[] sequence2, int start2,
147 this.sequence = sequence2;
159 "POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
162 // Does sequence have the /start-end signature?
163 if (limitrx.search(name))
165 name = limitrx.left();
166 endrx.search(limitrx.stringMatched());
167 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
168 endrx.matchedFrom() - 1)));
169 setEnd(Integer.parseInt(endrx.stringMatched()));
173 void checkValidRange()
176 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
179 for (int j = 0; j < sequence.length; j++)
181 if (!jalview.util.Comparison.isGap(sequence[j]))
200 * default constructor
204 sequenceFeatureStore = new SequenceFeatures();
208 * Creates a new Sequence object.
215 public Sequence(String name, String sequence)
217 this(name, sequence, 1, -1);
221 * Creates a new Sequence object with new AlignmentAnnotations but inherits
222 * any existing dataset sequence reference. If non exists, everything is
226 * if seq is a dataset sequence, behaves like a plain old copy
229 public Sequence(SequenceI seq)
231 this(seq, seq.getAnnotation());
235 * Create a new sequence object with new features, DBRefEntries, and PDBIds
236 * but inherits any existing dataset sequence reference, and duplicate of any
237 * annotation that is present in the given annotation array.
240 * the sequence to be copied
241 * @param alAnnotation
242 * an array of annotation including some associated with seq
244 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
247 initSeqFrom(seq, alAnnotation);
251 * does the heavy lifting when cloning a dataset sequence, or coping data from
252 * dataset to a new derived sequence.
255 * - source of attributes.
256 * @param alAnnotation
257 * - alignment annotation present on seq that should be copied onto
260 protected void initSeqFrom(SequenceI seq,
261 AlignmentAnnotation[] alAnnotation)
263 char[] oseq = seq.getSequence(); // returns a copy of the array
264 initSeqAndName(seq.getName(), oseq, seq.getStart(), seq.getEnd());
266 description = seq.getDescription();
267 if (seq != datasetSequence)
269 setDatasetSequence(seq.getDatasetSequence());
273 * only copy DBRefs and seqfeatures if we really are a dataset sequence
275 if (datasetSequence == null)
277 if (seq.getDBRefs() != null)
279 DBRefEntry[] dbr = seq.getDBRefs();
280 for (int i = 0; i < dbr.length; i++)
282 addDBRef(new DBRefEntry(dbr[i]));
287 * make copies of any sequence features
289 for (SequenceFeature sf : seq.getSequenceFeatures())
291 addSequenceFeature(new SequenceFeature(sf));
295 if (seq.getAnnotation() != null)
297 AlignmentAnnotation[] sqann = seq.getAnnotation();
298 for (int i = 0; i < sqann.length; i++)
300 if (sqann[i] == null)
304 boolean found = (alAnnotation == null);
307 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
309 found = (alAnnotation[apos] == sqann[i]);
314 // only copy the given annotation
315 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
316 addAlignmentAnnotation(newann);
320 if (seq.getAllPDBEntries() != null)
322 Vector<PDBEntry> ids = seq.getAllPDBEntries();
323 for (PDBEntry pdb : ids)
325 this.addPDBId(new PDBEntry(pdb));
331 public void setSequenceFeatures(List<SequenceFeature> features)
333 if (datasetSequence != null)
335 datasetSequence.setSequenceFeatures(features);
338 sequenceFeatureStore = new SequenceFeatures(features);
342 public synchronized boolean addSequenceFeature(SequenceFeature sf)
344 if (sf.getType() == null)
346 System.err.println("SequenceFeature type may not be null: "
351 if (datasetSequence != null)
353 return datasetSequence.addSequenceFeature(sf);
356 return sequenceFeatureStore.add(sf);
360 public void deleteFeature(SequenceFeature sf)
362 if (datasetSequence != null)
364 datasetSequence.deleteFeature(sf);
368 sequenceFeatureStore.delete(sf);
378 public List<SequenceFeature> getSequenceFeatures()
380 if (datasetSequence != null)
382 return datasetSequence.getSequenceFeatures();
384 return sequenceFeatureStore.getAllFeatures();
388 public SequenceFeaturesI getFeatures()
390 return datasetSequence != null ? datasetSequence.getFeatures()
391 : sequenceFeatureStore;
395 public boolean addPDBId(PDBEntry entry)
399 pdbIds = new Vector<>();
404 for (PDBEntry pdbe : pdbIds)
406 if (pdbe.updateFrom(entry))
411 pdbIds.addElement(entry);
422 public void setPDBId(Vector<PDBEntry> id)
430 * @return DOCUMENT ME!
433 public Vector<PDBEntry> getAllPDBEntries()
435 return pdbIds == null ? new Vector<PDBEntry>() : pdbIds;
441 * @return DOCUMENT ME!
444 public String getDisplayId(boolean jvsuffix)
446 StringBuffer result = new StringBuffer(name);
449 result.append("/" + start + "-" + end);
452 return result.toString();
462 public void setName(String name)
471 * @return DOCUMENT ME!
474 public String getName()
486 public void setStart(int start)
494 * @return DOCUMENT ME!
497 public int getStart()
509 public void setEnd(int end)
517 * @return DOCUMENT ME!
528 * @return DOCUMENT ME!
531 public int getLength()
533 return this.sequence.length;
543 public void setSequence(String seq)
545 this.sequence = seq.toCharArray();
551 public String getSequenceAsString()
553 return new String(sequence);
557 public String getSequenceAsString(int start, int end)
559 return new String(getSequence(start, end));
563 public char[] getSequence()
566 return sequence == null ? null : Arrays.copyOf(sequence,
573 * @see jalview.datamodel.SequenceI#getSequence(int, int)
576 public char[] getSequence(int start, int end)
582 // JBPNote - left to user to pad the result here (TODO:Decide on this
584 if (start >= sequence.length)
589 if (end >= sequence.length)
591 end = sequence.length;
594 char[] reply = new char[end - start];
595 System.arraycopy(sequence, start, reply, 0, end - start);
601 public SequenceI getSubSequence(int start, int end)
607 char[] seq = getSequence(start, end);
612 int nstart = findPosition(start);
613 int nend = findPosition(end) - 1;
614 // JBPNote - this is an incomplete copy.
615 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
616 nseq.setDescription(description);
617 if (datasetSequence != null)
619 nseq.setDatasetSequence(datasetSequence);
623 nseq.setDatasetSequence(this);
629 * Returns the character of the aligned sequence at the given position (base
630 * zero), or space if the position is not within the sequence's bounds
635 public char getCharAt(int i)
637 if (i >= 0 && i < sequence.length)
654 public void setDescription(String desc)
656 this.description = desc;
662 * @return DOCUMENT ME!
665 public String getDescription()
667 return this.description;
674 public int findIndex(int pos)
677 * use a valid, hopefully nearby, cursor if available
679 if (isValidCursor(cursor))
681 return findIndex(pos, cursor);
689 * traverse sequence from the start counting gaps; make a note of
690 * the column of the first residue to save in the cursor
692 while ((i < sequence.length) && (j <= end) && (j <= pos))
694 if (!Comparison.isGap(sequence[i]))
705 if (j == end && j < pos)
710 updateCursor(pos, i, startColumn);
715 * Updates the cursor to the latest found residue and column position
722 * column position of the first sequence residue
724 protected void updateCursor(int residuePos, int column, int startColumn)
727 * preserve end residue column provided cursor was valid
729 int endColumn = isValidCursor(cursor) ? cursor.lastColumnPosition : 0;
730 if (residuePos == this.end)
735 cursor = new SequenceCursor(this, residuePos, column, startColumn,
736 endColumn, this.changeCount);
740 * Answers the aligned column position (1..) for the given residue position
741 * (start..) given a 'hint' of a residue/column location in the neighbourhood.
742 * The hint may be left of, at, or to the right of the required position.
748 protected int findIndex(int pos, SequenceCursor curs)
750 if (!isValidCursor(curs))
753 * wrong or invalidated cursor, compute de novo
755 return findIndex(pos);
758 if (curs.residuePosition == pos)
760 return curs.columnPosition;
764 * move left or right to find pos from hint.position
766 int col = curs.columnPosition - 1; // convert from base 1 to 0-based array
768 int newPos = curs.residuePosition;
769 int delta = newPos > pos ? -1 : 1;
771 while (newPos != pos)
773 col += delta; // shift one column left or right
774 if (col < 0 || col == sequence.length)
778 if (!Comparison.isGap(sequence[col]))
784 col++; // convert back to base 1
785 updateCursor(pos, col, curs.firstColumnPosition);
794 public int findPosition(final int column)
797 * use a valid, hopefully nearby, cursor if available
799 if (isValidCursor(cursor))
801 return findPosition(column + 1, cursor);
804 // TODO recode this more naturally i.e. count residues only
805 // as they are found, not 'in anticipation'
808 * traverse the sequence counting gaps; note the column position
809 * of the first residue, to save in the cursor
811 int firstResidueColumn = 0;
812 int lastPosFound = 0;
813 int lastPosFoundColumn = 0;
814 int seqlen = sequence.length;
816 if (seqlen > 0 && !Comparison.isGap(sequence[0]))
818 lastPosFound = start;
819 lastPosFoundColumn = 0;
825 while (j < column && j < seqlen)
827 if (!Comparison.isGap(sequence[j]))
830 lastPosFoundColumn = j;
831 if (pos == this.start)
833 firstResidueColumn = j;
839 if (j < seqlen && !Comparison.isGap(sequence[j]))
842 lastPosFoundColumn = j;
843 if (pos == this.start)
845 firstResidueColumn = j;
850 * update the cursor to the last residue position found (if any)
851 * (converting column position to base 1)
853 if (lastPosFound != 0)
855 updateCursor(lastPosFound, lastPosFoundColumn + 1,
856 firstResidueColumn + 1);
863 * Answers true if the given cursor is not null, is for this sequence object,
864 * and has a token value that matches this object's changeCount, else false.
865 * This allows us to ignore a cursor as 'stale' if the sequence has been
866 * modified since the cursor was created.
871 protected boolean isValidCursor(SequenceCursor curs)
873 if (curs == null || curs.sequence != this || curs.token != changeCount)
878 * sanity check against range
880 if (curs.columnPosition < 0 || curs.columnPosition > sequence.length)
884 if (curs.residuePosition < start || curs.residuePosition > end)
892 * Answers the sequence position (start..) for the given aligned column
893 * position (1..), given a hint of a cursor in the neighbourhood. The cursor
894 * may lie left of, at, or to the right of the column position.
900 protected int findPosition(final int col, SequenceCursor curs)
902 if (!isValidCursor(curs))
905 * wrong or invalidated cursor, compute de novo
907 return findPosition(col - 1);// ugh back to base 0
910 if (curs.columnPosition == col)
912 cursor = curs; // in case this method becomes public
913 return curs.residuePosition; // easy case :-)
916 if (curs.lastColumnPosition > 0 && curs.lastColumnPosition < col)
919 * sequence lies entirely to the left of col
920 * - return last residue + 1
925 if (curs.firstColumnPosition > 0 && curs.firstColumnPosition > col)
928 * sequence lies entirely to the right of col
929 * - return first residue
934 // todo could choose closest to col out of column,
935 // firstColumnPosition, lastColumnPosition as a start point
938 * move left or right to find pos from cursor position
940 int firstResidueColumn = curs.firstColumnPosition;
941 int column = curs.columnPosition - 1; // to base 0
942 int newPos = curs.residuePosition;
943 int delta = curs.columnPosition > col ? -1 : 1;
944 boolean gapped = false;
945 int lastFoundPosition = curs.residuePosition;
946 int lastFoundPositionColumn = curs.columnPosition;
948 while (column != col - 1)
950 column += delta; // shift one column left or right
951 if (column < 0 || column == sequence.length)
955 gapped = Comparison.isGap(sequence[column]);
959 lastFoundPosition = newPos;
960 lastFoundPositionColumn = column + 1;
961 if (lastFoundPosition == this.start)
963 firstResidueColumn = column + 1;
968 if (cursor == null || lastFoundPosition != cursor.residuePosition)
970 updateCursor(lastFoundPosition, lastFoundPositionColumn,
975 * hack to give position to the right if on a gap
976 * or beyond the length of the sequence (see JAL-2562)
978 if (delta > 0 && (gapped || column >= sequence.length))
990 public Range findPositions(int fromColumn, int toColumn)
992 if (toColumn < fromColumn || fromColumn < 1)
998 * find the first non-gapped position, if any
1000 int firstPosition = 0;
1001 int col = fromColumn - 1;
1002 int length = sequence.length;
1003 while (col < length && col < toColumn)
1005 if (!Comparison.isGap(sequence[col]))
1007 firstPosition = findPosition(col++);
1013 if (firstPosition == 0)
1019 * find the last non-gapped position
1021 int lastPosition = firstPosition;
1022 while (col < length && col < toColumn)
1024 if (!Comparison.isGap(sequence[col++]))
1030 return new Range(firstPosition, lastPosition);
1034 * Returns an int array where indices correspond to each residue in the
1035 * sequence and the element value gives its position in the alignment
1037 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
1038 * residues in SequenceI object
1041 public int[] gapMap()
1043 String seq = jalview.analysis.AlignSeq.extractGaps(
1044 jalview.util.Comparison.GapChars, new String(sequence));
1045 int[] map = new int[seq.length()];
1049 while (j < sequence.length)
1051 if (!jalview.util.Comparison.isGap(sequence[j]))
1063 * Build a bitset corresponding to sequence gaps
1065 * @return a BitSet where set values correspond to gaps in the sequence
1068 public BitSet gapBitset()
1070 BitSet gaps = new BitSet(sequence.length);
1072 while (j < sequence.length)
1074 if (jalview.util.Comparison.isGap(sequence[j]))
1084 public int[] findPositionMap()
1086 int map[] = new int[sequence.length];
1089 int seqlen = sequence.length;
1090 while ((j < seqlen))
1093 if (!jalview.util.Comparison.isGap(sequence[j]))
1104 public List<int[]> getInsertions()
1106 ArrayList<int[]> map = new ArrayList<>();
1107 int lastj = -1, j = 0;
1109 int seqlen = sequence.length;
1110 while ((j < seqlen))
1112 if (jalview.util.Comparison.isGap(sequence[j]))
1123 map.add(new int[] { lastj, j - 1 });
1131 map.add(new int[] { lastj, j - 1 });
1138 public BitSet getInsertionsAsBits()
1140 BitSet map = new BitSet();
1141 int lastj = -1, j = 0;
1143 int seqlen = sequence.length;
1144 while ((j < seqlen))
1146 if (jalview.util.Comparison.isGap(sequence[j]))
1172 public void deleteChars(int i, int j)
1174 int newstart = start, newend = end;
1175 if (i >= sequence.length || i < 0)
1180 char[] tmp = StringUtils.deleteChars(sequence, i, j);
1181 boolean createNewDs = false;
1182 // TODO: take a (second look) at the dataset creation validation method for
1183 // the very large sequence case
1184 int eindex = -1, sindex = -1;
1185 boolean ecalc = false, scalc = false;
1186 for (int s = i; s < j; s++)
1188 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
1198 sindex = findIndex(start) - 1;
1203 // delete characters including start of sequence
1204 newstart = findPosition(j);
1205 break; // don't need to search for any more residue characters.
1209 // delete characters after start.
1212 eindex = findIndex(end) - 1;
1217 // delete characters at end of sequence
1218 newend = findPosition(i - 1);
1219 break; // don't need to search for any more residue characters.
1224 newend--; // decrease end position by one for the deleted residue
1225 // and search further
1231 // deletion occured in the middle of the sequence
1232 if (createNewDs && this.datasetSequence != null)
1234 // construct a new sequence
1235 Sequence ds = new Sequence(datasetSequence);
1236 // TODO: remove any non-inheritable properties ?
1237 // TODO: create a sequence mapping (since there is a relation here ?)
1238 ds.deleteChars(i, j);
1239 datasetSequence = ds;
1248 public void insertCharAt(int i, int length, char c)
1250 char[] tmp = new char[sequence.length + length];
1252 if (i >= sequence.length)
1254 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
1255 i = sequence.length;
1259 System.arraycopy(sequence, 0, tmp, 0, i);
1269 if (i < sequence.length)
1271 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
1279 public void insertCharAt(int i, char c)
1281 insertCharAt(i, 1, c);
1285 public String getVamsasId()
1291 public void setVamsasId(String id)
1297 public void setDBRefs(DBRefEntry[] dbref)
1299 if (dbrefs == null && datasetSequence != null
1300 && this != datasetSequence)
1302 datasetSequence.setDBRefs(dbref);
1308 DBRefUtils.ensurePrimaries(this);
1313 public DBRefEntry[] getDBRefs()
1315 if (dbrefs == null && datasetSequence != null
1316 && this != datasetSequence)
1318 return datasetSequence.getDBRefs();
1324 public void addDBRef(DBRefEntry entry)
1326 if (datasetSequence != null)
1328 datasetSequence.addDBRef(entry);
1334 dbrefs = new DBRefEntry[0];
1337 for (DBRefEntryI dbr : dbrefs)
1339 if (dbr.updateFrom(entry))
1342 * found a dbref that either matched, or could be
1343 * updated from, the new entry - no need to add it
1350 * extend the array to make room for one more
1352 // TODO use an ArrayList instead
1353 int j = dbrefs.length;
1354 DBRefEntry[] temp = new DBRefEntry[j + 1];
1355 System.arraycopy(dbrefs, 0, temp, 0, j);
1356 temp[temp.length - 1] = entry;
1360 DBRefUtils.ensurePrimaries(this);
1364 public void setDatasetSequence(SequenceI seq)
1368 throw new IllegalArgumentException(
1369 "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
1371 if (seq != null && seq.getDatasetSequence() != null)
1373 throw new IllegalArgumentException(
1374 "Implementation error: cascading dataset sequences are not allowed.");
1376 datasetSequence = seq;
1380 public SequenceI getDatasetSequence()
1382 return datasetSequence;
1386 public AlignmentAnnotation[] getAnnotation()
1388 return annotation == null ? null
1390 .toArray(new AlignmentAnnotation[annotation.size()]);
1394 public boolean hasAnnotation(AlignmentAnnotation ann)
1396 return annotation == null ? false : annotation.contains(ann);
1400 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
1402 if (this.annotation == null)
1404 this.annotation = new Vector<>();
1406 if (!this.annotation.contains(annotation))
1408 this.annotation.addElement(annotation);
1410 annotation.setSequenceRef(this);
1414 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1416 if (this.annotation != null)
1418 this.annotation.removeElement(annotation);
1419 if (this.annotation.size() == 0)
1421 this.annotation = null;
1427 * test if this is a valid candidate for another sequence's dataset sequence.
1430 private boolean isValidDatasetSequence()
1432 if (datasetSequence != null)
1436 for (int i = 0; i < sequence.length; i++)
1438 if (jalview.util.Comparison.isGap(sequence[i]))
1447 public SequenceI deriveSequence()
1449 Sequence seq = null;
1450 if (datasetSequence == null)
1452 if (isValidDatasetSequence())
1454 // Use this as dataset sequence
1455 seq = new Sequence(getName(), "", 1, -1);
1456 seq.setDatasetSequence(this);
1457 seq.initSeqFrom(this, getAnnotation());
1462 // Create a new, valid dataset sequence
1463 createDatasetSequence();
1466 return new Sequence(this);
1469 private boolean _isNa;
1471 private int _seqhash = 0;
1474 * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
1478 public boolean isProtein()
1480 if (datasetSequence != null)
1482 return datasetSequence.isProtein();
1484 if (_seqhash != sequence.hashCode())
1486 _seqhash = sequence.hashCode();
1487 _isNa = Comparison.isNucleotide(this);
1495 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1498 public SequenceI createDatasetSequence()
1500 if (datasetSequence == null)
1502 Sequence dsseq = new Sequence(getName(),
1503 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
1504 getSequenceAsString()),
1505 getStart(), getEnd());
1507 datasetSequence = dsseq;
1509 dsseq.setDescription(description);
1510 // move features and database references onto dataset sequence
1511 dsseq.sequenceFeatureStore = sequenceFeatureStore;
1512 sequenceFeatureStore = null;
1513 dsseq.dbrefs = dbrefs;
1515 // TODO: search and replace any references to this sequence with
1516 // references to the dataset sequence in Mappings on dbref
1517 dsseq.pdbIds = pdbIds;
1519 datasetSequence.updatePDBIds();
1520 if (annotation != null)
1522 // annotation is cloned rather than moved, to preserve what's currently
1524 for (AlignmentAnnotation aa : annotation)
1526 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1527 _aa.sequenceRef = datasetSequence;
1528 _aa.adjustForAlignment(); // uses annotation's own record of
1529 // sequence-column mapping
1530 datasetSequence.addAlignmentAnnotation(_aa);
1534 return datasetSequence;
1541 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1545 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1547 if (annotation != null)
1549 annotation.removeAllElements();
1551 if (annotations != null)
1553 for (int i = 0; i < annotations.length; i++)
1555 if (annotations[i] != null)
1557 addAlignmentAnnotation(annotations[i]);
1564 public AlignmentAnnotation[] getAnnotation(String label)
1566 if (annotation == null || annotation.size() == 0)
1571 Vector<AlignmentAnnotation> subset = new Vector<>();
1572 Enumeration<AlignmentAnnotation> e = annotation.elements();
1573 while (e.hasMoreElements())
1575 AlignmentAnnotation ann = e.nextElement();
1576 if (ann.label != null && ann.label.equals(label))
1578 subset.addElement(ann);
1581 if (subset.size() == 0)
1585 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1587 e = subset.elements();
1588 while (e.hasMoreElements())
1590 anns[i++] = e.nextElement();
1592 subset.removeAllElements();
1597 public boolean updatePDBIds()
1599 if (datasetSequence != null)
1601 // TODO: could merge DBRefs
1602 return datasetSequence.updatePDBIds();
1604 if (dbrefs == null || dbrefs.length == 0)
1608 boolean added = false;
1609 for (DBRefEntry dbr : dbrefs)
1611 if (DBRefSource.PDB.equals(dbr.getSource()))
1614 * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the
1615 * PDB id is not already present in a 'matching' PDBEntry
1616 * Constructor parses out a chain code if appended to the accession id
1617 * (a fudge used to 'store' the chain code in the DBRef)
1619 PDBEntry pdbe = new PDBEntry(dbr);
1620 added |= addPDBId(pdbe);
1627 public void transferAnnotation(SequenceI entry, Mapping mp)
1629 if (datasetSequence != null)
1631 datasetSequence.transferAnnotation(entry, mp);
1634 if (entry.getDatasetSequence() != null)
1636 transferAnnotation(entry.getDatasetSequence(), mp);
1639 // transfer any new features from entry onto sequence
1640 if (entry.getSequenceFeatures() != null)
1643 List<SequenceFeature> sfs = entry.getSequenceFeatures();
1644 for (SequenceFeature feature : sfs)
1646 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
1647 : new SequenceFeature[] { new SequenceFeature(feature) };
1650 for (int sfi = 0; sfi < sf.length; sfi++)
1652 addSequenceFeature(sf[sfi]);
1658 // transfer PDB entries
1659 if (entry.getAllPDBEntries() != null)
1661 Enumeration<PDBEntry> e = entry.getAllPDBEntries().elements();
1662 while (e.hasMoreElements())
1664 PDBEntry pdb = e.nextElement();
1668 // transfer database references
1669 DBRefEntry[] entryRefs = entry.getDBRefs();
1670 if (entryRefs != null)
1672 for (int r = 0; r < entryRefs.length; r++)
1674 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1675 if (newref.getMap() != null && mp != null)
1677 // remap ref using our local mapping
1679 // we also assume all version string setting is done by dbSourceProxy
1681 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1682 * newref.setSource(dbSource); }
1690 * @return The index (zero-based) on this sequence in the MSA. It returns
1691 * {@code -1} if this information is not available.
1694 public int getIndex()
1700 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1701 * if this information is undefined.
1704 * position for this sequence. This value is zero-based (zero for
1705 * this first sequence)
1708 public void setIndex(int value)
1714 public void setRNA(RNA r)
1726 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1729 List<AlignmentAnnotation> result = new ArrayList<>();
1730 if (this.annotation != null)
1732 for (AlignmentAnnotation ann : annotation)
1734 if (ann.calcId != null && ann.calcId.equals(calcId)
1735 && ann.label != null && ann.label.equals(label))
1745 public String toString()
1747 return getDisplayId(false);
1751 public PDBEntry getPDBEntry(String pdbIdStr)
1753 if (getDatasetSequence() != null)
1755 return getDatasetSequence().getPDBEntry(pdbIdStr);
1761 List<PDBEntry> entries = getAllPDBEntries();
1762 for (PDBEntry entry : entries)
1764 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1773 public List<DBRefEntry> getPrimaryDBRefs()
1775 if (datasetSequence != null)
1777 return datasetSequence.getPrimaryDBRefs();
1779 if (dbrefs == null || dbrefs.length == 0)
1781 return Collections.emptyList();
1783 synchronized (dbrefs)
1785 List<DBRefEntry> primaries = new ArrayList<>();
1786 DBRefEntry[] tmp = new DBRefEntry[1];
1787 for (DBRefEntry ref : dbrefs)
1789 if (!ref.isPrimaryCandidate())
1795 MapList mp = ref.getMap().getMap();
1796 if (mp.getFromLowest() > start || mp.getFromHighest() < end)
1798 // map only involves a subsequence, so cannot be primary
1802 // whilst it looks like it is a primary ref, we also sanity check type
1803 if (DBRefUtils.getCanonicalName(DBRefSource.PDB)
1804 .equals(DBRefUtils.getCanonicalName(ref.getSource())))
1806 // PDB dbrefs imply there should be a PDBEntry associated
1807 // TODO: tighten PDB dbrefs
1808 // formally imply Jalview has actually downloaded and
1809 // parsed the pdb file. That means there should be a cached file
1810 // handle on the PDBEntry, and a real mapping between sequence and
1811 // extracted sequence from PDB file
1812 PDBEntry pdbentry = getPDBEntry(ref.getAccessionId());
1813 if (pdbentry != null && pdbentry.getFile() != null)
1819 // check standard protein or dna sources
1821 DBRefEntry[] res = DBRefUtils.selectDbRefs(!isProtein(), tmp);
1822 if (res != null && res[0] == tmp[0])
1836 public List<SequenceFeature> findFeatures(int fromColumn, int toColumn,
1839 int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
1840 int endPos = fromColumn == toColumn ? startPos
1841 : findPosition(toColumn - 1);
1843 List<SequenceFeature> result = getFeatures().findFeatures(startPos,
1847 * if end column is gapped, endPos may be to the right,
1848 * and we may have included adjacent or enclosing features;
1849 * remove any that are not enclosing, non-contact features
1851 if (endPos > this.end || Comparison.isGap(sequence[toColumn - 1]))
1853 ListIterator<SequenceFeature> it = result.listIterator();
1854 while (it.hasNext())
1856 SequenceFeature sf = it.next();
1857 int sfBegin = sf.getBegin();
1858 int sfEnd = sf.getEnd();
1859 int featureStartColumn = findIndex(sfBegin);
1860 if (featureStartColumn > toColumn)
1864 else if (featureStartColumn < fromColumn)
1866 int featureEndColumn = sfEnd == sfBegin ? featureStartColumn
1868 if (featureEndColumn < fromColumn)
1872 else if (featureEndColumn > toColumn && sf.isContactFeature())
1875 * remove an enclosing feature if it is a contact feature
1887 * Invalidates any stale cursors (forcing recalculation) by incrementing the
1888 * token that has to match the one presented by the cursor
1891 public void sequenceChanged()
1900 public int replace(char c1, char c2)
1907 synchronized (sequence)
1909 for (int c = 0; c < sequence.length; c++)
1911 if (sequence[c] == c1)