X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FAlignment.java;h=08c04f5b779f3b73c27cdba8baf081912a48d199;hb=50ad147ba0eb763fae29b364c80632abc1155e9c;hp=b5f31bccc0ee0e5d3904f568d8b75c986d4eef47;hpb=4d2e0d36506302cc00677527725bcccbdf27d766;p=jalview.git diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index b5f31bc..08c04f5 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -1,700 +1,804 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4) + * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.datamodel; -import jalview.analysis.*; - -import jalview.util.*; - import java.util.*; -/** Data structure to hold and manipulate a multiple sequence alignment +import jalview.analysis.*; + +/** + * Data structure to hold and manipulate a multiple sequence alignment */ public class Alignment implements AlignmentI { - protected Alignment dataset; - protected Vector sequences; - protected Vector groups = new Vector(); - protected char gapCharacter = '-'; - protected int type = NUCLEOTIDE; - public static final int PROTEIN = 0; - public static final int NUCLEOTIDE = 1; + protected Alignment dataset; - /** DOCUMENT ME!! */ - public AlignmentAnnotation[] annotations; + protected Vector sequences; - HiddenSequences hiddenSequences = new HiddenSequences(this); + protected Vector groups = new Vector(); - private void initAlignment(SequenceI[] seqs) { - int i=0; + protected char gapCharacter = '-'; - if( jalview.util.Comparison.isNucleotide(seqs)) - type = NUCLEOTIDE; - else - type = PROTEIN; + protected int type = NUCLEOTIDE; - sequences = new Vector(); + public static final int PROTEIN = 0; - for (i = 0; i < seqs.length; i++) - { - sequences.addElement(seqs[i]); - } + public static final int NUCLEOTIDE = 1; - } - /** Make an alignment from an array of Sequences. - * - * @param sequences - */ - public Alignment(SequenceI[] seqs) + /** DOCUMENT ME!! */ + public AlignmentAnnotation[] annotations; + + HiddenSequences hiddenSequences = new HiddenSequences(this); + + public Hashtable alignmentProperties; + + private void initAlignment(SequenceI[] seqs) + { + int i = 0; + + if (jalview.util.Comparison.isNucleotide(seqs)) { - initAlignment(seqs); - } - /** - * Make a new alignment from an array of SeqCigars - * @param seqs SeqCigar[] - */ - public Alignment(SeqCigar[] alseqs) { - SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs, gapCharacter, new ColumnSelection(), null); - initAlignment(seqs); + type = NUCLEOTIDE; } - /** - * Make a new alignment from an CigarArray - * JBPNote - can only do this when compactAlignment does not contain hidden regions. - * JBPNote - must also check that compactAlignment resolves to a set of SeqCigars - or construct them appropriately. - * @param compactAlignment CigarArray - */ - public static AlignmentI createAlignment(CigarArray compactAlignment) { - throw new Error("Alignment(CigarArray) not yet implemented"); - // this(compactAlignment.refCigars); + else + { + type = PROTEIN; } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public Vector getSequences() + sequences = new Vector(); + + for (i = 0; i < seqs.length; i++) { - return sequences; + sequences.addElement(seqs[i]); } - public SequenceI [] getSequencesArray() + } + + /** + * Make an alignment from an array of Sequences. + * + * @param sequences + */ + public Alignment(SequenceI[] seqs) + { + initAlignment(seqs); + } + + /** + * Make a new alignment from an array of SeqCigars + * + * @param seqs + * SeqCigar[] + */ + public Alignment(SeqCigar[] alseqs) + { + SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs, + gapCharacter, new ColumnSelection(), null); + initAlignment(seqs); + } + + /** + * Make a new alignment from an CigarArray JBPNote - can only do this when + * compactAlignment does not contain hidden regions. JBPNote - must also check + * that compactAlignment resolves to a set of SeqCigars - or construct them + * appropriately. + * + * @param compactAlignment + * CigarArray + */ + public static AlignmentI createAlignment(CigarArray compactAlignment) + { + throw new Error("Alignment(CigarArray) not yet implemented"); + // this(compactAlignment.refCigars); + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Vector getSequences() + { + return sequences; + } + + public SequenceI[] getSequencesArray() + { + if (sequences == null) + return null; + SequenceI[] reply = new SequenceI[sequences.size()]; + for (int i = 0; i < sequences.size(); i++) { - SequenceI [] reply = new SequenceI[sequences.size()]; - for(int i=0; i -1 && i < getHeight()) { - deleteSequence(findIndex(s)); + sequences.removeElementAt(i); + hiddenSequences.adjustHeightSequenceDeleted(i); } + } - /** - * DOCUMENT ME! - * - * @param i DOCUMENT ME! - */ - public void deleteSequence(int i) + /** */ + public SequenceGroup findGroup(SequenceI s) + { + for (int i = 0; i < this.groups.size(); i++) { - if(i>-1 && i 0) + { + int i, iSize = sg.getSize(); + for (i = 0; i < iSize; i++) { - if(hiddenSequences.getSize()>0) + if (!sequences.contains(sg.getSequenceAt(i))) { - //We're not going to make groups of - //Hidden sequences - int i, iSize = sg.getSize(false); - for (i = 0; i < iSize; i++) - { - if (!sequences.contains(sg.getSequenceAt(i))) - { - sg.deleteSequence(sg.getSequenceAt(i), false); - iSize--; - i--; - } - } - - if (sg.getSize(true) < 1) - return; + sg.deleteSequence(sg.getSequenceAt(i), false); + iSize--; + i--; } - - groups.addElement(sg); } - } - /** - * DOCUMENT ME! - */ - public void deleteAllGroups() - { - groups.removeAllElements(); - } - - /** */ - public void deleteGroup(SequenceGroup g) - { - if (groups.contains(g)) + if (sg.getSize() < 1) { - groups.removeElement(g); + return; } + } + + groups.addElement(sg); } + } + + /** + * DOCUMENT ME! + */ + public void deleteAllGroups() + { + groups.removeAllElements(); + } - /** */ - public SequenceI findName(String name) + /** */ + public void deleteGroup(SequenceGroup g) + { + if (groups.contains(g)) { - int i = 0; + groups.removeElement(g); + } + } - while (i < sequences.size()) - { - if (getSequenceAt(i).getName().equals(name)) - { - return getSequenceAt(i); - } + /** */ + public SequenceI findName(String name) + { + return findName(name, false); + } - i++; - } + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean) + */ + public SequenceI findName(String token, boolean b) + { + return findName(null, token, b); + } - return null; - } + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String, + * boolean) + */ + public SequenceI findName(SequenceI startAfter, String token, boolean b) + { - public SequenceI [] findSequenceMatch(String name) + int i = 0; + SequenceI sq = null; + String sqname = null; + if (startAfter != null) { - Vector matches = new Vector(); - int i = 0; - + // try to find the sequence in the alignment + boolean matched = false; while (i < sequences.size()) { - if (getSequenceAt(i).getName().equals(name)) - { - matches.addElement(getSequenceAt(i)); - } - i++; + if (getSequenceAt(i++) == startAfter) + { + matched = true; + break; + } + } + if (!matched) + { + i = 0; } - - SequenceI [] result = new SequenceI[matches.size()]; - for(i=0; i maxLength) - { - maxLength = getSequenceAt(i).getLength(); - } - } - - return maxLength; + result[i] = (SequenceI) matches.elementAt(i); } + return result; + + } + /* (non-Javadoc) + * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI) + */ + public int findIndex(SequenceI s) + { + int i = 0; - /** - * DOCUMENT ME! - * - * @param gc DOCUMENT ME! - */ - public void setGapCharacter(char gc) + while (i < sequences.size()) { - gapCharacter = gc; + if (s == getSequenceAt(i)) + { + return i; + } - for (int i = 0; i < sequences.size(); i++) - { - Sequence seq = (Sequence) sequences.elementAt(i); - seq.setSequence( seq.getSequenceAsString().replace('.', gc) ); - seq.setSequence( seq.getSequenceAsString().replace('-', gc) ); - seq.setSequence( seq.getSequenceAsString().replace(' ', gc) ); - } + i++; } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public char getGapCharacter() + return -1; + } + /* (non-Javadoc) + * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults) + */ + public int findIndex(SearchResults results) + { + int i=0; + + while (i maxLength) + { + maxLength = getSequenceAt(i).getLength(); + } + } - for (int i = 0; i < sequences.size(); i++) - { - if (getSequenceAt(i).getLength() != width) - { - return false; - } - } + return maxLength; + } - return true; - } + /** + * DOCUMENT ME! + * + * @param gc + * DOCUMENT ME! + */ + public void setGapCharacter(char gc) + { + gapCharacter = gc; - /** - * DOCUMENT ME! - * - * @param aa DOCUMENT ME! - */ - public void deleteAnnotation(AlignmentAnnotation aa) + for (int i = 0; i < sequences.size(); i++) { - int aSize = 1; + Sequence seq = (Sequence) sequences.elementAt(i); + seq.setSequence(seq.getSequenceAsString().replace('.', gc).replace( + '-', gc).replace(' ', gc)); + } + } - if (annotations != null) - { - aSize = annotations.length; - } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public char getGapCharacter() + { + return gapCharacter; + } - if(aSize<1) - return; + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public boolean isAligned() + { + int width = getWidth(); - AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1]; + for (int i = 0; i < sequences.size(); i++) + { + if (getSequenceAt(i).getLength() != width) + { + return false; + } + } - int tIndex = 0; + return true; + } - for (int i = 0; i < aSize; i++) - { - if (annotations[i] == aa) - { - continue; - } + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation) + */ + public boolean deleteAnnotation(AlignmentAnnotation aa) + { + int aSize = 1; - temp[tIndex] = annotations[i]; - tIndex++; - } + if (annotations != null) + { + aSize = annotations.length; + } - annotations = temp; + if (aSize < 1) + { + return false; } + AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1]; - public void adjustSequenceAnnotations() + boolean swap = false; + int tIndex = 0; + + for (int i = 0; i < aSize; i++) { - if(annotations!=null) + if (annotations[i] == aa) { - for (int a = 0; a < annotations.length; a++) - { - if (annotations[a].sequenceRef != null) - { - annotations[a].adjustForAlignment(); - } - } + swap = true; + continue; } + if (tIndex < temp.length) + temp[tIndex++] = annotations[i]; } - /** - * DOCUMENT ME! - * - * @param aa DOCUMENT ME! - */ - public void addAnnotation(AlignmentAnnotation aa) + if (swap) { - int aSize = 1; - if (annotations != null) - { - aSize = annotations.length + 1; - } + annotations = temp; + if (aa.sequenceRef != null) + aa.sequenceRef.removeAlignmentAnnotation(aa); + } + return swap; + } - AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize]; + /** + * DOCUMENT ME! + * + * @param aa + * DOCUMENT ME! + */ + public void addAnnotation(AlignmentAnnotation aa) + { + int aSize = 1; + if (annotations != null) + { + aSize = annotations.length + 1; + } - temp[aSize-1] = aa; + AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize]; - int i = 0; + temp[aSize - 1] = aa; - if (aSize > 1) - { - for (i = 0; i < (aSize-1); i++) - { - temp[i] = annotations[i]; - } - } + int i = 0; - annotations = temp; + if (aSize > 1) + { + for (i = 0; i < (aSize - 1); i++) + { + temp[i] = annotations[i]; + } } - public void setAnnotationIndex(AlignmentAnnotation aa, int index) + annotations = temp; + } + + public void setAnnotationIndex(AlignmentAnnotation aa, int index) + { + if (aa == null || annotations == null || annotations.length - 1 < index) { - if(aa==null || annotations==null || annotations.length-1 maxLength; j--) - { - if (j > maxLength && !jalview.util.Comparison.isGap( - current.getCharAt(j))) - { - maxLength = j; - break; - } - } - } + public Alignment getDataset() + { + return dataset; + } - maxLength++; + public boolean padGaps() + { + boolean modified = false; - int cLength; - for (int i = 0; i < sequences.size(); - i++) - { - current = getSequenceAt(i); - cLength = current.getLength(); + // Remove excess gaps from the end of alignment + int maxLength = -1; - if (cLength < maxLength) - { - current.insertCharAt(cLength, - maxLength-cLength, gapCharacter); - modified=true; - } - else if(current.getLength() > maxLength) + SequenceI current; + for (int i = 0; i < sequences.size(); i++) + { + current = getSequenceAt(i); + for (int j = current.getLength(); j > maxLength; j--) + { + if (j > maxLength + && !jalview.util.Comparison.isGap(current.getCharAt(j))) { - current.deleteChars(maxLength, current.getLength()); + maxLength = j; + break; } } - return modified; } - public HiddenSequences getHiddenSequences() - { - return hiddenSequences; - } - SequenceI [] getVisibleAndRepresentedSeqs() + maxLength++; + + int cLength; + for (int i = 0; i < sequences.size(); i++) { - if(hiddenSequences==null || hiddenSequences.getSize()<1) - return getSequencesArray(); + current = getSequenceAt(i); + cLength = current.getLength(); - Vector seqs = new Vector(); - SequenceI seq; - SequenceGroup hidden; - for (int i = 0; i < sequences.size(); i++) + if (cLength < maxLength) { - seq = (SequenceI) sequences.elementAt(i); - seqs.addElement(seq); - hidden = seq.getHiddenSequences(); - if(hidden!=null) - { - for(int j=0; j maxLength) + { + current.deleteChars(maxLength, current.getLength()); } - SequenceI [] result = new SequenceI[seqs.size()]; - for(int i=0; i