2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
22 import jalview.analysis.*;
25 * Data structure to hold and manipulate a multiple sequence alignment
31 public class Alignment implements AlignmentI
33 protected Alignment dataset;
35 protected List<SequenceI> sequences;
37 protected List<SequenceGroup> groups = java.util.Collections
38 .synchronizedList(new ArrayList<SequenceGroup>());
40 protected char gapCharacter = '-';
42 protected int type = NUCLEOTIDE;
44 public static final int PROTEIN = 0;
46 public static final int NUCLEOTIDE = 1;
48 public boolean hasRNAStructure = false;
51 public AlignmentAnnotation[] annotations;
53 HiddenSequences hiddenSequences = new HiddenSequences(this);
55 public Hashtable alignmentProperties;
57 private void initAlignment(SequenceI[] seqs)
61 if (jalview.util.Comparison.isNucleotide(seqs))
70 sequences = java.util.Collections
71 .synchronizedList(new ArrayList<SequenceI>());
73 for (i = 0; i < seqs.length; i++)
75 sequences.add(seqs[i]);
81 * Make an alignment from an array of Sequences.
85 public Alignment(SequenceI[] seqs)
91 * Make a new alignment from an array of SeqCigars
96 public Alignment(SeqCigar[] alseqs)
98 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
99 gapCharacter, new ColumnSelection(), null);
104 * Make a new alignment from an CigarArray JBPNote - can only do this when
105 * compactAlignment does not contain hidden regions. JBPNote - must also check
106 * that compactAlignment resolves to a set of SeqCigars - or construct them
109 * @param compactAlignment
112 public static AlignmentI createAlignment(CigarArray compactAlignment)
114 throw new Error("Alignment(CigarArray) not yet implemented");
115 // this(compactAlignment.refCigars);
121 * @return DOCUMENT ME!
124 public List<SequenceI> getSequences()
129 public List<SequenceI> getSequences(
130 Map<SequenceI, SequenceCollectionI> hiddenReps)
132 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to work on this.
136 public SequenceI[] getSequencesArray()
138 if (sequences == null)
140 synchronized (sequences)
142 return sequences.toArray(new SequenceI[sequences.size()]);
152 * @return DOCUMENT ME!
154 public SequenceI getSequenceAt(int i)
156 synchronized (sequences)
158 if (i > -1 && i < sequences.size())
160 return sequences.get(i);
167 * Adds a sequence to the alignment. Recalculates maxLength and size.
171 public void addSequence(SequenceI snew)
175 // maintain dataset integrity
176 if (snew.getDatasetSequence() != null)
178 getDataset().addSequence(snew.getDatasetSequence());
182 // derive new sequence
183 SequenceI adding = snew.deriveSequence();
184 getDataset().addSequence(adding.getDatasetSequence());
188 if (sequences == null)
190 initAlignment(new SequenceI[]
195 synchronized (sequences)
200 if (hiddenSequences != null)
201 hiddenSequences.adjustHeightSequenceAdded();
205 * Adds a sequence to the alignment. Recalculates maxLength and size.
209 public void setSequenceAt(int i, SequenceI snew)
211 SequenceI oldseq = getSequenceAt(i);
213 synchronized (sequences)
215 sequences.set(i, snew);
222 * @return DOCUMENT ME!
224 public Vector getGroups()
229 public void finalize()
231 if (getDataset() != null)
232 getDataset().removeAlignmentRef();
238 hiddenSequences = null;
242 * decrement the alignmentRefs counter by one and call finalize if it goes to
245 private void removeAlignmentRef()
247 if (--alignmentRefs == 0)
259 public void deleteSequence(SequenceI s)
261 deleteSequence(findIndex(s));
270 public void deleteSequence(int i)
272 if (i > -1 && i < getHeight())
274 synchronized (sequences)
278 hiddenSequences.adjustHeightSequenceDeleted(i);
285 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
288 public SequenceGroup findGroup(SequenceI s)
290 synchronized (groups)
292 for (int i = 0; i < this.groups.size(); i++)
294 SequenceGroup sg = groups.get(i);
296 if (sg.getSequences(null).contains(s))
311 * @return DOCUMENT ME!
313 public SequenceGroup[] findAllGroups(SequenceI s)
315 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
317 synchronized (groups)
319 int gSize = groups.size();
320 for (int i = 0; i < gSize; i++)
322 SequenceGroup sg = groups.get(i);
323 if (sg == null || sg.getSequences(null) == null)
325 this.deleteGroup(sg);
330 if (sg.getSequences(null).contains(s))
336 SequenceGroup[] ret = new SequenceGroup[temp.size()];
337 return temp.toArray(ret);
341 public void addGroup(SequenceGroup sg)
343 synchronized (groups)
345 if (!groups.contains(sg))
347 if (hiddenSequences.getSize() > 0)
349 int i, iSize = sg.getSize();
350 for (i = 0; i < iSize; i++)
352 if (!sequences.contains(sg.getSequenceAt(i)))
354 sg.deleteSequence(sg.getSequenceAt(i), false);
360 if (sg.getSize() < 1)
372 * remove any annotation that references gp
375 * (if null, removes all group associated annotation)
377 private void removeAnnotationForGroup(SequenceGroup gp)
379 if (annotations == null || annotations.length == 0)
383 // remove annotation very quickly
384 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
388 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
390 if (annotations[i].groupRef != null)
392 todelete[p++] = annotations[i];
396 tokeep[k++] = annotations[i];
402 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
404 if (annotations[i].groupRef == gp)
406 todelete[p++] = annotations[i];
410 tokeep[k++] = annotations[i];
416 // clear out the group associated annotation.
417 for (i = 0; i < p; i++)
419 unhookAnnotation(todelete[i]);
422 t = new AlignmentAnnotation[k];
423 for (i = 0; i < k; i++)
431 public void deleteAllGroups()
433 synchronized (groups)
435 if (annotations != null)
437 removeAnnotationForGroup(null);
444 public void deleteGroup(SequenceGroup g)
446 synchronized (groups)
448 if (groups.contains(g))
450 removeAnnotationForGroup(g);
457 public SequenceI findName(String name)
459 return findName(name, false);
465 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
467 public SequenceI findName(String token, boolean b)
469 return findName(null, token, b);
475 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
478 public SequenceI findName(SequenceI startAfter, String token, boolean b)
483 String sqname = null;
484 if (startAfter != null)
486 // try to find the sequence in the alignment
487 boolean matched = false;
488 while (i < sequences.size())
490 if (getSequenceAt(i++) == startAfter)
501 while (i < sequences.size())
503 sq = getSequenceAt(i);
504 sqname = sq.getName();
505 if (sqname.equals(token) // exact match
506 || (b && // allow imperfect matches - case varies
507 (sqname.equalsIgnoreCase(token))))
509 return getSequenceAt(i);
518 public SequenceI[] findSequenceMatch(String name)
520 Vector matches = new Vector();
523 while (i < sequences.size())
525 if (getSequenceAt(i).getName().equals(name))
527 matches.addElement(getSequenceAt(i));
532 SequenceI[] result = new SequenceI[matches.size()];
533 for (i = 0; i < result.length; i++)
535 result[i] = (SequenceI) matches.elementAt(i);
545 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
547 public int findIndex(SequenceI s)
551 while (i < sequences.size())
553 if (s == getSequenceAt(i))
568 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
570 public int findIndex(SearchResults results)
574 while (i < sequences.size())
576 if (results.involvesSequence(getSequenceAt(i)))
588 * @return DOCUMENT ME!
590 public int getHeight()
592 return sequences.size();
598 * @return DOCUMENT ME!
600 public int getWidth()
604 for (int i = 0; i < sequences.size(); i++)
606 if (getSequenceAt(i).getLength() > maxLength)
608 maxLength = getSequenceAt(i).getLength();
621 public void setGapCharacter(char gc)
624 synchronized (sequences)
626 for (SequenceI seq : sequences)
628 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
629 .replace('-', gc).replace(' ', gc));
637 * @return DOCUMENT ME!
639 public char getGapCharacter()
647 * @see jalview.datamodel.AlignmentI#isAligned()
649 public boolean isAligned()
651 return isAligned(false);
657 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
659 public boolean isAligned(boolean includeHidden)
661 int width = getWidth();
662 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
664 includeHidden = true; // no hidden sequences to check against.
666 for (int i = 0; i < sequences.size(); i++)
668 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
670 if (getSequenceAt(i).getLength() != width)
683 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
684 * AlignmentAnnotation)
686 public boolean deleteAnnotation(AlignmentAnnotation aa)
688 return deleteAnnotation(aa, true);
691 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
695 if (annotations != null)
697 aSize = annotations.length;
705 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
707 boolean swap = false;
710 for (int i = 0; i < aSize; i++)
712 if (annotations[i] == aa)
717 if (tIndex < temp.length)
718 temp[tIndex++] = annotations[i];
725 unhookAnnotation(aa);
732 * remove any object references associated with this annotation
736 private void unhookAnnotation(AlignmentAnnotation aa)
738 if (aa.sequenceRef != null)
740 aa.sequenceRef.removeAlignmentAnnotation(aa);
742 if (aa.groupRef != null)
744 // probably need to do more here in the future (post 2.5.0)
752 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
753 * AlignmentAnnotation)
755 public void addAnnotation(AlignmentAnnotation aa)
757 addAnnotation(aa, -1);
763 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
764 * AlignmentAnnotation, int)
766 public void addAnnotation(AlignmentAnnotation aa, int pos)
768 if(aa.getRNAStruc()!= null){
769 hasRNAStructure=true;
773 if (annotations != null)
775 aSize = annotations.length + 1;
778 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
780 if (pos == -1 || pos >= aSize)
782 temp[aSize - 1] = aa;
791 for (i = 0; i < (aSize - 1); i++, p++)
799 temp[p] = annotations[i];
807 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
809 if (aa == null || annotations == null || annotations.length - 1 < index)
814 int aSize = annotations.length;
815 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
819 for (int i = 0; i < aSize; i++)
828 temp[i] = annotations[i];
832 temp[i] = annotations[i - 1];
841 * returns all annotation on the alignment
843 public AlignmentAnnotation[] getAlignmentAnnotation()
848 public void setNucleotide(boolean b)
860 public boolean isNucleotide()
862 if (type == NUCLEOTIDE)
872 public boolean hasRNAStructure(){
873 //TODO can it happen that structure is removed from alignment?
874 return hasRNAStructure;
877 public void setDataset(Alignment data)
879 if (dataset == null && data == null)
881 // Create a new dataset for this alignment.
882 // Can only be done once, if dataset is not null
883 // This will not be performed
884 SequenceI[] seqs = new SequenceI[getHeight()];
885 SequenceI currentSeq;
886 for (int i = 0; i < getHeight(); i++)
888 currentSeq = getSequenceAt(i);
889 if (currentSeq.getDatasetSequence() != null)
891 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
895 seqs[i] = currentSeq.createDatasetSequence();
899 dataset = new Alignment(seqs);
901 else if (dataset == null && data != null)
905 dataset.addAlignmentRef();
909 * reference count for number of alignments referencing this one.
911 int alignmentRefs = 0;
914 * increase reference count to this alignment.
916 private void addAlignmentRef()
921 public Alignment getDataset()
926 public boolean padGaps()
928 boolean modified = false;
930 // Remove excess gaps from the end of alignment
934 for (int i = 0; i < sequences.size(); i++)
936 current = getSequenceAt(i);
937 for (int j = current.getLength(); j > maxLength; j--)
940 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
951 for (int i = 0; i < sequences.size(); i++)
953 current = getSequenceAt(i);
954 cLength = current.getLength();
956 if (cLength < maxLength)
958 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
961 else if (current.getLength() > maxLength)
963 current.deleteChars(maxLength, current.getLength());
970 * Justify the sequences to the left or right by deleting and inserting gaps
971 * before the initial residue or after the terminal residue
974 * true if alignment padded to right, false to justify to left
975 * @return true if alignment was changed
977 public boolean justify(boolean right)
979 boolean modified = false;
981 // Remove excess gaps from the end of alignment
983 int ends[] = new int[sequences.size() * 2];
985 for (int i = 0; i < sequences.size(); i++)
987 current = getSequenceAt(i);
988 // This should really be a sequence method
989 ends[i * 2] = current.findIndex(current.getStart());
990 ends[i * 2 + 1] = current.findIndex(current.getStart()
991 + current.getLength());
992 boolean hitres = false;
993 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
995 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1004 ends[i * 2 + 1] = j;
1005 if (j - ends[i * 2] > maxLength)
1007 maxLength = j - ends[i * 2];
1015 // now edit the flanking gaps to justify to either left or right
1016 int cLength, extent, diff;
1017 for (int i = 0; i < sequences.size(); i++)
1019 current = getSequenceAt(i);
1021 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1022 diff = maxLength - cLength; // number of gaps to indent
1023 extent = current.getLength();
1027 if (extent > ends[i * 2 + 1])
1029 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1032 if (ends[i * 2] > diff)
1034 current.deleteChars(0, ends[i * 2] - diff);
1039 if (ends[i * 2] < diff)
1041 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1049 if (ends[i * 2] > 0)
1051 current.deleteChars(0, ends[i * 2]);
1053 ends[i * 2 + 1] -= ends[i * 2];
1054 extent -= ends[i * 2];
1056 if (extent > maxLength)
1058 current.deleteChars(maxLength + 1, extent);
1063 if (extent < maxLength)
1065 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1074 public HiddenSequences getHiddenSequences()
1076 return hiddenSequences;
1079 public CigarArray getCompactAlignment()
1081 synchronized (sequences)
1083 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1085 for (SequenceI seq : sequences)
1087 alseqs[i++] = new SeqCigar(seq);
1089 CigarArray cal = new CigarArray(alseqs);
1090 cal.addOperation(CigarArray.M, getWidth());
1096 public void setProperty(Object key, Object value)
1098 if (alignmentProperties == null)
1099 alignmentProperties = new Hashtable();
1101 alignmentProperties.put(key, value);
1104 public Object getProperty(Object key)
1106 if (alignmentProperties != null)
1107 return alignmentProperties.get(key);
1112 public Hashtable getProperties()
1114 return alignmentProperties;
1117 AlignedCodonFrame[] codonFrameList = null;
1123 * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1126 public void addCodonFrame(AlignedCodonFrame codons)
1130 if (codonFrameList == null)
1132 codonFrameList = new AlignedCodonFrame[]
1136 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
1137 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
1138 t[codonFrameList.length] = codons;
1145 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
1147 public AlignedCodonFrame getCodonFrame(int index)
1149 return codonFrameList[index];
1156 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1158 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
1160 if (seq == null || codonFrameList == null)
1162 Vector cframes = new Vector();
1163 for (int f = 0; f < codonFrameList.length; f++)
1165 if (codonFrameList[f].involvesSequence(seq))
1166 cframes.addElement(codonFrameList[f]);
1168 if (cframes.size() == 0)
1170 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
1171 cframes.copyInto(cfr);
1178 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1180 public AlignedCodonFrame[] getCodonFrames()
1182 return codonFrameList;
1188 * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
1189 * AlignedCodonFrame)
1191 public boolean removeCodonFrame(AlignedCodonFrame codons)
1193 if (codons == null || codonFrameList == null)
1195 boolean removed = false;
1196 int i = 0, iSize = codonFrameList.length;
1199 if (codonFrameList[i] == codons)
1204 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize
1217 public void append(AlignmentI toappend)
1219 if (toappend == this)
1221 System.err.println("Self append may cause a deadlock.");
1223 // TODO test this method for a future 2.5 release
1224 // currently tested for use in jalview.gui.SequenceFetcher
1225 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1226 char oldc = toappend.getGapCharacter();
1227 boolean hashidden = toappend.getHiddenSequences() != null
1228 && toappend.getHiddenSequences().hiddenSequences != null;
1229 // get all sequences including any hidden ones
1230 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1231 .getFullAlignment().getSequences() : toappend.getSequences();
1236 for (SequenceI addedsq : sqs)
1240 char[] oldseq = addedsq.getSequence();
1241 for (int c = 0; c < oldseq.length; c++)
1243 if (oldseq[c] == oldc)
1245 oldseq[c] = gapCharacter;
1249 addSequence(addedsq);
1253 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1254 for (int a = 0; alan != null && a < alan.length; a++)
1256 addAnnotation(alan[a]);
1258 AlignedCodonFrame[] acod = toappend.getCodonFrames();
1259 for (int a = 0; acod != null && a < acod.length; a++)
1261 this.addCodonFrame(acod[a]);
1263 List<SequenceGroup> sg = toappend.getGroups();
1266 for (SequenceGroup _sg:sg)
1271 if (toappend.getHiddenSequences() != null)
1273 HiddenSequences hs = toappend.getHiddenSequences();
1274 if (hiddenSequences == null)
1276 hiddenSequences = new HiddenSequences(this);
1278 if (hs.hiddenSequences != null)
1280 for (int s = 0; s < hs.hiddenSequences.length; s++)
1282 // hide the newly appended sequence in the alignment
1283 if (hs.hiddenSequences[s] != null)
1285 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1290 if (toappend.getProperties() != null)
1292 // we really can't do very much here - just try to concatenate strings
1293 // where property collisions occur.
1294 Enumeration key = toappend.getProperties().keys();
1295 while (key.hasMoreElements())
1297 Object k = key.nextElement();
1298 Object ourval = this.getProperty(k);
1299 Object toapprop = toappend.getProperty(k);
1302 if (ourval.getClass().equals(toapprop.getClass())
1303 && !ourval.equals(toapprop))
1305 if (ourval instanceof String)
1308 this.setProperty(k, ((String) ourval) + "; "
1309 + ((String) toapprop));
1313 if (ourval instanceof Vector)
1316 Enumeration theirv = ((Vector) toapprop).elements();
1317 while (theirv.hasMoreElements())
1319 ((Vector) ourval).addElement(theirv);
1327 // just add new property directly
1328 setProperty(k, toapprop);
1336 public AlignmentAnnotation findOrCreateAnnotation(String name, boolean autoCalc,
1337 SequenceI seqRef, SequenceGroup groupRef)
1339 for (AlignmentAnnotation annot :
1340 getAlignmentAnnotation())
1342 if (annot.autoCalculated == autoCalc
1343 && annot.getCalcId().equals(name)
1344 && annot.sequenceRef == seqRef && annot.groupRef == groupRef)
1349 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1350 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1351 annot.hasText = false;
1352 annot.setCalcId(new String(name));
1353 annot.autoCalculated = autoCalc;
1356 annot.setSequenceRef(seqRef);
1358 annot.groupRef = groupRef;
1359 addAnnotation(annot);
1365 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1367 ArrayList<AlignmentAnnotation> aa=new ArrayList<AlignmentAnnotation>();
1368 for (AlignmentAnnotation a:getAlignmentAnnotation())
1370 if (a.getCalcId()==calcId || (a.getCalcId()!=null && calcId!=null && a.getCalcId().equals(calcId)))
1379 public void moveSelectedSequencesByOne(SequenceGroup sg,
1380 Map<SequenceI, SequenceCollectionI> map, boolean up)
1382 synchronized (sequences)
1387 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1389 SequenceI seq = sequences.get(i);
1390 if (!sg.getSequences(map).contains(seq))
1395 SequenceI temp = sequences.get(i - 1);
1396 if (sg.getSequences(null).contains(temp))
1401 sequences.set(i, temp);
1402 sequences.set(i - 1, seq);
1407 for (int i = sequences.size() - 2; i > -1; i--)
1409 SequenceI seq = sequences.get(i);
1410 if (!sg.getSequences(map).contains(seq))
1415 SequenceI temp = sequences.get(i + 1);
1416 if (sg.getSequences(map).contains(temp))
1421 sequences.set(i, temp);
1422 sequences.set(i + 1, seq);