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 java.util.Collections;
025import java.util.HashMap;
026import java.util.HashSet;
027import java.util.Map;
028import java.util.Set;
029
030import org.biojava.utils.ChangeVetoException;
031
032public class SimpleHomologeneDB 
033    extends SimpleOrthoPairCollection 
034    implements HomologeneDB
035{
036
037    // every Orthologue is stored in an OrthologueSet delegate
038    private OrthologueSet orthologues = new SimpleOrthologueSet();
039
040    // orthologies are also stored in a set
041    private Set orthologySet = new HashSet();
042
043    // indices
044    private Map orthologyByTaxonID = new HashMap();
045    private Map orthologyBySimilarityType = new HashMap();
046
047    public Orthologue createOrthologue(Taxon taxon, String locusID, String homologeneID, String accession)
048        throws ChangeVetoException
049    {
050        // create the Orthologue
051        Orthologue newOrthologue = new SimpleOrthologue(taxon, locusID, homologeneID, accession);
052
053        orthologues.addOrthologue(newOrthologue);
054        return newOrthologue;
055    }
056
057    public Orthologue createOrthologue(int taxonID, String locusID, String homologeneID, String accession)
058        throws IllegalArgumentException, ChangeVetoException
059    {
060        // create the Orthologue
061        Orthologue newOrthologue = new SimpleOrthologue(taxonID, locusID, homologeneID, accession);
062
063        orthologues.addOrthologue(newOrthologue);
064        return newOrthologue;
065   }
066
067    public Orthologue getOrthologue(String homologeneID)
068    {
069        return orthologues.getOrthologue(homologeneID);
070    }
071
072    public OrthoPair createOrthoPair(Orthologue first, Orthologue second, SimilarityType type, double percentIdentity)
073    {
074        OrthoPair newOrthoPair = new SimpleOrthoPair(first, second, type, percentIdentity);
075
076        // index it
077        indexByTaxonID(first.getTaxonID(), newOrthoPair);
078        indexByTaxonID(second.getTaxonID(), newOrthoPair);
079        indexBySimilarityType(type, newOrthoPair);
080
081        orthologySet.add(newOrthoPair);
082
083        return newOrthoPair;
084    }
085
086    // should implement a uniqueness check here later!!!!
087
088    public OrthoPair createOrthoPair(Orthologue first, Orthologue second, String ref)
089    {
090        OrthoPair newOrthoPair = new SimpleOrthoPair(first, second, ref);
091
092        // index it
093        indexByTaxonID(first.getTaxonID(), newOrthoPair);
094        indexByTaxonID(second.getTaxonID(), newOrthoPair);
095        indexBySimilarityType(SimilarityType.CURATED, newOrthoPair);
096
097        orthologySet.add(newOrthoPair);
098
099        return newOrthoPair;
100    }
101
102    public OrthoPairSet createOrthoPairSet()
103    {
104        OrthoPairSet newGroup = new SimpleOrthoPairSet();
105        groups.add(newGroup);        
106
107        return newGroup;
108    }
109
110    public OrthoPairCollection getOrthoPairSets()
111    {
112        return new SimpleOrthoPairCollection(Collections.unmodifiableSet(groups));
113    }
114
115    private void indexByTaxonID(int taxonID, OrthoPair orthology)
116    {
117        Integer taxonIDIndex = new Integer(taxonID);
118        Set indexSet = (Set) orthologyByTaxonID.get(taxonIDIndex);
119
120        if (indexSet == null) {
121            indexSet = new HashSet();
122            orthologyByTaxonID.put(taxonIDIndex, indexSet);
123        }
124
125        indexSet.add(orthology);
126    }
127
128    private void indexBySimilarityType(SimilarityType type, OrthoPair orthology)
129    {
130        Set indexSet = (Set) orthologyBySimilarityType.get(type);
131
132        if (indexSet == null) {
133            indexSet = new HashSet();
134            orthologyByTaxonID.put(type, indexSet);
135        }
136
137        indexSet.add(orthology);
138    }
139}