Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / editor / AlignmentEditor.java
index c1023e1c6c3266f7786bf7e2b0f6292f11d1c009..41a6b82e139ab0fe2c06b9315a91c183520c8bcc 100644 (file)
@@ -72,7 +72,6 @@ import eu.etaxonomy.cdm.model.molecular.Sequence;
 import eu.etaxonomy.cdm.model.molecular.SequenceString;
 import eu.etaxonomy.cdm.model.molecular.SingleRead;
 import eu.etaxonomy.cdm.model.molecular.SingleReadAlignment;
-import eu.etaxonomy.cdm.model.molecular.SingleReadAlignment.Shift;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.molecular.TaxeditorMolecularPlugin;
 import eu.etaxonomy.taxeditor.molecular.handler.ToggleInsertOverwriteHandler;
@@ -133,11 +132,11 @@ public class AlignmentEditor extends EditorPart {
 
 
     private MultipleAlignmentsContainer alignmentsContainer = null;
-    private final Map<Integer, SingleReadAlignment> cdmMap = new TreeMap<Integer, SingleReadAlignment>();  //TODO Move this to ContigSequenceDataProvider
+    private final Map<String, SingleReadAlignment> cdmMap = new TreeMap<String, SingleReadAlignment>();  //TODO Move this to ContigSequenceDataProvider
     private boolean dirty = false;
 
-    public AlignmentEditor()
-    {
+
+    public AlignmentEditor() {
        super();
        conversationHolder = CdmStore.createConversation();
        //conversationHolder = null;
@@ -186,9 +185,9 @@ public class AlignmentEditor extends EditorPart {
                result.setAllowVerticalScrolling(allowVerticalScrolling);
 
                CharacterTokenSet tokenSet = CharacterTokenSet.newDNAInstance();  //TODO Should NUCLEOTIDE be used instead?
-               AlignmentModel<Character> provider = new PackedAlignmentModel<Character>(tokenSet);
-               result.setAlignmentModel(provider, false);
-               provider.getChangeListeners().add(DIRTY_LISTENER);
+               AlignmentModel<Character> model = new PackedAlignmentModel<Character>(tokenSet);
+               result.setAlignmentModel(model, false);
+               model.getChangeListeners().add(DIRTY_LISTENER);
                result.getPaintSettings().getTokenPainterList().set(0, new NucleotideTokenPainter());
 
                return result;
@@ -274,12 +273,12 @@ public class AlignmentEditor extends EditorPart {
     }
 
 
-    public boolean hasPherogram(int sequenceID) {
+    public boolean hasPherogram(String sequenceID) {
         return getReadsArea().getDataAreas().getSequenceAreas(sequenceID).size() > PHEROGRAM_AREA_INDEX;
     }
 
 
-    public PherogramArea getPherogramArea(int sequenceID) {
+    public PherogramArea getPherogramArea(String sequenceID) {
         if (hasPherogram(sequenceID)) {
             return (PherogramArea)getReadsArea().getDataAreas().getSequenceAreas(sequenceID).get(PHEROGRAM_AREA_INDEX);
         }
@@ -306,7 +305,7 @@ public class AlignmentEditor extends EditorPart {
 
                        // Add test consensus sequence:
                        AlignmentModel consensusModel = getEditableConsensusArea().getAlignmentModel();
-                       int id = consensusModel.addSequence(CONSENSUS_NAME);
+                       String 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(consensusModel.getTokenSet().tokenByRepresentation("A"));
                        tokens.add(consensusModel.getTokenSet().tokenByRepresentation("C"));
@@ -327,12 +326,8 @@ public class AlignmentEditor extends EditorPart {
                for (SingleReadAlignment singleReadAlignment : sequenceNode.getSingleReadAlignments()) {
                        try {
                                SingleRead pherogramInfo = singleReadAlignment.getSingleRead();
-                               URI uri = null;
-                               if (pherogramInfo.getPherogram() != null) {
-                                   uri = MediaUtils.getFirstMediaRepresentationPart(pherogramInfo.getPherogram()).getUri();
-                               }
-                               int id = addRead(DerivateLabelProvider.getDerivateText(pherogramInfo, conversationHolder),
-                                               uri,
+                               String id = addRead(DerivateLabelProvider.getDerivateText(pherogramInfo, conversationHolder),
+                                               getPherogramURI(pherogramInfo),
                                                singleReadAlignment.isReverseComplement(),
                                                singleReadAlignment.getEditedSequence(),
                                                singleReadAlignment.getFirstSeqPosition(),
@@ -348,10 +343,10 @@ public class AlignmentEditor extends EditorPart {
                }
 
                // Set consensus sequence:
-               AlignmentModel consensusProvider = getEditableConsensusArea().getAlignmentModel();
-               int id = consensusProvider.addSequence(CONSENSUS_NAME);
-               consensusProvider.insertTokensAt(id, 0, AlignmentModelUtils.charSequenceToTokenList(
-                               sequenceNode.getConsensusSequence().getString(), consensusProvider.getTokenSet()));
+               AlignmentModel consensusModel = getEditableConsensusArea().getAlignmentModel();
+               String id = consensusModel.addSequence(CONSENSUS_NAME);
+               consensusModel.insertTokensAt(id, 0, AlignmentModelUtils.charSequenceToTokenList(
+                               sequenceNode.getConsensusSequence().getString(), consensusModel.getTokenSet()));
                //TODO Can the consensus sequence also be null? / Should it be created here, if nothing is in the DB?
     }
 
@@ -398,25 +393,25 @@ public class AlignmentEditor extends EditorPart {
         bars.getStatusLineManager().setMessage("Edit mode: " +
                        (getReadsArea().getEditSettings().isInsert() ? "Insert" : "Overwrite") + "  " +
                        "Insertion in pherogram: " +
-                       (getReadsArea().getEditSettings().isInsertLeftInDataArea() ? "Left" : "Right"));
+                       (getReadsArea().getEditSettings().isInsertLeftInDataArea() ? "Left" : "Right"));  //TODO multi language
     }
 
 
     private SingleReadAlignment.Shift[] convertToCDMShifts(PherogramAreaModel model) {
        Iterator<ShiftChange> iterator = model.shiftChangeIterator();
-       List<Shift> shifts = new ArrayList<SingleReadAlignment.Shift>();
+       List<SingleReadAlignment.Shift> shifts = new ArrayList<SingleReadAlignment.Shift>();
        while (iterator.hasNext()) {
                ShiftChange shiftChange = iterator.next();
                shifts.add(new SingleReadAlignment.Shift(shiftChange.getBaseCallIndex(), shiftChange.getShiftChange()));
        }
-       return shifts.toArray(new Shift[]{});
+       return shifts.toArray(new SingleReadAlignment.Shift[shifts.size()]);
     }
 
 
     @Override
     public void doSave(IProgressMonitor monitor) {
        if (getEditorInput() instanceof AlignmentEditorInput) {
-               String taskName = "Saving alignment";
+               String taskName = "Saving alignment";  //TODO multi language
             monitor.beginTask(taskName, 3);
 
             //re-loading sequence to avoid session conflicts
@@ -438,12 +433,12 @@ public class AlignmentEditor extends EditorPart {
                // Write single reads:
                stringProvider.setUnderlyingModel(getReadsArea().getAlignmentModel());
                sequenceNode.getSingleReadAlignments().retainAll(cdmMap.values());  // Remove all reads that are not in the alignment anymore.
-               Iterator<Integer> iterator = getReadsArea().getAlignmentModel().sequenceIDIterator();
+               Iterator<String> iterator = getReadsArea().getAlignmentModel().sequenceIDIterator();
                while (iterator.hasNext()) {
-                       int id = iterator.next();
+                       String id = iterator.next();
                        SingleReadAlignment singleRead = cdmMap.get(id);
                        if (singleRead == null) {
-                           throw new InternalError("Creating new reads from AlignmentEditor not implemented.");
+                           throw new InternalError("Creating new reads from AlignmentEditor not implemented.");  //TODO multi language
                                //TODO Create new read object. => Shall it be allowed to add reads in the alignment editor which are not represented in the CDM tree before the alignment editor is saved?
                                //singleRead = SingleReadAlignment.NewInstance(consensusSequence, singleRead, shifts, editedSequence);
                        }
@@ -542,13 +537,13 @@ public class AlignmentEditor extends EditorPart {
     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.";
+            return "Cutting pherograms is only possible if exactly one row is selected.";  //TODO multi language
         }
         else {
             PherogramArea pherogramArea =
                     getPherogramArea(getReadsArea().getSequenceOrder().idByIndex(selection.getCursorRow()));
             if (pherogramArea == null) {
-                return "There is no pherogram attached to the current sequence.";
+                return "There is no pherogram attached to the current sequence.";  //TODO multi language
             }
             else {
                 if (left) {
@@ -556,7 +551,7 @@ public class AlignmentEditor extends EditorPart {
                         return null;
                     }
                     else {
-                        return "The left end of the selection lies outside the pherogram attached to this sequence.";
+                        return "The left end of the selection lies outside the pherogram attached to this sequence.";  //TODO multi language
                     }
                 }
                 else {
@@ -564,7 +559,7 @@ public class AlignmentEditor extends EditorPart {
                         return null;
                     }
                     else {
-                        return "The right end of the selection lies outside the pherogram attached to this sequence.";
+                        return "The right end of the selection lies outside the pherogram attached to this sequence.";  //TODO multi language
                     }
                 }
             }
@@ -586,7 +581,7 @@ public class AlignmentEditor extends EditorPart {
        SelectionModel selection = getReadsArea().getSelection();
        AlignmentModel<?> model = getReadsArea().getAlignmentModel();
        for (int row = selection.getFirstRow(); row < selection.getFirstRow() + selection.getCursorHeight(); row++) {
-                       int sequenceID = getReadsArea().getSequenceOrder().idByIndex(row);
+               String sequenceID = getReadsArea().getSequenceOrder().idByIndex(row);
                        PherogramArea area = getPherogramArea(sequenceID);
                        PherogramAreaModel pherogramAlignmentModel = area.getModel();
                        AlignmentModelUtils.reverseComplement(model, sequenceID,
@@ -607,7 +602,7 @@ public class AlignmentEditor extends EditorPart {
     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.
+        String sequenceID = model.sequenceIDIterator().next();  // There is always one sequence contained.
         int length = getReadsArea().getAlignmentModel().getMaxSequenceLength();
 
         Collection<T> tokens = new ArrayList<T>(length);
@@ -629,7 +624,7 @@ public class AlignmentEditor extends EditorPart {
         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.
+        String sequenceID = model.sequenceIDIterator().next();  // There is always one sequence contained.
         int currentConsensusLength = model.getSequenceLength(sequenceID);
         int overallLength = getReadsArea().getAlignmentModel().getMaxSequenceLength();
 
@@ -669,9 +664,7 @@ public class AlignmentEditor extends EditorPart {
 
        private String newReadName() {
                int index = 1;
-               while (getReadsArea().getAlignmentModel().sequenceIDByName(DEFAULT_READ_NAME_PREFIX + index)
-                               != AlignmentModel.NO_SEQUENCE_FOUND) {
-
+               while (getReadsArea().getAlignmentModel().sequenceIDByName(DEFAULT_READ_NAME_PREFIX + index) != null) {
                        index++;
                }
                return DEFAULT_READ_NAME_PREFIX + index;
@@ -700,11 +693,11 @@ public class AlignmentEditor extends EditorPart {
      * @throws IOException if an error occurred when trying to read the pherogram file
      * @throws UnsupportedChromatogramFormatException if the format of the pherogram file is not supported
      */
-    public int addRead(String name, URI pherogramURI, boolean reverseComplemented, String editedSequence,
+    public String addRead(String name, URI pherogramURI, boolean reverseComplemented, String editedSequence,
             Integer firstSeqPos, Integer leftCutPos, Integer rightCutPos, SingleReadAlignment.Shift[] shifts)
             throws IOException, UnsupportedChromatogramFormatException {
 
-               AlignmentModel provider = getReadsArea().getAlignmentModel();
+               AlignmentModel model = getReadsArea().getAlignmentModel();
                PherogramProvider pherogramProvider = null;
                if (pherogramURI != null) {
                    pherogramProvider = readPherogram(pherogramURI);  // Must happen before a sequence is added, because it might throw an exception.
@@ -714,25 +707,25 @@ public class AlignmentEditor extends EditorPart {
                }
 
         // Create sequence:
-               provider.addSequence(name);
-               int id = provider.sequenceIDByName(name);
+               model.addSequence(name);
+               String id = model.sequenceIDByName(name);
 
                // Set edited sequence:
                Collection<Object> tokens = null;  // First save tokens in a collection to avoid GUI updated for each token.
                if (editedSequence != null) {
-                       tokens = AlignmentModelUtils.charSequenceToTokenList(editedSequence, provider.getTokenSet());
+                       tokens = AlignmentModelUtils.charSequenceToTokenList(editedSequence, model.getTokenSet());
                }
                else if (pherogramProvider != null) {  // Copy base call sequence into alignment:
                        tokens = new ArrayList<Object>();
                        for (int i = 0; i < pherogramProvider.getSequenceLength(); i++) {
-                               tokens.add(provider.getTokenSet().tokenByRepresentation(
+                               tokens.add(model.getTokenSet().tokenByRepresentation(
                                        Character.toString(pherogramProvider.getBaseCall(i))));
                        }
                        setDirty();
                }
 
                if (tokens != null) {  // If either an edited sequence or a pherogram URI was provided.
-                   provider.insertTokensAt(id, 0, tokens);
+                   model.insertTokensAt(id, 0, tokens);
 
                    if (pherogramProvider != null) {
                        // Create pherogram area:
@@ -740,16 +733,16 @@ public class AlignmentEditor extends EditorPart {
                                new PherogramAreaModel(pherogramProvider));
 
                        // Set position properties and shifts:
-                       PherogramAreaModel model = pherogramArea.getModel();
+                       PherogramAreaModel phergramModel = pherogramArea.getModel();
                        if ((firstSeqPos != null) && (leftCutPos != null)) {
-                           model.setFirstSeqLeftCutPos(firstSeqPos, leftCutPos);
+                           phergramModel.setFirstSeqLeftCutPos(firstSeqPos, leftCutPos);
                        }
                        if (rightCutPos != null) {
-                           model.setRightCutPosition(rightCutPos);
+                           phergramModel.setRightCutPosition(rightCutPos);
                        }
                        if ((shifts != null) && (shifts.length > 0)) {
                            for (int i = 0; i < shifts.length; i++) {
-                               model.addShiftChange(shifts[i].position, shifts[i].shift);
+                               phergramModel.addShiftChange(shifts[i].position, shifts[i].shift);
                            }
                            setDirty();
                        }
@@ -761,4 +754,14 @@ public class AlignmentEditor extends EditorPart {
                }
                return id;
        }
+
+
+    public static URI getPherogramURI(SingleRead pherogramInfo) {
+        if (pherogramInfo.getPherogram() != null) {
+            return MediaUtils.getFirstMediaRepresentationPart(pherogramInfo.getPherogram()).getUri();
+        }
+        else {
+            return null;
+        }
+    }
 }