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.analysis.Conservation;
25 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
26 import jalview.io.FastaFile;
27 import jalview.util.Comparison;
28 import jalview.util.LinkedIdentityHashSet;
29 import jalview.util.MessageManager;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.BitSet;
34 import java.util.Collections;
35 import java.util.Enumeration;
36 import java.util.HashSet;
37 import java.util.Hashtable;
38 import java.util.Iterator;
39 import java.util.List;
42 import java.util.Vector;
45 * Data structure to hold and manipulate a multiple sequence alignment
51 public class Alignment implements AlignmentI, AutoCloseable
53 private Alignment dataset;
55 private List<SequenceI> sequences;
57 protected List<SequenceGroup> groups;
59 protected char gapCharacter = '-';
61 private boolean nucleotide = true;
63 public boolean hasRNAStructure = false;
65 public AlignmentAnnotation[] annotations;
67 HiddenSequences hiddenSequences;
69 HiddenColumns hiddenCols;
71 public Hashtable alignmentProperties;
73 private List<AlignedCodonFrame> codonFrameList;
75 private Conservation conservation;
77 private ProfilesI consensus;
79 private Hashtable[] codonConsensus, rnaStructureConsensus;
81 private void initAlignment(SequenceI[] seqs)
83 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
84 hiddenSequences = new HiddenSequences(this);
85 hiddenCols = new HiddenColumns();
86 codonFrameList = new ArrayList<>();
88 nucleotide = Comparison.isNucleotide(seqs);
90 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
92 for (int i = 0; i < seqs.length; i++)
94 sequences.add(seqs[i]);
100 * Make a 'copy' alignment - sequences have new copies of features and
101 * annotations, but share the original dataset sequences.
103 public Alignment(AlignmentI al)
105 SequenceI[] seqs = al.getSequencesArray();
106 for (int i = 0; i < seqs.length; i++)
108 seqs[i] = new Sequence(seqs[i]);
114 * Share the same dataset sequence mappings (if any).
116 if (dataset == null && al.getDataset() == null)
118 this.setCodonFrames(al.getCodonFrames());
123 * Make an alignment from an array of Sequences.
127 public Alignment(SequenceI[] seqs)
133 * Make a new alignment from an array of SeqCigars
138 public Alignment(SeqCigar[] alseqs)
140 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
141 gapCharacter, new HiddenColumns(), null);
146 * Make a new alignment from an CigarArray JBPNote - can only do this when
147 * compactAlignment does not contain hidden regions. JBPNote - must also check
148 * that compactAlignment resolves to a set of SeqCigars - or construct them
151 * @param compactAlignment
154 public static AlignmentI createAlignment(CigarArray compactAlignment)
156 throw new Error(MessageManager
157 .getString("error.alignment_cigararray_not_implemented"));
158 // this(compactAlignment.refCigars);
162 public List<SequenceI> getSequences()
168 public List<SequenceI> getSequences(
169 Map<SequenceI, SequenceCollectionI> hiddenReps)
171 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
177 public SequenceI[] getSequencesArray()
179 if (sequences == null)
183 synchronized (sequences)
185 return sequences.toArray(new SequenceI[sequences.size()]);
190 * Returns a map of lists of sequences keyed by sequence name.
195 public Map<String, List<SequenceI>> getSequencesByName()
197 return AlignmentUtils.getSequencesByName(this);
201 public SequenceI getSequenceAt(int i)
203 synchronized (sequences)
206 if (i > -1 && i < sequences.size())
208 return sequences.get(i);
216 public SequenceI getSequenceAtAbsoluteIndex(int i)
218 SequenceI seq = null;
219 if (getHiddenSequences().getSize() > 0)
221 seq = getHiddenSequences().getHiddenSequence(i);
224 // didn't find the sequence in the hidden sequences, get it from the
226 int index = getHiddenSequences().findIndexWithoutHiddenSeqs(i);
227 seq = getSequenceAt(index);
232 seq = getSequenceAt(i);
238 * Adds a sequence to the alignment. Recalculates maxLength and size. Note
239 * this currently does not recalculate whether or not the alignment is
240 * nucleotide, so mixed alignments may have undefined behaviour.
245 public void addSequence(SequenceI snew)
250 // maintain dataset integrity
251 SequenceI dsseq = snew.getDatasetSequence();
254 // derive new sequence
255 SequenceI adding = snew.deriveSequence();
257 dsseq = snew.getDatasetSequence();
259 if (getDataset().findIndex(dsseq) == -1)
261 getDataset().addSequence(dsseq);
265 if (sequences == null)
267 initAlignment(new SequenceI[] { snew });
271 synchronized (sequences)
276 if (hiddenSequences != null)
278 hiddenSequences.adjustHeightSequenceAdded();
283 public SequenceI replaceSequenceAt(int i, SequenceI snew)
285 synchronized (sequences)
287 if (sequences.size() > i)
289 return sequences.set(i, snew);
295 hiddenSequences.adjustHeightSequenceAdded();
304 * @return DOCUMENT ME!
307 public List<SequenceGroup> getGroups()
315 if (getDataset() != null)
319 getDataset().removeAlignmentRef();
320 } catch (Throwable e)
330 * Defensively nulls out references in case this object is not garbage
333 void nullReferences()
339 hiddenSequences = null;
343 * decrement the alignmentRefs counter by one and null references if it goes
348 private void removeAlignmentRef() throws Throwable
350 if (--alignmentRefs == 0)
357 public void deleteSequence(SequenceI s)
359 synchronized (sequences)
361 deleteSequence(findIndex(s));
366 public void deleteSequence(int i)
368 synchronized (sequences)
370 if (i > -1 && i < getHeight())
373 hiddenSequences.adjustHeightSequenceDeleted(i);
379 public void deleteHiddenSequence(int i)
381 synchronized (sequences)
383 if (i > -1 && i < getHeight())
393 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
396 public SequenceGroup findGroup(SequenceI seq, int position)
398 synchronized (groups)
400 for (SequenceGroup sg : groups)
402 if (sg.getSequences(null).contains(seq))
404 if (position >= sg.getStartRes() && position <= sg.getEndRes())
418 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
421 public SequenceGroup[] findAllGroups(SequenceI s)
423 ArrayList<SequenceGroup> temp = new ArrayList<>();
425 synchronized (groups)
427 int gSize = groups.size();
428 for (int i = 0; i < gSize; i++)
430 SequenceGroup sg = groups.get(i);
431 if (sg == null || sg.getSequences() == null)
433 this.deleteGroup(sg);
438 if (sg.getSequences().contains(s))
444 SequenceGroup[] ret = new SequenceGroup[temp.size()];
445 return temp.toArray(ret);
450 public void addGroup(SequenceGroup sg)
452 synchronized (groups)
454 if (!groups.contains(sg))
456 if (hiddenSequences.getSize() > 0)
458 int i, iSize = sg.getSize();
459 for (i = 0; i < iSize; i++)
461 if (!sequences.contains(sg.getSequenceAt(i)))
463 sg.deleteSequence(sg.getSequenceAt(i), false);
469 if (sg.getSize() < 1)
474 sg.setContext(this, true);
481 * remove any annotation that references gp
484 * (if null, removes all group associated annotation)
486 private void removeAnnotationForGroup(SequenceGroup gp)
488 if (annotations == null || annotations.length == 0)
492 // remove annotation very quickly
493 AlignmentAnnotation[] t,
494 todelete = new AlignmentAnnotation[annotations.length],
495 tokeep = new AlignmentAnnotation[annotations.length];
499 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
501 if (annotations[i].groupRef != null)
503 todelete[p++] = annotations[i];
507 tokeep[k++] = annotations[i];
513 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
515 if (annotations[i].groupRef == gp)
517 todelete[p++] = annotations[i];
521 tokeep[k++] = annotations[i];
527 // clear out the group associated annotation.
528 for (i = 0; i < p; i++)
530 unhookAnnotation(todelete[i]);
533 t = new AlignmentAnnotation[k];
534 for (i = 0; i < k; i++)
543 public void deleteAllGroups()
545 synchronized (groups)
547 if (annotations != null)
549 removeAnnotationForGroup(null);
551 for (SequenceGroup sg : groups)
553 sg.setContext(null, false);
561 public void deleteGroup(SequenceGroup g)
563 synchronized (groups)
565 if (groups.contains(g))
567 removeAnnotationForGroup(g);
569 g.setContext(null, false);
576 public SequenceI findName(String name)
578 return findName(name, false);
584 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
587 public SequenceI findName(String token, boolean b)
589 return findName(null, token, b);
595 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
599 public SequenceI findName(SequenceI startAfter, String token, boolean b)
604 String sqname = null;
605 int nseq = sequences.size();
606 if (startAfter != null)
608 // try to find the sequence in the alignment
609 boolean matched = false;
612 if (getSequenceAt(i++) == startAfter)
625 sq = getSequenceAt(i);
626 sqname = sq.getName();
627 if (sqname.equals(token) // exact match
628 || (b && // allow imperfect matches - case varies
629 (sqname.equalsIgnoreCase(token))))
631 return getSequenceAt(i);
641 public SequenceI[] findSequenceMatch(String name)
643 Vector matches = new Vector();
646 while (i < sequences.size())
648 if (getSequenceAt(i).getName().equals(name))
650 matches.addElement(getSequenceAt(i));
655 SequenceI[] result = new SequenceI[matches.size()];
656 for (i = 0; i < result.length; i++)
658 result[i] = (SequenceI) matches.elementAt(i);
668 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
671 public int findIndex(SequenceI s)
675 while (i < sequences.size())
677 if (s == getSequenceAt(i))
692 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
695 public int findIndex(SearchResultsI results)
699 while (i < sequences.size())
701 if (results.involvesSequence(getSequenceAt(i)))
711 public int getHeight()
713 return sequences.size();
717 public int getAbsoluteHeight()
719 return sequences.size() + getHiddenSequences().getSize();
723 public int getWidth()
727 for (int i = 0; i < sequences.size(); i++)
729 maxLength = Math.max(maxLength, getSequenceAt(i).getLength());
735 public int getVisibleWidth()
738 if (hiddenCols != null)
740 w -= hiddenCols.getSize();
752 public void setGapCharacter(char gc)
755 synchronized (sequences)
757 for (SequenceI seq : sequences)
759 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
760 .replace('-', gc).replace(' ', gc));
768 * @return DOCUMENT ME!
771 public char getGapCharacter()
779 * @see jalview.datamodel.AlignmentI#isAligned()
782 public boolean isAligned()
784 return isAligned(false);
790 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
793 public boolean isAligned(boolean includeHidden)
795 int width = getWidth();
796 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
798 includeHidden = true; // no hidden sequences to check against.
800 for (int i = 0; i < sequences.size(); i++)
802 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
804 if (getSequenceAt(i).getLength() != width)
815 public boolean isHidden(int alignmentIndex)
817 return (getHiddenSequences().getHiddenSequence(alignmentIndex) != null);
821 * Delete all annotations, including auto-calculated if the flag is set true.
822 * Returns true if at least one annotation was deleted, else false.
824 * @param includingAutoCalculated
828 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
830 boolean result = false;
831 for (AlignmentAnnotation alan : getAlignmentAnnotation())
833 if (!alan.autoCalculated || includingAutoCalculated)
835 deleteAnnotation(alan);
845 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
846 * AlignmentAnnotation)
849 public boolean deleteAnnotation(AlignmentAnnotation aa)
851 return deleteAnnotation(aa, true);
855 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
859 if (annotations != null)
861 aSize = annotations.length;
869 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
871 boolean swap = false;
874 for (int i = 0; i < aSize; i++)
876 if (annotations[i] == aa)
881 if (tIndex < temp.length)
883 temp[tIndex++] = annotations[i];
892 unhookAnnotation(aa);
899 * remove any object references associated with this annotation
903 private void unhookAnnotation(AlignmentAnnotation aa)
905 if (aa.sequenceRef != null)
907 aa.sequenceRef.removeAlignmentAnnotation(aa);
909 if (aa.groupRef != null)
911 // probably need to do more here in the future (post 2.5.0)
919 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
920 * AlignmentAnnotation)
923 public void addAnnotation(AlignmentAnnotation aa)
925 addAnnotation(aa, -1);
931 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
932 * AlignmentAnnotation, int)
935 public void addAnnotation(AlignmentAnnotation aa, int pos)
937 if (aa.getRNAStruc() != null)
939 hasRNAStructure = true;
943 if (annotations != null)
945 aSize = annotations.length + 1;
948 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
950 if (pos == -1 || pos >= aSize)
952 temp[aSize - 1] = aa;
961 for (i = 0; i < (aSize - 1); i++, p++)
969 temp[p] = annotations[i];
978 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
980 if (aa == null || annotations == null || annotations.length - 1 < index)
985 int aSize = annotations.length;
986 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
990 for (int i = 0; i < aSize; i++)
999 temp[i] = annotations[i];
1003 temp[i] = annotations[i - 1];
1012 * returns all annotation on the alignment
1014 public AlignmentAnnotation[] getAlignmentAnnotation()
1020 public boolean isNucleotide()
1026 public boolean hasRNAStructure()
1028 // TODO can it happen that structure is removed from alignment?
1029 return hasRNAStructure;
1033 public void setDataset(AlignmentI data)
1035 if (dataset == null && data == null)
1037 createDatasetAlignment();
1039 else if (dataset == null && data != null)
1043 throw new IllegalArgumentException("Circular dataset reference");
1045 if (!(data instanceof Alignment))
1048 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1050 dataset = (Alignment) data;
1051 for (int i = 0; i < getHeight(); i++)
1053 SequenceI currentSeq = getSequenceAt(i);
1054 SequenceI dsq = currentSeq.getDatasetSequence();
1057 dsq = currentSeq.createDatasetSequence();
1058 dataset.addSequence(dsq);
1062 while (dsq.getDatasetSequence() != null)
1064 dsq = dsq.getDatasetSequence();
1066 if (dataset.findIndex(dsq) == -1)
1068 dataset.addSequence(dsq);
1073 dataset.addAlignmentRef();
1077 * add dataset sequences to seq for currentSeq and any sequences it references
1079 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1080 Set<SequenceI> seqs, boolean createDatasetSequence)
1082 SequenceI alignedSeq = currentSeq;
1083 if (currentSeq.getDatasetSequence() != null)
1085 currentSeq = currentSeq.getDatasetSequence();
1089 if (createDatasetSequence)
1091 currentSeq = currentSeq.createDatasetSequence();
1095 List<SequenceI> toProcess = new ArrayList<>();
1096 toProcess.add(currentSeq);
1097 while (toProcess.size() > 0)
1100 SequenceI curDs = toProcess.remove(0);
1102 if (!seqs.add(curDs))
1106 // iterate over database references, making sure we add forward referenced
1108 if (curDs.getDBRefs() != null)
1110 for (DBRefEntry dbr : curDs.getDBRefs())
1112 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1114 if (dbr.getMap().getTo() == alignedSeq)
1117 * update mapping to be to the newly created dataset sequence
1119 dbr.getMap().setTo(currentSeq);
1121 if (dbr.getMap().getTo().getDatasetSequence() != null)
1123 throw new Error("Implementation error: Map.getTo() for dbref "
1124 + dbr + " from " + curDs.getName()
1125 + " is not a dataset sequence.");
1127 // we recurse to add all forward references to dataset sequences via
1129 toProcess.add(dbr.getMap().getTo());
1137 * Creates a new dataset for this alignment. Can only be done once - if
1138 * dataset is not null this will not be performed.
1140 public void createDatasetAlignment()
1142 if (dataset != null)
1146 // try to avoid using SequenceI.equals at this stage, it will be expensive
1147 Set<SequenceI> seqs = new LinkedIdentityHashSet<>();
1149 for (int i = 0; i < getHeight(); i++)
1151 SequenceI currentSeq = getSequenceAt(i);
1152 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1155 // verify all mappings are in dataset
1156 for (AlignedCodonFrame cf : codonFrameList)
1158 for (SequenceToSequenceMapping ssm : cf.getMappings())
1160 if (!seqs.contains(ssm.getFromSeq()))
1162 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1164 if (!seqs.contains(ssm.getMapping().getTo()))
1166 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1170 // finally construct dataset
1171 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1172 // move mappings to the dataset alignment
1173 dataset.codonFrameList = this.codonFrameList;
1174 this.codonFrameList = null;
1178 * reference count for number of alignments referencing this one.
1180 int alignmentRefs = 0;
1183 * increase reference count to this alignment.
1185 private void addAlignmentRef()
1191 public Alignment getDataset()
1197 public boolean padGaps()
1199 boolean modified = false;
1201 // Remove excess gaps from the end of alignment
1205 int nseq = sequences.size();
1206 for (int i = 0; i < nseq; i++)
1208 current = getSequenceAt(i);
1209 for (int j = current.getLength(); j > maxLength; j--)
1212 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1223 for (int i = 0; i < nseq; i++)
1225 current = getSequenceAt(i);
1226 cLength = current.getLength();
1228 if (cLength < maxLength)
1230 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1233 else if (current.getLength() > maxLength)
1235 current.deleteChars(maxLength, current.getLength());
1242 * Justify the sequences to the left or right by deleting and inserting gaps
1243 * before the initial residue or after the terminal residue
1246 * true if alignment padded to right, false to justify to left
1247 * @return true if alignment was changed
1250 public boolean justify(boolean right)
1252 boolean modified = false;
1254 // Remove excess gaps from the end of alignment
1256 int ends[] = new int[sequences.size() * 2];
1258 for (int i = 0; i < sequences.size(); i++)
1260 current = getSequenceAt(i);
1261 // This should really be a sequence method
1262 ends[i * 2] = current.findIndex(current.getStart());
1263 ends[i * 2 + 1] = current
1264 .findIndex(current.getStart() + current.getLength());
1265 boolean hitres = false;
1266 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1268 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1277 ends[i * 2 + 1] = j;
1278 if (j - ends[i * 2] > maxLength)
1280 maxLength = j - ends[i * 2];
1288 // now edit the flanking gaps to justify to either left or right
1289 int cLength, extent, diff;
1290 for (int i = 0; i < sequences.size(); i++)
1292 current = getSequenceAt(i);
1294 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1295 diff = maxLength - cLength; // number of gaps to indent
1296 extent = current.getLength();
1300 if (extent > ends[i * 2 + 1])
1302 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1305 if (ends[i * 2] > diff)
1307 current.deleteChars(0, ends[i * 2] - diff);
1312 if (ends[i * 2] < diff)
1314 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1322 if (ends[i * 2] > 0)
1324 current.deleteChars(0, ends[i * 2]);
1326 ends[i * 2 + 1] -= ends[i * 2];
1327 extent -= ends[i * 2];
1329 if (extent > maxLength)
1331 current.deleteChars(maxLength + 1, extent);
1336 if (extent < maxLength)
1338 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1348 public HiddenSequences getHiddenSequences()
1350 return hiddenSequences;
1354 public HiddenColumns getHiddenColumns()
1360 public CigarArray getCompactAlignment()
1362 synchronized (sequences)
1364 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1366 for (SequenceI seq : sequences)
1368 alseqs[i++] = new SeqCigar(seq);
1370 CigarArray cal = new CigarArray(alseqs);
1371 cal.addOperation(CigarArray.M, getWidth());
1377 public void setProperty(Object key, Object value)
1379 if (alignmentProperties == null)
1381 alignmentProperties = new Hashtable();
1384 alignmentProperties.put(key, value);
1388 public Object getProperty(Object key)
1390 if (alignmentProperties != null)
1392 return alignmentProperties.get(key);
1401 public Hashtable getProperties()
1403 return alignmentProperties;
1407 * Adds the given mapping to the stored set. Note this may be held on the
1408 * dataset alignment.
1411 public void addCodonFrame(AlignedCodonFrame codons)
1413 List<AlignedCodonFrame> acfs = getCodonFrames();
1414 if (codons != null && acfs != null && !acfs.contains(codons))
1424 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1427 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1433 List<AlignedCodonFrame> cframes = new ArrayList<>();
1434 for (AlignedCodonFrame acf : getCodonFrames())
1436 if (acf.involvesSequence(seq))
1445 * Sets the codon frame mappings (replacing any existing mappings). Note the
1446 * mappings are set on the dataset alignment instead if there is one.
1448 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1451 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1453 if (dataset != null)
1455 dataset.setCodonFrames(acfs);
1459 this.codonFrameList = acfs;
1464 * Returns the set of codon frame mappings. Any changes to the returned set
1465 * will affect the alignment. The mappings are held on (and read from) the
1466 * dataset alignment if there is one.
1468 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1471 public List<AlignedCodonFrame> getCodonFrames()
1473 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1474 // this behaviour is currently incorrect. method should return codon frames
1475 // for just the alignment,
1476 // selected from dataset
1477 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1481 * Removes the given mapping from the stored set. Note that the mappings are
1482 * held on the dataset alignment if there is one.
1485 public boolean removeCodonFrame(AlignedCodonFrame codons)
1487 List<AlignedCodonFrame> acfs = getCodonFrames();
1488 if (codons == null || acfs == null)
1492 return acfs.remove(codons);
1496 public void append(AlignmentI toappend)
1498 // TODO JAL-1270 needs test coverage
1499 // currently tested for use in jalview.gui.SequenceFetcher
1500 char oldc = toappend.getGapCharacter();
1501 boolean samegap = oldc == getGapCharacter();
1502 boolean hashidden = toappend.getHiddenSequences() != null
1503 && toappend.getHiddenSequences().hiddenSequences != null;
1504 // get all sequences including any hidden ones
1505 List<SequenceI> sqs = (hashidden)
1506 ? toappend.getHiddenSequences().getFullAlignment()
1508 : toappend.getSequences();
1511 // avoid self append deadlock by
1512 List<SequenceI> toappendsq = new ArrayList<>();
1515 for (SequenceI addedsq : sqs)
1519 addedsq.replace(oldc, gapCharacter);
1521 toappendsq.add(addedsq);
1524 for (SequenceI addedsq : toappendsq)
1526 addSequence(addedsq);
1529 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1530 for (int a = 0; alan != null && a < alan.length; a++)
1532 addAnnotation(alan[a]);
1536 getCodonFrames().addAll(toappend.getCodonFrames());
1538 List<SequenceGroup> sg = toappend.getGroups();
1541 for (SequenceGroup _sg : sg)
1546 if (toappend.getHiddenSequences() != null)
1548 HiddenSequences hs = toappend.getHiddenSequences();
1549 if (hiddenSequences == null)
1551 hiddenSequences = new HiddenSequences(this);
1553 if (hs.hiddenSequences != null)
1555 for (int s = 0; s < hs.hiddenSequences.length; s++)
1557 // hide the newly appended sequence in the alignment
1558 if (hs.hiddenSequences[s] != null)
1560 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1565 if (toappend.getProperties() != null)
1567 // we really can't do very much here - just try to concatenate strings
1568 // where property collisions occur.
1569 Enumeration key = toappend.getProperties().keys();
1570 while (key.hasMoreElements())
1572 Object k = key.nextElement();
1573 Object ourval = this.getProperty(k);
1574 Object toapprop = toappend.getProperty(k);
1577 if (ourval.getClass().equals(toapprop.getClass())
1578 && !ourval.equals(toapprop))
1580 if (ourval instanceof String)
1584 ((String) ourval) + "; " + ((String) toapprop));
1588 if (ourval instanceof Vector)
1591 Enumeration theirv = ((Vector) toapprop).elements();
1592 while (theirv.hasMoreElements())
1594 ((Vector) ourval).addElement(theirv);
1602 // just add new property directly
1603 setProperty(k, toapprop);
1611 public AlignmentAnnotation findOrCreateAnnotation(String name,
1612 String calcId, boolean autoCalc, SequenceI seqRef,
1613 SequenceGroup groupRef)
1615 if (annotations != null)
1617 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1619 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1620 && (calcId == null || annot.getCalcId().equals(calcId))
1621 && annot.sequenceRef == seqRef
1622 && annot.groupRef == groupRef)
1628 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1629 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1630 annot.hasText = false;
1633 annot.setCalcId(new String(calcId));
1635 annot.autoCalculated = autoCalc;
1638 annot.setSequenceRef(seqRef);
1640 annot.groupRef = groupRef;
1641 addAnnotation(annot);
1647 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1649 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1650 if (alignmentAnnotation != null)
1652 return AlignmentAnnotation.findAnnotation(
1653 Arrays.asList(getAlignmentAnnotation()), calcId);
1655 return Arrays.asList(new AlignmentAnnotation[] {});
1659 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1660 String calcId, String label)
1662 return AlignmentAnnotation.findAnnotations(
1663 Arrays.asList(getAlignmentAnnotation()), seq, calcId, label);
1667 public void moveSelectedSequencesByOne(SequenceGroup sg,
1668 Map<SequenceI, SequenceCollectionI> map, boolean up)
1670 synchronized (sequences)
1675 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1677 SequenceI seq = sequences.get(i);
1678 if (!sg.getSequences(map).contains(seq))
1683 SequenceI temp = sequences.get(i - 1);
1684 if (sg.getSequences(null).contains(temp))
1689 sequences.set(i, temp);
1690 sequences.set(i - 1, seq);
1695 for (int i = sequences.size() - 2; i > -1; i--)
1697 SequenceI seq = sequences.get(i);
1698 if (!sg.getSequences(map).contains(seq))
1703 SequenceI temp = sequences.get(i + 1);
1704 if (sg.getSequences(map).contains(temp))
1709 sequences.set(i, temp);
1710 sequences.set(i + 1, seq);
1718 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1720 alignmentAnnotation.validateRangeAndDisplay();
1721 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1723 hasRNAStructure = true;
1727 private SequenceI seqrep = null;
1731 * @return the representative sequence for this group
1734 public SequenceI getSeqrep()
1740 * set the representative sequence for this group. Note - this affects the
1741 * interpretation of the Hidereps attribute.
1744 * the seqrep to set (null means no sequence representative)
1747 public void setSeqrep(SequenceI seqrep)
1749 this.seqrep = seqrep;
1754 * @return true if group has a sequence representative
1757 public boolean hasSeqrep()
1759 return seqrep != null;
1763 public int getEndRes()
1765 return getWidth() - 1;
1769 public int getStartRes()
1775 * In the case of AlignmentI - returns the dataset for the alignment, if set
1778 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1781 public AnnotatedCollectionI getContext()
1787 * Align this alignment like the given (mapped) one.
1790 public int alignAs(AlignmentI al)
1793 * Currently retains unmapped gaps (in introns), regaps mapped regions
1796 return alignAs(al, false, true);
1800 * Align this alignment 'the same as' the given one. Mapped sequences only are
1801 * realigned. If both of the same type (nucleotide/protein) then align both
1802 * identically. If this is nucleotide and the other is protein, make 3 gaps
1803 * for each gap in the protein sequences. If this is protein and the other is
1804 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1805 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1806 * protein to match the relative ordering of codons in the nucleotide.
1808 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1809 * regions are preserved. Gaps that connect introns to exons are treated
1810 * conservatively, i.e. only preserved if both intron and exon gaps are
1811 * preserved. TODO: check caveats below where the implementation fails
1814 * - must have same dataset, and sequences in al must have equivalent
1815 * dataset sequence and start/end bounds under given mapping
1816 * @param preserveMappedGaps
1817 * if true, gaps within and between mapped codons are preserved
1818 * @param preserveUnmappedGaps
1819 * if true, gaps within and between unmapped codons are preserved
1822 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1823 boolean preserveUnmappedGaps)
1825 // TODO should this method signature be the one in the interface?
1826 // JBPComment - yes - neither flag is used, so should be deleted.
1827 boolean thisIsNucleotide = this.isNucleotide();
1828 boolean thatIsProtein = !al.isNucleotide();
1829 if (!thatIsProtein && !thisIsNucleotide)
1831 return AlignmentUtils.alignProteinAsDna(this, al);
1833 else if (thatIsProtein && thisIsNucleotide)
1835 return AlignmentUtils.alignCdsAsProtein(this, al);
1837 return AlignmentUtils.alignAs(this, al);
1841 * Returns the alignment in Fasta format. Behaviour of this method is not
1842 * guaranteed between versions.
1845 public String toString()
1847 return new FastaFile().print(getSequencesArray(), true);
1851 * Returns the set of distinct sequence names. No ordering is guaranteed.
1854 public Set<String> getSequenceNames()
1856 Set<String> names = new HashSet<>();
1857 for (SequenceI seq : getSequences())
1859 names.add(seq.getName());
1865 public boolean hasValidSequence()
1867 boolean hasValidSeq = false;
1868 for (SequenceI seq : getSequences())
1870 if ((seq.getEnd() - seq.getStart()) > 0)
1880 * Update any mappings to 'virtual' sequences to compatible real ones, if
1881 * present in the added sequences. Returns a count of mappings updated.
1887 public int realiseMappings(List<SequenceI> seqs)
1890 for (SequenceI seq : seqs)
1892 for (AlignedCodonFrame mapping : getCodonFrames())
1894 count += mapping.realiseWith(seq);
1901 * Returns the first AlignedCodonFrame that has a mapping between the given
1909 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1911 for (AlignedCodonFrame acf : getCodonFrames())
1913 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1922 public boolean setHiddenColumns(HiddenColumns cols)
1924 boolean changed = cols == null ? hiddenCols != null
1925 : !cols.equals(hiddenCols);
1931 public void setupJPredAlignment()
1933 SequenceI repseq = getSequenceAt(0);
1935 HiddenColumns cs = new HiddenColumns();
1936 cs.hideList(repseq.getInsertions());
1937 setHiddenColumns(cs);
1941 public HiddenColumns propagateInsertions(SequenceI profileseq,
1942 AlignmentView input)
1946 char gc = getGapCharacter();
1947 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1948 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1949 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1950 return propagateInsertions(profileseq, origseq, nview);
1956 * sequence in al which corresponds to origseq
1958 * alignment which is to have gaps inserted into it
1960 * sequence corresponding to profileseq which defines gap map for
1963 private HiddenColumns propagateInsertions(SequenceI profileseq,
1964 SequenceI origseq, HiddenColumns hc)
1966 // take the set of hidden columns, and the set of gaps in origseq,
1967 // and remove all the hidden gaps from hiddenColumns
1969 // first get the gaps as a Bitset
1970 // then calculate hidden ^ not(gap)
1971 BitSet gaps = origseq.gapBitset();
1974 // for each sequence in the alignment, except the profile sequence,
1975 // insert gaps corresponding to each hidden region but where each hidden
1976 // column region is shifted backwards by the number of preceding visible
1977 // gaps update hidden columns at the same time
1978 HiddenColumns newhidden = new HiddenColumns();
1980 int numGapsBefore = 0;
1981 int gapPosition = 0;
1982 Iterator<int[]> it = hc.iterator();
1983 while (it.hasNext())
1985 int[] region = it.next();
1987 // get region coordinates accounting for gaps
1988 // we can rely on gaps not being *in* hidden regions because we already
1990 while (gapPosition < region[0])
1993 if (gaps.get(gapPosition))
1999 int left = region[0] - numGapsBefore;
2000 int right = region[1] - numGapsBefore;
2002 newhidden.hideColumns(left, right);
2003 padGaps(left, right, profileseq);
2009 * Pad gaps in all sequences in alignment except profileseq
2012 * position of first gap to insert
2014 * position of last gap to insert
2016 * sequence not to pad
2018 private void padGaps(int left, int right, SequenceI profileseq)
2020 char gc = getGapCharacter();
2022 // make a string with number of gaps = length of hidden region
2023 StringBuilder sb = new StringBuilder();
2024 for (int g = 0; g < right - left + 1; g++)
2029 // loop over the sequences and pad with gaps where required
2030 for (int s = 0, ns = getHeight(); s < ns; s++)
2032 SequenceI sqobj = getSequenceAt(s);
2033 if ((sqobj != profileseq) && (sqobj.getLength() >= left))
2035 String sq = sqobj.getSequenceAsString();
2037 sq.substring(0, left) + sb.toString() + sq.substring(left));
2043 public Hashtable[] getComplementConsensusHash()
2045 return codonConsensus;
2049 public Conservation getConservation()
2051 return conservation;
2055 public Hashtable[] getRnaStructureConsensusHash()
2057 return rnaStructureConsensus;
2061 public ProfilesI getSequenceConsensusHash()
2067 public void setComplementConsensusHash(Hashtable[] hconsensus)
2069 codonConsensus = hconsensus;
2074 public void setConservation(Conservation cons)
2076 conservation = cons;
2081 public void setRnaStructureConsensusHash(Hashtable[] hStrucConsensus)
2083 rnaStructureConsensus = hStrucConsensus;
2088 public void setSequenceConsensusHash(ProfilesI hconsensus)
2090 consensus = hconsensus;