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.seq.impl;
023
024import org.biojava.bio.alignment.Alignment;
025import org.biojava.bio.seq.Feature;
026import org.biojava.bio.seq.FeatureHolder;
027import org.biojava.bio.seq.Sequence;
028import org.biojava.bio.seq.homol.SimilarityPairFeature;
029import org.biojava.bio.symbol.IllegalAlphabetException;
030import org.biojava.utils.ChangeEvent;
031import org.biojava.utils.ChangeSupport;
032import org.biojava.utils.ChangeVetoException;
033
034/**
035 * <code>SimpleSimilarityPairFeature</code> represents a similarity
036 * between a query sequence and a subject sequence as produced by a
037 * search program.
038 *
039 * @author Keith James
040 * @since 1.2
041 */
042public class SimpleSimilarityPairFeature extends SimpleStrandedFeature
043    implements SimilarityPairFeature
044{
045    private SimilarityPairFeature sibling;
046    private Alignment             alignment;
047    private double                score;
048
049    /**
050     * Creates a new <code>SimpleSimilarityPairFeature</code>.
051     *
052     * @param sourceSeq a <code>Sequence</code>.
053     * @param parent a <code>FeatureHolder</code>.
054     * @param template a <code>SimilarityPairFeature.Template</code>.
055     */
056    public SimpleSimilarityPairFeature(Sequence                       sourceSeq,
057                                       FeatureHolder                  parent,
058                                       SimilarityPairFeature.Template template)
059        throws IllegalAlphabetException                 
060    {
061        super(sourceSeq, parent, template);
062
063        this.sibling   = template.sibling;
064        this.alignment = template.alignment;
065        this.score     = template.score;
066    }
067
068    /**
069     * <code>getSibling</code> returns the sibling feature of the
070     * pair.
071     *
072     * @return a <code>Feature</code>.
073     */
074    public SimilarityPairFeature getSibling()
075    {
076        return sibling;
077    }
078
079    public void setSibling(SimilarityPairFeature sibling)
080        throws ChangeVetoException
081    {
082        if (hasListeners())
083        {
084            ChangeSupport cs = getChangeSupport(SimilarityPairFeature.SIBLING);
085            synchronized(cs)
086            {
087                ChangeEvent ce = new ChangeEvent(this, SimilarityPairFeature.SIBLING,
088                                                 this.sibling, sibling);
089                cs.firePreChangeEvent(ce);
090                this.sibling = sibling;
091                cs.firePostChangeEvent(ce);
092            }
093        }
094        else
095        {
096            this.sibling = sibling;
097        }
098    }
099
100    /**
101     * <code>getAlignment</code> returns the alignment between the two
102     * features.
103     *
104     * @return an <code>Alignment</code>.
105     */
106    public Alignment getAlignment()
107    {
108        return alignment;
109    }
110
111    /**
112     * <code>getScore</code> returns the alignment score.
113     *
114     * @return a <code>double</code>.
115     */
116    public double getScore()
117    {
118        return score;
119    }
120
121    public Feature.Template makeTemplate()
122    {
123        SimilarityPairFeature.Template ft = new SimilarityPairFeature.Template();
124        fillTemplate(ft);
125        return ft;
126    }
127
128    protected void fillTemplate(SimilarityPairFeature.Template ft)
129    {
130        super.fillTemplate(ft);
131        ft.sibling = getSibling();
132        ft.alignment = getAlignment();
133        ft.score = getScore();
134    }
135
136    public String toString()
137    {
138        return super.toString() + " [score " + score + "]";
139    }
140}