root/trunk/plugins/examples/org/pathvisio/example/ExExporter.java

Revision 3093, 3.5 KB (checked in by martijn, 6 months ago)

Fix casing for PublicationXref? (#1022)

Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.pathvisio.example;
18
19import java.io.File;
20import java.io.FileWriter;
21import java.io.IOException;
22
23import org.pathvisio.biopax.reflect.BiopaxElement;
24import org.pathvisio.biopax.reflect.PublicationXref;
25import org.pathvisio.gui.swing.PvDesktop;
26import org.pathvisio.model.ConverterException;
27import org.pathvisio.model.Pathway;
28import org.pathvisio.model.PathwayExporter;
29import org.pathvisio.plugin.Plugin;
30
31/**
32 * Shows how to implement a new file format,
33 * and how to register it with PathVisio.
34 * <p>
35 * In this case we'll create an exporter to
36 * export only the literature references of
37 * a Pathway.
38 */
39public class ExExporter implements Plugin
40{
41        private PvDesktop desktop;
42
43        public void init(PvDesktop desktop)
44        {
45                this.desktop = desktop;
46                // here we register an instance of our exporter
47                desktop.getSwingEngine().getEngine().addPathwayExporter(new BibliographyExporter());
48        }
49
50        /**
51         * Export all literature references of a pathway
52         * as a simple plain-text format.
53         * This is not any standard format, it's just an example
54         * of what an exporter can do.BibliographyExporter
55         */
56        private static class BibliographyExporter implements PathwayExporter
57        {
58                /**  Called after the user has selected a file to export */
59                public void doExport(File file, Pathway pathway)
60                                throws ConverterException {
61
62                        try {
63                                // open the file for writing
64                                FileWriter fos = new FileWriter(file);
65                                int i = 0;
66                                // loop over all embedded Biopax elements of the Pathway
67                                for (BiopaxElement be : pathway.getBiopaxElementManager().getElements())
68                                {
69                                        fos.write ("BIBLIOGRAPHY\n");
70                                        fos.write ("============\n");
71                                        // check if this is a publication reference
72                                        if (be instanceof PublicationXref)
73                                        {
74                                                // print some information in a very simple text format
75                                                PublicationXref pxref = (PublicationXref)be;
76                                                fos.write ("\n");
77                                                i++;
78                                                fos.write ("#" + i + ":\n");
79                                                fos.write("Authors: " + pxref.getAuthors() + "\n");
80                                                fos.write("Title: " + pxref.getTitle() + "\n");
81                                                fos.write("Source: " + pxref.getSource() + "\n");
82                                                fos.write("Year: " + pxref.getYear() + "\n");
83                                                fos.write("PMID: " + pxref.getPubmedId() + "\n");
84                                        }
85                                }
86                                // if i remains at zero, there were no references
87                                if (i == 0)
88                                {
89                                        fos.write ("\nNo publication references found\n");
90                                }
91                                fos.close();
92                        }
93                        catch (IOException e)
94                        {
95                                throw new ConverterException (e);
96                        }
97
98                }
99
100                private static final String[] EXTENSIONS = new String[] { "txt" };
101
102                /**
103                 * Valid extensions for this file type. The first
104                 * of these will be automatically appended to the File passed to doExport.
105                 */
106                public String[] getExtensions()
107                {
108                        return EXTENSIONS;
109                }
110
111                /**
112                 * A suitable name to display in the JFileChooser dialog.
113                 */
114                public String getName()
115                {
116                        return "Text Bibliography Exporter";
117                }
118        }
119
120        public void done() {}
121}
Note: See TracBrowser for help on using the browser.