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 */
021
022package org.biojava.bio.program.homologene;
023
024import org.biojava.utils.ChangeVetoException;
025
026/**
027 * A simple no-frills implementation of the HomologeneBuilder interface.
028 * Used to instantiate a in-memory copy of the Homologene data.
029 *
030 * @author David Huen
031 */
032public class SimpleHomologeneBuilder implements HomologeneBuilder
033{
034    HomologeneDB db = null;
035
036    OrthoPairTemplate orthologyTmpl = null;
037
038    OrthologueTemplate orthologueTmpl = null;
039
040    OrthoPairSet group = null;
041
042    private int level = 0;
043
044
045    private class OrthoPairTemplate
046    {
047        Orthologue     firstOrtho;
048        Orthologue     secondOrtho;
049        SimilarityType type;
050        double         percentIdentity;
051        String         ref;
052    }
053
054    private class OrthologueTemplate
055    {
056        int taxonID = - 9999;
057        String locusID = null;
058        String homologeneID = null;
059        String accession = null;
060    }
061
062    public void startDB()
063    {
064        if (level != 0) return;
065
066        db = new SimpleHomologeneDB();
067        level++;
068    }
069
070    public void startGroup()
071    {
072        if (level != 1) return;
073
074        group = db.createOrthoPairSet();
075        level++;
076    }
077
078    public void startOrthoPair()
079    {
080        if (level != 2) return;
081
082        orthologyTmpl = new OrthoPairTemplate();
083        level++;
084    }
085
086    public void startOrthologue()
087    {
088        if (level != 3) return;
089
090        orthologueTmpl = new OrthologueTemplate();
091        level++;
092    }
093
094    public void addOrthologueProperty(String key, String value)
095    {
096        if (level != 4) return;
097
098        // the property can be taxon id, locus id, homologene id
099        if (orthologueTmpl != null) {
100            if (key.equals(TAXONID)) {
101                orthologueTmpl.taxonID = Integer.parseInt(value);
102            }
103            else if (key.equals(LOCUSID)) {
104                orthologueTmpl.locusID = value;
105            }
106            else if (key.equals(HOMOID)) {
107                orthologueTmpl.homologeneID = value;
108            }
109            else if (key.equals(ACCESSION)) {
110                orthologueTmpl.accession = value;
111            }
112        }
113    }
114
115    public void endOrthologue()
116    {
117        if (level != 4) return;
118        level--;
119
120        // validate the template
121        if ((orthologueTmpl.taxonID == -9999) 
122           || (orthologueTmpl.homologeneID == null)
123           || (orthologueTmpl.accession == null))
124        {
125//            System.out.println(orthologueTmpl.taxonID + " " + orthologueTmpl.homologeneID + " " + orthologueTmpl.accession);
126//            System.out.println("endOrthologue test failed");
127            return;
128        }
129        // get the taxon
130        Taxon taxon;
131        if ((taxon = HomologeneTools.getTaxon(orthologueTmpl.taxonID)) == null) 
132        {
133            try {
134//                System.out.println("failed taxon lookup for " + orthologueTmpl.taxonID);
135                taxon = HomologeneTools.createTaxon(orthologueTmpl.taxonID, "Unknown species " + orthologueTmpl.taxonID);
136            }
137            catch (DuplicateTaxonException dte) {}
138        }
139
140        // create the Orthologue
141        Orthologue orthologue;
142        if ((orthologue = db.getOrthologue(orthologueTmpl.homologeneID)) == null) {
143            orthologue = new SimpleOrthologue(taxon, orthologueTmpl.locusID, orthologueTmpl.homologeneID, orthologueTmpl.accession);
144        }
145
146        // fill in the orthology template
147        if (orthologyTmpl.firstOrtho == null) 
148            orthologyTmpl.firstOrtho = orthologue;
149        else
150            orthologyTmpl.secondOrtho = orthologue;
151 
152        orthologueTmpl = null;
153    }
154
155    public void addOrthoPairProperty(String key, String value)
156    {
157        if (level != 3) return;
158
159           if (key.equals(SIMILARITYTYPE)) {
160               String type = value.trim();
161
162               if (type.equals(TWIN)) {
163                   orthologyTmpl.type = SimilarityType.TWIN;
164               }
165               else if (type.equals(MULTIPLE)) {
166                   orthologyTmpl.type = SimilarityType.MULTIPLE;
167               }
168               else if (type.equals(CURATED)) {
169                   orthologyTmpl.type = SimilarityType.CURATED;
170               }
171           }
172           else if (key.equals(PERCENTIDENTITY)) {
173               orthologyTmpl.percentIdentity = Double.parseDouble(value);
174           }
175           else if (key.equals(REFERENCE)) {
176               orthologyTmpl.ref = value;
177           }
178    }
179
180    public void endOrthoPair()
181    {
182        if (level !=3) return;
183        level--;
184
185        // validate template
186        if ((orthologyTmpl.type == null) 
187            || (orthologyTmpl.firstOrtho == null) 
188            || (orthologyTmpl.secondOrtho == null)) 
189        {
190            System.out.println(orthologyTmpl.type + " " + orthologyTmpl.firstOrtho + " " + orthologyTmpl.secondOrtho);
191            System.out.println("endOrthoPair test failed"); return;
192        }
193
194        try {
195            if (orthologyTmpl.type == SimilarityType.CURATED) {
196                if (orthologyTmpl.ref == null) return;
197
198                OrthoPair orthology = db.createOrthoPair(
199                    orthologyTmpl.firstOrtho,
200                    orthologyTmpl.secondOrtho,
201                    orthologyTmpl.ref);
202
203                group.addOrthoPair(orthology);
204            }
205            else {
206                OrthoPair orthology = db.createOrthoPair(
207                    orthologyTmpl.firstOrtho,
208                    orthologyTmpl.secondOrtho,
209                    orthologyTmpl.type,
210                    orthologyTmpl.percentIdentity);
211
212                group.addOrthoPair(orthology);
213            }
214        }
215        catch (ChangeVetoException cve) { 
216            // is it worth reporting? 
217        }
218
219    }
220
221    public void addTitle(int taxonID, String homologeneID, String title)
222    {
223        // retrieve the Orthologue
224        Orthologue ortho = db.getOrthologue(homologeneID);
225
226        if (ortho != null) {
227            ortho.setTitle(title);
228        }
229    }
230
231    public void endGroup()
232    {
233        if (level != 2) return;
234
235        level--;
236    }
237
238    public void endDB()
239    {
240        if (level != 1) return;
241
242        level--;
243    }
244
245    public HomologeneDB getDB()
246    {
247        return db;
248    }
249}
250