2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.analysis.AlignmentUtils;
24 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
25 import jalview.io.FastaFile;
26 import jalview.util.Comparison;
27 import jalview.util.LinkedIdentityHashSet;
28 import jalview.util.MessageManager;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.HashSet;
34 import java.util.Hashtable;
35 import java.util.List;
38 import java.util.Vector;
41 * Data structure to hold and manipulate a multiple sequence alignment
47 public class Alignment implements AlignmentI
49 private Alignment dataset;
51 protected List<SequenceI> sequences;
53 protected List<SequenceGroup> groups;
55 protected char gapCharacter = '-';
57 private boolean nucleotide = true;
59 public boolean hasRNAStructure = false;
61 public AlignmentAnnotation[] annotations;
63 HiddenSequences hiddenSequences;
65 public Hashtable alignmentProperties;
67 private List<AlignedCodonFrame> codonFrameList;
69 private void initAlignment(SequenceI[] seqs)
71 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
72 hiddenSequences = new HiddenSequences(this);
73 codonFrameList = new ArrayList<AlignedCodonFrame>();
75 nucleotide = Comparison.isNucleotide(seqs);
77 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
79 for (int i = 0; i < seqs.length; i++)
81 sequences.add(seqs[i]);
87 * Make a 'copy' alignment - sequences have new copies of features and
88 * annotations, but share the original dataset sequences.
90 public Alignment(AlignmentI al)
92 SequenceI[] seqs = al.getSequencesArray();
93 for (int i = 0; i < seqs.length; i++)
95 seqs[i] = new Sequence(seqs[i]);
101 * Share the same dataset sequence mappings (if any).
103 if (dataset == null && al.getDataset() == null)
105 this.setCodonFrames(al.getCodonFrames());
110 * Make an alignment from an array of Sequences.
114 public Alignment(SequenceI[] seqs)
120 * Make a new alignment from an array of SeqCigars
125 public Alignment(SeqCigar[] alseqs)
127 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
128 gapCharacter, new ColumnSelection(), null);
133 * Make a new alignment from an CigarArray JBPNote - can only do this when
134 * compactAlignment does not contain hidden regions. JBPNote - must also check
135 * that compactAlignment resolves to a set of SeqCigars - or construct them
138 * @param compactAlignment
141 public static AlignmentI createAlignment(CigarArray compactAlignment)
145 .getString("error.alignment_cigararray_not_implemented"));
146 // this(compactAlignment.refCigars);
150 public List<SequenceI> getSequences()
156 public List<SequenceI> getSequences(
157 Map<SequenceI, SequenceCollectionI> hiddenReps)
159 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
165 public SequenceI[] getSequencesArray()
167 if (sequences == null)
171 synchronized (sequences)
173 return sequences.toArray(new SequenceI[sequences.size()]);
178 * Returns a map of lists of sequences keyed by sequence name.
183 public Map<String, List<SequenceI>> getSequencesByName()
185 return AlignmentUtils.getSequencesByName(this);
190 public SequenceI getSequenceAt(int i)
192 synchronized (sequences)
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<SequenceGroup>();
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)
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, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
479 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
481 if (annotations[i].groupRef != null)
483 todelete[p++] = annotations[i];
487 tokeep[k++] = annotations[i];
493 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
495 if (annotations[i].groupRef == gp)
497 todelete[p++] = annotations[i];
501 tokeep[k++] = annotations[i];
507 // clear out the group associated annotation.
508 for (i = 0; i < p; i++)
510 unhookAnnotation(todelete[i]);
513 t = new AlignmentAnnotation[k];
514 for (i = 0; i < k; i++)
523 public void deleteAllGroups()
525 synchronized (groups)
527 if (annotations != null)
529 removeAnnotationForGroup(null);
531 for (SequenceGroup sg : groups)
541 public void deleteGroup(SequenceGroup g)
543 synchronized (groups)
545 if (groups.contains(g))
547 removeAnnotationForGroup(g);
556 public SequenceI findName(String name)
558 return findName(name, false);
564 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
567 public SequenceI findName(String token, boolean b)
569 return findName(null, token, b);
575 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
579 public SequenceI findName(SequenceI startAfter, String token, boolean b)
584 String sqname = null;
585 if (startAfter != null)
587 // try to find the sequence in the alignment
588 boolean matched = false;
589 while (i < sequences.size())
591 if (getSequenceAt(i++) == startAfter)
602 while (i < sequences.size())
604 sq = getSequenceAt(i);
605 sqname = sq.getName();
606 if (sqname.equals(token) // exact match
607 || (b && // allow imperfect matches - case varies
608 (sqname.equalsIgnoreCase(token))))
610 return getSequenceAt(i);
620 public SequenceI[] findSequenceMatch(String name)
622 Vector matches = new Vector();
625 while (i < sequences.size())
627 if (getSequenceAt(i).getName().equals(name))
629 matches.addElement(getSequenceAt(i));
634 SequenceI[] result = new SequenceI[matches.size()];
635 for (i = 0; i < result.length; i++)
637 result[i] = (SequenceI) matches.elementAt(i);
647 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
650 public int findIndex(SequenceI s)
654 while (i < sequences.size())
656 if (s == getSequenceAt(i))
671 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
674 public int findIndex(SearchResultsI results)
678 while (i < sequences.size())
680 if (results.involvesSequence(getSequenceAt(i)))
691 public int getHeight()
693 return sequences.size();
697 public int getAbsoluteHeight()
699 return sequences.size() + getHiddenSequences().getSize();
703 public int getWidth()
707 for (int i = 0; i < sequences.size(); i++)
709 if (getSequenceAt(i).getLength() > maxLength)
711 maxLength = getSequenceAt(i).getLength();
725 public void setGapCharacter(char gc)
728 synchronized (sequences)
730 for (SequenceI seq : sequences)
732 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
733 .replace('-', gc).replace(' ', gc));
741 * @return DOCUMENT ME!
744 public char getGapCharacter()
752 * @see jalview.datamodel.AlignmentI#isAligned()
755 public boolean isAligned()
757 return isAligned(false);
763 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
766 public boolean isAligned(boolean includeHidden)
768 int width = getWidth();
769 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
771 includeHidden = true; // no hidden sequences to check against.
773 for (int i = 0; i < sequences.size(); i++)
775 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
777 if (getSequenceAt(i).getLength() != width)
788 public boolean isHidden(int alignmentIndex)
790 return (getHiddenSequences().getHiddenSequence(alignmentIndex) != null);
794 * Delete all annotations, including auto-calculated if the flag is set true.
795 * Returns true if at least one annotation was deleted, else false.
797 * @param includingAutoCalculated
801 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
803 boolean result = false;
804 for (AlignmentAnnotation alan : getAlignmentAnnotation())
806 if (!alan.autoCalculated || includingAutoCalculated)
808 deleteAnnotation(alan);
818 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
819 * AlignmentAnnotation)
822 public boolean deleteAnnotation(AlignmentAnnotation aa)
824 return deleteAnnotation(aa, true);
828 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
832 if (annotations != null)
834 aSize = annotations.length;
842 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
844 boolean swap = false;
847 for (int i = 0; i < aSize; i++)
849 if (annotations[i] == aa)
854 if (tIndex < temp.length)
856 temp[tIndex++] = annotations[i];
865 unhookAnnotation(aa);
872 * remove any object references associated with this annotation
876 private void unhookAnnotation(AlignmentAnnotation aa)
878 if (aa.sequenceRef != null)
880 aa.sequenceRef.removeAlignmentAnnotation(aa);
882 if (aa.groupRef != null)
884 // probably need to do more here in the future (post 2.5.0)
892 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
893 * AlignmentAnnotation)
896 public void addAnnotation(AlignmentAnnotation aa)
898 addAnnotation(aa, -1);
904 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
905 * AlignmentAnnotation, int)
908 public void addAnnotation(AlignmentAnnotation aa, int pos)
910 if (aa.getRNAStruc() != null)
912 hasRNAStructure = true;
916 if (annotations != null)
918 aSize = annotations.length + 1;
921 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
923 if (pos == -1 || pos >= aSize)
925 temp[aSize - 1] = aa;
934 for (i = 0; i < (aSize - 1); i++, p++)
942 temp[p] = annotations[i];
951 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
953 if (aa == null || annotations == null || annotations.length - 1 < index)
958 int aSize = annotations.length;
959 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
963 for (int i = 0; i < aSize; i++)
972 temp[i] = annotations[i];
976 temp[i] = annotations[i - 1];
985 * returns all annotation on the alignment
987 public AlignmentAnnotation[] getAlignmentAnnotation()
993 public boolean isNucleotide()
999 public boolean hasRNAStructure()
1001 // TODO can it happen that structure is removed from alignment?
1002 return hasRNAStructure;
1006 public void setDataset(AlignmentI data)
1008 if (dataset == null && data == null)
1010 createDatasetAlignment();
1012 else if (dataset == null && data != null)
1014 if (!(data instanceof Alignment))
1017 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1019 dataset = (Alignment) data;
1020 for (int i = 0; i < getHeight(); i++)
1022 SequenceI currentSeq = getSequenceAt(i);
1023 SequenceI dsq = currentSeq.getDatasetSequence();
1026 dsq = currentSeq.createDatasetSequence();
1027 dataset.addSequence(dsq);
1031 while (dsq.getDatasetSequence() != null)
1033 dsq = dsq.getDatasetSequence();
1035 if (dataset.findIndex(dsq) == -1)
1037 dataset.addSequence(dsq);
1042 dataset.addAlignmentRef();
1046 * add dataset sequences to seq for currentSeq and any sequences it references
1048 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1049 Set<SequenceI> seqs, boolean createDatasetSequence)
1051 SequenceI alignedSeq = currentSeq;
1052 if (currentSeq.getDatasetSequence() != null)
1054 currentSeq = currentSeq.getDatasetSequence();
1058 if (createDatasetSequence)
1060 currentSeq = currentSeq.createDatasetSequence();
1063 if (seqs.contains(currentSeq))
1067 List<SequenceI> toProcess = new ArrayList<SequenceI>();
1068 toProcess.add(currentSeq);
1069 while (toProcess.size() > 0)
1072 SequenceI curDs = toProcess.remove(0);
1073 if (seqs.contains(curDs))
1078 // iterate over database references, making sure we add forward referenced
1080 if (curDs.getDBRefs() != null)
1082 for (DBRefEntry dbr : curDs.getDBRefs())
1084 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1086 if (dbr.getMap().getTo() == alignedSeq)
1089 * update mapping to be to the newly created dataset sequence
1091 dbr.getMap().setTo(currentSeq);
1093 if (dbr.getMap().getTo().getDatasetSequence() != null)
1096 "Implementation error: Map.getTo() for dbref " + dbr
1097 + " from " + curDs.getName()
1098 + " is not a dataset sequence.");
1100 // we recurse to add all forward references to dataset sequences via
1102 toProcess.add(dbr.getMap().getTo());
1110 * Creates a new dataset for this alignment. Can only be done once - if
1111 * dataset is not null this will not be performed.
1113 public void createDatasetAlignment()
1115 if (dataset != null)
1119 // try to avoid using SequenceI.equals at this stage, it will be expensive
1120 Set<SequenceI> seqs = new LinkedIdentityHashSet<SequenceI>();
1122 for (int i = 0; i < getHeight(); i++)
1124 SequenceI currentSeq = getSequenceAt(i);
1125 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1128 // verify all mappings are in dataset
1129 for (AlignedCodonFrame cf : codonFrameList)
1131 for (SequenceToSequenceMapping ssm : cf.getMappings())
1133 if (!seqs.contains(ssm.getFromSeq()))
1135 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1137 if (!seqs.contains(ssm.getMapping().getTo()))
1139 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1143 // finally construct dataset
1144 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1145 // move mappings to the dataset alignment
1146 dataset.codonFrameList = this.codonFrameList;
1147 this.codonFrameList = null;
1151 * reference count for number of alignments referencing this one.
1153 int alignmentRefs = 0;
1156 * increase reference count to this alignment.
1158 private void addAlignmentRef()
1164 public Alignment getDataset()
1170 public boolean padGaps()
1172 boolean modified = false;
1174 // Remove excess gaps from the end of alignment
1178 for (int i = 0; i < sequences.size(); i++)
1180 current = getSequenceAt(i);
1181 for (int j = current.getLength(); j > maxLength; j--)
1184 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1195 for (int i = 0; i < sequences.size(); i++)
1197 current = getSequenceAt(i);
1198 cLength = current.getLength();
1200 if (cLength < maxLength)
1202 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1205 else if (current.getLength() > maxLength)
1207 current.deleteChars(maxLength, current.getLength());
1214 * Justify the sequences to the left or right by deleting and inserting gaps
1215 * before the initial residue or after the terminal residue
1218 * true if alignment padded to right, false to justify to left
1219 * @return true if alignment was changed
1222 public boolean justify(boolean right)
1224 boolean modified = false;
1226 // Remove excess gaps from the end of alignment
1228 int ends[] = new int[sequences.size() * 2];
1230 for (int i = 0; i < sequences.size(); i++)
1232 current = getSequenceAt(i);
1233 // This should really be a sequence method
1234 ends[i * 2] = current.findIndex(current.getStart());
1235 ends[i * 2 + 1] = current.findIndex(current.getStart()
1236 + current.getLength());
1237 boolean hitres = false;
1238 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1240 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1249 ends[i * 2 + 1] = j;
1250 if (j - ends[i * 2] > maxLength)
1252 maxLength = j - ends[i * 2];
1260 // now edit the flanking gaps to justify to either left or right
1261 int cLength, extent, diff;
1262 for (int i = 0; i < sequences.size(); i++)
1264 current = getSequenceAt(i);
1266 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1267 diff = maxLength - cLength; // number of gaps to indent
1268 extent = current.getLength();
1272 if (extent > ends[i * 2 + 1])
1274 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1277 if (ends[i * 2] > diff)
1279 current.deleteChars(0, ends[i * 2] - diff);
1284 if (ends[i * 2] < diff)
1286 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1294 if (ends[i * 2] > 0)
1296 current.deleteChars(0, ends[i * 2]);
1298 ends[i * 2 + 1] -= ends[i * 2];
1299 extent -= ends[i * 2];
1301 if (extent > maxLength)
1303 current.deleteChars(maxLength + 1, extent);
1308 if (extent < maxLength)
1310 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1320 public HiddenSequences getHiddenSequences()
1322 return hiddenSequences;
1326 public CigarArray getCompactAlignment()
1328 synchronized (sequences)
1330 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1332 for (SequenceI seq : sequences)
1334 alseqs[i++] = new SeqCigar(seq);
1336 CigarArray cal = new CigarArray(alseqs);
1337 cal.addOperation(CigarArray.M, getWidth());
1343 public void setProperty(Object key, Object value)
1345 if (alignmentProperties == null)
1347 alignmentProperties = new Hashtable();
1350 alignmentProperties.put(key, value);
1354 public Object getProperty(Object key)
1356 if (alignmentProperties != null)
1358 return alignmentProperties.get(key);
1367 public Hashtable getProperties()
1369 return alignmentProperties;
1373 * Adds the given mapping to the stored set. Note this may be held on the
1374 * dataset alignment.
1377 public void addCodonFrame(AlignedCodonFrame codons)
1379 List<AlignedCodonFrame> acfs = getCodonFrames();
1380 if (codons != null && acfs != null && !acfs.contains(codons))
1390 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1393 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1399 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1400 for (AlignedCodonFrame acf : getCodonFrames())
1402 if (acf.involvesSequence(seq))
1411 * Sets the codon frame mappings (replacing any existing mappings). Note the
1412 * mappings are set on the dataset alignment instead if there is one.
1414 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1417 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1419 if (dataset != null)
1421 dataset.setCodonFrames(acfs);
1425 this.codonFrameList = acfs;
1430 * Returns the set of codon frame mappings. Any changes to the returned set
1431 * will affect the alignment. The mappings are held on (and read from) the
1432 * dataset alignment if there is one.
1434 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1437 public List<AlignedCodonFrame> getCodonFrames()
1439 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1440 // this behaviour is currently incorrect. method should return codon frames
1441 // for just the alignment,
1442 // selected from dataset
1443 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1447 * Removes the given mapping from the stored set. Note that the mappings are
1448 * held on the dataset alignment if there is one.
1451 public boolean removeCodonFrame(AlignedCodonFrame codons)
1453 List<AlignedCodonFrame> acfs = getCodonFrames();
1454 if (codons == null || acfs == null)
1458 return acfs.remove(codons);
1462 public void append(AlignmentI toappend)
1464 // TODO JAL-1270 needs test coverage
1465 // currently tested for use in jalview.gui.SequenceFetcher
1466 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1467 char oldc = toappend.getGapCharacter();
1468 boolean hashidden = toappend.getHiddenSequences() != null
1469 && toappend.getHiddenSequences().hiddenSequences != null;
1470 // get all sequences including any hidden ones
1471 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1472 .getFullAlignment().getSequences() : toappend.getSequences();
1475 // avoid self append deadlock by
1476 List<SequenceI> toappendsq = new ArrayList<SequenceI>();
1479 for (SequenceI addedsq : sqs)
1483 char[] oldseq = addedsq.getSequence();
1484 for (int c = 0; c < oldseq.length; c++)
1486 if (oldseq[c] == oldc)
1488 oldseq[c] = gapCharacter;
1492 toappendsq.add(addedsq);
1495 for (SequenceI addedsq : toappendsq)
1497 addSequence(addedsq);
1500 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1501 for (int a = 0; alan != null && a < alan.length; a++)
1503 addAnnotation(alan[a]);
1507 getCodonFrames().addAll(toappend.getCodonFrames());
1509 List<SequenceGroup> sg = toappend.getGroups();
1512 for (SequenceGroup _sg : sg)
1517 if (toappend.getHiddenSequences() != null)
1519 HiddenSequences hs = toappend.getHiddenSequences();
1520 if (hiddenSequences == null)
1522 hiddenSequences = new HiddenSequences(this);
1524 if (hs.hiddenSequences != null)
1526 for (int s = 0; s < hs.hiddenSequences.length; s++)
1528 // hide the newly appended sequence in the alignment
1529 if (hs.hiddenSequences[s] != null)
1531 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1536 if (toappend.getProperties() != null)
1538 // we really can't do very much here - just try to concatenate strings
1539 // where property collisions occur.
1540 Enumeration key = toappend.getProperties().keys();
1541 while (key.hasMoreElements())
1543 Object k = key.nextElement();
1544 Object ourval = this.getProperty(k);
1545 Object toapprop = toappend.getProperty(k);
1548 if (ourval.getClass().equals(toapprop.getClass())
1549 && !ourval.equals(toapprop))
1551 if (ourval instanceof String)
1554 this.setProperty(k, ((String) ourval) + "; "
1555 + ((String) toapprop));
1559 if (ourval instanceof Vector)
1562 Enumeration theirv = ((Vector) toapprop).elements();
1563 while (theirv.hasMoreElements())
1565 ((Vector) ourval).addElement(theirv);
1573 // just add new property directly
1574 setProperty(k, toapprop);
1582 public AlignmentAnnotation findOrCreateAnnotation(String name,
1583 String calcId, boolean autoCalc, SequenceI seqRef,
1584 SequenceGroup groupRef)
1586 if (annotations != null)
1588 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1590 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1591 && (calcId == null || annot.getCalcId().equals(calcId))
1592 && annot.sequenceRef == seqRef
1593 && annot.groupRef == groupRef)
1599 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1600 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1601 annot.hasText = false;
1602 annot.setCalcId(new String(calcId));
1603 annot.autoCalculated = autoCalc;
1606 annot.setSequenceRef(seqRef);
1608 annot.groupRef = groupRef;
1609 addAnnotation(annot);
1615 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1617 List<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1618 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1619 if (alignmentAnnotation != null)
1621 for (AlignmentAnnotation a : alignmentAnnotation)
1623 if (a.getCalcId() == calcId
1624 || (a.getCalcId() != null && calcId != null && a
1625 .getCalcId().equals(calcId)))
1635 * Returns an iterable collection of any annotations that match on given
1636 * sequence ref, calcId and label (ignoring null values).
1639 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1640 String calcId, String label)
1642 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1643 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1645 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1646 && ann.sequenceRef != null && 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<String>();
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 int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1913 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1914 int startPos = alignmentStartEnd[0];
1915 int endPos = alignmentStartEnd[1];
1917 int[] lowestRange = new int[] { -1, -1 };
1918 int[] higestRange = new int[] { -1, -1 };
1920 for (int[] hiddenCol : hiddenCols)
1922 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1923 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1926 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1928 startPos = alignmentStartEnd[0];
1932 startPos = lowestRange[1] + 1;
1935 if (higestRange[0] == -1 && higestRange[1] == -1)
1937 endPos = alignmentStartEnd[1];
1941 endPos = higestRange[0] - 1;
1943 return new int[] { startPos, endPos };