Basic implementation for LoadPherogramHandler added. (Imported data is currently...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / handler / LoadPherogramHandler.java
1 /**
2 * Copyright (C) 2007 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.editor.handler;
10
11
12 import info.bioinfweb.libralign.AlignmentArea;
13 import info.bioinfweb.libralign.AlignmentContentArea;
14 import info.bioinfweb.libralign.dataarea.implementations.pherogram.PherogramArea;
15 import info.bioinfweb.libralign.pherogram.BioJavaPherogramProvider;
16 import info.bioinfweb.libralign.sequenceprovider.SequenceDataProvider;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import org.biojava.bio.chromatogram.ChromatogramFactory;
22 import org.biojava.bio.chromatogram.UnsupportedChromatogramFormatException;
23 import org.eclipse.core.commands.AbstractHandler;
24 import org.eclipse.core.commands.ExecutionEvent;
25 import org.eclipse.core.commands.ExecutionException;
26 import org.eclipse.swt.widgets.FileDialog;
27 import org.eclipse.ui.IEditorPart;
28
29 import eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor;
30 import eu.etaxonomy.taxeditor.model.AbstractUtility;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32
33
34
35 /**
36 * Handler that loads an additional read into the contig alignment displayed by an instance of {@link AlignmentEditor}.
37 *
38 * @author Ben Stöver
39 * @author pplitzner
40 * @date 16.10.2014
41 */
42 public class LoadPherogramHandler extends AbstractHandler {
43 public static final String DEFAULT_READ_NAME_PREFIX = "Read ";
44
45
46 private String newReadName(SequenceDataProvider provider) {
47 int index = 1;
48 while (provider.sequenceIDByName(DEFAULT_READ_NAME_PREFIX + index) != SequenceDataProvider.NO_SEQUENCE_FOUND) {
49 index++;
50 }
51 return DEFAULT_READ_NAME_PREFIX + index;
52 }
53
54
55 private void addPherogram(AlignmentContentArea contentArea, File pherogram)
56 throws IOException, UnsupportedChromatogramFormatException {
57
58 SequenceDataProvider provider = contentArea.getSequenceProvider();
59 BioJavaPherogramProvider pherogramProvider = new BioJavaPherogramProvider(ChromatogramFactory.create(pherogram)); // Must happen before a sequence is added, because it might throw an exception.
60 String name = newReadName(provider);
61
62 // Create sequence:
63 provider.addSequence(name);
64 int id = provider.sequenceIDByName(name);
65
66 // Copy base call sequence into alignment:
67 for (int i = 0; i < pherogramProvider.getSequenceLength(); i++) {
68 provider.insertTokenAt(id, i, provider.getTokenSet().tokenByKeyChar(
69 pherogramProvider.getBaseCall(i).getUpperedBase().charAt(0)));
70 }
71
72 // Add data area:
73 PherogramArea pherogramArea = new PherogramArea(contentArea, pherogramProvider);
74 contentArea.getDataAreas().getSequenceAreas(id).add(pherogramArea);
75 }
76
77
78 /* (non-Javadoc)
79 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
80 */
81 @Override
82 public Object execute(ExecutionEvent event) throws ExecutionException {
83 IEditorPart activeEditor = AbstractUtility.getActiveEditor();
84 if (activeEditor instanceof AlignmentEditor) {
85 AlignmentEditor alignmentEditor = (AlignmentEditor)activeEditor;
86
87 FileDialog fileDialog = new FileDialog(alignmentEditor.getSite().getShell());
88 fileDialog.setText("Import pherogram into contig alignment");
89 fileDialog.setFilterNames(new String[]{"All supported formats", "AB1 pherogram files", "SCF pherogram files", "All files"});
90 fileDialog.setFilterExtensions(new String[]{"*.ab1;*.scf", "*.ab1", "*.scf", "*.*"});
91
92 String path = fileDialog.open();
93 if (path != null) {
94 try {
95 addPherogram(alignmentEditor.getReadsArea().getContentArea(), new File(path));
96 }
97 catch (UnsupportedChromatogramFormatException e) {
98 MessagingUtils.errorDialog("Unsupported format", this, "The format of the pherogram file \"" + path +
99 "\" is not supported. (Only AB1 and SCF are supported.)", "eu.etaxonomy.taxeditor.editor", e, false); //TODO set pluginID
100 }
101 catch (IOException e) {
102 MessagingUtils.errorDialog("Unsupported format", this,
103 "An IO error occurred while trying to read the file \"" + path + "\".",
104 "/eu.etaxonomy.taxeditor.editor", e, false); //TODO set pluginID
105 }
106 }
107 }
108 return null;
109 }
110 }