1
|
/**
|
2
|
* Copyright (C) 2014 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.molecular.editor.e4;
|
10
|
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.IOException;
|
14
|
import java.io.InputStream;
|
15
|
import java.net.URI;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.Collection;
|
18
|
import java.util.Collections;
|
19
|
import java.util.Iterator;
|
20
|
import java.util.List;
|
21
|
import java.util.Map;
|
22
|
import java.util.TreeMap;
|
23
|
|
24
|
import javax.annotation.PostConstruct;
|
25
|
import javax.annotation.PreDestroy;
|
26
|
import javax.inject.Inject;
|
27
|
|
28
|
import org.biojava.bio.chromatogram.ChromatogramFactory;
|
29
|
import org.biojava.bio.chromatogram.UnsupportedChromatogramFormatException;
|
30
|
import org.eclipse.core.runtime.IProgressMonitor;
|
31
|
import org.eclipse.e4.ui.di.Focus;
|
32
|
import org.eclipse.e4.ui.di.Persist;
|
33
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
34
|
import org.eclipse.swt.SWT;
|
35
|
import org.eclipse.swt.dnd.Clipboard;
|
36
|
import org.eclipse.swt.widgets.Composite;
|
37
|
import org.eclipse.swt.widgets.Display;
|
38
|
import org.eclipse.ui.PartInitException;
|
39
|
import org.eclipse.ui.PlatformUI;
|
40
|
import org.eclipse.ui.commands.ICommandService;
|
41
|
|
42
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
43
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
44
|
import eu.etaxonomy.cdm.model.media.MediaUtils;
|
45
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
46
|
import eu.etaxonomy.cdm.model.molecular.SequenceString;
|
47
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
48
|
import eu.etaxonomy.cdm.model.molecular.SingleReadAlignment;
|
49
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
50
|
import eu.etaxonomy.taxeditor.molecular.TaxeditorMolecularPlugin;
|
51
|
import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditorActionUpdater;
|
52
|
import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditorInput;
|
53
|
import eu.etaxonomy.taxeditor.molecular.editor.PherogramMouseListener;
|
54
|
import eu.etaxonomy.taxeditor.molecular.handler.ToggleInsertOverwriteHandler;
|
55
|
import eu.etaxonomy.taxeditor.molecular.handler.ToggleLeftRightInsertionHandler;
|
56
|
import eu.etaxonomy.taxeditor.molecular.l10n.Messages;
|
57
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
58
|
import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
|
59
|
import info.bioinfweb.commons.swt.SWTUtils;
|
60
|
import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
|
61
|
import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
|
62
|
import info.bioinfweb.libralign.alignmentarea.tokenpainter.NucleotideTokenPainter;
|
63
|
import info.bioinfweb.libralign.dataarea.implementations.ConsensusSequenceArea;
|
64
|
import info.bioinfweb.libralign.dataarea.implementations.pherogram.PherogramArea;
|
65
|
import info.bioinfweb.libralign.dataarea.implementations.sequenceindex.SequenceIndexArea;
|
66
|
import info.bioinfweb.libralign.editsettings.EditSettingsChangeEvent;
|
67
|
import info.bioinfweb.libralign.editsettings.EditSettingsListener;
|
68
|
import info.bioinfweb.libralign.model.AlignmentModel;
|
69
|
import info.bioinfweb.libralign.model.AlignmentModelChangeListener;
|
70
|
import info.bioinfweb.libralign.model.adapters.StringAdapter;
|
71
|
import info.bioinfweb.libralign.model.events.SequenceChangeEvent;
|
72
|
import info.bioinfweb.libralign.model.events.SequenceRenamedEvent;
|
73
|
import info.bioinfweb.libralign.model.events.TokenChangeEvent;
|
74
|
import info.bioinfweb.libralign.model.implementations.PackedAlignmentModel;
|
75
|
import info.bioinfweb.libralign.model.tokenset.CharacterTokenSet;
|
76
|
import info.bioinfweb.libralign.model.tokenset.TokenSet;
|
77
|
import info.bioinfweb.libralign.model.utils.AlignmentModelUtils;
|
78
|
import info.bioinfweb.libralign.multiplealignments.AlignmentAreaList;
|
79
|
import info.bioinfweb.libralign.multiplealignments.MultipleAlignmentsContainer;
|
80
|
import info.bioinfweb.libralign.pherogram.model.PherogramAlignmentRelation;
|
81
|
import info.bioinfweb.libralign.pherogram.model.PherogramAreaModel;
|
82
|
import info.bioinfweb.libralign.pherogram.model.ShiftChange;
|
83
|
import info.bioinfweb.libralign.pherogram.provider.BioJavaPherogramProvider;
|
84
|
import info.bioinfweb.libralign.pherogram.provider.PherogramProvider;
|
85
|
import info.bioinfweb.libralign.pherogram.provider.ReverseComplementPherogramProvider;
|
86
|
import info.bioinfweb.tic.SWTComponentFactory;
|
87
|
|
88
|
|
89
|
|
90
|
/**
|
91
|
* Editor component to edit a contig alignment used to combine different overlapping pherograms from Sanger sequencing to
|
92
|
* a consensus sequence.
|
93
|
* <p>
|
94
|
* The contained GUI components used to edit the alignment come from <a href="http://bioinfweb.info/LibrAlign/">LibrAlign</a>.
|
95
|
*
|
96
|
* @author Ben Stöver
|
97
|
* @author pplitzner
|
98
|
* @date 04.08.2014
|
99
|
*/
|
100
|
public class AlignmentEditorE4 {
|
101
|
public static final String ID = "eu.etaxonomy.taxeditor.molecular.AlignmentEditor"; //$NON-NLS-1$
|
102
|
|
103
|
public static final int READS_AREA_INDEX = 1;
|
104
|
public static final int EDITABLE_CONSENSUS_AREA_INDEX = READS_AREA_INDEX + 1;
|
105
|
public static final int CONSENSUS_HINT_AREA_INDEX = EDITABLE_CONSENSUS_AREA_INDEX + 1;
|
106
|
public static final int PHEROGRAM_AREA_INDEX = 0;
|
107
|
public static final int CONSENSUS_DATA_AREA_INDEX = 0;
|
108
|
public static final String DEFAULT_READ_NAME_PREFIX = "Read "; //$NON-NLS-1$
|
109
|
public static final String CONSENSUS_NAME = "Consensus"; //$NON-NLS-1$
|
110
|
|
111
|
|
112
|
private ConversationHolder conversationHolder;
|
113
|
private final AlignmentModelChangeListener DIRTY_LISTENER = new AlignmentModelChangeListener() {
|
114
|
@Override
|
115
|
public <T> void afterTokenChange(TokenChangeEvent<T> e) {
|
116
|
setDirty();
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public <T> void afterSequenceRenamed(SequenceRenamedEvent<T> e) {
|
121
|
setDirty();
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public <T> void afterSequenceChange(SequenceChangeEvent<T> e) {
|
126
|
setDirty();
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
public <T, U> void afterProviderChanged(AlignmentModel<T> oldProvider,
|
131
|
AlignmentModel<U> newProvider) { // Not expected.
|
132
|
|
133
|
setDirty();
|
134
|
}
|
135
|
};
|
136
|
private final AlignmentEditorActionUpdater ACTION_UPDATER = new AlignmentEditorActionUpdater();
|
137
|
public final Clipboard CLIPBOARD = new Clipboard(Display.getCurrent()); //TODO Move to global EDITor class.
|
138
|
|
139
|
|
140
|
private MultipleAlignmentsContainer alignmentsContainer = null;
|
141
|
private final Map<String, SingleReadAlignment> cdmMap = new TreeMap<String, SingleReadAlignment>(); //TODO Move this to ContigSequenceDataProvider
|
142
|
|
143
|
|
144
|
@Inject
|
145
|
private MDirtyable dirty;
|
146
|
|
147
|
private AlignmentEditorInput input;
|
148
|
|
149
|
@Inject
|
150
|
public AlignmentEditorE4() {
|
151
|
}
|
152
|
|
153
|
|
154
|
private void refreshToolbarElement(String id) {
|
155
|
ICommandService commandService =
|
156
|
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);
|
157
|
if (commandService != null) {
|
158
|
commandService.refreshElements(id, Collections.EMPTY_MAP);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
|
163
|
private void registerEditSettingListener(MultipleAlignmentsContainer container) {
|
164
|
container.getEditSettings().addListener(new EditSettingsListener() {
|
165
|
@Override
|
166
|
public void workingModeChanged(EditSettingsChangeEvent e) {} // Currently nothing to do
|
167
|
|
168
|
@Override
|
169
|
public void insertLeftInDataAreaChanged(EditSettingsChangeEvent e) {
|
170
|
updateStatusBar();
|
171
|
refreshToolbarElement(ToggleLeftRightInsertionHandler.COMMAND_ID);
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
public void insertChanged(EditSettingsChangeEvent e) {
|
176
|
updateStatusBar();
|
177
|
refreshToolbarElement(ToggleInsertOverwriteHandler.COMMAND_ID);
|
178
|
}
|
179
|
});
|
180
|
}
|
181
|
|
182
|
|
183
|
private AlignmentArea createIndexArea(MultipleAlignmentsContainer container, AlignmentArea labeledArea) {
|
184
|
AlignmentArea result = new AlignmentArea(container);
|
185
|
result.setAllowVerticalScrolling(false);
|
186
|
result.getDataAreas().getTopAreas().add(new SequenceIndexArea(result.getContentArea(), labeledArea));
|
187
|
return result;
|
188
|
}
|
189
|
|
190
|
|
191
|
private AlignmentArea createEditableAlignmentArea(MultipleAlignmentsContainer container, boolean allowVerticalScrolling) {
|
192
|
AlignmentArea result = new AlignmentArea(container);
|
193
|
result.setAllowVerticalScrolling(allowVerticalScrolling);
|
194
|
|
195
|
CharacterTokenSet tokenSet = CharacterTokenSet.newDNAInstance(); //TODO Should NUCLEOTIDE be used instead?
|
196
|
AlignmentModel<Character> model = new PackedAlignmentModel<Character>(tokenSet);
|
197
|
result.setAlignmentModel(model, false);
|
198
|
model.getChangeListeners().add(DIRTY_LISTENER);
|
199
|
result.getPaintSettings().getTokenPainterList().set(0, new NucleotideTokenPainter());
|
200
|
|
201
|
return result;
|
202
|
}
|
203
|
|
204
|
|
205
|
private AlignmentArea createConsensusHintArea(MultipleAlignmentsContainer container,
|
206
|
AlignmentArea labeledArea) {
|
207
|
|
208
|
AlignmentArea result = new AlignmentArea(container);
|
209
|
result.setAllowVerticalScrolling(false);
|
210
|
result.getDataAreas().getBottomAreas().add(
|
211
|
new ConsensusSequenceArea(result.getContentArea(), labeledArea));
|
212
|
return result;
|
213
|
}
|
214
|
|
215
|
|
216
|
private MultipleAlignmentsContainer getAlignmentsContainer() {
|
217
|
if (alignmentsContainer == null) {
|
218
|
alignmentsContainer = new MultipleAlignmentsContainer();
|
219
|
|
220
|
AlignmentAreaList list = alignmentsContainer.getAlignmentAreas();
|
221
|
AlignmentArea readsArea = createEditableAlignmentArea(alignmentsContainer, true);
|
222
|
readsArea.getSelection().addSelectionListener(ACTION_UPDATER);
|
223
|
list.add(createIndexArea(alignmentsContainer, readsArea));
|
224
|
list.add(readsArea); // Make sure READS_AREA_INDEX is correct.
|
225
|
AlignmentArea editableConsensusArea = createEditableAlignmentArea(alignmentsContainer, false);
|
226
|
editableConsensusArea.getSelection().addSelectionListener(ACTION_UPDATER);
|
227
|
list.add(editableConsensusArea); // Make sure COMSENSUS_AREA_INDEX is correct.
|
228
|
list.add(createConsensusHintArea(alignmentsContainer, readsArea));
|
229
|
|
230
|
registerEditSettingListener(alignmentsContainer);
|
231
|
}
|
232
|
return alignmentsContainer;
|
233
|
}
|
234
|
|
235
|
|
236
|
public AlignmentArea getReadsArea() {
|
237
|
return getAlignmentsContainer().getAlignmentAreas().get(READS_AREA_INDEX);
|
238
|
}
|
239
|
|
240
|
|
241
|
public AlignmentArea getEditableConsensusArea() {
|
242
|
return getAlignmentsContainer().getAlignmentAreas().get(EDITABLE_CONSENSUS_AREA_INDEX);
|
243
|
}
|
244
|
|
245
|
|
246
|
/**
|
247
|
* Checks whether {@link #getReadsArea()} or {@link #getEditableConsensusArea()} currently
|
248
|
* have the user focus and returns the according component.
|
249
|
*
|
250
|
* @return either the reads or the consensus alignment area or {@code null} if none of these
|
251
|
* components is currently focused
|
252
|
*/
|
253
|
public AlignmentArea getFocusedArea() {
|
254
|
AlignmentArea result = getReadsArea();
|
255
|
if (hasFocus(result)) {
|
256
|
return result;
|
257
|
}
|
258
|
else {
|
259
|
result = getEditableConsensusArea();
|
260
|
if (hasFocus(result)) {
|
261
|
return result;
|
262
|
}
|
263
|
else {
|
264
|
return null;
|
265
|
}
|
266
|
}
|
267
|
}
|
268
|
|
269
|
|
270
|
/**
|
271
|
* Checks whether the specified alignment area or one of its subcomponents currently has the
|
272
|
* focus.
|
273
|
*
|
274
|
* @param area the alignment area to be checked (Can only be {@link #getReadsArea()} or
|
275
|
* {@link #getEditableConsensusArea()}.)
|
276
|
* @return {@code true} if the specified component is focused and is either equal to
|
277
|
* {@link #getReadsArea()} or {@link #getEditableConsensusArea()}or {@code false} otherwise
|
278
|
*/
|
279
|
private boolean hasFocus(AlignmentArea area) {
|
280
|
return SWTUtils.childHasFocus((Composite)area.getToolkitComponent());
|
281
|
}
|
282
|
|
283
|
|
284
|
public boolean hasPherogram(String sequenceID) {
|
285
|
return getReadsArea().getDataAreas().getSequenceAreas(sequenceID).size() > PHEROGRAM_AREA_INDEX;
|
286
|
}
|
287
|
|
288
|
|
289
|
public PherogramArea getPherogramArea(String sequenceID) {
|
290
|
if (hasPherogram(sequenceID)) {
|
291
|
return (PherogramArea)getReadsArea().getDataAreas().getSequenceAreas(sequenceID).get(PHEROGRAM_AREA_INDEX);
|
292
|
}
|
293
|
else {
|
294
|
return null;
|
295
|
}
|
296
|
}
|
297
|
|
298
|
|
299
|
private ConsensusSequenceArea getConsensusHintDataArea() {
|
300
|
return (ConsensusSequenceArea)getAlignmentsContainer().getAlignmentAreas().
|
301
|
get(CONSENSUS_HINT_AREA_INDEX).getDataAreas().getBottomAreas().
|
302
|
get(CONSENSUS_DATA_AREA_INDEX);
|
303
|
}
|
304
|
|
305
|
|
306
|
@Deprecated //TODO Remove as soon as testing period is over
|
307
|
private void createTestContents() {
|
308
|
// Just for testing:
|
309
|
try {
|
310
|
addRead(new File("D:/Users/BenStoever/ownCloud/Dokumente/Projekte/EDITor/Quelltexte/LibrAlign branch/Repository/eu.etaxonomy.taxeditor.editor/src/main/resources/AlignmentTestData/JR430_JR-P01.ab1").toURI(), false); //$NON-NLS-1$
|
311
|
//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);
|
312
|
addRead(new File("D:/Users/BenStoever/ownCloud/Dokumente/Projekte/EDITor/Quelltexte/LibrAlign branch/Repository/eu.etaxonomy.taxeditor.editor/src/main/resources/AlignmentTestData/Test_qualityScore.scf").toURI(), false); //$NON-NLS-1$
|
313
|
|
314
|
// Add test consensus sequence:
|
315
|
AlignmentModel consensusModel = getEditableConsensusArea().getAlignmentModel();
|
316
|
String id = consensusModel.addSequence(CONSENSUS_NAME);
|
317
|
Collection<Object> tokens = new ArrayList<Object>(); // First save tokens in a collection to avoid GUI updated for each token.
|
318
|
tokens.add(consensusModel.getTokenSet().tokenByRepresentation("A")); //$NON-NLS-1$
|
319
|
tokens.add(consensusModel.getTokenSet().tokenByRepresentation("C")); //$NON-NLS-1$
|
320
|
tokens.add(consensusModel.getTokenSet().tokenByRepresentation("G")); //$NON-NLS-1$
|
321
|
tokens.add(consensusModel.getTokenSet().tokenByRepresentation("T")); //$NON-NLS-1$
|
322
|
consensusModel.insertTokensAt(id, 0, tokens);
|
323
|
}
|
324
|
catch (Exception e) {
|
325
|
throw new RuntimeException(e);
|
326
|
}
|
327
|
}
|
328
|
|
329
|
|
330
|
private void readCDMData(Sequence sequenceNode) {
|
331
|
//TODO If called from somewhere else than createPartControl() the editorInput needs to be checked and previous contents need to be cleared (or updated).
|
332
|
|
333
|
// Add reads:
|
334
|
for (SingleReadAlignment singleReadAlignment : sequenceNode.getSingleReadAlignments()) {
|
335
|
try {
|
336
|
SingleRead pherogramInfo = singleReadAlignment.getSingleRead();
|
337
|
String id = addRead(DerivateLabelProvider.getDerivateText(pherogramInfo, conversationHolder),
|
338
|
getPherogramURI(pherogramInfo),
|
339
|
singleReadAlignment.isReverseComplement(),
|
340
|
singleReadAlignment.getEditedSequence(),
|
341
|
singleReadAlignment.getFirstSeqPosition(),
|
342
|
singleReadAlignment.getLeftCutPosition(),
|
343
|
singleReadAlignment.getRightCutPosition(),
|
344
|
singleReadAlignment.getShifts());
|
345
|
cdmMap.put(id, singleReadAlignment);
|
346
|
}
|
347
|
catch (Exception e) { // Usually due to an error while trying to read the pherogram (e.g. due to an unsupported format or an invalid URI).
|
348
|
MessagingUtils.errorDialog(Messages.AlignmentEditor_ERROR_SINGLE_READ, null, Messages.AlignmentEditor_ERROR_SINGLE_READ_MESSAGE +
|
349
|
e.getLocalizedMessage(), TaxeditorMolecularPlugin.PLUGIN_ID, e, false);
|
350
|
}
|
351
|
}
|
352
|
|
353
|
// Set consensus sequence:
|
354
|
AlignmentModel consensusModel = getEditableConsensusArea().getAlignmentModel();
|
355
|
String id = consensusModel.addSequence(CONSENSUS_NAME);
|
356
|
consensusModel.insertTokensAt(id, 0, AlignmentModelUtils.charSequenceToTokenList(
|
357
|
sequenceNode.getConsensusSequence().getString(), consensusModel.getTokenSet()));
|
358
|
//TODO Can the consensus sequence also be null? / Should it be created here, if nothing is in the DB?
|
359
|
}
|
360
|
|
361
|
|
362
|
@PostConstruct
|
363
|
public void createPartControl(Composite parent) {
|
364
|
if (CdmStore.isActive()){
|
365
|
if(conversationHolder == null){
|
366
|
conversationHolder = CdmStore.createConversation();
|
367
|
}
|
368
|
}
|
369
|
else{
|
370
|
return;
|
371
|
}
|
372
|
SWTComponentFactory.getInstance().getSWTComponent(getAlignmentsContainer(), parent, SWT.NONE);
|
373
|
Display.getCurrent().addFilter(SWT.FocusIn, ACTION_UPDATER);
|
374
|
Display.getCurrent().addFilter(SWT.FocusOut, ACTION_UPDATER);
|
375
|
}
|
376
|
|
377
|
|
378
|
@PreDestroy
|
379
|
public void dispose() {
|
380
|
Display.getCurrent().removeFilter(SWT.FocusIn, ACTION_UPDATER);
|
381
|
Display.getCurrent().removeFilter(SWT.FocusOut, ACTION_UPDATER);
|
382
|
CLIPBOARD.dispose();
|
383
|
input.dispose();
|
384
|
}
|
385
|
|
386
|
|
387
|
private void updateStatusBar() {
|
388
|
//FIXME E4 migrate
|
389
|
// IActionBars bars = getEditorSite().getActionBars();
|
390
|
// bars.getStatusLineManager().setMessage(
|
391
|
// Messages.AlignmentEditor_EDIT_MODE + (getReadsArea().getEditSettings().isInsert() ? Messages.AlignmentEditor_INSERT : Messages.AlignmentEditor_OVERWRITE) + " " + //$NON-NLS-1$
|
392
|
// Messages.AlignmentEditor_INSERTION_PHEROGRAM +
|
393
|
// (getReadsArea().getEditSettings().isInsertLeftInDataArea() ? Messages.AlignmentEditor_LEFT : Messages.AlignmentEditor_RIGHT)); //TODO multi language
|
394
|
}
|
395
|
|
396
|
|
397
|
private SingleReadAlignment.Shift[] convertToCDMShifts(PherogramAreaModel model) {
|
398
|
Iterator<ShiftChange> iterator = model.shiftChangeIterator();
|
399
|
List<SingleReadAlignment.Shift> shifts = new ArrayList<SingleReadAlignment.Shift>();
|
400
|
while (iterator.hasNext()) {
|
401
|
ShiftChange shiftChange = iterator.next();
|
402
|
shifts.add(new SingleReadAlignment.Shift(shiftChange.getBaseCallIndex(), shiftChange.getShiftChange()));
|
403
|
}
|
404
|
return shifts.toArray(new SingleReadAlignment.Shift[shifts.size()]);
|
405
|
}
|
406
|
|
407
|
|
408
|
@Persist
|
409
|
public void doSave(IProgressMonitor monitor) {
|
410
|
String taskName = Messages.AlignmentEditor_SAVING_ALIGNMENT; //TODO multi language
|
411
|
monitor.beginTask(taskName, 3);
|
412
|
|
413
|
//re-loading sequence to avoid session conflicts
|
414
|
Sequence sequenceNode = CdmStore.getService(ISequenceService.class).load(input.getSequenceNodeUuid());
|
415
|
input.setSequenceNode(sequenceNode);
|
416
|
StringAdapter stringProvider = new StringAdapter(getEditableConsensusArea().getAlignmentModel(), false); // Throws an exception if a token has more than one character.
|
417
|
|
418
|
// Write consensus sequence:
|
419
|
SequenceString consensusSequenceObj = sequenceNode.getConsensusSequence();
|
420
|
String newConsensusSequence = stringProvider.getSequence(
|
421
|
getEditableConsensusArea().getAlignmentModel().sequenceIDByName(CONSENSUS_NAME));
|
422
|
if (consensusSequenceObj == null) {
|
423
|
sequenceNode.setConsensusSequence(SequenceString.NewInstance(newConsensusSequence));
|
424
|
}
|
425
|
else {
|
426
|
consensusSequenceObj.setString(newConsensusSequence);
|
427
|
}
|
428
|
|
429
|
// Write single reads:
|
430
|
stringProvider.setUnderlyingModel(getReadsArea().getAlignmentModel());
|
431
|
sequenceNode.getSingleReadAlignments().retainAll(cdmMap.values()); // Remove all reads that are not in the alignment anymore.
|
432
|
Iterator<String> iterator = getReadsArea().getAlignmentModel().sequenceIDIterator();
|
433
|
while (iterator.hasNext()) {
|
434
|
String id = iterator.next();
|
435
|
SingleReadAlignment singleRead = cdmMap.get(id);
|
436
|
if (singleRead == null) {
|
437
|
throw new InternalError(Messages.AlignmentEditor_NEW_READ_FAILURE); //TODO multi language
|
438
|
//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?
|
439
|
//singleRead = SingleReadAlignment.NewInstance(consensusSequence, singleRead, shifts, editedSequence);
|
440
|
}
|
441
|
|
442
|
singleRead.setEditedSequence(stringProvider.getSequence(id));
|
443
|
|
444
|
PherogramArea pherogramArea = getPherogramArea(id);
|
445
|
if (pherogramArea != null) {
|
446
|
PherogramAreaModel model = pherogramArea.getModel();
|
447
|
singleRead.setReverseComplement(model.getPherogramProvider() instanceof ReverseComplementPherogramProvider); // Works only if ReverseComplementPherogramProvider instances are not nested.
|
448
|
singleRead.setShifts(convertToCDMShifts(getPherogramArea(id).getModel()));
|
449
|
singleRead.setFirstSeqPosition(model.getFirstSeqPos());
|
450
|
singleRead.setLeftCutPosition(model.getLeftCutPosition());
|
451
|
singleRead.setRightCutPosition(model.getRightCutPosition());
|
452
|
}
|
453
|
}
|
454
|
|
455
|
if (!conversationHolder.isBound()) {
|
456
|
conversationHolder.bind();
|
457
|
}
|
458
|
monitor.worked(1);
|
459
|
|
460
|
input.merge();
|
461
|
// Commit the conversation and start a new transaction immediately:
|
462
|
conversationHolder.commit(true);
|
463
|
monitor.worked(1);
|
464
|
|
465
|
dirty.setDirty(false);
|
466
|
monitor.worked(1);
|
467
|
monitor.done();
|
468
|
}
|
469
|
|
470
|
|
471
|
public void init(AlignmentEditorInput input) throws PartInitException {
|
472
|
this.input = input;
|
473
|
|
474
|
updateStatusBar();
|
475
|
|
476
|
if (input.getSequenceNodeUuid() != null) {
|
477
|
Sequence sequenceNode = CdmStore.getService(ISequenceService.class).load(input.getSequenceNodeUuid());
|
478
|
//re-load into the current session if it is already persisted in the DB
|
479
|
if(sequenceNode!=null && sequenceNode.getId()!=0){
|
480
|
sequenceNode = CdmStore.getService(ISequenceService.class).load(sequenceNode.getUuid());
|
481
|
}
|
482
|
readCDMData(sequenceNode);
|
483
|
}
|
484
|
else {
|
485
|
createTestContents(); // This case will removed after the test phase and an exception should probably be thrown.
|
486
|
}
|
487
|
}
|
488
|
|
489
|
|
490
|
public boolean isDirty() {
|
491
|
return dirty.isDirty();
|
492
|
}
|
493
|
|
494
|
|
495
|
private void setDirty() {
|
496
|
dirty.setDirty(true);
|
497
|
}
|
498
|
|
499
|
|
500
|
@Focus
|
501
|
public void setFocus() {
|
502
|
if(conversationHolder != null){
|
503
|
conversationHolder.bind();
|
504
|
}
|
505
|
if(input!=null){
|
506
|
input.bind();
|
507
|
}
|
508
|
}
|
509
|
|
510
|
public boolean isInsertMode() {
|
511
|
return getAlignmentsContainer().getEditSettings().isInsert();
|
512
|
}
|
513
|
|
514
|
|
515
|
public boolean isInsertLeftInPherogram() {
|
516
|
return getAlignmentsContainer().getEditSettings().isInsertLeftInDataArea();
|
517
|
}
|
518
|
|
519
|
|
520
|
public void toggleLeftRightInsertionInPherogram() {
|
521
|
getAlignmentsContainer().getEditSettings().toggleInsertLeftInDataArea();
|
522
|
}
|
523
|
|
524
|
|
525
|
public void toggleInsertOverwrite() {
|
526
|
getAlignmentsContainer().getEditSettings().toggleInsert();
|
527
|
}
|
528
|
|
529
|
|
530
|
private String cutPherogram(boolean left) {
|
531
|
SelectionModel selection = getReadsArea().getSelection();
|
532
|
if (selection.getCursorHeight() != 1) {
|
533
|
return Messages.AlignmentEditor_CUTTING_FAILURE; //TODO multi language
|
534
|
}
|
535
|
else {
|
536
|
PherogramArea pherogramArea =
|
537
|
getPherogramArea(getReadsArea().getSequenceOrder().idByIndex(selection.getCursorRow()));
|
538
|
if (pherogramArea == null) {
|
539
|
return Messages.AlignmentEditor_NO_ATTACHED_PHEROGRAM; //TODO multi language
|
540
|
}
|
541
|
else {
|
542
|
if (left) {
|
543
|
if (pherogramArea.setLeftCutPositionBySelection()) {
|
544
|
return null;
|
545
|
}
|
546
|
else {
|
547
|
return Messages.AlignmentEditor_LEFT_END_OUTSIDE; //TODO multi language
|
548
|
}
|
549
|
}
|
550
|
else {
|
551
|
if (pherogramArea.setRightCutPositionBySelection()) {
|
552
|
return null;
|
553
|
}
|
554
|
else {
|
555
|
return Messages.AlignmentEditor_RIGHT_END_OUTSIDE; //TODO multi language
|
556
|
}
|
557
|
}
|
558
|
}
|
559
|
}
|
560
|
}
|
561
|
|
562
|
|
563
|
public String cutPherogramLeft() {
|
564
|
return cutPherogram(true);
|
565
|
}
|
566
|
|
567
|
|
568
|
public String cutPherogramRight() {
|
569
|
return cutPherogram(false);
|
570
|
}
|
571
|
|
572
|
|
573
|
public void reverseComplementSelectedSequences() {
|
574
|
SelectionModel selection = getReadsArea().getSelection();
|
575
|
AlignmentModel<?> model = getReadsArea().getAlignmentModel();
|
576
|
for (int row = selection.getFirstRow(); row < selection.getFirstRow() + selection.getCursorHeight(); row++) {
|
577
|
String sequenceID = getReadsArea().getSequenceOrder().idByIndex(row);
|
578
|
PherogramArea area = getPherogramArea(sequenceID);
|
579
|
PherogramAreaModel pherogramAlignmentModel = area.getModel();
|
580
|
|
581
|
PherogramAlignmentRelation rightRelation = pherogramAlignmentModel.editableIndexByBaseCallIndex(
|
582
|
pherogramAlignmentModel.getRightCutPosition());
|
583
|
int rightBorder;
|
584
|
if (rightRelation.getCorresponding() == PherogramAlignmentRelation.OUT_OF_RANGE) {
|
585
|
rightBorder = rightRelation.getBeforeValidIndex() + 1;
|
586
|
}
|
587
|
else {
|
588
|
rightBorder = rightRelation.getAfterValidIndex();
|
589
|
}
|
590
|
|
591
|
AlignmentModelUtils.reverseComplement(model, sequenceID,
|
592
|
pherogramAlignmentModel.editableIndexByBaseCallIndex(
|
593
|
pherogramAlignmentModel.getLeftCutPosition()).getBeforeValidIndex(),
|
594
|
rightBorder);
|
595
|
pherogramAlignmentModel.reverseComplement();
|
596
|
}
|
597
|
}
|
598
|
|
599
|
|
600
|
/**
|
601
|
* Recreates the whole consensus sequence from all single read sequences. The previous consensus
|
602
|
* sequence is overwritten.
|
603
|
*/
|
604
|
@SuppressWarnings("unchecked")
|
605
|
public <T> void createConsensusSequence() {
|
606
|
ConsensusSequenceArea area = getConsensusHintDataArea();
|
607
|
AlignmentModel<T> model = (AlignmentModel<T>)getEditableConsensusArea().getAlignmentModel();
|
608
|
String sequenceID = model.sequenceIDIterator().next(); // There is always one sequence contained.
|
609
|
int length = getReadsArea().getAlignmentModel().getMaxSequenceLength();
|
610
|
|
611
|
Collection<T> tokens = new ArrayList<T>(length);
|
612
|
for (int column = 0; column < length; column++) {
|
613
|
tokens.add(model.getTokenSet().tokenByRepresentation(area.getConsensusToken(column)));
|
614
|
}
|
615
|
|
616
|
model.removeTokensAt(sequenceID, 0, model.getSequenceLength(sequenceID));
|
617
|
model.insertTokensAt(sequenceID, 0, tokens);
|
618
|
}
|
619
|
|
620
|
|
621
|
/**
|
622
|
* Updates the current consensus sequence by replacing gaps by the according consensus tokens
|
623
|
* calculated from the single read sequences and extends the consensus sequence if necessary.
|
624
|
*/
|
625
|
@SuppressWarnings("unchecked")
|
626
|
public <T> void updateConsensusSequence() {
|
627
|
ConsensusSequenceArea area = getConsensusHintDataArea();
|
628
|
AlignmentModel<T> model = (AlignmentModel<T>)getEditableConsensusArea().getAlignmentModel();
|
629
|
TokenSet<T> tokenSet = model.getTokenSet();
|
630
|
String sequenceID = model.sequenceIDIterator().next(); // There is always one sequence contained.
|
631
|
int currentConsensusLength = model.getSequenceLength(sequenceID);
|
632
|
int overallLength = getReadsArea().getAlignmentModel().getMaxSequenceLength();
|
633
|
|
634
|
// Replace gaps by new information:
|
635
|
for (int column = 0; column < currentConsensusLength; column++) {
|
636
|
if (tokenSet.isGapToken(model.getTokenAt(sequenceID, column))) {
|
637
|
T newToken = tokenSet.tokenByRepresentation(area.getConsensusToken(column));
|
638
|
if (!tokenSet.isGapToken(newToken)) {
|
639
|
model.setTokenAt(sequenceID, column, newToken);
|
640
|
}
|
641
|
}
|
642
|
}
|
643
|
|
644
|
// Append additional tokens:
|
645
|
if (overallLength > currentConsensusLength) {
|
646
|
Collection<T> tokens = new ArrayList<T>(overallLength);
|
647
|
for (int column = currentConsensusLength; column < overallLength; column++) {
|
648
|
tokens.add(tokenSet.tokenByRepresentation(area.getConsensusToken(column)));
|
649
|
}
|
650
|
model.appendTokens(sequenceID, tokens);
|
651
|
}
|
652
|
}
|
653
|
|
654
|
|
655
|
public static PherogramProvider readPherogram(URI uri) throws IOException, UnsupportedChromatogramFormatException {
|
656
|
PherogramProvider result;
|
657
|
InputStream stream = uri.toURL().openStream();
|
658
|
try {
|
659
|
result = new BioJavaPherogramProvider(ChromatogramFactory.create(stream));
|
660
|
}
|
661
|
finally {
|
662
|
stream.close();
|
663
|
}
|
664
|
return result;
|
665
|
}
|
666
|
|
667
|
|
668
|
private String newReadName() {
|
669
|
int index = 1;
|
670
|
while (getReadsArea().getAlignmentModel().sequenceIDByName(DEFAULT_READ_NAME_PREFIX + index) != null) {
|
671
|
index++;
|
672
|
}
|
673
|
return DEFAULT_READ_NAME_PREFIX + index;
|
674
|
}
|
675
|
|
676
|
|
677
|
public void addRead(URI pherogramURI, boolean reverseComplemented) throws IOException, UnsupportedChromatogramFormatException {
|
678
|
addRead(newReadName(), pherogramURI, reverseComplemented, null, null, null, null, null);
|
679
|
}
|
680
|
|
681
|
|
682
|
/**
|
683
|
* Adds a new sequence with attached phergram data area to the reads alignment.
|
684
|
* <p>
|
685
|
* If {@code null} is specified as {@code editedSequence} the base call sequence from the pherogram will
|
686
|
* be set as the edited sequence. If {@code null} is specified as {@code shifts} no shifts between the edited
|
687
|
* and the base calls sequence are assumed.
|
688
|
*
|
689
|
* @param name the name of the new sequence
|
690
|
* @param pherogramURI the URI where the associated pherogram file is located
|
691
|
* @param reverseComplemented Specify {@code true} here, if the reverse complement of the pherogram data should
|
692
|
* be added, {@code false} otherwise.
|
693
|
* @param editedSequence the edited version of the base call sequence (May be {@code null}.)
|
694
|
* @param shifts the alignment information that links the edited and the base call sequence (May be {@code null}.)
|
695
|
* @return the sequence ID of the added read
|
696
|
* @throws IOException if an error occurred when trying to read the pherogram file
|
697
|
* @throws UnsupportedChromatogramFormatException if the format of the pherogram file is not supported
|
698
|
*/
|
699
|
public String addRead(String name, URI pherogramURI, boolean reverseComplemented, String editedSequence,
|
700
|
Integer firstSeqPos, Integer leftCutPos, Integer rightCutPos, SingleReadAlignment.Shift[] shifts)
|
701
|
throws IOException, UnsupportedChromatogramFormatException {
|
702
|
|
703
|
AlignmentModel model = getReadsArea().getAlignmentModel();
|
704
|
PherogramProvider pherogramProvider = null;
|
705
|
if (pherogramURI != null) {
|
706
|
pherogramProvider = readPherogram(pherogramURI); // Must happen before a sequence is added, because it might throw an exception.
|
707
|
if (reverseComplemented) {
|
708
|
pherogramProvider = new ReverseComplementPherogramProvider(pherogramProvider);
|
709
|
}
|
710
|
}
|
711
|
|
712
|
// Create sequence:
|
713
|
model.addSequence(name);
|
714
|
String id = model.sequenceIDByName(name);
|
715
|
|
716
|
// Set edited sequence:
|
717
|
Collection<Object> tokens = null; // First save tokens in a collection to avoid GUI updated for each token.
|
718
|
if (editedSequence != null) {
|
719
|
tokens = AlignmentModelUtils.charSequenceToTokenList(editedSequence, model.getTokenSet());
|
720
|
}
|
721
|
else if (pherogramProvider != null) { // Copy base call sequence into alignment:
|
722
|
tokens = new ArrayList<Object>();
|
723
|
for (int i = 0; i < pherogramProvider.getSequenceLength(); i++) {
|
724
|
tokens.add(model.getTokenSet().tokenByRepresentation(
|
725
|
Character.toString(pherogramProvider.getBaseCall(i))));
|
726
|
}
|
727
|
setDirty();
|
728
|
}
|
729
|
|
730
|
if (tokens != null) { // If either an edited sequence or a pherogram URI was provided.
|
731
|
model.insertTokensAt(id, 0, tokens);
|
732
|
|
733
|
if (pherogramProvider != null) {
|
734
|
// Create pherogram area:
|
735
|
PherogramArea pherogramArea = new PherogramArea(getReadsArea().getContentArea(),
|
736
|
new PherogramAreaModel(pherogramProvider));
|
737
|
|
738
|
// Set position properties and shifts:
|
739
|
PherogramAreaModel phergramModel = pherogramArea.getModel();
|
740
|
if ((firstSeqPos != null) && (leftCutPos != null)) {
|
741
|
phergramModel.setFirstSeqLeftCutPos(firstSeqPos, leftCutPos);
|
742
|
}
|
743
|
if (rightCutPos != null) {
|
744
|
phergramModel.setRightCutPosition(rightCutPos);
|
745
|
}
|
746
|
if ((shifts != null) && (shifts.length > 0)) {
|
747
|
for (int i = 0; i < shifts.length; i++) {
|
748
|
phergramModel.addShiftChange(shifts[i].position, shifts[i].shift);
|
749
|
}
|
750
|
setDirty();
|
751
|
}
|
752
|
|
753
|
// Add pherogram area to GUI:
|
754
|
pherogramArea.addMouseListener(new PherogramMouseListener(pherogramArea));
|
755
|
getReadsArea().getDataAreas().getSequenceAreas(id).add(pherogramArea);
|
756
|
}
|
757
|
}
|
758
|
return id;
|
759
|
}
|
760
|
|
761
|
|
762
|
public static URI getPherogramURI(SingleRead pherogramInfo) {
|
763
|
if (pherogramInfo.getPherogram() != null) {
|
764
|
return MediaUtils.getFirstMediaRepresentationPart(pherogramInfo.getPherogram()).getUri();
|
765
|
}
|
766
|
else {
|
767
|
return null;
|
768
|
}
|
769
|
}
|
770
|
}
|