X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FAlignment.java;h=5d97fe3bd3759235e7ad84571297bc079157e568;hb=b2f9a8d7bce642ff4011bc6d49e02bb0569fbb11;hp=c839271a27809dd78e6dd4224c76824fb1fe3e5f;hpb=64eb36a729257a5b03ec3b663ff74797f55e728c;p=jalview.git diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index c839271..5d97fe3 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -1,838 +1,1515 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2005 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 + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1) + * Copyright (C) 2014 The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview 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 3 of the License, or (at your option) any later version. + * + * Jalview 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 Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.datamodel; -import jalview.analysis.*; - -import jalview.util.*; - import java.util.*; -/** Data structure to hold and manipulate a multiple sequence alignment +/** + * Data structure to hold and manipulate a multiple sequence alignment + */ +/** + * @author JimP + * */ 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 List sequences; - HiddenSequences hiddenSequences = new HiddenSequences(this); + protected List groups = java.util.Collections + .synchronizedList(new ArrayList()); - 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) + public boolean hasRNAStructure = false; + + /** 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); + type = NUCLEOTIDE; } - /** - * 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); + else + { + type = PROTEIN; } - /** - * 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); + + sequences = java.util.Collections + .synchronizedList(new ArrayList()); + + for (i = 0; i < seqs.length; i++) + { + sequences.add(seqs[i]); } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public Vector getSequences() + } + + /** + * 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! + */ + @Override + public List getSequences() + { + return sequences; + } + + @Override + public List getSequences( + Map hiddenReps) + { + // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to + // work on this. + return sequences; + } + + @Override + public SequenceI[] getSequencesArray() + { + if (sequences == null) + return null; + synchronized (sequences) { - return sequences; + return sequences.toArray(new SequenceI[sequences.size()]); } + } - public SequenceI [] getSequencesArray() + /** + * DOCUMENT ME! + * + * @param i + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + @Override + public SequenceI getSequenceAt(int i) + { + synchronized (sequences) { - SequenceI [] reply = new SequenceI[sequences.size()]; - for(int i=0; i -1 && i < sequences.size()) { - reply[i] = (SequenceI)sequences.elementAt(i); + return sequences.get(i); } - return reply; } + return null; + } - /** - * DOCUMENT ME! - * - * @param i DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public SequenceI getSequenceAt(int i) + /** + * Adds a sequence to the alignment. Recalculates maxLength and size. + * + * @param snew + */ + @Override + public void addSequence(SequenceI snew) + { + if (dataset != null) { - if (i < sequences.size()) - { - return (SequenceI) sequences.elementAt(i); - } - - return null; + // maintain dataset integrity + if (snew.getDatasetSequence() != null) + { + getDataset().addSequence(snew.getDatasetSequence()); + } + else + { + // derive new sequence + SequenceI adding = snew.deriveSequence(); + getDataset().addSequence(adding.getDatasetSequence()); + snew = adding; + } } - - /** Adds a sequence to the alignment. Recalculates maxLength and size. - * - * @param snew - */ - public void addSequence(SequenceI snew) + if (sequences == null) { - if(dataset!=null) + initAlignment(new SequenceI[] + { snew }); + } + else + { + synchronized (sequences) { - if(snew.getDatasetSequence()!=null) - { - System.out.println(snew.getName()); - getDataset().addSequence(snew.getDatasetSequence()); - } - else - { - Sequence ds = new Sequence(snew.getName(), - AlignSeq.extractGaps("-. ", - snew.getSequence()), - snew.getStart(), - snew.getEnd()); - - snew.setDatasetSequence(ds); - getDataset().addSequence(ds); - } + sequences.add(snew); } + } + if (hiddenSequences != null) + hiddenSequences.adjustHeightSequenceAdded(); + } - sequences.addElement(snew); + /** + * Adds a sequence to the alignment. Recalculates maxLength and size. + * + * @param snew + */ + @Override + public void setSequenceAt(int i, SequenceI snew) + { + SequenceI oldseq = getSequenceAt(i); + deleteSequence(i); + synchronized (sequences) + { + sequences.set(i, snew); } + } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + @Override + public List getGroups() + { + return groups; + } - /** Adds a sequence to the alignment. Recalculates maxLength and size. - * - * @param snew - */ - public void setSequenceAt(int i, SequenceI snew) - { - SequenceI oldseq = getSequenceAt(i); - deleteSequence(oldseq); + @Override + public void finalize() + { + if (getDataset() != null) + getDataset().removeAlignmentRef(); + + dataset = null; + sequences = null; + groups = null; + annotations = null; + hiddenSequences = null; + } - sequences.setElementAt(snew, i); + /** + * decrement the alignmentRefs counter by one and call finalize if it goes to + * zero. + */ + private void removeAlignmentRef() + { + if (--alignmentRefs == 0) + { + finalize(); } + } + + /** + * DOCUMENT ME! + * + * @param s + * DOCUMENT ME! + */ + @Override + public void deleteSequence(SequenceI s) + { + deleteSequence(findIndex(s)); + } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public Vector getGroups() + /** + * DOCUMENT ME! + * + * @param i + * DOCUMENT ME! + */ + @Override + public void deleteSequence(int i) + { + if (i > -1 && i < getHeight()) { - return groups; + synchronized (sequences) + { + sequences.remove(i); + } + hiddenSequences.adjustHeightSequenceDeleted(i); } + } - /** Takes out columns consisting entirely of gaps (-,.," ") - */ - public void removeGaps() + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI) + */ + @Override + public SequenceGroup findGroup(SequenceI s) + { + synchronized (groups) { - SequenceI[] seqs = getVisibleAndRepresentedSeqs(); - int j, jSize = seqs.length; + for (int i = 0; i < this.groups.size(); i++) + { + SequenceGroup sg = groups.get(i); - int width = 0; - for (int i = 0; i < jSize; i++) + if (sg.getSequences(null).contains(s)) { - if (seqs[i].getLength() > width) - { - width = seqs[i].getLength(); - } + return sg; } + } + } + return null; + } - int startCol = -1, endCol = -1; - boolean delete = true; - for (int i = 0; i < width; i++) - { - delete = true; - - for (j = 0; j < jSize; j++) - { - if (seqs[j].getLength() > i) - { - if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i))) - { - if(delete) - endCol = i; - - delete = false; - break; - } - } - } - - if(delete && startCol==-1) - { - startCol = i; - } - + /* + * (non-Javadoc) + * + * @see + * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI) + */ + @Override + public SequenceGroup[] findAllGroups(SequenceI s) + { + ArrayList temp = new ArrayList(); - if (!delete && startCol > -1) - { - deleteColumns(seqs, startCol, endCol); - width -= (endCol - startCol); - i -= (endCol - startCol); - startCol = -1; - endCol = -1; - } + synchronized (groups) + { + int gSize = groups.size(); + for (int i = 0; i < gSize; i++) + { + SequenceGroup sg = groups.get(i); + if (sg == null || sg.getSequences(null) == null) + { + this.deleteGroup(sg); + gSize--; + continue; } - if (delete && startCol > -1) + if (sg.getSequences(null).contains(s)) { - deleteColumns(seqs, startCol, endCol); + temp.add(sg); } + } } + SequenceGroup[] ret = new SequenceGroup[temp.size()]; + return temp.toArray(ret); + } - /** Removes a range of columns (start to end inclusive). - * - * @param seqs Sequences to remove columns from - * @param start Start column in the alignment - * @param end End column in the alignment - */ - public void deleteColumns(SequenceI [] seqs, int start, int end) - { - for(int i=0; i 0) { - int newstart = seqs[j].findPosition(i); - - if(i>seqs[j].getLength()) - { - sequences.removeElement(seqs[j]); - j--; - jSize--; - } - else + int i, iSize = sg.getSize(); + for (i = 0; i < iSize; i++) + { + if (!sequences.contains(sg.getSequenceAt(i))) { - seqs[j].setStart(newstart); - seqs[j].setSequence(seqs[j].getSequence().substring(i)); + sg.deleteSequence(sg.getSequenceAt(i), false); + iSize--; + i--; } + } + + if (sg.getSize() < 1) + { + return; + } } + sg.setContext(this); + groups.add(sg); + } } + } - /** - * DOCUMENT ME! - * - * @param i DOCUMENT ME! - */ - public void trimRight(int i) + /** + * remove any annotation that references gp + * + * @param gp + * (if null, removes all group associated annotation) + */ + private void removeAnnotationForGroup(SequenceGroup gp) + { + if (annotations == null || annotations.length == 0) { - SequenceI[] seqs = getVisibleAndRepresentedSeqs(); - int j, jSize = seqs.length; - for (j = 0; j < jSize; j++) + return; + } + // remove annotation very quickly + AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length]; + int i, p, k; + if (gp == null) + { + for (i = 0, p = 0, k = 0; i < annotations.length; i++) + { + if (annotations[i].groupRef != null) { - int newend = seqs[j].findPosition(i); - - seqs[j].setEnd(newend); - if(seqs[j].getLength()>i) - seqs[j].setSequence(seqs[j].getSequence().substring(0, i + 1)); + todelete[p++] = annotations[i]; + } + else + { + tokeep[k++] = annotations[i]; } + } } - - /** - * DOCUMENT ME! - * - * @param s DOCUMENT ME! - */ - public void deleteSequence(SequenceI s) + else { - for (int i = 0; i < getHeight(); i++) + for (i = 0, p = 0, k = 0; i < annotations.length; i++) + { + if (annotations[i].groupRef == gp) { - if (getSequenceAt(i) == s) - { - deleteSequence(i); - } + todelete[p++] = annotations[i]; + } + else + { + tokeep[k++] = annotations[i]; } + } } - - /** - * DOCUMENT ME! - * - * @param i DOCUMENT ME! - */ - public void deleteSequence(int i) + if (p > 0) { - sequences.removeElementAt(i); + // clear out the group associated annotation. + for (i = 0; i < p; i++) + { + unhookAnnotation(todelete[i]); + todelete[i] = null; + } + t = new AlignmentAnnotation[k]; + for (i = 0; i < k; i++) + { + t[i] = tokeep[i]; + } + annotations = t; } + } - - /** */ - public SequenceGroup findGroup(SequenceI s) + @Override + public void deleteAllGroups() + { + synchronized (groups) { - for (int i = 0; i < this.groups.size(); i++) - { - SequenceGroup sg = (SequenceGroup) groups.elementAt(i); - - if (sg.getSequences(false).contains(s)) - { - return sg; - } - } - - return null; + if (annotations != null) + { + removeAnnotationForGroup(null); + } + for (SequenceGroup sg:groups) { + sg.setContext(null); + } + groups.clear(); } + } - /** - * DOCUMENT ME! - * - * @param s DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public SequenceGroup[] findAllGroups(SequenceI s) + /** */ + @Override + public void deleteGroup(SequenceGroup g) + { + synchronized (groups) { - Vector temp = new Vector(); + if (groups.contains(g)) + { + removeAnnotationForGroup(g); + groups.remove(g); + g.setContext(null); + } + } + } - int gSize = groups.size(); - for (int i = 0; i < gSize; i++) - { - SequenceGroup sg = (SequenceGroup) groups.elementAt(i); - if(sg==null || sg.getSequences(false)==null) - { - this.deleteGroup(sg); - gSize--; - continue; - } + /** */ + @Override + public SequenceI findName(String name) + { + return findName(name, false); + } - if (sg.getSequences(false).contains(s)) - { - temp.addElement(sg); - } - } + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean) + */ + @Override + public SequenceI findName(String token, boolean b) + { + return findName(null, token, b); + } - SequenceGroup[] ret = new SequenceGroup[temp.size()]; + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String, + * boolean) + */ + @Override + public SequenceI findName(SequenceI startAfter, String token, boolean b) + { - for (int i = 0; i < temp.size(); i++) + int i = 0; + SequenceI sq = null; + String sqname = null; + if (startAfter != null) + { + // try to find the sequence in the alignment + boolean matched = false; + while (i < sequences.size()) + { + if (getSequenceAt(i++) == startAfter) { - ret[i] = (SequenceGroup) temp.elementAt(i); + matched = true; + break; } + } + if (!matched) + { + 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)))) + { + return getSequenceAt(i); + } - return ret; + i++; } + return null; + } + @Override + public SequenceI[] findSequenceMatch(String name) + { + Vector matches = new Vector(); + int i = 0; - /** */ - public void addGroup(SequenceGroup sg) + while (i < sequences.size()) { - if (!groups.contains(sg)) - { - groups.addElement(sg); - } + if (getSequenceAt(i).getName().equals(name)) + { + matches.addElement(getSequenceAt(i)); + } + i++; } - /** - * DOCUMENT ME! - */ - public void deleteAllGroups() + SequenceI[] result = new SequenceI[matches.size()]; + for (i = 0; i < result.length; i++) { - groups.removeAllElements(); + result[i] = (SequenceI) matches.elementAt(i); + } - int i = 0; + return result; - while (i < sequences.size()) - { - SequenceI s = getSequenceAt(i); - s.setColor(java.awt.Color.white); - i++; - } - } + } + + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI) + */ + @Override + public int findIndex(SequenceI s) + { + int i = 0; - /** */ - public void deleteGroup(SequenceGroup g) + while (i < sequences.size()) { - if (groups.contains(g)) - { - groups.removeElement(g); - } + if (s == getSequenceAt(i)) + { + return i; + } + + i++; } - /** */ - public SequenceI findName(String name) + return -1; + } + + /* + * (non-Javadoc) + * + * @see + * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults) + */ + @Override + public int findIndex(SearchResults results) + { + int i = 0; + + while (i < sequences.size()) { - int i = 0; + if (results.involvesSequence(getSequenceAt(i))) + { + return i; + } + i++; + } + return -1; + } - while (i < sequences.size()) - { - if (getSequenceAt(i).getName().equals(name)) - { - return getSequenceAt(i); - } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + @Override + public int getHeight() + { + return sequences.size(); + } - i++; - } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + @Override + public int getWidth() + { + int maxLength = -1; - return null; + for (int i = 0; i < sequences.size(); i++) + { + if (getSequenceAt(i).getLength() > maxLength) + { + maxLength = getSequenceAt(i).getLength(); + } } - public SequenceI [] findSequenceMatch(String name) - { - Vector matches = new Vector(); - int i = 0; + return maxLength; + } - while (i < sequences.size()) + /** + * DOCUMENT ME! + * + * @param gc + * DOCUMENT ME! + */ + @Override + public void setGapCharacter(char gc) + { + gapCharacter = gc; + synchronized (sequences) + { + for (SequenceI seq : sequences) { - if (getSequenceAt(i).getName().equals(name)) - { - matches.addElement(getSequenceAt(i)); - } - i++; + seq.setSequence(seq.getSequenceAsString().replace('.', gc) + .replace('-', gc).replace(' ', gc)); } + } + } - SequenceI [] result = new SequenceI[matches.size()]; - for(i=0; i