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.Collections;
35 import java.util.Enumeration;
36 import java.util.List;
37 import java.util.Vector;
39 import com.stevesoft.pat.Regex;
41 import fr.orsay.lri.varna.models.rna.RNA;
45 * Implements the SequenceI interface for a char[] based sequence object.
50 public class Sequence extends ASequence implements SequenceI
52 private static final Regex limitrx = new Regex(
53 "[/][0-9]{1,}[-][0-9]{1,}$");
55 private static final Regex endrx = new Regex("[0-9]{1,}$");
57 SequenceI datasetSequence;
61 private char[] sequence;
69 Vector<PDBEntry> pdbIds;
78 * This annotation is displayed below the alignment but the positions are tied
79 * to the residues of this sequence
81 * TODO: change to List<>
83 Vector<AlignmentAnnotation> annotation;
86 * The index of the sequence in a MSA
90 private SequenceFeatures sequenceFeatureStore;
93 * A cursor holding the approximate current view position to the sequence,
94 * as determined by findIndex or findPosition or findPositions.
95 * Using a cursor as a hint allows these methods to be more performant for
98 private SequenceCursor cursor;
101 * A number that should be incremented whenever the sequence is edited.
102 * If the value matches the cursor token, then we can trust the cursor,
103 * if not then it should be recomputed.
105 private int changeCount;
108 * Creates a new Sequence object.
111 * display name string
113 * string to form a possibly gapped sequence out of
115 * first position of non-gap residue in the sequence
117 * last position of ungapped residues (nearly always only used for
120 public Sequence(String name, String sequence, int start, int end)
123 initSeqAndName(name, sequence.toCharArray(), start, end);
126 public Sequence(String name, char[] sequence, int start, int end)
129 initSeqAndName(name, sequence, start, end);
133 * Stage 1 constructor - assign name, sequence, and set start and end fields.
134 * start and end are updated values from name2 if it ends with /start-end
141 protected void initSeqAndName(String name2, char[] sequence2, int start2,
145 this.sequence = sequence2;
157 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
160 // Does sequence have the /start-end signature?
161 if (limitrx.search(name))
163 name = limitrx.left();
164 endrx.search(limitrx.stringMatched());
165 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
166 endrx.matchedFrom() - 1)));
167 setEnd(Integer.parseInt(endrx.stringMatched()));
171 void checkValidRange()
174 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
177 for (int j = 0; j < sequence.length; j++)
179 if (!jalview.util.Comparison.isGap(sequence[j]))
198 * default constructor
202 sequenceFeatureStore = new SequenceFeatures();
206 * Creates a new Sequence object.
213 public Sequence(String name, String sequence)
215 this(name, sequence, 1, -1);
219 * Creates a new Sequence object with new AlignmentAnnotations but inherits
220 * any existing dataset sequence reference. If non exists, everything is
224 * if seq is a dataset sequence, behaves like a plain old copy
227 public Sequence(SequenceI seq)
229 this(seq, seq.getAnnotation());
233 * Create a new sequence object with new features, DBRefEntries, and PDBIds
234 * but inherits any existing dataset sequence reference, and duplicate of any
235 * annotation that is present in the given annotation array.
238 * the sequence to be copied
239 * @param alAnnotation
240 * an array of annotation including some associated with seq
242 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
245 initSeqFrom(seq, alAnnotation);
249 * does the heavy lifting when cloning a dataset sequence, or coping data from
250 * dataset to a new derived sequence.
253 * - source of attributes.
254 * @param alAnnotation
255 * - alignment annotation present on seq that should be copied onto
258 protected void initSeqFrom(SequenceI seq,
259 AlignmentAnnotation[] alAnnotation)
261 char[] oseq = seq.getSequence();
262 initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length),
263 seq.getStart(), seq.getEnd());
265 description = seq.getDescription();
266 if (seq != datasetSequence)
268 setDatasetSequence(seq.getDatasetSequence());
272 * only copy DBRefs and seqfeatures if we really are a dataset sequence
274 if (datasetSequence == null)
276 if (seq.getDBRefs() != null)
278 DBRefEntry[] dbr = seq.getDBRefs();
279 for (int i = 0; i < dbr.length; i++)
281 addDBRef(new DBRefEntry(dbr[i]));
286 * make copies of any sequence features
288 for (SequenceFeature sf : seq.getSequenceFeatures())
290 addSequenceFeature(new SequenceFeature(sf));
294 if (seq.getAnnotation() != null)
296 AlignmentAnnotation[] sqann = seq.getAnnotation();
297 for (int i = 0; i < sqann.length; i++)
299 if (sqann[i] == null)
303 boolean found = (alAnnotation == null);
306 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
308 found = (alAnnotation[apos] == sqann[i]);
313 // only copy the given annotation
314 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
315 addAlignmentAnnotation(newann);
319 if (seq.getAllPDBEntries() != null)
321 Vector<PDBEntry> ids = seq.getAllPDBEntries();
322 for (PDBEntry pdb : ids)
324 this.addPDBId(new PDBEntry(pdb));
330 public void setSequenceFeatures(List<SequenceFeature> features)
332 if (datasetSequence != null)
334 datasetSequence.setSequenceFeatures(features);
337 sequenceFeatureStore = new SequenceFeatures(features);
341 public synchronized boolean addSequenceFeature(SequenceFeature sf)
343 if (sf.getType() == null)
345 System.err.println("SequenceFeature type may not be null: "
350 if (datasetSequence != null)
352 return datasetSequence.addSequenceFeature(sf);
355 return sequenceFeatureStore.add(sf);
359 public void deleteFeature(SequenceFeature sf)
361 if (datasetSequence != null)
363 datasetSequence.deleteFeature(sf);
367 sequenceFeatureStore.delete(sf);
377 public List<SequenceFeature> getSequenceFeatures()
379 if (datasetSequence != null)
381 return datasetSequence.getSequenceFeatures();
383 return sequenceFeatureStore.getAllFeatures();
387 public SequenceFeaturesI getFeatures()
389 return datasetSequence != null ? datasetSequence.getFeatures()
390 : sequenceFeatureStore;
394 public boolean addPDBId(PDBEntry entry)
398 pdbIds = new Vector<PDBEntry>();
403 for (PDBEntry pdbe : pdbIds)
405 if (pdbe.updateFrom(entry))
410 pdbIds.addElement(entry);
421 public void setPDBId(Vector<PDBEntry> id)
429 * @return DOCUMENT ME!
432 public Vector<PDBEntry> getAllPDBEntries()
434 return pdbIds == null ? new Vector<PDBEntry>() : pdbIds;
440 * @return DOCUMENT ME!
443 public String getDisplayId(boolean jvsuffix)
445 StringBuffer result = new StringBuffer(name);
448 result.append("/" + start + "-" + end);
451 return result.toString();
461 public void setName(String name)
470 * @return DOCUMENT ME!
473 public String getName()
485 public void setStart(int start)
493 * @return DOCUMENT ME!
496 public int getStart()
508 public void setEnd(int end)
516 * @return DOCUMENT ME!
527 * @return DOCUMENT ME!
530 public int getLength()
532 return this.sequence.length;
542 public void setSequence(String seq)
544 this.sequence = seq.toCharArray();
550 public String getSequenceAsString()
552 return new String(sequence);
556 public String getSequenceAsString(int start, int end)
558 return new String(getSequence(start, end));
562 public char[] getSequence()
570 * @see jalview.datamodel.SequenceI#getSequence(int, int)
573 public char[] getSequence(int start, int end)
579 // JBPNote - left to user to pad the result here (TODO:Decide on this
581 if (start >= sequence.length)
586 if (end >= sequence.length)
588 end = sequence.length;
591 char[] reply = new char[end - start];
592 System.arraycopy(sequence, start, reply, 0, end - start);
598 public SequenceI getSubSequence(int start, int end)
604 char[] seq = getSequence(start, end);
609 int nstart = findPosition(start);
610 int nend = findPosition(end) - 1;
611 // JBPNote - this is an incomplete copy.
612 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
613 nseq.setDescription(description);
614 if (datasetSequence != null)
616 nseq.setDatasetSequence(datasetSequence);
620 nseq.setDatasetSequence(this);
626 * Returns the character of the aligned sequence at the given position (base
627 * zero), or space if the position is not within the sequence's bounds
632 public char getCharAt(int i)
634 if (i >= 0 && i < sequence.length)
651 public void setDescription(String desc)
653 this.description = desc;
659 * @return DOCUMENT ME!
662 public String getDescription()
664 return this.description;
670 * @see jalview.datamodel.SequenceI#findIndex(int)
673 public int findIndex(int pos)
676 * use a valid nearby cursor if available
678 if (cursor != null && cursor.sequence == this
679 && cursor.token == changeCount)
681 return findIndex(pos, cursor);
686 // Rely on end being at least as long as the length of the sequence.
687 while ((i < sequence.length) && (j <= end) && (j <= pos))
689 if (!Comparison.isGap(sequence[i]))
696 if ((j == end) && (j < pos))
702 updateCursor(pos, i);
707 protected void updateCursor(int residuePos, int column)
709 // TODO probably want to synchronize this on something
710 cursor = new SequenceCursor(this, residuePos, column, this.changeCount);
714 * Answers the aligned column position (1..) for the given residue position
715 * (start..) given a 'hint' of a residue/column location in the neighbourhood.
716 * The hint may be left of, at, or to the right of the required position.
722 protected int findIndex(int pos, SequenceCursor curs)
724 if (curs.sequence != this || curs.token != changeCount)
727 * wrong or invalidated cursor, compute de novo
729 return findIndex(pos);
732 if (curs.residuePosition == pos)
734 return curs.columnPosition;
738 * move left or right to find pos from hint.position
740 int col = curs.columnPosition - 1; // convert from base 1 to 0-based array
742 int newPos = curs.residuePosition;
743 int delta = newPos > pos ? -1 : 1;
745 while (newPos != pos)
747 col += delta; // shift one column left or right
748 if (col < 0 || col == sequence.length)
752 if (!Comparison.isGap(sequence[col]))
758 col++; // convert back to base 1
759 updateCursor(pos, col);
765 public int findPosition(final int i)
768 * use a valid nearby cursor if available
770 if (cursor != null && cursor.sequence == this
771 && cursor.token == changeCount)
773 return findPosition(i + 1, cursor);
778 int seqlen = sequence.length;
779 while ((j < i) && (j < seqlen))
781 if (!Comparison.isGap(sequence[j]))
789 if (j == i && !Comparison.isGap(sequence[i]))
791 updateCursor(pos, i + 1);
798 * Answers the sequence position (start..) for the given aligned column
799 * position (1..), given a hint of a cursor in the neighbourhood. The cursor
800 * may lie left of, at, or to the right of the column position.
806 protected int findPosition(final int col, SequenceCursor curs)
808 if (curs.sequence != this || curs.token != changeCount)
811 * wrong or invalidated cursor, compute de novo
813 return findPosition(col - 1);// ugh back to base 0
816 if (curs.columnPosition == col)
818 return curs.residuePosition; // easy case :-)
822 * move left or right to find pos from cursor position
824 int column = curs.columnPosition - 1; // to base 0
825 int newPos = curs.residuePosition;
826 int delta = curs.columnPosition > col ? -1 : 1;
827 boolean gapped = false;
829 while (column != col - 1)
831 column += delta; // shift one column left or right
832 if (column < 0 || column == sequence.length)
836 gapped = Comparison.isGap(sequence[column]);
844 * hack to give position to the right if on a gap
845 * pending resolution of JAL-2562
847 if (delta > 0 && gapped)
859 public Range findPositions(int fromCol, int toCol)
861 if (cursor != null && cursor.sequence == this
862 && cursor.token == changeCount)
864 return findPositions(fromCol, toCol, cursor);
868 * count residues before fromCol
872 int seqlen = sequence.length;
873 while (j < fromCol && j < seqlen)
875 if (!Comparison.isGap(sequence[j]))
883 * find first and last residues between fromCol and toCol
887 boolean foundFirst = false;
889 while (j <= toCol && j < seqlen)
891 if (!Comparison.isGap(sequence[j]))
907 * no residues in this range
913 * adjust for sequence start coordinate
915 firstPos += start - 1;
916 lastPos += start - 1;
918 return new Range(firstPos, lastPos);
922 * Returns the range of sequence positions included in the given alignment
923 * position range. If no positions are included (the range is entirely gaps),
924 * then returns null. The cursor parameter may provide a starting position in
925 * the neighbourhood of the search (which may be left of, right of, or
926 * overlapping the search region).
929 * start column of region (0..)
931 * end column of region (0..)
935 protected Range findPositions(int fromCol, int toCol, SequenceCursor curs)
937 if (curs.sequence != this || curs.token != changeCount)
940 * wrong or invalidated cursor, compute de novo
942 return findPositions(fromCol, toCol);
946 * keep this simple...first step from cursor to fromCol...
948 final int seqlen = sequence.length;
949 int resNo = curs.residuePosition;
950 int col = curs.columnPosition - 1; // from base 1 to base 0
953 int delta = col > fromCol ? -1 : 1;
954 while (col != fromCol && col >= 0 && col < seqlen)
956 if (!Comparison.isGap(sequence[col]))
964 if (col < fromCol || col == seqlen)
967 * sequence lies to the left of the target region
973 * resNo is now the residue at fromCol (if not gapped), else the one
974 * before it (if delta == 1), else the one after (if delta == -1);
975 * we want the residue before fromCol
977 if (!Comparison.isGap(sequence[fromCol]))
981 else if (curs.columnPosition > fromCol)
987 * now first and last residues between fromCol and toCol
991 boolean foundFirst = false;
993 while (col <= toCol && col < seqlen)
995 if (!Comparison.isGap(sequence[col]))
1011 * no residues in this range
1016 return new Range(firstPos, lastPos);
1020 * Returns an int array where indices correspond to each residue in the
1021 * sequence and the element value gives its position in the alignment
1023 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
1024 * residues in SequenceI object
1027 public int[] gapMap()
1029 String seq = jalview.analysis.AlignSeq.extractGaps(
1030 jalview.util.Comparison.GapChars, new String(sequence));
1031 int[] map = new int[seq.length()];
1035 while (j < sequence.length)
1037 if (!jalview.util.Comparison.isGap(sequence[j]))
1049 public int[] findPositionMap()
1051 int map[] = new int[sequence.length];
1054 int seqlen = sequence.length;
1055 while ((j < seqlen))
1058 if (!jalview.util.Comparison.isGap(sequence[j]))
1069 public List<int[]> getInsertions()
1071 ArrayList<int[]> map = new ArrayList<int[]>();
1072 int lastj = -1, j = 0;
1074 int seqlen = sequence.length;
1075 while ((j < seqlen))
1077 if (jalview.util.Comparison.isGap(sequence[j]))
1088 map.add(new int[] { lastj, j - 1 });
1096 map.add(new int[] { lastj, j - 1 });
1103 public void deleteChars(int i, int j)
1105 int newstart = start, newend = end;
1106 if (i >= sequence.length || i < 0)
1111 char[] tmp = StringUtils.deleteChars(sequence, i, j);
1112 boolean createNewDs = false;
1113 // TODO: take a (second look) at the dataset creation validation method for
1114 // the very large sequence case
1115 int eindex = -1, sindex = -1;
1116 boolean ecalc = false, scalc = false;
1117 for (int s = i; s < j; s++)
1119 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
1129 sindex = findIndex(start) - 1;
1134 // delete characters including start of sequence
1135 newstart = findPosition(j);
1136 break; // don't need to search for any more residue characters.
1140 // delete characters after start.
1143 eindex = findIndex(end) - 1;
1148 // delete characters at end of sequence
1149 newend = findPosition(i - 1);
1150 break; // don't need to search for any more residue characters.
1155 newend--; // decrease end position by one for the deleted residue
1156 // and search further
1162 // deletion occured in the middle of the sequence
1163 if (createNewDs && this.datasetSequence != null)
1165 // construct a new sequence
1166 Sequence ds = new Sequence(datasetSequence);
1167 // TODO: remove any non-inheritable properties ?
1168 // TODO: create a sequence mapping (since there is a relation here ?)
1169 ds.deleteChars(i, j);
1170 datasetSequence = ds;
1179 public void insertCharAt(int i, int length, char c)
1181 char[] tmp = new char[sequence.length + length];
1183 if (i >= sequence.length)
1185 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
1186 i = sequence.length;
1190 System.arraycopy(sequence, 0, tmp, 0, i);
1200 if (i < sequence.length)
1202 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
1210 public void insertCharAt(int i, char c)
1212 insertCharAt(i, 1, c);
1216 public String getVamsasId()
1222 public void setVamsasId(String id)
1228 public void setDBRefs(DBRefEntry[] dbref)
1230 if (dbrefs == null && datasetSequence != null
1231 && this != datasetSequence)
1233 datasetSequence.setDBRefs(dbref);
1239 DBRefUtils.ensurePrimaries(this);
1244 public DBRefEntry[] getDBRefs()
1246 if (dbrefs == null && datasetSequence != null
1247 && this != datasetSequence)
1249 return datasetSequence.getDBRefs();
1255 public void addDBRef(DBRefEntry entry)
1257 if (datasetSequence != null)
1259 datasetSequence.addDBRef(entry);
1265 dbrefs = new DBRefEntry[0];
1268 for (DBRefEntryI dbr : dbrefs)
1270 if (dbr.updateFrom(entry))
1273 * found a dbref that either matched, or could be
1274 * updated from, the new entry - no need to add it
1281 * extend the array to make room for one more
1283 // TODO use an ArrayList instead
1284 int j = dbrefs.length;
1285 DBRefEntry[] temp = new DBRefEntry[j + 1];
1286 System.arraycopy(dbrefs, 0, temp, 0, j);
1287 temp[temp.length - 1] = entry;
1291 DBRefUtils.ensurePrimaries(this);
1295 public void setDatasetSequence(SequenceI seq)
1299 throw new IllegalArgumentException(
1300 "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
1302 if (seq != null && seq.getDatasetSequence() != null)
1304 throw new IllegalArgumentException(
1305 "Implementation error: cascading dataset sequences are not allowed.");
1307 datasetSequence = seq;
1311 public SequenceI getDatasetSequence()
1313 return datasetSequence;
1317 public AlignmentAnnotation[] getAnnotation()
1319 return annotation == null ? null : annotation
1320 .toArray(new AlignmentAnnotation[annotation.size()]);
1324 public boolean hasAnnotation(AlignmentAnnotation ann)
1326 return annotation == null ? false : annotation.contains(ann);
1330 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
1332 if (this.annotation == null)
1334 this.annotation = new Vector<AlignmentAnnotation>();
1336 if (!this.annotation.contains(annotation))
1338 this.annotation.addElement(annotation);
1340 annotation.setSequenceRef(this);
1344 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1346 if (this.annotation != null)
1348 this.annotation.removeElement(annotation);
1349 if (this.annotation.size() == 0)
1351 this.annotation = null;
1357 * test if this is a valid candidate for another sequence's dataset sequence.
1360 private boolean isValidDatasetSequence()
1362 if (datasetSequence != null)
1366 for (int i = 0; i < sequence.length; i++)
1368 if (jalview.util.Comparison.isGap(sequence[i]))
1377 public SequenceI deriveSequence()
1379 Sequence seq = null;
1380 if (datasetSequence == null)
1382 if (isValidDatasetSequence())
1384 // Use this as dataset sequence
1385 seq = new Sequence(getName(), "", 1, -1);
1386 seq.setDatasetSequence(this);
1387 seq.initSeqFrom(this, getAnnotation());
1392 // Create a new, valid dataset sequence
1393 createDatasetSequence();
1396 return new Sequence(this);
1399 private boolean _isNa;
1401 private long _seqhash = 0;
1404 * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
1408 public boolean isProtein()
1410 if (datasetSequence != null)
1412 return datasetSequence.isProtein();
1414 if (_seqhash != sequence.hashCode())
1416 _seqhash = sequence.hashCode();
1417 _isNa = Comparison.isNucleotide(this);
1425 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1428 public SequenceI createDatasetSequence()
1430 if (datasetSequence == null)
1432 Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps(
1433 jalview.util.Comparison.GapChars, getSequenceAsString()),
1434 getStart(), getEnd());
1436 datasetSequence = dsseq;
1438 dsseq.setDescription(description);
1439 // move features and database references onto dataset sequence
1440 dsseq.sequenceFeatureStore = sequenceFeatureStore;
1441 sequenceFeatureStore = null;
1442 dsseq.dbrefs = dbrefs;
1444 // TODO: search and replace any references to this sequence with
1445 // references to the dataset sequence in Mappings on dbref
1446 dsseq.pdbIds = pdbIds;
1448 datasetSequence.updatePDBIds();
1449 if (annotation != null)
1451 // annotation is cloned rather than moved, to preserve what's currently
1453 for (AlignmentAnnotation aa : annotation)
1455 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1456 _aa.sequenceRef = datasetSequence;
1457 _aa.adjustForAlignment(); // uses annotation's own record of
1458 // sequence-column mapping
1459 datasetSequence.addAlignmentAnnotation(_aa);
1463 return datasetSequence;
1470 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1474 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1476 if (annotation != null)
1478 annotation.removeAllElements();
1480 if (annotations != null)
1482 for (int i = 0; i < annotations.length; i++)
1484 if (annotations[i] != null)
1486 addAlignmentAnnotation(annotations[i]);
1493 public AlignmentAnnotation[] getAnnotation(String label)
1495 if (annotation == null || annotation.size() == 0)
1500 Vector<AlignmentAnnotation> subset = new Vector<AlignmentAnnotation>();
1501 Enumeration<AlignmentAnnotation> e = annotation.elements();
1502 while (e.hasMoreElements())
1504 AlignmentAnnotation ann = e.nextElement();
1505 if (ann.label != null && ann.label.equals(label))
1507 subset.addElement(ann);
1510 if (subset.size() == 0)
1514 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1516 e = subset.elements();
1517 while (e.hasMoreElements())
1519 anns[i++] = e.nextElement();
1521 subset.removeAllElements();
1526 public boolean updatePDBIds()
1528 if (datasetSequence != null)
1530 // TODO: could merge DBRefs
1531 return datasetSequence.updatePDBIds();
1533 if (dbrefs == null || dbrefs.length == 0)
1537 boolean added = false;
1538 for (DBRefEntry dbr : dbrefs)
1540 if (DBRefSource.PDB.equals(dbr.getSource()))
1543 * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the
1544 * PDB id is not already present in a 'matching' PDBEntry
1545 * Constructor parses out a chain code if appended to the accession id
1546 * (a fudge used to 'store' the chain code in the DBRef)
1548 PDBEntry pdbe = new PDBEntry(dbr);
1549 added |= addPDBId(pdbe);
1556 public void transferAnnotation(SequenceI entry, Mapping mp)
1558 if (datasetSequence != null)
1560 datasetSequence.transferAnnotation(entry, mp);
1563 if (entry.getDatasetSequence() != null)
1565 transferAnnotation(entry.getDatasetSequence(), mp);
1568 // transfer any new features from entry onto sequence
1569 if (entry.getSequenceFeatures() != null)
1572 List<SequenceFeature> sfs = entry.getSequenceFeatures();
1573 for (SequenceFeature feature : sfs)
1575 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
1576 : new SequenceFeature[] { new SequenceFeature(feature) };
1579 for (int sfi = 0; sfi < sf.length; sfi++)
1581 addSequenceFeature(sf[sfi]);
1587 // transfer PDB entries
1588 if (entry.getAllPDBEntries() != null)
1590 Enumeration<PDBEntry> e = entry.getAllPDBEntries().elements();
1591 while (e.hasMoreElements())
1593 PDBEntry pdb = e.nextElement();
1597 // transfer database references
1598 DBRefEntry[] entryRefs = entry.getDBRefs();
1599 if (entryRefs != null)
1601 for (int r = 0; r < entryRefs.length; r++)
1603 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1604 if (newref.getMap() != null && mp != null)
1606 // remap ref using our local mapping
1608 // we also assume all version string setting is done by dbSourceProxy
1610 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1611 * newref.setSource(dbSource); }
1619 * @return The index (zero-based) on this sequence in the MSA. It returns
1620 * {@code -1} if this information is not available.
1623 public int getIndex()
1629 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1630 * if this information is undefined.
1633 * position for this sequence. This value is zero-based (zero for
1634 * this first sequence)
1637 public void setIndex(int value)
1643 public void setRNA(RNA r)
1655 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1658 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1659 if (this.annotation != null)
1661 for (AlignmentAnnotation ann : annotation)
1663 if (ann.calcId != null && ann.calcId.equals(calcId)
1664 && ann.label != null && ann.label.equals(label))
1674 public String toString()
1676 return getDisplayId(false);
1680 public PDBEntry getPDBEntry(String pdbIdStr)
1682 if (getDatasetSequence() != null)
1684 return getDatasetSequence().getPDBEntry(pdbIdStr);
1690 List<PDBEntry> entries = getAllPDBEntries();
1691 for (PDBEntry entry : entries)
1693 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1702 public List<DBRefEntry> getPrimaryDBRefs()
1704 if (datasetSequence != null)
1706 return datasetSequence.getPrimaryDBRefs();
1708 if (dbrefs == null || dbrefs.length == 0)
1710 return Collections.emptyList();
1712 synchronized (dbrefs)
1714 List<DBRefEntry> primaries = new ArrayList<DBRefEntry>();
1715 DBRefEntry[] tmp = new DBRefEntry[1];
1716 for (DBRefEntry ref : dbrefs)
1718 if (!ref.isPrimaryCandidate())
1724 MapList mp = ref.getMap().getMap();
1725 if (mp.getFromLowest() > start || mp.getFromHighest() < end)
1727 // map only involves a subsequence, so cannot be primary
1731 // whilst it looks like it is a primary ref, we also sanity check type
1732 if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals(
1733 DBRefUtils.getCanonicalName(ref.getSource())))
1735 // PDB dbrefs imply there should be a PDBEntry associated
1736 // TODO: tighten PDB dbrefs
1737 // formally imply Jalview has actually downloaded and
1738 // parsed the pdb file. That means there should be a cached file
1739 // handle on the PDBEntry, and a real mapping between sequence and
1740 // extracted sequence from PDB file
1741 PDBEntry pdbentry = getPDBEntry(ref.getAccessionId());
1742 if (pdbentry != null && pdbentry.getFile() != null)
1748 // check standard protein or dna sources
1750 DBRefEntry[] res = DBRefUtils.selectDbRefs(!isProtein(), tmp);
1751 if (res != null && res[0] == tmp[0])
1765 public List<SequenceFeature> findFeatures(int from, int to,
1768 if (datasetSequence != null)
1770 return datasetSequence.findFeatures(from, to, types);
1772 return sequenceFeatureStore.findFeatures(from, to, types);
1776 * Invalidates any stale cursors (forcing recalculation) by incrementing the
1777 * token that has to match the one presented by the cursor
1780 public void sequenceChanged()