2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.MessageManager;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.List;
34 import java.util.Vector;
37 * Data structure to hold and manipulate a multiple sequence alignment
43 public class Alignment implements AlignmentI
45 protected Alignment dataset;
47 protected List<SequenceI> sequences;
49 protected List<SequenceGroup> groups = java.util.Collections
50 .synchronizedList(new ArrayList<SequenceGroup>());
52 protected char gapCharacter = '-';
54 protected int type = NUCLEOTIDE;
56 public static final int PROTEIN = 0;
58 public static final int NUCLEOTIDE = 1;
60 public boolean hasRNAStructure = false;
63 public AlignmentAnnotation[] annotations;
65 HiddenSequences hiddenSequences = new HiddenSequences(this);
67 public Hashtable alignmentProperties;
69 private void initAlignment(SequenceI[] seqs)
73 if (jalview.util.Comparison.isNucleotide(seqs))
82 sequences = java.util.Collections
83 .synchronizedList(new ArrayList<SequenceI>());
85 for (i = 0; i < seqs.length; i++)
87 sequences.add(seqs[i]);
93 * Make a 'copy' alignment - sequences have new copies of features and
94 * annotations, but share the original dataset sequences.
96 public Alignment(AlignmentI al)
98 SequenceI[] seqs = al.getSequencesArray();
99 for (int i = 0; i < seqs.length; i++)
101 seqs[i] = new Sequence(seqs[i]);
107 * Make an alignment from an array of Sequences.
111 public Alignment(SequenceI[] seqs)
117 * Make a new alignment from an array of SeqCigars
122 public Alignment(SeqCigar[] alseqs)
124 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
125 gapCharacter, new ColumnSelection(), null);
130 * Make a new alignment from an CigarArray JBPNote - can only do this when
131 * compactAlignment does not contain hidden regions. JBPNote - must also check
132 * that compactAlignment resolves to a set of SeqCigars - or construct them
135 * @param compactAlignment
138 public static AlignmentI createAlignment(CigarArray compactAlignment)
140 throw new Error(MessageManager.getString("error.alignment_cigararray_not_implemented"));
141 // this(compactAlignment.refCigars);
147 * @return DOCUMENT ME!
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);
194 * @return DOCUMENT ME!
197 public SequenceI getSequenceAt(int i)
199 synchronized (sequences)
201 if (i > -1 && i < sequences.size())
203 return sequences.get(i);
210 * Adds a sequence to the alignment. Recalculates maxLength and size.
215 public void addSequence(SequenceI snew)
219 // maintain dataset integrity
220 if (snew.getDatasetSequence() != null)
222 getDataset().addSequence(snew.getDatasetSequence());
226 // derive new sequence
227 SequenceI adding = snew.deriveSequence();
228 getDataset().addSequence(adding.getDatasetSequence());
232 if (sequences == null)
234 initAlignment(new SequenceI[]
239 synchronized (sequences)
244 if (hiddenSequences != null)
246 hiddenSequences.adjustHeightSequenceAdded();
251 * Adds a sequence to the alignment. Recalculates maxLength and size.
256 public void setSequenceAt(int i, SequenceI snew)
258 SequenceI oldseq = getSequenceAt(i);
260 synchronized (sequences)
262 sequences.set(i, snew);
269 * @return DOCUMENT ME!
272 public List<SequenceGroup> getGroups()
278 public void finalize()
280 if (getDataset() != null)
282 getDataset().removeAlignmentRef();
289 hiddenSequences = null;
293 * decrement the alignmentRefs counter by one and call finalize if it goes to
296 private void removeAlignmentRef()
298 if (--alignmentRefs == 0)
311 public void deleteSequence(SequenceI s)
313 deleteSequence(findIndex(s));
323 public void deleteSequence(int i)
325 if (i > -1 && i < getHeight())
327 synchronized (sequences)
331 hiddenSequences.adjustHeightSequenceDeleted(i);
338 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
341 public SequenceGroup findGroup(SequenceI s)
343 synchronized (groups)
345 for (int i = 0; i < this.groups.size(); i++)
347 SequenceGroup sg = groups.get(i);
349 if (sg.getSequences(null).contains(s))
362 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
365 public SequenceGroup[] findAllGroups(SequenceI s)
367 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
369 synchronized (groups)
371 int gSize = groups.size();
372 for (int i = 0; i < gSize; i++)
374 SequenceGroup sg = groups.get(i);
375 if (sg == null || sg.getSequences(null) == null)
377 this.deleteGroup(sg);
382 if (sg.getSequences(null).contains(s))
388 SequenceGroup[] ret = new SequenceGroup[temp.size()];
389 return temp.toArray(ret);
394 public void addGroup(SequenceGroup sg)
396 synchronized (groups)
398 if (!groups.contains(sg))
400 if (hiddenSequences.getSize() > 0)
402 int i, iSize = sg.getSize();
403 for (i = 0; i < iSize; i++)
405 if (!sequences.contains(sg.getSequenceAt(i)))
407 sg.deleteSequence(sg.getSequenceAt(i), false);
413 if (sg.getSize() < 1)
425 * remove any annotation that references gp
428 * (if null, removes all group associated annotation)
430 private void removeAnnotationForGroup(SequenceGroup gp)
432 if (annotations == null || annotations.length == 0)
436 // remove annotation very quickly
437 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
441 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
443 if (annotations[i].groupRef != null)
445 todelete[p++] = annotations[i];
449 tokeep[k++] = annotations[i];
455 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
457 if (annotations[i].groupRef == gp)
459 todelete[p++] = annotations[i];
463 tokeep[k++] = annotations[i];
469 // clear out the group associated annotation.
470 for (i = 0; i < p; i++)
472 unhookAnnotation(todelete[i]);
475 t = new AlignmentAnnotation[k];
476 for (i = 0; i < k; i++)
485 public void deleteAllGroups()
487 synchronized (groups)
489 if (annotations != null)
491 removeAnnotationForGroup(null);
493 for (SequenceGroup sg : groups)
503 public void deleteGroup(SequenceGroup g)
505 synchronized (groups)
507 if (groups.contains(g))
509 removeAnnotationForGroup(g);
518 public SequenceI findName(String name)
520 return findName(name, false);
526 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
529 public SequenceI findName(String token, boolean b)
531 return findName(null, token, b);
537 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
541 public SequenceI findName(SequenceI startAfter, String token, boolean b)
546 String sqname = null;
547 if (startAfter != null)
549 // try to find the sequence in the alignment
550 boolean matched = false;
551 while (i < sequences.size())
553 if (getSequenceAt(i++) == startAfter)
564 while (i < sequences.size())
566 sq = getSequenceAt(i);
567 sqname = sq.getName();
568 if (sqname.equals(token) // exact match
569 || (b && // allow imperfect matches - case varies
570 (sqname.equalsIgnoreCase(token))))
572 return getSequenceAt(i);
582 public SequenceI[] findSequenceMatch(String name)
584 Vector matches = new Vector();
587 while (i < sequences.size())
589 if (getSequenceAt(i).getName().equals(name))
591 matches.addElement(getSequenceAt(i));
596 SequenceI[] result = new SequenceI[matches.size()];
597 for (i = 0; i < result.length; i++)
599 result[i] = (SequenceI) matches.elementAt(i);
609 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
612 public int findIndex(SequenceI s)
616 while (i < sequences.size())
618 if (s == getSequenceAt(i))
633 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
636 public int findIndex(SearchResults results)
640 while (i < sequences.size())
642 if (results.involvesSequence(getSequenceAt(i)))
654 * @return DOCUMENT ME!
657 public int getHeight()
659 return sequences.size();
665 * @return DOCUMENT ME!
668 public int getWidth()
672 for (int i = 0; i < sequences.size(); i++)
674 if (getSequenceAt(i).getLength() > maxLength)
676 maxLength = getSequenceAt(i).getLength();
690 public void setGapCharacter(char gc)
693 synchronized (sequences)
695 for (SequenceI seq : sequences)
697 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
698 .replace('-', gc).replace(' ', gc));
706 * @return DOCUMENT ME!
709 public char getGapCharacter()
717 * @see jalview.datamodel.AlignmentI#isAligned()
720 public boolean isAligned()
722 return isAligned(false);
728 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
731 public boolean isAligned(boolean includeHidden)
733 int width = getWidth();
734 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
736 includeHidden = true; // no hidden sequences to check against.
738 for (int i = 0; i < sequences.size(); i++)
740 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
742 if (getSequenceAt(i).getLength() != width)
755 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
756 * AlignmentAnnotation)
759 public boolean deleteAnnotation(AlignmentAnnotation aa)
761 return deleteAnnotation(aa, true);
765 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
769 if (annotations != null)
771 aSize = annotations.length;
779 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
781 boolean swap = false;
784 for (int i = 0; i < aSize; i++)
786 if (annotations[i] == aa)
791 if (tIndex < temp.length)
793 temp[tIndex++] = annotations[i];
802 unhookAnnotation(aa);
809 * remove any object references associated with this annotation
813 private void unhookAnnotation(AlignmentAnnotation aa)
815 if (aa.sequenceRef != null)
817 aa.sequenceRef.removeAlignmentAnnotation(aa);
819 if (aa.groupRef != null)
821 // probably need to do more here in the future (post 2.5.0)
829 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
830 * AlignmentAnnotation)
833 public void addAnnotation(AlignmentAnnotation aa)
835 addAnnotation(aa, -1);
841 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
842 * AlignmentAnnotation, int)
845 public void addAnnotation(AlignmentAnnotation aa, int pos)
847 if (aa.getRNAStruc() != null)
849 hasRNAStructure = true;
853 if (annotations != null)
855 aSize = annotations.length + 1;
858 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
860 if (pos == -1 || pos >= aSize)
862 temp[aSize - 1] = aa;
871 for (i = 0; i < (aSize - 1); i++, p++)
879 temp[p] = annotations[i];
888 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
890 if (aa == null || annotations == null || annotations.length - 1 < index)
895 int aSize = annotations.length;
896 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
900 for (int i = 0; i < aSize; i++)
909 temp[i] = annotations[i];
913 temp[i] = annotations[i - 1];
922 * returns all annotation on the alignment
924 public AlignmentAnnotation[] getAlignmentAnnotation()
930 public void setNucleotide(boolean b)
943 public boolean isNucleotide()
945 if (type == NUCLEOTIDE)
956 public boolean hasRNAStructure()
958 // TODO can it happen that structure is removed from alignment?
959 return hasRNAStructure;
963 public void setDataset(Alignment data)
965 if (dataset == null && data == null)
967 // Create a new dataset for this alignment.
968 // Can only be done once, if dataset is not null
969 // This will not be performed
970 SequenceI[] seqs = new SequenceI[getHeight()];
971 SequenceI currentSeq;
972 for (int i = 0; i < getHeight(); i++)
974 currentSeq = getSequenceAt(i);
975 if (currentSeq.getDatasetSequence() != null)
977 seqs[i] = currentSeq.getDatasetSequence();
981 seqs[i] = currentSeq.createDatasetSequence();
985 dataset = new Alignment(seqs);
987 else if (dataset == null && data != null)
990 for (int i = 0; i < getHeight(); i++)
992 SequenceI currentSeq = getSequenceAt(i);
993 SequenceI dsq = currentSeq.getDatasetSequence();
996 dsq = currentSeq.createDatasetSequence();
997 dataset.addSequence(dsq);
1001 while (dsq.getDatasetSequence() != null)
1003 dsq = dsq.getDatasetSequence();
1005 if (dataset.findIndex(dsq) == -1)
1007 dataset.addSequence(dsq);
1012 dataset.addAlignmentRef();
1016 * reference count for number of alignments referencing this one.
1018 int alignmentRefs = 0;
1021 * increase reference count to this alignment.
1023 private void addAlignmentRef()
1029 public Alignment getDataset()
1035 public boolean padGaps()
1037 boolean modified = false;
1039 // Remove excess gaps from the end of alignment
1043 for (int i = 0; i < sequences.size(); i++)
1045 current = getSequenceAt(i);
1046 for (int j = current.getLength(); j > maxLength; j--)
1049 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1060 for (int i = 0; i < sequences.size(); i++)
1062 current = getSequenceAt(i);
1063 cLength = current.getLength();
1065 if (cLength < maxLength)
1067 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1070 else if (current.getLength() > maxLength)
1072 current.deleteChars(maxLength, current.getLength());
1079 * Justify the sequences to the left or right by deleting and inserting gaps
1080 * before the initial residue or after the terminal residue
1083 * true if alignment padded to right, false to justify to left
1084 * @return true if alignment was changed
1087 public boolean justify(boolean right)
1089 boolean modified = false;
1091 // Remove excess gaps from the end of alignment
1093 int ends[] = new int[sequences.size() * 2];
1095 for (int i = 0; i < sequences.size(); i++)
1097 current = getSequenceAt(i);
1098 // This should really be a sequence method
1099 ends[i * 2] = current.findIndex(current.getStart());
1100 ends[i * 2 + 1] = current.findIndex(current.getStart()
1101 + current.getLength());
1102 boolean hitres = false;
1103 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1105 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1114 ends[i * 2 + 1] = j;
1115 if (j - ends[i * 2] > maxLength)
1117 maxLength = j - ends[i * 2];
1125 // now edit the flanking gaps to justify to either left or right
1126 int cLength, extent, diff;
1127 for (int i = 0; i < sequences.size(); i++)
1129 current = getSequenceAt(i);
1131 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1132 diff = maxLength - cLength; // number of gaps to indent
1133 extent = current.getLength();
1137 if (extent > ends[i * 2 + 1])
1139 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1142 if (ends[i * 2] > diff)
1144 current.deleteChars(0, ends[i * 2] - diff);
1149 if (ends[i * 2] < diff)
1151 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1159 if (ends[i * 2] > 0)
1161 current.deleteChars(0, ends[i * 2]);
1163 ends[i * 2 + 1] -= ends[i * 2];
1164 extent -= ends[i * 2];
1166 if (extent > maxLength)
1168 current.deleteChars(maxLength + 1, extent);
1173 if (extent < maxLength)
1175 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1185 public HiddenSequences getHiddenSequences()
1187 return hiddenSequences;
1191 public CigarArray getCompactAlignment()
1193 synchronized (sequences)
1195 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1197 for (SequenceI seq : sequences)
1199 alseqs[i++] = new SeqCigar(seq);
1201 CigarArray cal = new CigarArray(alseqs);
1202 cal.addOperation(CigarArray.M, getWidth());
1208 public void setProperty(Object key, Object value)
1210 if (alignmentProperties == null)
1212 alignmentProperties = new Hashtable();
1215 alignmentProperties.put(key, value);
1219 public Object getProperty(Object key)
1221 if (alignmentProperties != null)
1223 return alignmentProperties.get(key);
1232 public Hashtable getProperties()
1234 return alignmentProperties;
1237 AlignedCodonFrame[] codonFrameList = null;
1243 * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1247 public void addCodonFrame(AlignedCodonFrame codons)
1253 if (codonFrameList == null)
1255 codonFrameList = new AlignedCodonFrame[]
1259 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
1260 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
1261 t[codonFrameList.length] = codons;
1268 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
1271 public AlignedCodonFrame getCodonFrame(int index)
1273 return codonFrameList[index];
1280 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1283 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
1285 if (seq == null || codonFrameList == null)
1289 Vector cframes = new Vector();
1290 for (int f = 0; f < codonFrameList.length; f++)
1292 if (codonFrameList[f].involvesSequence(seq))
1294 cframes.addElement(codonFrameList[f]);
1297 if (cframes.size() == 0)
1301 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
1302 cframes.copyInto(cfr);
1309 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1312 public AlignedCodonFrame[] getCodonFrames()
1314 return codonFrameList;
1320 * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
1321 * AlignedCodonFrame)
1324 public boolean removeCodonFrame(AlignedCodonFrame codons)
1326 if (codons == null || codonFrameList == null)
1330 boolean removed = false;
1331 int i = 0, iSize = codonFrameList.length;
1334 if (codonFrameList[i] == codons)
1339 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize
1353 public void append(AlignmentI toappend)
1355 if (toappend == this)
1357 System.err.println("Self append may cause a deadlock.");
1359 // TODO test this method for a future 2.5 release
1360 // currently tested for use in jalview.gui.SequenceFetcher
1361 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1362 char oldc = toappend.getGapCharacter();
1363 boolean hashidden = toappend.getHiddenSequences() != null
1364 && toappend.getHiddenSequences().hiddenSequences != null;
1365 // get all sequences including any hidden ones
1366 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1367 .getFullAlignment().getSequences() : toappend.getSequences();
1372 for (SequenceI addedsq : sqs)
1376 char[] oldseq = addedsq.getSequence();
1377 for (int c = 0; c < oldseq.length; c++)
1379 if (oldseq[c] == oldc)
1381 oldseq[c] = gapCharacter;
1385 addSequence(addedsq);
1389 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1390 for (int a = 0; alan != null && a < alan.length; a++)
1392 addAnnotation(alan[a]);
1394 AlignedCodonFrame[] acod = toappend.getCodonFrames();
1395 for (int a = 0; acod != null && a < acod.length; a++)
1397 this.addCodonFrame(acod[a]);
1399 List<SequenceGroup> sg = toappend.getGroups();
1402 for (SequenceGroup _sg : sg)
1407 if (toappend.getHiddenSequences() != null)
1409 HiddenSequences hs = toappend.getHiddenSequences();
1410 if (hiddenSequences == null)
1412 hiddenSequences = new HiddenSequences(this);
1414 if (hs.hiddenSequences != null)
1416 for (int s = 0; s < hs.hiddenSequences.length; s++)
1418 // hide the newly appended sequence in the alignment
1419 if (hs.hiddenSequences[s] != null)
1421 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1426 if (toappend.getProperties() != null)
1428 // we really can't do very much here - just try to concatenate strings
1429 // where property collisions occur.
1430 Enumeration key = toappend.getProperties().keys();
1431 while (key.hasMoreElements())
1433 Object k = key.nextElement();
1434 Object ourval = this.getProperty(k);
1435 Object toapprop = toappend.getProperty(k);
1438 if (ourval.getClass().equals(toapprop.getClass())
1439 && !ourval.equals(toapprop))
1441 if (ourval instanceof String)
1444 this.setProperty(k, ((String) ourval) + "; "
1445 + ((String) toapprop));
1449 if (ourval instanceof Vector)
1452 Enumeration theirv = ((Vector) toapprop).elements();
1453 while (theirv.hasMoreElements())
1455 ((Vector) ourval).addElement(theirv);
1463 // just add new property directly
1464 setProperty(k, toapprop);
1472 public AlignmentAnnotation findOrCreateAnnotation(String name,
1473 String calcId, boolean autoCalc, SequenceI seqRef,
1474 SequenceGroup groupRef)
1476 assert (name != null);
1477 if (annotations != null)
1479 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1481 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1482 && (calcId == null || annot.getCalcId().equals(calcId))
1483 && annot.sequenceRef == seqRef
1484 && annot.groupRef == groupRef)
1490 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1491 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1492 annot.hasText = false;
1493 annot.setCalcId(new String(calcId));
1494 annot.autoCalculated = autoCalc;
1497 annot.setSequenceRef(seqRef);
1499 annot.groupRef = groupRef;
1500 addAnnotation(annot);
1506 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1508 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1509 for (AlignmentAnnotation a : getAlignmentAnnotation())
1511 if (a.getCalcId() == calcId
1512 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1522 * Returns an iterable collection of any annotations that match on given
1523 * sequence ref, calcId and label (ignoring null values).
1526 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1527 String calcId, String label)
1529 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1530 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1532 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1533 && ann.sequenceRef != null && ann.sequenceRef == seq
1534 && ann.label != null && ann.label.equals(label))
1543 public void moveSelectedSequencesByOne(SequenceGroup sg,
1544 Map<SequenceI, SequenceCollectionI> map, boolean up)
1546 synchronized (sequences)
1551 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1553 SequenceI seq = sequences.get(i);
1554 if (!sg.getSequences(map).contains(seq))
1559 SequenceI temp = sequences.get(i - 1);
1560 if (sg.getSequences(null).contains(temp))
1565 sequences.set(i, temp);
1566 sequences.set(i - 1, seq);
1571 for (int i = sequences.size() - 2; i > -1; i--)
1573 SequenceI seq = sequences.get(i);
1574 if (!sg.getSequences(map).contains(seq))
1579 SequenceI temp = sequences.get(i + 1);
1580 if (sg.getSequences(map).contains(temp))
1585 sequences.set(i, temp);
1586 sequences.set(i + 1, seq);
1594 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1596 alignmentAnnotation.validateRangeAndDisplay();
1597 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1599 hasRNAStructure = true;
1604 public int getEndRes()
1606 return getWidth() - 1;
1610 public int getStartRes()
1616 * In the case of AlignmentI - returns the dataset for the alignment, if set
1619 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1622 public AnnotatedCollectionI getContext()
1628 * Align this alignment like the given (mapped) one.
1631 public int alignAs(AlignmentI al)
1634 * Currently retains unmapped gaps (in introns), regaps mapped regions
1637 return alignAs(al, false, true);
1641 * Align this alignment 'the same as' the given one. Mapped sequences only are
1642 * realigned. If both of the same type (nucleotide/protein) then align both
1643 * identically. If this is nucleotide and the other is protein, make 3 gaps
1644 * for each gap in the protein sequences. If this is protein and the other is
1645 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1646 * nucleotide bases. Does nothing if alignment of protein from cDNA is
1647 * requested (not yet implemented).
1649 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1650 * regions are preserved. Gaps that connect introns to exons are treated
1651 * conservatively, i.e. only preserved if both intron and exon gaps are
1655 * @param preserveMappedGaps
1656 * if true, gaps within and between mapped codons are preserved
1657 * @param preserveUnmappedGaps
1658 * if true, gaps within and between unmapped codons are preserved
1661 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1662 boolean preserveUnmappedGaps)
1664 // TODO should this method signature be the one in the interface?
1666 boolean thisIsNucleotide = this.isNucleotide();
1667 boolean thatIsProtein = !al.isNucleotide();
1668 if (!thatIsProtein && !thisIsNucleotide)
1671 .println("Alignment of protein from cDNA not yet implemented");
1673 // todo: build it - a variant of Dna.CdnaTranslate()
1676 char thisGapChar = this.getGapCharacter();
1677 String gap = thisIsNucleotide && thatIsProtein ? String
1679 { thisGapChar, thisGapChar, thisGapChar }) : String
1680 .valueOf(thisGapChar);
1683 * Get mappings from 'that' alignment's sequences to this.
1685 for (SequenceI alignTo : getSequences())
1687 count += AlignmentUtils.alignSequenceAs(alignTo, al, gap, preserveMappedGaps,
1688 preserveUnmappedGaps) ? 1 : 0;
1694 * Returns the alignment in Fasta format. Behaviour of this method is not
1695 * guaranteed between versions.
1698 public String toString()
1700 return new FastaFile().print(getSequencesArray());
1704 * Returns the set of distinct sequence names. No ordering is guaranteed.
1707 public Set<String> getSequenceNames()
1709 Set<String> names = new HashSet<String>();
1710 for (SequenceI seq : getSequences())
1712 names.add(seq.getName());