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.AlignmentUtils;
24 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
25 import jalview.io.FastaFile;
26 import jalview.util.Comparison;
27 import jalview.util.LinkedIdentityHashSet;
28 import jalview.util.MessageManager;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.Hashtable;
36 import java.util.List;
39 import java.util.Vector;
42 * Data structure to hold and manipulate a multiple sequence alignment
48 public class Alignment implements AlignmentI
50 private Alignment dataset;
52 protected List<SequenceI> sequences;
54 protected List<SequenceGroup> groups;
56 protected char gapCharacter = '-';
58 protected int type = NUCLEOTIDE;
60 public static final int PROTEIN = 0;
62 public static final int NUCLEOTIDE = 1;
64 public boolean hasRNAStructure = false;
66 public AlignmentAnnotation[] annotations;
68 HiddenSequences hiddenSequences;
70 public Hashtable alignmentProperties;
72 private List<AlignedCodonFrame> codonFrameList;
74 private void initAlignment(SequenceI[] seqs)
76 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
77 hiddenSequences = new HiddenSequences(this);
78 codonFrameList = new ArrayList<AlignedCodonFrame>();
80 if (Comparison.isNucleotide(seqs))
89 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
91 for (int i = 0; i < seqs.length; i++)
93 sequences.add(seqs[i]);
99 * Make a 'copy' alignment - sequences have new copies of features and
100 * annotations, but share the original dataset sequences.
102 public Alignment(AlignmentI al)
104 SequenceI[] seqs = al.getSequencesArray();
105 for (int i = 0; i < seqs.length; i++)
107 seqs[i] = new Sequence(seqs[i]);
113 * Share the same dataset sequence mappings (if any).
115 if (dataset == null && al.getDataset() == null)
117 this.setCodonFrames(al.getCodonFrames());
122 * Make an alignment from an array of Sequences.
126 public Alignment(SequenceI[] seqs)
132 * Make a new alignment from an array of SeqCigars
137 public Alignment(SeqCigar[] alseqs)
139 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
140 gapCharacter, new ColumnSelection(), null);
145 * Make a new alignment from an CigarArray JBPNote - can only do this when
146 * compactAlignment does not contain hidden regions. JBPNote - must also check
147 * that compactAlignment resolves to a set of SeqCigars - or construct them
150 * @param compactAlignment
153 public static AlignmentI createAlignment(CigarArray compactAlignment)
157 .getString("error.alignment_cigararray_not_implemented"));
158 // this(compactAlignment.refCigars);
162 public List<SequenceI> getSequences()
168 public List<SequenceI> getSequences(
169 Map<SequenceI, SequenceCollectionI> hiddenReps)
171 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
177 public SequenceI[] getSequencesArray()
179 if (sequences == null)
183 synchronized (sequences)
185 return sequences.toArray(new SequenceI[sequences.size()]);
190 * Returns a map of lists of sequences keyed by sequence name.
195 public Map<String, List<SequenceI>> getSequencesByName()
197 return AlignmentUtils.getSequencesByName(this);
206 * @return DOCUMENT ME!
209 public SequenceI getSequenceAt(int i)
211 synchronized (sequences)
213 if (i > -1 && i < sequences.size())
215 return sequences.get(i);
222 * Adds a sequence to the alignment. Recalculates maxLength and size.
227 public void addSequence(SequenceI snew)
232 // maintain dataset integrity
233 SequenceI dsseq = snew.getDatasetSequence();
236 // derive new sequence
237 SequenceI adding = snew.deriveSequence();
239 dsseq = snew.getDatasetSequence();
241 if (getDataset().findIndex(dsseq) == -1)
243 getDataset().addSequence(dsseq);
247 if (sequences == null)
249 initAlignment(new SequenceI[] { snew });
253 synchronized (sequences)
258 if (hiddenSequences != null)
260 hiddenSequences.adjustHeightSequenceAdded();
265 public SequenceI replaceSequenceAt(int i, SequenceI snew)
267 synchronized (sequences)
269 if (sequences.size() > i)
271 return sequences.set(i, snew);
277 hiddenSequences.adjustHeightSequenceAdded();
286 * @return DOCUMENT ME!
289 public List<SequenceGroup> getGroups()
295 public void finalize() throws Throwable
297 if (getDataset() != null)
299 getDataset().removeAlignmentRef();
307 * Defensively nulls out references in case this object is not garbage
310 void nullReferences()
316 hiddenSequences = null;
320 * decrement the alignmentRefs counter by one and null references if it goes
325 private void removeAlignmentRef() throws Throwable
327 if (--alignmentRefs == 0)
340 public void deleteSequence(SequenceI s)
342 deleteSequence(findIndex(s));
352 public void deleteSequence(int i)
354 if (i > -1 && i < getHeight())
356 synchronized (sequences)
359 hiddenSequences.adjustHeightSequenceDeleted(i);
367 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
370 public SequenceGroup findGroup(SequenceI s)
372 synchronized (groups)
374 for (int i = 0; i < this.groups.size(); i++)
376 SequenceGroup sg = groups.get(i);
378 if (sg.getSequences(null).contains(s))
391 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
394 public SequenceGroup[] findAllGroups(SequenceI s)
396 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
398 synchronized (groups)
400 int gSize = groups.size();
401 for (int i = 0; i < gSize; i++)
403 SequenceGroup sg = groups.get(i);
404 if (sg == null || sg.getSequences() == null)
406 this.deleteGroup(sg);
411 if (sg.getSequences().contains(s))
417 SequenceGroup[] ret = new SequenceGroup[temp.size()];
418 return temp.toArray(ret);
423 public void addGroup(SequenceGroup sg)
425 synchronized (groups)
427 if (!groups.contains(sg))
429 if (hiddenSequences.getSize() > 0)
431 int i, iSize = sg.getSize();
432 for (i = 0; i < iSize; i++)
434 if (!sequences.contains(sg.getSequenceAt(i)))
436 sg.deleteSequence(sg.getSequenceAt(i), false);
442 if (sg.getSize() < 1)
454 * remove any annotation that references gp
457 * (if null, removes all group associated annotation)
459 private void removeAnnotationForGroup(SequenceGroup gp)
461 if (annotations == null || annotations.length == 0)
465 // remove annotation very quickly
466 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
470 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
472 if (annotations[i].groupRef != null)
474 todelete[p++] = annotations[i];
478 tokeep[k++] = annotations[i];
484 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
486 if (annotations[i].groupRef == gp)
488 todelete[p++] = annotations[i];
492 tokeep[k++] = annotations[i];
498 // clear out the group associated annotation.
499 for (i = 0; i < p; i++)
501 unhookAnnotation(todelete[i]);
504 t = new AlignmentAnnotation[k];
505 for (i = 0; i < k; i++)
514 public void deleteAllGroups()
516 synchronized (groups)
518 if (annotations != null)
520 removeAnnotationForGroup(null);
522 for (SequenceGroup sg : groups)
532 public void deleteGroup(SequenceGroup g)
534 synchronized (groups)
536 if (groups.contains(g))
538 removeAnnotationForGroup(g);
547 public SequenceI findName(String name)
549 return findName(name, false);
555 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
558 public SequenceI findName(String token, boolean b)
560 return findName(null, token, b);
566 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
570 public SequenceI findName(SequenceI startAfter, String token, boolean b)
575 String sqname = null;
576 if (startAfter != null)
578 // try to find the sequence in the alignment
579 boolean matched = false;
580 while (i < sequences.size())
582 if (getSequenceAt(i++) == startAfter)
593 while (i < sequences.size())
595 sq = getSequenceAt(i);
596 sqname = sq.getName();
597 if (sqname.equals(token) // exact match
598 || (b && // allow imperfect matches - case varies
599 (sqname.equalsIgnoreCase(token))))
601 return getSequenceAt(i);
611 public SequenceI[] findSequenceMatch(String name)
613 Vector matches = new Vector();
616 while (i < sequences.size())
618 if (getSequenceAt(i).getName().equals(name))
620 matches.addElement(getSequenceAt(i));
625 SequenceI[] result = new SequenceI[matches.size()];
626 for (i = 0; i < result.length; i++)
628 result[i] = (SequenceI) matches.elementAt(i);
638 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
641 public int findIndex(SequenceI s)
645 while (i < sequences.size())
647 if (s == getSequenceAt(i))
662 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
665 public int findIndex(SearchResultsI results)
669 while (i < sequences.size())
671 if (results.involvesSequence(getSequenceAt(i)))
683 * @return DOCUMENT ME!
686 public int getHeight()
688 return sequences.size();
694 * @return DOCUMENT ME!
697 public int getWidth()
701 for (int i = 0; i < sequences.size(); i++)
703 if (getSequenceAt(i).getLength() > maxLength)
705 maxLength = getSequenceAt(i).getLength();
719 public void setGapCharacter(char gc)
722 synchronized (sequences)
724 for (SequenceI seq : sequences)
726 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
727 .replace('-', gc).replace(' ', gc));
735 * @return DOCUMENT ME!
738 public char getGapCharacter()
746 * @see jalview.datamodel.AlignmentI#isAligned()
749 public boolean isAligned()
751 return isAligned(false);
757 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
760 public boolean isAligned(boolean includeHidden)
762 int width = getWidth();
763 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
765 includeHidden = true; // no hidden sequences to check against.
767 for (int i = 0; i < sequences.size(); i++)
769 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
771 if (getSequenceAt(i).getLength() != width)
782 * Delete all annotations, including auto-calculated if the flag is set true.
783 * Returns true if at least one annotation was deleted, else false.
785 * @param includingAutoCalculated
789 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
791 boolean result = false;
792 for (AlignmentAnnotation alan : getAlignmentAnnotation())
794 if (!alan.autoCalculated || includingAutoCalculated)
796 deleteAnnotation(alan);
806 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
807 * AlignmentAnnotation)
810 public boolean deleteAnnotation(AlignmentAnnotation aa)
812 return deleteAnnotation(aa, true);
816 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
820 if (annotations != null)
822 aSize = annotations.length;
830 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
832 boolean swap = false;
835 for (int i = 0; i < aSize; i++)
837 if (annotations[i] == aa)
842 if (tIndex < temp.length)
844 temp[tIndex++] = annotations[i];
853 unhookAnnotation(aa);
860 * remove any object references associated with this annotation
864 private void unhookAnnotation(AlignmentAnnotation aa)
866 if (aa.sequenceRef != null)
868 aa.sequenceRef.removeAlignmentAnnotation(aa);
870 if (aa.groupRef != null)
872 // probably need to do more here in the future (post 2.5.0)
880 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
881 * AlignmentAnnotation)
884 public void addAnnotation(AlignmentAnnotation aa)
886 addAnnotation(aa, -1);
892 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
893 * AlignmentAnnotation, int)
896 public void addAnnotation(AlignmentAnnotation aa, int pos)
898 if (aa.getRNAStruc() != null)
900 hasRNAStructure = true;
904 if (annotations != null)
906 aSize = annotations.length + 1;
909 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
911 if (pos == -1 || pos >= aSize)
913 temp[aSize - 1] = aa;
922 for (i = 0; i < (aSize - 1); i++, p++)
930 temp[p] = annotations[i];
939 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
941 if (aa == null || annotations == null || annotations.length - 1 < index)
946 int aSize = annotations.length;
947 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
951 for (int i = 0; i < aSize; i++)
960 temp[i] = annotations[i];
964 temp[i] = annotations[i - 1];
973 * returns all annotation on the alignment
975 public AlignmentAnnotation[] getAlignmentAnnotation()
981 public void setNucleotide(boolean b)
994 public boolean isNucleotide()
996 if (type == NUCLEOTIDE)
1007 public boolean hasRNAStructure()
1009 // TODO can it happen that structure is removed from alignment?
1010 return hasRNAStructure;
1014 public void setDataset(AlignmentI data)
1016 if (dataset == null && data == null)
1018 createDatasetAlignment();
1020 else if (dataset == null && data != null)
1022 if (!(data instanceof Alignment))
1025 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1027 dataset = (Alignment) data;
1028 for (int i = 0; i < getHeight(); i++)
1030 SequenceI currentSeq = getSequenceAt(i);
1031 SequenceI dsq = currentSeq.getDatasetSequence();
1034 dsq = currentSeq.createDatasetSequence();
1035 dataset.addSequence(dsq);
1039 while (dsq.getDatasetSequence() != null)
1041 dsq = dsq.getDatasetSequence();
1043 if (dataset.findIndex(dsq) == -1)
1045 dataset.addSequence(dsq);
1050 dataset.addAlignmentRef();
1054 * add dataset sequences to seq for currentSeq and any sequences it references
1056 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1057 Set<SequenceI> seqs, boolean createDatasetSequence)
1059 SequenceI alignedSeq = currentSeq;
1060 if (currentSeq.getDatasetSequence() != null)
1062 currentSeq = currentSeq.getDatasetSequence();
1066 if (createDatasetSequence)
1068 currentSeq = currentSeq.createDatasetSequence();
1071 if (seqs.contains(currentSeq))
1075 List<SequenceI> toProcess = new ArrayList<SequenceI>();
1076 toProcess.add(currentSeq);
1077 while (toProcess.size() > 0)
1080 SequenceI curDs = toProcess.remove(0);
1081 if (seqs.contains(curDs))
1086 // iterate over database references, making sure we add forward referenced
1088 if (curDs.getDBRefs() != null)
1090 for (DBRefEntry dbr : curDs.getDBRefs())
1092 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1094 if (dbr.getMap().getTo() == alignedSeq)
1097 * update mapping to be to the newly created dataset sequence
1099 dbr.getMap().setTo(currentSeq);
1101 if (dbr.getMap().getTo().getDatasetSequence() != null)
1104 "Implementation error: Map.getTo() for dbref " + dbr
1105 + " from " + curDs.getName()
1106 + " is not a dataset sequence.");
1108 // we recurse to add all forward references to dataset sequences via
1110 toProcess.add(dbr.getMap().getTo());
1118 * Creates a new dataset for this alignment. Can only be done once - if
1119 * dataset is not null this will not be performed.
1121 public void createDatasetAlignment()
1123 if (dataset != null)
1127 // try to avoid using SequenceI.equals at this stage, it will be expensive
1128 Set<SequenceI> seqs = new LinkedIdentityHashSet<SequenceI>();
1130 for (int i = 0; i < getHeight(); i++)
1132 SequenceI currentSeq = getSequenceAt(i);
1133 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1136 // verify all mappings are in dataset
1137 for (AlignedCodonFrame cf : codonFrameList)
1139 for (SequenceToSequenceMapping ssm : cf.getMappings())
1141 if (!seqs.contains(ssm.getFromSeq()))
1143 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1145 if (!seqs.contains(ssm.getMapping().getTo()))
1147 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1151 // finally construct dataset
1152 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1153 // move mappings to the dataset alignment
1154 dataset.codonFrameList = this.codonFrameList;
1155 this.codonFrameList = null;
1159 * reference count for number of alignments referencing this one.
1161 int alignmentRefs = 0;
1164 * increase reference count to this alignment.
1166 private void addAlignmentRef()
1172 public Alignment getDataset()
1178 public boolean padGaps()
1180 boolean modified = false;
1182 // Remove excess gaps from the end of alignment
1186 for (int i = 0; i < sequences.size(); i++)
1188 current = getSequenceAt(i);
1189 for (int j = current.getLength(); j > maxLength; j--)
1192 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1203 for (int i = 0; i < sequences.size(); i++)
1205 current = getSequenceAt(i);
1206 cLength = current.getLength();
1208 if (cLength < maxLength)
1210 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1213 else if (current.getLength() > maxLength)
1215 current.deleteChars(maxLength, current.getLength());
1222 * Justify the sequences to the left or right by deleting and inserting gaps
1223 * before the initial residue or after the terminal residue
1226 * true if alignment padded to right, false to justify to left
1227 * @return true if alignment was changed
1230 public boolean justify(boolean right)
1232 boolean modified = false;
1234 // Remove excess gaps from the end of alignment
1236 int ends[] = new int[sequences.size() * 2];
1238 for (int i = 0; i < sequences.size(); i++)
1240 current = getSequenceAt(i);
1241 // This should really be a sequence method
1242 ends[i * 2] = current.findIndex(current.getStart());
1243 ends[i * 2 + 1] = current.findIndex(current.getStart()
1244 + current.getLength());
1245 boolean hitres = false;
1246 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1248 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1257 ends[i * 2 + 1] = j;
1258 if (j - ends[i * 2] > maxLength)
1260 maxLength = j - ends[i * 2];
1268 // now edit the flanking gaps to justify to either left or right
1269 int cLength, extent, diff;
1270 for (int i = 0; i < sequences.size(); i++)
1272 current = getSequenceAt(i);
1274 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1275 diff = maxLength - cLength; // number of gaps to indent
1276 extent = current.getLength();
1280 if (extent > ends[i * 2 + 1])
1282 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1285 if (ends[i * 2] > diff)
1287 current.deleteChars(0, ends[i * 2] - diff);
1292 if (ends[i * 2] < diff)
1294 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1302 if (ends[i * 2] > 0)
1304 current.deleteChars(0, ends[i * 2]);
1306 ends[i * 2 + 1] -= ends[i * 2];
1307 extent -= ends[i * 2];
1309 if (extent > maxLength)
1311 current.deleteChars(maxLength + 1, extent);
1316 if (extent < maxLength)
1318 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1328 public HiddenSequences getHiddenSequences()
1330 return hiddenSequences;
1334 public CigarArray getCompactAlignment()
1336 synchronized (sequences)
1338 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1340 for (SequenceI seq : sequences)
1342 alseqs[i++] = new SeqCigar(seq);
1344 CigarArray cal = new CigarArray(alseqs);
1345 cal.addOperation(CigarArray.M, getWidth());
1351 public void setProperty(Object key, Object value)
1353 if (alignmentProperties == null)
1355 alignmentProperties = new Hashtable();
1358 alignmentProperties.put(key, value);
1362 public Object getProperty(Object key)
1364 if (alignmentProperties != null)
1366 return alignmentProperties.get(key);
1375 public Hashtable getProperties()
1377 return alignmentProperties;
1381 * Adds the given mapping to the stored set. Note this may be held on the
1382 * dataset alignment.
1385 public void addCodonFrame(AlignedCodonFrame codons)
1387 List<AlignedCodonFrame> acfs = getCodonFrames();
1388 if (codons != null && acfs != null && !acfs.contains(codons))
1398 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1401 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1407 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1408 for (AlignedCodonFrame acf : getCodonFrames())
1410 if (acf.involvesSequence(seq))
1419 * Sets the codon frame mappings (replacing any existing mappings). Note the
1420 * mappings are set on the dataset alignment instead if there is one.
1422 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1425 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1427 if (dataset != null)
1429 dataset.setCodonFrames(acfs);
1433 this.codonFrameList = acfs;
1438 * Returns the set of codon frame mappings. Any changes to the returned set
1439 * will affect the alignment. The mappings are held on (and read from) the
1440 * dataset alignment if there is one.
1442 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1445 public List<AlignedCodonFrame> getCodonFrames()
1447 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1448 // this behaviour is currently incorrect. method should return codon frames
1449 // for just the alignment,
1450 // selected from dataset
1451 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1455 * Removes the given mapping from the stored set. Note that the mappings are
1456 * held on the dataset alignment if there is one.
1459 public boolean removeCodonFrame(AlignedCodonFrame codons)
1461 List<AlignedCodonFrame> acfs = getCodonFrames();
1462 if (codons == null || acfs == null)
1466 return acfs.remove(codons);
1470 public void append(AlignmentI toappend)
1472 // TODO JAL-1270 needs test coverage
1473 // currently tested for use in jalview.gui.SequenceFetcher
1474 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1475 char oldc = toappend.getGapCharacter();
1476 boolean hashidden = toappend.getHiddenSequences() != null
1477 && toappend.getHiddenSequences().hiddenSequences != null;
1478 // get all sequences including any hidden ones
1479 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1480 .getFullAlignment().getSequences() : toappend.getSequences();
1483 // avoid self append deadlock by
1484 List<SequenceI> toappendsq = new ArrayList<SequenceI>();
1487 for (SequenceI addedsq : sqs)
1491 char[] oldseq = addedsq.getSequence();
1492 for (int c = 0; c < oldseq.length; c++)
1494 if (oldseq[c] == oldc)
1496 oldseq[c] = gapCharacter;
1500 toappendsq.add(addedsq);
1503 for (SequenceI addedsq : toappendsq)
1505 addSequence(addedsq);
1508 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1509 for (int a = 0; alan != null && a < alan.length; a++)
1511 addAnnotation(alan[a]);
1515 getCodonFrames().addAll(toappend.getCodonFrames());
1517 List<SequenceGroup> sg = toappend.getGroups();
1520 for (SequenceGroup _sg : sg)
1525 if (toappend.getHiddenSequences() != null)
1527 HiddenSequences hs = toappend.getHiddenSequences();
1528 if (hiddenSequences == null)
1530 hiddenSequences = new HiddenSequences(this);
1532 if (hs.hiddenSequences != null)
1534 for (int s = 0; s < hs.hiddenSequences.length; s++)
1536 // hide the newly appended sequence in the alignment
1537 if (hs.hiddenSequences[s] != null)
1539 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1544 if (toappend.getProperties() != null)
1546 // we really can't do very much here - just try to concatenate strings
1547 // where property collisions occur.
1548 Enumeration key = toappend.getProperties().keys();
1549 while (key.hasMoreElements())
1551 Object k = key.nextElement();
1552 Object ourval = this.getProperty(k);
1553 Object toapprop = toappend.getProperty(k);
1556 if (ourval.getClass().equals(toapprop.getClass())
1557 && !ourval.equals(toapprop))
1559 if (ourval instanceof String)
1562 this.setProperty(k, ((String) ourval) + "; "
1563 + ((String) toapprop));
1567 if (ourval instanceof Vector)
1570 Enumeration theirv = ((Vector) toapprop).elements();
1571 while (theirv.hasMoreElements())
1573 ((Vector) ourval).addElement(theirv);
1581 // just add new property directly
1582 setProperty(k, toapprop);
1590 public AlignmentAnnotation findOrCreateAnnotation(String name,
1591 String calcId, boolean autoCalc, SequenceI seqRef,
1592 SequenceGroup groupRef)
1594 assert (name != null);
1595 if (annotations != null)
1597 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1599 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1600 && (calcId == null || annot.getCalcId().equals(calcId))
1601 && annot.sequenceRef == seqRef
1602 && annot.groupRef == groupRef)
1608 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1609 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1610 annot.hasText = false;
1611 annot.setCalcId(new String(calcId));
1612 annot.autoCalculated = autoCalc;
1615 annot.setSequenceRef(seqRef);
1617 annot.groupRef = groupRef;
1618 addAnnotation(annot);
1624 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1626 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1627 for (AlignmentAnnotation a : getAlignmentAnnotation())
1629 if (a.getCalcId() == calcId
1630 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1640 * Returns an iterable collection of any annotations that match on given
1641 * sequence ref, calcId and label (ignoring null values).
1644 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1645 String calcId, String label)
1647 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1648 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1650 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1651 && ann.sequenceRef != null && ann.sequenceRef == seq
1652 && ann.label != null && ann.label.equals(label))
1661 public void moveSelectedSequencesByOne(SequenceGroup sg,
1662 Map<SequenceI, SequenceCollectionI> map, boolean up)
1664 synchronized (sequences)
1669 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1671 SequenceI seq = sequences.get(i);
1672 if (!sg.getSequences(map).contains(seq))
1677 SequenceI temp = sequences.get(i - 1);
1678 if (sg.getSequences(null).contains(temp))
1683 sequences.set(i, temp);
1684 sequences.set(i - 1, seq);
1689 for (int i = sequences.size() - 2; i > -1; i--)
1691 SequenceI seq = sequences.get(i);
1692 if (!sg.getSequences(map).contains(seq))
1697 SequenceI temp = sequences.get(i + 1);
1698 if (sg.getSequences(map).contains(temp))
1703 sequences.set(i, temp);
1704 sequences.set(i + 1, seq);
1712 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1714 alignmentAnnotation.validateRangeAndDisplay();
1715 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1717 hasRNAStructure = true;
1721 private SequenceI seqrep = null;
1725 * @return the representative sequence for this group
1728 public SequenceI getSeqrep()
1734 * set the representative sequence for this group. Note - this affects the
1735 * interpretation of the Hidereps attribute.
1738 * the seqrep to set (null means no sequence representative)
1741 public void setSeqrep(SequenceI seqrep)
1743 this.seqrep = seqrep;
1748 * @return true if group has a sequence representative
1751 public boolean hasSeqrep()
1753 return seqrep != null;
1757 public int getEndRes()
1759 return getWidth() - 1;
1763 public int getStartRes()
1769 * In the case of AlignmentI - returns the dataset for the alignment, if set
1772 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1775 public AnnotatedCollectionI getContext()
1781 * Align this alignment like the given (mapped) one.
1784 public int alignAs(AlignmentI al)
1787 * Currently retains unmapped gaps (in introns), regaps mapped regions
1790 return alignAs(al, false, true);
1794 * Align this alignment 'the same as' the given one. Mapped sequences only are
1795 * realigned. If both of the same type (nucleotide/protein) then align both
1796 * identically. If this is nucleotide and the other is protein, make 3 gaps
1797 * for each gap in the protein sequences. If this is protein and the other is
1798 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1799 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1800 * protein to match the relative ordering of codons in the nucleotide.
1802 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1803 * regions are preserved. Gaps that connect introns to exons are treated
1804 * conservatively, i.e. only preserved if both intron and exon gaps are
1805 * preserved. TODO: check caveats below where the implementation fails
1808 * - must have same dataset, and sequences in al must have equivalent
1809 * dataset sequence and start/end bounds under given mapping
1810 * @param preserveMappedGaps
1811 * if true, gaps within and between mapped codons are preserved
1812 * @param preserveUnmappedGaps
1813 * if true, gaps within and between unmapped codons are preserved
1816 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1817 boolean preserveUnmappedGaps)
1819 // TODO should this method signature be the one in the interface?
1820 // JBPComment - yes - neither flag is used, so should be deleted.
1821 boolean thisIsNucleotide = this.isNucleotide();
1822 boolean thatIsProtein = !al.isNucleotide();
1823 if (!thatIsProtein && !thisIsNucleotide)
1825 return AlignmentUtils.alignProteinAsDna(this, al);
1827 else if (thatIsProtein && thisIsNucleotide)
1829 return AlignmentUtils.alignCdsAsProtein(this, al);
1831 return AlignmentUtils.alignAs(this, al);
1835 * Returns the alignment in Fasta format. Behaviour of this method is not
1836 * guaranteed between versions.
1839 public String toString()
1841 return new FastaFile().print(getSequencesArray(), true);
1845 * Returns the set of distinct sequence names. No ordering is guaranteed.
1848 public Set<String> getSequenceNames()
1850 Set<String> names = new HashSet<String>();
1851 for (SequenceI seq : getSequences())
1853 names.add(seq.getName());
1859 public boolean hasValidSequence()
1861 boolean hasValidSeq = false;
1862 for (SequenceI seq : getSequences())
1864 if ((seq.getEnd() - seq.getStart()) > 0)
1874 * Update any mappings to 'virtual' sequences to compatible real ones, if
1875 * present in the added sequences. Returns a count of mappings updated.
1881 public int realiseMappings(List<SequenceI> seqs)
1884 for (SequenceI seq : seqs)
1886 for (AlignedCodonFrame mapping : getCodonFrames())
1888 count += mapping.realiseWith(seq);
1895 * Returns the first AlignedCodonFrame that has a mapping between the given
1903 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1905 for (AlignedCodonFrame acf : getCodonFrames())
1907 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1916 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1918 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1919 int startPos = alignmentStartEnd[0];
1920 int endPos = alignmentStartEnd[1];
1922 int[] lowestRange = new int[] { -1, -1 };
1923 int[] higestRange = new int[] { -1, -1 };
1925 for (int[] hiddenCol : hiddenCols)
1927 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1928 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1931 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1933 startPos = alignmentStartEnd[0];
1937 startPos = lowestRange[1] + 1;
1940 if (higestRange[0] == -1 && higestRange[1] == -1)
1942 endPos = alignmentStartEnd[1];
1946 endPos = higestRange[0] - 1;
1948 return new int[] { startPos, endPos };
1951 Map<Object, ContactMatrixI> contactmaps = new HashMap<Object, ContactMatrixI>();
1954 ContactListI getContactListFor(AlignmentAnnotation _aa, int column)
1956 ContactMatrixI cm = contactmaps.get(_aa.annotationId);
1961 return cm.getContactList(column);
1965 public AlignmentAnnotation addContactList(ContactMatrixI cm)
1967 Annotation _aa[] = new Annotation[getWidth()];
1968 Annotation dummy = new Annotation(0.0f);
1969 for (int i = 0; i < _aa.length; _aa[i++] = dummy)
1973 AlignmentAnnotation aa = new AlignmentAnnotation("Contact Matrix",
1974 "Contact Matrix", _aa);
1975 aa.graph = AlignmentAnnotation.CUSTOMRENDERER;
1976 contactmaps.put(aa.annotationId, cm);