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.Arrays;
32 import java.util.Collections;
33 import java.util.Enumeration;
34 import java.util.HashSet;
35 import java.util.Hashtable;
36 import java.util.List;
39 import java.util.Vector;
42 * Data structure to hold and manipulate a multiple sequence alignment
48 public class Alignment implements AlignmentI
50 private Alignment dataset;
52 protected List<SequenceI> sequences;
54 protected List<SequenceGroup> groups;
56 protected char gapCharacter = '-';
58 private boolean nucleotide = true;
60 public boolean hasRNAStructure = false;
62 public AlignmentAnnotation[] annotations;
64 HiddenSequences hiddenSequences;
66 HiddenColumns hiddenCols;
68 public Hashtable alignmentProperties;
70 private List<AlignedCodonFrame> codonFrameList;
72 private void initAlignment(SequenceI[] seqs)
74 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
75 hiddenSequences = new HiddenSequences(this);
76 hiddenCols = new HiddenColumns();
77 codonFrameList = new ArrayList<>();
79 nucleotide = Comparison.isNucleotide(seqs);
81 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
83 for (int i = 0; i < seqs.length; i++)
85 sequences.add(seqs[i]);
91 * Make a 'copy' alignment - sequences have new copies of features and
92 * annotations, but share the original dataset sequences.
94 public Alignment(AlignmentI al)
96 SequenceI[] seqs = al.getSequencesArray();
97 for (int i = 0; i < seqs.length; i++)
99 seqs[i] = new Sequence(seqs[i]);
105 * Share the same dataset sequence mappings (if any).
107 if (dataset == null && al.getDataset() == null)
109 this.setCodonFrames(al.getCodonFrames());
114 * Make an alignment from an array of Sequences.
118 public Alignment(SequenceI[] seqs)
124 * Make a new alignment from an array of SeqCigars
129 public Alignment(SeqCigar[] alseqs)
131 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
132 gapCharacter, new HiddenColumns(), null);
137 * Make a new alignment from an CigarArray JBPNote - can only do this when
138 * compactAlignment does not contain hidden regions. JBPNote - must also check
139 * that compactAlignment resolves to a set of SeqCigars - or construct them
142 * @param compactAlignment
145 public static AlignmentI createAlignment(CigarArray compactAlignment)
147 throw new Error(MessageManager
148 .getString("error.alignment_cigararray_not_implemented"));
149 // this(compactAlignment.refCigars);
153 public List<SequenceI> getSequences()
159 public List<SequenceI> getSequences(
160 Map<SequenceI, SequenceCollectionI> hiddenReps)
162 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
168 public SequenceI[] getSequencesArray()
170 if (sequences == null)
174 synchronized (sequences)
176 return sequences.toArray(new SequenceI[sequences.size()]);
181 * Returns a map of lists of sequences keyed by sequence name.
186 public Map<String, List<SequenceI>> getSequencesByName()
188 return AlignmentUtils.getSequencesByName(this);
192 public SequenceI getSequenceAt(int i)
194 synchronized (sequences)
196 if (i > -1 && i < sequences.size())
198 return sequences.get(i);
205 public SequenceI getSequenceAtAbsoluteIndex(int i)
207 SequenceI seq = null;
208 if (getHiddenSequences().getSize() > 0)
210 seq = getHiddenSequences().getHiddenSequence(i);
213 // didn't find the sequence in the hidden sequences, get it from the
215 int index = getHiddenSequences().findIndexWithoutHiddenSeqs(i);
216 seq = getSequenceAt(index);
221 seq = getSequenceAt(i);
227 * Adds a sequence to the alignment. Recalculates maxLength and size. Note
228 * this currently does not recalculate whether or not the alignment is
229 * nucleotide, so mixed alignments may have undefined behaviour.
234 public void addSequence(SequenceI snew)
239 // maintain dataset integrity
240 SequenceI dsseq = snew.getDatasetSequence();
243 // derive new sequence
244 SequenceI adding = snew.deriveSequence();
246 dsseq = snew.getDatasetSequence();
248 if (getDataset().findIndex(dsseq) == -1)
250 getDataset().addSequence(dsseq);
254 if (sequences == null)
256 initAlignment(new SequenceI[] { snew });
260 synchronized (sequences)
265 if (hiddenSequences != null)
267 hiddenSequences.adjustHeightSequenceAdded();
272 public SequenceI replaceSequenceAt(int i, SequenceI snew)
274 synchronized (sequences)
276 if (sequences.size() > i)
278 return sequences.set(i, snew);
284 hiddenSequences.adjustHeightSequenceAdded();
293 * @return DOCUMENT ME!
296 public List<SequenceGroup> getGroups()
302 public void finalize() throws Throwable
304 if (getDataset() != null)
306 getDataset().removeAlignmentRef();
314 * Defensively nulls out references in case this object is not garbage
317 void nullReferences()
323 hiddenSequences = null;
327 * decrement the alignmentRefs counter by one and null references if it goes
332 private void removeAlignmentRef() throws Throwable
334 if (--alignmentRefs == 0)
341 public void deleteSequence(SequenceI s)
343 synchronized (sequences)
345 deleteSequence(findIndex(s));
350 public void deleteSequence(int i)
352 synchronized (sequences)
354 if (i > -1 && i < getHeight())
357 hiddenSequences.adjustHeightSequenceDeleted(i);
363 public void deleteHiddenSequence(int i)
365 synchronized (sequences)
367 if (i > -1 && i < getHeight())
377 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
380 public SequenceGroup findGroup(SequenceI seq, int position)
382 synchronized (groups)
384 for (SequenceGroup sg : groups)
386 if (sg.getSequences(null).contains(seq))
388 if (position >= sg.getStartRes() && position <= sg.getEndRes())
402 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
405 public SequenceGroup[] findAllGroups(SequenceI s)
407 ArrayList<SequenceGroup> temp = new ArrayList<>();
409 synchronized (groups)
411 int gSize = groups.size();
412 for (int i = 0; i < gSize; i++)
414 SequenceGroup sg = groups.get(i);
415 if (sg == null || sg.getSequences() == null)
417 this.deleteGroup(sg);
422 if (sg.getSequences().contains(s))
428 SequenceGroup[] ret = new SequenceGroup[temp.size()];
429 return temp.toArray(ret);
434 public void addGroup(SequenceGroup sg)
436 synchronized (groups)
438 if (!groups.contains(sg))
440 if (hiddenSequences.getSize() > 0)
442 int i, iSize = sg.getSize();
443 for (i = 0; i < iSize; i++)
445 if (!sequences.contains(sg.getSequenceAt(i)))
447 sg.deleteSequence(sg.getSequenceAt(i), false);
453 if (sg.getSize() < 1)
458 sg.setContext(this, true);
465 * remove any annotation that references gp
468 * (if null, removes all group associated annotation)
470 private void removeAnnotationForGroup(SequenceGroup gp)
472 if (annotations == null || annotations.length == 0)
476 // remove annotation very quickly
477 AlignmentAnnotation[] t,
478 todelete = new AlignmentAnnotation[annotations.length],
479 tokeep = new AlignmentAnnotation[annotations.length];
483 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
485 if (annotations[i].groupRef != null)
487 todelete[p++] = annotations[i];
491 tokeep[k++] = annotations[i];
497 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
499 if (annotations[i].groupRef == gp)
501 todelete[p++] = annotations[i];
505 tokeep[k++] = annotations[i];
511 // clear out the group associated annotation.
512 for (i = 0; i < p; i++)
514 unhookAnnotation(todelete[i]);
517 t = new AlignmentAnnotation[k];
518 for (i = 0; i < k; i++)
527 public void deleteAllGroups()
529 synchronized (groups)
531 if (annotations != null)
533 removeAnnotationForGroup(null);
535 for (SequenceGroup sg : groups)
537 sg.setContext(null, false);
545 public void deleteGroup(SequenceGroup g)
547 synchronized (groups)
549 if (groups.contains(g))
551 removeAnnotationForGroup(g);
553 g.setContext(null, false);
560 public SequenceI findName(String name)
562 return findName(name, false);
568 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
571 public SequenceI findName(String token, boolean b)
573 return findName(null, token, b);
579 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
583 public SequenceI findName(SequenceI startAfter, String token, boolean b)
588 String sqname = null;
589 if (startAfter != null)
591 // try to find the sequence in the alignment
592 boolean matched = false;
593 while (i < sequences.size())
595 if (getSequenceAt(i++) == startAfter)
606 while (i < sequences.size())
608 sq = getSequenceAt(i);
609 sqname = sq.getName();
610 if (sqname.equals(token) // exact match
611 || (b && // allow imperfect matches - case varies
612 (sqname.equalsIgnoreCase(token))))
614 return getSequenceAt(i);
624 public SequenceI[] findSequenceMatch(String name)
626 Vector matches = new Vector();
629 while (i < sequences.size())
631 if (getSequenceAt(i).getName().equals(name))
633 matches.addElement(getSequenceAt(i));
638 SequenceI[] result = new SequenceI[matches.size()];
639 for (i = 0; i < result.length; i++)
641 result[i] = (SequenceI) matches.elementAt(i);
651 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
654 public int findIndex(SequenceI s)
658 while (i < sequences.size())
660 if (s == getSequenceAt(i))
675 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
678 public int findIndex(SearchResultsI results)
682 while (i < sequences.size())
684 if (results.involvesSequence(getSequenceAt(i)))
694 public int getHeight()
696 return sequences.size();
700 public int getAbsoluteHeight()
702 return sequences.size() + getHiddenSequences().getSize();
706 public int getWidth()
710 for (int i = 0; i < sequences.size(); i++)
712 if (getSequenceAt(i).getLength() > maxLength)
714 maxLength = getSequenceAt(i).getLength();
728 public void setGapCharacter(char gc)
731 synchronized (sequences)
733 for (SequenceI seq : sequences)
735 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
736 .replace('-', gc).replace(' ', gc));
744 * @return DOCUMENT ME!
747 public char getGapCharacter()
755 * @see jalview.datamodel.AlignmentI#isAligned()
758 public boolean isAligned()
760 return isAligned(false);
766 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
769 public boolean isAligned(boolean includeHidden)
771 int width = getWidth();
772 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
774 includeHidden = true; // no hidden sequences to check against.
776 for (int i = 0; i < sequences.size(); i++)
778 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
780 if (getSequenceAt(i).getLength() != width)
791 public boolean isHidden(int alignmentIndex)
793 return (getHiddenSequences().getHiddenSequence(alignmentIndex) != null);
797 * Delete all annotations, including auto-calculated if the flag is set true.
798 * Returns true if at least one annotation was deleted, else false.
800 * @param includingAutoCalculated
804 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
806 boolean result = false;
807 for (AlignmentAnnotation alan : getAlignmentAnnotation())
809 if (!alan.autoCalculated || includingAutoCalculated)
811 deleteAnnotation(alan);
821 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
822 * AlignmentAnnotation)
825 public boolean deleteAnnotation(AlignmentAnnotation aa)
827 return deleteAnnotation(aa, true);
831 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
835 if (annotations != null)
837 aSize = annotations.length;
845 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
847 boolean swap = false;
850 for (int i = 0; i < aSize; i++)
852 if (annotations[i] == aa)
857 if (tIndex < temp.length)
859 temp[tIndex++] = annotations[i];
868 unhookAnnotation(aa);
875 * remove any object references associated with this annotation
879 private void unhookAnnotation(AlignmentAnnotation aa)
881 if (aa.sequenceRef != null)
883 aa.sequenceRef.removeAlignmentAnnotation(aa);
885 if (aa.groupRef != null)
887 // probably need to do more here in the future (post 2.5.0)
895 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
896 * AlignmentAnnotation)
899 public void addAnnotation(AlignmentAnnotation aa)
901 addAnnotation(aa, -1);
907 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
908 * AlignmentAnnotation, int)
911 public void addAnnotation(AlignmentAnnotation aa, int pos)
913 if (aa.getRNAStruc() != null)
915 hasRNAStructure = true;
919 if (annotations != null)
921 aSize = annotations.length + 1;
924 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
926 if (pos == -1 || pos >= aSize)
928 temp[aSize - 1] = aa;
937 for (i = 0; i < (aSize - 1); i++, p++)
945 temp[p] = annotations[i];
954 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
956 if (aa == null || annotations == null || annotations.length - 1 < index)
961 int aSize = annotations.length;
962 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
966 for (int i = 0; i < aSize; i++)
975 temp[i] = annotations[i];
979 temp[i] = annotations[i - 1];
988 * returns all annotation on the alignment
990 public AlignmentAnnotation[] getAlignmentAnnotation()
996 public boolean isNucleotide()
1002 public boolean hasRNAStructure()
1004 // TODO can it happen that structure is removed from alignment?
1005 return hasRNAStructure;
1009 public void setDataset(AlignmentI data)
1011 if (dataset == null && data == null)
1013 createDatasetAlignment();
1015 else if (dataset == null && data != null)
1019 throw new IllegalArgumentException("Circular dataset reference");
1021 if (!(data instanceof Alignment))
1024 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1026 dataset = (Alignment) data;
1027 for (int i = 0; i < getHeight(); i++)
1029 SequenceI currentSeq = getSequenceAt(i);
1030 SequenceI dsq = currentSeq.getDatasetSequence();
1033 dsq = currentSeq.createDatasetSequence();
1034 dataset.addSequence(dsq);
1038 while (dsq.getDatasetSequence() != null)
1040 dsq = dsq.getDatasetSequence();
1042 if (dataset.findIndex(dsq) == -1)
1044 dataset.addSequence(dsq);
1049 dataset.addAlignmentRef();
1053 * add dataset sequences to seq for currentSeq and any sequences it references
1055 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1056 Set<SequenceI> seqs, boolean createDatasetSequence)
1058 SequenceI alignedSeq = currentSeq;
1059 if (currentSeq.getDatasetSequence() != null)
1061 currentSeq = currentSeq.getDatasetSequence();
1065 if (createDatasetSequence)
1067 currentSeq = currentSeq.createDatasetSequence();
1071 List<SequenceI> toProcess = new ArrayList<>();
1072 toProcess.add(currentSeq);
1073 while (toProcess.size() > 0)
1076 SequenceI curDs = toProcess.remove(0);
1078 if (!seqs.add(curDs))
1082 // iterate over database references, making sure we add forward referenced
1084 if (curDs.getDBRefs() != null)
1086 for (DBRefEntry dbr : curDs.getDBRefs())
1088 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1090 if (dbr.getMap().getTo() == alignedSeq)
1093 * update mapping to be to the newly created dataset sequence
1095 dbr.getMap().setTo(currentSeq);
1097 if (dbr.getMap().getTo().getDatasetSequence() != null)
1099 throw new Error("Implementation error: Map.getTo() for dbref "
1100 + dbr + " from " + curDs.getName()
1101 + " is not a dataset sequence.");
1103 // we recurse to add all forward references to dataset sequences via
1105 toProcess.add(dbr.getMap().getTo());
1113 * Creates a new dataset for this alignment. Can only be done once - if
1114 * dataset is not null this will not be performed.
1116 public void createDatasetAlignment()
1118 if (dataset != null)
1122 // try to avoid using SequenceI.equals at this stage, it will be expensive
1123 Set<SequenceI> seqs = new LinkedIdentityHashSet<>();
1125 for (int i = 0; i < getHeight(); i++)
1127 SequenceI currentSeq = getSequenceAt(i);
1128 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1131 // verify all mappings are in dataset
1132 for (AlignedCodonFrame cf : codonFrameList)
1134 for (SequenceToSequenceMapping ssm : cf.getMappings())
1136 if (!seqs.contains(ssm.getFromSeq()))
1138 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1140 if (!seqs.contains(ssm.getMapping().getTo()))
1142 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1146 // finally construct dataset
1147 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1148 // move mappings to the dataset alignment
1149 dataset.codonFrameList = this.codonFrameList;
1150 this.codonFrameList = null;
1154 * reference count for number of alignments referencing this one.
1156 int alignmentRefs = 0;
1159 * increase reference count to this alignment.
1161 private void addAlignmentRef()
1167 public Alignment getDataset()
1173 public boolean padGaps()
1175 boolean modified = false;
1177 // Remove excess gaps from the end of alignment
1181 for (int i = 0; i < sequences.size(); i++)
1183 current = getSequenceAt(i);
1184 for (int j = current.getLength(); j > maxLength; j--)
1187 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1198 for (int i = 0; i < sequences.size(); i++)
1200 current = getSequenceAt(i);
1201 cLength = current.getLength();
1203 if (cLength < maxLength)
1205 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1208 else if (current.getLength() > maxLength)
1210 current.deleteChars(maxLength, current.getLength());
1217 * Justify the sequences to the left or right by deleting and inserting gaps
1218 * before the initial residue or after the terminal residue
1221 * true if alignment padded to right, false to justify to left
1222 * @return true if alignment was changed
1225 public boolean justify(boolean right)
1227 boolean modified = false;
1229 // Remove excess gaps from the end of alignment
1231 int ends[] = new int[sequences.size() * 2];
1233 for (int i = 0; i < sequences.size(); i++)
1235 current = getSequenceAt(i);
1236 // This should really be a sequence method
1237 ends[i * 2] = current.findIndex(current.getStart());
1238 ends[i * 2 + 1] = current
1239 .findIndex(current.getStart() + current.getLength());
1240 boolean hitres = false;
1241 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1243 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1252 ends[i * 2 + 1] = j;
1253 if (j - ends[i * 2] > maxLength)
1255 maxLength = j - ends[i * 2];
1263 // now edit the flanking gaps to justify to either left or right
1264 int cLength, extent, diff;
1265 for (int i = 0; i < sequences.size(); i++)
1267 current = getSequenceAt(i);
1269 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1270 diff = maxLength - cLength; // number of gaps to indent
1271 extent = current.getLength();
1275 if (extent > ends[i * 2 + 1])
1277 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1280 if (ends[i * 2] > diff)
1282 current.deleteChars(0, ends[i * 2] - diff);
1287 if (ends[i * 2] < diff)
1289 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1297 if (ends[i * 2] > 0)
1299 current.deleteChars(0, ends[i * 2]);
1301 ends[i * 2 + 1] -= ends[i * 2];
1302 extent -= ends[i * 2];
1304 if (extent > maxLength)
1306 current.deleteChars(maxLength + 1, extent);
1311 if (extent < maxLength)
1313 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1323 public HiddenSequences getHiddenSequences()
1325 return hiddenSequences;
1329 public HiddenColumns getHiddenColumns()
1335 public CigarArray getCompactAlignment()
1337 synchronized (sequences)
1339 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1341 for (SequenceI seq : sequences)
1343 alseqs[i++] = new SeqCigar(seq);
1345 CigarArray cal = new CigarArray(alseqs);
1346 cal.addOperation(CigarArray.M, getWidth());
1352 public void setProperty(Object key, Object value)
1354 if (alignmentProperties == null)
1356 alignmentProperties = new Hashtable();
1359 alignmentProperties.put(key, value);
1363 public Object getProperty(Object key)
1365 if (alignmentProperties != null)
1367 return alignmentProperties.get(key);
1376 public Hashtable getProperties()
1378 return alignmentProperties;
1382 * Adds the given mapping to the stored set. Note this may be held on the
1383 * dataset alignment.
1386 public void addCodonFrame(AlignedCodonFrame codons)
1388 List<AlignedCodonFrame> acfs = getCodonFrames();
1389 if (codons != null && acfs != null && !acfs.contains(codons))
1399 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1402 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1408 List<AlignedCodonFrame> cframes = new ArrayList<>();
1409 for (AlignedCodonFrame acf : getCodonFrames())
1411 if (acf.involvesSequence(seq))
1420 * Sets the codon frame mappings (replacing any existing mappings). Note the
1421 * mappings are set on the dataset alignment instead if there is one.
1423 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1426 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1428 if (dataset != null)
1430 dataset.setCodonFrames(acfs);
1434 this.codonFrameList = acfs;
1439 * Returns the set of codon frame mappings. Any changes to the returned set
1440 * will affect the alignment. The mappings are held on (and read from) the
1441 * dataset alignment if there is one.
1443 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1446 public List<AlignedCodonFrame> getCodonFrames()
1448 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1449 // this behaviour is currently incorrect. method should return codon frames
1450 // for just the alignment,
1451 // selected from dataset
1452 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1456 * Removes the given mapping from the stored set. Note that the mappings are
1457 * held on the dataset alignment if there is one.
1460 public boolean removeCodonFrame(AlignedCodonFrame codons)
1462 List<AlignedCodonFrame> acfs = getCodonFrames();
1463 if (codons == null || acfs == null)
1467 return acfs.remove(codons);
1471 public void append(AlignmentI toappend)
1473 // TODO JAL-1270 needs test coverage
1474 // currently tested for use in jalview.gui.SequenceFetcher
1475 char oldc = toappend.getGapCharacter();
1476 boolean samegap = oldc == getGapCharacter();
1477 boolean hashidden = toappend.getHiddenSequences() != null
1478 && toappend.getHiddenSequences().hiddenSequences != null;
1479 // get all sequences including any hidden ones
1480 List<SequenceI> sqs = (hashidden)
1481 ? toappend.getHiddenSequences().getFullAlignment()
1483 : toappend.getSequences();
1486 // avoid self append deadlock by
1487 List<SequenceI> toappendsq = new ArrayList<>();
1490 for (SequenceI addedsq : sqs)
1494 addedsq.replace(oldc, gapCharacter);
1496 toappendsq.add(addedsq);
1499 for (SequenceI addedsq : toappendsq)
1501 addSequence(addedsq);
1504 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1505 for (int a = 0; alan != null && a < alan.length; a++)
1507 addAnnotation(alan[a]);
1511 getCodonFrames().addAll(toappend.getCodonFrames());
1513 List<SequenceGroup> sg = toappend.getGroups();
1516 for (SequenceGroup _sg : sg)
1521 if (toappend.getHiddenSequences() != null)
1523 HiddenSequences hs = toappend.getHiddenSequences();
1524 if (hiddenSequences == null)
1526 hiddenSequences = new HiddenSequences(this);
1528 if (hs.hiddenSequences != null)
1530 for (int s = 0; s < hs.hiddenSequences.length; s++)
1532 // hide the newly appended sequence in the alignment
1533 if (hs.hiddenSequences[s] != null)
1535 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1540 if (toappend.getProperties() != null)
1542 // we really can't do very much here - just try to concatenate strings
1543 // where property collisions occur.
1544 Enumeration key = toappend.getProperties().keys();
1545 while (key.hasMoreElements())
1547 Object k = key.nextElement();
1548 Object ourval = this.getProperty(k);
1549 Object toapprop = toappend.getProperty(k);
1552 if (ourval.getClass().equals(toapprop.getClass())
1553 && !ourval.equals(toapprop))
1555 if (ourval instanceof String)
1559 ((String) ourval) + "; " + ((String) toapprop));
1563 if (ourval instanceof Vector)
1566 Enumeration theirv = ((Vector) toapprop).elements();
1567 while (theirv.hasMoreElements())
1569 ((Vector) ourval).addElement(theirv);
1577 // just add new property directly
1578 setProperty(k, toapprop);
1586 public AlignmentAnnotation findOrCreateAnnotation(String name,
1587 String calcId, boolean autoCalc, SequenceI seqRef,
1588 SequenceGroup groupRef)
1590 if (annotations != null)
1592 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1594 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1595 && (calcId == null || annot.getCalcId().equals(calcId))
1596 && annot.sequenceRef == seqRef
1597 && annot.groupRef == groupRef)
1603 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1604 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1605 annot.hasText = false;
1606 annot.setCalcId(new String(calcId));
1607 annot.autoCalculated = autoCalc;
1610 annot.setSequenceRef(seqRef);
1612 annot.groupRef = groupRef;
1613 addAnnotation(annot);
1619 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1621 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1622 if (alignmentAnnotation != null)
1624 return AlignmentAnnotation.findAnnotation(
1625 Arrays.asList(getAlignmentAnnotation()), calcId);
1627 return Arrays.asList(new AlignmentAnnotation[] {});
1631 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1632 String calcId, String label)
1634 return AlignmentAnnotation.findAnnotations(
1635 Arrays.asList(getAlignmentAnnotation()), seq, calcId, label);
1639 public void moveSelectedSequencesByOne(SequenceGroup sg,
1640 Map<SequenceI, SequenceCollectionI> map, boolean up)
1642 synchronized (sequences)
1647 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1649 SequenceI seq = sequences.get(i);
1650 if (!sg.getSequences(map).contains(seq))
1655 SequenceI temp = sequences.get(i - 1);
1656 if (sg.getSequences(null).contains(temp))
1661 sequences.set(i, temp);
1662 sequences.set(i - 1, seq);
1667 for (int i = sequences.size() - 2; i > -1; i--)
1669 SequenceI seq = sequences.get(i);
1670 if (!sg.getSequences(map).contains(seq))
1675 SequenceI temp = sequences.get(i + 1);
1676 if (sg.getSequences(map).contains(temp))
1681 sequences.set(i, temp);
1682 sequences.set(i + 1, seq);
1690 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1692 alignmentAnnotation.validateRangeAndDisplay();
1693 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1695 hasRNAStructure = true;
1699 private SequenceI seqrep = null;
1703 * @return the representative sequence for this group
1706 public SequenceI getSeqrep()
1712 * set the representative sequence for this group. Note - this affects the
1713 * interpretation of the Hidereps attribute.
1716 * the seqrep to set (null means no sequence representative)
1719 public void setSeqrep(SequenceI seqrep)
1721 this.seqrep = seqrep;
1726 * @return true if group has a sequence representative
1729 public boolean hasSeqrep()
1731 return seqrep != null;
1735 public int getEndRes()
1737 return getWidth() - 1;
1741 public int getStartRes()
1747 * In the case of AlignmentI - returns the dataset for the alignment, if set
1750 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1753 public AnnotatedCollectionI getContext()
1759 * Align this alignment like the given (mapped) one.
1762 public int alignAs(AlignmentI al)
1765 * Currently retains unmapped gaps (in introns), regaps mapped regions
1768 return alignAs(al, false, true);
1772 * Align this alignment 'the same as' the given one. Mapped sequences only are
1773 * realigned. If both of the same type (nucleotide/protein) then align both
1774 * identically. If this is nucleotide and the other is protein, make 3 gaps
1775 * for each gap in the protein sequences. If this is protein and the other is
1776 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1777 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1778 * protein to match the relative ordering of codons in the nucleotide.
1780 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1781 * regions are preserved. Gaps that connect introns to exons are treated
1782 * conservatively, i.e. only preserved if both intron and exon gaps are
1783 * preserved. TODO: check caveats below where the implementation fails
1786 * - must have same dataset, and sequences in al must have equivalent
1787 * dataset sequence and start/end bounds under given mapping
1788 * @param preserveMappedGaps
1789 * if true, gaps within and between mapped codons are preserved
1790 * @param preserveUnmappedGaps
1791 * if true, gaps within and between unmapped codons are preserved
1794 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1795 boolean preserveUnmappedGaps)
1797 // TODO should this method signature be the one in the interface?
1798 // JBPComment - yes - neither flag is used, so should be deleted.
1799 boolean thisIsNucleotide = this.isNucleotide();
1800 boolean thatIsProtein = !al.isNucleotide();
1801 if (!thatIsProtein && !thisIsNucleotide)
1803 return AlignmentUtils.alignProteinAsDna(this, al);
1805 else if (thatIsProtein && thisIsNucleotide)
1807 return AlignmentUtils.alignCdsAsProtein(this, al);
1809 return AlignmentUtils.alignAs(this, al);
1813 * Returns the alignment in Fasta format. Behaviour of this method is not
1814 * guaranteed between versions.
1817 public String toString()
1819 return new FastaFile().print(getSequencesArray(), true);
1823 * Returns the set of distinct sequence names. No ordering is guaranteed.
1826 public Set<String> getSequenceNames()
1828 Set<String> names = new HashSet<>();
1829 for (SequenceI seq : getSequences())
1831 names.add(seq.getName());
1837 public boolean hasValidSequence()
1839 boolean hasValidSeq = false;
1840 for (SequenceI seq : getSequences())
1842 if ((seq.getEnd() - seq.getStart()) > 0)
1852 * Update any mappings to 'virtual' sequences to compatible real ones, if
1853 * present in the added sequences. Returns a count of mappings updated.
1859 public int realiseMappings(List<SequenceI> seqs)
1862 for (SequenceI seq : seqs)
1864 for (AlignedCodonFrame mapping : getCodonFrames())
1866 count += mapping.realiseWith(seq);
1873 * Returns the first AlignedCodonFrame that has a mapping between the given
1881 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1883 for (AlignedCodonFrame acf : getCodonFrames())
1885 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1894 public void setHiddenColumns(HiddenColumns cols)