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 .println("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();
264 initSeqAndName(seq.getName(), Arrays.copyOf(oseq, oseq.length),
265 seq.getStart(), seq.getEnd());
267 description = seq.getDescription();
268 if (seq != datasetSequence)
270 setDatasetSequence(seq.getDatasetSequence());
274 * only copy DBRefs and seqfeatures if we really are a dataset sequence
276 if (datasetSequence == null)
278 if (seq.getDBRefs() != null)
280 DBRefEntry[] dbr = seq.getDBRefs();
281 for (int i = 0; i < dbr.length; i++)
283 addDBRef(new DBRefEntry(dbr[i]));
288 * make copies of any sequence features
290 for (SequenceFeature sf : seq.getSequenceFeatures())
292 addSequenceFeature(new SequenceFeature(sf));
296 if (seq.getAnnotation() != null)
298 AlignmentAnnotation[] sqann = seq.getAnnotation();
299 for (int i = 0; i < sqann.length; i++)
301 if (sqann[i] == null)
305 boolean found = (alAnnotation == null);
308 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
310 found = (alAnnotation[apos] == sqann[i]);
315 // only copy the given annotation
316 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
317 addAlignmentAnnotation(newann);
321 if (seq.getAllPDBEntries() != null)
323 Vector<PDBEntry> ids = seq.getAllPDBEntries();
324 for (PDBEntry pdb : ids)
326 this.addPDBId(new PDBEntry(pdb));
332 public void setSequenceFeatures(List<SequenceFeature> features)
334 if (datasetSequence != null)
336 datasetSequence.setSequenceFeatures(features);
339 sequenceFeatureStore = new SequenceFeatures(features);
343 public synchronized boolean addSequenceFeature(SequenceFeature sf)
345 if (sf.getType() == null)
347 System.err.println("SequenceFeature type may not be null: "
352 if (datasetSequence != null)
354 return datasetSequence.addSequenceFeature(sf);
357 return sequenceFeatureStore.add(sf);
361 public void deleteFeature(SequenceFeature sf)
363 if (datasetSequence != null)
365 datasetSequence.deleteFeature(sf);
369 sequenceFeatureStore.delete(sf);
379 public List<SequenceFeature> getSequenceFeatures()
381 if (datasetSequence != null)
383 return datasetSequence.getSequenceFeatures();
385 return sequenceFeatureStore.getAllFeatures();
389 public SequenceFeaturesI getFeatures()
391 return datasetSequence != null ? datasetSequence.getFeatures()
392 : sequenceFeatureStore;
396 public boolean addPDBId(PDBEntry entry)
400 pdbIds = new Vector<PDBEntry>();
405 for (PDBEntry pdbe : pdbIds)
407 if (pdbe.updateFrom(entry))
412 pdbIds.addElement(entry);
423 public void setPDBId(Vector<PDBEntry> id)
431 * @return DOCUMENT ME!
434 public Vector<PDBEntry> getAllPDBEntries()
436 return pdbIds == null ? new Vector<PDBEntry>() : pdbIds;
442 * @return DOCUMENT ME!
445 public String getDisplayId(boolean jvsuffix)
447 StringBuffer result = new StringBuffer(name);
450 result.append("/" + start + "-" + end);
453 return result.toString();
463 public void setName(String name)
472 * @return DOCUMENT ME!
475 public String getName()
487 public void setStart(int start)
495 * @return DOCUMENT ME!
498 public int getStart()
510 public void setEnd(int end)
518 * @return DOCUMENT ME!
529 * @return DOCUMENT ME!
532 public int getLength()
534 return this.sequence.length;
544 public void setSequence(String seq)
546 this.sequence = seq.toCharArray();
552 public String getSequenceAsString()
554 return new String(sequence);
558 public String getSequenceAsString(int start, int end)
560 return new String(getSequence(start, end));
564 public char[] getSequence()
572 * @see jalview.datamodel.SequenceI#getSequence(int, int)
575 public char[] getSequence(int start, int end)
581 // JBPNote - left to user to pad the result here (TODO:Decide on this
583 if (start >= sequence.length)
588 if (end >= sequence.length)
590 end = sequence.length;
593 char[] reply = new char[end - start];
594 System.arraycopy(sequence, start, reply, 0, end - start);
600 public SequenceI getSubSequence(int start, int end)
606 char[] seq = getSequence(start, end);
611 int nstart = findPosition(start);
612 int nend = findPosition(end) - 1;
613 // JBPNote - this is an incomplete copy.
614 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
615 nseq.setDescription(description);
616 if (datasetSequence != null)
618 nseq.setDatasetSequence(datasetSequence);
622 nseq.setDatasetSequence(this);
628 * Returns the character of the aligned sequence at the given position (base
629 * zero), or space if the position is not within the sequence's bounds
634 public char getCharAt(int i)
636 if (i >= 0 && i < sequence.length)
653 public void setDescription(String desc)
655 this.description = desc;
661 * @return DOCUMENT ME!
664 public String getDescription()
666 return this.description;
673 public int findIndex(int pos)
676 * use a valid, hopefully nearby, cursor if available
678 if (isValidCursor(cursor))
680 return findIndex(pos, cursor);
685 // Rely on end being at least as long as the length of the sequence.
686 while ((i < sequence.length) && (j <= end) && (j <= pos))
688 if (!Comparison.isGap(sequence[i]))
695 if (j == end && j < pos)
700 updateCursor(pos, i);
705 * Updates the cursor to the latest found residue and column position
712 protected void updateCursor(int residuePos, int column)
714 cursor = new SequenceCursor(this, residuePos, column, this.changeCount);
718 * Answers the aligned column position (1..) for the given residue position
719 * (start..) given a 'hint' of a residue/column location in the neighbourhood.
720 * The hint may be left of, at, or to the right of the required position.
726 protected int findIndex(int pos, SequenceCursor curs)
728 if (!isValidCursor(curs))
731 * wrong or invalidated cursor, compute de novo
733 return findIndex(pos);
736 if (curs.residuePosition == pos)
738 return curs.columnPosition;
742 * move left or right to find pos from hint.position
744 int col = curs.columnPosition - 1; // convert from base 1 to 0-based array
746 int newPos = curs.residuePosition;
747 int delta = newPos > pos ? -1 : 1;
749 while (newPos != pos)
751 col += delta; // shift one column left or right
752 if (col < 0 || col == sequence.length)
756 if (!Comparison.isGap(sequence[col]))
762 col++; // convert back to base 1
763 updateCursor(pos, col);
772 public int findPosition(final int column)
775 * use a valid, hopefully nearby, cursor if available
777 if (isValidCursor(cursor))
779 return findPosition(column + 1, cursor);
782 // TODO recode this more naturally i.e. count residues only
783 // as they are found, not 'in anticipation'
785 int lastPosFound = 0;
786 int lastPosFoundColumn = 0;
787 int seqlen = sequence.length;
788 if (seqlen > 0 && !Comparison.isGap(sequence[0]))
790 lastPosFound = start;
791 lastPosFoundColumn = 0;
797 while (j < column && j < seqlen)
799 if (!Comparison.isGap(sequence[j]))
802 lastPosFoundColumn = j;
807 if (j < seqlen && !Comparison.isGap(sequence[j]))
810 lastPosFoundColumn = j;
814 * update the cursor to the last residue position found (if any)
815 * (converting column position to base 1)
817 if (lastPosFound != 0)
819 updateCursor(lastPosFound, lastPosFoundColumn + 1);
826 * Answers true if the given cursor is not null, is for this sequence object,
827 * and has a token value that matches this object's changeCount, else false.
828 * This allows us to ignore a cursor as 'stale' if the sequence has been
829 * modified since the cursor was created.
834 protected boolean isValidCursor(SequenceCursor curs)
836 if (curs == null || curs.sequence != this || curs.token != changeCount)
841 * sanity check against range
843 if (curs.columnPosition < 0 || curs.columnPosition > sequence.length)
847 if (curs.residuePosition < start || curs.residuePosition > end)
855 * Answers the sequence position (start..) for the given aligned column
856 * position (1..), given a hint of a cursor in the neighbourhood. The cursor
857 * may lie left of, at, or to the right of the column position.
863 protected int findPosition(final int col, SequenceCursor curs)
865 if (!isValidCursor(curs))
868 * wrong or invalidated cursor, compute de novo
870 return findPosition(col - 1);// ugh back to base 0
873 if (curs.columnPosition == col)
875 cursor = curs; // in case this method becomes public
876 return curs.residuePosition; // easy case :-)
880 * move left or right to find pos from cursor position
882 int column = curs.columnPosition - 1; // to base 0
883 int newPos = curs.residuePosition;
884 int delta = curs.columnPosition > col ? -1 : 1;
885 boolean gapped = false;
886 int lastFoundPosition = curs.residuePosition;
887 int lastFoundPositionColumn = curs.columnPosition;
889 while (column != col - 1)
891 column += delta; // shift one column left or right
892 if (column < 0 || column == sequence.length)
896 gapped = Comparison.isGap(sequence[column]);
900 lastFoundPosition = newPos;
901 lastFoundPositionColumn = column + 1;
905 if (cursor == null || lastFoundPosition != cursor.residuePosition)
907 updateCursor(lastFoundPosition, lastFoundPositionColumn);
911 * hack to give position to the right if on a gap
912 * or beyond the length of the sequence (see JAL-2562)
914 if (delta > 0 && (gapped || column >= sequence.length))
923 * Returns an int array where indices correspond to each residue in the
924 * sequence and the element value gives its position in the alignment
926 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
927 * residues in SequenceI object
930 public int[] gapMap()
932 String seq = jalview.analysis.AlignSeq.extractGaps(
933 jalview.util.Comparison.GapChars, new String(sequence));
934 int[] map = new int[seq.length()];
938 while (j < sequence.length)
940 if (!jalview.util.Comparison.isGap(sequence[j]))
952 public int[] findPositionMap()
954 int map[] = new int[sequence.length];
957 int seqlen = sequence.length;
961 if (!jalview.util.Comparison.isGap(sequence[j]))
972 public List<int[]> getInsertions()
974 ArrayList<int[]> map = new ArrayList<int[]>();
975 int lastj = -1, j = 0;
977 int seqlen = sequence.length;
980 if (jalview.util.Comparison.isGap(sequence[j]))
991 map.add(new int[] { lastj, j - 1 });
999 map.add(new int[] { lastj, j - 1 });
1006 public BitSet getInsertionsAsBits()
1008 BitSet map = new BitSet();
1009 int lastj = -1, j = 0;
1011 int seqlen = sequence.length;
1012 while ((j < seqlen))
1014 if (jalview.util.Comparison.isGap(sequence[j]))
1040 public void deleteChars(int i, int j)
1042 int newstart = start, newend = end;
1043 if (i >= sequence.length || i < 0)
1048 char[] tmp = StringUtils.deleteChars(sequence, i, j);
1049 boolean createNewDs = false;
1050 // TODO: take a (second look) at the dataset creation validation method for
1051 // the very large sequence case
1052 int eindex = -1, sindex = -1;
1053 boolean ecalc = false, scalc = false;
1054 for (int s = i; s < j; s++)
1056 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
1066 sindex = findIndex(start) - 1;
1071 // delete characters including start of sequence
1072 newstart = findPosition(j);
1073 break; // don't need to search for any more residue characters.
1077 // delete characters after start.
1080 eindex = findIndex(end) - 1;
1085 // delete characters at end of sequence
1086 newend = findPosition(i - 1);
1087 break; // don't need to search for any more residue characters.
1092 newend--; // decrease end position by one for the deleted residue
1093 // and search further
1099 // deletion occured in the middle of the sequence
1100 if (createNewDs && this.datasetSequence != null)
1102 // construct a new sequence
1103 Sequence ds = new Sequence(datasetSequence);
1104 // TODO: remove any non-inheritable properties ?
1105 // TODO: create a sequence mapping (since there is a relation here ?)
1106 ds.deleteChars(i, j);
1107 datasetSequence = ds;
1116 public void insertCharAt(int i, int length, char c)
1118 char[] tmp = new char[sequence.length + length];
1120 if (i >= sequence.length)
1122 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
1123 i = sequence.length;
1127 System.arraycopy(sequence, 0, tmp, 0, i);
1137 if (i < sequence.length)
1139 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
1147 public void insertCharAt(int i, char c)
1149 insertCharAt(i, 1, c);
1153 public String getVamsasId()
1159 public void setVamsasId(String id)
1165 public void setDBRefs(DBRefEntry[] dbref)
1167 if (dbrefs == null && datasetSequence != null
1168 && this != datasetSequence)
1170 datasetSequence.setDBRefs(dbref);
1176 DBRefUtils.ensurePrimaries(this);
1181 public DBRefEntry[] getDBRefs()
1183 if (dbrefs == null && datasetSequence != null
1184 && this != datasetSequence)
1186 return datasetSequence.getDBRefs();
1192 public void addDBRef(DBRefEntry entry)
1194 if (datasetSequence != null)
1196 datasetSequence.addDBRef(entry);
1202 dbrefs = new DBRefEntry[0];
1205 for (DBRefEntryI dbr : dbrefs)
1207 if (dbr.updateFrom(entry))
1210 * found a dbref that either matched, or could be
1211 * updated from, the new entry - no need to add it
1218 * extend the array to make room for one more
1220 // TODO use an ArrayList instead
1221 int j = dbrefs.length;
1222 DBRefEntry[] temp = new DBRefEntry[j + 1];
1223 System.arraycopy(dbrefs, 0, temp, 0, j);
1224 temp[temp.length - 1] = entry;
1228 DBRefUtils.ensurePrimaries(this);
1232 public void setDatasetSequence(SequenceI seq)
1236 throw new IllegalArgumentException(
1237 "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
1239 if (seq != null && seq.getDatasetSequence() != null)
1241 throw new IllegalArgumentException(
1242 "Implementation error: cascading dataset sequences are not allowed.");
1244 datasetSequence = seq;
1248 public SequenceI getDatasetSequence()
1250 return datasetSequence;
1254 public AlignmentAnnotation[] getAnnotation()
1256 return annotation == null ? null : annotation
1257 .toArray(new AlignmentAnnotation[annotation.size()]);
1261 public boolean hasAnnotation(AlignmentAnnotation ann)
1263 return annotation == null ? false : annotation.contains(ann);
1267 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
1269 if (this.annotation == null)
1271 this.annotation = new Vector<AlignmentAnnotation>();
1273 if (!this.annotation.contains(annotation))
1275 this.annotation.addElement(annotation);
1277 annotation.setSequenceRef(this);
1281 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1283 if (this.annotation != null)
1285 this.annotation.removeElement(annotation);
1286 if (this.annotation.size() == 0)
1288 this.annotation = null;
1294 * test if this is a valid candidate for another sequence's dataset sequence.
1297 private boolean isValidDatasetSequence()
1299 if (datasetSequence != null)
1303 for (int i = 0; i < sequence.length; i++)
1305 if (jalview.util.Comparison.isGap(sequence[i]))
1314 public SequenceI deriveSequence()
1316 Sequence seq = null;
1317 if (datasetSequence == null)
1319 if (isValidDatasetSequence())
1321 // Use this as dataset sequence
1322 seq = new Sequence(getName(), "", 1, -1);
1323 seq.setDatasetSequence(this);
1324 seq.initSeqFrom(this, getAnnotation());
1329 // Create a new, valid dataset sequence
1330 createDatasetSequence();
1333 return new Sequence(this);
1336 private boolean _isNa;
1338 private long _seqhash = 0;
1341 * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
1345 public boolean isProtein()
1347 if (datasetSequence != null)
1349 return datasetSequence.isProtein();
1351 if (_seqhash != sequence.hashCode())
1353 _seqhash = sequence.hashCode();
1354 _isNa = Comparison.isNucleotide(this);
1362 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1365 public SequenceI createDatasetSequence()
1367 if (datasetSequence == null)
1369 Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps(
1370 jalview.util.Comparison.GapChars, getSequenceAsString()),
1371 getStart(), getEnd());
1373 datasetSequence = dsseq;
1375 dsseq.setDescription(description);
1376 // move features and database references onto dataset sequence
1377 dsseq.sequenceFeatureStore = sequenceFeatureStore;
1378 sequenceFeatureStore = null;
1379 dsseq.dbrefs = dbrefs;
1381 // TODO: search and replace any references to this sequence with
1382 // references to the dataset sequence in Mappings on dbref
1383 dsseq.pdbIds = pdbIds;
1385 datasetSequence.updatePDBIds();
1386 if (annotation != null)
1388 // annotation is cloned rather than moved, to preserve what's currently
1390 for (AlignmentAnnotation aa : annotation)
1392 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1393 _aa.sequenceRef = datasetSequence;
1394 _aa.adjustForAlignment(); // uses annotation's own record of
1395 // sequence-column mapping
1396 datasetSequence.addAlignmentAnnotation(_aa);
1400 return datasetSequence;
1407 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1411 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1413 if (annotation != null)
1415 annotation.removeAllElements();
1417 if (annotations != null)
1419 for (int i = 0; i < annotations.length; i++)
1421 if (annotations[i] != null)
1423 addAlignmentAnnotation(annotations[i]);
1430 public AlignmentAnnotation[] getAnnotation(String label)
1432 if (annotation == null || annotation.size() == 0)
1437 Vector<AlignmentAnnotation> subset = new Vector<AlignmentAnnotation>();
1438 Enumeration<AlignmentAnnotation> e = annotation.elements();
1439 while (e.hasMoreElements())
1441 AlignmentAnnotation ann = e.nextElement();
1442 if (ann.label != null && ann.label.equals(label))
1444 subset.addElement(ann);
1447 if (subset.size() == 0)
1451 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1453 e = subset.elements();
1454 while (e.hasMoreElements())
1456 anns[i++] = e.nextElement();
1458 subset.removeAllElements();
1463 public boolean updatePDBIds()
1465 if (datasetSequence != null)
1467 // TODO: could merge DBRefs
1468 return datasetSequence.updatePDBIds();
1470 if (dbrefs == null || dbrefs.length == 0)
1474 boolean added = false;
1475 for (DBRefEntry dbr : dbrefs)
1477 if (DBRefSource.PDB.equals(dbr.getSource()))
1480 * 'Add' any PDB dbrefs as a PDBEntry - add is only performed if the
1481 * PDB id is not already present in a 'matching' PDBEntry
1482 * Constructor parses out a chain code if appended to the accession id
1483 * (a fudge used to 'store' the chain code in the DBRef)
1485 PDBEntry pdbe = new PDBEntry(dbr);
1486 added |= addPDBId(pdbe);
1493 public void transferAnnotation(SequenceI entry, Mapping mp)
1495 if (datasetSequence != null)
1497 datasetSequence.transferAnnotation(entry, mp);
1500 if (entry.getDatasetSequence() != null)
1502 transferAnnotation(entry.getDatasetSequence(), mp);
1505 // transfer any new features from entry onto sequence
1506 if (entry.getSequenceFeatures() != null)
1509 List<SequenceFeature> sfs = entry.getSequenceFeatures();
1510 for (SequenceFeature feature : sfs)
1512 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(feature)
1513 : new SequenceFeature[] { new SequenceFeature(feature) };
1516 for (int sfi = 0; sfi < sf.length; sfi++)
1518 addSequenceFeature(sf[sfi]);
1524 // transfer PDB entries
1525 if (entry.getAllPDBEntries() != null)
1527 Enumeration<PDBEntry> e = entry.getAllPDBEntries().elements();
1528 while (e.hasMoreElements())
1530 PDBEntry pdb = e.nextElement();
1534 // transfer database references
1535 DBRefEntry[] entryRefs = entry.getDBRefs();
1536 if (entryRefs != null)
1538 for (int r = 0; r < entryRefs.length; r++)
1540 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1541 if (newref.getMap() != null && mp != null)
1543 // remap ref using our local mapping
1545 // we also assume all version string setting is done by dbSourceProxy
1547 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1548 * newref.setSource(dbSource); }
1556 * @return The index (zero-based) on this sequence in the MSA. It returns
1557 * {@code -1} if this information is not available.
1560 public int getIndex()
1566 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1567 * if this information is undefined.
1570 * position for this sequence. This value is zero-based (zero for
1571 * this first sequence)
1574 public void setIndex(int value)
1580 public void setRNA(RNA r)
1592 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1595 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1596 if (this.annotation != null)
1598 for (AlignmentAnnotation ann : annotation)
1600 if (ann.calcId != null && ann.calcId.equals(calcId)
1601 && ann.label != null && ann.label.equals(label))
1611 public String toString()
1613 return getDisplayId(false);
1617 public PDBEntry getPDBEntry(String pdbIdStr)
1619 if (getDatasetSequence() != null)
1621 return getDatasetSequence().getPDBEntry(pdbIdStr);
1627 List<PDBEntry> entries = getAllPDBEntries();
1628 for (PDBEntry entry : entries)
1630 if (entry.getId().equalsIgnoreCase(pdbIdStr))
1639 public List<DBRefEntry> getPrimaryDBRefs()
1641 if (datasetSequence != null)
1643 return datasetSequence.getPrimaryDBRefs();
1645 if (dbrefs == null || dbrefs.length == 0)
1647 return Collections.emptyList();
1649 synchronized (dbrefs)
1651 List<DBRefEntry> primaries = new ArrayList<DBRefEntry>();
1652 DBRefEntry[] tmp = new DBRefEntry[1];
1653 for (DBRefEntry ref : dbrefs)
1655 if (!ref.isPrimaryCandidate())
1661 MapList mp = ref.getMap().getMap();
1662 if (mp.getFromLowest() > start || mp.getFromHighest() < end)
1664 // map only involves a subsequence, so cannot be primary
1668 // whilst it looks like it is a primary ref, we also sanity check type
1669 if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals(
1670 DBRefUtils.getCanonicalName(ref.getSource())))
1672 // PDB dbrefs imply there should be a PDBEntry associated
1673 // TODO: tighten PDB dbrefs
1674 // formally imply Jalview has actually downloaded and
1675 // parsed the pdb file. That means there should be a cached file
1676 // handle on the PDBEntry, and a real mapping between sequence and
1677 // extracted sequence from PDB file
1678 PDBEntry pdbentry = getPDBEntry(ref.getAccessionId());
1679 if (pdbentry != null && pdbentry.getFile() != null)
1685 // check standard protein or dna sources
1687 DBRefEntry[] res = DBRefUtils.selectDbRefs(!isProtein(), tmp);
1688 if (res != null && res[0] == tmp[0])
1702 public List<SequenceFeature> findFeatures(int fromColumn, int toColumn,
1705 int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
1706 int endPos = findPosition(toColumn - 1);
1708 List<SequenceFeature> result = new ArrayList<>();
1709 if (datasetSequence != null)
1711 result = datasetSequence.getFeatures().findFeatures(startPos, endPos,
1716 result = sequenceFeatureStore.findFeatures(startPos, endPos, types);
1720 * if the start or end column is gapped, startPos or endPos may be to the
1721 * left or right, and we may have included adjacent or enclosing features;
1722 * remove any that are not enclosing, non-contact features
1724 if (endPos > this.end || Comparison.isGap(sequence[fromColumn - 1])
1725 || Comparison.isGap(sequence[toColumn - 1]))
1727 ListIterator<SequenceFeature> it = result.listIterator();
1728 while (it.hasNext())
1730 SequenceFeature sf = it.next();
1731 int featureStartColumn = findIndex(sf.getBegin());
1732 int featureEndColumn = findIndex(sf.getEnd());
1733 boolean noOverlap = featureStartColumn > toColumn
1734 || featureEndColumn < fromColumn;
1737 * reject an 'enclosing' feature if it is actually a contact feature
1739 if (sf.isContactFeature() && featureStartColumn < fromColumn
1740 && featureEndColumn > toColumn)
1755 * Invalidates any stale cursors (forcing recalculation) by incrementing the
1756 * token that has to match the one presented by the cursor
1759 public void sequenceChanged()