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
024/**
025 * A no-frills implementation of the OrthoPair interface
026 *
027 * @author David Huen
028 */
029public class SimpleOrthoPair implements OrthoPair
030{
031    private Orthologue first;
032    private Orthologue second;
033    private SimilarityType type;
034    private double percentIdentity = 0.0;
035    private String ref = null;
036
037    /**
038     * constructor for the computed form
039     * of an orthology relationship.
040     *     
041     */
042    public SimpleOrthoPair(
043        Orthologue first, 
044        Orthologue second, 
045        SimilarityType type, 
046        double percentIdentity
047        )
048    {
049        // validate the parameters
050        if ((first == null) || (second == null)) throw new IllegalArgumentException();
051        if ((type != SimilarityType.MULTIPLE) && (type != SimilarityType.TWIN))
052            throw new IllegalArgumentException();
053
054        // we always store the orthologies in ascending Taxon ID
055        if (first.getTaxonID() < second.getTaxonID()) {
056            this.first = first;
057            this.second = second;
058        }
059        else {
060            this.first = second;
061            this.second = first;
062        }
063
064        this.type = type;
065        this.percentIdentity = percentIdentity;
066    }
067
068    /**
069     * constructor for the curated form
070     * of an orthology relationship
071     */
072    public SimpleOrthoPair(
073        Orthologue first,
074        Orthologue second,
075        String ref
076        )
077    {
078        // validate the parameters
079        if ((first == null) || (second == null)) throw new IllegalArgumentException();
080        if (ref == null) throw new IllegalArgumentException();
081
082        this.first = first;
083        this.second = second;
084        this.type = SimilarityType.CURATED;
085    }
086
087    public Orthologue getFirstOrthologue() { return first; }
088    public Orthologue getSecondOrthologue() { return second; }
089    public SimilarityType getSimilarity() { return type; }
090    public double getPercentIdentity() { return percentIdentity; }
091    public String getRef() { return ref; }
092
093    public boolean equals(Object o)
094    {
095        if (!(o instanceof OrthoPair)) return false;
096
097        OrthoPair other = (OrthoPair) o;
098
099        // we do not need to reverse the relationship
100        // since we store the entries in ascending taxonID
101        // and Homologene does not model same-species paralogy.
102
103        if (other.getFirstOrthologue().equals(first)) return false;
104        if (other.getSecondOrthologue().equals(second)) return false;
105
106        if (other.getSimilarity() != type) return false;
107
108        // we do not check other fields as uniqueness ought to
109        // have been determined already.  There should not
110        // be another OrthoPair with the same orthologues
111        // and SimilarityType.
112
113        return true;
114    }
115}