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

import compid.sequences.AminoSequence;
import net.aavalla.utils.general.CryptoUtils;
import java.util.HashMap;

/**
 *
 * @author Lari Natri
 */
public final class ProtSeq extends Tuple {
    public static final int MAXSEQUENCELENGTH = 32000; // must be in sync with DB
    public static final String TABLELABEL = "PROTSEQ";

    private static final int FLAG_TRUNCATED = 1;
    private static final int FLAG_ALTERED = 2;
    private static final int FLAG_UNKNOWN = 32768;
    
    // TODO: lazily generate string from amino seq? shouldn't be used in equals() in that case, mm..
    private final AminoSequence sequence;
    private String digestMD5 = null; // this is lazily generated
    private int status = 0;

    public ProtSeq(String sequence) {
        this(sequence,false);
    }

    public ProtSeq(String sequence, int status) {
        this(sequence,false);
        this.status = status;
    }
    
    public ProtSeq(final String sequence, boolean noMaximumLength) {

        if (sequence==null)
            throw new NullPointerException("Sequence must be set.");
        String seq = sequence;
        if (noMaximumLength==false && sequence.length() > MAXSEQUENCELENGTH) {
            seq = seq.substring(0,MAXSEQUENCELENGTH).toUpperCase();
            this.status = this.status | FLAG_TRUNCATED;
        } else {
            seq = sequence.toUpperCase();
        }
        if (seq.contains("J") || seq.contains("O")) {
            seq = seq.replaceAll("[JO]", "X");
            this.status = this.status | FLAG_ALTERED;
        }

        this.sequence = new AminoSequence(seq);

        if (this.sequence.getLength() < 1) {
            this.status = this.status | FLAG_UNKNOWN;
        }

        this.setTupleHasNoForeignKeys();       
    }

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

    public int getLength() {
        return this.sequence.getLength();
    }

    public boolean isTruncated() { 
        return (this.status & FLAG_TRUNCATED) == FLAG_TRUNCATED;
    }

    public boolean isAltered() { 
        return (this.status & FLAG_ALTERED) == FLAG_ALTERED;
    }

    public boolean isUnknown() {
        return (this.status & FLAG_UNKNOWN) == FLAG_UNKNOWN;
    }

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

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


    @Override
    public String getTableLabel() {
        return TABLELABEL;
    }

    @Override
    public HashMap<String,Object> getColsAndValues() {
        String seqStr = this.sequence.toString();
        setDigest(seqStr);
        HashMap<String,Object> map = new HashMap<String,Object>(2,1);
        map.put("DIGEST", this.digestMD5);
        map.put("SEQUENCE", seqStr);
        map.put("STATUS", this.status);
        return map;
    }
    
    @Override
    public synchronized HashMap<String,Object> getKeysAndValuesForExistenceCheck() {
        if (this.digestMD5==null) {
            setDigest(this.sequence.toString());
        }
        HashMap<String,Object> map = new HashMap<String,Object>(1,1);
        map.put("DIGEST", this.digestMD5);
        return map;
    }

    private synchronized final void setDigest(String seqStr) {
        if (this.digestMD5==null) {
            this.digestMD5 = CryptoUtils.MD5(seqStr);
        }
    }
 
}
