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.HashSet;
025import java.util.Set;
026
027import org.biojava.utils.ChangeEvent;
028import org.biojava.utils.ChangeSupport;
029import org.biojava.utils.ChangeVetoException;
030
031/**
032 * a no-frills implementation of a Homologene Group
033 *
034 * @author David Huen
035 */
036public class SimpleOrthoPairSet
037    extends AbstractOrthoPairSet
038{
039    String name;
040    Set orthologies = new HashSet();
041
042    public class Iterator implements OrthoPairSet.Iterator
043    {
044        private java.util.Iterator setIterator;
045
046        private Iterator(java.util.Iterator setIterator)
047        {
048            this.setIterator = setIterator;
049        }
050
051        public boolean hasNext()
052        {
053            return setIterator.hasNext();
054        }
055
056        public OrthoPair nextOrthoPair()
057        {
058            return (OrthoPair) setIterator.next();
059        }
060
061    }
062
063    {
064        generateChangeSupport();
065    }
066
067    public String getName()
068    {
069        return name;
070    }
071
072    public void setName(String name)
073    {
074        this.name = name;
075    }
076
077    public void addOrthoPair(OrthoPair orthology)
078        throws ChangeVetoException
079    {
080        if (!hasListeners()) {
081            orthologies.add(orthology);
082        }
083        else {
084            // get the change support
085            ChangeSupport cs = getChangeSupport(OrthoPairSet.MODIFY);
086
087            synchronized(cs) {
088                ChangeEvent ce = new ChangeEvent(this, OrthoPairSet.MODIFY);
089                cs.firePreChangeEvent(ce);
090                orthologies.add(orthology);
091                cs.firePostChangeEvent(ce);
092            }
093        }
094    }
095
096    public void removeOrthoPair(OrthoPair orthology)
097        throws ChangeVetoException
098    {
099        if (!hasListeners()) {
100            orthologies.remove(orthology);
101        }
102        else {
103            // get the change support
104            ChangeSupport cs = getChangeSupport(OrthoPairSet.MODIFY);
105
106            synchronized(cs) {
107                ChangeEvent ce = new ChangeEvent(this, OrthoPairSet.MODIFY);
108                cs.firePreChangeEvent(ce);
109                orthologies.remove(orthology);
110                cs.firePostChangeEvent(ce);
111            }
112        }
113    }
114
115    public OrthoPairSet.Iterator iterator()
116    {
117        return new Iterator(orthologies.iterator());
118    }
119
120    public double getMinIdentity()
121    {
122        double min = 100.0;
123
124        for (java.util.Iterator orthologiesI = orthologies.iterator();
125             orthologiesI.hasNext(); )
126        {
127            OrthoPair currOrthoPair = (OrthoPair) orthologiesI.next();
128
129            min = Math.min(min, currOrthoPair.getPercentIdentity());
130        }
131
132        return min;
133    }
134
135    public int size()
136    {
137        return orthologies.size();
138    }
139
140    public Set getTaxa()
141    {
142        Set taxa = new HashSet();
143
144        for (java.util.Iterator orthoI = orthologies.iterator(); orthoI.hasNext(); ) {
145            OrthoPair currOrtho = (OrthoPair) orthoI.next();
146
147            // look up the Taxon
148            taxa.add( currOrtho.getFirstOrthologue().getTaxon());
149            taxa.add( currOrtho.getSecondOrthologue().getTaxon());
150        }
151
152        return taxa;
153    }
154}