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 HiddenColumns hiddenCols;
67 public Hashtable alignmentProperties;
69 private List<AlignedCodonFrame> codonFrameList;
71 private void initAlignment(SequenceI[] seqs)
73 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
74 hiddenSequences = new HiddenSequences(this);
75 hiddenCols = new HiddenColumns();
76 codonFrameList = new ArrayList<>();
78 nucleotide = Comparison.isNucleotide(seqs);
80 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
82 for (int i = 0; i < seqs.length; i++)
84 sequences.add(seqs[i]);
90 * Make a 'copy' alignment - sequences have new copies of features and
91 * annotations, but share the original dataset sequences.
93 public Alignment(AlignmentI al)
95 SequenceI[] seqs = al.getSequencesArray();
96 for (int i = 0; i < seqs.length; i++)
98 seqs[i] = new Sequence(seqs[i]);
104 * Share the same dataset sequence mappings (if any).
106 if (dataset == null && al.getDataset() == null)
108 this.setCodonFrames(al.getCodonFrames());
113 * Make an alignment from an array of Sequences.
117 public Alignment(SequenceI[] seqs)
123 * Make a new alignment from an array of SeqCigars
128 public Alignment(SeqCigar[] alseqs)
130 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
131 gapCharacter, new HiddenColumns(), null);
136 * Make a new alignment from an CigarArray JBPNote - can only do this when
137 * compactAlignment does not contain hidden regions. JBPNote - must also check
138 * that compactAlignment resolves to a set of SeqCigars - or construct them
141 * @param compactAlignment
144 public static AlignmentI createAlignment(CigarArray compactAlignment)
146 throw new Error(MessageManager
147 .getString("error.alignment_cigararray_not_implemented"));
148 // this(compactAlignment.refCigars);
152 public List<SequenceI> getSequences()
158 public List<SequenceI> getSequences(
159 Map<SequenceI, SequenceCollectionI> hiddenReps)
161 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
167 public SequenceI[] getSequencesArray()
169 if (sequences == null)
173 synchronized (sequences)
175 return sequences.toArray(new SequenceI[sequences.size()]);
180 * Returns a map of lists of sequences keyed by sequence name.
185 public Map<String, List<SequenceI>> getSequencesByName()
187 return AlignmentUtils.getSequencesByName(this);
191 public SequenceI getSequenceAt(int i)
193 synchronized (sequences)
195 if (i > -1 && i < sequences.size())
197 return sequences.get(i);
204 public SequenceI getSequenceAtAbsoluteIndex(int i)
206 SequenceI seq = null;
207 if (getHiddenSequences().getSize() > 0)
209 seq = getHiddenSequences().getHiddenSequence(i);
212 // didn't find the sequence in the hidden sequences, get it from the
214 int index = getHiddenSequences().findIndexWithoutHiddenSeqs(i);
215 seq = getSequenceAt(index);
220 seq = getSequenceAt(i);
226 * Adds a sequence to the alignment. Recalculates maxLength and size. Note
227 * this currently does not recalculate whether or not the alignment is
228 * nucleotide, so mixed alignments may have undefined behaviour.
233 public void addSequence(SequenceI snew)
238 // maintain dataset integrity
239 SequenceI dsseq = snew.getDatasetSequence();
242 // derive new sequence
243 SequenceI adding = snew.deriveSequence();
245 dsseq = snew.getDatasetSequence();
247 if (getDataset().findIndex(dsseq) == -1)
249 getDataset().addSequence(dsseq);
253 if (sequences == null)
255 initAlignment(new SequenceI[] { snew });
259 synchronized (sequences)
264 if (hiddenSequences != null)
266 hiddenSequences.adjustHeightSequenceAdded();
271 public SequenceI replaceSequenceAt(int i, SequenceI snew)
273 synchronized (sequences)
275 if (sequences.size() > i)
277 return sequences.set(i, snew);
283 hiddenSequences.adjustHeightSequenceAdded();
292 * @return DOCUMENT ME!
295 public List<SequenceGroup> getGroups()
301 public void finalize() throws Throwable
303 if (getDataset() != null)
305 getDataset().removeAlignmentRef();
313 * Defensively nulls out references in case this object is not garbage
316 void nullReferences()
322 hiddenSequences = null;
326 * decrement the alignmentRefs counter by one and null references if it goes
331 private void removeAlignmentRef() throws Throwable
333 if (--alignmentRefs == 0)
340 public void deleteSequence(SequenceI s)
342 synchronized (sequences)
344 deleteSequence(findIndex(s));
349 public void deleteSequence(int i)
351 synchronized (sequences)
353 if (i > -1 && i < getHeight())
356 hiddenSequences.adjustHeightSequenceDeleted(i);
362 public void deleteHiddenSequence(int i)
364 synchronized (sequences)
366 if (i > -1 && i < getHeight())
376 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
379 public SequenceGroup findGroup(SequenceI seq, int position)
381 synchronized (groups)
383 for (SequenceGroup sg : groups)
385 if (sg.getSequences(null).contains(seq))
387 if (position >= sg.getStartRes() && position <= sg.getEndRes())
401 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
404 public SequenceGroup[] findAllGroups(SequenceI s)
406 ArrayList<SequenceGroup> temp = new ArrayList<>();
408 synchronized (groups)
410 int gSize = groups.size();
411 for (int i = 0; i < gSize; i++)
413 SequenceGroup sg = groups.get(i);
414 if (sg == null || sg.getSequences() == null)
416 this.deleteGroup(sg);
421 if (sg.getSequences().contains(s))
427 SequenceGroup[] ret = new SequenceGroup[temp.size()];
428 return temp.toArray(ret);
433 public void addGroup(SequenceGroup sg)
435 synchronized (groups)
437 if (!groups.contains(sg))
439 if (hiddenSequences.getSize() > 0)
441 int i, iSize = sg.getSize();
442 for (i = 0; i < iSize; i++)
444 if (!sequences.contains(sg.getSequenceAt(i)))
446 sg.deleteSequence(sg.getSequenceAt(i), false);
452 if (sg.getSize() < 1)
457 sg.setContext(this, true);
464 * remove any annotation that references gp
467 * (if null, removes all group associated annotation)
469 private void removeAnnotationForGroup(SequenceGroup gp)
471 if (annotations == null || annotations.length == 0)
475 // remove annotation very quickly
476 AlignmentAnnotation[] t,
477 todelete = new AlignmentAnnotation[annotations.length],
478 tokeep = new AlignmentAnnotation[annotations.length];
482 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
484 if (annotations[i].groupRef != null)
486 todelete[p++] = annotations[i];
490 tokeep[k++] = annotations[i];
496 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
498 if (annotations[i].groupRef == gp)
500 todelete[p++] = annotations[i];
504 tokeep[k++] = annotations[i];
510 // clear out the group associated annotation.
511 for (i = 0; i < p; i++)
513 unhookAnnotation(todelete[i]);
516 t = new AlignmentAnnotation[k];
517 for (i = 0; i < k; i++)
526 public void deleteAllGroups()
528 synchronized (groups)
530 if (annotations != null)
532 removeAnnotationForGroup(null);
534 for (SequenceGroup sg : groups)
536 sg.setContext(null, false);
544 public void deleteGroup(SequenceGroup g)
546 synchronized (groups)
548 if (groups.contains(g))
550 removeAnnotationForGroup(g);
552 g.setContext(null, false);
559 public SequenceI findName(String name)
561 return findName(name, false);
567 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
570 public SequenceI findName(String token, boolean b)
572 return findName(null, token, b);
578 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
582 public SequenceI findName(SequenceI startAfter, String token, boolean b)
587 String sqname = null;
588 if (startAfter != null)
590 // try to find the sequence in the alignment
591 boolean matched = false;
592 while (i < sequences.size())
594 if (getSequenceAt(i++) == startAfter)
605 while (i < sequences.size())
607 sq = getSequenceAt(i);
608 sqname = sq.getName();
609 if (sqname.equals(token) // exact match
610 || (b && // allow imperfect matches - case varies
611 (sqname.equalsIgnoreCase(token))))
613 return getSequenceAt(i);
623 public SequenceI[] findSequenceMatch(String name)
625 Vector matches = new Vector();
628 while (i < sequences.size())
630 if (getSequenceAt(i).getName().equals(name))
632 matches.addElement(getSequenceAt(i));
637 SequenceI[] result = new SequenceI[matches.size()];
638 for (i = 0; i < result.length; i++)
640 result[i] = (SequenceI) matches.elementAt(i);
650 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
653 public int findIndex(SequenceI s)
657 while (i < sequences.size())
659 if (s == getSequenceAt(i))
674 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
677 public int findIndex(SearchResultsI results)
681 while (i < sequences.size())
683 if (results.involvesSequence(getSequenceAt(i)))
693 public int getHeight()
695 return sequences.size();
699 public int getAbsoluteHeight()
701 return sequences.size() + getHiddenSequences().getSize();
705 public int getWidth()
709 for (int i = 0; i < sequences.size(); i++)
711 if (getSequenceAt(i).getLength() > maxLength)
713 maxLength = getSequenceAt(i).getLength();
727 public void setGapCharacter(char gc)
730 synchronized (sequences)
732 for (SequenceI seq : sequences)
734 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
735 .replace('-', gc).replace(' ', gc));
743 * @return DOCUMENT ME!
746 public char getGapCharacter()
754 * @see jalview.datamodel.AlignmentI#isAligned()
757 public boolean isAligned()
759 return isAligned(false);
765 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
768 public boolean isAligned(boolean includeHidden)
770 int width = getWidth();
771 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
773 includeHidden = true; // no hidden sequences to check against.
775 for (int i = 0; i < sequences.size(); i++)
777 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
779 if (getSequenceAt(i).getLength() != width)
790 public boolean isHidden(int alignmentIndex)
792 return (getHiddenSequences().getHiddenSequence(alignmentIndex) != null);
796 * Delete all annotations, including auto-calculated if the flag is set true.
797 * Returns true if at least one annotation was deleted, else false.
799 * @param includingAutoCalculated
803 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
805 boolean result = false;
806 for (AlignmentAnnotation alan : getAlignmentAnnotation())
808 if (!alan.autoCalculated || includingAutoCalculated)
810 deleteAnnotation(alan);
820 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
821 * AlignmentAnnotation)
824 public boolean deleteAnnotation(AlignmentAnnotation aa)
826 return deleteAnnotation(aa, true);
830 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
834 if (annotations != null)
836 aSize = annotations.length;
844 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
846 boolean swap = false;
849 for (int i = 0; i < aSize; i++)
851 if (annotations[i] == aa)
856 if (tIndex < temp.length)
858 temp[tIndex++] = annotations[i];
867 unhookAnnotation(aa);
874 * remove any object references associated with this annotation
878 private void unhookAnnotation(AlignmentAnnotation aa)
880 if (aa.sequenceRef != null)
882 aa.sequenceRef.removeAlignmentAnnotation(aa);
884 if (aa.groupRef != null)
886 // probably need to do more here in the future (post 2.5.0)
894 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
895 * AlignmentAnnotation)
898 public void addAnnotation(AlignmentAnnotation aa)
900 addAnnotation(aa, -1);
906 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
907 * AlignmentAnnotation, int)
910 public void addAnnotation(AlignmentAnnotation aa, int pos)
912 if (aa.getRNAStruc() != null)
914 hasRNAStructure = true;
918 if (annotations != null)
920 aSize = annotations.length + 1;
923 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
925 if (pos == -1 || pos >= aSize)
927 temp[aSize - 1] = aa;
936 for (i = 0; i < (aSize - 1); i++, p++)
944 temp[p] = annotations[i];
953 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
955 if (aa == null || annotations == null || annotations.length - 1 < index)
960 int aSize = annotations.length;
961 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
965 for (int i = 0; i < aSize; i++)
974 temp[i] = annotations[i];
978 temp[i] = annotations[i - 1];
987 * returns all annotation on the alignment
989 public AlignmentAnnotation[] getAlignmentAnnotation()
995 public boolean isNucleotide()
1001 public boolean hasRNAStructure()
1003 // TODO can it happen that structure is removed from alignment?
1004 return hasRNAStructure;
1008 public void setDataset(AlignmentI data)
1010 if (dataset == null && data == null)
1012 createDatasetAlignment();
1014 else if (dataset == null && data != null)
1018 throw new IllegalArgumentException("Circular dataset reference");
1020 if (!(data instanceof Alignment))
1023 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1025 dataset = (Alignment) data;
1026 for (int i = 0; i < getHeight(); i++)
1028 SequenceI currentSeq = getSequenceAt(i);
1029 SequenceI dsq = currentSeq.getDatasetSequence();
1032 dsq = currentSeq.createDatasetSequence();
1033 dataset.addSequence(dsq);
1037 while (dsq.getDatasetSequence() != null)
1039 dsq = dsq.getDatasetSequence();
1041 if (dataset.findIndex(dsq) == -1)
1043 dataset.addSequence(dsq);
1048 dataset.addAlignmentRef();
1052 * add dataset sequences to seq for currentSeq and any sequences it references
1054 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1055 Set<SequenceI> seqs, boolean createDatasetSequence)
1057 SequenceI alignedSeq = currentSeq;
1058 if (currentSeq.getDatasetSequence() != null)
1060 currentSeq = currentSeq.getDatasetSequence();
1064 if (createDatasetSequence)
1066 currentSeq = currentSeq.createDatasetSequence();
1070 List<SequenceI> toProcess = new ArrayList<>();
1071 toProcess.add(currentSeq);
1072 while (toProcess.size() > 0)
1075 SequenceI curDs = toProcess.remove(0);
1077 if (!seqs.add(curDs))
1081 // iterate over database references, making sure we add forward referenced
1083 if (curDs.getDBRefs() != null)
1085 for (DBRefEntry dbr : curDs.getDBRefs())
1087 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1089 if (dbr.getMap().getTo() == alignedSeq)
1092 * update mapping to be to the newly created dataset sequence
1094 dbr.getMap().setTo(currentSeq);
1096 if (dbr.getMap().getTo().getDatasetSequence() != null)
1098 throw new Error("Implementation error: Map.getTo() for dbref "
1099 + dbr + " from " + curDs.getName()
1100 + " is not a dataset sequence.");
1102 // we recurse to add all forward references to dataset sequences via
1104 toProcess.add(dbr.getMap().getTo());
1112 * Creates a new dataset for this alignment. Can only be done once - if
1113 * dataset is not null this will not be performed.
1115 public void createDatasetAlignment()
1117 if (dataset != null)
1121 // try to avoid using SequenceI.equals at this stage, it will be expensive
1122 Set<SequenceI> seqs = new LinkedIdentityHashSet<>();
1124 for (int i = 0; i < getHeight(); i++)
1126 SequenceI currentSeq = getSequenceAt(i);
1127 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1130 // verify all mappings are in dataset
1131 for (AlignedCodonFrame cf : codonFrameList)
1133 for (SequenceToSequenceMapping ssm : cf.getMappings())
1135 if (!seqs.contains(ssm.getFromSeq()))
1137 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1139 if (!seqs.contains(ssm.getMapping().getTo()))
1141 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1145 // finally construct dataset
1146 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1147 // move mappings to the dataset alignment
1148 dataset.codonFrameList = this.codonFrameList;
1149 this.codonFrameList = null;
1153 * reference count for number of alignments referencing this one.
1155 int alignmentRefs = 0;
1158 * increase reference count to this alignment.
1160 private void addAlignmentRef()
1166 public Alignment getDataset()
1172 public boolean padGaps()
1174 boolean modified = false;
1176 // Remove excess gaps from the end of alignment
1180 for (int i = 0; i < sequences.size(); i++)
1182 current = getSequenceAt(i);
1183 for (int j = current.getLength(); j > maxLength; j--)
1186 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1197 for (int i = 0; i < sequences.size(); i++)
1199 current = getSequenceAt(i);
1200 cLength = current.getLength();
1202 if (cLength < maxLength)
1204 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1207 else if (current.getLength() > maxLength)
1209 current.deleteChars(maxLength, current.getLength());
1216 * Justify the sequences to the left or right by deleting and inserting gaps
1217 * before the initial residue or after the terminal residue
1220 * true if alignment padded to right, false to justify to left
1221 * @return true if alignment was changed
1224 public boolean justify(boolean right)
1226 boolean modified = false;
1228 // Remove excess gaps from the end of alignment
1230 int ends[] = new int[sequences.size() * 2];
1232 for (int i = 0; i < sequences.size(); i++)
1234 current = getSequenceAt(i);
1235 // This should really be a sequence method
1236 ends[i * 2] = current.findIndex(current.getStart());
1237 ends[i * 2 + 1] = current
1238 .findIndex(current.getStart() + current.getLength());
1239 boolean hitres = false;
1240 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1242 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1251 ends[i * 2 + 1] = j;
1252 if (j - ends[i * 2] > maxLength)
1254 maxLength = j - ends[i * 2];
1262 // now edit the flanking gaps to justify to either left or right
1263 int cLength, extent, diff;
1264 for (int i = 0; i < sequences.size(); i++)
1266 current = getSequenceAt(i);
1268 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1269 diff = maxLength - cLength; // number of gaps to indent
1270 extent = current.getLength();
1274 if (extent > ends[i * 2 + 1])
1276 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1279 if (ends[i * 2] > diff)
1281 current.deleteChars(0, ends[i * 2] - diff);
1286 if (ends[i * 2] < diff)
1288 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1296 if (ends[i * 2] > 0)
1298 current.deleteChars(0, ends[i * 2]);
1300 ends[i * 2 + 1] -= ends[i * 2];
1301 extent -= ends[i * 2];
1303 if (extent > maxLength)
1305 current.deleteChars(maxLength + 1, extent);
1310 if (extent < maxLength)
1312 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1322 public HiddenSequences getHiddenSequences()
1324 return hiddenSequences;
1328 public HiddenColumns getHiddenColumns()
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<>();
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)
1480 ? toappend.getHiddenSequences().getFullAlignment()
1482 : toappend.getSequences();
1485 // avoid self append deadlock by
1486 List<SequenceI> toappendsq = new ArrayList<>();
1489 for (SequenceI addedsq : sqs)
1493 char[] oldseq = addedsq.getSequence();
1494 for (int c = 0; c < oldseq.length; c++)
1496 if (oldseq[c] == oldc)
1498 oldseq[c] = gapCharacter;
1502 toappendsq.add(addedsq);
1505 for (SequenceI addedsq : toappendsq)
1507 addSequence(addedsq);
1510 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1511 for (int a = 0; alan != null && a < alan.length; a++)
1513 addAnnotation(alan[a]);
1517 getCodonFrames().addAll(toappend.getCodonFrames());
1519 List<SequenceGroup> sg = toappend.getGroups();
1522 for (SequenceGroup _sg : sg)
1527 if (toappend.getHiddenSequences() != null)
1529 HiddenSequences hs = toappend.getHiddenSequences();
1530 if (hiddenSequences == null)
1532 hiddenSequences = new HiddenSequences(this);
1534 if (hs.hiddenSequences != null)
1536 for (int s = 0; s < hs.hiddenSequences.length; s++)
1538 // hide the newly appended sequence in the alignment
1539 if (hs.hiddenSequences[s] != null)
1541 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1546 if (toappend.getProperties() != null)
1548 // we really can't do very much here - just try to concatenate strings
1549 // where property collisions occur.
1550 Enumeration key = toappend.getProperties().keys();
1551 while (key.hasMoreElements())
1553 Object k = key.nextElement();
1554 Object ourval = this.getProperty(k);
1555 Object toapprop = toappend.getProperty(k);
1558 if (ourval.getClass().equals(toapprop.getClass())
1559 && !ourval.equals(toapprop))
1561 if (ourval instanceof String)
1565 ((String) ourval) + "; " + ((String) toapprop));
1569 if (ourval instanceof Vector)
1572 Enumeration theirv = ((Vector) toapprop).elements();
1573 while (theirv.hasMoreElements())
1575 ((Vector) ourval).addElement(theirv);
1583 // just add new property directly
1584 setProperty(k, toapprop);
1592 public AlignmentAnnotation findOrCreateAnnotation(String name,
1593 String calcId, boolean autoCalc, SequenceI seqRef,
1594 SequenceGroup groupRef)
1596 if (annotations != null)
1598 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1600 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1601 && (calcId == null || annot.getCalcId().equals(calcId))
1602 && annot.sequenceRef == seqRef
1603 && annot.groupRef == groupRef)
1609 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1610 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1611 annot.hasText = false;
1612 annot.setCalcId(new String(calcId));
1613 annot.autoCalculated = autoCalc;
1616 annot.setSequenceRef(seqRef);
1618 annot.groupRef = groupRef;
1619 addAnnotation(annot);
1625 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1627 List<AlignmentAnnotation> aa = new ArrayList<>();
1628 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1629 if (alignmentAnnotation != null)
1631 for (AlignmentAnnotation a : alignmentAnnotation)
1633 if (a.getCalcId() == calcId || (a.getCalcId() != null
1634 && calcId != null && a.getCalcId().equals(calcId)))
1644 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1645 String calcId, String label)
1647 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1648 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1650 if ((calcId == null || (ann.getCalcId() != null
1651 && ann.getCalcId().equals(calcId)))
1652 && (seq == null || (ann.sequenceRef != null
1653 && ann.sequenceRef == seq))
1655 || (ann.label != null && ann.label.equals(label))))
1664 public void moveSelectedSequencesByOne(SequenceGroup sg,
1665 Map<SequenceI, SequenceCollectionI> map, boolean up)
1667 synchronized (sequences)
1672 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1674 SequenceI seq = sequences.get(i);
1675 if (!sg.getSequences(map).contains(seq))
1680 SequenceI temp = sequences.get(i - 1);
1681 if (sg.getSequences(null).contains(temp))
1686 sequences.set(i, temp);
1687 sequences.set(i - 1, seq);
1692 for (int i = sequences.size() - 2; i > -1; i--)
1694 SequenceI seq = sequences.get(i);
1695 if (!sg.getSequences(map).contains(seq))
1700 SequenceI temp = sequences.get(i + 1);
1701 if (sg.getSequences(map).contains(temp))
1706 sequences.set(i, temp);
1707 sequences.set(i + 1, seq);
1715 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1717 alignmentAnnotation.validateRangeAndDisplay();
1718 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1720 hasRNAStructure = true;
1724 private SequenceI seqrep = null;
1728 * @return the representative sequence for this group
1731 public SequenceI getSeqrep()
1737 * set the representative sequence for this group. Note - this affects the
1738 * interpretation of the Hidereps attribute.
1741 * the seqrep to set (null means no sequence representative)
1744 public void setSeqrep(SequenceI seqrep)
1746 this.seqrep = seqrep;
1751 * @return true if group has a sequence representative
1754 public boolean hasSeqrep()
1756 return seqrep != null;
1760 public int getEndRes()
1762 return getWidth() - 1;
1766 public int getStartRes()
1772 * In the case of AlignmentI - returns the dataset for the alignment, if set
1775 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1778 public AnnotatedCollectionI getContext()
1784 * Align this alignment like the given (mapped) one.
1787 public int alignAs(AlignmentI al)
1790 * Currently retains unmapped gaps (in introns), regaps mapped regions
1793 return alignAs(al, false, true);
1797 * Align this alignment 'the same as' the given one. Mapped sequences only are
1798 * realigned. If both of the same type (nucleotide/protein) then align both
1799 * identically. If this is nucleotide and the other is protein, make 3 gaps
1800 * for each gap in the protein sequences. If this is protein and the other is
1801 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1802 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1803 * protein to match the relative ordering of codons in the nucleotide.
1805 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1806 * regions are preserved. Gaps that connect introns to exons are treated
1807 * conservatively, i.e. only preserved if both intron and exon gaps are
1808 * preserved. TODO: check caveats below where the implementation fails
1811 * - must have same dataset, and sequences in al must have equivalent
1812 * dataset sequence and start/end bounds under given mapping
1813 * @param preserveMappedGaps
1814 * if true, gaps within and between mapped codons are preserved
1815 * @param preserveUnmappedGaps
1816 * if true, gaps within and between unmapped codons are preserved
1819 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1820 boolean preserveUnmappedGaps)
1822 // TODO should this method signature be the one in the interface?
1823 // JBPComment - yes - neither flag is used, so should be deleted.
1824 boolean thisIsNucleotide = this.isNucleotide();
1825 boolean thatIsProtein = !al.isNucleotide();
1826 if (!thatIsProtein && !thisIsNucleotide)
1828 return AlignmentUtils.alignProteinAsDna(this, al);
1830 else if (thatIsProtein && thisIsNucleotide)
1832 return AlignmentUtils.alignCdsAsProtein(this, al);
1834 return AlignmentUtils.alignAs(this, al);
1838 * Returns the alignment in Fasta format. Behaviour of this method is not
1839 * guaranteed between versions.
1842 public String toString()
1844 return new FastaFile().print(getSequencesArray(), true);
1848 * Returns the set of distinct sequence names. No ordering is guaranteed.
1851 public Set<String> getSequenceNames()
1853 Set<String> names = new HashSet<>();
1854 for (SequenceI seq : getSequences())
1856 names.add(seq.getName());
1862 public boolean hasValidSequence()
1864 boolean hasValidSeq = false;
1865 for (SequenceI seq : getSequences())
1867 if ((seq.getEnd() - seq.getStart()) > 0)
1877 * Update any mappings to 'virtual' sequences to compatible real ones, if
1878 * present in the added sequences. Returns a count of mappings updated.
1884 public int realiseMappings(List<SequenceI> seqs)
1887 for (SequenceI seq : seqs)
1889 for (AlignedCodonFrame mapping : getCodonFrames())
1891 count += mapping.realiseWith(seq);
1898 * Returns the first AlignedCodonFrame that has a mapping between the given
1906 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1908 for (AlignedCodonFrame acf : getCodonFrames())
1910 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1919 public void setHiddenColumns(HiddenColumns cols)