X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSequence.java;h=2303d5d46fbc4de8a4f116899f1261f6210166a4;hb=7157dfd20efd2e1db47db1ac12e879eb51c67b92;hp=7df3eaf5c7c1dd18cc0be284cab4cf8634bf9991;hpb=0ff5a953c6b319333779bc27c9bcae5814530f66;p=jalview.git diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 7df3eaf..2303d5d 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -1,27 +1,26 @@ /* -* Jalview - A Sequence Alignment Editor and Viewer -* Copyright (C) 2006 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 + * 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 + * 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 java.awt.*; - import java.util.*; +import jalview.analysis.*; /** * DOCUMENT ME! @@ -29,11 +28,12 @@ import java.util.*; * @author $author$ * @version $Revision$ */ -public class Sequence implements SequenceI +public class Sequence + implements SequenceI { SequenceI datasetSequence; String name; - private String sequence; + private char[] sequence; String description; int start; int end; @@ -48,11 +48,6 @@ public class Sequence implements SequenceI /** DOCUMENT ME!! */ public SequenceFeature[] sequenceFeatures; - /** This array holds hidden sequences - * of which this sequence is the representitive member of a group - */ - SequenceGroup hiddenSequences; - /** * Creates a new Sequence object. * @@ -64,12 +59,20 @@ public class Sequence implements SequenceI public Sequence(String name, String sequence, int start, int end) { this.name = name; - this.sequence = sequence; + this.sequence = sequence.toCharArray(); this.start = start; this.end = end; - parseId(); + checkValidRange(); + } + public Sequence(String name, char[] sequence, int start, int end) + { + this.name = name; + this.sequence = sequence; + this.start = start; + this.end = end; + parseId(); checkValidRange(); } @@ -96,11 +99,9 @@ public class Sequence implements SequenceI if (end < 1) { int endRes = 0; - char ch; - for (int j = 0; j < sequence.length(); j++) + for (int j = 0; j < sequence.length; j++) { - ch = sequence.charAt(j); - if (!jalview.util.Comparison.isGap( (ch))) + if (!jalview.util.Comparison.isGap(sequence[j])) { endRes++; } @@ -133,7 +134,11 @@ public class Sequence implements SequenceI */ public Sequence(SequenceI seq) { - this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd()); + this(seq.getName(), + seq.getSequence(), + seq.getStart(), + seq.getEnd()); + description = seq.getDescription(); } /** @@ -170,10 +175,12 @@ public class Sequence implements SequenceI public void deleteFeature(SequenceFeature sf) { - if(sequenceFeatures==null) + if (sequenceFeatures == null) + { return; + } - int index=0; + int index = 0; for (index = 0; index < sequenceFeatures.length; index++) { if (sequenceFeatures[index].equals(sf)) @@ -182,25 +189,28 @@ public class Sequence implements SequenceI } } - - if(index==sequenceFeatures.length) + if (index == sequenceFeatures.length) + { return; + } int sfLength = sequenceFeatures.length; - if(sfLength<2) + if (sfLength < 2) { sequenceFeatures = null; } else { - SequenceFeature[] temp = new SequenceFeature[sfLength-1]; + SequenceFeature[] temp = new SequenceFeature[sfLength - 1]; System.arraycopy(sequenceFeatures, 0, temp, 0, index); - if(index= sequence.length()) + if (start >= sequence.length) { - return ""; + return new char[0]; } - if (end >= sequence.length()) + if (end >= sequence.length) { - end = sequence.length(); + end = sequence.length; } - return this.sequence.substring(start, end); + char[] reply = new char[end - start]; + System.arraycopy(sequence, start, reply, 0, end - start); + + return reply; } /** @@ -385,16 +405,27 @@ public class Sequence implements SequenceI public SequenceI getSubSequence(int start, int end) { if (start < 0) + { start = 0; - String seq = getSequence(start, end); - if (seq == "") + } + char[] seq = getSequence(start, end); + if (seq.length == 0) + { return null; + } int nstart = findPosition(start); int nend = findPosition(end) - 1; // JBPNote - this is an incomplete copy. SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend); nseq.setDescription(description); - nseq.setDatasetSequence(getDatasetSequence()); + if (datasetSequence != null) + { + nseq.setDatasetSequence(datasetSequence); + } + else + { + nseq.setDatasetSequence(this); + } return nseq; } @@ -407,9 +438,9 @@ public class Sequence implements SequenceI */ public char getCharAt(int i) { - if (i < sequence.length()) + if (i < sequence.length) { - return sequence.charAt(i); + return sequence[i]; } else { @@ -450,9 +481,9 @@ public class Sequence implements SequenceI int j = start; int i = 0; - while ( (i < sequence.length()) && (j <= end) && (j <= pos)) + while ( (i < sequence.length) && (j <= end) && (j <= pos)) { - if (!jalview.util.Comparison.isGap(sequence.charAt(i))) + if (!jalview.util.Comparison.isGap(sequence[i])) { j++; } @@ -481,10 +512,10 @@ public class Sequence implements SequenceI { int j = 0; int pos = start; - int seqlen = sequence.length(); + int seqlen = sequence.length; while ( (j < i) && (j < seqlen)) { - if (!jalview.util.Comparison.isGap( (sequence.charAt(j)))) + if (!jalview.util.Comparison.isGap(sequence[j])) { pos++; } @@ -503,14 +534,14 @@ public class Sequence implements SequenceI public int[] gapMap() { String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison. - GapChars, sequence); + GapChars, new String(sequence)); int[] map = new int[seq.length()]; int j = 0; int p = 0; - while (j < sequence.length()) + while (j < sequence.length) { - if (!jalview.util.Comparison.isGap(sequence.charAt(j))) + if (!jalview.util.Comparison.isGap(sequence[j])) { map[p++] = j; } @@ -525,38 +556,51 @@ public class Sequence implements SequenceI * DOCUMENT ME! * * @param i DOCUMENT ME! - */ - public void deleteCharAt(int i) - { - if (i >= sequence.length()) - { - return; - } - - sequence = sequence.substring(0, i) + sequence.substring(i + 1); - } - - /** - * DOCUMENT ME! - * - * @param i DOCUMENT ME! * @param j DOCUMENT ME! */ public void deleteChars(int i, int j) { - if (i >= sequence.length()) + if (i >= sequence.length) { return; } - if (j >= sequence.length()) + char[] tmp; + + if (j >= sequence.length) { - sequence = sequence.substring(0, i); + tmp = new char[i]; + System.arraycopy(sequence, 0, tmp, 0, i); } else { - sequence = sequence.substring(0, i) + sequence.substring(j); + tmp = new char[sequence.length - j + i]; + System.arraycopy(sequence, 0, tmp, 0, i); + System.arraycopy(sequence, j, tmp, i, sequence.length - j); + } + + if (this.datasetSequence != null) + { + for (int s = i; s < j; s++) + { + if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23) + { + + Sequence ds = new Sequence(name, + AlignSeq.extractGaps( + jalview.util.Comparison.GapChars, + this.getSequenceAsString() + ), + start, + end); + ds.setDescription(description); + } + break; + } } + + sequence = tmp; + } /** @@ -568,27 +612,31 @@ public class Sequence implements SequenceI */ public void insertCharAt(int i, int length, char c) { - StringBuffer tmp; + char[] tmp = new char[sequence.length + length]; - if (i >= sequence.length()) + if (i >= sequence.length) { - tmp = new StringBuffer(sequence); + System.arraycopy(sequence, 0, tmp, 0, sequence.length); + i = sequence.length; } else - tmp = new StringBuffer(sequence.substring(0, i)); + { + System.arraycopy(sequence, 0, tmp, 0, i); + } + int index = i; while (length > 0) { - tmp.append(c); + tmp[index++] = c; length--; } - if (i < sequence.length()) + if (i < sequence.length) { - tmp.append(sequence.substring(i)); + System.arraycopy(sequence, i, tmp, index, sequence.length - i); } - sequence = tmp.toString(); + sequence = tmp; } public void insertCharAt(int i, char c) @@ -619,17 +667,21 @@ public class Sequence implements SequenceI public void addDBRef(DBRefEntry entry) { if (dbrefs == null) + { dbrefs = new DBRefEntry[0]; + } int i, iSize = dbrefs.length; - for(i=0; i