From ebb678af128304dc13e1b6ec9e0961489bf83d39 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Tue, 24 Oct 2006 11:12:13 +0000 Subject: [PATCH] Commands which can be done, or undone --- src/jalview/commands/ChangeCaseCommand.java | 121 +++++++++++ src/jalview/commands/CommandI.java | 30 +++ src/jalview/commands/EditCommand.java | 285 +++++++++++++++++++++++++ src/jalview/commands/OrderCommand.java | 62 ++++++ src/jalview/commands/RemoveGapColCommand.java | 97 +++++++++ src/jalview/commands/RemoveGapsCommand.java | 111 ++++++++++ src/jalview/commands/TrimRegionCommand.java | 109 ++++++++++ 7 files changed, 815 insertions(+) create mode 100644 src/jalview/commands/ChangeCaseCommand.java create mode 100644 src/jalview/commands/CommandI.java create mode 100644 src/jalview/commands/EditCommand.java create mode 100644 src/jalview/commands/OrderCommand.java create mode 100644 src/jalview/commands/RemoveGapColCommand.java create mode 100644 src/jalview/commands/RemoveGapsCommand.java create mode 100644 src/jalview/commands/TrimRegionCommand.java diff --git a/src/jalview/commands/ChangeCaseCommand.java b/src/jalview/commands/ChangeCaseCommand.java new file mode 100644 index 0000000..ad5c937 --- /dev/null +++ b/src/jalview/commands/ChangeCaseCommand.java @@ -0,0 +1,121 @@ + /* + * 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 + */ +package jalview.commands; + +import jalview.datamodel.*; + +public class ChangeCaseCommand implements CommandI +{ + String description; + public static int TO_LOWER = 0; + public static int TO_UPPER = 1; + public static int TOGGLE_CASE = 2; + int caseChange = -1; + SequenceI [] seqs; + int [][] regions; + public ChangeCaseCommand(String description, + SequenceI[] seqs, + int [][] regions, + int caseChange) + { + this.description = description; + this.seqs = seqs; + this.regions = regions; + this.caseChange = caseChange; + doCommand(); + } + + public String getDescription() + { + return description; + } + + public int getSize() + { + return 1; + } + + public void doCommand() + { + changeCase(true); + } + + public void undoCommand() + { + changeCase(false); + } + + void changeCase(boolean doCommand) + { + String sequence; + int start, end; + char nextChar; + for (int r = 0; r < regions.length; r++) + { + start = regions[r][0]; + end = regions[r][1]; + for (int s = 0; s < seqs.length; s++) + { + sequence = seqs[s].getSequence(); + StringBuffer newSeq = new StringBuffer(); + + if (end > sequence.length()) + end = sequence.length(); + + if (start > 0) + { + newSeq.append(sequence.substring(0, start)); + } + + if ( (caseChange == TO_UPPER && doCommand) + || (caseChange == TO_LOWER && !doCommand)) + newSeq.append(sequence.substring(start, end).toUpperCase()); + + else if ( (caseChange == TO_LOWER && doCommand) + || (caseChange == TO_UPPER && !doCommand)) + newSeq.append(sequence.substring(start, end).toLowerCase()); + + else //TOGGLE CASE + { + for (int c = start; c < end; c++) + { + nextChar = sequence.charAt(c); + if ('a' <= nextChar && nextChar <= 'z') + { + // TO UPPERCASE !!! + nextChar -= ('a' - 'A'); + } + else if ('A' <= nextChar && nextChar <= 'Z') + { + // TO LOWERCASE !!! + nextChar += ('a' - 'A'); + } + newSeq.append(nextChar); + } + } + + if (end < sequence.length()) + newSeq.append(sequence.substring(end)); + + seqs[s].setSequence(newSeq.toString()); + } + } + } + +} diff --git a/src/jalview/commands/CommandI.java b/src/jalview/commands/CommandI.java new file mode 100644 index 0000000..e12a4c7 --- /dev/null +++ b/src/jalview/commands/CommandI.java @@ -0,0 +1,30 @@ +/* + * 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 + */ +package jalview.commands; + +public interface CommandI +{ + public void doCommand(); + + public void undoCommand(); + + public String getDescription(); + + public int getSize(); +} diff --git a/src/jalview/commands/EditCommand.java b/src/jalview/commands/EditCommand.java new file mode 100644 index 0000000..a53798a --- /dev/null +++ b/src/jalview/commands/EditCommand.java @@ -0,0 +1,285 @@ +/* + * 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 + */ +package jalview.commands; + +import jalview.datamodel.*; + +/** + * + *

Title: EditCommmand

+ * + *

Description: Essential information for performing + * undo and redo for cut/paste insert/delete gap + * which can be stored in the HistoryList

+ * + *

Copyright: Copyright (c) 2006

+ * + *

Company: Dundee University

+ * + * @author not attributable + * @version 1.0 + */ +public class EditCommand implements CommandI +{ + public static String INSERT_GAP = "InsertGap"; + public static String DELETE_GAP = "DeleteGap"; + public static String CUT = "Cut"; + public static String PASTE = "Paste"; + + Edit[] edits; + + String description; + + public EditCommand() + {} + + public EditCommand(String description) + { + this.description = description; + } + + public EditCommand(String description, + String command, + SequenceI[] seqs, + int position, + int number, + char gapChar) + { + this.description = description; + + if (command.equalsIgnoreCase(INSERT_GAP) + || command.equalsIgnoreCase(DELETE_GAP)) + { + edits = new Edit[] { new Edit(command, seqs, position, number, gapChar)}; + } + + performEdit(0); + } + + public EditCommand( String description, + String command, + SequenceI[] seqs, + int position, + int number, + AlignmentI al) + { + this.description = description; + if ( command.equalsIgnoreCase(CUT) || command.equalsIgnoreCase(PASTE)) + { + edits = new Edit[]{new Edit(command, seqs, position, number, al)}; + } + + performEdit(0); + } + + + public String getDescription() + { + return description; + } + + public int getSize() + { + return edits==null?0:edits.length; + } + + + public void appendEdit(String command, + SequenceI[] seqs, + int position, + int number, + char gapChar, + boolean performEdit) + { + Edit edit = new Edit(command, seqs, position, number, gapChar); + + if (edits != null) + { + Edit[] temp = new Edit[edits.length + 1]; + System.arraycopy(edits, 0, temp, 0, edits.length); + edits = temp; + edits[edits.length - 1] = edit; + } + else + edits = new Edit[] { edit }; + + if (performEdit) + performEdit(edits.length - 1); + } + + void performEdit(int commandIndex) + { + int eSize = edits.length; + for (int e = commandIndex; e < eSize; e++) + { + if (edits[e].command.equals(INSERT_GAP)) + { + insertGap(edits[e]); + } + else if (edits[e].command.equals(DELETE_GAP)) + { + deleteGap(edits[e]); + } + else if(edits[e].command.equals(CUT)) + { + cut(edits[e]); + } + else if(edits[e].command.equals(PASTE)) + { + paste(edits[e]); + } + } + } + + public void doCommand() + { + performEdit(0); + } + + public void undoCommand() + { + int e = 0, eSize = edits.length; + for (e = eSize-1; e > -1; e--) + { + if (edits[e].command.equals(INSERT_GAP)) + { + deleteGap(edits[e]); + } + else if (edits[e].command.equals(DELETE_GAP)) + { + insertGap(edits[e]); + } + else if (edits[e].command.equals(CUT)) + { + paste(edits[e]); + } + else if (edits[e].command.equals(PASTE)) + { + cut(edits[e]); + } + } + } + + void insertGap(Edit command) + { + for(int s=0; s i) + { + if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i))) + { + if (delete) + endCol = i; + + delete = false; + break; + } + } + } + + if (delete && startCol == -1) + { + startCol = i; + } + + if (!delete && startCol > -1) + { + this.appendEdit(DELETE_GAP, seqs, + startCol - deletedCols, + endCol - startCol, + gapChar, + false); + + deletedCols += (endCol - startCol); + startCol = -1; + endCol = -1; + } + } + + if (delete && startCol > -1) + { + //This is for empty columns at the + //end of the alignment + int width = end-endCol; + + if(endCol==-1) + width = end-start+1; + + + this.appendEdit(DELETE_GAP, seqs, + startCol - deletedCols, + width, + gapChar, + false); + } + + performEdit(0); + } + +} diff --git a/src/jalview/commands/RemoveGapsCommand.java b/src/jalview/commands/RemoveGapsCommand.java new file mode 100644 index 0000000..9343ec2 --- /dev/null +++ b/src/jalview/commands/RemoveGapsCommand.java @@ -0,0 +1,111 @@ +package jalview.commands; + /* + * 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 + */ + +import jalview.datamodel.*; + +public class RemoveGapsCommand extends EditCommand +{ + public RemoveGapsCommand( String description, + SequenceI[] seqs, char gapChar) + { + this.description = description; + int width = 0; + for(int i=0; iwidth) + width = seqs[i].getLength(); + + findGaps(seqs, 0, width, gapChar); + } + + public RemoveGapsCommand( String description, + SequenceI[] seqs, + int start, int end, char gapChar) + { + this.description = description; + findGaps(seqs, start, end, gapChar); + } + + void findGaps(SequenceI [] seqs, int start, int end, char gapChar) + { + + int startCol = -1, endCol = -1; + int deletedCols = 0; + + edits = new Edit[0]; + + boolean delete = true; + char [] sequence; + for(int s=0; s -1) + { + this.appendEdit(DELETE_GAP, new SequenceI[]{seqs[s]}, + startCol - deletedCols, + endCol - startCol, + gapChar, + false); + + deletedCols += (endCol - startCol); + startCol = -1; + endCol = -1; + } + } + if (delete && startCol > -1) + { + int width = end - endCol; + + if (endCol == -1) + width = end - start + 1; + + //This is the end of the region. + this.appendEdit(DELETE_GAP, new SequenceI[]{seqs[s]}, + startCol - deletedCols, + width, + gapChar, + false); + } + + } + + performEdit(0); + } + +} diff --git a/src/jalview/commands/TrimRegionCommand.java b/src/jalview/commands/TrimRegionCommand.java new file mode 100644 index 0000000..8ada376 --- /dev/null +++ b/src/jalview/commands/TrimRegionCommand.java @@ -0,0 +1,109 @@ +/* + * 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 + */ +package jalview.commands; + +import jalview.util.ShiftList; + +import jalview.datamodel.*; + +public class TrimRegionCommand + extends EditCommand +{ + public static String TRIM_LEFT = "TrimLeft"; + public static String TRIM_RIGHT = "TrimRight"; + + public ColumnSelection colSel = null; + + int [] start; + + public TrimRegionCommand(String description, + String command, + SequenceI[] seqs, + int column, + AlignmentI al, + ColumnSelection colSel) + { + this.description = description; + if (command.equalsIgnoreCase(TRIM_LEFT)) + { + edits = new Edit[] { new Edit(CUT, seqs, 0, column, al)}; + this.colSel = colSel; + } + else if (command.equalsIgnoreCase(TRIM_RIGHT)) + { + edits = new Edit[] + { new Edit(CUT, seqs, column+1, al.getWidth() - column, al)}; + } + + //We need to keep a record of the sequence start + //in order to restore the state after a redo + int i, isize = edits[0].seqs.length; + start = new int[isize]; + for(i=0; i