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 fr.orsay.lri.varna.models.rna.RNA;
45 * Implements the SequenceI interface for a char[] based sequence object
47 public class Sequence extends ASequence implements SequenceI
49 SequenceI datasetSequence;
53 private char[] sequence;
61 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 private SequenceFeaturesI sequenceFeatureStore;
85 * A cursor holding the approximate current view position to the sequence,
86 * as determined by findIndex or findPosition or findPositions.
87 * Using a cursor as a hint allows these methods to be more performant for
90 private SequenceCursor cursor;
93 * A number that should be incremented whenever the sequence is edited.
94 * If the value matches the cursor token, then we can trust the cursor,
95 * if not then it should be recomputed.
97 private int changeCount;
100 * Creates a new Sequence object.
103 * display name string
105 * string to form a possibly gapped sequence out of
107 * first position of non-gap residue in the sequence
109 * last position of ungapped residues (nearly always only used for
112 public Sequence(String name, String sequence, int start, int end)
115 initSeqAndName(name, sequence.toCharArray(), start, end);
118 public Sequence(String name, char[] sequence, int start, int end)
121 initSeqAndName(name, sequence, start, end);
125 * Stage 1 constructor - assign name, sequence, and set start and end fields.
126 * start and end are updated values from name2 if it ends with /start-end
133 protected void initSeqAndName(String name2, char[] sequence2, int start2,
137 this.sequence = sequence2;
145 * If 'name' ends in /i-j, where i >= j > 0 are integers, extracts i and j as
146 * start and end respectively and removes the suffix from the name
153 "POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
156 int slashPos = name.lastIndexOf('/');
157 if (slashPos > -1 && slashPos < name.length() - 1)
159 String suffix = name.substring(slashPos + 1);
160 String[] range = suffix.split("-");
161 if (range.length == 2)
165 int from = Integer.valueOf(range[0]);
166 int to = Integer.valueOf(range[1]);
167 if (from > 0 && to >= from)
169 name = name.substring(0, slashPos);
174 } catch (NumberFormatException e)
176 // leave name unchanged if suffix is invalid
183 * Ensures that 'end' is not before the end of the sequence, that is,
184 * (end-start+1) is at least as long as the count of ungapped positions. Note
185 * that end is permitted to be beyond the end of the sequence data.
187 void checkValidRange()
190 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
193 for (int j = 0; j < sequence.length; j++)
195 if (!Comparison.isGap(sequence[j]))
214 * default constructor
218 sequenceFeatureStore = new SequenceFeatures();
222 * Creates a new Sequence object.
229 public Sequence(String name, String sequence)
231 this(name, sequence, 1, -1);
235 * Creates a new Sequence object with new AlignmentAnnotations but inherits
236 * any existing dataset sequence reference. If non exists, everything is
240 * if seq is a dataset sequence, behaves like a plain old copy
243 public Sequence(SequenceI seq)
245 this(seq, seq.getAnnotation());
249 * Create a new sequence object with new features, DBRefEntries, and PDBIds
250 * but inherits any existing dataset sequence reference, and duplicate of any
251 * annotation that is present in the given annotation array.
254 * the sequence to be copied
255 * @param alAnnotation
256 * an array of annotation including some associated with seq
258 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
261 initSeqFrom(seq, alAnnotation);
265 * does the heavy lifting when cloning a dataset sequence, or coping data from
266 * dataset to a new derived sequence.
269 * - source of attributes.
270 * @param alAnnotation
271 * - alignment annotation present on seq that should be copied onto
274 protected void initSeqFrom(SequenceI seq,
275 AlignmentAnnotation[] alAnnotation)
277 char[] oseq = seq.getSequence(); // returns a copy of the array
278 initSeqAndName(seq.getName(), oseq, seq.getStart(), seq.getEnd());
280 description = seq.getDescription();
281 if (seq != datasetSequence)
283 setDatasetSequence(seq.getDatasetSequence());
287 * only copy DBRefs and seqfeatures if we really are a dataset sequence
289 if (datasetSequence == null)
291 if (seq.getDBRefs() != null)
293 DBRefEntry[] dbr = seq.getDBRefs();
294 for (int i = 0; i < dbr.length; i++)
296 addDBRef(new DBRefEntry(dbr[i]));
301 * make copies of any sequence features
303 for (SequenceFeature sf : seq.getSequenceFeatures())
305 addSequenceFeature(new SequenceFeature(sf));
309 if (seq.getAnnotation() != null)
311 AlignmentAnnotation[] sqann = seq.getAnnotation();
312 for (int i = 0; i < sqann.length; i++)
314 if (sqann[i] == null)
318 boolean found = (alAnnotation == null);
321 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
323 found = (alAnnotation[apos] == sqann[i]);
328 // only copy the given annotation
329 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
330 addAlignmentAnnotation(newann);
334 if (seq.getAllPDBEntries() != null)
336 Vector<PDBEntry> ids = seq.getAllPDBEntries();
337 for (PDBEntry pdb : ids)
339 this.addPDBId(new PDBEntry(pdb));
345 public void setSequenceFeatures(List<SequenceFeature> features)
347 if (datasetSequence != null)
349 datasetSequence.setSequenceFeatures(features);
352 sequenceFeatureStore = new SequenceFeatures(features);
356 public synchronized boolean addSequenceFeature(SequenceFeature sf)
358 if (sf.getType() == null)
360 System.err.println("SequenceFeature type may not be null: "
365 if (datasetSequence != null)
367 return datasetSequence.addSequenceFeature(sf);
370 return sequenceFeatureStore.add(sf);
374 public void deleteFeature(SequenceFeature sf)
376 if (datasetSequence != null)
378 datasetSequence.deleteFeature(sf);
382 sequenceFeatureStore.delete(sf);
392 public List<SequenceFeature> getSequenceFeatures()
394 if (datasetSequence != null)
396 return datasetSequence.getSequenceFeatures();
398 return sequenceFeatureStore.getAllFeatures();
402 public SequenceFeaturesI getFeatures()
404 return datasetSequence != null ? datasetSequence.getFeatures()
405 : sequenceFeatureStore;
409 public boolean addPDBId(PDBEntry entry)
413 pdbIds = new Vector<PDBEntry>();
418 for (PDBEntry pdbe : pdbIds)
420 if (pdbe.updateFrom(entry))
425 pdbIds.addElement(entry);
436 public void setPDBId(Vector<PDBEntry> id)
444 * @return DOCUMENT ME!
447 public Vector<PDBEntry> getAllPDBEntries()
449 return pdbIds == null ? new Vector<PDBEntry>() : pdbIds;
455 * @return DOCUMENT ME!
458 public String getDisplayId(boolean jvsuffix)
460 StringBuffer result = new StringBuffer(name);
463 result.append("/" + start + "-" + end);
466 return result.toString();
470 * Sets the sequence name. If the name ends in /start-end, then the start-end
471 * values are parsed out and set, and the suffix is removed from the name.
476 public void setName(String theName)
485 * @return DOCUMENT ME!
488 public String getName()
500 public void setStart(int start)
508 * @return DOCUMENT ME!
511 public int getStart()
523 public void setEnd(int end)
531 * @return DOCUMENT ME!
542 * @return DOCUMENT ME!
545 public int getLength()
547 return this.sequence.length;
557 public void setSequence(String seq)
559 this.sequence = seq.toCharArray();
565 public String getSequenceAsString()
567 return new String(sequence);
571 public String getSequenceAsString(int start, int end)
573 return new String(getSequence(start, end));
577 public char[] getSequence()
580 return sequence == null ? null : Arrays.copyOf(sequence,
587 * @see jalview.datamodel.SequenceI#getSequence(int, int)
590 public char[] getSequence(int start, int end)
596 // JBPNote - left to user to pad the result here (TODO:Decide on this
598 if (start >= sequence.length)
603 if (end >= sequence.length)
605 end = sequence.length;
608 char[] reply = new char[end - start];
609 System.arraycopy(sequence, start, reply, 0, end - start);
615 public SequenceI getSubSequence(int start, int end)
621 char[] seq = getSequence(start, end);
626 int nstart = findPosition(start);
627 int nend = findPosition(end) - 1;
628 // JBPNote - this is an incomplete copy.
629 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
630 nseq.setDescription(description);
631 if (datasetSequence != null)
633 nseq.setDatasetSequence(datasetSequence);
637 nseq.setDatasetSequence(this);
643 * Returns the character of the aligned sequence at the given position (base
644 * zero), or space if the position is not within the sequence's bounds
649 public char getCharAt(int i)
651 if (i >= 0 && i < sequence.length)
668 public void setDescription(String desc)
670 this.description = desc;
676 * @return DOCUMENT ME!
679 public String getDescription()
681 return this.description;
688 public int findIndex(int pos)
691 * use a valid, hopefully nearby, cursor if available
693 if (isValidCursor(cursor))
695 return findIndex(pos, cursor);
703 * traverse sequence from the start counting gaps; make a note of
704 * the column of the first residue to save in the cursor
706 while ((i < sequence.length) && (j <= end) && (j <= pos))
708 if (!Comparison.isGap(sequence[i]))
719 if (j == end && j < pos)
724 updateCursor(pos, i, startColumn);
729 * Updates the cursor to the latest found residue and column position
736 * column position of the first sequence residue
738 protected void updateCursor(int residuePos, int column, int startColumn)
741 * preserve end residue column provided cursor was valid
743 int endColumn = isValidCursor(cursor) ? cursor.lastColumnPosition : 0;
745 if (residuePos == this.end)
750 cursor = new SequenceCursor(this, residuePos, column, startColumn,
751 endColumn, this.changeCount);
755 * Answers the aligned column position (1..) for the given residue position
756 * (start..) given a 'hint' of a residue/column location in the neighbourhood.
757 * The hint may be left of, at, or to the right of the required position.
763 protected int findIndex(int pos, SequenceCursor curs)
765 if (!isValidCursor(curs))
768 * wrong or invalidated cursor, compute de novo
770 return findIndex(pos);
773 if (curs.residuePosition == pos)
775 return curs.columnPosition;
779 * move left or right to find pos from hint.position
781 int col = curs.columnPosition - 1; // convert from base 1 to base 0
782 int newPos = curs.residuePosition;
783 int delta = newPos > pos ? -1 : 1;
785 while (newPos != pos)
787 col += delta; // shift one column left or right
788 if (col < 0 || col == sequence.length)
792 if (!Comparison.isGap(sequence[col]))
798 col++; // convert back to base 1
799 updateCursor(pos, col, curs.firstColumnPosition);
808 public int findPosition(final int column)
811 * use a valid, hopefully nearby, cursor if available
813 if (isValidCursor(cursor))
815 return findPosition(column + 1, cursor);
818 // TODO recode this more naturally i.e. count residues only
819 // as they are found, not 'in anticipation'
822 * traverse the sequence counting gaps; note the column position
823 * of the first residue, to save in the cursor
825 int firstResidueColumn = 0;
826 int lastPosFound = 0;
827 int lastPosFoundColumn = 0;
828 int seqlen = sequence.length;
830 if (seqlen > 0 && !Comparison.isGap(sequence[0]))
832 lastPosFound = start;
833 lastPosFoundColumn = 0;
839 while (j < column && j < seqlen)
841 if (!Comparison.isGap(sequence[j]))
844 lastPosFoundColumn = j;
845 if (pos == this.start)
847 firstResidueColumn = j;
853 if (j < seqlen && !Comparison.isGap(sequence[j]))
856 lastPosFoundColumn = j;
857 if (pos == this.start)
859 firstResidueColumn = j;
864 * update the cursor to the last residue position found (if any)
865 * (converting column position to base 1)
867 if (lastPosFound != 0)
869 updateCursor(lastPosFound, lastPosFoundColumn + 1,
870 firstResidueColumn + 1);
877 * Answers true if the given cursor is not null, is for this sequence object,
878 * and has a token value that matches this object's changeCount, else false.
879 * This allows us to ignore a cursor as 'stale' if the sequence has been
880 * modified since the cursor was created.
885 protected boolean isValidCursor(SequenceCursor curs)
887 if (curs == null || curs.sequence != this || curs.token != changeCount)
892 * sanity check against range
894 if (curs.columnPosition < 0 || curs.columnPosition > sequence.length)
898 if (curs.residuePosition < start || curs.residuePosition > end)
906 * Answers the sequence position (start..) for the given aligned column
907 * position (1..), given a hint of a cursor in the neighbourhood. The cursor
908 * may lie left of, at, or to the right of the column position.
914 protected int findPosition(final int col, SequenceCursor curs)
916 if (!isValidCursor(curs))
919 * wrong or invalidated cursor, compute de novo
921 return findPosition(col - 1);// ugh back to base 0
924 if (curs.columnPosition == col)
926 cursor = curs; // in case this method becomes public
927 return curs.residuePosition; // easy case :-)
930 if (curs.lastColumnPosition > 0 && curs.lastColumnPosition < col)
933 * sequence lies entirely to the left of col
934 * - return last residue + 1
939 if (curs.firstColumnPosition > 0 && curs.firstColumnPosition > col)
942 * sequence lies entirely to the right of col
943 * - return first residue
948 // todo could choose closest to col out of column,
949 // firstColumnPosition, lastColumnPosition as a start point
952 * move left or right to find pos from cursor position
954 int firstResidueColumn = curs.firstColumnPosition;
955 int column = curs.columnPosition - 1; // to base 0
956 int newPos = curs.residuePosition;
957 int delta = curs.columnPosition > col ? -1 : 1;
958 boolean gapped = false;
959 int lastFoundPosition = curs.residuePosition;
960 int lastFoundPositionColumn = curs.columnPosition;
962 while (column != col - 1)
964 column += delta; // shift one column left or right
965 if (column < 0 || column == sequence.length)
969 gapped = Comparison.isGap(sequence[column]);
973 lastFoundPosition = newPos;
974 lastFoundPositionColumn = column + 1;
975 if (lastFoundPosition == this.start)
977 firstResidueColumn = column + 1;
982 if (cursor == null || lastFoundPosition != cursor.residuePosition)
984 updateCursor(lastFoundPosition, lastFoundPositionColumn,
989 * hack to give position to the right if on a gap
990 * or beyond the length of the sequence (see JAL-2562)
992 if (delta > 0 && (gapped || column >= sequence.length))
1004 public Range findPositions(int fromColumn, int toColumn)
1006 if (toColumn < fromColumn || fromColumn < 1)
1012 * find the first non-gapped position, if any
1014 int firstPosition = 0;
1015 int col = fromColumn - 1;
1016 int length = sequence.length;
1017 while (col < length && col < toColumn)
1019 if (!Comparison.isGap(sequence[col]))
1021 firstPosition = findPosition(col++);
1027 if (firstPosition == 0)
1033 * find the last non-gapped position
1035 int lastPosition = firstPosition;
1036 while (col < length && col < toColumn)
1038 if (!Comparison.isGap(sequence[col++]))
1044 return new Range(firstPosition, lastPosition);
1048 * Returns an int array where indices correspond to each residue in the
1049 * sequence and the element value gives its position in the alignment
1051 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
1052 * residues in SequenceI object
1055 public int[] gapMap()
1057 String seq = jalview.analysis.AlignSeq.extractGaps(
1058 jalview.util.Comparison.GapChars, new String(sequence));
1059 int[] map = new int[seq.length()];
1063 while (j < sequence.length)
1065 if (!jalview.util.Comparison.isGap(sequence[j]))
1077 public int[] findPositionMap()
1079 int map[] = new int[sequence.length];
1082 int seqlen = sequence.length;
1083 while ((j < seqlen))
1086 if (!jalview.util.Comparison.isGap(sequence[j]))
1097 public List<int[]> getInsertions()
1099 ArrayList<int[]> map = new ArrayList<int[]>();
1100 int lastj = -1, j = 0;
1102 int seqlen = sequence.length;
1103 while ((j < seqlen))
1105 if (jalview.util.Comparison.isGap(sequence[j]))
1116 map.add(new int[] { lastj, j - 1 });
1124 map.add(new int[] { lastj, j - 1 });
1131 public BitSet getInsertionsAsBits()
1133 BitSet map = new BitSet();
1134 int lastj = -1, j = 0;
1136 int seqlen = sequence.length;
1137 while ((j < seqlen))
1139 if (jalview.util.Comparison.isGap(sequence[j]))
1165 public void deleteChars(int i, int j)
1167 int newstart = start, newend = end;
1168 if (i >= sequence.length || i < 0)
1173 char[] tmp = StringUtils.deleteChars(sequence, i, j);
1174 boolean createNewDs = false;
1175 // TODO: take a (second look) at the dataset creation validation method for
1176 // the very large sequence case
1177 int eindex = -1, sindex = -1;
1178 boolean ecalc = false, scalc = false;
1179 for (int s = i; s < j && s < sequence.length; s++)
1181 if (!Comparison.isGap(sequence[s]))
1191 sindex = findIndex(start) - 1;
1196 // delete characters including start of sequence
1197 newstart = findPosition(j);
1198 break; // don't need to search for any more residue characters.
1202 // delete characters after start.
1205 eindex = findIndex(end) - 1;
1210 // delete characters at end of sequence
1211 newend = findPosition(i - 1);
1212 break; // don't need to search for any more residue characters.
1217 newend--; // decrease end position by one for the deleted residue
1218 // and search further
1224 // deletion occured in the middle of the sequence
1225 if (createNewDs && this.datasetSequence != null)
1227 // construct a new sequence
1228 Sequence ds = new Sequence(datasetSequence);
1229 // TODO: remove any non-inheritable properties ?
1230 // TODO: create a sequence mapping (since there is a relation here ?)
1231 ds.deleteChars(i, j);
1232 datasetSequence = ds;
1241 public void insertCharAt(int i, int length, char c)
1243 char[] tmp = new char[sequence.length + length];
1245 if (i >= sequence.length)
1247 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
1248 i = sequence.length;
1252 System.arraycopy(sequence, 0, tmp, 0, i);
1262 if (i < sequence.length)
1264 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
1272 public void insertCharAt(int i, char c)
1274 insertCharAt(i, 1, c);
1278 public String getVamsasId()
1284 public void setVamsasId(String id)
1290 public void setDBRefs(DBRefEntry[] dbref)
1292 if (dbrefs == null && datasetSequence != null
1293 && this != datasetSequence)
1295 datasetSequence.setDBRefs(dbref);
1301 DBRefUtils.ensurePrimaries(this);
1306 public DBRefEntry[] getDBRefs()
1308 if (dbrefs == null && datasetSequence != null
1309 && this != datasetSequence)
1311 return datasetSequence.getDBRefs();
1317 public void addDBRef(DBRefEntry entry)
1319 if (datasetSequence != null)
1321 datasetSequence.addDBRef(entry);
1327 dbrefs = new DBRefEntry[0];
1330 for (DBRefEntryI dbr : dbrefs)
1332 if (dbr.updateFrom(entry))
1335 * found a dbref that either matched, or could be
1336 * updated from, the new entry - no need to add it
1343 * extend the array to make room for one more
1345 // TODO use an ArrayList instead
1346 int j = dbrefs.length;
1347 DBRefEntry[] temp = new DBRefEntry[j + 1];
1348 System.arraycopy(dbrefs, 0, temp, 0, j);
1349 temp[temp.length - 1] = entry;
1353 DBRefUtils.ensurePrimaries(this);
1357 public void setDatasetSequence(SequenceI seq)
1361 throw new IllegalArgumentException(
1362 "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
1364 if (seq != null && seq.getDatasetSequence() != null)
1366 throw new IllegalArgumentException(
1367 "Implementation error: cascading dataset sequences are not allowed.");
1369 datasetSequence = seq;
1373 public SequenceI getDatasetSequence()
1375 return datasetSequence;
1379 public AlignmentAnnotation[] getAnnotation()
1381 return annotation == null ? null
1383 .toArray(new AlignmentAnnotation[annotation.size()]);
1387 public boolean hasAnnotation(AlignmentAnnotation ann)
1389 return annotation == null ? false : annotation.contains(ann);
1393 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
1395 if (this.annotation == null)
1397 this.annotation = new Vector<AlignmentAnnotation>();
1399 if (!this.annotation.contains(annotation))
1401 this.annotation.addElement(annotation);
1403 annotation.setSequenceRef(this);
1407 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1409 if (this.annotation != null)
1411 this.annotation.removeElement(annotation);
1412 if (this.annotation.size() == 0)
1414 this.annotation = null;
1420 * test if this is a valid candidate for another sequence's dataset sequence.
1423 private boolean isValidDatasetSequence()
1425 if (datasetSequence != null)
1429 for (int i = 0; i < sequence.length; i++)
1431 if (jalview.util.Comparison.isGap(sequence[i]))
1440 public SequenceI deriveSequence()
1442 Sequence seq = null;
1443 if (datasetSequence == null)
1445 if (isValidDatasetSequence())
1447 // Use this as dataset sequence
1448 seq = new Sequence(getName(), "", 1, -1);
1449 seq.setDatasetSequence(this);
1450 seq.initSeqFrom(this, getAnnotation());
1455 // Create a new, valid dataset sequence
1456 createDatasetSequence();
1459 return new Sequence(this);
1462 private boolean _isNa;
1464 private int _seqhash = 0;
1467 * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
1471 public boolean isProtein()
1473 if (datasetSequence != null)
1475 return datasetSequence.isProtein();
1477 if (_seqhash != sequence.hashCode())
1479 _seqhash = sequence.hashCode();
1480 _isNa = Comparison.isNucleotide(this);
1488 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1491 public SequenceI createDatasetSequence()
1493 if (datasetSequence == null)
1495 Sequence dsseq = new Sequence(getName(),
1496 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
1497 getSequenceAsString()),
1498 getStart(), getEnd());
1500 datasetSequence = dsseq;
1502 dsseq.setDescription(description);
1503 // move features and database references onto dataset sequence
1504 dsseq.sequenceFeatureStore = sequenceFeatureStore;
1505 sequenceFeatureStore = null;
1506 dsseq.dbrefs = dbrefs;
1508 // TODO: search and replace any references to this sequence with
1509 // references to the dataset sequence in Mappings on dbref
1510 dsseq.pdbIds = pdbIds;
1512 datasetSequence.updatePDBIds();
1513 if (annotation != null)
1515 // annotation is cloned rather than moved, to preserve what's currently
1517 for (AlignmentAnnotation aa : annotation)
1519 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1520 _aa.sequenceRef = datasetSequence;
1521 _aa.adjustForAlignment(); // uses annotation's own record of
1522 // sequence-column mapping
1523 datasetSequence.addAlignmentAnnotation(_aa);
1527 return datasetSequence;
1534 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1538 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1540 if (annotation != null)
1542 annotation.removeAllElements();
1544 if (annotations != null)
1546 for (int i = 0; i < annotations.length; i++)
1548 if (annotations[i] != null)
1550 addAlignmentAnnotation(annotations[i]);
1557 public AlignmentAnnotation[] getAnnotation(String label)
1559 if (annotation == null || annotation.size() == 0)
1564 Vector<AlignmentAnnotation> subset = new Vector<AlignmentAnnotation>();
1565 Enumeration<AlignmentAnnotation> e = annotation.elements();
1566 while (e.hasMoreElements())
1568 AlignmentAnnotation ann = e.nextElement();
1569 if (ann.label != null && ann.label.equals(label))
1571 subset.addElement(ann);
1574 if (subset.size() == 0)
1578 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1580 e = subset.elements();
1581 while (e.hasMoreElements())
1583 anns[i++] = e.nextElement();
1585 subset.removeAllElements();
1590 public boolean updatePDBIds()
1592 if (datasetSequence != null)
1594 // TODO: could merge DBRefs
1595 return datasetSequence.updatePDBIds();
1597 if (dbrefs == null || dbrefs.length == 0)
1601 boolean added = false;
1602 for (DBRefEntry dbr : dbrefs)
1604 if (DBRefSource.PDB.equals(dbr.getSource()))
1607 * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the
1608 * PDB id is not already present in a 'matching' PDBEntry
1609 * Constructor parses out a chain code if appended to the accession id
1610 * (a fudge used to 'store' the chain code in the DBRef)
1612 PDBEntry pdbe = new PDBEntry(dbr);
1613 added |= addPDBId(pdbe);
1620 public void transferAnnotation(SequenceI entry, Mapping mp)
1622 if (datasetSequence != null)
1624 datasetSequence.transferAnnotation(entry, mp);
1627 if (entry.getDatasetSequence() != null)
1629 transferAnnotation(entry.getDatasetSequence(), mp);
1632 // transfer any new features from entry onto sequence
1633 if (entry.getSequenceFeatures() != null)
1636 List<SequenceFeature> sfs = entry.getSequenceFeatures();
1637 for (SequenceFeature feature : sfs)
1639 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
1640 : new SequenceFeature[] { new SequenceFeature(feature) };
1643 for (int sfi = 0; sfi < sf.length; sfi++)
1645 addSequenceFeature(sf[sfi]);
1651 // transfer PDB entries
1652 if (entry.getAllPDBEntries() != null)
1654 Enumeration<PDBEntry> e = entry.getAllPDBEntries().elements();
1655 while (e.hasMoreElements())
1657 PDBEntry pdb = e.nextElement();
1661 // transfer database references
1662 DBRefEntry[] entryRefs = entry.getDBRefs();
1663 if (entryRefs != null)
1665 for (int r = 0; r < entryRefs.length; r++)
1667 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1668 if (newref.getMap() != null && mp != null)
1670 // remap ref using our local mapping
1672 // we also assume all version string setting is done by dbSourceProxy
1674 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1675 * newref.setSource(dbSource); }
1683 * @return The index (zero-based) on this sequence in the MSA. It returns
1684 * {@code -1} if this information is not available.
1687 public int getIndex()
1693 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1694 * if this information is undefined.
1697 * position for this sequence. This value is zero-based (zero for
1698 * this first sequence)
1701 public void setIndex(int value)
1707 public void setRNA(RNA r)
1719 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1722 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1723 if (this.annotation != null)
1725 for (AlignmentAnnotation ann : annotation)
1727 if (ann.calcId != null && ann.calcId.equals(calcId)
1728 && ann.label != null && ann.label.equals(label))
1738 public String toString()
1740 return getDisplayId(false);
1744 public PDBEntry getPDBEntry(String pdbIdStr)
1746 if (getDatasetSequence() != null)
1748 return getDatasetSequence().getPDBEntry(pdbIdStr);
1754 List<PDBEntry> entries = getAllPDBEntries();
1755 for (PDBEntry entry : entries)
1757 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1766 public List<DBRefEntry> getPrimaryDBRefs()
1768 if (datasetSequence != null)
1770 return datasetSequence.getPrimaryDBRefs();
1772 if (dbrefs == null || dbrefs.length == 0)
1774 return Collections.emptyList();
1776 synchronized (dbrefs)
1778 List<DBRefEntry> primaries = new ArrayList<DBRefEntry>();
1779 DBRefEntry[] tmp = new DBRefEntry[1];
1780 for (DBRefEntry ref : dbrefs)
1782 if (!ref.isPrimaryCandidate())
1788 MapList mp = ref.getMap().getMap();
1789 if (mp.getFromLowest() > start || mp.getFromHighest() < end)
1791 // map only involves a subsequence, so cannot be primary
1795 // whilst it looks like it is a primary ref, we also sanity check type
1796 if (DBRefUtils.getCanonicalName(DBRefSource.PDB)
1797 .equals(DBRefUtils.getCanonicalName(ref.getSource())))
1799 // PDB dbrefs imply there should be a PDBEntry associated
1800 // TODO: tighten PDB dbrefs
1801 // formally imply Jalview has actually downloaded and
1802 // parsed the pdb file. That means there should be a cached file
1803 // handle on the PDBEntry, and a real mapping between sequence and
1804 // extracted sequence from PDB file
1805 PDBEntry pdbentry = getPDBEntry(ref.getAccessionId());
1806 if (pdbentry != null && pdbentry.getFile() != null)
1812 // check standard protein or dna sources
1814 DBRefEntry[] res = DBRefUtils.selectDbRefs(!isProtein(), tmp);
1815 if (res != null && res[0] == tmp[0])
1829 public List<SequenceFeature> findFeatures(int fromColumn, int toColumn,
1832 int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
1833 int endPos = fromColumn == toColumn ? startPos
1834 : findPosition(toColumn - 1);
1836 List<SequenceFeature> result = getFeatures().findFeatures(startPos,
1838 if (datasetSequence != null)
1840 result = datasetSequence.getFeatures().findFeatures(startPos, endPos,
1845 result = sequenceFeatureStore.findFeatures(startPos, endPos, types);
1849 * if end column is gapped, endPos may be to the right,
1850 * and we may have included adjacent or enclosing features;
1851 * remove any that are not enclosing, non-contact features
1853 boolean endColumnIsGapped = toColumn > 0 && toColumn <= sequence.length
1854 && Comparison.isGap(sequence[toColumn - 1]);
1855 if (endPos > this.end || endColumnIsGapped)
1857 ListIterator<SequenceFeature> it = result.listIterator();
1858 while (it.hasNext())
1860 SequenceFeature sf = it.next();
1861 int sfBegin = sf.getBegin();
1862 int sfEnd = sf.getEnd();
1863 int featureStartColumn = findIndex(sfBegin);
1864 if (featureStartColumn > toColumn)
1868 else if (featureStartColumn < fromColumn)
1870 int featureEndColumn = sfEnd == sfBegin ? featureStartColumn
1872 if (featureEndColumn < fromColumn)
1876 else if (featureEndColumn > toColumn && sf.isContactFeature())
1879 * remove an enclosing feature if it is a contact feature
1891 * Invalidates any stale cursors (forcing recalculation) by incrementing the
1892 * token that has to match the one presented by the cursor
1895 public void sequenceChanged()
1904 public int replace(char c1, char c2)
1911 synchronized (sequence)
1913 for (int c = 0; c < sequence.length; c++)
1915 if (sequence[c] == c1)