/*
 * Copyright 2009, 2010 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 java.util.HashMap;

/**
 *
 * @author Lari Natri
 */
public final class Protein extends Tuple{
    
    public static final String TABLELABEL = "PROTEIN";
    
    private final String accession;
    private final String description;
    private final String species;
    private ProtDbInfo protDbInfo = null;
    private ProtSeq protSeq = null;

    public Protein(String accession, String description, String species) {
        this(accession, description, species, null, null);
    }

    public Protein(String accession, String description, String species, ProtDbInfo protDbInfo, ProtSeq protSeq) {
        this.accession = accession;
        this.description = description;
        if (species==null || species.length()==0) {
            int speciesStartIdx = description.indexOf("[");
            int speciesEndIdx = description.indexOf("]");
            if (speciesStartIdx>=0 && speciesEndIdx>speciesStartIdx+1) {
                this.species = description.substring(speciesStartIdx+1,speciesEndIdx);
            } else this.species = "";
        } else {
            this.species = species;
        }
        this.protDbInfo = protDbInfo;
        this.protSeq = protSeq;

        if (this.protDbInfo==null) {
            this.protDbInfo = ProtDbInfo.getProtDbByAccession(accession);
            this.protDbInfo.setPrimaryKey(1);
        }

        if (this.protSeq==null) {
            this.protSeq = new ProtSeq("");
            this.protSeq.setPrimaryKey(1);
        }

    }
    
    public String getAccession() { return this.accession; }
    public String getDescription() { return this.description; }
    public ProtDbInfo getProtDbInfo() { return this.protDbInfo; }
    public ProtSeq getProtSeq() { return this.protSeq; }

    /**
     * Sets ProtSeq object. Intended to be used by protein db importers and
     * FullDataset.
     * @param newProtSeq new ProtSeq to set
     * @return old ProtSeq object
     */
    public ProtSeq setProtSeq(ProtSeq newProtSeq) {
        ProtSeq old = this.protSeq;
        this.protSeq = newProtSeq;
        return old;
    }


    /**
     * Sets ProtDb object. Intended to be used by FullDataset.
     * @param newProtDb new ProtDbInfo to set
     * @return old ProtDbInfo object
     */
    public ProtDbInfo setProtDb(ProtDbInfo newProtDb) {
        ProtDbInfo old = this.protDbInfo;
        this.protDbInfo = newProtDb;
        return old;
    }

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

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




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

    @Override
    public HashMap<String,Object> getColsAndValues() {
        HashMap<String,Object> map = new HashMap<String,Object>(3,1);
        map.put("ACCESSION", this.accession);
        map.put("DESCRIPTION", this.description);
        map.put("SPECIES", this.species);
        return map;
    }
    
    @Override
    public HashMap<String,Object> getKeysAndValuesForExistenceCheck() {
        HashMap<String,Object> map = new HashMap<String,Object>(1,1);
        map.put("ACCESSION", this.accession);
        return map;
    }    
    
}
