X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FAlignment.java;h=5d97fe3bd3759235e7ad84571297bc079157e568;hb=b2f9a8d7bce642ff4011bc6d49e02bb0569fbb11;hp=08c04f5b779f3b73c27cdba8baf081912a48d199;hpb=961b10e4c895aadb7fc9df82a641c6c53c0d5c70;p=jalview.git diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index 08c04f5..5d97fe3 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -1,37 +1,40 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4) - * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1) + * Copyright (C) 2014 The Jalview Authors * - * 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 file is part of Jalview. * - * 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. + * 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * 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 java.util.*; -import jalview.analysis.*; - /** * Data structure to hold and manipulate a multiple sequence alignment */ +/** + * @author JimP + * + */ public class Alignment implements AlignmentI { protected Alignment dataset; - protected Vector sequences; + protected List sequences; - protected Vector groups = new Vector(); + protected List groups = java.util.Collections + .synchronizedList(new ArrayList()); protected char gapCharacter = '-'; @@ -41,6 +44,8 @@ public class Alignment implements AlignmentI public static final int NUCLEOTIDE = 1; + public boolean hasRNAStructure = false; + /** DOCUMENT ME!! */ public AlignmentAnnotation[] annotations; @@ -61,11 +66,12 @@ public class Alignment implements AlignmentI type = PROTEIN; } - sequences = new Vector(); + sequences = java.util.Collections + .synchronizedList(new ArrayList()); for (i = 0; i < seqs.length; i++) { - sequences.addElement(seqs[i]); + sequences.add(seqs[i]); } } @@ -84,7 +90,7 @@ public class Alignment implements AlignmentI * Make a new alignment from an array of SeqCigars * * @param seqs - * SeqCigar[] + * SeqCigar[] */ public Alignment(SeqCigar[] alseqs) { @@ -100,7 +106,7 @@ public class Alignment implements AlignmentI * appropriately. * * @param compactAlignment - * CigarArray + * CigarArray */ public static AlignmentI createAlignment(CigarArray compactAlignment) { @@ -113,38 +119,50 @@ public class Alignment implements AlignmentI * * @return DOCUMENT ME! */ - public Vector getSequences() + @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; - SequenceI[] reply = new SequenceI[sequences.size()]; - for (int i = 0; i < sequences.size(); i++) + synchronized (sequences) { - reply[i] = (SequenceI) sequences.elementAt(i); + return sequences.toArray(new SequenceI[sequences.size()]); } - return reply; } /** * DOCUMENT ME! * * @param i - * DOCUMENT ME! + * DOCUMENT ME! * * @return DOCUMENT ME! */ + @Override public SequenceI getSequenceAt(int i) { - if (i < sequences.size()) + synchronized (sequences) { - return (SequenceI) sequences.elementAt(i); + if (i > -1 && i < sequences.size()) + { + return sequences.get(i); + } } - return null; } @@ -153,6 +171,7 @@ public class Alignment implements AlignmentI * * @param snew */ + @Override public void addSequence(SequenceI snew) { if (dataset != null) @@ -177,7 +196,10 @@ public class Alignment implements AlignmentI } else { - sequences.addElement(snew); + synchronized (sequences) + { + sequences.add(snew); + } } if (hiddenSequences != null) hiddenSequences.adjustHeightSequenceAdded(); @@ -188,12 +210,15 @@ public class Alignment implements AlignmentI * * @param snew */ + @Override public void setSequenceAt(int i, SequenceI snew) { SequenceI oldseq = getSequenceAt(i); - deleteSequence(oldseq); - - sequences.setElementAt(snew, i); + deleteSequence(i); + synchronized (sequences) + { + sequences.set(i, snew); + } } /** @@ -201,11 +226,13 @@ public class Alignment implements AlignmentI * * @return DOCUMENT ME! */ - public Vector getGroups() + @Override + public List getGroups() { return groups; } + @Override public void finalize() { if (getDataset() != null) @@ -234,8 +261,9 @@ public class Alignment implements AlignmentI * DOCUMENT ME! * * @param s - * DOCUMENT ME! + * DOCUMENT ME! */ + @Override public void deleteSequence(SequenceI s) { deleteSequence(findIndex(s)); @@ -245,118 +273,203 @@ public class Alignment implements AlignmentI * DOCUMENT ME! * * @param i - * DOCUMENT ME! + * DOCUMENT ME! */ + @Override public void deleteSequence(int i) { if (i > -1 && i < getHeight()) { - sequences.removeElementAt(i); + synchronized (sequences) + { + sequences.remove(i); + } hiddenSequences.adjustHeightSequenceDeleted(i); } } - /** */ + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI) + */ + @Override public SequenceGroup findGroup(SequenceI s) { - for (int i = 0; i < this.groups.size(); i++) + synchronized (groups) { - SequenceGroup sg = (SequenceGroup) groups.elementAt(i); - - if (sg.getSequences(null).contains(s)) + for (int i = 0; i < this.groups.size(); i++) { - return sg; + SequenceGroup sg = groups.get(i); + + if (sg.getSequences(null).contains(s)) + { + return sg; + } } } - return null; } - /** - * DOCUMENT ME! - * - * @param s - * DOCUMENT ME! + /* + * (non-Javadoc) * - * @return DOCUMENT ME! + * @see + * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI) */ + @Override public SequenceGroup[] findAllGroups(SequenceI s) { - Vector temp = new Vector(); + ArrayList temp = new ArrayList(); - int gSize = groups.size(); - for (int i = 0; i < gSize; i++) + synchronized (groups) { - SequenceGroup sg = (SequenceGroup) groups.elementAt(i); - if (sg == null || sg.getSequences(null) == null) + int gSize = groups.size(); + for (int i = 0; i < gSize; i++) { - this.deleteGroup(sg); - gSize--; - continue; - } + SequenceGroup sg = groups.get(i); + if (sg == null || sg.getSequences(null) == null) + { + this.deleteGroup(sg); + gSize--; + continue; + } - if (sg.getSequences(null).contains(s)) - { - temp.addElement(sg); + if (sg.getSequences(null).contains(s)) + { + temp.add(sg); + } } } - SequenceGroup[] ret = new SequenceGroup[temp.size()]; - - for (int i = 0; i < temp.size(); i++) - { - ret[i] = (SequenceGroup) temp.elementAt(i); - } - - return ret; + return temp.toArray(ret); } /** */ + @Override public void addGroup(SequenceGroup sg) { - if (!groups.contains(sg)) + synchronized (groups) { - if (hiddenSequences.getSize() > 0) + if (!groups.contains(sg)) { - 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++) { - sg.deleteSequence(sg.getSequenceAt(i), false); - iSize--; - i--; + if (!sequences.contains(sg.getSequenceAt(i))) + { + sg.deleteSequence(sg.getSequenceAt(i), false); + iSize--; + i--; + } } - } - if (sg.getSize() < 1) - { - return; + if (sg.getSize() < 1) + { + return; + } } + sg.setContext(this); + groups.add(sg); } - - groups.addElement(sg); } } /** - * DOCUMENT ME! + * 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) + { + 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) + { + todelete[p++] = annotations[i]; + } + else + { + tokeep[k++] = annotations[i]; + } + } + } + else + { + for (i = 0, p = 0, k = 0; i < annotations.length; i++) + { + if (annotations[i].groupRef == gp) + { + todelete[p++] = annotations[i]; + } + else + { + tokeep[k++] = annotations[i]; + } + } + } + if (p > 0) + { + // 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; + } + } + + @Override public void deleteAllGroups() { - groups.removeAllElements(); + synchronized (groups) + { + if (annotations != null) + { + removeAnnotationForGroup(null); + } + for (SequenceGroup sg:groups) { + sg.setContext(null); + } + groups.clear(); + } } /** */ + @Override public void deleteGroup(SequenceGroup g) { - if (groups.contains(g)) + synchronized (groups) { - groups.removeElement(g); + if (groups.contains(g)) + { + removeAnnotationForGroup(g); + groups.remove(g); + g.setContext(null); + } } } /** */ + @Override public SequenceI findName(String name) { return findName(name, false); @@ -367,6 +480,7 @@ public class Alignment implements AlignmentI * * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean) */ + @Override public SequenceI findName(String token, boolean b) { return findName(null, token, b); @@ -376,8 +490,9 @@ public class Alignment implements AlignmentI * (non-Javadoc) * * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String, - * boolean) + * boolean) */ + @Override public SequenceI findName(SequenceI startAfter, String token, boolean b) { @@ -418,6 +533,7 @@ public class Alignment implements AlignmentI return null; } + @Override public SequenceI[] findSequenceMatch(String name) { Vector matches = new Vector(); @@ -441,9 +557,13 @@ public class Alignment implements AlignmentI return result; } - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI) */ + @Override public int findIndex(SequenceI s) { int i = 0; @@ -460,14 +580,19 @@ public class Alignment implements AlignmentI return -1; } - /* (non-Javadoc) - * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults) + + /* + * (non-Javadoc) + * + * @see + * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults) */ + @Override public int findIndex(SearchResults results) { - int i=0; - - while (i