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.ReadWriteParameterNames;
|
16
|
import info.bioinfweb.jphyloio.dataadapters.implementations.ListBasedDocumentDataAdapter;
|
17
|
import info.bioinfweb.jphyloio.factory.JPhyloIOReaderWriterFactory;
|
18
|
import info.bioinfweb.jphyloio.formats.JPhyloIOFormatIDs;
|
19
|
import info.bioinfweb.jphyloio.objecttranslation.ObjectTranslatorFactory;
|
20
|
|
21
|
import java.io.File;
|
22
|
import java.io.IOException;
|
23
|
|
24
|
import org.eclipse.core.commands.AbstractHandler;
|
25
|
import org.eclipse.core.commands.ExecutionEvent;
|
26
|
import org.eclipse.core.commands.ExecutionException;
|
27
|
import org.eclipse.jface.viewers.ISelection;
|
28
|
import org.eclipse.jface.viewers.TreeNode;
|
29
|
import org.eclipse.ui.handlers.HandlerUtil;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
32
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
33
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
34
|
import eu.etaxonomy.taxeditor.molecular.TaxeditorMolecularPlugin;
|
35
|
import eu.etaxonomy.taxeditor.molecular.io.CDMPherogramAlignmentObjectTranslator;
|
36
|
import eu.etaxonomy.taxeditor.molecular.io.CDMSequenceMatrixAdapter;
|
37
|
import eu.etaxonomy.taxeditor.molecular.io.SingleReadAlignmentRDFXMLConstants;
|
38
|
|
39
|
|
40
|
|
41
|
/**
|
42
|
* Allows to export a single read alignment to various alignment formats using
|
43
|
* <a href="http://bioinfweb.info/JPhyloIO/">JPhyloIO</a>.
|
44
|
*
|
45
|
* @author Ben Stöver
|
46
|
* @date 24.04.2016
|
47
|
*/
|
48
|
public class ExportSequenceToFileHandler extends AbstractHandler {
|
49
|
private static final JPhyloIOReaderWriterFactory factory = new JPhyloIOReaderWriterFactory();
|
50
|
|
51
|
|
52
|
@Override
|
53
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
54
|
ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
|
55
|
TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(currentSelection);
|
56
|
if (treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof Sequence) {
|
57
|
Sequence sequence = (Sequence)treeNodeOfSelection.getValue();
|
58
|
|
59
|
//TODO Collect the following information using a wizard.
|
60
|
File targetFile = new File("O:\\Projects\\Bereich BBI\\EDIT Campanula\\Testdaten\\NeXMLExport.xml");
|
61
|
String formatID = JPhyloIOFormatIDs.NEXML_FORMAT_ID;
|
62
|
|
63
|
ReadWriteParameterMap parameters = new ReadWriteParameterMap();
|
64
|
ObjectTranslatorFactory translatorFactory = new ObjectTranslatorFactory();
|
65
|
translatorFactory.addXSDTranslators(true);
|
66
|
translatorFactory.addTranslator(new CDMPherogramAlignmentObjectTranslator(), true, SingleReadAlignmentRDFXMLConstants.DATA_TYPE_PHERORAGM_ALIGNMENT);
|
67
|
parameters.put(ReadWriteParameterNames.KEY_OBJECT_TRANSLATOR_FACTORY, translatorFactory);
|
68
|
|
69
|
String consensusSequenceLabel = "Consensus sequence";
|
70
|
JPhyloIOEventWriter writer = factory.getWriter(formatID);
|
71
|
|
72
|
ListBasedDocumentDataAdapter document = new ListBasedDocumentDataAdapter();
|
73
|
document.getMatrices().add(new CDMSequenceMatrixAdapter(sequence, consensusSequenceLabel));
|
74
|
|
75
|
try {
|
76
|
writer.writeDocument(document, targetFile, parameters);
|
77
|
}
|
78
|
catch (IOException e) {
|
79
|
e.printStackTrace();
|
80
|
MessagingUtils.errorDialog("IO error", this,
|
81
|
"An error occured when trying to export a consensus sequence alignment to the file \"" +
|
82
|
targetFile.getAbsolutePath() + "\".", TaxeditorMolecularPlugin.PLUGIN_ID, e, false); //TODO set pluginID
|
83
|
//TODO Use multi language error message.
|
84
|
}
|
85
|
}
|
86
|
return null;
|
87
|
}
|
88
|
}
|