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.ontology.io;
025
026import java.io.BufferedReader;
027import java.io.IOException;
028
029
030import org.biojava.bio.BioError;
031import org.biojava.bio.seq.io.ParseException;
032import org.biojava.ontology.AlreadyExistsException;
033import org.biojava.ontology.OntoTools;
034import org.biojava.ontology.Ontology;
035import org.biojava.ontology.OntologyException;
036import org.biojava.ontology.OntologyFactory;
037import org.biojava.ontology.obo.OboFileEventListener;
038import org.biojava.ontology.obo.OboFileHandler;
039import org.biojava.ontology.obo.OboFileParser;
040import org.biojava.utils.ChangeVetoException;
041
042/** Parses an OBO file.
043 * 
044 * @author Andreas Prlic
045 * @since 1.7
046 * 
047 * <h2>Example</h2>
048 * <pre>
049 * OboParser parser = new OboParser();
050                InputStream inStream = this.getClass().getResourceAsStream("/files/ontology/biosapiens.obo");
051                
052                BufferedReader oboFile = new BufferedReader ( new InputStreamReader ( inStream ) );
053                try {
054                        Ontology ontology = parser.parseOBO(oboFile, "BioSapiens", "the BioSapiens ontology");
055                                                
056                        Set keys = ontology.getTerms();
057                        Iterator iter = keys.iterator();
058                        while (iter.hasNext()){
059                                System.out.println(iter.next());
060                        }
061                        
062                } catch (Exception e){
063                        e.printStackTrace();
064                }
065 * </pre>
066 *
067 */
068public class OboParser {
069        
070        /** Parse a OBO file and return its content as a BioJava Ontology object
071         * 
072         * @param oboFile the file to be parsed 
073         * @param ontoName
074         * @param ontoDescription
075
076         * @return the ontology represented as a BioJava ontology file
077         * @throws ParseException
078         * @throws IOException
079         */
080        public Ontology parseOBO(
081                        BufferedReader oboFile,
082            String ontoName,
083            String ontoDescription
084            )
085        throws ParseException, IOException {
086                
087                 try {
088                         OntologyFactory factory = OntoTools.getDefaultFactory();
089                         Ontology ontology = factory.createOntology(ontoName, ontoDescription);
090                         
091                 OboFileParser parser = new OboFileParser();
092                 
093                 OboFileEventListener handler = new OboFileHandler(ontology);
094                 
095                 parser.addOboFileEventListener(handler);
096                 parser.parseOBO(oboFile);
097                 
098                 return ontology;
099                 
100                         
101                 } catch (AlreadyExistsException ex) {
102                    throw new ParseException(ex, "Duplication in ontology");
103                } catch (OntologyException ex) {
104                    throw new ParseException(ex);
105                } catch (ChangeVetoException ex) {
106                    throw new BioError("Error accessing newly created ontology",ex);
107                }
108                
109        }
110}