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.obo;
025
026import java.util.ArrayList;
027import java.util.List;
028
029import org.biojava.bio.Annotation;
030import org.biojava.ontology.AlreadyExistsException;
031import org.biojava.ontology.Ontology;
032import org.biojava.ontology.Synonym;
033import org.biojava.ontology.Term;
034
035
036/** A  file handler for .obo files
037 * 
038 * @author Andreas Prlic
039 *
040 */
041public class OboFileHandler implements OboFileEventListener {
042
043        Ontology ontology;
044        List<Term> termStack ;
045
046        public static final String TERM        = "Term";
047        public static final String TYPEDEF     = "Typedef";
048        public static final String ONTOLOGY    = "ontologys";
049        public static final String ID_KEY      = "id";
050        public static final String SYNONYM     = "synonym";
051        public static final String EXACT_SYNONYM  = "exact_synonym";
052        public static final String BROAD_SYNONYM  = "broad_synonym";
053        public static final String NARROW_SYNONYM = "narrow_synonym";
054        public static final String REL_SYNONYM = "related_synonym";
055        public static final String NAME        = "name";
056        public static final String DEF         = "def";
057        public static final String XREF_ANALOG = "xref_analog";
058        public static final String COMMENT     = "comment";
059        public static final String IS_A        = "is_a";
060        public static final String IS_OBSOLETE = "is_obsolete";
061        public static final String RELATIONSHIP = "relationship";
062        public static final String DISJOINT_FROM = "disjoint_from";
063        public static final String SUBSET       = "subset";
064        public static final String INTERSECTION_OF = "intersection_of";
065        
066                
067        public static final String ALT_ID      = "alt_id";
068        
069        boolean isTerm ;
070
071        private Term currentTerm;
072        
073        public OboFileHandler(Ontology ontology){
074                this.ontology = ontology ;
075                
076                //Term isa = onto.importTerm(OntoTools.IS_A, null);
077                //Term partof = onto.importTerm(OntoTools.PART_OF, null);;
078
079        }
080
081        public void documentEnd() {
082                // TODO Auto-generated method stub
083
084        }
085
086        public void documentStart() {
087                termStack = new ArrayList<Term>();
088        }
089
090        public void newOboFileHeader() {
091                // TODO Auto-generated method stub
092        }
093
094        public void newStanza(String stanza) {
095                //System.out.println("got a new stanza: " + stanza);
096                if ( stanza.equals(TERM)){
097                        isTerm = true;
098                        currentTerm = null;
099                } else {
100                        isTerm = false;
101                }
102                
103        }
104
105        public void newKey(String key, String value) {
106                if (isTerm) {
107                        
108                        if ( key.equals(ID_KEY)) {
109                                if ( ontology.containsTerm(key)){
110                                        currentTerm = ontology.getTerm(key);
111                                } else {
112                                        try {
113                                                if (  ontology.containsTerm(value)) {
114                                                        currentTerm = ontology.getTerm(value);
115                                                } else {
116                                                        currentTerm = ontology.createTerm(value);
117                                                }
118                                        } catch (AlreadyExistsException ex) {
119                                                ex.printStackTrace();
120                                        }
121                                        
122                                }
123                                return;
124                        } 
125                        if (currentTerm == null) {
126                                System.err.println("did not find ID for Term! ");
127                                return;
128                        }
129                        else if (key.equals(NAME)){
130                                currentTerm.setDescription(value);
131                        } else if (key.equals(DEF)){
132                                //TODO
133                                // set definition
134                                Annotation anno = currentTerm.getAnnotation();                          
135                                anno.setProperty(DEF, value);
136                        } else if (key.equals(XREF_ANALOG)){                            
137                                // set xref analog
138                                Annotation anno = currentTerm.getAnnotation();                          
139                                anno.setProperty(XREF_ANALOG, value);
140                        } else if (key.equals(IS_OBSOLETE)) {
141                                // ignore obsolete Terms...
142                                //System.out.println("obsolete: " + currentTerm);
143                                Annotation anno = currentTerm.getAnnotation();                          
144                                anno.setProperty(IS_OBSOLETE, new Boolean(true));
145                                
146                        } else if (key.equals(IS_A) || 
147                                        key.equals(RELATIONSHIP) || 
148                                        key.equals(DISJOINT_FROM) ||
149                                        key.equals(INTERSECTION_OF) ||
150                                        key.equals(SUBSET)) {
151                                try {
152                                        Term object = ontology.containsTerm(value) ?
153                                                object = ontology.getTerm(value): ontology.createTerm(value);
154                                        Term predicate = ontology.containsTerm(key) ? ontology.getTerm(key) : ontology.createTerm(key);
155                                ontology.createTriple(currentTerm, object, predicate, currentTerm + " " + predicate + " " + object, key+"-relationship");                                     
156                                } catch (AlreadyExistsException ex) {
157                                }
158                                                                
159                        } else if (key.equals(COMMENT)){
160                                Annotation anno = currentTerm.getAnnotation();                          
161                                anno.setProperty(COMMENT, value);
162                        } else if (key.equals(ALT_ID)){
163                                Annotation anno = currentTerm.getAnnotation();                          
164                                anno.setProperty(ALT_ID, value);
165                        } 
166                        
167                        else {
168                                //System.out.println("unknown key " + key);
169                        }
170                        
171                                                                                        
172                } else {
173                        //System.out.println("not a term and ignoring: " + key + " " + value);
174                }
175
176        }
177
178        public void newSynonym(Synonym synonym) {
179                if (isTerm) {
180                        currentTerm.addSynonym(synonym);
181                }
182        }
183
184}