2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
25 import java.util.Enumeration;
26 import java.util.Vector;
28 import fr.orsay.lri.varna.models.rna.RNA;
32 * Implements the SequenceI interface for a char[] based sequence object.
37 public class Sequence implements SequenceI
39 SequenceI datasetSequence;
43 private char[] sequence;
60 * This annotation is displayed below the alignment but the positions are tied
61 * to the residues of this sequence
63 * TODO: change to List<>
65 Vector<AlignmentAnnotation> annotation;
68 * The index of the sequence in a MSA
72 /** array of sequence features - may not be null for a valid sequence object */
73 public SequenceFeature[] sequenceFeatures;
76 * Creates a new Sequence object.
81 * string to form a possibly gapped sequence out of
83 * first position of non-gap residue in the sequence
85 * last position of ungapped residues (nearly always only used for
88 public Sequence(String name, String sequence, int start, int end)
91 this.sequence = sequence.toCharArray();
98 public Sequence(String name, char[] sequence, int start, int end)
101 this.sequence = sequence;
108 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
109 "[/][0-9]{1,}[-][0-9]{1,}$");
111 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
118 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
121 // Does sequence have the /start-end signiature?
122 if (limitrx.search(name))
124 name = limitrx.left();
125 endrx.search(limitrx.stringMatched());
126 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
127 endrx.matchedFrom() - 1)));
128 setEnd(Integer.parseInt(endrx.stringMatched()));
132 void checkValidRange()
135 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
138 for (int j = 0; j < sequence.length; j++)
140 if (!jalview.util.Comparison.isGap(sequence[j]))
159 * Creates a new Sequence object.
166 public Sequence(String name, String sequence)
168 this(name, sequence, 1, -1);
172 * Creates a new Sequence object with new features, DBRefEntries,
173 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
179 public Sequence(SequenceI seq)
181 this(seq, seq.getAnnotation());
185 * Create a new sequence object with new features, DBRefEntries, and PDBIds
186 * but inherits any existing dataset sequence reference, and duplicate of any
187 * annotation that is present in the given annotation array.
190 * the sequence to be copied
191 * @param alAnnotation
192 * an array of annotation including some associated with seq
194 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
196 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
197 description = seq.getDescription();
198 if (seq.getSequenceFeatures() != null)
200 SequenceFeature[] sf = seq.getSequenceFeatures();
201 for (int i = 0; i < sf.length; i++)
203 addSequenceFeature(new SequenceFeature(sf[i]));
206 setDatasetSequence(seq.getDatasetSequence());
207 if (datasetSequence == null && seq.getDBRef() != null)
209 // only copy DBRefs if we really are a dataset sequence
210 DBRefEntry[] dbr = seq.getDBRef();
211 for (int i = 0; i < dbr.length; i++)
213 addDBRef(new DBRefEntry(dbr[i]));
216 if (seq.getAnnotation() != null)
218 AlignmentAnnotation[] sqann = seq.getAnnotation();
219 for (int i = 0; i < sqann.length; i++)
221 if (sqann[i] == null)
225 boolean found = (alAnnotation == null);
228 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
230 found = (alAnnotation[apos] == sqann[i]);
235 // only copy the given annotation
236 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
237 addAlignmentAnnotation(newann);
241 if (seq.getPDBId() != null)
243 Vector ids = seq.getPDBId();
244 Enumeration e = ids.elements();
245 while (e.hasMoreElements())
247 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
258 public void setSequenceFeatures(SequenceFeature[] features)
260 sequenceFeatures = features;
263 public synchronized void addSequenceFeature(SequenceFeature sf)
265 if (sequenceFeatures == null)
267 sequenceFeatures = new SequenceFeature[0];
270 for (int i = 0; i < sequenceFeatures.length; i++)
272 if (sequenceFeatures[i].equals(sf))
278 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
279 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
280 temp[sequenceFeatures.length] = sf;
282 sequenceFeatures = temp;
285 public void deleteFeature(SequenceFeature sf)
287 if (sequenceFeatures == null)
293 for (index = 0; index < sequenceFeatures.length; index++)
295 if (sequenceFeatures[index].equals(sf))
301 if (index == sequenceFeatures.length)
306 int sfLength = sequenceFeatures.length;
309 sequenceFeatures = null;
313 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
314 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
316 if (index < sfLength)
318 System.arraycopy(sequenceFeatures, index + 1, temp, index,
319 sequenceFeatures.length - index - 1);
322 sequenceFeatures = temp;
329 * @return DOCUMENT ME!
331 public SequenceFeature[] getSequenceFeatures()
333 return sequenceFeatures;
336 public void addPDBId(PDBEntry entry)
340 pdbIds = new Vector();
342 if (!pdbIds.contains(entry))
344 pdbIds.addElement(entry);
354 public void setPDBId(Vector id)
362 * @return DOCUMENT ME!
364 public Vector getPDBId()
372 * @return DOCUMENT ME!
374 public String getDisplayId(boolean jvsuffix)
376 StringBuffer result = new StringBuffer(name);
379 result.append("/" + start + "-" + end);
382 return result.toString();
391 public void setName(String name)
400 * @return DOCUMENT ME!
402 public String getName()
413 public void setStart(int start)
421 * @return DOCUMENT ME!
423 public int getStart()
434 public void setEnd(int end)
442 * @return DOCUMENT ME!
452 * @return DOCUMENT ME!
454 public int getLength()
456 return this.sequence.length;
465 public void setSequence(String seq)
467 this.sequence = seq.toCharArray();
471 public String getSequenceAsString()
473 return new String(sequence);
476 public String getSequenceAsString(int start, int end)
478 return new String(getSequence(start, end));
481 public char[] getSequence()
489 * @see jalview.datamodel.SequenceI#getSequence(int, int)
491 public char[] getSequence(int start, int end)
495 // JBPNote - left to user to pad the result here (TODO:Decide on this
497 if (start >= sequence.length)
502 if (end >= sequence.length)
504 end = sequence.length;
507 char[] reply = new char[end - start];
508 System.arraycopy(sequence, start, reply, 0, end - start);
514 * make a new Sequence object from start to end (including gaps) over this
523 public SequenceI getSubSequence(int start, int end)
529 char[] seq = getSequence(start, end);
534 int nstart = findPosition(start);
535 int nend = findPosition(end) - 1;
536 // JBPNote - this is an incomplete copy.
537 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
538 nseq.setDescription(description);
539 if (datasetSequence != null)
541 nseq.setDatasetSequence(datasetSequence);
545 nseq.setDatasetSequence(this);
556 * @return DOCUMENT ME!
558 public char getCharAt(int i)
560 if (i < sequence.length)
576 public void setDescription(String desc)
578 this.description = desc;
584 * @return DOCUMENT ME!
586 public String getDescription()
588 return this.description;
594 * @see jalview.datamodel.SequenceI#findIndex(int)
596 public int findIndex(int pos)
598 // returns the alignment position for a residue
601 // Rely on end being at least as long as the length of the sequence.
602 while ((i < sequence.length) && (j <= end) && (j <= pos))
604 if (!jalview.util.Comparison.isGap(sequence[i]))
612 if ((j == end) && (j < pos))
623 * Returns the sequence position for an alignment position
626 * column index in alignment (from 1)
628 * @return residue number for residue (left of and) nearest ith column
630 public int findPosition(int i)
634 int seqlen = sequence.length;
635 while ((j < i) && (j < seqlen))
637 if (!jalview.util.Comparison.isGap(sequence[j]))
649 * Returns an int array where indices correspond to each residue in the
650 * sequence and the element value gives its position in the alignment
652 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
653 * residues in SequenceI object
655 public int[] gapMap()
657 String seq = jalview.analysis.AlignSeq.extractGaps(
658 jalview.util.Comparison.GapChars, new String(sequence));
659 int[] map = new int[seq.length()];
663 while (j < sequence.length)
665 if (!jalview.util.Comparison.isGap(sequence[j]))
679 * @see jalview.datamodel.SequenceI#findPositionMap()
681 public int[] findPositionMap()
683 int map[] = new int[sequence.length];
686 int seqlen = sequence.length;
690 if (!jalview.util.Comparison.isGap(sequence[j]))
703 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
705 public void deleteChars(int i, int j)
707 int newstart = start, newend = end;
708 if (i >= sequence.length)
715 if (j >= sequence.length)
718 System.arraycopy(sequence, 0, tmp, 0, i);
723 tmp = new char[sequence.length - j + i];
724 System.arraycopy(sequence, 0, tmp, 0, i);
725 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
727 boolean createNewDs = false;
728 // TODO: take a look at the new dataset creation validation method below -
729 // this could become time comsuming for large sequences - consider making it
731 for (int s = i; s < j; s++)
733 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
741 int sindex = findIndex(start) - 1;
744 // delete characters including start of sequence
745 newstart = findPosition(j);
746 break; // don't need to search for any more residue characters.
750 // delete characters after start.
751 int eindex = findIndex(end) - 1;
754 // delete characters at end of sequence
755 newend = findPosition(i - 1);
756 break; // don't need to search for any more residue characters.
761 newend--; // decrease end position by one for the deleted residue
762 // and search further
768 // deletion occured in the middle of the sequence
769 if (createNewDs && this.datasetSequence != null)
771 // construct a new sequence
772 Sequence ds = new Sequence(datasetSequence);
773 // TODO: remove any non-inheritable properties ?
774 // TODO: create a sequence mapping (since there is a relation here ?)
775 ds.deleteChars(i, j);
776 datasetSequence = ds;
793 public void insertCharAt(int i, int length, char c)
795 char[] tmp = new char[sequence.length + length];
797 if (i >= sequence.length)
799 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
804 System.arraycopy(sequence, 0, tmp, 0, i);
814 if (i < sequence.length)
816 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
822 public void insertCharAt(int i, char c)
824 insertCharAt(i, 1, c);
827 public String getVamsasId()
832 public void setVamsasId(String id)
837 public void setDBRef(DBRefEntry[] dbref)
842 public DBRefEntry[] getDBRef()
844 if (dbrefs == null && datasetSequence != null
845 && this != datasetSequence)
847 return datasetSequence.getDBRef();
852 public void addDBRef(DBRefEntry entry)
856 dbrefs = new DBRefEntry[0];
859 int i, iSize = dbrefs.length;
861 for (i = 0; i < iSize; i++)
863 if (dbrefs[i].equalRef(entry))
865 if (entry.getMap() != null)
867 if (dbrefs[i].getMap() == null)
869 // overwrite with 'superior' entry that contains a mapping.
877 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
878 System.arraycopy(dbrefs, 0, temp, 0, iSize);
879 temp[temp.length - 1] = entry;
884 public void setDatasetSequence(SequenceI seq)
886 datasetSequence = seq;
889 public SequenceI getDatasetSequence()
891 return datasetSequence;
894 public AlignmentAnnotation[] getAnnotation()
896 if (annotation == null)
901 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
902 for (int r = 0; r < ret.length; r++)
904 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
910 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
912 if (this.annotation == null)
914 this.annotation = new Vector();
916 if (!this.annotation.contains(annotation))
918 this.annotation.addElement(annotation);
920 annotation.setSequenceRef(this);
923 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
925 if (this.annotation != null)
927 this.annotation.removeElement(annotation);
928 if (this.annotation.size() == 0)
929 this.annotation = null;
934 * test if this is a valid candidate for another sequence's dataset sequence.
937 private boolean isValidDatasetSequence()
939 if (datasetSequence != null)
943 for (int i = 0; i < sequence.length; i++)
945 if (jalview.util.Comparison.isGap(sequence[i]))
956 * @see jalview.datamodel.SequenceI#deriveSequence()
958 public SequenceI deriveSequence()
960 SequenceI seq = new Sequence(this);
961 if (datasetSequence != null)
963 // duplicate current sequence with same dataset
964 seq.setDatasetSequence(datasetSequence);
968 if (isValidDatasetSequence())
970 // Use this as dataset sequence
971 seq.setDatasetSequence(this);
975 // Create a new, valid dataset sequence
977 ds.setSequence(AlignSeq.extractGaps(
978 jalview.util.Comparison.GapChars, new String(sequence)));
979 setDatasetSequence(ds);
980 ds.setSequenceFeatures(getSequenceFeatures());
981 seq = this; // and return this sequence as the derived sequence.
990 * @see jalview.datamodel.SequenceI#createDatasetSequence()
992 public SequenceI createDatasetSequence()
994 if (datasetSequence == null)
996 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
997 jalview.util.Comparison.GapChars, getSequenceAsString()),
998 getStart(), getEnd());
999 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1000 datasetSequence.setDescription(getDescription());
1001 setSequenceFeatures(null);
1002 // move database references onto dataset sequence
1003 datasetSequence.setDBRef(getDBRef());
1005 datasetSequence.setPDBId(getPDBId());
1007 datasetSequence.updatePDBIds();
1008 if (annotation != null)
1010 for (AlignmentAnnotation aa : annotation)
1012 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1013 _aa.sequenceRef = datasetSequence;
1014 _aa.adjustForAlignment(); // uses annotation's own record of
1015 // sequence-column mapping
1016 datasetSequence.addAlignmentAnnotation(_aa);
1020 return datasetSequence;
1027 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1030 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1032 if (annotation != null)
1034 annotation.removeAllElements();
1036 if (annotations != null)
1038 for (int i = 0; i < annotations.length; i++)
1040 if (annotations[i] != null)
1041 addAlignmentAnnotation(annotations[i]);
1049 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1051 public AlignmentAnnotation[] getAnnotation(String label)
1053 if (annotation == null || annotation.size() == 0)
1058 Vector subset = new Vector();
1059 Enumeration e = annotation.elements();
1060 while (e.hasMoreElements())
1062 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1063 if (ann.label != null && ann.label.equals(label))
1065 subset.addElement(ann);
1068 if (subset.size() == 0)
1072 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1074 e = subset.elements();
1075 while (e.hasMoreElements())
1077 anns[i++] = (AlignmentAnnotation) e.nextElement();
1079 subset.removeAllElements();
1083 public boolean updatePDBIds()
1085 if (datasetSequence != null)
1087 // TODO: could merge DBRefs
1088 return datasetSequence.updatePDBIds();
1090 if (dbrefs == null || dbrefs.length == 0)
1094 Vector newpdb = new Vector();
1095 for (int i = 0; i < dbrefs.length; i++)
1097 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1099 PDBEntry pdbe = new PDBEntry();
1100 pdbe.setId(dbrefs[i].getAccessionId());
1101 if (pdbIds == null || pdbIds.size() == 0)
1103 newpdb.addElement(pdbe);
1107 Enumeration en = pdbIds.elements();
1108 boolean matched = false;
1109 while (!matched && en.hasMoreElements())
1111 PDBEntry anentry = (PDBEntry) en.nextElement();
1112 if (anentry.getId().equals(pdbe.getId()))
1119 newpdb.addElement(pdbe);
1124 if (newpdb.size() > 0)
1126 Enumeration en = newpdb.elements();
1127 while (en.hasMoreElements())
1129 addPDBId((PDBEntry) en.nextElement());
1140 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1141 * jalview.datamodel.Mapping)
1143 public void transferAnnotation(SequenceI entry, Mapping mp)
1145 if (datasetSequence != null)
1147 datasetSequence.transferAnnotation(entry, mp);
1150 if (entry.getDatasetSequence() != null)
1152 transferAnnotation(entry.getDatasetSequence(), mp);
1155 // transfer any new features from entry onto sequence
1156 if (entry.getSequenceFeatures() != null)
1159 SequenceFeature[] sfs = entry.getSequenceFeatures();
1160 for (int si = 0; si < sfs.length; si++)
1162 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1163 : new SequenceFeature[]
1164 { new SequenceFeature(sfs[si]) };
1165 if (sf != null && sf.length > 0)
1167 for (int sfi = 0; sfi < sf.length; sfi++)
1169 addSequenceFeature(sf[sfi]);
1175 // transfer PDB entries
1176 if (entry.getPDBId() != null)
1178 Enumeration e = entry.getPDBId().elements();
1179 while (e.hasMoreElements())
1181 PDBEntry pdb = (PDBEntry) e.nextElement();
1185 // transfer database references
1186 DBRefEntry[] entryRefs = entry.getDBRef();
1187 if (entryRefs != null)
1189 for (int r = 0; r < entryRefs.length; r++)
1191 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1192 if (newref.getMap() != null && mp != null)
1194 // remap ref using our local mapping
1196 // we also assume all version string setting is done by dbSourceProxy
1198 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1199 * newref.setSource(dbSource); }
1207 * @return The index (zero-based) on this sequence in the MSA. It returns
1208 * {@code -1} if this information is not available.
1210 public int getIndex()
1216 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1217 * if this information is undefined.
1220 * position for this sequence. This value is zero-based (zero for
1221 * this first sequence)
1223 public void setIndex(int value)
1228 public void setRNA(RNA r)