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.Vector;
40 import com.stevesoft.pat.Regex;
42 import fr.orsay.lri.varna.models.rna.RNA;
46 * Implements the SequenceI interface for a char[] based sequence object.
51 public class Sequence extends ASequence implements SequenceI
53 private static final Regex limitrx = new Regex(
54 "[/][0-9]{1,}[-][0-9]{1,}$");
56 private static final Regex endrx = new Regex("[0-9]{1,}$");
58 SequenceI datasetSequence;
62 private char[] sequence;
70 Vector<PDBEntry> pdbIds;
79 * This annotation is displayed below the alignment but the positions are tied
80 * to the residues of this sequence
82 * TODO: change to List<>
84 Vector<AlignmentAnnotation> annotation;
87 * The index of the sequence in a MSA
91 private SequenceFeatures sequenceFeatureStore;
94 * A cursor holding the approximate current view position to the sequence,
95 * as determined by findIndex or findPosition or findPositions.
96 * Using a cursor as a hint allows these methods to be more performant for
99 private SequenceCursor cursor;
102 * A number that should be incremented whenever the sequence is edited.
103 * If the value matches the cursor token, then we can trust the cursor,
104 * if not then it should be recomputed.
106 private int changeCount;
109 * Creates a new Sequence object.
112 * display name string
114 * string to form a possibly gapped sequence out of
116 * first position of non-gap residue in the sequence
118 * last position of ungapped residues (nearly always only used for
121 public Sequence(String name, String sequence, int start, int end)
124 initSeqAndName(name, sequence.toCharArray(), start, end);
127 public Sequence(String name, char[] sequence, int start, int end)
130 initSeqAndName(name, sequence, start, end);
134 * Stage 1 constructor - assign name, sequence, and set start and end fields.
135 * start and end are updated values from name2 if it ends with /start-end
142 protected void initSeqAndName(String name2, char[] sequence2, int start2,
146 this.sequence = sequence2;
158 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
161 // Does sequence have the /start-end signature?
162 if (limitrx.search(name))
164 name = limitrx.left();
165 endrx.search(limitrx.stringMatched());
166 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
167 endrx.matchedFrom() - 1)));
168 setEnd(Integer.parseInt(endrx.stringMatched()));
172 void checkValidRange()
175 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
178 for (int j = 0; j < sequence.length; j++)
180 if (!jalview.util.Comparison.isGap(sequence[j]))
199 * default constructor
203 sequenceFeatureStore = new SequenceFeatures();
207 * Creates a new Sequence object.
214 public Sequence(String name, String sequence)
216 this(name, sequence, 1, -1);
220 * Creates a new Sequence object with new AlignmentAnnotations but inherits
221 * any existing dataset sequence reference. If non exists, everything is
225 * if seq is a dataset sequence, behaves like a plain old copy
228 public Sequence(SequenceI seq)
230 this(seq, seq.getAnnotation());
234 * Create a new sequence object with new features, DBRefEntries, and PDBIds
235 * but inherits any existing dataset sequence reference, and duplicate of any
236 * annotation that is present in the given annotation array.
239 * the sequence to be copied
240 * @param alAnnotation
241 * an array of annotation including some associated with seq
243 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
246 initSeqFrom(seq, alAnnotation);
250 * does the heavy lifting when cloning a dataset sequence, or coping data from
251 * dataset to a new derived sequence.
254 * - source of attributes.
255 * @param alAnnotation
256 * - alignment annotation present on seq that should be copied onto
259 protected void initSeqFrom(SequenceI seq,
260 AlignmentAnnotation[] alAnnotation)
262 char[] oseq = seq.getSequence();
263 initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length),
264 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<PDBEntry>();
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()
571 * @see jalview.datamodel.SequenceI#getSequence(int, int)
574 public char[] getSequence(int start, int end)
580 // JBPNote - left to user to pad the result here (TODO:Decide on this
582 if (start >= sequence.length)
587 if (end >= sequence.length)
589 end = sequence.length;
592 char[] reply = new char[end - start];
593 System.arraycopy(sequence, start, reply, 0, end - start);
599 public SequenceI getSubSequence(int start, int end)
605 char[] seq = getSequence(start, end);
610 int nstart = findPosition(start);
611 int nend = findPosition(end) - 1;
612 // JBPNote - this is an incomplete copy.
613 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
614 nseq.setDescription(description);
615 if (datasetSequence != null)
617 nseq.setDatasetSequence(datasetSequence);
621 nseq.setDatasetSequence(this);
627 * Returns the character of the aligned sequence at the given position (base
628 * zero), or space if the position is not within the sequence's bounds
633 public char getCharAt(int i)
635 if (i >= 0 && i < sequence.length)
652 public void setDescription(String desc)
654 this.description = desc;
660 * @return DOCUMENT ME!
663 public String getDescription()
665 return this.description;
672 public int findIndex(int pos)
675 * use a valid, hopefully nearby, cursor if available
677 if (isValidCursor(cursor))
679 return findIndex(pos, cursor);
684 // Rely on end being at least as long as the length of the sequence.
685 while ((i < sequence.length) && (j <= end) && (j <= pos))
687 if (!Comparison.isGap(sequence[i]))
694 if (j == end && j < pos)
699 updateCursor(pos, i);
704 * Updates the cursor to the latest found residue and column position
711 protected void updateCursor(int residuePos, int column)
713 cursor = new SequenceCursor(this, residuePos, column, this.changeCount);
717 * Answers the aligned column position (1..) for the given residue position
718 * (start..) given a 'hint' of a residue/column location in the neighbourhood.
719 * The hint may be left of, at, or to the right of the required position.
725 protected int findIndex(int pos, SequenceCursor curs)
727 if (!isValidCursor(curs))
730 * wrong or invalidated cursor, compute de novo
732 return findIndex(pos);
735 if (curs.residuePosition == pos)
737 return curs.columnPosition;
741 * move left or right to find pos from hint.position
743 int col = curs.columnPosition - 1; // convert from base 1 to 0-based array
745 int newPos = curs.residuePosition;
746 int delta = newPos > pos ? -1 : 1;
748 while (newPos != pos)
750 col += delta; // shift one column left or right
751 if (col < 0 || col == sequence.length)
755 if (!Comparison.isGap(sequence[col]))
761 col++; // convert back to base 1
762 updateCursor(pos, col);
771 public int findPosition(final int column)
774 * use a valid, hopefully nearby, cursor if available
776 if (isValidCursor(cursor))
778 return findPosition(column + 1, cursor);
781 // TODO recode this more naturally i.e. count residues only
782 // as they are found, not 'in anticipation'
784 int lastPosFound = 0;
785 int lastPosFoundColumn = 0;
786 int seqlen = sequence.length;
787 if (seqlen > 0 && !Comparison.isGap(sequence[0]))
789 lastPosFound = start;
790 lastPosFoundColumn = 0;
796 while (j < column && j < seqlen)
798 if (!Comparison.isGap(sequence[j]))
801 lastPosFoundColumn = j;
806 if (j < seqlen && !Comparison.isGap(sequence[j]))
809 lastPosFoundColumn = j;
813 * update the cursor to the last residue position found (if any)
814 * (converting column position to base 1)
816 if (lastPosFound != 0)
818 updateCursor(lastPosFound, lastPosFoundColumn + 1);
825 * Answers true if the given cursor is not null, is for this sequence object,
826 * and has a token value that matches this object's changeCount, else false.
827 * This allows us to ignore a cursor as 'stale' if the sequence has been
828 * modified since the cursor was created.
833 protected boolean isValidCursor(SequenceCursor curs)
835 if (curs == null || curs.sequence != this || curs.token != changeCount)
840 * sanity check against range
842 if (curs.columnPosition < 0 || curs.columnPosition >= sequence.length)
846 if (curs.residuePosition < start || curs.residuePosition > end)
854 * Answers the sequence position (start..) for the given aligned column
855 * position (1..), given a hint of a cursor in the neighbourhood. The cursor
856 * may lie left of, at, or to the right of the column position.
862 protected int findPosition(final int col, SequenceCursor curs)
864 if (!isValidCursor(curs))
867 * wrong or invalidated cursor, compute de novo
869 return findPosition(col - 1);// ugh back to base 0
872 if (curs.columnPosition == col)
874 cursor = curs; // in case this method becomes public
875 return curs.residuePosition; // easy case :-)
879 * move left or right to find pos from cursor position
881 int column = curs.columnPosition - 1; // to base 0
882 int newPos = curs.residuePosition;
883 int delta = curs.columnPosition > col ? -1 : 1;
884 boolean gapped = false;
885 int lastFoundPosition = curs.residuePosition;
886 int lastFoundPositionColumn = curs.columnPosition;
888 while (column != col - 1)
890 column += delta; // shift one column left or right
891 if (column < 0 || column == sequence.length)
895 gapped = Comparison.isGap(sequence[column]);
899 lastFoundPosition = newPos;
900 lastFoundPositionColumn = column + 1;
904 if (cursor == null || lastFoundPosition != cursor.residuePosition)
906 updateCursor(lastFoundPosition, lastFoundPositionColumn);
910 * hack to give position to the right if on a gap
911 * or beyond the length of the sequence (see JAL-2562)
913 if (delta > 0 && (gapped || column >= sequence.length))
925 public Range findPositions(int fromCol, int toCol)
927 if (cursor != null && cursor.sequence == this
928 && cursor.token == changeCount)
930 return findPositions(fromCol, toCol, cursor);
934 * count residues before fromCol
938 int seqlen = sequence.length;
939 while (j < fromCol && j < seqlen)
941 if (!Comparison.isGap(sequence[j]))
949 * find first and last residues between fromCol and toCol
953 boolean foundFirst = false;
955 while (j <= toCol && j < seqlen)
957 if (!Comparison.isGap(sequence[j]))
973 * no residues in this range
979 * adjust for sequence start coordinate
981 firstPos += start - 1;
982 lastPos += start - 1;
984 return new Range(firstPos, lastPos);
988 * Returns the range of sequence positions included in the given alignment
989 * position range. If no positions are included (the range is entirely gaps),
990 * then returns null. The cursor parameter may provide a starting position in
991 * the neighbourhood of the search (which may be left of, right of, or
992 * overlapping the search region).
995 * start column of region (0..)
997 * end column of region (0..)
1001 protected Range findPositions(int fromCol, int toCol, SequenceCursor curs)
1003 if (!isValidCursor(curs))
1006 * wrong or invalidated cursor, compute de novo
1008 return findPositions(fromCol, toCol);
1012 * keep this simple...first step from cursor to fromCol...
1014 final int seqlen = sequence.length;
1015 int resNo = curs.residuePosition;
1016 int col = curs.columnPosition - 1; // from base 1 to base 0
1019 int delta = col > fromCol ? -1 : 1;
1020 while (col != fromCol && col >= 0 && col < seqlen)
1022 if (!Comparison.isGap(sequence[col]))
1030 if (col < fromCol || col == seqlen)
1033 * sequence lies to the left of the target region
1039 * resNo is now the residue at fromCol (if not gapped), else the one
1040 * before it (if delta == 1), else the one after (if delta == -1);
1041 * we want the residue before fromCol
1043 if (!Comparison.isGap(sequence[fromCol]))
1047 else if (curs.columnPosition > fromCol)
1053 * now first and last residues between fromCol and toCol
1057 boolean foundFirst = false;
1059 while (col <= toCol && col < seqlen)
1061 if (!Comparison.isGap(sequence[col]))
1077 * no residues in this range
1082 return new Range(firstPos, lastPos);
1086 * Returns an int array where indices correspond to each residue in the
1087 * sequence and the element value gives its position in the alignment
1089 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
1090 * residues in SequenceI object
1093 public int[] gapMap()
1095 String seq = jalview.analysis.AlignSeq.extractGaps(
1096 jalview.util.Comparison.GapChars, new String(sequence));
1097 int[] map = new int[seq.length()];
1101 while (j < sequence.length)
1103 if (!jalview.util.Comparison.isGap(sequence[j]))
1115 public int[] findPositionMap()
1117 int map[] = new int[sequence.length];
1120 int seqlen = sequence.length;
1121 while ((j < seqlen))
1124 if (!jalview.util.Comparison.isGap(sequence[j]))
1135 public List<int[]> getInsertions()
1137 ArrayList<int[]> map = new ArrayList<int[]>();
1138 int lastj = -1, j = 0;
1140 int seqlen = sequence.length;
1141 while ((j < seqlen))
1143 if (jalview.util.Comparison.isGap(sequence[j]))
1154 map.add(new int[] { lastj, j - 1 });
1162 map.add(new int[] { lastj, j - 1 });
1169 public BitSet getInsertionsAsBits()
1171 BitSet map = new BitSet();
1172 int lastj = -1, j = 0;
1174 int seqlen = sequence.length;
1175 while ((j < seqlen))
1177 if (jalview.util.Comparison.isGap(sequence[j]))
1203 public void deleteChars(int i, int j)
1205 int newstart = start, newend = end;
1206 if (i >= sequence.length || i < 0)
1211 char[] tmp = StringUtils.deleteChars(sequence, i, j);
1212 boolean createNewDs = false;
1213 // TODO: take a (second look) at the dataset creation validation method for
1214 // the very large sequence case
1215 int eindex = -1, sindex = -1;
1216 boolean ecalc = false, scalc = false;
1217 for (int s = i; s < j; s++)
1219 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
1229 sindex = findIndex(start) - 1;
1234 // delete characters including start of sequence
1235 newstart = findPosition(j);
1236 break; // don't need to search for any more residue characters.
1240 // delete characters after start.
1243 eindex = findIndex(end) - 1;
1248 // delete characters at end of sequence
1249 newend = findPosition(i - 1);
1250 break; // don't need to search for any more residue characters.
1255 newend--; // decrease end position by one for the deleted residue
1256 // and search further
1262 // deletion occured in the middle of the sequence
1263 if (createNewDs && this.datasetSequence != null)
1265 // construct a new sequence
1266 Sequence ds = new Sequence(datasetSequence);
1267 // TODO: remove any non-inheritable properties ?
1268 // TODO: create a sequence mapping (since there is a relation here ?)
1269 ds.deleteChars(i, j);
1270 datasetSequence = ds;
1279 public void insertCharAt(int i, int length, char c)
1281 char[] tmp = new char[sequence.length + length];
1283 if (i >= sequence.length)
1285 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
1286 i = sequence.length;
1290 System.arraycopy(sequence, 0, tmp, 0, i);
1300 if (i < sequence.length)
1302 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
1310 public void insertCharAt(int i, char c)
1312 insertCharAt(i, 1, c);
1316 public String getVamsasId()
1322 public void setVamsasId(String id)
1328 public void setDBRefs(DBRefEntry[] dbref)
1330 if (dbrefs == null && datasetSequence != null
1331 && this != datasetSequence)
1333 datasetSequence.setDBRefs(dbref);
1339 DBRefUtils.ensurePrimaries(this);
1344 public DBRefEntry[] getDBRefs()
1346 if (dbrefs == null && datasetSequence != null
1347 && this != datasetSequence)
1349 return datasetSequence.getDBRefs();
1355 public void addDBRef(DBRefEntry entry)
1357 if (datasetSequence != null)
1359 datasetSequence.addDBRef(entry);
1365 dbrefs = new DBRefEntry[0];
1368 for (DBRefEntryI dbr : dbrefs)
1370 if (dbr.updateFrom(entry))
1373 * found a dbref that either matched, or could be
1374 * updated from, the new entry - no need to add it
1381 * extend the array to make room for one more
1383 // TODO use an ArrayList instead
1384 int j = dbrefs.length;
1385 DBRefEntry[] temp = new DBRefEntry[j + 1];
1386 System.arraycopy(dbrefs, 0, temp, 0, j);
1387 temp[temp.length - 1] = entry;
1391 DBRefUtils.ensurePrimaries(this);
1395 public void setDatasetSequence(SequenceI seq)
1399 throw new IllegalArgumentException(
1400 "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
1402 if (seq != null && seq.getDatasetSequence() != null)
1404 throw new IllegalArgumentException(
1405 "Implementation error: cascading dataset sequences are not allowed.");
1407 datasetSequence = seq;
1411 public SequenceI getDatasetSequence()
1413 return datasetSequence;
1417 public AlignmentAnnotation[] getAnnotation()
1419 return annotation == null ? null : annotation
1420 .toArray(new AlignmentAnnotation[annotation.size()]);
1424 public boolean hasAnnotation(AlignmentAnnotation ann)
1426 return annotation == null ? false : annotation.contains(ann);
1430 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
1432 if (this.annotation == null)
1434 this.annotation = new Vector<AlignmentAnnotation>();
1436 if (!this.annotation.contains(annotation))
1438 this.annotation.addElement(annotation);
1440 annotation.setSequenceRef(this);
1444 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1446 if (this.annotation != null)
1448 this.annotation.removeElement(annotation);
1449 if (this.annotation.size() == 0)
1451 this.annotation = null;
1457 * test if this is a valid candidate for another sequence's dataset sequence.
1460 private boolean isValidDatasetSequence()
1462 if (datasetSequence != null)
1466 for (int i = 0; i < sequence.length; i++)
1468 if (jalview.util.Comparison.isGap(sequence[i]))
1477 public SequenceI deriveSequence()
1479 Sequence seq = null;
1480 if (datasetSequence == null)
1482 if (isValidDatasetSequence())
1484 // Use this as dataset sequence
1485 seq = new Sequence(getName(), "", 1, -1);
1486 seq.setDatasetSequence(this);
1487 seq.initSeqFrom(this, getAnnotation());
1492 // Create a new, valid dataset sequence
1493 createDatasetSequence();
1496 return new Sequence(this);
1499 private boolean _isNa;
1501 private long _seqhash = 0;
1504 * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
1508 public boolean isProtein()
1510 if (datasetSequence != null)
1512 return datasetSequence.isProtein();
1514 if (_seqhash != sequence.hashCode())
1516 _seqhash = sequence.hashCode();
1517 _isNa = Comparison.isNucleotide(this);
1525 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1528 public SequenceI createDatasetSequence()
1530 if (datasetSequence == null)
1532 Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps(
1533 jalview.util.Comparison.GapChars, getSequenceAsString()),
1534 getStart(), getEnd());
1536 datasetSequence = dsseq;
1538 dsseq.setDescription(description);
1539 // move features and database references onto dataset sequence
1540 dsseq.sequenceFeatureStore = sequenceFeatureStore;
1541 sequenceFeatureStore = null;
1542 dsseq.dbrefs = dbrefs;
1544 // TODO: search and replace any references to this sequence with
1545 // references to the dataset sequence in Mappings on dbref
1546 dsseq.pdbIds = pdbIds;
1548 datasetSequence.updatePDBIds();
1549 if (annotation != null)
1551 // annotation is cloned rather than moved, to preserve what's currently
1553 for (AlignmentAnnotation aa : annotation)
1555 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1556 _aa.sequenceRef = datasetSequence;
1557 _aa.adjustForAlignment(); // uses annotation's own record of
1558 // sequence-column mapping
1559 datasetSequence.addAlignmentAnnotation(_aa);
1563 return datasetSequence;
1570 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1574 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1576 if (annotation != null)
1578 annotation.removeAllElements();
1580 if (annotations != null)
1582 for (int i = 0; i < annotations.length; i++)
1584 if (annotations[i] != null)
1586 addAlignmentAnnotation(annotations[i]);
1593 public AlignmentAnnotation[] getAnnotation(String label)
1595 if (annotation == null || annotation.size() == 0)
1600 Vector<AlignmentAnnotation> subset = new Vector<AlignmentAnnotation>();
1601 Enumeration<AlignmentAnnotation> e = annotation.elements();
1602 while (e.hasMoreElements())
1604 AlignmentAnnotation ann = e.nextElement();
1605 if (ann.label != null && ann.label.equals(label))
1607 subset.addElement(ann);
1610 if (subset.size() == 0)
1614 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1616 e = subset.elements();
1617 while (e.hasMoreElements())
1619 anns[i++] = e.nextElement();
1621 subset.removeAllElements();
1626 public boolean updatePDBIds()
1628 if (datasetSequence != null)
1630 // TODO: could merge DBRefs
1631 return datasetSequence.updatePDBIds();
1633 if (dbrefs == null || dbrefs.length == 0)
1637 boolean added = false;
1638 for (DBRefEntry dbr : dbrefs)
1640 if (DBRefSource.PDB.equals(dbr.getSource()))
1643 * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the
1644 * PDB id is not already present in a 'matching' PDBEntry
1645 * Constructor parses out a chain code if appended to the accession id
1646 * (a fudge used to 'store' the chain code in the DBRef)
1648 PDBEntry pdbe = new PDBEntry(dbr);
1649 added |= addPDBId(pdbe);
1656 public void transferAnnotation(SequenceI entry, Mapping mp)
1658 if (datasetSequence != null)
1660 datasetSequence.transferAnnotation(entry, mp);
1663 if (entry.getDatasetSequence() != null)
1665 transferAnnotation(entry.getDatasetSequence(), mp);
1668 // transfer any new features from entry onto sequence
1669 if (entry.getSequenceFeatures() != null)
1672 List<SequenceFeature> sfs = entry.getSequenceFeatures();
1673 for (SequenceFeature feature : sfs)
1675 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
1676 : new SequenceFeature[] { new SequenceFeature(feature) };
1679 for (int sfi = 0; sfi < sf.length; sfi++)
1681 addSequenceFeature(sf[sfi]);
1687 // transfer PDB entries
1688 if (entry.getAllPDBEntries() != null)
1690 Enumeration<PDBEntry> e = entry.getAllPDBEntries().elements();
1691 while (e.hasMoreElements())
1693 PDBEntry pdb = e.nextElement();
1697 // transfer database references
1698 DBRefEntry[] entryRefs = entry.getDBRefs();
1699 if (entryRefs != null)
1701 for (int r = 0; r < entryRefs.length; r++)
1703 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1704 if (newref.getMap() != null && mp != null)
1706 // remap ref using our local mapping
1708 // we also assume all version string setting is done by dbSourceProxy
1710 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1711 * newref.setSource(dbSource); }
1719 * @return The index (zero-based) on this sequence in the MSA. It returns
1720 * {@code -1} if this information is not available.
1723 public int getIndex()
1729 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1730 * if this information is undefined.
1733 * position for this sequence. This value is zero-based (zero for
1734 * this first sequence)
1737 public void setIndex(int value)
1743 public void setRNA(RNA r)
1755 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1758 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1759 if (this.annotation != null)
1761 for (AlignmentAnnotation ann : annotation)
1763 if (ann.calcId != null && ann.calcId.equals(calcId)
1764 && ann.label != null && ann.label.equals(label))
1774 public String toString()
1776 return getDisplayId(false);
1780 public PDBEntry getPDBEntry(String pdbIdStr)
1782 if (getDatasetSequence() != null)
1784 return getDatasetSequence().getPDBEntry(pdbIdStr);
1790 List<PDBEntry> entries = getAllPDBEntries();
1791 for (PDBEntry entry : entries)
1793 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1802 public List<DBRefEntry> getPrimaryDBRefs()
1804 if (datasetSequence != null)
1806 return datasetSequence.getPrimaryDBRefs();
1808 if (dbrefs == null || dbrefs.length == 0)
1810 return Collections.emptyList();
1812 synchronized (dbrefs)
1814 List<DBRefEntry> primaries = new ArrayList<DBRefEntry>();
1815 DBRefEntry[] tmp = new DBRefEntry[1];
1816 for (DBRefEntry ref : dbrefs)
1818 if (!ref.isPrimaryCandidate())
1824 MapList mp = ref.getMap().getMap();
1825 if (mp.getFromLowest() > start || mp.getFromHighest() < end)
1827 // map only involves a subsequence, so cannot be primary
1831 // whilst it looks like it is a primary ref, we also sanity check type
1832 if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals(
1833 DBRefUtils.getCanonicalName(ref.getSource())))
1835 // PDB dbrefs imply there should be a PDBEntry associated
1836 // TODO: tighten PDB dbrefs
1837 // formally imply Jalview has actually downloaded and
1838 // parsed the pdb file. That means there should be a cached file
1839 // handle on the PDBEntry, and a real mapping between sequence and
1840 // extracted sequence from PDB file
1841 PDBEntry pdbentry = getPDBEntry(ref.getAccessionId());
1842 if (pdbentry != null && pdbentry.getFile() != null)
1848 // check standard protein or dna sources
1850 DBRefEntry[] res = DBRefUtils.selectDbRefs(!isProtein(), tmp);
1851 if (res != null && res[0] == tmp[0])
1865 public List<SequenceFeature> findFeatures(int from, int to,
1868 if (datasetSequence != null)
1870 return datasetSequence.findFeatures(from, to, types);
1872 return sequenceFeatureStore.findFeatures(from, to, types);
1876 * Invalidates any stale cursors (forcing recalculation) by incrementing the
1877 * token that has to match the one presented by the cursor
1880 public void sequenceChanged()