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 // don't need to synchronise here as sequences is a synchronizedList
194 if (i > -1 && i < sequences.size())
196 return sequences.get(i);
203 public SequenceI getSequenceAtAbsoluteIndex(int i)
205 SequenceI seq = null;
206 if (getHiddenSequences().getSize() > 0)
208 seq = getHiddenSequences().getHiddenSequence(i);
211 // didn't find the sequence in the hidden sequences, get it from the
213 int index = getHiddenSequences().findIndexWithoutHiddenSeqs(i);
214 seq = getSequenceAt(index);
219 seq = getSequenceAt(i);
225 * Adds a sequence to the alignment. Recalculates maxLength and size. Note
226 * this currently does not recalculate whether or not the alignment is
227 * nucleotide, so mixed alignments may have undefined behaviour.
232 public void addSequence(SequenceI snew)
237 // maintain dataset integrity
238 SequenceI dsseq = snew.getDatasetSequence();
241 // derive new sequence
242 SequenceI adding = snew.deriveSequence();
244 dsseq = snew.getDatasetSequence();
246 if (getDataset().findIndex(dsseq) == -1)
248 getDataset().addSequence(dsseq);
252 if (sequences == null)
254 initAlignment(new SequenceI[] { snew });
258 synchronized (sequences)
263 if (hiddenSequences != null)
265 hiddenSequences.adjustHeightSequenceAdded();
270 public SequenceI replaceSequenceAt(int i, SequenceI snew)
272 synchronized (sequences)
274 if (sequences.size() > i)
276 return sequences.set(i, snew);
282 hiddenSequences.adjustHeightSequenceAdded();
291 * @return DOCUMENT ME!
294 public List<SequenceGroup> getGroups()
300 public void finalize() throws Throwable
302 if (getDataset() != null)
304 getDataset().removeAlignmentRef();
312 * Defensively nulls out references in case this object is not garbage
315 void nullReferences()
321 hiddenSequences = null;
325 * decrement the alignmentRefs counter by one and null references if it goes
330 private void removeAlignmentRef() throws Throwable
332 if (--alignmentRefs == 0)
339 public void deleteSequence(SequenceI s)
341 synchronized (sequences)
343 deleteSequence(findIndex(s));
348 public void deleteSequence(int i)
350 synchronized (sequences)
352 if (i > -1 && i < getHeight())
355 hiddenSequences.adjustHeightSequenceDeleted(i);
361 public void deleteHiddenSequence(int i)
363 synchronized (sequences)
365 if (i > -1 && i < getHeight())
375 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
378 public SequenceGroup findGroup(SequenceI seq, int position)
380 synchronized (groups)
382 for (SequenceGroup sg : groups)
384 if (sg.getSequences(null).contains(seq))
386 if (position >= sg.getStartRes() && position <= sg.getEndRes())
400 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
403 public SequenceGroup[] findAllGroups(SequenceI s)
405 ArrayList<SequenceGroup> temp = new ArrayList<>();
407 synchronized (groups)
409 int gSize = groups.size();
410 for (int i = 0; i < gSize; i++)
412 SequenceGroup sg = groups.get(i);
413 if (sg == null || sg.getSequences() == null)
415 this.deleteGroup(sg);
420 if (sg.getSequences().contains(s))
426 SequenceGroup[] ret = new SequenceGroup[temp.size()];
427 return temp.toArray(ret);
432 public void addGroup(SequenceGroup sg)
434 synchronized (groups)
436 if (!groups.contains(sg))
438 if (hiddenSequences.getSize() > 0)
440 int i, iSize = sg.getSize();
441 for (i = 0; i < iSize; i++)
443 if (!sequences.contains(sg.getSequenceAt(i)))
445 sg.deleteSequence(sg.getSequenceAt(i), false);
451 if (sg.getSize() < 1)
456 sg.setContext(this, true);
463 * remove any annotation that references gp
466 * (if null, removes all group associated annotation)
468 private void removeAnnotationForGroup(SequenceGroup gp)
470 if (annotations == null || annotations.length == 0)
474 // remove annotation very quickly
475 AlignmentAnnotation[] t,
476 todelete = new AlignmentAnnotation[annotations.length],
477 tokeep = new AlignmentAnnotation[annotations.length];
481 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
483 if (annotations[i].groupRef != null)
485 todelete[p++] = annotations[i];
489 tokeep[k++] = annotations[i];
495 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
497 if (annotations[i].groupRef == gp)
499 todelete[p++] = annotations[i];
503 tokeep[k++] = annotations[i];
509 // clear out the group associated annotation.
510 for (i = 0; i < p; i++)
512 unhookAnnotation(todelete[i]);
515 t = new AlignmentAnnotation[k];
516 for (i = 0; i < k; i++)
525 public void deleteAllGroups()
527 synchronized (groups)
529 if (annotations != null)
531 removeAnnotationForGroup(null);
533 for (SequenceGroup sg : groups)
535 sg.setContext(null, false);
543 public void deleteGroup(SequenceGroup g)
545 synchronized (groups)
547 if (groups.contains(g))
549 removeAnnotationForGroup(g);
551 g.setContext(null, false);
558 public SequenceI findName(String name)
560 return findName(name, false);
566 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
569 public SequenceI findName(String token, boolean b)
571 return findName(null, token, b);
577 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
581 public SequenceI findName(SequenceI startAfter, String token, boolean b)
586 String sqname = null;
587 if (startAfter != null)
589 // try to find the sequence in the alignment
590 boolean matched = false;
591 while (i < sequences.size())
593 if (getSequenceAt(i++) == startAfter)
604 while (i < sequences.size())
606 sq = getSequenceAt(i);
607 sqname = sq.getName();
608 if (sqname.equals(token) // exact match
609 || (b && // allow imperfect matches - case varies
610 (sqname.equalsIgnoreCase(token))))
612 return getSequenceAt(i);
622 public SequenceI[] findSequenceMatch(String name)
624 Vector matches = new Vector();
627 while (i < sequences.size())
629 if (getSequenceAt(i).getName().equals(name))
631 matches.addElement(getSequenceAt(i));
636 SequenceI[] result = new SequenceI[matches.size()];
637 for (i = 0; i < result.length; i++)
639 result[i] = (SequenceI) matches.elementAt(i);
649 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
652 public int findIndex(SequenceI s)
656 while (i < sequences.size())
658 if (s == getSequenceAt(i))
673 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
676 public int findIndex(SearchResultsI results)
680 while (i < sequences.size())
682 if (results.involvesSequence(getSequenceAt(i)))
692 public int getHeight()
694 return sequences.size();
698 public int getAbsoluteHeight()
700 return sequences.size() + getHiddenSequences().getSize();
704 public int getWidth()
708 for (int i = 0; i < sequences.size(); i++)
710 if (getSequenceAt(i).getLength() > maxLength)
712 maxLength = getSequenceAt(i).getLength();
726 public void setGapCharacter(char gc)
729 synchronized (sequences)
731 for (SequenceI seq : sequences)
733 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
734 .replace('-', gc).replace(' ', gc));
742 * @return DOCUMENT ME!
745 public char getGapCharacter()
753 * @see jalview.datamodel.AlignmentI#isAligned()
756 public boolean isAligned()
758 return isAligned(false);
764 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
767 public boolean isAligned(boolean includeHidden)
769 int width = getWidth();
770 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
772 includeHidden = true; // no hidden sequences to check against.
774 for (int i = 0; i < sequences.size(); i++)
776 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
778 if (getSequenceAt(i).getLength() != width)
789 public boolean isHidden(int alignmentIndex)
791 return (getHiddenSequences().getHiddenSequence(alignmentIndex) != null);
795 * Delete all annotations, including auto-calculated if the flag is set true.
796 * Returns true if at least one annotation was deleted, else false.
798 * @param includingAutoCalculated
802 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
804 boolean result = false;
805 for (AlignmentAnnotation alan : getAlignmentAnnotation())
807 if (!alan.autoCalculated || includingAutoCalculated)
809 deleteAnnotation(alan);
819 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
820 * AlignmentAnnotation)
823 public boolean deleteAnnotation(AlignmentAnnotation aa)
825 return deleteAnnotation(aa, true);
829 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
833 if (annotations != null)
835 aSize = annotations.length;
843 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
845 boolean swap = false;
848 for (int i = 0; i < aSize; i++)
850 if (annotations[i] == aa)
855 if (tIndex < temp.length)
857 temp[tIndex++] = annotations[i];
866 unhookAnnotation(aa);
873 * remove any object references associated with this annotation
877 private void unhookAnnotation(AlignmentAnnotation aa)
879 if (aa.sequenceRef != null)
881 aa.sequenceRef.removeAlignmentAnnotation(aa);
883 if (aa.groupRef != null)
885 // probably need to do more here in the future (post 2.5.0)
893 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
894 * AlignmentAnnotation)
897 public void addAnnotation(AlignmentAnnotation aa)
899 addAnnotation(aa, -1);
905 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
906 * AlignmentAnnotation, int)
909 public void addAnnotation(AlignmentAnnotation aa, int pos)
911 if (aa.getRNAStruc() != null)
913 hasRNAStructure = true;
917 if (annotations != null)
919 aSize = annotations.length + 1;
922 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
924 if (pos == -1 || pos >= aSize)
926 temp[aSize - 1] = aa;
935 for (i = 0; i < (aSize - 1); i++, p++)
943 temp[p] = annotations[i];
952 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
954 if (aa == null || annotations == null || annotations.length - 1 < index)
959 int aSize = annotations.length;
960 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
964 for (int i = 0; i < aSize; i++)
973 temp[i] = annotations[i];
977 temp[i] = annotations[i - 1];
986 * returns all annotation on the alignment
988 public AlignmentAnnotation[] getAlignmentAnnotation()
994 public boolean isNucleotide()
1000 public boolean hasRNAStructure()
1002 // TODO can it happen that structure is removed from alignment?
1003 return hasRNAStructure;
1007 public void setDataset(AlignmentI data)
1009 if (dataset == null && data == null)
1011 createDatasetAlignment();
1013 else if (dataset == null && data != null)
1017 throw new IllegalArgumentException("Circular dataset reference");
1019 if (!(data instanceof Alignment))
1022 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1024 dataset = (Alignment) data;
1025 for (int i = 0; i < getHeight(); i++)
1027 SequenceI currentSeq = getSequenceAt(i);
1028 SequenceI dsq = currentSeq.getDatasetSequence();
1031 dsq = currentSeq.createDatasetSequence();
1032 dataset.addSequence(dsq);
1036 while (dsq.getDatasetSequence() != null)
1038 dsq = dsq.getDatasetSequence();
1040 if (dataset.findIndex(dsq) == -1)
1042 dataset.addSequence(dsq);
1047 dataset.addAlignmentRef();
1051 * add dataset sequences to seq for currentSeq and any sequences it references
1053 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1054 Set<SequenceI> seqs, boolean createDatasetSequence)
1056 SequenceI alignedSeq = currentSeq;
1057 if (currentSeq.getDatasetSequence() != null)
1059 currentSeq = currentSeq.getDatasetSequence();
1063 if (createDatasetSequence)
1065 currentSeq = currentSeq.createDatasetSequence();
1069 List<SequenceI> toProcess = new ArrayList<>();
1070 toProcess.add(currentSeq);
1071 while (toProcess.size() > 0)
1074 SequenceI curDs = toProcess.remove(0);
1076 if (!seqs.add(curDs))
1080 // iterate over database references, making sure we add forward referenced
1082 if (curDs.getDBRefs() != null)
1084 for (DBRefEntry dbr : curDs.getDBRefs())
1086 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1088 if (dbr.getMap().getTo() == alignedSeq)
1091 * update mapping to be to the newly created dataset sequence
1093 dbr.getMap().setTo(currentSeq);
1095 if (dbr.getMap().getTo().getDatasetSequence() != null)
1097 throw new Error("Implementation error: Map.getTo() for dbref "
1098 + dbr + " from " + curDs.getName()
1099 + " is not a dataset sequence.");
1101 // we recurse to add all forward references to dataset sequences via
1103 toProcess.add(dbr.getMap().getTo());
1111 * Creates a new dataset for this alignment. Can only be done once - if
1112 * dataset is not null this will not be performed.
1114 public void createDatasetAlignment()
1116 if (dataset != null)
1120 // try to avoid using SequenceI.equals at this stage, it will be expensive
1121 Set<SequenceI> seqs = new LinkedIdentityHashSet<>();
1123 for (int i = 0; i < getHeight(); i++)
1125 SequenceI currentSeq = getSequenceAt(i);
1126 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1129 // verify all mappings are in dataset
1130 for (AlignedCodonFrame cf : codonFrameList)
1132 for (SequenceToSequenceMapping ssm : cf.getMappings())
1134 if (!seqs.contains(ssm.getFromSeq()))
1136 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1138 if (!seqs.contains(ssm.getMapping().getTo()))
1140 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1144 // finally construct dataset
1145 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1146 // move mappings to the dataset alignment
1147 dataset.codonFrameList = this.codonFrameList;
1148 this.codonFrameList = null;
1152 * reference count for number of alignments referencing this one.
1154 int alignmentRefs = 0;
1157 * increase reference count to this alignment.
1159 private void addAlignmentRef()
1165 public Alignment getDataset()
1171 public boolean padGaps()
1173 boolean modified = false;
1175 // Remove excess gaps from the end of alignment
1179 for (int i = 0; i < sequences.size(); i++)
1181 current = getSequenceAt(i);
1182 for (int j = current.getLength(); j > maxLength; j--)
1185 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1196 for (int i = 0; i < sequences.size(); i++)
1198 current = getSequenceAt(i);
1199 cLength = current.getLength();
1201 if (cLength < maxLength)
1203 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1206 else if (current.getLength() > maxLength)
1208 current.deleteChars(maxLength, current.getLength());
1215 * Justify the sequences to the left or right by deleting and inserting gaps
1216 * before the initial residue or after the terminal residue
1219 * true if alignment padded to right, false to justify to left
1220 * @return true if alignment was changed
1223 public boolean justify(boolean right)
1225 boolean modified = false;
1227 // Remove excess gaps from the end of alignment
1229 int ends[] = new int[sequences.size() * 2];
1231 for (int i = 0; i < sequences.size(); i++)
1233 current = getSequenceAt(i);
1234 // This should really be a sequence method
1235 ends[i * 2] = current.findIndex(current.getStart());
1236 ends[i * 2 + 1] = current
1237 .findIndex(current.getStart() + current.getLength());
1238 boolean hitres = false;
1239 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1241 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1250 ends[i * 2 + 1] = j;
1251 if (j - ends[i * 2] > maxLength)
1253 maxLength = j - ends[i * 2];
1261 // now edit the flanking gaps to justify to either left or right
1262 int cLength, extent, diff;
1263 for (int i = 0; i < sequences.size(); i++)
1265 current = getSequenceAt(i);
1267 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1268 diff = maxLength - cLength; // number of gaps to indent
1269 extent = current.getLength();
1273 if (extent > ends[i * 2 + 1])
1275 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1278 if (ends[i * 2] > diff)
1280 current.deleteChars(0, ends[i * 2] - diff);
1285 if (ends[i * 2] < diff)
1287 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1295 if (ends[i * 2] > 0)
1297 current.deleteChars(0, ends[i * 2]);
1299 ends[i * 2 + 1] -= ends[i * 2];
1300 extent -= ends[i * 2];
1302 if (extent > maxLength)
1304 current.deleteChars(maxLength + 1, extent);
1309 if (extent < maxLength)
1311 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1321 public HiddenSequences getHiddenSequences()
1323 return hiddenSequences;
1327 public HiddenColumns getHiddenColumns()
1333 public CigarArray getCompactAlignment()
1335 synchronized (sequences)
1337 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1339 for (SequenceI seq : sequences)
1341 alseqs[i++] = new SeqCigar(seq);
1343 CigarArray cal = new CigarArray(alseqs);
1344 cal.addOperation(CigarArray.M, getWidth());
1350 public void setProperty(Object key, Object value)
1352 if (alignmentProperties == null)
1354 alignmentProperties = new Hashtable();
1357 alignmentProperties.put(key, value);
1361 public Object getProperty(Object key)
1363 if (alignmentProperties != null)
1365 return alignmentProperties.get(key);
1374 public Hashtable getProperties()
1376 return alignmentProperties;
1380 * Adds the given mapping to the stored set. Note this may be held on the
1381 * dataset alignment.
1384 public void addCodonFrame(AlignedCodonFrame codons)
1386 List<AlignedCodonFrame> acfs = getCodonFrames();
1387 if (codons != null && acfs != null && !acfs.contains(codons))
1397 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1400 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1406 List<AlignedCodonFrame> cframes = new ArrayList<>();
1407 for (AlignedCodonFrame acf : getCodonFrames())
1409 if (acf.involvesSequence(seq))
1418 * Sets the codon frame mappings (replacing any existing mappings). Note the
1419 * mappings are set on the dataset alignment instead if there is one.
1421 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1424 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1426 if (dataset != null)
1428 dataset.setCodonFrames(acfs);
1432 this.codonFrameList = acfs;
1437 * Returns the set of codon frame mappings. Any changes to the returned set
1438 * will affect the alignment. The mappings are held on (and read from) the
1439 * dataset alignment if there is one.
1441 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1444 public List<AlignedCodonFrame> getCodonFrames()
1446 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1447 // this behaviour is currently incorrect. method should return codon frames
1448 // for just the alignment,
1449 // selected from dataset
1450 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1454 * Removes the given mapping from the stored set. Note that the mappings are
1455 * held on the dataset alignment if there is one.
1458 public boolean removeCodonFrame(AlignedCodonFrame codons)
1460 List<AlignedCodonFrame> acfs = getCodonFrames();
1461 if (codons == null || acfs == null)
1465 return acfs.remove(codons);
1469 public void append(AlignmentI toappend)
1471 // TODO JAL-1270 needs test coverage
1472 // currently tested for use in jalview.gui.SequenceFetcher
1473 char oldc = toappend.getGapCharacter();
1474 boolean samegap = oldc == getGapCharacter();
1475 boolean hashidden = toappend.getHiddenSequences() != null
1476 && toappend.getHiddenSequences().hiddenSequences != null;
1477 // get all sequences including any hidden ones
1478 List<SequenceI> sqs = (hashidden)
1479 ? toappend.getHiddenSequences().getFullAlignment()
1481 : toappend.getSequences();
1484 // avoid self append deadlock by
1485 List<SequenceI> toappendsq = new ArrayList<>();
1488 for (SequenceI addedsq : sqs)
1492 addedsq.replace(oldc, gapCharacter);
1494 toappendsq.add(addedsq);
1497 for (SequenceI addedsq : toappendsq)
1499 addSequence(addedsq);
1502 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1503 for (int a = 0; alan != null && a < alan.length; a++)
1505 addAnnotation(alan[a]);
1509 getCodonFrames().addAll(toappend.getCodonFrames());
1511 List<SequenceGroup> sg = toappend.getGroups();
1514 for (SequenceGroup _sg : sg)
1519 if (toappend.getHiddenSequences() != null)
1521 HiddenSequences hs = toappend.getHiddenSequences();
1522 if (hiddenSequences == null)
1524 hiddenSequences = new HiddenSequences(this);
1526 if (hs.hiddenSequences != null)
1528 for (int s = 0; s < hs.hiddenSequences.length; s++)
1530 // hide the newly appended sequence in the alignment
1531 if (hs.hiddenSequences[s] != null)
1533 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1538 if (toappend.getProperties() != null)
1540 // we really can't do very much here - just try to concatenate strings
1541 // where property collisions occur.
1542 Enumeration key = toappend.getProperties().keys();
1543 while (key.hasMoreElements())
1545 Object k = key.nextElement();
1546 Object ourval = this.getProperty(k);
1547 Object toapprop = toappend.getProperty(k);
1550 if (ourval.getClass().equals(toapprop.getClass())
1551 && !ourval.equals(toapprop))
1553 if (ourval instanceof String)
1557 ((String) ourval) + "; " + ((String) toapprop));
1561 if (ourval instanceof Vector)
1564 Enumeration theirv = ((Vector) toapprop).elements();
1565 while (theirv.hasMoreElements())
1567 ((Vector) ourval).addElement(theirv);
1575 // just add new property directly
1576 setProperty(k, toapprop);
1584 public AlignmentAnnotation findOrCreateAnnotation(String name,
1585 String calcId, boolean autoCalc, SequenceI seqRef,
1586 SequenceGroup groupRef)
1588 if (annotations != null)
1590 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1592 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1593 && (calcId == null || annot.getCalcId().equals(calcId))
1594 && annot.sequenceRef == seqRef
1595 && annot.groupRef == groupRef)
1601 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1602 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1603 annot.hasText = false;
1604 annot.setCalcId(new String(calcId));
1605 annot.autoCalculated = autoCalc;
1608 annot.setSequenceRef(seqRef);
1610 annot.groupRef = groupRef;
1611 addAnnotation(annot);
1617 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1619 List<AlignmentAnnotation> aa = new ArrayList<>();
1620 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1621 if (alignmentAnnotation != null)
1623 for (AlignmentAnnotation a : alignmentAnnotation)
1625 if (a.getCalcId() == calcId || (a.getCalcId() != null
1626 && calcId != null && a.getCalcId().equals(calcId)))
1636 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1637 String calcId, String label)
1639 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1640 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1642 if ((calcId == null || (ann.getCalcId() != null
1643 && ann.getCalcId().equals(calcId)))
1644 && (seq == null || (ann.sequenceRef != null
1645 && ann.sequenceRef == seq))
1647 || (ann.label != null && ann.label.equals(label))))
1656 public void moveSelectedSequencesByOne(SequenceGroup sg,
1657 Map<SequenceI, SequenceCollectionI> map, boolean up)
1659 synchronized (sequences)
1664 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1666 SequenceI seq = sequences.get(i);
1667 if (!sg.getSequences(map).contains(seq))
1672 SequenceI temp = sequences.get(i - 1);
1673 if (sg.getSequences(null).contains(temp))
1678 sequences.set(i, temp);
1679 sequences.set(i - 1, seq);
1684 for (int i = sequences.size() - 2; i > -1; i--)
1686 SequenceI seq = sequences.get(i);
1687 if (!sg.getSequences(map).contains(seq))
1692 SequenceI temp = sequences.get(i + 1);
1693 if (sg.getSequences(map).contains(temp))
1698 sequences.set(i, temp);
1699 sequences.set(i + 1, seq);
1707 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1709 alignmentAnnotation.validateRangeAndDisplay();
1710 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1712 hasRNAStructure = true;
1716 private SequenceI seqrep = null;
1720 * @return the representative sequence for this group
1723 public SequenceI getSeqrep()
1729 * set the representative sequence for this group. Note - this affects the
1730 * interpretation of the Hidereps attribute.
1733 * the seqrep to set (null means no sequence representative)
1736 public void setSeqrep(SequenceI seqrep)
1738 this.seqrep = seqrep;
1743 * @return true if group has a sequence representative
1746 public boolean hasSeqrep()
1748 return seqrep != null;
1752 public int getEndRes()
1754 return getWidth() - 1;
1758 public int getStartRes()
1764 * In the case of AlignmentI - returns the dataset for the alignment, if set
1767 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1770 public AnnotatedCollectionI getContext()
1776 * Align this alignment like the given (mapped) one.
1779 public int alignAs(AlignmentI al)
1782 * Currently retains unmapped gaps (in introns), regaps mapped regions
1785 return alignAs(al, false, true);
1789 * Align this alignment 'the same as' the given one. Mapped sequences only are
1790 * realigned. If both of the same type (nucleotide/protein) then align both
1791 * identically. If this is nucleotide and the other is protein, make 3 gaps
1792 * for each gap in the protein sequences. If this is protein and the other is
1793 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1794 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1795 * protein to match the relative ordering of codons in the nucleotide.
1797 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1798 * regions are preserved. Gaps that connect introns to exons are treated
1799 * conservatively, i.e. only preserved if both intron and exon gaps are
1800 * preserved. TODO: check caveats below where the implementation fails
1803 * - must have same dataset, and sequences in al must have equivalent
1804 * dataset sequence and start/end bounds under given mapping
1805 * @param preserveMappedGaps
1806 * if true, gaps within and between mapped codons are preserved
1807 * @param preserveUnmappedGaps
1808 * if true, gaps within and between unmapped codons are preserved
1811 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1812 boolean preserveUnmappedGaps)
1814 // TODO should this method signature be the one in the interface?
1815 // JBPComment - yes - neither flag is used, so should be deleted.
1816 boolean thisIsNucleotide = this.isNucleotide();
1817 boolean thatIsProtein = !al.isNucleotide();
1818 if (!thatIsProtein && !thisIsNucleotide)
1820 return AlignmentUtils.alignProteinAsDna(this, al);
1822 else if (thatIsProtein && thisIsNucleotide)
1824 return AlignmentUtils.alignCdsAsProtein(this, al);
1826 return AlignmentUtils.alignAs(this, al);
1830 * Returns the alignment in Fasta format. Behaviour of this method is not
1831 * guaranteed between versions.
1834 public String toString()
1836 return new FastaFile().print(getSequencesArray(), true);
1840 * Returns the set of distinct sequence names. No ordering is guaranteed.
1843 public Set<String> getSequenceNames()
1845 Set<String> names = new HashSet<>();
1846 for (SequenceI seq : getSequences())
1848 names.add(seq.getName());
1854 public boolean hasValidSequence()
1856 boolean hasValidSeq = false;
1857 for (SequenceI seq : getSequences())
1859 if ((seq.getEnd() - seq.getStart()) > 0)
1869 * Update any mappings to 'virtual' sequences to compatible real ones, if
1870 * present in the added sequences. Returns a count of mappings updated.
1876 public int realiseMappings(List<SequenceI> seqs)
1879 for (SequenceI seq : seqs)
1881 for (AlignedCodonFrame mapping : getCodonFrames())
1883 count += mapping.realiseWith(seq);
1890 * Returns the first AlignedCodonFrame that has a mapping between the given
1898 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1900 for (AlignedCodonFrame acf : getCodonFrames())
1902 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1911 public void setHiddenColumns(HiddenColumns cols)