Actions for creating and updating the consensus sequence in AlignmentEditor implemented.
authorb.stoever <b.stoever@localhost>
Fri, 19 Jun 2015 10:42:56 +0000 (10:42 +0000)
committerb.stoever <b.stoever@localhost>
Fri, 19 Jun 2015 10:42:56 +0000 (10:42 +0000)
.gitattributes
eu.etaxonomy.taxeditor.editor/plugin.xml
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/CreateConsensusSequenceHandler.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/UpdateConsensusSequenceHandler.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/package-info.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/molecular/AlignmentEditor.java

index 0a972b7a2fad9bb1d74a1e788df20e2c278abcbb..07f5c4eb6b69a74083263b680fe23561fab0f284 100644 (file)
@@ -427,6 +427,7 @@ eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handle
 eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/ToggleInsertOverwriteHandler.java -text
 eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/ToggleLeftRightInsertionHandler.java -text
 eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/UpdateConsensusSequenceHandler.java -text
+eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/package-info.java -text svneol=unset#text/plain
 eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/create/NewNameHandler.java -text
 eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/create/NewPersonHandler.java -text
 eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/create/NewReferenceHandler.java -text
index d365557a59c90a1e62767c3fb3510277ea475d22..9b1edc9bb89d269001f9e07dae7f6cfcec3e8c62 100644 (file)
                label="Alignment Editor">
             <command
                   commandId="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.loadPherogram"
-                  label="Load Pherogram"
                   style="push">
             </command>
             <command
             </command>
             <command
                   commandId="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.reverseComplementRows"
-                  label="Reverse complement selected rows"
                   mnemonic="r"
                   style="push">
             </command>
+            <command
+                  commandId="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.createConsensus"
+                  style="push">
+            </command>
+            <command
+                  commandId="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.updateConsensus"
+                  style="push">
+            </command>
             <visibleWhen
                   checkEnabled="true">
                <with
             class="eu.etaxonomy.taxeditor.editor.handler.alignmenteditor.ReverseComplementHandler"
             commandId="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.reverseComplementRows">
       </handler>
+      <handler
+            class="eu.etaxonomy.taxeditor.editor.handler.alignmenteditor.CreateConsensusSequenceHandler"
+            commandId="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.createConsensus">
+      </handler>
+      <handler
+            class="eu.etaxonomy.taxeditor.editor.handler.alignmenteditor.UpdateConsensusSequenceHandler"
+            commandId="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.updateConsensus">
+      </handler>
    </extension>
    <extension
          name="%extension.name"
             id="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.reverseComplementRows"
             name="Reverse complement selected rows">
       </command>
+      <command
+            id="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.createConsensus"
+            name="(Re)create consensus sequence">
+      </command>
+      <command
+            id="eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor.updateConsensus"
+            name="Update consensus sequence">
+      </command>
    </extension>
    <extension
          point="org.eclipse.core.expressions.definitions">
index 1424b3088f8d55484f04e4637e63dc05655a0269..2a2cc73262a29bf6e5293d01709ed779dc49c94d 100644 (file)
@@ -16,13 +16,18 @@ import eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor;
 \r
 \r
 \r
+/**\r
+ * Handler that creates the consensus sequence from all single read sequences in the active instance\r
+ * of {@link AlignmentEditor}. A previously present consensus sequence will be overwritten.\r
+ *\r
+ * @author Ben Stöver\r
+ * @date 19.06.2015\r
+ * @see UpdateConsensusSequenceHandler\r
+ * @see AlignmentEditor#createConsensusSequence()\r
+ */\r
 public class CreateConsensusSequenceHandler extends AbstractAlignmentEditorHandler {\r
-    /* (non-Javadoc)\r
-     * @see eu.etaxonomy.taxeditor.editor.handler.alignmenteditor.AbstractAlignmentEditorHandler#doExecute(org.eclipse.core.commands.ExecutionEvent, eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor)\r
-     */\r
     @Override\r
     public void doExecute(ExecutionEvent event, AlignmentEditor editor) throws ExecutionException {\r
-        // TODO Auto-generated method stub\r
-\r
+        editor.createConsensusSequence();\r
     }\r
 }\r
index 802c3b3e52beb40a28d6605a1de1d80576f8b0dd..6bcdf98d476c313d3fb100a9e5f6009cbf0d3fc6 100644 (file)
@@ -19,6 +19,6 @@ import eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor;
 public class UpdateConsensusSequenceHandler extends AbstractAlignmentEditorHandler {\r
     @Override\r
     public void doExecute(ExecutionEvent event, AlignmentEditor editor) throws ExecutionException {\r
-        //TODO implement\r
+        editor.updateConsensusSequence();\r
     }\r
 }\r
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/package-info.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/alignmenteditor/package-info.java
new file mode 100644 (file)
index 0000000..bd0508a
--- /dev/null
@@ -0,0 +1,17 @@
+// $Id$\r
+/**\r
+ * Copyright (C) 2015 EDIT\r
+ * European Distributed Institute of Taxonomy\r
+ * http://www.e-taxonomy.eu\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License Version 1.1\r
+ * See LICENSE.TXT at the top of this package for the full license terms.\r
+ */\r
+/**\r
+ * Contains handlers that trigger actions in an active instance of\r
+ * {@link eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor}. Handlers opening\r
+ * an alignment editor or pherogram view are not contained in here.\r
+ *\r
+ * @author Ben Stöver\r
+ */\r
+package eu.etaxonomy.taxeditor.editor.handler.alignmenteditor;
\ No newline at end of file
index 087b59c220582dabb73919dc781f16c851915cc5..440bc7e070b9401d3047644daec4edd1d1a6a230 100644 (file)
@@ -91,8 +91,10 @@ public class AlignmentEditor extends EditorPart {
     public static final String ID = "eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor";
 
        public static final int READS_AREA_INDEX = 1;
-       public static final int CONSENSUS_AREA_INDEX = READS_AREA_INDEX + 1;
+    public static final int EDITABLE_CONSENSUS_AREA_INDEX = READS_AREA_INDEX + 1;
+    public static final int CONSENSUS_HINT_AREA_INDEX = EDITABLE_CONSENSUS_AREA_INDEX + 1;
        public static final int PHEROGRAM_AREA_INDEX = 0;
+       public static final int CONSENSUS_DATA_AREA_INDEX = 0;
        public static final String DEFAULT_READ_NAME_PREFIX = "Read ";
        public static final String CONSENSUS_NAME = "Consensus";
 
@@ -204,7 +206,7 @@ public class AlignmentEditor extends EditorPart {
 
                AlignmentAreaList list = alignmentsContainer.getAlignmentAreas();
                AlignmentArea readsArea = createEditableAlignmentArea(alignmentsContainer, true);
-        list.add(createIndexArea(alignmentsContainer, readsArea));
+           list.add(createIndexArea(alignmentsContainer, readsArea));
                list.add(readsArea);  // Make sure READS_AREA_INDEX is correct.
                list.add(createEditableAlignmentArea(alignmentsContainer, false));  // Make sure COMSENSUS_AREA_INDEX is correct.
                list.add(createConsensusHintArea(alignmentsContainer, readsArea));
@@ -220,13 +222,21 @@ public class AlignmentEditor extends EditorPart {
     }
 
 
-    private AlignmentArea getConsensusArea() {
-       return getAlignmentsContainer().getAlignmentAreas().get(CONSENSUS_AREA_INDEX);
+    private AlignmentArea getEditableConsensusArea() {
+       return getAlignmentsContainer().getAlignmentAreas().get(EDITABLE_CONSENSUS_AREA_INDEX);
     }
 
 
     private PherogramArea getPherogramArea(int sequenceID) {
-       return (PherogramArea)getReadsArea().getDataAreas().getSequenceAreas(sequenceID).get(PHEROGRAM_AREA_INDEX);
+       return (PherogramArea)getReadsArea().getDataAreas().getSequenceAreas(sequenceID).get(
+               PHEROGRAM_AREA_INDEX);
+    }
+
+
+    private ConsensusSequenceArea getConsensusHintDataArea() {
+        return (ConsensusSequenceArea)getAlignmentsContainer().getAlignmentAreas().
+                get(CONSENSUS_HINT_AREA_INDEX).getDataAreas().getBottomAreas().
+                get(CONSENSUS_DATA_AREA_INDEX);
     }
 
 
@@ -237,14 +247,14 @@ public class AlignmentEditor extends EditorPart {
                        addRead(new File("D:/Users/BenStoever/ownCloud/Dokumente/Projekte/EDITor/Quelltexte/LibrAlign branch/Repository/eu.etaxonomy.taxeditor.editor/src/main/resources/AlignmentTestData/JR444_JR-P05.ab1").toURI(), false);
 
                        // Add test consensus sequence:
-                       AlignmentModel consensusProvider = getConsensusArea().getAlignmentModel();
-                       int id = consensusProvider.addSequence(CONSENSUS_NAME);
+                       AlignmentModel consensusModel = getEditableConsensusArea().getAlignmentModel();
+                       int id = consensusModel.addSequence(CONSENSUS_NAME);
                        Collection<Object> tokens = new ArrayList<Object>();  // First save tokens in a collection to avoid GUI updated for each token.
-                       tokens.add(consensusProvider.getTokenSet().tokenByKeyChar('A'));
-                       tokens.add(consensusProvider.getTokenSet().tokenByKeyChar('C'));
-                       tokens.add(consensusProvider.getTokenSet().tokenByKeyChar('G'));
-                       tokens.add(consensusProvider.getTokenSet().tokenByKeyChar('T'));
-                       consensusProvider.insertTokensAt(id, 0, tokens);
+                       tokens.add(consensusModel.getTokenSet().tokenByKeyChar('A'));
+                       tokens.add(consensusModel.getTokenSet().tokenByKeyChar('C'));
+                       tokens.add(consensusModel.getTokenSet().tokenByKeyChar('G'));
+                       tokens.add(consensusModel.getTokenSet().tokenByKeyChar('T'));
+                       consensusModel.insertTokensAt(id, 0, tokens);
                }
                catch (Exception e) {
                        throw new RuntimeException(e);
@@ -275,7 +285,7 @@ public class AlignmentEditor extends EditorPart {
                }
 
                // Set consensus sequence:
-               AlignmentModel consensusProvider = getConsensusArea().getAlignmentModel();
+               AlignmentModel consensusProvider = getEditableConsensusArea().getAlignmentModel();
                int id = consensusProvider.addSequence(CONSENSUS_NAME);
                consensusProvider.insertTokensAt(id, 0, AlignmentModelUtils.charSequenceToTokenList(
                                sequenceNode.getConsensusSequence().getString(), consensusProvider.getTokenSet()));
@@ -311,7 +321,7 @@ public class AlignmentEditor extends EditorPart {
        }
 
 
-  private void updateStatusBar() {
+    private void updateStatusBar() {
         IActionBars bars = getEditorSite().getActionBars();
         bars.getStatusLineManager().setMessage("Edit mode: " +
                        (getReadsArea().getEditSettings().isInsert() ? "Insert" : "Overwrite") + "  " +
@@ -342,12 +352,12 @@ public class AlignmentEditor extends EditorPart {
             monitor.beginTask(taskName, 3);
 
                Sequence sequenceNode = ((AlignmentEditorInput)getEditorInput()).getSequenceNode();
-               StringAdapter stringProvider = new StringAdapter(getConsensusArea().getAlignmentModel(), false);  // Throws an exception if a token has more than one character.
+               StringAdapter stringProvider = new StringAdapter(getEditableConsensusArea().getAlignmentModel(), false);  // Throws an exception if a token has more than one character.
 
                // Write consensus sequence:
                SequenceString consensusSequenceObj = sequenceNode.getConsensusSequence();
                String newConsensusSequence = stringProvider.getSequence(
-                               getConsensusArea().getAlignmentModel().sequenceIDByName(CONSENSUS_NAME));
+                               getEditableConsensusArea().getAlignmentModel().sequenceIDByName(CONSENSUS_NAME));
                if (consensusSequenceObj == null) {
                        sequenceNode.setConsensusSequence(SequenceString.NewInstance(newConsensusSequence));
                }
@@ -519,6 +529,61 @@ public class AlignmentEditor extends EditorPart {
     }
 
 
+    /**
+     * Recreates the whole consensus sequence from all single read sequences. The previous consensus
+     * sequence is overwritte.
+     */
+    @SuppressWarnings("unchecked")
+    public <T> void createConsensusSequence() {
+        ConsensusSequenceArea area = getConsensusHintDataArea();
+        AlignmentModel<T> model = (AlignmentModel<T>)getEditableConsensusArea().getAlignmentModel();
+        int sequenceID = model.sequenceIDIterator().next();  // There is always one sequence contained.
+        int length = getReadsArea().getAlignmentModel().getMaxSequenceLength();
+
+        Collection<T> tokens = new ArrayList<T>(length);
+        for (int column = 0; column < length; column++) {
+            tokens.add(model.getTokenSet().tokenByRepresentation(area.getConsensusToken(column)));
+        }
+
+        model.removeTokensAt(sequenceID, 0, model.getSequenceLength(sequenceID));
+        model.insertTokensAt(sequenceID, 0, tokens);
+    }
+
+
+    /**
+     * Updates the current consensus sequence by replacing gaps by the according consensus tokens
+     * calculated from the single read sequences and extends the consensus sequence if necessary.
+     */
+    @SuppressWarnings("unchecked")
+    public <T> void updateConsensusSequence() {
+        ConsensusSequenceArea area = getConsensusHintDataArea();
+        AlignmentModel<T> model = (AlignmentModel<T>)getEditableConsensusArea().getAlignmentModel();
+        TokenSet<T> tokenSet = model.getTokenSet();
+        int sequenceID = model.sequenceIDIterator().next();  // There is always one sequence contained.
+        int currentConsensusLength = model.getSequenceLength(sequenceID);
+        int overallLength = getReadsArea().getAlignmentModel().getMaxSequenceLength();
+
+        // Replace gaps by new information:
+        for (int column = 0; column < currentConsensusLength; column++) {
+            if (tokenSet.isGapToken(model.getTokenAt(sequenceID, column))) {
+                T newToken = tokenSet.tokenByRepresentation(area.getConsensusToken(column));
+                if (!tokenSet.isGapToken(newToken)) {
+                    model.setTokenAt(sequenceID, column, newToken);
+                }
+            }
+        }
+
+        // Append additional tokens:
+        if (overallLength > currentConsensusLength) {
+            Collection<T> tokens = new ArrayList<T>(overallLength);
+            for (int column = currentConsensusLength; column < overallLength; column++) {
+                tokens.add(tokenSet.tokenByRepresentation(area.getConsensusToken(column)));
+            }
+            model.appendTokens(sequenceID, tokens);
+        }
+    }
+
+
        public static PherogramModel readPherogram(URI uri) throws IOException, UnsupportedChromatogramFormatException {
                PherogramModel result;
                InputStream stream = uri.toURL().openStream();