001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on DATE
021 *
022 */
023package org.biojava.nbio.core.sequence;
024
025import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
026import org.biojava.nbio.core.sequence.compound.DNACompoundSet;
027import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
028import org.biojava.nbio.core.sequence.template.CompoundSet;
029import org.biojava.nbio.core.sequence.template.SequenceReader;
030
031import java.util.LinkedHashMap;
032
033/**
034 * A ChromosomeSequence is a DNASequence but keeps track of geneSequences
035 * @author Scooter Willis
036 */
037public class ChromosomeSequence extends DNASequence {
038
039        private int chromosomeNumber;
040        private LinkedHashMap<String, GeneSequence> geneSequenceHashMap = new LinkedHashMap<String, GeneSequence>();
041
042        /**
043         * Empty constructor used by tools that need a proper Bean that allows the actual
044         * sequence data to be set after construction. Not recommended
045         */
046        public ChromosomeSequence() {
047//        throw new UnsupportedOperationException("Null constructor not supported");
048        }
049
050        /**
051         * String is king and assume DNA
052         * @param seqString
053         * @throws CompoundNotFoundException
054         */
055        public ChromosomeSequence(String seqString) throws CompoundNotFoundException {
056                super(seqString, DNACompoundSet.getDNACompoundSet());
057        }
058
059        /**
060         * Fairly important constructor given the size of a ChromsomeSequence where the
061         * ProxySequenceReader could load from disk via RandomAccessFile so that the sequence
062         * doesn't need to be kept in memory. Could also be a NCBI proxy to load sequence
063         * data as needed from remote web server.
064         * @param proxyLoader
065         */
066        public ChromosomeSequence(SequenceReader<NucleotideCompound> proxyLoader) {
067                super(proxyLoader, DNACompoundSet.getDNACompoundSet());
068        }
069
070        /**
071         * Allows the creation of a ChromosomeSequence using String for the sequence with a custom CompoundSet
072         * @param seqString
073         * @param compoundSet
074         * @throws CompoundNotFoundException
075         */
076        public ChromosomeSequence(String seqString, CompoundSet<NucleotideCompound> compoundSet) throws CompoundNotFoundException {
077                super(seqString, compoundSet);
078        }
079
080        /**
081         * Allows the creation of a ChromosomeSequence using a ProxyResequenceReader for the sequence with a custom CompoundSet
082         * @param proxyLoader
083         * @param compoundSet
084         */
085        public ChromosomeSequence(SequenceReader<NucleotideCompound> proxyLoader, CompoundSet<NucleotideCompound> compoundSet) {
086                super(proxyLoader, compoundSet);
087        }
088
089        /**
090         * @return the chromosomeNumber
091         */
092        public int getChromosomeNumber() {
093                return chromosomeNumber;
094        }
095
096        /**
097         * @param chromosomeNumber the chromosomeNumber to set
098         */
099        public void setChromosomeNumber(int chromosomeNumber) {
100                this.chromosomeNumber = chromosomeNumber;
101        }
102
103        /**
104         * Get the list of genes that have been added to the ChromosomeSequence where accession.toString is the key.
105         * The list retains the order the genes are added
106         * @return
107         */
108
109        public LinkedHashMap<String, GeneSequence> getGeneSequences() {
110                return geneSequenceHashMap;
111        }
112
113        /**
114         *
115         * @param accession
116         * @return
117         */
118        public GeneSequence removeGeneSequence(String accession) {
119                return geneSequenceHashMap.remove(accession);
120        }
121
122        /**
123         * Add a gene to the chromosome sequence using bioIndexing starts at 1 instead of 0. The
124         * GeneSequence that is returned will have a reference to parent chromosome sequence
125         * which actually contains the sequence data. Strand is important for positive and negative
126         * direction where negative strand means we need reverse complement. If negative strand then
127         * bioBegin will be greater than bioEnd
128         *
129         *
130         * @param accession
131         * @param begin
132         * @param end
133         * @param strand
134         * @return
135         */
136        public GeneSequence addGene(AccessionID accession, int bioBegin, int bioEnd, Strand strand) {
137                GeneSequence geneSequence = new GeneSequence(this, bioBegin, bioEnd, strand);
138                geneSequence.setAccession(accession);
139                geneSequenceHashMap.put(accession.toString(), geneSequence);
140                return geneSequence;
141        }
142
143        /**
144         * Get the gene based on accession. Will return null if not found
145         * @param accession
146         * @return
147         */
148        public GeneSequence getGene(String accession) {
149                return geneSequenceHashMap.get(accession);
150        }
151}