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.io.FastaFile;
25 import jalview.util.Comparison;
26 import jalview.util.MessageManager;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.Hashtable;
33 import java.util.Iterator;
34 import java.util.List;
37 import java.util.Vector;
40 * Data structure to hold and manipulate a multiple sequence alignment
46 public class Alignment implements AlignmentI
48 protected Alignment dataset;
50 protected List<SequenceI> sequences;
52 protected List<SequenceGroup> groups;
54 protected char gapCharacter = '-';
56 protected int type = NUCLEOTIDE;
58 public static final int PROTEIN = 0;
60 public static final int NUCLEOTIDE = 1;
62 public boolean hasRNAStructure = false;
64 public AlignmentAnnotation[] annotations;
66 HiddenSequences hiddenSequences;
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 codonFrameList = new ArrayList<AlignedCodonFrame>();
78 if (Comparison.isNucleotide(seqs))
87 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
89 for (int i = 0; i < seqs.length; i++)
91 sequences.add(seqs[i]);
97 * Make a 'copy' alignment - sequences have new copies of features and
98 * annotations, but share the original dataset sequences.
100 public Alignment(AlignmentI al)
102 SequenceI[] seqs = al.getSequencesArray();
103 for (int i = 0; i < seqs.length; i++)
105 seqs[i] = new Sequence(seqs[i]);
111 * Share the same dataset sequence mappings (if any).
113 this.setCodonFrames(al.getCodonFrames());
117 * Make an alignment from an array of Sequences.
121 public Alignment(SequenceI[] seqs)
127 * Make a new alignment from an array of SeqCigars
132 public Alignment(SeqCigar[] alseqs)
134 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
135 gapCharacter, new ColumnSelection(), null);
140 * Make a new alignment from an CigarArray JBPNote - can only do this when
141 * compactAlignment does not contain hidden regions. JBPNote - must also check
142 * that compactAlignment resolves to a set of SeqCigars - or construct them
145 * @param compactAlignment
148 public static AlignmentI createAlignment(CigarArray compactAlignment)
152 .getString("error.alignment_cigararray_not_implemented"));
153 // this(compactAlignment.refCigars);
157 public List<SequenceI> getSequences()
163 public List<SequenceI> getSequences(
164 Map<SequenceI, SequenceCollectionI> hiddenReps)
166 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
172 public SequenceI[] getSequencesArray()
174 if (sequences == null)
178 synchronized (sequences)
180 return sequences.toArray(new SequenceI[sequences.size()]);
185 * Returns a map of lists of sequences keyed by sequence name.
190 public Map<String, List<SequenceI>> getSequencesByName()
192 return AlignmentUtils.getSequencesByName(this);
201 * @return DOCUMENT ME!
204 public SequenceI getSequenceAt(int i)
206 synchronized (sequences)
208 if (i > -1 && i < sequences.size())
210 return sequences.get(i);
217 * Adds a sequence to the alignment. Recalculates maxLength and size.
222 public void addSequence(SequenceI snew)
226 // maintain dataset integrity
227 if (snew.getDatasetSequence() != null)
229 getDataset().addSequence(snew.getDatasetSequence());
233 // derive new sequence
234 SequenceI adding = snew.deriveSequence();
235 getDataset().addSequence(adding.getDatasetSequence());
239 if (sequences == null)
241 initAlignment(new SequenceI[] { snew });
245 synchronized (sequences)
250 if (hiddenSequences != null)
252 hiddenSequences.adjustHeightSequenceAdded();
257 * Adds a sequence to the alignment. Recalculates maxLength and size.
262 public void setSequenceAt(int i, SequenceI snew)
264 synchronized (sequences)
267 sequences.set(i, snew);
274 * @return DOCUMENT ME!
277 public List<SequenceGroup> getGroups()
283 public void finalize()
285 if (getDataset() != null)
287 getDataset().removeAlignmentRef();
294 hiddenSequences = null;
298 * decrement the alignmentRefs counter by one and call finalize if it goes to
301 private void removeAlignmentRef()
303 if (--alignmentRefs == 0)
316 public void deleteSequence(SequenceI s)
318 deleteSequence(findIndex(s));
328 public void deleteSequence(int i)
330 if (i > -1 && i < getHeight())
332 synchronized (sequences)
335 hiddenSequences.adjustHeightSequenceDeleted(i);
343 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
346 public SequenceGroup findGroup(SequenceI s)
348 synchronized (groups)
350 for (int i = 0; i < this.groups.size(); i++)
352 SequenceGroup sg = groups.get(i);
354 if (sg.getSequences(null).contains(s))
367 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
370 public SequenceGroup[] findAllGroups(SequenceI s)
372 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
374 synchronized (groups)
376 int gSize = groups.size();
377 for (int i = 0; i < gSize; i++)
379 SequenceGroup sg = groups.get(i);
380 if (sg == null || sg.getSequences() == null)
382 this.deleteGroup(sg);
387 if (sg.getSequences().contains(s))
393 SequenceGroup[] ret = new SequenceGroup[temp.size()];
394 return temp.toArray(ret);
399 public void addGroup(SequenceGroup sg)
401 synchronized (groups)
403 if (!groups.contains(sg))
405 if (hiddenSequences.getSize() > 0)
407 int i, iSize = sg.getSize();
408 for (i = 0; i < iSize; i++)
410 if (!sequences.contains(sg.getSequenceAt(i)))
412 sg.deleteSequence(sg.getSequenceAt(i), false);
418 if (sg.getSize() < 1)
430 * remove any annotation that references gp
433 * (if null, removes all group associated annotation)
435 private void removeAnnotationForGroup(SequenceGroup gp)
437 if (annotations == null || annotations.length == 0)
441 // remove annotation very quickly
442 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
446 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
448 if (annotations[i].groupRef != null)
450 todelete[p++] = annotations[i];
454 tokeep[k++] = annotations[i];
460 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
462 if (annotations[i].groupRef == gp)
464 todelete[p++] = annotations[i];
468 tokeep[k++] = annotations[i];
474 // clear out the group associated annotation.
475 for (i = 0; i < p; i++)
477 unhookAnnotation(todelete[i]);
480 t = new AlignmentAnnotation[k];
481 for (i = 0; i < k; i++)
490 public void deleteAllGroups()
492 synchronized (groups)
494 if (annotations != null)
496 removeAnnotationForGroup(null);
498 for (SequenceGroup sg : groups)
508 public void deleteGroup(SequenceGroup g)
510 synchronized (groups)
512 if (groups.contains(g))
514 removeAnnotationForGroup(g);
523 public SequenceI findName(String name)
525 return findName(name, false);
531 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
534 public SequenceI findName(String token, boolean b)
536 return findName(null, token, b);
542 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
546 public SequenceI findName(SequenceI startAfter, String token, boolean b)
551 String sqname = null;
552 if (startAfter != null)
554 // try to find the sequence in the alignment
555 boolean matched = false;
556 while (i < sequences.size())
558 if (getSequenceAt(i++) == startAfter)
569 while (i < sequences.size())
571 sq = getSequenceAt(i);
572 sqname = sq.getName();
573 if (sqname.equals(token) // exact match
574 || (b && // allow imperfect matches - case varies
575 (sqname.equalsIgnoreCase(token))))
577 return getSequenceAt(i);
587 public SequenceI[] findSequenceMatch(String name)
589 Vector matches = new Vector();
592 while (i < sequences.size())
594 if (getSequenceAt(i).getName().equals(name))
596 matches.addElement(getSequenceAt(i));
601 SequenceI[] result = new SequenceI[matches.size()];
602 for (i = 0; i < result.length; i++)
604 result[i] = (SequenceI) matches.elementAt(i);
614 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
617 public int findIndex(SequenceI s)
621 while (i < sequences.size())
623 if (s == getSequenceAt(i))
638 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
641 public int findIndex(SearchResults results)
645 while (i < sequences.size())
647 if (results.involvesSequence(getSequenceAt(i)))
659 * @return DOCUMENT ME!
662 public int getHeight()
664 return sequences.size();
670 * @return DOCUMENT ME!
673 public int getWidth()
677 for (int i = 0; i < sequences.size(); i++)
679 if (getSequenceAt(i).getLength() > maxLength)
681 maxLength = getSequenceAt(i).getLength();
695 public void setGapCharacter(char gc)
698 synchronized (sequences)
700 for (SequenceI seq : sequences)
702 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
703 .replace('-', gc).replace(' ', gc));
711 * @return DOCUMENT ME!
714 public char getGapCharacter()
722 * @see jalview.datamodel.AlignmentI#isAligned()
725 public boolean isAligned()
727 return isAligned(false);
733 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
736 public boolean isAligned(boolean includeHidden)
738 int width = getWidth();
739 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
741 includeHidden = true; // no hidden sequences to check against.
743 for (int i = 0; i < sequences.size(); i++)
745 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
747 if (getSequenceAt(i).getLength() != width)
758 * Delete all annotations, including auto-calculated if the flag is set true.
759 * Returns true if at least one annotation was deleted, else false.
761 * @param includingAutoCalculated
765 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
767 boolean result = false;
768 for (AlignmentAnnotation alan : getAlignmentAnnotation())
770 if (!alan.autoCalculated || includingAutoCalculated)
772 deleteAnnotation(alan);
782 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
783 * AlignmentAnnotation)
786 public boolean deleteAnnotation(AlignmentAnnotation aa)
788 return deleteAnnotation(aa, true);
792 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
796 if (annotations != null)
798 aSize = annotations.length;
806 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
808 boolean swap = false;
811 for (int i = 0; i < aSize; i++)
813 if (annotations[i] == aa)
818 if (tIndex < temp.length)
820 temp[tIndex++] = annotations[i];
829 unhookAnnotation(aa);
836 * remove any object references associated with this annotation
840 private void unhookAnnotation(AlignmentAnnotation aa)
842 if (aa.sequenceRef != null)
844 aa.sequenceRef.removeAlignmentAnnotation(aa);
846 if (aa.groupRef != null)
848 // probably need to do more here in the future (post 2.5.0)
856 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
857 * AlignmentAnnotation)
860 public void addAnnotation(AlignmentAnnotation aa)
862 addAnnotation(aa, -1);
868 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
869 * AlignmentAnnotation, int)
872 public void addAnnotation(AlignmentAnnotation aa, int pos)
874 if (aa.getRNAStruc() != null)
876 hasRNAStructure = true;
880 if (annotations != null)
882 aSize = annotations.length + 1;
885 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
887 if (pos == -1 || pos >= aSize)
889 temp[aSize - 1] = aa;
898 for (i = 0; i < (aSize - 1); i++, p++)
906 temp[p] = annotations[i];
915 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
917 if (aa == null || annotations == null || annotations.length - 1 < index)
922 int aSize = annotations.length;
923 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
927 for (int i = 0; i < aSize; i++)
936 temp[i] = annotations[i];
940 temp[i] = annotations[i - 1];
949 * returns all annotation on the alignment
951 public AlignmentAnnotation[] getAlignmentAnnotation()
957 public void setNucleotide(boolean b)
970 public boolean isNucleotide()
972 if (type == NUCLEOTIDE)
983 public boolean hasRNAStructure()
985 // TODO can it happen that structure is removed from alignment?
986 return hasRNAStructure;
990 public void setDataset(Alignment data)
992 if (dataset == null && data == null)
994 createDatasetAlignment();
996 else if (dataset == null && data != null)
999 for (int i = 0; i < getHeight(); i++)
1001 SequenceI currentSeq = getSequenceAt(i);
1002 SequenceI dsq = currentSeq.getDatasetSequence();
1005 dsq = currentSeq.createDatasetSequence();
1006 dataset.addSequence(dsq);
1010 while (dsq.getDatasetSequence() != null)
1012 dsq = dsq.getDatasetSequence();
1014 if (dataset.findIndex(dsq) == -1)
1016 dataset.addSequence(dsq);
1021 dataset.addAlignmentRef();
1025 * Creates a new dataset for this alignment. Can only be done once - if
1026 * dataset is not null this will not be performed.
1028 public void createDatasetAlignment()
1030 if (dataset != null)
1034 SequenceI[] seqs = new SequenceI[getHeight()];
1035 SequenceI currentSeq;
1036 for (int i = 0; i < getHeight(); i++)
1038 currentSeq = getSequenceAt(i);
1039 if (currentSeq.getDatasetSequence() != null)
1041 seqs[i] = currentSeq.getDatasetSequence();
1045 seqs[i] = currentSeq.createDatasetSequence();
1049 dataset = new Alignment(seqs);
1050 // move mappings to the dataset alignment
1051 dataset.codonFrameList = this.codonFrameList;
1052 this.codonFrameList = null;
1056 * reference count for number of alignments referencing this one.
1058 int alignmentRefs = 0;
1061 * increase reference count to this alignment.
1063 private void addAlignmentRef()
1069 public Alignment getDataset()
1075 public boolean padGaps()
1077 boolean modified = false;
1079 // Remove excess gaps from the end of alignment
1083 for (int i = 0; i < sequences.size(); i++)
1085 current = getSequenceAt(i);
1086 for (int j = current.getLength(); j > maxLength; j--)
1089 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1100 for (int i = 0; i < sequences.size(); i++)
1102 current = getSequenceAt(i);
1103 cLength = current.getLength();
1105 if (cLength < maxLength)
1107 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1110 else if (current.getLength() > maxLength)
1112 current.deleteChars(maxLength, current.getLength());
1119 * Justify the sequences to the left or right by deleting and inserting gaps
1120 * before the initial residue or after the terminal residue
1123 * true if alignment padded to right, false to justify to left
1124 * @return true if alignment was changed
1127 public boolean justify(boolean right)
1129 boolean modified = false;
1131 // Remove excess gaps from the end of alignment
1133 int ends[] = new int[sequences.size() * 2];
1135 for (int i = 0; i < sequences.size(); i++)
1137 current = getSequenceAt(i);
1138 // This should really be a sequence method
1139 ends[i * 2] = current.findIndex(current.getStart());
1140 ends[i * 2 + 1] = current.findIndex(current.getStart()
1141 + current.getLength());
1142 boolean hitres = false;
1143 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1145 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1154 ends[i * 2 + 1] = j;
1155 if (j - ends[i * 2] > maxLength)
1157 maxLength = j - ends[i * 2];
1165 // now edit the flanking gaps to justify to either left or right
1166 int cLength, extent, diff;
1167 for (int i = 0; i < sequences.size(); i++)
1169 current = getSequenceAt(i);
1171 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1172 diff = maxLength - cLength; // number of gaps to indent
1173 extent = current.getLength();
1177 if (extent > ends[i * 2 + 1])
1179 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1182 if (ends[i * 2] > diff)
1184 current.deleteChars(0, ends[i * 2] - diff);
1189 if (ends[i * 2] < diff)
1191 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1199 if (ends[i * 2] > 0)
1201 current.deleteChars(0, ends[i * 2]);
1203 ends[i * 2 + 1] -= ends[i * 2];
1204 extent -= ends[i * 2];
1206 if (extent > maxLength)
1208 current.deleteChars(maxLength + 1, extent);
1213 if (extent < maxLength)
1215 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1225 public HiddenSequences getHiddenSequences()
1227 return hiddenSequences;
1231 public CigarArray getCompactAlignment()
1233 synchronized (sequences)
1235 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1237 for (SequenceI seq : sequences)
1239 alseqs[i++] = new SeqCigar(seq);
1241 CigarArray cal = new CigarArray(alseqs);
1242 cal.addOperation(CigarArray.M, getWidth());
1248 public void setProperty(Object key, Object value)
1250 if (alignmentProperties == null)
1252 alignmentProperties = new Hashtable();
1255 alignmentProperties.put(key, value);
1259 public Object getProperty(Object key)
1261 if (alignmentProperties != null)
1263 return alignmentProperties.get(key);
1272 public Hashtable getProperties()
1274 return alignmentProperties;
1278 * Adds the given mapping to the stored set. Note this may be held on the
1279 * dataset alignment.
1282 public void addCodonFrame(AlignedCodonFrame codons)
1284 List<AlignedCodonFrame> acfs = getCodonFrames();
1285 if (codons != null && acfs != null && !acfs.contains(codons))
1292 * adds a set of mappings (while ignoring any duplicates)
1295 public void addCodonFrames(Iterable<AlignedCodonFrame> codons)
1299 Iterator<AlignedCodonFrame> it = codons.iterator();
1300 while (it.hasNext())
1302 addCodonFrame(it.next());
1311 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1314 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1320 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1321 for (AlignedCodonFrame acf : getCodonFrames())
1323 if (acf.involvesSequence(seq))
1332 * Sets the codon frame mappings (replacing any existing mappings). Note the
1333 * mappings are set on the dataset alignment instead if there is one.
1335 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1338 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1340 if (dataset != null)
1342 dataset.setCodonFrames(acfs);
1346 this.codonFrameList = acfs;
1351 * Returns the set of codon frame mappings. Any changes to the returned set
1352 * will affect the alignment. The mappings are held on (and read from) the
1353 * dataset alignment if there is one.
1355 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1358 public List<AlignedCodonFrame> getCodonFrames()
1360 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1364 * Removes the given mapping from the stored set. Note that the mappings are
1365 * held on the dataset alignment if there is one.
1368 public boolean removeCodonFrame(AlignedCodonFrame codons)
1370 List<AlignedCodonFrame> acfs = getCodonFrames();
1371 if (codons == null || acfs == null)
1375 return acfs.remove(codons);
1379 public void append(AlignmentI toappend)
1381 if (toappend == this)
1383 System.err.println("Self append may cause a deadlock.");
1385 // TODO test this method for a future 2.5 release
1386 // currently tested for use in jalview.gui.SequenceFetcher
1387 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1388 char oldc = toappend.getGapCharacter();
1389 boolean hashidden = toappend.getHiddenSequences() != null
1390 && toappend.getHiddenSequences().hiddenSequences != null;
1391 // get all sequences including any hidden ones
1392 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1393 .getFullAlignment().getSequences() : toappend.getSequences();
1398 for (SequenceI addedsq : sqs)
1402 char[] oldseq = addedsq.getSequence();
1403 for (int c = 0; c < oldseq.length; c++)
1405 if (oldseq[c] == oldc)
1407 oldseq[c] = gapCharacter;
1411 addSequence(addedsq);
1415 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1416 for (int a = 0; alan != null && a < alan.length; a++)
1418 addAnnotation(alan[a]);
1421 getCodonFrames().addAll(toappend.getCodonFrames());
1423 List<SequenceGroup> sg = toappend.getGroups();
1426 for (SequenceGroup _sg : sg)
1431 if (toappend.getHiddenSequences() != null)
1433 HiddenSequences hs = toappend.getHiddenSequences();
1434 if (hiddenSequences == null)
1436 hiddenSequences = new HiddenSequences(this);
1438 if (hs.hiddenSequences != null)
1440 for (int s = 0; s < hs.hiddenSequences.length; s++)
1442 // hide the newly appended sequence in the alignment
1443 if (hs.hiddenSequences[s] != null)
1445 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1450 if (toappend.getProperties() != null)
1452 // we really can't do very much here - just try to concatenate strings
1453 // where property collisions occur.
1454 Enumeration key = toappend.getProperties().keys();
1455 while (key.hasMoreElements())
1457 Object k = key.nextElement();
1458 Object ourval = this.getProperty(k);
1459 Object toapprop = toappend.getProperty(k);
1462 if (ourval.getClass().equals(toapprop.getClass())
1463 && !ourval.equals(toapprop))
1465 if (ourval instanceof String)
1468 this.setProperty(k, ((String) ourval) + "; "
1469 + ((String) toapprop));
1473 if (ourval instanceof Vector)
1476 Enumeration theirv = ((Vector) toapprop).elements();
1477 while (theirv.hasMoreElements())
1479 ((Vector) ourval).addElement(theirv);
1487 // just add new property directly
1488 setProperty(k, toapprop);
1496 public AlignmentAnnotation findOrCreateAnnotation(String name,
1497 String calcId, boolean autoCalc, SequenceI seqRef,
1498 SequenceGroup groupRef)
1500 assert (name != null);
1501 if (annotations != null)
1503 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1505 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1506 && (calcId == null || annot.getCalcId().equals(calcId))
1507 && annot.sequenceRef == seqRef
1508 && annot.groupRef == groupRef)
1514 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1515 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1516 annot.hasText = false;
1517 annot.setCalcId(new String(calcId));
1518 annot.autoCalculated = autoCalc;
1521 annot.setSequenceRef(seqRef);
1523 annot.groupRef = groupRef;
1524 addAnnotation(annot);
1530 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1532 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1533 for (AlignmentAnnotation a : getAlignmentAnnotation())
1535 if (a.getCalcId() == calcId
1536 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1546 * Returns an iterable collection of any annotations that match on given
1547 * sequence ref, calcId and label (ignoring null values).
1550 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1551 String calcId, String label)
1553 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1554 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1556 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1557 && ann.sequenceRef != null && ann.sequenceRef == seq
1558 && ann.label != null && ann.label.equals(label))
1567 public void moveSelectedSequencesByOne(SequenceGroup sg,
1568 Map<SequenceI, SequenceCollectionI> map, boolean up)
1570 synchronized (sequences)
1575 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1577 SequenceI seq = sequences.get(i);
1578 if (!sg.getSequences(map).contains(seq))
1583 SequenceI temp = sequences.get(i - 1);
1584 if (sg.getSequences(null).contains(temp))
1589 sequences.set(i, temp);
1590 sequences.set(i - 1, seq);
1595 for (int i = sequences.size() - 2; i > -1; i--)
1597 SequenceI seq = sequences.get(i);
1598 if (!sg.getSequences(map).contains(seq))
1603 SequenceI temp = sequences.get(i + 1);
1604 if (sg.getSequences(map).contains(temp))
1609 sequences.set(i, temp);
1610 sequences.set(i + 1, seq);
1618 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1620 alignmentAnnotation.validateRangeAndDisplay();
1621 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1623 hasRNAStructure = true;
1627 private SequenceI seqrep = null;
1631 * @return the representative sequence for this group
1634 public SequenceI getSeqrep()
1640 * set the representative sequence for this group. Note - this affects the
1641 * interpretation of the Hidereps attribute.
1644 * the seqrep to set (null means no sequence representative)
1647 public void setSeqrep(SequenceI seqrep)
1649 this.seqrep = seqrep;
1654 * @return true if group has a sequence representative
1657 public boolean hasSeqrep()
1659 return seqrep != null;
1663 public int getEndRes()
1665 return getWidth() - 1;
1669 public int getStartRes()
1675 * In the case of AlignmentI - returns the dataset for the alignment, if set
1678 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1681 public AnnotatedCollectionI getContext()
1687 * Align this alignment like the given (mapped) one.
1690 public int alignAs(AlignmentI al)
1693 * Currently retains unmapped gaps (in introns), regaps mapped regions
1696 return alignAs(al, false, true);
1700 * Align this alignment 'the same as' the given one. Mapped sequences only are
1701 * realigned. If both of the same type (nucleotide/protein) then align both
1702 * identically. If this is nucleotide and the other is protein, make 3 gaps
1703 * for each gap in the protein sequences. If this is protein and the other is
1704 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1705 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1706 * protein to match the relative ordering of codons in the nucleotide.
1708 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1709 * regions are preserved. Gaps that connect introns to exons are treated
1710 * conservatively, i.e. only preserved if both intron and exon gaps are
1714 * @param preserveMappedGaps
1715 * if true, gaps within and between mapped codons are preserved
1716 * @param preserveUnmappedGaps
1717 * if true, gaps within and between unmapped codons are preserved
1720 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1721 boolean preserveUnmappedGaps)
1723 // TODO should this method signature be the one in the interface?
1724 boolean thisIsNucleotide = this.isNucleotide();
1725 boolean thatIsProtein = !al.isNucleotide();
1726 if (!thatIsProtein && !thisIsNucleotide)
1728 return AlignmentUtils.alignProteinAsDna(this, al);
1730 return AlignmentUtils.alignAs(this, al);
1734 * Returns the alignment in Fasta format. Behaviour of this method is not
1735 * guaranteed between versions.
1738 public String toString()
1740 return new FastaFile().print(getSequencesArray());
1744 * Returns the set of distinct sequence names. No ordering is guaranteed.
1747 public Set<String> getSequenceNames()
1749 Set<String> names = new HashSet<String>();
1750 for (SequenceI seq : getSequences())
1752 names.add(seq.getName());
1758 public boolean hasValidSequence()
1760 boolean hasValidSeq = false;
1761 for (SequenceI seq : getSequences())
1763 if ((seq.getEnd() - seq.getStart()) > 0)
1773 * Update any mappings to 'virtual' sequences to compatible real ones, if
1774 * present in the added sequences. Returns a count of mappings updated.
1780 public int realiseMappings(List<SequenceI> seqs)
1783 for (SequenceI seq : seqs)
1785 for (AlignedCodonFrame mapping : getCodonFrames())
1787 count += mapping.realiseWith(seq);
1794 * Returns the first AlignedCodonFrame that has a mapping between the given
1802 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1804 for (AlignedCodonFrame acf : getCodonFrames())
1806 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1815 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1817 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1818 int startPos = alignmentStartEnd[0];
1819 int endPos = alignmentStartEnd[1];
1821 int[] lowestRange = new int[] { -1, -1 };
1822 int[] higestRange = new int[] { -1, -1 };
1824 for (int[] hiddenCol : hiddenCols)
1826 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1827 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1830 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1832 startPos = alignmentStartEnd[0];
1836 startPos = lowestRange[1] + 1;
1839 if (higestRange[0] == -1 && higestRange[1] == -1)
1841 endPos = alignmentStartEnd[1];
1845 endPos = higestRange[0] - 1;
1847 return new int[] { startPos, endPos };