/*
 * 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.sequences;

import java.util.HashMap;
import java.util.ListIterator;

/**
 * Instances of this class hold the results of a sequence alignment, usually
 * done in classes derived from Aligner.
 * @author Lari Natri
 */
public class Alignment
{

    private final Sequence[] origSeqs;   // original sequences
    private final double score;          // score of this alignment
    private final int substitutionCount; // number of substitutions in the al.
    private final HashMap<Integer,Integer>[] gapLocations;

    /**
     * Constructor.
     * @param sequences    Original Sequences that have been aligned
     * @param score        score of this alignment
     * @param substCount   number of substitutions in this alignment
     * @param gapLocations locations of gaps as an array of HashMaps. Each
     *                     array index must correspond to the sequences indexes.
     *                     HashMap keys are indexes to corresponding sequence,
     *                     and HashMap value holds the number of gaps at that
     *                     particular spot in the sequence. 0-Length gaps do not
     *                     need to be included in the map. Also, this parameter
     *                     can be null if there are no gaps in the alignment. 
     */
    Alignment(Sequence[] sequences, double score, int substCount,
              HashMap<Integer,Integer>[] gapLocations)
    {
        if (sequences==null) throw new NullPointerException();
        this.origSeqs = sequences;
        this.score = score;
        this.substitutionCount = substCount;
        this.gapLocations = gapLocations;
    }

    /** Returns the count of sequences in this alignment */
    public int sequenceCount()
    {
        return this.origSeqs.length;
    }

    /** Returns the score of this alignment. */
    public double getScore()
    {
        return this.score;
    }

    /** Returns the amount of substitutions in this alignment. */
    public int getSubstitutionCount()
    {
        return this.substitutionCount;
    }

    /**
     * Returns one of the original aligned sequences.
     * @param index of the wanted sequence
     */
    public Sequence getSequence(int index)
    {
        if (index<0 || index>=this.origSeqs.length)
            throw new IndexOutOfBoundsException();
        return this.origSeqs[index];
    }

    /**
     * Returns one of the aligned sequences with added gaps as a string. String
     * representation is gotten using standard toString() methods of the
     * components.
     * @param index of the wanted sequence
     */
    public String asAlignedString(int index)
    {
        if (index<0 || index>=this.origSeqs.length)
            throw new IndexOutOfBoundsException();
        
        StringBuilder sb = new StringBuilder();
        GapComponent gapComponent = GapComponent.createGapComponent();
        ListIterator<Component> iter = origSeqs[index].iterator();
        int i = 0;
        while (iter.hasNext()) { // iterates through the sequence
           
            if (gapLocations!=null && gapLocations[index]!=null
                                   && gapLocations[index].containsKey(i)) {
                Integer gapsHere = gapLocations[index].get(i);
                for (int g=0; g<gapsHere; ++g) {
                    sb.append(gapComponent);
                }
            }

            sb.append(iter.next());
            ++i;
        }

        // adds the possible trailing gaps
        if (gapLocations!=null && gapLocations[index]!=null
                               && gapLocations[index].containsKey(i)) {
            Integer gapsHere = gapLocations[index].get(i);
            for (int g=0; g<gapsHere; ++g) {
                sb.append(gapComponent);
            }
        }
        
        return sb.toString();
    }
    
    
    // TODO: getAsAlignedSequence(int index) [including gaps]

}
