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 * Created on June 21, 2010
021 * Author: Mark Chapman
022 */
023
024package org.biojava.nbio.alignment;
025
026import org.biojava.nbio.core.alignment.template.SequencePair;
027import org.biojava.nbio.alignment.template.*;
028import org.biojava.nbio.core.sequence.template.Compound;
029import org.biojava.nbio.core.sequence.template.Sequence;
030
031/**
032 * Implements an algorithm which computes a score for a sequence alignment pair.  The reported score is the number of
033 * alignment columns which have similar {@link Compound}s.
034 *
035 * @author Mark Chapman
036 * @param <S> each {@link Sequence} of the alignment pair is of type S
037 * @param <C> each element of an {@link AlignedSequence} is a {@link Compound} of type C
038 */
039public class FractionalSimilarityScorer<S extends Sequence<C>, C extends Compound> extends AbstractScorer
040                implements PairwiseSequenceScorer<S, C> {
041
042        // always stored
043        private S query, target;
044        private int max, score;
045
046        // optional cached input field
047        private PairwiseSequenceAligner<S, C> aligner;
048
049        /**
050         * Creates a fractional similarity scorer for a pair of sequences aligned by the given pairwise sequence aligner.
051         *
052         * @param aligner a pairwise sequence aligner
053         */
054        public FractionalSimilarityScorer(PairwiseSequenceAligner<S, C> aligner) {
055                query = aligner.getQuery();
056                target = aligner.getTarget();
057                this.aligner = aligner;
058        }
059
060        /**
061         * Creates a fractional similarity scorer for an aligned pair of sequences.
062         *
063         * @param pair an aligned pair of sequences
064         */
065        public FractionalSimilarityScorer(SequencePair<S, C> pair) {
066                query = pair.getQuery().getOriginalSequence();
067                target = pair.getTarget().getOriginalSequence();
068                max = pair.getLength();
069                score = pair.getNumSimilars();
070        }
071
072        // methods for PairwiseSequenceScorer
073
074        @Override
075        public S getQuery() {
076                return query;
077        }
078
079        @Override
080        public S getTarget() {
081                return target;
082        }
083
084        // methods for Scorer
085
086        @Override
087        public double getMaxScore() {
088                if (aligner != null) {
089                        align();
090                }
091                return max;
092        }
093
094        @Override
095        public double getMinScore() {
096                return 0;
097        }
098
099        @Override
100        public double getScore() {
101                if (aligner != null) {
102                        align();
103                }
104                return score;
105        }
106
107        // helper method for initialization from an aligner
108        private void align() {
109                max = aligner.getPair().getLength();
110                score = aligner.getPair().getNumSimilars();
111                aligner = null;
112        }
113
114}