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;
109 private GeneLoci geneLoci;
112 * Creates a new Sequence object.
115 * display name string
117 * string to form a possibly gapped sequence out of
119 * first position of non-gap residue in the sequence
121 * last position of ungapped residues (nearly always only used for
124 public Sequence(String name, String sequence, int start, int end)
127 initSeqAndName(name, sequence.toCharArray(), start, end);
130 public Sequence(String name, char[] sequence, int start, int end)
133 initSeqAndName(name, sequence, start, end);
137 * Stage 1 constructor - assign name, sequence, and set start and end fields.
138 * start and end are updated values from name2 if it ends with /start-end
145 protected void initSeqAndName(String name2, char[] sequence2, int start2,
149 this.sequence = sequence2;
161 "POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
164 // Does sequence have the /start-end signature?
165 if (limitrx.search(name))
167 name = limitrx.left();
168 endrx.search(limitrx.stringMatched());
169 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
170 endrx.matchedFrom() - 1)));
171 setEnd(Integer.parseInt(endrx.stringMatched()));
175 void checkValidRange()
178 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
181 for (int j = 0; j < sequence.length; j++)
183 if (!jalview.util.Comparison.isGap(sequence[j]))
202 * default constructor
206 sequenceFeatureStore = new SequenceFeatures();
210 * Creates a new Sequence object.
217 public Sequence(String name, String sequence)
219 this(name, sequence, 1, -1);
223 * Creates a new Sequence object with new AlignmentAnnotations but inherits
224 * any existing dataset sequence reference. If non exists, everything is
228 * if seq is a dataset sequence, behaves like a plain old copy
231 public Sequence(SequenceI seq)
233 this(seq, seq.getAnnotation());
237 * Create a new sequence object with new features, DBRefEntries, and PDBIds
238 * but inherits any existing dataset sequence reference, and duplicate of any
239 * annotation that is present in the given annotation array.
242 * the sequence to be copied
243 * @param alAnnotation
244 * an array of annotation including some associated with seq
246 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
249 initSeqFrom(seq, alAnnotation);
253 * does the heavy lifting when cloning a dataset sequence, or coping data from
254 * dataset to a new derived sequence.
257 * - source of attributes.
258 * @param alAnnotation
259 * - alignment annotation present on seq that should be copied onto
262 protected void initSeqFrom(SequenceI seq,
263 AlignmentAnnotation[] alAnnotation)
265 char[] oseq = seq.getSequence(); // returns a copy of the array
266 initSeqAndName(seq.getName(), oseq, seq.getStart(), seq.getEnd());
268 description = seq.getDescription();
269 if (seq != datasetSequence)
271 setDatasetSequence(seq.getDatasetSequence());
275 * only copy DBRefs and seqfeatures if we really are a dataset sequence
277 if (datasetSequence == null)
279 if (seq.getDBRefs() != null)
281 DBRefEntry[] dbr = seq.getDBRefs();
282 for (int i = 0; i < dbr.length; i++)
284 addDBRef(new DBRefEntry(dbr[i]));
289 * make copies of any sequence features
291 for (SequenceFeature sf : seq.getSequenceFeatures())
293 addSequenceFeature(new SequenceFeature(sf));
297 if (seq.getAnnotation() != null)
299 AlignmentAnnotation[] sqann = seq.getAnnotation();
300 for (int i = 0; i < sqann.length; i++)
302 if (sqann[i] == null)
306 boolean found = (alAnnotation == null);
309 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
311 found = (alAnnotation[apos] == sqann[i]);
316 // only copy the given annotation
317 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
318 addAlignmentAnnotation(newann);
322 if (seq.getAllPDBEntries() != null)
324 Vector<PDBEntry> ids = seq.getAllPDBEntries();
325 for (PDBEntry pdb : ids)
327 this.addPDBId(new PDBEntry(pdb));
333 public void setSequenceFeatures(List<SequenceFeature> features)
335 if (datasetSequence != null)
337 datasetSequence.setSequenceFeatures(features);
340 sequenceFeatureStore = new SequenceFeatures(features);
344 public synchronized boolean addSequenceFeature(SequenceFeature sf)
346 if (sf.getType() == null)
348 System.err.println("SequenceFeature type may not be null: "
353 if (datasetSequence != null)
355 return datasetSequence.addSequenceFeature(sf);
358 return sequenceFeatureStore.add(sf);
362 public void deleteFeature(SequenceFeature sf)
364 if (datasetSequence != null)
366 datasetSequence.deleteFeature(sf);
370 sequenceFeatureStore.delete(sf);
380 public List<SequenceFeature> getSequenceFeatures()
382 if (datasetSequence != null)
384 return datasetSequence.getSequenceFeatures();
386 return sequenceFeatureStore.getAllFeatures();
390 public SequenceFeaturesI getFeatures()
392 return datasetSequence != null ? datasetSequence.getFeatures()
393 : sequenceFeatureStore;
397 public boolean addPDBId(PDBEntry entry)
401 pdbIds = new Vector<PDBEntry>();
406 for (PDBEntry pdbe : pdbIds)
408 if (pdbe.updateFrom(entry))
413 pdbIds.addElement(entry);
424 public void setPDBId(Vector<PDBEntry> id)
432 * @return DOCUMENT ME!
435 public Vector<PDBEntry> getAllPDBEntries()
437 return pdbIds == null ? new Vector<PDBEntry>() : pdbIds;
443 * @return DOCUMENT ME!
446 public String getDisplayId(boolean jvsuffix)
448 StringBuffer result = new StringBuffer(name);
451 result.append("/" + start + "-" + end);
454 return result.toString();
464 public void setName(String name)
473 * @return DOCUMENT ME!
476 public String getName()
488 public void setStart(int start)
496 * @return DOCUMENT ME!
499 public int getStart()
511 public void setEnd(int end)
519 * @return DOCUMENT ME!
530 * @return DOCUMENT ME!
533 public int getLength()
535 return this.sequence.length;
545 public void setSequence(String seq)
547 this.sequence = seq.toCharArray();
553 public String getSequenceAsString()
555 return new String(sequence);
559 public String getSequenceAsString(int start, int end)
561 return new String(getSequence(start, end));
565 public char[] getSequence()
568 return sequence == null ? null : Arrays.copyOf(sequence,
575 * @see jalview.datamodel.SequenceI#getSequence(int, int)
578 public char[] getSequence(int start, int end)
584 // JBPNote - left to user to pad the result here (TODO:Decide on this
586 if (start >= sequence.length)
591 if (end >= sequence.length)
593 end = sequence.length;
596 char[] reply = new char[end - start];
597 System.arraycopy(sequence, start, reply, 0, end - start);
603 public SequenceI getSubSequence(int start, int end)
609 char[] seq = getSequence(start, end);
614 int nstart = findPosition(start);
615 int nend = findPosition(end) - 1;
616 // JBPNote - this is an incomplete copy.
617 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
618 nseq.setDescription(description);
619 if (datasetSequence != null)
621 nseq.setDatasetSequence(datasetSequence);
625 nseq.setDatasetSequence(this);
631 * Returns the character of the aligned sequence at the given position (base
632 * zero), or space if the position is not within the sequence's bounds
637 public char getCharAt(int i)
639 if (i >= 0 && i < sequence.length)
650 * Sets the sequence description, and also parses out any special formats of
656 public void setDescription(String desc)
658 this.description = desc;
663 * Parses and saves fields of an Ensembl-style description e.g.
664 * chromosome:GRCh38:17:45051610:45109016:1
666 protected void parseDescription()
668 if (description == null)
672 String[] tokens = description.split(":");
673 if (tokens.length == 6 && "chromosome".equals(tokens[0])) {
674 String ref = tokens[1];
675 String chrom = tokens[2];
677 int chStart = Integer.parseInt(tokens[3]);
678 int chEnd = Integer.parseInt(tokens[4]);
679 boolean forwardStrand = "1".equals(tokens[5]);
680 String species = ""; // dunno yet!
681 int[] from = new int[] { start, end };
682 int[] to = new int[] { forwardStrand ? chStart : chEnd,
683 forwardStrand ? chEnd : chStart };
684 MapList map = new MapList(from, to, 1, 1);
685 GeneLoci gl = new GeneLoci(species, ref, chrom, map);
687 } catch (NumberFormatException e)
689 System.err.println("Bad integers in description " + description);
694 public void setGeneLoci(GeneLoci gl)
700 * Returns the gene loci mapping for the sequence (may be null)
704 public GeneLoci getGeneLoci()
710 * Answers the description
715 public String getDescription()
717 return this.description;
724 public int findIndex(int pos)
727 * use a valid, hopefully nearby, cursor if available
729 if (isValidCursor(cursor))
731 return findIndex(pos, cursor);
739 * traverse sequence from the start counting gaps; make a note of
740 * the column of the first residue to save in the cursor
742 while ((i < sequence.length) && (j <= end) && (j <= pos))
744 if (!Comparison.isGap(sequence[i]))
755 if (j == end && j < pos)
760 updateCursor(pos, i, startColumn);
765 * Updates the cursor to the latest found residue and column position
772 * column position of the first sequence residue
774 protected void updateCursor(int residuePos, int column, int startColumn)
777 * preserve end residue column provided cursor was valid
779 int endColumn = isValidCursor(cursor) ? cursor.lastColumnPosition : 0;
780 if (residuePos == this.end)
785 cursor = new SequenceCursor(this, residuePos, column, startColumn,
786 endColumn, this.changeCount);
790 * Answers the aligned column position (1..) for the given residue position
791 * (start..) given a 'hint' of a residue/column location in the neighbourhood.
792 * The hint may be left of, at, or to the right of the required position.
798 protected int findIndex(int pos, SequenceCursor curs)
800 if (!isValidCursor(curs))
803 * wrong or invalidated cursor, compute de novo
805 return findIndex(pos);
808 if (curs.residuePosition == pos)
810 return curs.columnPosition;
814 * move left or right to find pos from hint.position
816 int col = curs.columnPosition - 1; // convert from base 1 to 0-based array
818 int newPos = curs.residuePosition;
819 int delta = newPos > pos ? -1 : 1;
821 while (newPos != pos)
823 col += delta; // shift one column left or right
824 if (col < 0 || col == sequence.length)
828 if (!Comparison.isGap(sequence[col]))
834 col++; // convert back to base 1
835 updateCursor(pos, col, curs.firstColumnPosition);
844 public int findPosition(final int column)
847 * use a valid, hopefully nearby, cursor if available
849 if (isValidCursor(cursor))
851 return findPosition(column + 1, cursor);
854 // TODO recode this more naturally i.e. count residues only
855 // as they are found, not 'in anticipation'
858 * traverse the sequence counting gaps; note the column position
859 * of the first residue, to save in the cursor
861 int firstResidueColumn = 0;
862 int lastPosFound = 0;
863 int lastPosFoundColumn = 0;
864 int seqlen = sequence.length;
866 if (seqlen > 0 && !Comparison.isGap(sequence[0]))
868 lastPosFound = start;
869 lastPosFoundColumn = 0;
875 while (j < column && j < seqlen)
877 if (!Comparison.isGap(sequence[j]))
880 lastPosFoundColumn = j;
881 if (pos == this.start)
883 firstResidueColumn = j;
889 if (j < seqlen && !Comparison.isGap(sequence[j]))
892 lastPosFoundColumn = j;
893 if (pos == this.start)
895 firstResidueColumn = j;
900 * update the cursor to the last residue position found (if any)
901 * (converting column position to base 1)
903 if (lastPosFound != 0)
905 updateCursor(lastPosFound, lastPosFoundColumn + 1,
906 firstResidueColumn + 1);
913 * Answers true if the given cursor is not null, is for this sequence object,
914 * and has a token value that matches this object's changeCount, else false.
915 * This allows us to ignore a cursor as 'stale' if the sequence has been
916 * modified since the cursor was created.
921 protected boolean isValidCursor(SequenceCursor curs)
923 if (curs == null || curs.sequence != this || curs.token != changeCount)
928 * sanity check against range
930 if (curs.columnPosition < 0 || curs.columnPosition > sequence.length)
934 if (curs.residuePosition < start || curs.residuePosition > end)
942 * Answers the sequence position (start..) for the given aligned column
943 * position (1..), given a hint of a cursor in the neighbourhood. The cursor
944 * may lie left of, at, or to the right of the column position.
950 protected int findPosition(final int col, SequenceCursor curs)
952 if (!isValidCursor(curs))
955 * wrong or invalidated cursor, compute de novo
957 return findPosition(col - 1);// ugh back to base 0
960 if (curs.columnPosition == col)
962 cursor = curs; // in case this method becomes public
963 return curs.residuePosition; // easy case :-)
966 if (curs.lastColumnPosition > 0 && curs.lastColumnPosition < col)
969 * sequence lies entirely to the left of col
970 * - return last residue + 1
975 if (curs.firstColumnPosition > 0 && curs.firstColumnPosition > col)
978 * sequence lies entirely to the right of col
979 * - return first residue
984 // todo could choose closest to col out of column,
985 // firstColumnPosition, lastColumnPosition as a start point
988 * move left or right to find pos from cursor position
990 int firstResidueColumn = curs.firstColumnPosition;
991 int column = curs.columnPosition - 1; // to base 0
992 int newPos = curs.residuePosition;
993 int delta = curs.columnPosition > col ? -1 : 1;
994 boolean gapped = false;
995 int lastFoundPosition = curs.residuePosition;
996 int lastFoundPositionColumn = curs.columnPosition;
998 while (column != col - 1)
1000 column += delta; // shift one column left or right
1001 if (column < 0 || column == sequence.length)
1005 gapped = Comparison.isGap(sequence[column]);
1009 lastFoundPosition = newPos;
1010 lastFoundPositionColumn = column + 1;
1011 if (lastFoundPosition == this.start)
1013 firstResidueColumn = column + 1;
1018 if (cursor == null || lastFoundPosition != cursor.residuePosition)
1020 updateCursor(lastFoundPosition, lastFoundPositionColumn,
1021 firstResidueColumn);
1025 * hack to give position to the right if on a gap
1026 * or beyond the length of the sequence (see JAL-2562)
1028 if (delta > 0 && (gapped || column >= sequence.length))
1040 public Range findPositions(int fromColumn, int toColumn)
1042 if (toColumn < fromColumn || fromColumn < 1)
1048 * find the first non-gapped position, if any
1050 int firstPosition = 0;
1051 int col = fromColumn - 1;
1052 int length = sequence.length;
1053 while (col < length && col < toColumn)
1055 if (!Comparison.isGap(sequence[col]))
1057 firstPosition = findPosition(col++);
1063 if (firstPosition == 0)
1069 * find the last non-gapped position
1071 int lastPosition = firstPosition;
1072 while (col < length && col < toColumn)
1074 if (!Comparison.isGap(sequence[col++]))
1080 return new Range(firstPosition, lastPosition);
1084 * Returns an int array where indices correspond to each residue in the
1085 * sequence and the element value gives its position in the alignment
1087 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
1088 * residues in SequenceI object
1091 public int[] gapMap()
1093 String seq = jalview.analysis.AlignSeq.extractGaps(
1094 jalview.util.Comparison.GapChars, new String(sequence));
1095 int[] map = new int[seq.length()];
1099 while (j < sequence.length)
1101 if (!jalview.util.Comparison.isGap(sequence[j]))
1113 public int[] findPositionMap()
1115 int map[] = new int[sequence.length];
1118 int seqlen = sequence.length;
1119 while ((j < seqlen))
1122 if (!jalview.util.Comparison.isGap(sequence[j]))
1133 public List<int[]> getInsertions()
1135 ArrayList<int[]> map = new ArrayList<int[]>();
1136 int lastj = -1, j = 0;
1138 int seqlen = sequence.length;
1139 while ((j < seqlen))
1141 if (jalview.util.Comparison.isGap(sequence[j]))
1152 map.add(new int[] { lastj, j - 1 });
1160 map.add(new int[] { lastj, j - 1 });
1167 public BitSet getInsertionsAsBits()
1169 BitSet map = new BitSet();
1170 int lastj = -1, j = 0;
1172 int seqlen = sequence.length;
1173 while ((j < seqlen))
1175 if (jalview.util.Comparison.isGap(sequence[j]))
1201 public void deleteChars(int i, int j)
1203 int newstart = start, newend = end;
1204 if (i >= sequence.length || i < 0)
1209 char[] tmp = StringUtils.deleteChars(sequence, i, j);
1210 boolean createNewDs = false;
1211 // TODO: take a (second look) at the dataset creation validation method for
1212 // the very large sequence case
1213 int eindex = -1, sindex = -1;
1214 boolean ecalc = false, scalc = false;
1215 for (int s = i; s < j; s++)
1217 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
1227 sindex = findIndex(start) - 1;
1232 // delete characters including start of sequence
1233 newstart = findPosition(j);
1234 break; // don't need to search for any more residue characters.
1238 // delete characters after start.
1241 eindex = findIndex(end) - 1;
1246 // delete characters at end of sequence
1247 newend = findPosition(i - 1);
1248 break; // don't need to search for any more residue characters.
1253 newend--; // decrease end position by one for the deleted residue
1254 // and search further
1260 // deletion occured in the middle of the sequence
1261 if (createNewDs && this.datasetSequence != null)
1263 // construct a new sequence
1264 Sequence ds = new Sequence(datasetSequence);
1265 // TODO: remove any non-inheritable properties ?
1266 // TODO: create a sequence mapping (since there is a relation here ?)
1267 ds.deleteChars(i, j);
1268 datasetSequence = ds;
1277 public void insertCharAt(int i, int length, char c)
1279 char[] tmp = new char[sequence.length + length];
1281 if (i >= sequence.length)
1283 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
1284 i = sequence.length;
1288 System.arraycopy(sequence, 0, tmp, 0, i);
1298 if (i < sequence.length)
1300 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
1308 public void insertCharAt(int i, char c)
1310 insertCharAt(i, 1, c);
1314 public String getVamsasId()
1320 public void setVamsasId(String id)
1326 public void setDBRefs(DBRefEntry[] dbref)
1328 if (dbrefs == null && datasetSequence != null
1329 && this != datasetSequence)
1331 datasetSequence.setDBRefs(dbref);
1337 DBRefUtils.ensurePrimaries(this);
1342 public DBRefEntry[] getDBRefs()
1344 if (dbrefs == null && datasetSequence != null
1345 && this != datasetSequence)
1347 return datasetSequence.getDBRefs();
1353 public void addDBRef(DBRefEntry entry)
1355 if (datasetSequence != null)
1357 datasetSequence.addDBRef(entry);
1363 dbrefs = new DBRefEntry[0];
1366 for (DBRefEntryI dbr : dbrefs)
1368 if (dbr.updateFrom(entry))
1371 * found a dbref that either matched, or could be
1372 * updated from, the new entry - no need to add it
1379 * extend the array to make room for one more
1381 // TODO use an ArrayList instead
1382 int j = dbrefs.length;
1383 DBRefEntry[] temp = new DBRefEntry[j + 1];
1384 System.arraycopy(dbrefs, 0, temp, 0, j);
1385 temp[temp.length - 1] = entry;
1389 DBRefUtils.ensurePrimaries(this);
1393 public void setDatasetSequence(SequenceI seq)
1397 throw new IllegalArgumentException(
1398 "Implementation Error: self reference passed to SequenceI.setDatasetSequence");
1400 if (seq != null && seq.getDatasetSequence() != null)
1402 throw new IllegalArgumentException(
1403 "Implementation error: cascading dataset sequences are not allowed.");
1405 datasetSequence = seq;
1409 public SequenceI getDatasetSequence()
1411 return datasetSequence;
1415 public AlignmentAnnotation[] getAnnotation()
1417 return annotation == null ? null
1419 .toArray(new AlignmentAnnotation[annotation.size()]);
1423 public boolean hasAnnotation(AlignmentAnnotation ann)
1425 return annotation == null ? false : annotation.contains(ann);
1429 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
1431 if (this.annotation == null)
1433 this.annotation = new Vector<AlignmentAnnotation>();
1435 if (!this.annotation.contains(annotation))
1437 this.annotation.addElement(annotation);
1439 annotation.setSequenceRef(this);
1443 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1445 if (this.annotation != null)
1447 this.annotation.removeElement(annotation);
1448 if (this.annotation.size() == 0)
1450 this.annotation = null;
1456 * test if this is a valid candidate for another sequence's dataset sequence.
1459 private boolean isValidDatasetSequence()
1461 if (datasetSequence != null)
1465 for (int i = 0; i < sequence.length; i++)
1467 if (jalview.util.Comparison.isGap(sequence[i]))
1476 public SequenceI deriveSequence()
1478 Sequence seq = null;
1479 if (datasetSequence == null)
1481 if (isValidDatasetSequence())
1483 // Use this as dataset sequence
1484 seq = new Sequence(getName(), "", 1, -1);
1485 seq.setDatasetSequence(this);
1486 seq.initSeqFrom(this, getAnnotation());
1491 // Create a new, valid dataset sequence
1492 createDatasetSequence();
1495 return new Sequence(this);
1498 private boolean _isNa;
1500 private int _seqhash = 0;
1503 * Answers false if the sequence is more than 85% nucleotide (ACGTU), else
1507 public boolean isProtein()
1509 if (datasetSequence != null)
1511 return datasetSequence.isProtein();
1513 if (_seqhash != sequence.hashCode())
1515 _seqhash = sequence.hashCode();
1516 _isNa = Comparison.isNucleotide(this);
1524 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1527 public SequenceI createDatasetSequence()
1529 if (datasetSequence == null)
1531 Sequence dsseq = new Sequence(getName(),
1532 AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
1533 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)
1833 .equals(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 fromColumn, int toColumn,
1868 int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
1869 int endPos = fromColumn == toColumn ? startPos
1870 : findPosition(toColumn - 1);
1872 List<SequenceFeature> result = getFeatures().findFeatures(startPos,
1876 * if end column is gapped, endPos may be to the right,
1877 * and we may have included adjacent or enclosing features;
1878 * remove any that are not enclosing, non-contact features
1880 if (endPos > this.end || Comparison.isGap(sequence[toColumn - 1]))
1882 ListIterator<SequenceFeature> it = result.listIterator();
1883 while (it.hasNext())
1885 SequenceFeature sf = it.next();
1886 int sfBegin = sf.getBegin();
1887 int sfEnd = sf.getEnd();
1888 int featureStartColumn = findIndex(sfBegin);
1889 if (featureStartColumn > toColumn)
1893 else if (featureStartColumn < fromColumn)
1895 int featureEndColumn = sfEnd == sfBegin ? featureStartColumn
1897 if (featureEndColumn < fromColumn)
1901 else if (featureEndColumn > toColumn && sf.isContactFeature())
1904 * remove an enclosing feature if it is a contact feature
1916 * Invalidates any stale cursors (forcing recalculation) by incrementing the
1917 * token that has to match the one presented by the cursor
1920 public void sequenceChanged()
1929 public int replace(char c1, char c2)
1936 synchronized (sequence)
1938 for (int c = 0; c < sequence.length; c++)
1940 if (sequence[c] == c1)