ToggleInsertOverwriteHandler now also triggers enabling and disabling of ToggleLeftRi...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / molecular / AlignmentEditor.java
index 79d0ca257670f992155653132b24b17f9f549535..087b59c220582dabb73919dc781f16c851915cc5 100644 (file)
@@ -23,7 +23,7 @@ import info.bioinfweb.libralign.editsettings.EditSettingsChangeEvent;
 import info.bioinfweb.libralign.editsettings.EditSettingsListener;
 import info.bioinfweb.libralign.model.AlignmentModel;
 import info.bioinfweb.libralign.model.AlignmentModelChangeListener;
-import info.bioinfweb.libralign.model.SequenceUtils;
+import info.bioinfweb.libralign.model.AlignmentModelUtils;
 import info.bioinfweb.libralign.model.adapters.StringAdapter;
 import info.bioinfweb.libralign.model.events.SequenceChangeEvent;
 import info.bioinfweb.libralign.model.events.SequenceRenamedEvent;
@@ -277,7 +277,7 @@ public class AlignmentEditor extends EditorPart {
                // Set consensus sequence:
                AlignmentModel consensusProvider = getConsensusArea().getAlignmentModel();
                int id = consensusProvider.addSequence(CONSENSUS_NAME);
-               consensusProvider.insertTokensAt(id, 0, SequenceUtils.stringToTokenList(
+               consensusProvider.insertTokensAt(id, 0, AlignmentModelUtils.charSequenceToTokenList(
                                sequenceNode.getConsensusSequence().getString(), consensusProvider.getTokenSet()));
                //TODO Can the consensus sequence also be null? / Should it be created here, if nothing is in the DB?
     }
@@ -461,18 +461,60 @@ public class AlignmentEditor extends EditorPart {
     }
 
 
-    public void reverseComplementSelection() {
+    private String cutPherogram(boolean left) {
+        SelectionModel selection = getReadsArea().getSelection();
+        if (selection.getCursorHeight() != 1) {
+            return "Cutting pherograms is only possible if exactly one row is selected.";
+        }
+        else {
+            PherogramArea pherogramArea =
+                    getPherogramArea(getReadsArea().getSequenceOrder().idByIndex(selection.getCursorRow()));
+            if (pherogramArea == null) {
+                return "There is no pherogram attached to the current sequence.";
+            }
+            else {
+                if (left) {
+                    if (pherogramArea.setLeftCutPositionBySelection()) {
+                        return null;
+                    }
+                    else {
+                        return "The left end of the selection lies outside the pherogram attached to this sequence.";
+                    }
+                }
+                else {
+                    if (pherogramArea.setRightCutPositionBySelection()) {
+                        return null;
+                    }
+                    else {
+                        return "The right end of the selection lies outside the pherogram attached to this sequence.";
+                    }
+                }
+            }
+        }
+    }
+
+
+    public String cutPherogramLeft() {
+        return cutPherogram(true);
+    }
+
+
+    public String cutPherogramRight() {
+        return cutPherogram(false);
+    }
+
+
+    public void reverseComplementSelectedSequences() {
        SelectionModel selection = getReadsArea().getSelection();
-       AlignmentModel<?> provider = getReadsArea().getAlignmentModel();
-       for (int row = selection.getStartRow(); row < selection.getStartRow() + selection.getCursorHeight(); row++) {
+       AlignmentModel<?> model = getReadsArea().getAlignmentModel();
+       for (int row = selection.getFirstRow(); row < selection.getFirstRow() + selection.getCursorHeight(); row++) {
                        int sequenceID = getReadsArea().getSequenceOrder().idByIndex(row);
-                       //TODO rc edited sequence
-
-                       if (getPherogramArea(sequenceID).getPherogramModel() instanceof ReverseComplementPherogramModel) {
-                               //getPherogramArea(sequenceID).
-                               //TODO Allow to set new provider in PherogramArea or create new PherogramArea
-                               //TODO Reposition pherogram according to previous position in edited sequence and length
-                       }
+                       PherogramArea area = getPherogramArea(sequenceID);
+                       PherogramAlignmentModel pherogramAlignmentModel = area.getPherogramAlignmentModel();
+                       AlignmentModelUtils.reverseComplement(model, sequenceID,
+                               pherogramAlignmentModel.editableIndexByBaseCallIndex(area.getLeftCutPosition()).getBeforeValidIndex(),
+                               pherogramAlignmentModel.editableIndexByBaseCallIndex(area.getRightCutPosition()).getAfterValidIndex());
+                       area.reverseComplement();
                }
     }
 
@@ -539,7 +581,7 @@ public class AlignmentEditor extends EditorPart {
                // Set edited sequence:
                Collection<Object> tokens;  // First save tokens in a collection to avoid GUI updated for each token.
                if (editedSequence != null) {
-                       tokens = SequenceUtils.stringToTokenList(editedSequence, provider.getTokenSet());
+                       tokens = AlignmentModelUtils.charSequenceToTokenList(editedSequence, provider.getTokenSet());
                }
                else {  // Copy base call sequence into alignment:
                        tokens = new ArrayList<Object>();