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.HashSet;
34 import java.util.Hashtable;
35 import java.util.List;
38 import java.util.Vector;
41 * Data structure to hold and manipulate a multiple sequence alignment
47 public class Alignment implements AlignmentI
49 private Alignment dataset;
51 protected List<SequenceI> sequences;
53 protected List<SequenceGroup> groups;
55 protected char gapCharacter = '-';
57 private boolean nucleotide = true;
59 public boolean hasRNAStructure = false;
61 public AlignmentAnnotation[] annotations;
63 HiddenSequences hiddenSequences;
65 public Hashtable alignmentProperties;
67 private List<AlignedCodonFrame> codonFrameList;
69 private void initAlignment(SequenceI[] seqs)
71 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
72 hiddenSequences = new HiddenSequences(this);
73 codonFrameList = new ArrayList<AlignedCodonFrame>();
75 nucleotide = Comparison.isNucleotide(seqs);
77 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
79 for (int i = 0; i < seqs.length; i++)
81 sequences.add(seqs[i]);
87 * Make a 'copy' alignment - sequences have new copies of features and
88 * annotations, but share the original dataset sequences.
90 public Alignment(AlignmentI al)
92 SequenceI[] seqs = al.getSequencesArray();
93 for (int i = 0; i < seqs.length; i++)
95 seqs[i] = new Sequence(seqs[i]);
101 * Share the same dataset sequence mappings (if any).
103 if (dataset == null && al.getDataset() == null)
105 this.setCodonFrames(al.getCodonFrames());
110 * Make an alignment from an array of Sequences.
114 public Alignment(SequenceI[] seqs)
120 * Make a new alignment from an array of SeqCigars
125 public Alignment(SeqCigar[] alseqs)
127 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
128 gapCharacter, new ColumnSelection(), null);
133 * Make a new alignment from an CigarArray JBPNote - can only do this when
134 * compactAlignment does not contain hidden regions. JBPNote - must also check
135 * that compactAlignment resolves to a set of SeqCigars - or construct them
138 * @param compactAlignment
141 public static AlignmentI createAlignment(CigarArray compactAlignment)
145 .getString("error.alignment_cigararray_not_implemented"));
146 // this(compactAlignment.refCigars);
150 public List<SequenceI> getSequences()
156 public List<SequenceI> getSequences(
157 Map<SequenceI, SequenceCollectionI> hiddenReps)
159 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
165 public SequenceI[] getSequencesArray()
167 if (sequences == null)
171 synchronized (sequences)
173 return sequences.toArray(new SequenceI[sequences.size()]);
178 * Returns a map of lists of sequences keyed by sequence name.
183 public Map<String, List<SequenceI>> getSequencesByName()
185 return AlignmentUtils.getSequencesByName(this);
194 * @return DOCUMENT ME!
197 public SequenceI getSequenceAt(int i)
199 synchronized (sequences)
201 if (i > -1 && i < sequences.size())
203 return sequences.get(i);
210 * Adds a sequence to the alignment. Recalculates maxLength and size. Note
211 * this currently does not recalculate whether or not the alignment is
212 * nucleotide, so mixed alignments may have undefined behaviour.
217 public void addSequence(SequenceI snew)
222 // maintain dataset integrity
223 SequenceI dsseq = snew.getDatasetSequence();
226 // derive new sequence
227 SequenceI adding = snew.deriveSequence();
229 dsseq = snew.getDatasetSequence();
231 if (getDataset().findIndex(dsseq) == -1)
233 getDataset().addSequence(dsseq);
237 if (sequences == null)
239 initAlignment(new SequenceI[] { snew });
243 synchronized (sequences)
248 if (hiddenSequences != null)
250 hiddenSequences.adjustHeightSequenceAdded();
255 public SequenceI replaceSequenceAt(int i, SequenceI snew)
257 synchronized (sequences)
259 if (sequences.size() > i)
261 return sequences.set(i, snew);
267 hiddenSequences.adjustHeightSequenceAdded();
276 * @return DOCUMENT ME!
279 public List<SequenceGroup> getGroups()
285 public void finalize() throws Throwable
287 if (getDataset() != null)
289 getDataset().removeAlignmentRef();
297 * Defensively nulls out references in case this object is not garbage
300 void nullReferences()
306 hiddenSequences = null;
310 * decrement the alignmentRefs counter by one and null references if it goes
315 private void removeAlignmentRef() throws Throwable
317 if (--alignmentRefs == 0)
330 public void deleteSequence(SequenceI s)
332 deleteSequence(findIndex(s));
342 public void deleteSequence(int i)
344 if (i > -1 && i < getHeight())
346 synchronized (sequences)
349 hiddenSequences.adjustHeightSequenceDeleted(i);
357 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
360 public SequenceGroup findGroup(SequenceI s)
362 synchronized (groups)
364 for (int i = 0; i < this.groups.size(); i++)
366 SequenceGroup sg = groups.get(i);
368 if (sg.getSequences(null).contains(s))
381 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
384 public SequenceGroup[] findAllGroups(SequenceI s)
386 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
388 synchronized (groups)
390 int gSize = groups.size();
391 for (int i = 0; i < gSize; i++)
393 SequenceGroup sg = groups.get(i);
394 if (sg == null || sg.getSequences() == null)
396 this.deleteGroup(sg);
401 if (sg.getSequences().contains(s))
407 SequenceGroup[] ret = new SequenceGroup[temp.size()];
408 return temp.toArray(ret);
413 public void addGroup(SequenceGroup sg)
415 synchronized (groups)
417 if (!groups.contains(sg))
419 if (hiddenSequences.getSize() > 0)
421 int i, iSize = sg.getSize();
422 for (i = 0; i < iSize; i++)
424 if (!sequences.contains(sg.getSequenceAt(i)))
426 sg.deleteSequence(sg.getSequenceAt(i), false);
432 if (sg.getSize() < 1)
444 * remove any annotation that references gp
447 * (if null, removes all group associated annotation)
449 private void removeAnnotationForGroup(SequenceGroup gp)
451 if (annotations == null || annotations.length == 0)
455 // remove annotation very quickly
456 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
460 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
462 if (annotations[i].groupRef != null)
464 todelete[p++] = annotations[i];
468 tokeep[k++] = annotations[i];
474 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
476 if (annotations[i].groupRef == gp)
478 todelete[p++] = annotations[i];
482 tokeep[k++] = annotations[i];
488 // clear out the group associated annotation.
489 for (i = 0; i < p; i++)
491 unhookAnnotation(todelete[i]);
494 t = new AlignmentAnnotation[k];
495 for (i = 0; i < k; i++)
504 public void deleteAllGroups()
506 synchronized (groups)
508 if (annotations != null)
510 removeAnnotationForGroup(null);
512 for (SequenceGroup sg : groups)
522 public void deleteGroup(SequenceGroup g)
524 synchronized (groups)
526 if (groups.contains(g))
528 removeAnnotationForGroup(g);
537 public SequenceI findName(String name)
539 return findName(name, false);
545 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
548 public SequenceI findName(String token, boolean b)
550 return findName(null, token, b);
556 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
560 public SequenceI findName(SequenceI startAfter, String token, boolean b)
565 String sqname = null;
566 if (startAfter != null)
568 // try to find the sequence in the alignment
569 boolean matched = false;
570 while (i < sequences.size())
572 if (getSequenceAt(i++) == startAfter)
583 while (i < sequences.size())
585 sq = getSequenceAt(i);
586 sqname = sq.getName();
587 if (sqname.equals(token) // exact match
588 || (b && // allow imperfect matches - case varies
589 (sqname.equalsIgnoreCase(token))))
591 return getSequenceAt(i);
601 public SequenceI[] findSequenceMatch(String name)
603 Vector matches = new Vector();
606 while (i < sequences.size())
608 if (getSequenceAt(i).getName().equals(name))
610 matches.addElement(getSequenceAt(i));
615 SequenceI[] result = new SequenceI[matches.size()];
616 for (i = 0; i < result.length; i++)
618 result[i] = (SequenceI) matches.elementAt(i);
628 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
631 public int findIndex(SequenceI s)
635 while (i < sequences.size())
637 if (s == getSequenceAt(i))
652 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
655 public int findIndex(SearchResultsI results)
659 while (i < sequences.size())
661 if (results.involvesSequence(getSequenceAt(i)))
673 * @return DOCUMENT ME!
676 public int getHeight()
678 return sequences.size();
684 * @return DOCUMENT ME!
687 public int getWidth()
691 for (int i = 0; i < sequences.size(); i++)
693 if (getSequenceAt(i).getLength() > maxLength)
695 maxLength = getSequenceAt(i).getLength();
709 public void setGapCharacter(char gc)
712 synchronized (sequences)
714 for (SequenceI seq : sequences)
716 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
717 .replace('-', gc).replace(' ', gc));
725 * @return DOCUMENT ME!
728 public char getGapCharacter()
736 * @see jalview.datamodel.AlignmentI#isAligned()
739 public boolean isAligned()
741 return isAligned(false);
747 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
750 public boolean isAligned(boolean includeHidden)
752 int width = getWidth();
753 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
755 includeHidden = true; // no hidden sequences to check against.
757 for (int i = 0; i < sequences.size(); i++)
759 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
761 if (getSequenceAt(i).getLength() != width)
772 * Delete all annotations, including auto-calculated if the flag is set true.
773 * Returns true if at least one annotation was deleted, else false.
775 * @param includingAutoCalculated
779 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
781 boolean result = false;
782 for (AlignmentAnnotation alan : getAlignmentAnnotation())
784 if (!alan.autoCalculated || includingAutoCalculated)
786 deleteAnnotation(alan);
796 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
797 * AlignmentAnnotation)
800 public boolean deleteAnnotation(AlignmentAnnotation aa)
802 return deleteAnnotation(aa, true);
806 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
810 if (annotations != null)
812 aSize = annotations.length;
820 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
822 boolean swap = false;
825 for (int i = 0; i < aSize; i++)
827 if (annotations[i] == aa)
832 if (tIndex < temp.length)
834 temp[tIndex++] = annotations[i];
843 unhookAnnotation(aa);
850 * remove any object references associated with this annotation
854 private void unhookAnnotation(AlignmentAnnotation aa)
856 if (aa.sequenceRef != null)
858 aa.sequenceRef.removeAlignmentAnnotation(aa);
860 if (aa.groupRef != null)
862 // probably need to do more here in the future (post 2.5.0)
870 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
871 * AlignmentAnnotation)
874 public void addAnnotation(AlignmentAnnotation aa)
876 addAnnotation(aa, -1);
882 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
883 * AlignmentAnnotation, int)
886 public void addAnnotation(AlignmentAnnotation aa, int pos)
888 if (aa.getRNAStruc() != null)
890 hasRNAStructure = true;
894 if (annotations != null)
896 aSize = annotations.length + 1;
899 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
901 if (pos == -1 || pos >= aSize)
903 temp[aSize - 1] = aa;
912 for (i = 0; i < (aSize - 1); i++, p++)
920 temp[p] = annotations[i];
929 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
931 if (aa == null || annotations == null || annotations.length - 1 < index)
936 int aSize = annotations.length;
937 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
941 for (int i = 0; i < aSize; i++)
950 temp[i] = annotations[i];
954 temp[i] = annotations[i - 1];
963 * returns all annotation on the alignment
965 public AlignmentAnnotation[] getAlignmentAnnotation()
971 public boolean isNucleotide()
977 public boolean hasRNAStructure()
979 // TODO can it happen that structure is removed from alignment?
980 return hasRNAStructure;
984 public void setDataset(AlignmentI data)
986 if (dataset == null && data == null)
988 createDatasetAlignment();
990 else if (dataset == null && data != null)
992 if (!(data instanceof Alignment))
995 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
997 dataset = (Alignment) data;
998 for (int i = 0; i < getHeight(); i++)
1000 SequenceI currentSeq = getSequenceAt(i);
1001 SequenceI dsq = currentSeq.getDatasetSequence();
1004 dsq = currentSeq.createDatasetSequence();
1005 dataset.addSequence(dsq);
1009 while (dsq.getDatasetSequence() != null)
1011 dsq = dsq.getDatasetSequence();
1013 if (dataset.findIndex(dsq) == -1)
1015 dataset.addSequence(dsq);
1020 dataset.addAlignmentRef();
1024 * add dataset sequences to seq for currentSeq and any sequences it references
1026 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1027 Set<SequenceI> seqs, boolean createDatasetSequence)
1029 SequenceI alignedSeq = currentSeq;
1030 if (currentSeq.getDatasetSequence() != null)
1032 currentSeq = currentSeq.getDatasetSequence();
1036 if (createDatasetSequence)
1038 currentSeq = currentSeq.createDatasetSequence();
1041 if (seqs.contains(currentSeq))
1045 List<SequenceI> toProcess = new ArrayList<SequenceI>();
1046 toProcess.add(currentSeq);
1047 while (toProcess.size() > 0)
1050 SequenceI curDs = toProcess.remove(0);
1051 if (seqs.contains(curDs))
1056 // iterate over database references, making sure we add forward referenced
1058 if (curDs.getDBRefs() != null)
1060 for (DBRefEntry dbr : curDs.getDBRefs())
1062 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1064 if (dbr.getMap().getTo() == alignedSeq)
1067 * update mapping to be to the newly created dataset sequence
1069 dbr.getMap().setTo(currentSeq);
1071 if (dbr.getMap().getTo().getDatasetSequence() != null)
1074 "Implementation error: Map.getTo() for dbref " + dbr
1075 + " from " + curDs.getName()
1076 + " is not a dataset sequence.");
1078 // we recurse to add all forward references to dataset sequences via
1080 toProcess.add(dbr.getMap().getTo());
1088 * Creates a new dataset for this alignment. Can only be done once - if
1089 * dataset is not null this will not be performed.
1091 public void createDatasetAlignment()
1093 if (dataset != null)
1097 // try to avoid using SequenceI.equals at this stage, it will be expensive
1098 Set<SequenceI> seqs = new LinkedIdentityHashSet<SequenceI>();
1100 for (int i = 0; i < getHeight(); i++)
1102 SequenceI currentSeq = getSequenceAt(i);
1103 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1106 // verify all mappings are in dataset
1107 for (AlignedCodonFrame cf : codonFrameList)
1109 for (SequenceToSequenceMapping ssm : cf.getMappings())
1111 if (!seqs.contains(ssm.getFromSeq()))
1113 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1115 if (!seqs.contains(ssm.getMapping().getTo()))
1117 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1121 // finally construct dataset
1122 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1123 // move mappings to the dataset alignment
1124 dataset.codonFrameList = this.codonFrameList;
1125 this.codonFrameList = null;
1129 * reference count for number of alignments referencing this one.
1131 int alignmentRefs = 0;
1134 * increase reference count to this alignment.
1136 private void addAlignmentRef()
1142 public Alignment getDataset()
1148 public boolean padGaps()
1150 boolean modified = false;
1152 // Remove excess gaps from the end of alignment
1156 for (int i = 0; i < sequences.size(); i++)
1158 current = getSequenceAt(i);
1159 for (int j = current.getLength(); j > maxLength; j--)
1162 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1173 for (int i = 0; i < sequences.size(); i++)
1175 current = getSequenceAt(i);
1176 cLength = current.getLength();
1178 if (cLength < maxLength)
1180 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1183 else if (current.getLength() > maxLength)
1185 current.deleteChars(maxLength, current.getLength());
1192 * Justify the sequences to the left or right by deleting and inserting gaps
1193 * before the initial residue or after the terminal residue
1196 * true if alignment padded to right, false to justify to left
1197 * @return true if alignment was changed
1200 public boolean justify(boolean right)
1202 boolean modified = false;
1204 // Remove excess gaps from the end of alignment
1206 int ends[] = new int[sequences.size() * 2];
1208 for (int i = 0; i < sequences.size(); i++)
1210 current = getSequenceAt(i);
1211 // This should really be a sequence method
1212 ends[i * 2] = current.findIndex(current.getStart());
1213 ends[i * 2 + 1] = current.findIndex(current.getStart()
1214 + current.getLength());
1215 boolean hitres = false;
1216 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1218 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1227 ends[i * 2 + 1] = j;
1228 if (j - ends[i * 2] > maxLength)
1230 maxLength = j - ends[i * 2];
1238 // now edit the flanking gaps to justify to either left or right
1239 int cLength, extent, diff;
1240 for (int i = 0; i < sequences.size(); i++)
1242 current = getSequenceAt(i);
1244 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1245 diff = maxLength - cLength; // number of gaps to indent
1246 extent = current.getLength();
1250 if (extent > ends[i * 2 + 1])
1252 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1255 if (ends[i * 2] > diff)
1257 current.deleteChars(0, ends[i * 2] - diff);
1262 if (ends[i * 2] < diff)
1264 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1272 if (ends[i * 2] > 0)
1274 current.deleteChars(0, ends[i * 2]);
1276 ends[i * 2 + 1] -= ends[i * 2];
1277 extent -= ends[i * 2];
1279 if (extent > maxLength)
1281 current.deleteChars(maxLength + 1, extent);
1286 if (extent < maxLength)
1288 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1298 public HiddenSequences getHiddenSequences()
1300 return hiddenSequences;
1304 public CigarArray getCompactAlignment()
1306 synchronized (sequences)
1308 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1310 for (SequenceI seq : sequences)
1312 alseqs[i++] = new SeqCigar(seq);
1314 CigarArray cal = new CigarArray(alseqs);
1315 cal.addOperation(CigarArray.M, getWidth());
1321 public void setProperty(Object key, Object value)
1323 if (alignmentProperties == null)
1325 alignmentProperties = new Hashtable();
1328 alignmentProperties.put(key, value);
1332 public Object getProperty(Object key)
1334 if (alignmentProperties != null)
1336 return alignmentProperties.get(key);
1345 public Hashtable getProperties()
1347 return alignmentProperties;
1351 * Adds the given mapping to the stored set. Note this may be held on the
1352 * dataset alignment.
1355 public void addCodonFrame(AlignedCodonFrame codons)
1357 List<AlignedCodonFrame> acfs = getCodonFrames();
1358 if (codons != null && acfs != null && !acfs.contains(codons))
1368 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1371 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1377 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1378 for (AlignedCodonFrame acf : getCodonFrames())
1380 if (acf.involvesSequence(seq))
1389 * Sets the codon frame mappings (replacing any existing mappings). Note the
1390 * mappings are set on the dataset alignment instead if there is one.
1392 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1395 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1397 if (dataset != null)
1399 dataset.setCodonFrames(acfs);
1403 this.codonFrameList = acfs;
1408 * Returns the set of codon frame mappings. Any changes to the returned set
1409 * will affect the alignment. The mappings are held on (and read from) the
1410 * dataset alignment if there is one.
1412 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1415 public List<AlignedCodonFrame> getCodonFrames()
1417 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1418 // this behaviour is currently incorrect. method should return codon frames
1419 // for just the alignment,
1420 // selected from dataset
1421 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1425 * Removes the given mapping from the stored set. Note that the mappings are
1426 * held on the dataset alignment if there is one.
1429 public boolean removeCodonFrame(AlignedCodonFrame codons)
1431 List<AlignedCodonFrame> acfs = getCodonFrames();
1432 if (codons == null || acfs == null)
1436 return acfs.remove(codons);
1440 public void append(AlignmentI toappend)
1442 // TODO JAL-1270 needs test coverage
1443 // currently tested for use in jalview.gui.SequenceFetcher
1444 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1445 char oldc = toappend.getGapCharacter();
1446 boolean hashidden = toappend.getHiddenSequences() != null
1447 && toappend.getHiddenSequences().hiddenSequences != null;
1448 // get all sequences including any hidden ones
1449 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1450 .getFullAlignment().getSequences() : toappend.getSequences();
1453 // avoid self append deadlock by
1454 List<SequenceI> toappendsq = new ArrayList<SequenceI>();
1457 for (SequenceI addedsq : sqs)
1461 char[] oldseq = addedsq.getSequence();
1462 for (int c = 0; c < oldseq.length; c++)
1464 if (oldseq[c] == oldc)
1466 oldseq[c] = gapCharacter;
1470 toappendsq.add(addedsq);
1473 for (SequenceI addedsq : toappendsq)
1475 addSequence(addedsq);
1478 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1479 for (int a = 0; alan != null && a < alan.length; a++)
1481 addAnnotation(alan[a]);
1485 getCodonFrames().addAll(toappend.getCodonFrames());
1487 List<SequenceGroup> sg = toappend.getGroups();
1490 for (SequenceGroup _sg : sg)
1495 if (toappend.getHiddenSequences() != null)
1497 HiddenSequences hs = toappend.getHiddenSequences();
1498 if (hiddenSequences == null)
1500 hiddenSequences = new HiddenSequences(this);
1502 if (hs.hiddenSequences != null)
1504 for (int s = 0; s < hs.hiddenSequences.length; s++)
1506 // hide the newly appended sequence in the alignment
1507 if (hs.hiddenSequences[s] != null)
1509 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1514 if (toappend.getProperties() != null)
1516 // we really can't do very much here - just try to concatenate strings
1517 // where property collisions occur.
1518 Enumeration key = toappend.getProperties().keys();
1519 while (key.hasMoreElements())
1521 Object k = key.nextElement();
1522 Object ourval = this.getProperty(k);
1523 Object toapprop = toappend.getProperty(k);
1526 if (ourval.getClass().equals(toapprop.getClass())
1527 && !ourval.equals(toapprop))
1529 if (ourval instanceof String)
1532 this.setProperty(k, ((String) ourval) + "; "
1533 + ((String) toapprop));
1537 if (ourval instanceof Vector)
1540 Enumeration theirv = ((Vector) toapprop).elements();
1541 while (theirv.hasMoreElements())
1543 ((Vector) ourval).addElement(theirv);
1551 // just add new property directly
1552 setProperty(k, toapprop);
1560 public AlignmentAnnotation findOrCreateAnnotation(String name,
1561 String calcId, boolean autoCalc, SequenceI seqRef,
1562 SequenceGroup groupRef)
1564 if (annotations != null)
1566 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1568 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1569 && (calcId == null || annot.getCalcId().equals(calcId))
1570 && annot.sequenceRef == seqRef
1571 && annot.groupRef == groupRef)
1577 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1578 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1579 annot.hasText = false;
1580 annot.setCalcId(new String(calcId));
1581 annot.autoCalculated = autoCalc;
1584 annot.setSequenceRef(seqRef);
1586 annot.groupRef = groupRef;
1587 addAnnotation(annot);
1593 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1595 List<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1596 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1597 if (alignmentAnnotation != null)
1599 for (AlignmentAnnotation a : alignmentAnnotation)
1601 if (a.getCalcId() == calcId
1602 || (a.getCalcId() != null && calcId != null && a
1603 .getCalcId().equals(calcId)))
1613 * Returns an iterable collection of any annotations that match on given
1614 * sequence ref, calcId and label (ignoring null values).
1617 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1618 String calcId, String label)
1620 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1621 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1623 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1624 && ann.sequenceRef != null && ann.sequenceRef == seq
1625 && ann.label != null && ann.label.equals(label))
1634 public void moveSelectedSequencesByOne(SequenceGroup sg,
1635 Map<SequenceI, SequenceCollectionI> map, boolean up)
1637 synchronized (sequences)
1642 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1644 SequenceI seq = sequences.get(i);
1645 if (!sg.getSequences(map).contains(seq))
1650 SequenceI temp = sequences.get(i - 1);
1651 if (sg.getSequences(null).contains(temp))
1656 sequences.set(i, temp);
1657 sequences.set(i - 1, seq);
1662 for (int i = sequences.size() - 2; i > -1; i--)
1664 SequenceI seq = sequences.get(i);
1665 if (!sg.getSequences(map).contains(seq))
1670 SequenceI temp = sequences.get(i + 1);
1671 if (sg.getSequences(map).contains(temp))
1676 sequences.set(i, temp);
1677 sequences.set(i + 1, seq);
1685 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1687 alignmentAnnotation.validateRangeAndDisplay();
1688 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1690 hasRNAStructure = true;
1694 private SequenceI seqrep = null;
1698 * @return the representative sequence for this group
1701 public SequenceI getSeqrep()
1707 * set the representative sequence for this group. Note - this affects the
1708 * interpretation of the Hidereps attribute.
1711 * the seqrep to set (null means no sequence representative)
1714 public void setSeqrep(SequenceI seqrep)
1716 this.seqrep = seqrep;
1721 * @return true if group has a sequence representative
1724 public boolean hasSeqrep()
1726 return seqrep != null;
1730 public int getEndRes()
1732 return getWidth() - 1;
1736 public int getStartRes()
1742 * In the case of AlignmentI - returns the dataset for the alignment, if set
1745 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1748 public AnnotatedCollectionI getContext()
1754 * Align this alignment like the given (mapped) one.
1757 public int alignAs(AlignmentI al)
1760 * Currently retains unmapped gaps (in introns), regaps mapped regions
1763 return alignAs(al, false, true);
1767 * Align this alignment 'the same as' the given one. Mapped sequences only are
1768 * realigned. If both of the same type (nucleotide/protein) then align both
1769 * identically. If this is nucleotide and the other is protein, make 3 gaps
1770 * for each gap in the protein sequences. If this is protein and the other is
1771 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1772 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1773 * protein to match the relative ordering of codons in the nucleotide.
1775 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1776 * regions are preserved. Gaps that connect introns to exons are treated
1777 * conservatively, i.e. only preserved if both intron and exon gaps are
1778 * preserved. TODO: check caveats below where the implementation fails
1781 * - must have same dataset, and sequences in al must have equivalent
1782 * dataset sequence and start/end bounds under given mapping
1783 * @param preserveMappedGaps
1784 * if true, gaps within and between mapped codons are preserved
1785 * @param preserveUnmappedGaps
1786 * if true, gaps within and between unmapped codons are preserved
1789 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1790 boolean preserveUnmappedGaps)
1792 // TODO should this method signature be the one in the interface?
1793 // JBPComment - yes - neither flag is used, so should be deleted.
1794 boolean thisIsNucleotide = this.isNucleotide();
1795 boolean thatIsProtein = !al.isNucleotide();
1796 if (!thatIsProtein && !thisIsNucleotide)
1798 return AlignmentUtils.alignProteinAsDna(this, al);
1800 else if (thatIsProtein && thisIsNucleotide)
1802 return AlignmentUtils.alignCdsAsProtein(this, al);
1804 return AlignmentUtils.alignAs(this, al);
1808 * Returns the alignment in Fasta format. Behaviour of this method is not
1809 * guaranteed between versions.
1812 public String toString()
1814 return new FastaFile().print(getSequencesArray(), true);
1818 * Returns the set of distinct sequence names. No ordering is guaranteed.
1821 public Set<String> getSequenceNames()
1823 Set<String> names = new HashSet<String>();
1824 for (SequenceI seq : getSequences())
1826 names.add(seq.getName());
1832 public boolean hasValidSequence()
1834 boolean hasValidSeq = false;
1835 for (SequenceI seq : getSequences())
1837 if ((seq.getEnd() - seq.getStart()) > 0)
1847 * Update any mappings to 'virtual' sequences to compatible real ones, if
1848 * present in the added sequences. Returns a count of mappings updated.
1854 public int realiseMappings(List<SequenceI> seqs)
1857 for (SequenceI seq : seqs)
1859 for (AlignedCodonFrame mapping : getCodonFrames())
1861 count += mapping.realiseWith(seq);
1868 * Returns the first AlignedCodonFrame that has a mapping between the given
1876 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1878 for (AlignedCodonFrame acf : getCodonFrames())
1880 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1889 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1891 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1892 int startPos = alignmentStartEnd[0];
1893 int endPos = alignmentStartEnd[1];
1895 int[] lowestRange = new int[] { -1, -1 };
1896 int[] higestRange = new int[] { -1, -1 };
1898 for (int[] hiddenCol : hiddenCols)
1900 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1901 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1904 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1906 startPos = alignmentStartEnd[0];
1910 startPos = lowestRange[1] + 1;
1913 if (higestRange[0] == -1 && higestRange[1] == -1)
1915 endPos = alignmentStartEnd[1];
1919 endPos = higestRange[0] - 1;
1921 return new int[] { startPos, endPos };