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 an alignment from an array of Sequences.
97 public Alignment(SequenceI[] seqs)
103 * Make a new alignment from an array of SeqCigars
108 public Alignment(SeqCigar[] alseqs)
110 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
111 gapCharacter, new ColumnSelection(), null);
116 * Make a new alignment from an CigarArray JBPNote - can only do this when
117 * compactAlignment does not contain hidden regions. JBPNote - must also check
118 * that compactAlignment resolves to a set of SeqCigars - or construct them
121 * @param compactAlignment
124 public static AlignmentI createAlignment(CigarArray compactAlignment)
126 throw new Error(MessageManager.getString("error.alignment_cigararray_not_implemented"));
127 // this(compactAlignment.refCigars);
133 * @return DOCUMENT ME!
136 public List<SequenceI> getSequences()
142 public List<SequenceI> getSequences(
143 Map<SequenceI, SequenceCollectionI> hiddenReps)
145 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
151 public SequenceI[] getSequencesArray()
153 if (sequences == null)
157 synchronized (sequences)
159 return sequences.toArray(new SequenceI[sequences.size()]);
164 * Returns a map of lists of sequences keyed by sequence name.
169 public Map<String, List<SequenceI>> getSequencesByName()
171 return AlignmentUtils.getSequencesByName(this);
180 * @return DOCUMENT ME!
183 public SequenceI getSequenceAt(int i)
185 synchronized (sequences)
187 if (i > -1 && i < sequences.size())
189 return sequences.get(i);
196 * Adds a sequence to the alignment. Recalculates maxLength and size.
201 public void addSequence(SequenceI snew)
205 // maintain dataset integrity
206 if (snew.getDatasetSequence() != null)
208 getDataset().addSequence(snew.getDatasetSequence());
212 // derive new sequence
213 SequenceI adding = snew.deriveSequence();
214 getDataset().addSequence(adding.getDatasetSequence());
218 if (sequences == null)
220 initAlignment(new SequenceI[]
225 synchronized (sequences)
230 if (hiddenSequences != null)
232 hiddenSequences.adjustHeightSequenceAdded();
237 * Adds a sequence to the alignment. Recalculates maxLength and size.
242 public void setSequenceAt(int i, SequenceI snew)
244 SequenceI oldseq = getSequenceAt(i);
246 synchronized (sequences)
248 sequences.set(i, snew);
255 * @return DOCUMENT ME!
258 public List<SequenceGroup> getGroups()
264 public void finalize()
266 if (getDataset() != null)
268 getDataset().removeAlignmentRef();
275 hiddenSequences = null;
279 * decrement the alignmentRefs counter by one and call finalize if it goes to
282 private void removeAlignmentRef()
284 if (--alignmentRefs == 0)
297 public void deleteSequence(SequenceI s)
299 deleteSequence(findIndex(s));
309 public void deleteSequence(int i)
311 if (i > -1 && i < getHeight())
313 synchronized (sequences)
317 hiddenSequences.adjustHeightSequenceDeleted(i);
324 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
327 public SequenceGroup findGroup(SequenceI s)
329 synchronized (groups)
331 for (int i = 0; i < this.groups.size(); i++)
333 SequenceGroup sg = groups.get(i);
335 if (sg.getSequences(null).contains(s))
348 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
351 public SequenceGroup[] findAllGroups(SequenceI s)
353 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
355 synchronized (groups)
357 int gSize = groups.size();
358 for (int i = 0; i < gSize; i++)
360 SequenceGroup sg = groups.get(i);
361 if (sg == null || sg.getSequences(null) == null)
363 this.deleteGroup(sg);
368 if (sg.getSequences(null).contains(s))
374 SequenceGroup[] ret = new SequenceGroup[temp.size()];
375 return temp.toArray(ret);
380 public void addGroup(SequenceGroup sg)
382 synchronized (groups)
384 if (!groups.contains(sg))
386 if (hiddenSequences.getSize() > 0)
388 int i, iSize = sg.getSize();
389 for (i = 0; i < iSize; i++)
391 if (!sequences.contains(sg.getSequenceAt(i)))
393 sg.deleteSequence(sg.getSequenceAt(i), false);
399 if (sg.getSize() < 1)
411 * remove any annotation that references gp
414 * (if null, removes all group associated annotation)
416 private void removeAnnotationForGroup(SequenceGroup gp)
418 if (annotations == null || annotations.length == 0)
422 // remove annotation very quickly
423 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
427 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
429 if (annotations[i].groupRef != null)
431 todelete[p++] = annotations[i];
435 tokeep[k++] = annotations[i];
441 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
443 if (annotations[i].groupRef == gp)
445 todelete[p++] = annotations[i];
449 tokeep[k++] = annotations[i];
455 // clear out the group associated annotation.
456 for (i = 0; i < p; i++)
458 unhookAnnotation(todelete[i]);
461 t = new AlignmentAnnotation[k];
462 for (i = 0; i < k; i++)
471 public void deleteAllGroups()
473 synchronized (groups)
475 if (annotations != null)
477 removeAnnotationForGroup(null);
479 for (SequenceGroup sg : groups)
489 public void deleteGroup(SequenceGroup g)
491 synchronized (groups)
493 if (groups.contains(g))
495 removeAnnotationForGroup(g);
504 public SequenceI findName(String name)
506 return findName(name, false);
512 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
515 public SequenceI findName(String token, boolean b)
517 return findName(null, token, b);
523 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
527 public SequenceI findName(SequenceI startAfter, String token, boolean b)
532 String sqname = null;
533 if (startAfter != null)
535 // try to find the sequence in the alignment
536 boolean matched = false;
537 while (i < sequences.size())
539 if (getSequenceAt(i++) == startAfter)
550 while (i < sequences.size())
552 sq = getSequenceAt(i);
553 sqname = sq.getName();
554 if (sqname.equals(token) // exact match
555 || (b && // allow imperfect matches - case varies
556 (sqname.equalsIgnoreCase(token))))
558 return getSequenceAt(i);
568 public SequenceI[] findSequenceMatch(String name)
570 Vector matches = new Vector();
573 while (i < sequences.size())
575 if (getSequenceAt(i).getName().equals(name))
577 matches.addElement(getSequenceAt(i));
582 SequenceI[] result = new SequenceI[matches.size()];
583 for (i = 0; i < result.length; i++)
585 result[i] = (SequenceI) matches.elementAt(i);
595 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
598 public int findIndex(SequenceI s)
602 while (i < sequences.size())
604 if (s == getSequenceAt(i))
619 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
622 public int findIndex(SearchResults results)
626 while (i < sequences.size())
628 if (results.involvesSequence(getSequenceAt(i)))
640 * @return DOCUMENT ME!
643 public int getHeight()
645 return sequences.size();
651 * @return DOCUMENT ME!
654 public int getWidth()
658 for (int i = 0; i < sequences.size(); i++)
660 if (getSequenceAt(i).getLength() > maxLength)
662 maxLength = getSequenceAt(i).getLength();
676 public void setGapCharacter(char gc)
679 synchronized (sequences)
681 for (SequenceI seq : sequences)
683 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
684 .replace('-', gc).replace(' ', gc));
692 * @return DOCUMENT ME!
695 public char getGapCharacter()
703 * @see jalview.datamodel.AlignmentI#isAligned()
706 public boolean isAligned()
708 return isAligned(false);
714 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
717 public boolean isAligned(boolean includeHidden)
719 int width = getWidth();
720 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
722 includeHidden = true; // no hidden sequences to check against.
724 for (int i = 0; i < sequences.size(); i++)
726 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
728 if (getSequenceAt(i).getLength() != width)
741 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
742 * AlignmentAnnotation)
745 public boolean deleteAnnotation(AlignmentAnnotation aa)
747 return deleteAnnotation(aa, true);
751 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
755 if (annotations != null)
757 aSize = annotations.length;
765 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
767 boolean swap = false;
770 for (int i = 0; i < aSize; i++)
772 if (annotations[i] == aa)
777 if (tIndex < temp.length)
779 temp[tIndex++] = annotations[i];
788 unhookAnnotation(aa);
795 * remove any object references associated with this annotation
799 private void unhookAnnotation(AlignmentAnnotation aa)
801 if (aa.sequenceRef != null)
803 aa.sequenceRef.removeAlignmentAnnotation(aa);
805 if (aa.groupRef != null)
807 // probably need to do more here in the future (post 2.5.0)
815 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
816 * AlignmentAnnotation)
819 public void addAnnotation(AlignmentAnnotation aa)
821 addAnnotation(aa, -1);
827 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
828 * AlignmentAnnotation, int)
831 public void addAnnotation(AlignmentAnnotation aa, int pos)
833 if (aa.getRNAStruc() != null)
835 hasRNAStructure = true;
839 if (annotations != null)
841 aSize = annotations.length + 1;
844 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
846 if (pos == -1 || pos >= aSize)
848 temp[aSize - 1] = aa;
857 for (i = 0; i < (aSize - 1); i++, p++)
865 temp[p] = annotations[i];
874 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
876 if (aa == null || annotations == null || annotations.length - 1 < index)
881 int aSize = annotations.length;
882 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
886 for (int i = 0; i < aSize; i++)
895 temp[i] = annotations[i];
899 temp[i] = annotations[i - 1];
908 * returns all annotation on the alignment
910 public AlignmentAnnotation[] getAlignmentAnnotation()
916 public void setNucleotide(boolean b)
929 public boolean isNucleotide()
931 if (type == NUCLEOTIDE)
942 public boolean hasRNAStructure()
944 // TODO can it happen that structure is removed from alignment?
945 return hasRNAStructure;
949 public void setDataset(Alignment data)
951 if (dataset == null && data == null)
953 // Create a new dataset for this alignment.
954 // Can only be done once, if dataset is not null
955 // This will not be performed
956 SequenceI[] seqs = new SequenceI[getHeight()];
957 SequenceI currentSeq;
958 for (int i = 0; i < getHeight(); i++)
960 currentSeq = getSequenceAt(i);
961 if (currentSeq.getDatasetSequence() != null)
963 seqs[i] = currentSeq.getDatasetSequence();
967 seqs[i] = currentSeq.createDatasetSequence();
971 dataset = new Alignment(seqs);
973 else if (dataset == null && data != null)
976 for (int i = 0; i < getHeight(); i++)
978 SequenceI currentSeq = getSequenceAt(i);
979 SequenceI dsq = currentSeq.getDatasetSequence();
982 dsq = currentSeq.createDatasetSequence();
983 dataset.addSequence(dsq);
987 while (dsq.getDatasetSequence() != null)
989 dsq = dsq.getDatasetSequence();
991 if (dataset.findIndex(dsq) == -1)
993 dataset.addSequence(dsq);
998 dataset.addAlignmentRef();
1002 * reference count for number of alignments referencing this one.
1004 int alignmentRefs = 0;
1007 * increase reference count to this alignment.
1009 private void addAlignmentRef()
1015 public Alignment getDataset()
1021 public boolean padGaps()
1023 boolean modified = false;
1025 // Remove excess gaps from the end of alignment
1029 for (int i = 0; i < sequences.size(); i++)
1031 current = getSequenceAt(i);
1032 for (int j = current.getLength(); j > maxLength; j--)
1035 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1046 for (int i = 0; i < sequences.size(); i++)
1048 current = getSequenceAt(i);
1049 cLength = current.getLength();
1051 if (cLength < maxLength)
1053 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1056 else if (current.getLength() > maxLength)
1058 current.deleteChars(maxLength, current.getLength());
1065 * Justify the sequences to the left or right by deleting and inserting gaps
1066 * before the initial residue or after the terminal residue
1069 * true if alignment padded to right, false to justify to left
1070 * @return true if alignment was changed
1073 public boolean justify(boolean right)
1075 boolean modified = false;
1077 // Remove excess gaps from the end of alignment
1079 int ends[] = new int[sequences.size() * 2];
1081 for (int i = 0; i < sequences.size(); i++)
1083 current = getSequenceAt(i);
1084 // This should really be a sequence method
1085 ends[i * 2] = current.findIndex(current.getStart());
1086 ends[i * 2 + 1] = current.findIndex(current.getStart()
1087 + current.getLength());
1088 boolean hitres = false;
1089 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1091 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1100 ends[i * 2 + 1] = j;
1101 if (j - ends[i * 2] > maxLength)
1103 maxLength = j - ends[i * 2];
1111 // now edit the flanking gaps to justify to either left or right
1112 int cLength, extent, diff;
1113 for (int i = 0; i < sequences.size(); i++)
1115 current = getSequenceAt(i);
1117 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1118 diff = maxLength - cLength; // number of gaps to indent
1119 extent = current.getLength();
1123 if (extent > ends[i * 2 + 1])
1125 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1128 if (ends[i * 2] > diff)
1130 current.deleteChars(0, ends[i * 2] - diff);
1135 if (ends[i * 2] < diff)
1137 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1145 if (ends[i * 2] > 0)
1147 current.deleteChars(0, ends[i * 2]);
1149 ends[i * 2 + 1] -= ends[i * 2];
1150 extent -= ends[i * 2];
1152 if (extent > maxLength)
1154 current.deleteChars(maxLength + 1, extent);
1159 if (extent < maxLength)
1161 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1171 public HiddenSequences getHiddenSequences()
1173 return hiddenSequences;
1177 public CigarArray getCompactAlignment()
1179 synchronized (sequences)
1181 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1183 for (SequenceI seq : sequences)
1185 alseqs[i++] = new SeqCigar(seq);
1187 CigarArray cal = new CigarArray(alseqs);
1188 cal.addOperation(CigarArray.M, getWidth());
1194 public void setProperty(Object key, Object value)
1196 if (alignmentProperties == null)
1198 alignmentProperties = new Hashtable();
1201 alignmentProperties.put(key, value);
1205 public Object getProperty(Object key)
1207 if (alignmentProperties != null)
1209 return alignmentProperties.get(key);
1218 public Hashtable getProperties()
1220 return alignmentProperties;
1223 AlignedCodonFrame[] codonFrameList = null;
1229 * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1233 public void addCodonFrame(AlignedCodonFrame codons)
1239 if (codonFrameList == null)
1241 codonFrameList = new AlignedCodonFrame[]
1245 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
1246 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
1247 t[codonFrameList.length] = codons;
1254 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
1257 public AlignedCodonFrame getCodonFrame(int index)
1259 return codonFrameList[index];
1266 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1269 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
1271 if (seq == null || codonFrameList == null)
1275 Vector cframes = new Vector();
1276 for (int f = 0; f < codonFrameList.length; f++)
1278 if (codonFrameList[f].involvesSequence(seq))
1280 cframes.addElement(codonFrameList[f]);
1283 if (cframes.size() == 0)
1287 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
1288 cframes.copyInto(cfr);
1295 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1298 public AlignedCodonFrame[] getCodonFrames()
1300 return codonFrameList;
1306 * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
1307 * AlignedCodonFrame)
1310 public boolean removeCodonFrame(AlignedCodonFrame codons)
1312 if (codons == null || codonFrameList == null)
1316 boolean removed = false;
1317 int i = 0, iSize = codonFrameList.length;
1320 if (codonFrameList[i] == codons)
1325 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize
1339 public void append(AlignmentI toappend)
1341 if (toappend == this)
1343 System.err.println("Self append may cause a deadlock.");
1345 // TODO test this method for a future 2.5 release
1346 // currently tested for use in jalview.gui.SequenceFetcher
1347 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1348 char oldc = toappend.getGapCharacter();
1349 boolean hashidden = toappend.getHiddenSequences() != null
1350 && toappend.getHiddenSequences().hiddenSequences != null;
1351 // get all sequences including any hidden ones
1352 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1353 .getFullAlignment().getSequences() : toappend.getSequences();
1358 for (SequenceI addedsq : sqs)
1362 char[] oldseq = addedsq.getSequence();
1363 for (int c = 0; c < oldseq.length; c++)
1365 if (oldseq[c] == oldc)
1367 oldseq[c] = gapCharacter;
1371 addSequence(addedsq);
1375 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1376 for (int a = 0; alan != null && a < alan.length; a++)
1378 addAnnotation(alan[a]);
1380 AlignedCodonFrame[] acod = toappend.getCodonFrames();
1381 for (int a = 0; acod != null && a < acod.length; a++)
1383 this.addCodonFrame(acod[a]);
1385 List<SequenceGroup> sg = toappend.getGroups();
1388 for (SequenceGroup _sg : sg)
1393 if (toappend.getHiddenSequences() != null)
1395 HiddenSequences hs = toappend.getHiddenSequences();
1396 if (hiddenSequences == null)
1398 hiddenSequences = new HiddenSequences(this);
1400 if (hs.hiddenSequences != null)
1402 for (int s = 0; s < hs.hiddenSequences.length; s++)
1404 // hide the newly appended sequence in the alignment
1405 if (hs.hiddenSequences[s] != null)
1407 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1412 if (toappend.getProperties() != null)
1414 // we really can't do very much here - just try to concatenate strings
1415 // where property collisions occur.
1416 Enumeration key = toappend.getProperties().keys();
1417 while (key.hasMoreElements())
1419 Object k = key.nextElement();
1420 Object ourval = this.getProperty(k);
1421 Object toapprop = toappend.getProperty(k);
1424 if (ourval.getClass().equals(toapprop.getClass())
1425 && !ourval.equals(toapprop))
1427 if (ourval instanceof String)
1430 this.setProperty(k, ((String) ourval) + "; "
1431 + ((String) toapprop));
1435 if (ourval instanceof Vector)
1438 Enumeration theirv = ((Vector) toapprop).elements();
1439 while (theirv.hasMoreElements())
1441 ((Vector) ourval).addElement(theirv);
1449 // just add new property directly
1450 setProperty(k, toapprop);
1458 public AlignmentAnnotation findOrCreateAnnotation(String name,
1459 String calcId, boolean autoCalc, SequenceI seqRef,
1460 SequenceGroup groupRef)
1462 assert (name != null);
1463 if (annotations != null)
1465 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1467 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1468 && (calcId == null || annot.getCalcId().equals(calcId))
1469 && annot.sequenceRef == seqRef
1470 && annot.groupRef == groupRef)
1476 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1477 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1478 annot.hasText = false;
1479 annot.setCalcId(new String(calcId));
1480 annot.autoCalculated = autoCalc;
1483 annot.setSequenceRef(seqRef);
1485 annot.groupRef = groupRef;
1486 addAnnotation(annot);
1492 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1494 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1495 for (AlignmentAnnotation a : getAlignmentAnnotation())
1497 if (a.getCalcId() == calcId
1498 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1508 * Returns an iterable collection of any annotations that match on given
1509 * sequence ref, calcId and label (ignoring null values).
1512 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1513 String calcId, String label)
1515 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1516 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1518 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1519 && ann.sequenceRef != null && ann.sequenceRef == seq
1520 && ann.label != null && ann.label.equals(label))
1529 public void moveSelectedSequencesByOne(SequenceGroup sg,
1530 Map<SequenceI, SequenceCollectionI> map, boolean up)
1532 synchronized (sequences)
1537 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1539 SequenceI seq = sequences.get(i);
1540 if (!sg.getSequences(map).contains(seq))
1545 SequenceI temp = sequences.get(i - 1);
1546 if (sg.getSequences(null).contains(temp))
1551 sequences.set(i, temp);
1552 sequences.set(i - 1, seq);
1557 for (int i = sequences.size() - 2; i > -1; i--)
1559 SequenceI seq = sequences.get(i);
1560 if (!sg.getSequences(map).contains(seq))
1565 SequenceI temp = sequences.get(i + 1);
1566 if (sg.getSequences(map).contains(temp))
1571 sequences.set(i, temp);
1572 sequences.set(i + 1, seq);
1580 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1582 alignmentAnnotation.validateRangeAndDisplay();
1583 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1585 hasRNAStructure = true;
1590 public int getEndRes()
1592 return getWidth() - 1;
1596 public int getStartRes()
1602 * In the case of AlignmentI - returns the dataset for the alignment, if set
1605 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1608 public AnnotatedCollectionI getContext()
1614 public int alignAs(AlignmentI al)
1616 return alignAs(al, true, true);
1620 * Align this alignment 'the same as' the given one. Mapped sequences only are
1621 * realigned. If both of the same type (nucleotide/protein) then align both
1622 * identically. If this is nucleotide and the other is protein, make 3 gaps
1623 * for each gap in the protein sequences. If this is protein and the other is
1624 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1625 * nucleotide bases. Does nothing if alignment of protein from cDNA is
1626 * requested (not yet implemented).
1631 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1632 boolean preserveUnmappedGaps)
1634 // TODO should this method signature be the one in the interface?
1636 boolean thisIsNucleotide = this.isNucleotide();
1637 boolean thatIsProtein = !al.isNucleotide();
1638 if (!thatIsProtein && !thisIsNucleotide)
1641 .println("Alignment of protein from cDNA not yet implemented");
1643 // todo: build it - a variant of Dna.CdnaTranslate()
1646 char thisGapChar = this.getGapCharacter();
1647 String gap = thisIsNucleotide && thatIsProtein ? String
1649 { thisGapChar, thisGapChar, thisGapChar }) : String
1650 .valueOf(thisGapChar);
1653 * Get mappings from 'that' alignment's sequences to this.
1655 for (SequenceI alignTo : getSequences())
1657 count += AlignmentUtils.alignSequenceAs(alignTo, al, gap, preserveMappedGaps,
1658 preserveUnmappedGaps) ? 1 : 0;
1664 * Returns the alignment in Fasta format. Behaviour of this method is not
1665 * guaranteed between versions.
1668 public String toString()
1670 return new FastaFile().print(getSequencesArray());
1674 * Returns the set of distinct sequence names. No ordering is guaranteed.
1677 public Set<String> getSequenceNames()
1679 Set<String> names = new HashSet<String>();
1680 for (SequenceI seq : getSequences())
1682 names.add(seq.getName());