How to programmatically import trees through the Geneious API
Unless you're writing your own importer for a tree file format Geneious doesn't already support, there are two different general ways to import trees into Geneious, depending on whether you're having trouble auto-detecting the file format.
PluginUtilities.importDocuments() should import tree files in any format that can be read by Geneious. For example:
private List<AnnotatedPluginDocument> getTreesFromFile(String filename, ProgressListener progress) throws DocumentImportException, IOException { File treeFile = new File(filename); if (! treeFile.exists()) { throw new DocumentImportException("Tree file " + filename + " does not exist"); } List<AnnotatedPluginDocument> documents = PluginUtilities.importDocuments(treeFile, progress); for (AnnotatedPluginDocument doc : documents) { if (! TreeDocument.class.isAssignableFrom(doc.getDocumentClass())) { throw new DocumentImportException("Document is not a tree: " + doc.getName()); } } return documents; }
This will return a (singleton) list containing the trees in the input file, providing it exists and contains one or more trees.
That's probably the approach you should use, unless there's something about your files that's making it hard to detect the format. The other approach, if you want to specify the format of the trees you want to import, is to use the JEBL classes that are part of our Public API. JEBL's documentation is available on Sourceforge, here's the link to the jebl.evolution.io package, which includes the importers: http://jebl.sourceforge.net/doc/api/jebl/evolution/io/package-summary.html
For example, I copied and modified the ExampleFastaImporter module from the devkit to create a Newick importer that uses jebl.evolution.io.NewickImporter to do the work:
package com.biomatters.exampleTreeImporterPlugin; import com.biomatters.geneious.publicapi.implementations.DefaultSameTaxaTreesDocument; import com.biomatters.geneious.publicapi.plugin.DocumentFileImporter; import com.biomatters.geneious.publicapi.plugin.DocumentImportException; import com.biomatters.geneious.publicapi.utilities.ProgressInputStream; import jebl.evolution.io.ImportException; import jebl.evolution.io.NewickImporter; import jebl.evolution.trees.Tree; import jebl.util.ProgressListener; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.Date; import java.util.List; public class ExampleTreeImporter extends DocumentFileImporter { public String[] getPermissibleExtensions() { return new String[]{".tree",".tre"}; } public String getFileTypeDescription() { return "Example Tree Importer"; } public AutoDetectStatus tentativeAutoDetect(File file, String fileContentsStart) { if (fileContentsStart.startsWith("(")) { return AutoDetectStatus.MAYBE; } else { return AutoDetectStatus.REJECT_FILE; } } public void importDocuments(File file, ImportCallback callback, ProgressListener progressListener) throws IOException, DocumentImportException { BufferedReader reader = new BufferedReader(new InputStreamReader(new ProgressInputStream(progressListener, file))); NewickImporter importer = new NewickImporter(reader, true); List<Tree> trees; try { trees = importer.importTrees(); } catch (ImportException e) { throw new DocumentImportException(e.getMessage()); } callback.addDocument(new DefaultSameTaxaTreesDocument(trees, file.getName(), new Date())); } }
If you're writing your own importer for a new file format, you'll need to take a look at the JEBL documentation (jebl.evolution.trees package). You'll probably need to create a DefaultSameTaxaTreesDocument or a DefaultPhylogenyDocument depending on whether you're attaching an alignment or not, but either way you'll need to look at tree data structures provided by JEBL.
Please sign in to leave a comment.
Comments
0 comments