X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FAlignment.java;h=3689433aefcdcba248a8c16fdb0d005f187a436f;hb=34d0e74532d41a1b46d253e0190baf6c7c29b8da;hp=552a5f84a99600c4c6d4f0a3bb480a8d38fb2de1;hpb=0452450533728e478ef8c893ea2cb3483c740fb3;p=jalview.git diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index 552a5f8..3689433 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -1,6 +1,6 @@ /* * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Copyright (C) 2007 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 @@ -18,656 +18,820 @@ */ package jalview.datamodel; -import jalview.analysis.*; - import java.util.*; +import jalview.analysis.*; + /** Data structure to hold and manipulate a multiple sequence alignment */ -public class Alignment implements AlignmentI +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; - - /** DOCUMENT ME!! */ - public AlignmentAnnotation[] annotations; + 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; - HiddenSequences hiddenSequences = new HiddenSequences(this); + /** DOCUMENT ME!! */ + public AlignmentAnnotation[] annotations; - private void initAlignment(SequenceI[] seqs) { - int i=0; + HiddenSequences hiddenSequences = new HiddenSequences(this); - if( jalview.util.Comparison.isNucleotide(seqs)) - type = NUCLEOTIDE; - else - type = PROTEIN; - - sequences = new Vector(); + public Hashtable alignmentProperties; - for (i = 0; i < seqs.length; i++) - { - sequences.addElement(seqs[i]); - } + private void initAlignment(SequenceI[] seqs) + { + int i = 0; - } - /** Make an alignment from an array of Sequences. - * - * @param sequences - */ - public Alignment(SequenceI[] seqs) + 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))) { - int i, iSize = sg.getSize(); - for (i = 0; i < iSize; i++) - { - if (!sequences.contains(sg.getSequenceAt(i))) - { - sg.deleteSequence(sg.getSequenceAt(i), false); - iSize--; - i--; - } - } - - if (sg.getSize() < 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); } + } - /** */ - public SequenceI findName(String name) - { - int i = 0; + /** + * DOCUMENT ME! + */ + public void deleteAllGroups() + { + groups.removeAllElements(); + } - while (i < sequences.size()) - { - if (getSequenceAt(i).getName().equals(name)) - { - return getSequenceAt(i); - } + /** */ + public void deleteGroup(SequenceGroup g) + { + if (groups.contains(g)) + { + groups.removeElement(g); + } + } - i++; - } + /** */ + public SequenceI findName(String name) + { + return findName(name,false); + } - return null; - } + /* (non-Javadoc) + * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean) + */ + public SequenceI findName(String token, boolean b) + { - public SequenceI [] findSequenceMatch(String name) + int i = 0; + SequenceI sq=null; + String sqname=null; + while (i < sequences.size()) { - Vector matches = new Vector(); - int i = 0; - - while (i < sequences.size()) + sq = getSequenceAt(i); + sqname = sq.getName(); + if (sqname.equals(token) // exact match + || (b && // allow imperfect matches - case varies + (sqname.equalsIgnoreCase(token)))) { - if (getSequenceAt(i).getName().equals(name)) - { - matches.addElement(getSequenceAt(i)); - } - i++; + return getSequenceAt(i); } - SequenceI [] result = new SequenceI[matches.size()]; - for(i=0; i maxLength) - { - maxLength = getSequenceAt(i).getLength(); - } - } - - return maxLength; - } + } + /** */ + 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 gapCharacter; - } + return -1; + } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public int getHeight() + { + return sequences.size(); + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public int getWidth() + { + int maxLength = -1; - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public boolean isAligned() + for (int i = 0; i < sequences.size(); i++) { - int width = getWidth(); + if (getSequenceAt(i).getLength() > 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; - - if (annotations != null) - { - aSize = annotations.length; - } + Sequence seq = (Sequence) sequences.elementAt(i); + seq.setSequence(seq.getSequenceAsString() + .replace('.', gc) + .replace('-', gc) + .replace(' ', gc) + ); + } + } - if(aSize<1) - return; + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public char getGapCharacter() + { + return gapCharacter; + } - AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1]; + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public boolean isAligned() + { + int width = getWidth(); - int tIndex = 0; + for (int i = 0; i < sequences.size(); i++) + { + if (getSequenceAt(i).getLength() != width) + { + return false; + } + } - for (int i = 0; i < aSize; i++) - { - if (annotations[i] == aa) - { - continue; - } + return true; + } + /* (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]; + + boolean swap=false; + int tIndex = 0; - public void adjustSequenceAnnotations() + 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 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--) { - current = getSequenceAt(i); - for (int j = current.getLength(); j > maxLength; j--) + if (j > maxLength && !jalview.util.Comparison.isGap( + current.getCharAt(j))) { - if (j > maxLength && !jalview.util.Comparison.isGap( - current.getCharAt(j))) - { - maxLength = j; - break; - } + maxLength = j; + break; } } + } - maxLength++; + maxLength++; - int cLength; - for (int i = 0; i < sequences.size(); - i++) - { - current = getSequenceAt(i); - cLength = current.getLength(); + int cLength; + for (int i = 0; i < sequences.size(); + i++) + { + current = getSequenceAt(i); + cLength = current.getLength(); - if (cLength < maxLength) - { - current.insertCharAt(cLength, - maxLength-cLength, gapCharacter); - modified=true; - } - else if(current.getLength() > maxLength) - { - current.deleteChars(maxLength, current.getLength()); - } + if (cLength < maxLength) + { + current.insertCharAt(cLength, + maxLength - cLength, gapCharacter); + modified = true; + } + else if (current.getLength() > maxLength) + { + current.deleteChars(maxLength, current.getLength()); } - return modified; } + return modified; + } - public HiddenSequences getHiddenSequences() - { - return hiddenSequences; - } + public HiddenSequences getHiddenSequences() + { + return hiddenSequences; + } public CigarArray getCompactAlignment() { SeqCigar alseqs[] = new SeqCigar[sequences.size()]; - for (int i=0; i