Project

General

Profile

Download (3.31 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.molecular.handler;
11

    
12

    
13
import info.bioinfweb.jphyloio.JPhyloIOEventWriter;
14
import info.bioinfweb.jphyloio.ReadWriteParameterMap;
15
import info.bioinfweb.jphyloio.dataadapters.implementations.ListBasedDocumentDataAdapter;
16
import info.bioinfweb.jphyloio.factory.JPhyloIOReaderWriterFactory;
17
import info.bioinfweb.jphyloio.formats.JPhyloIOFormatIDs;
18

    
19
import java.io.File;
20
import java.io.IOException;
21

    
22
import org.eclipse.core.commands.AbstractHandler;
23
import org.eclipse.core.commands.ExecutionEvent;
24
import org.eclipse.core.commands.ExecutionException;
25
import org.eclipse.jface.viewers.ISelection;
26
import org.eclipse.jface.viewers.TreeNode;
27
import org.eclipse.ui.handlers.HandlerUtil;
28

    
29
import eu.etaxonomy.cdm.model.molecular.Sequence;
30
import eu.etaxonomy.taxeditor.editor.EditorUtil;
31
import eu.etaxonomy.taxeditor.model.MessagingUtils;
32
import eu.etaxonomy.taxeditor.molecular.TaxeditorMolecularPlugin;
33
import eu.etaxonomy.taxeditor.molecular.io.CDMSequenceMatrixAdapter;
34

    
35

    
36

    
37
/**
38
 * Allows to export a single read alignment to various alignment formats using
39
 * <a href="http://bioinfweb.info/JPhyloIO/">JPhyloIO</a>.
40
 *
41
 * @author Ben Stöver
42
 * @date 24.04.2016
43
 */
44
public class ExportSequenceToFileHandler extends AbstractHandler {
45
    private static final JPhyloIOReaderWriterFactory factory = new JPhyloIOReaderWriterFactory();
46

    
47

    
48
    @Override
49
    public Object execute(ExecutionEvent event) throws ExecutionException {
50
        ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
51
        TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(currentSelection);
52
        if (treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof Sequence) {
53
            Sequence sequence = (Sequence)treeNodeOfSelection.getValue();
54

    
55
            //TODO Collect the following information using a wizard.
56
            File targetFile = new File("O:\\Projects\\Bereich BBI\\EDIT Campanula\\Testdaten\\NeXMLExport.xml");
57
            String formatID = JPhyloIOFormatIDs.NEXML_FORMAT_ID;
58
            ReadWriteParameterMap parameters = new ReadWriteParameterMap();
59
            String consensusSequenceLabel = "Consensus sequence";
60

    
61
            JPhyloIOEventWriter writer = factory.getWriter(formatID);
62

    
63
            ListBasedDocumentDataAdapter document = new ListBasedDocumentDataAdapter();
64
            document.getMatrices().add(new CDMSequenceMatrixAdapter(sequence, consensusSequenceLabel));
65

    
66
            try {
67
                writer.writeDocument(document, targetFile, parameters);
68
            }
69
            catch (IOException e) {
70
                e.printStackTrace();
71
                MessagingUtils.errorDialog("IO error", this,
72
                        "An error occured when trying to export a consensus sequence alignment to the file \"" +
73
                        targetFile.getAbsolutePath() + "\".", TaxeditorMolecularPlugin.PLUGIN_ID, e, false);  //TODO set pluginID
74
                        //TODO Use multi language error message.
75
            }
76
        }
77
        return null;
78
    }
79
}
(12-12/22)