X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FAlignment.java;h=04977e852fcd85d83afaee169af0423b76b7468f;hb=797df64fa2a0a30773d0f48f5494d4155e5a8be3;hp=1312e224498b9d54115c43c5e0cecc8d0906321d;hpb=506d60f0e188723ddc91c26824b41ac7034df3fe;p=jalview.git diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index 1312e22..04977e8 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -1,20 +1,19 @@ /* - * 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.7) + * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 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. * - * 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 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 . */ package jalview.datamodel; @@ -25,6 +24,10 @@ import jalview.analysis.*; /** * Data structure to hold and manipulate a multiple sequence alignment */ +/** + * @author JimP + * + */ public class Alignment implements AlignmentI { protected Alignment dataset; @@ -40,6 +43,8 @@ public class Alignment implements AlignmentI public static final int PROTEIN = 0; public static final int NUCLEOTIDE = 1; + + public boolean hasRNAStructure = false; /** DOCUMENT ME!! */ public AlignmentAnnotation[] annotations; @@ -84,7 +89,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 +105,7 @@ public class Alignment implements AlignmentI * appropriately. * * @param compactAlignment - * CigarArray + * CigarArray */ public static AlignmentI createAlignment(CigarArray compactAlignment) { @@ -134,13 +139,13 @@ public class Alignment implements AlignmentI * DOCUMENT ME! * * @param i - * DOCUMENT ME! + * DOCUMENT ME! * * @return DOCUMENT ME! */ public SequenceI getSequenceAt(int i) { - if (i < sequences.size()) + if (i>-1 && i < sequences.size()) { return (SequenceI) sequences.elementAt(i); } @@ -234,7 +239,7 @@ public class Alignment implements AlignmentI * DOCUMENT ME! * * @param s - * DOCUMENT ME! + * DOCUMENT ME! */ public void deleteSequence(SequenceI s) { @@ -245,7 +250,7 @@ public class Alignment implements AlignmentI * DOCUMENT ME! * * @param i - * DOCUMENT ME! + * DOCUMENT ME! */ public void deleteSequence(int i) { @@ -276,7 +281,7 @@ public class Alignment implements AlignmentI * DOCUMENT ME! * * @param s - * DOCUMENT ME! + * DOCUMENT ME! * * @return DOCUMENT ME! */ @@ -340,10 +345,71 @@ public class Alignment implements AlignmentI } /** - * 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; + } + } + public void deleteAllGroups() { + if (annotations != null) + { + removeAnnotationForGroup(null); + } groups.removeAllElements(); } @@ -352,6 +418,7 @@ public class Alignment implements AlignmentI { if (groups.contains(g)) { + removeAnnotationForGroup(g); groups.removeElement(g); } } @@ -376,7 +443,7 @@ public class Alignment implements AlignmentI * (non-Javadoc) * * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String, - * boolean) + * boolean) */ public SequenceI findName(SequenceI startAfter, String token, boolean b) { @@ -442,7 +509,11 @@ public class Alignment implements AlignmentI } - /** */ + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI) + */ public int findIndex(SequenceI s) { int i = 0; @@ -460,6 +531,27 @@ public class Alignment implements AlignmentI return -1; } + /* + * (non-Javadoc) + * + * @see + * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults) + */ + public int findIndex(SearchResults results) + { + int i = 0; + + while (i < sequences.size()) + { + if (results.involvesSequence(getSequenceAt(i))) + { + return i; + } + i++; + } + return -1; + } + /** * DOCUMENT ME! * @@ -494,7 +586,7 @@ public class Alignment implements AlignmentI * DOCUMENT ME! * * @param gc - * DOCUMENT ME! + * DOCUMENT ME! */ public void setGapCharacter(char gc) { @@ -503,8 +595,8 @@ public class Alignment implements AlignmentI for (int i = 0; i < sequences.size(); i++) { Sequence seq = (Sequence) sequences.elementAt(i); - seq.setSequence(seq.getSequenceAsString().replace('.', gc).replace( - '-', gc).replace(' ', gc)); + seq.setSequence(seq.getSequenceAsString().replace('.', gc) + .replace('-', gc).replace(' ', gc)); } } @@ -518,20 +610,36 @@ public class Alignment implements AlignmentI return gapCharacter; } - /** - * DOCUMENT ME! + /* + * (non-Javadoc) * - * @return DOCUMENT ME! + * @see jalview.datamodel.AlignmentI#isAligned() */ public boolean isAligned() { - int width = getWidth(); + return isAligned(false); + } + /* + * (non-Javadoc) + * + * @see jalview.datamodel.AlignmentI#isAligned(boolean) + */ + public boolean isAligned(boolean includeHidden) + { + int width = getWidth(); + if (hiddenSequences == null || hiddenSequences.getSize() == 0) + { + includeHidden = true; // no hidden sequences to check against. + } for (int i = 0; i < sequences.size(); i++) { - if (getSequenceAt(i).getLength() != width) + if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i))) { - return false; + if (getSequenceAt(i).getLength() != width) + { + return false; + } } } @@ -541,10 +649,16 @@ public class Alignment implements AlignmentI /* * (non-Javadoc) * - * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation) + * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel. + * AlignmentAnnotation) */ public boolean deleteAnnotation(AlignmentAnnotation aa) { + return deleteAnnotation(aa, true); + } + + public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook) + { int aSize = 1; if (annotations != null) @@ -576,20 +690,54 @@ public class Alignment implements AlignmentI if (swap) { annotations = temp; - if (aa.sequenceRef != null) - aa.sequenceRef.removeAlignmentAnnotation(aa); + if (unhook) { + unhookAnnotation(aa); + } } return swap; } /** - * DOCUMENT ME! + * remove any object references associated with this annotation * * @param aa - * DOCUMENT ME! + */ + private void unhookAnnotation(AlignmentAnnotation aa) + { + if (aa.sequenceRef != null) + { + aa.sequenceRef.removeAlignmentAnnotation(aa); + } + if (aa.groupRef != null) + { + // probably need to do more here in the future (post 2.5.0) + aa.groupRef = null; + } + } + + /* + * (non-Javadoc) + * + * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel. + * AlignmentAnnotation) */ public void addAnnotation(AlignmentAnnotation aa) { + addAnnotation(aa, -1); + } + + /* + * (non-Javadoc) + * + * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel. + * AlignmentAnnotation, int) + */ + public void addAnnotation(AlignmentAnnotation aa, int pos) + { + if(aa.getRNAStruc()!= null){ + hasRNAStructure=true; + } + int aSize = 1; if (annotations != null) { @@ -597,16 +745,28 @@ public class Alignment implements AlignmentI } AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize]; - - temp[aSize - 1] = aa; - int i = 0; - + if (pos == -1 || pos >= aSize) + { + temp[aSize - 1] = aa; + } + else + { + temp[pos] = aa; + } if (aSize > 1) { - for (i = 0; i < (aSize - 1); i++) + int p = 0; + for (i = 0; i < (aSize - 1); i++, p++) { - temp[i] = annotations[i]; + if (p == pos) + { + p++; + } + if (p < temp.length) + { + temp[p] = annotations[i]; + } } } @@ -678,6 +838,11 @@ public class Alignment implements AlignmentI return false; } } + + public boolean hasRNAStructure(){ + //TODO can it happen that structure is removed from alignment? + return hasRNAStructure; + } public void setDataset(Alignment data) { @@ -771,6 +936,111 @@ public class Alignment implements AlignmentI return modified; } + /** + * Justify the sequences to the left or right by deleting and inserting gaps + * before the initial residue or after the terminal residue + * + * @param right + * true if alignment padded to right, false to justify to left + * @return true if alignment was changed + */ + public boolean justify(boolean right) + { + boolean modified = false; + + // Remove excess gaps from the end of alignment + int maxLength = -1; + int ends[] = new int[sequences.size() * 2]; + SequenceI current; + for (int i = 0; i < sequences.size(); i++) + { + current = getSequenceAt(i); + // This should really be a sequence method + ends[i * 2] = current.findIndex(current.getStart()); + ends[i * 2 + 1] = current.findIndex(current.getStart() + + current.getLength()); + boolean hitres = false; + for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++) + { + if (!jalview.util.Comparison.isGap(current.getCharAt(j))) + { + if (!hitres) + { + ends[i * 2] = j; + hitres = true; + } + else + { + ends[i * 2 + 1] = j; + if (j - ends[i * 2] > maxLength) + { + maxLength = j - ends[i * 2]; + } + } + } + } + } + + maxLength++; + // now edit the flanking gaps to justify to either left or right + int cLength, extent, diff; + for (int i = 0; i < sequences.size(); i++) + { + current = getSequenceAt(i); + + cLength = 1 + ends[i * 2 + 1] - ends[i * 2]; + diff = maxLength - cLength; // number of gaps to indent + extent = current.getLength(); + if (right) + { + // right justify + if (extent > ends[i * 2 + 1]) + { + current.deleteChars(ends[i * 2 + 1] + 1, extent); + modified = true; + } + if (ends[i * 2] > diff) + { + current.deleteChars(0, ends[i * 2] - diff); + modified = true; + } + else + { + if (ends[i * 2] < diff) + { + current.insertCharAt(0, diff - ends[i * 2], gapCharacter); + modified = true; + } + } + } + else + { + // left justify + if (ends[i * 2] > 0) + { + current.deleteChars(0, ends[i * 2]); + modified = true; + ends[i * 2 + 1] -= ends[i * 2]; + extent -= ends[i * 2]; + } + if (extent > maxLength) + { + current.deleteChars(maxLength + 1, extent); + modified = true; + } + else + { + if (extent < maxLength) + { + current.insertCharAt(extent, maxLength - extent, gapCharacter); + modified = true; + } + } + } + } + return modified; + } + public HiddenSequences getHiddenSequences() { return hiddenSequences; @@ -814,7 +1084,9 @@ public class Alignment implements AlignmentI /* * (non-Javadoc) * - * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame) + * @see + * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame + * ) */ public void addCodonFrame(AlignedCodonFrame codons) { @@ -845,7 +1117,8 @@ public class Alignment implements AlignmentI /* * (non-Javadoc) * - * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI) + * @see + * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI) */ public AlignedCodonFrame[] getCodonFrame(SequenceI seq) { @@ -877,7 +1150,8 @@ public class Alignment implements AlignmentI /* * (non-Javadoc) * - * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame) + * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel. + * AlignedCodonFrame) */ public boolean removeCodonFrame(AlignedCodonFrame codons) { @@ -904,4 +1178,119 @@ public class Alignment implements AlignmentI } return removed; } + + public void append(AlignmentI toappend) + { + // TODO test this method for a future 2.5 release + // currently tested for use in jalview.gui.SequenceFetcher + boolean samegap = toappend.getGapCharacter() == getGapCharacter(); + char oldc = toappend.getGapCharacter(); + boolean hashidden = toappend.getHiddenSequences() != null + && toappend.getHiddenSequences().hiddenSequences != null; + // get all sequences including any hidden ones + Vector sqs = (hashidden) ? toappend.getHiddenSequences() + .getFullAlignment().getSequences() : toappend.getSequences(); + if (sqs != null) + { + Enumeration sq = sqs.elements(); + while (sq.hasMoreElements()) + { + SequenceI addedsq = (SequenceI) sq.nextElement(); + if (!samegap) + { + char[] oldseq = addedsq.getSequence(); + for (int c = 0; c < oldseq.length; c++) + { + if (oldseq[c] == oldc) + { + oldseq[c] = gapCharacter; + } + } + } + addSequence(addedsq); + } + } + AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation(); + for (int a = 0; alan != null && a < alan.length; a++) + { + addAnnotation(alan[a]); + } + AlignedCodonFrame[] acod = toappend.getCodonFrames(); + for (int a = 0; acod != null && a < acod.length; a++) + { + this.addCodonFrame(acod[a]); + } + Vector sg = toappend.getGroups(); + if (sg != null) + { + Enumeration el = sg.elements(); + while (el.hasMoreElements()) + { + addGroup((SequenceGroup) el.nextElement()); + } + } + if (toappend.getHiddenSequences() != null) + { + HiddenSequences hs = toappend.getHiddenSequences(); + if (hiddenSequences == null) + { + hiddenSequences = new HiddenSequences(this); + } + if (hs.hiddenSequences != null) + { + for (int s = 0; s < hs.hiddenSequences.length; s++) + { + // hide the newly appended sequence in the alignment + if (hs.hiddenSequences[s] != null) + { + hiddenSequences.hideSequence(hs.hiddenSequences[s]); + } + } + } + } + if (toappend.getProperties() != null) + { + // we really can't do very much here - just try to concatenate strings + // where property collisions occur. + Enumeration key = toappend.getProperties().keys(); + while (key.hasMoreElements()) + { + Object k = key.nextElement(); + Object ourval = this.getProperty(k); + Object toapprop = toappend.getProperty(k); + if (ourval != null) + { + if (ourval.getClass().equals(toapprop.getClass()) + && !ourval.equals(toapprop)) + { + if (ourval instanceof String) + { + // append strings + this.setProperty(k, ((String) ourval) + "; " + + ((String) toapprop)); + } + else + { + if (ourval instanceof Vector) + { + // append vectors + Enumeration theirv = ((Vector) toapprop).elements(); + while (theirv.hasMoreElements()) + { + ((Vector) ourval).addElement(theirv); + } + } + } + } + } + else + { + // just add new property directly + setProperty(k, toapprop); + } + + } + } + } + }