001/*
002 *                  BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Jan 18, 2008
021 *
022 */
023
024package org.biojava.nbio.ontology.io;
025
026import org.biojava.nbio.ontology.*;
027import org.biojava.nbio.ontology.obo.OboFileEventListener;
028import org.biojava.nbio.ontology.obo.OboFileHandler;
029import org.biojava.nbio.ontology.obo.OboFileParser;
030
031import java.io.BufferedReader;
032import java.io.IOException;
033import java.text.ParseException;
034
035
036
037
038/** Parses an OBO file.
039 *
040 * @author Andreas Prlic
041 * @since 1.7
042 *
043 * <h2>Example</h2>
044 * <pre>
045 * OboParser parser = new OboParser();
046                InputStream inStream = this.getClass().getResourceAsStream("/files/ontology/biosapiens.obo");
047
048                BufferedReader oboFile = new BufferedReader ( new InputStreamReader ( inStream ) );
049                try {
050                        Ontology ontology = parser.parseOBO(oboFile, "BioSapiens", "the BioSapiens ontology");
051
052                        Set keys = ontology.getTerms();
053                        Iterator iter = keys.iterator();
054                        while (iter.hasNext()){
055                                System.out.println(iter.next());
056                        }
057
058                } catch (Exception e){
059                        e.printStackTrace();
060                }
061 * </pre>
062 *
063 */
064public class OboParser {
065
066        /** Parse a OBO file and return its content as a BioJava Ontology object
067         *
068         * @param oboFile the file to be parsed
069         * @param ontoName
070         * @param ontoDescription
071
072         * @return the ontology represented as a BioJava ontology file
073         * @throws ParseException
074         * @throws IOException
075         */
076        public Ontology parseOBO(
077                        BufferedReader oboFile,
078                        String ontoName,
079                        String ontoDescription
080                        )
081                                        throws ParseException, IOException {
082
083                try {
084                        OntologyFactory factory = OntoTools.getDefaultFactory();
085                        Ontology ontology = factory.createOntology(ontoName, ontoDescription);
086
087                        OboFileParser parser = new OboFileParser();
088
089                        OboFileEventListener handler = new OboFileHandler(ontology);
090
091                        parser.addOboFileEventListener(handler);
092                        parser.parseOBO(oboFile);
093
094                        return ontology;
095
096
097                } catch (AlreadyExistsException ex) {
098                        throw new RuntimeException( "Duplication in ontology");
099                } catch (OntologyException ex) {
100                        throw new RuntimeException(ex);
101                }
102
103        }
104}