/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.data;


import compid.data.tuples.PeptideHit;
import compid.sequences.AminoSequence;
import net.aavalla.utils.general.RetrievalHashSet;
import java.util.Collection;

/**
 *
 * @author Lari Natri
 */
public class PeptideSequenceWithHits implements Comparable {

    private final RetrievalHashSet<PeptideHit> hits;
    private final AminoSequence sequence;

    public PeptideSequenceWithHits(String sequence, Collection<PeptideHit> hits)
            throws Exception
    {
        this(new AminoSequence(sequence),hits);
    }

    public PeptideSequenceWithHits(AminoSequence sequence,
                                   Collection<PeptideHit> hits)
    {
        if (sequence==null) throw new NullPointerException();
        this.sequence = sequence;
        if (hits==null) {
            this.hits = new RetrievalHashSet<PeptideHit>(0);
        } else {
            this.hits = new RetrievalHashSet<PeptideHit>(hits);
        }
    }

    public AminoSequence getSequence() { return this.sequence; }

    public int getHitCount() { return hits.size(); }

    public PeptideHit getGoodRepresentingHit() {
        PeptideHit chosen = null;
        double chosenScore = -2;
        for (PeptideHit pepHit : hits) {
            double score = pepHit.getComparableConfidence();
            if (chosen==null || score>chosenScore) {
                chosen = pepHit;
                chosenScore = score;
            }
        }
        return chosen;
    }

    public RetrievalHashSet<PeptideHit> getHits() {
        return new RetrievalHashSet<PeptideHit>(this.hits);
    }

    public boolean addHit(PeptideHit pepHit) {
        return this.hits.add(pepHit);
    }


    @Override
    public int compareTo(Object obj)
    {
        return sequence.toString().compareTo(((PeptideSequenceWithHits)obj).sequence.toString());
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PeptideSequenceWithHits other = (PeptideSequenceWithHits) obj;
        if (this.sequence != other.sequence && (this.sequence == null || !this.sequence.equals(other.sequence))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        int hash = 5;
        hash = 37 * hash + (this.sequence != null ? this.sequence.hashCode() : 0);
        return hash;
    }

}
