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


/**
 *
 * @author Lari Natri
 */
public class AminoSequence extends Sequence
{
    private AminoSequence() {}

    public AminoSequence(String aminoSeq)
    {
        aminoSeq = aminoSeq.trim().toUpperCase();
        ArrayList<AminoAcid> aminoList = new ArrayList<AminoAcid>(aminoSeq.length());

        for (int i = 0; i < aminoSeq.length(); ++i) {
            AminoAcid aa = AminoAcid.produceAminoAcid(aminoSeq.charAt(i));
            if (aa!=null) { // skip non-aminoacids
                aminoList.add(aa);
            }
        }

        AminoAcid[] aminos = new AminoAcid[aminoList.size()];
        aminoList.toArray(aminos);
        aminoList.clear(); // to ensure GC
        aminoList = null;
        
        this.initializeSequenceUnsafe(aminos);
    }

    public AminoSequence(AminoAcid[] aminoSeq, boolean looseEquality)
    {
        try {
            this.initializeSequence(aminoSeq);
        } catch (Exception ex) {
        } // never happens
    }

    @Override
    public boolean isCompatible(Sequence seq)
    {
        if (seq == null || seq.getClass() != this.getClass()) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean isCompatible(Component sc)
    {
        if (sc.getClass() == AminoAcid.class) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public AminoSequence getCompatibleComponents()
    {
        return AminoAcid.getAllAminoAcids();
    }

    /** Returns true if all characters in given String can be mapped
     *  to AminoAcids.
     */
    public static boolean isAminoSequence(String seq) {
        if (seq==null) return false;
        for (int i=0; i<seq.length(); ++i) {
            if (!AminoAcid.isAminoAcidSymbol(seq.charAt(i))) return false;
        }
        return true;
    }
   

}
