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 01-21-2010
021 */
022package org.biojava.nbio.core.sequence.io;
023
024import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
025import org.biojava.nbio.core.sequence.DNASequence;
026import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
027import org.biojava.nbio.core.sequence.io.template.SequenceCreatorInterface;
028import org.biojava.nbio.core.sequence.io.template.SequenceParserInterface;
029import org.biojava.nbio.core.sequence.loader.SequenceFileProxyLoader;
030import org.biojava.nbio.core.sequence.template.AbstractSequence;
031import org.biojava.nbio.core.sequence.template.CompoundSet;
032import org.biojava.nbio.core.sequence.template.ProxySequenceReader;
033
034import java.io.File;
035import java.io.IOException;
036import java.util.List;
037
038/**
039 * This class is a good example of using the SequenceCreatorInterface where during parsing of the stream
040 * the sequence and the offset index are passed to create a Protein sequence that will be loaded in lazily.
041 * This way you can load very large fasta files and store accession id and delay loading the sequence to save
042 * memory. The index is the file stream offset so when a DNASequence has a call to getSequence() the
043 * SequenceFileProxyLoader will open the file and offset to the index and retrieve the sequence.
044 *
045 * Same approach can be used for genome sequence data stored in a local fasta file, in a database or via http
046 * interface to a remote server
047 *
048 * @author Scooter Willis <willishf at gmail dot com>
049 */
050public class FileProxyDNASequenceCreator implements
051                SequenceCreatorInterface<NucleotideCompound> {
052
053        CompoundSet<NucleotideCompound> compoundSet = null;
054        File file = null;
055        SequenceParserInterface sequenceParser;
056
057        /**
058         * Need File so that we can store full path name in SequenceFileProxyLoader for Random File access as a quick read
059         * @param fastaFile
060         * @param compoundSet
061         */
062        public FileProxyDNASequenceCreator(File file,
063                        CompoundSet<NucleotideCompound> compoundSet,
064                        SequenceParserInterface sequenceParser) {
065                this.compoundSet = compoundSet;
066                this.file = file;
067                this.sequenceParser = sequenceParser;
068        }
069
070        /**
071         * Even though we are passing in the sequence we really only care about the length of the sequence and the offset
072         * index in the fasta file.
073         * @param sequence
074         * @param index
075         * @return
076         * @throws CompoundNotFoundException
077         * @throws IOException
078         */
079        @Override
080        public AbstractSequence<NucleotideCompound> getSequence(String sequence, long index ) throws CompoundNotFoundException, IOException {
081                SequenceFileProxyLoader<NucleotideCompound> sequenceFileProxyLoader = new SequenceFileProxyLoader<NucleotideCompound>(
082                                file,
083                                sequenceParser,
084                                index,
085                                sequence.length(),
086                                compoundSet);
087                return new DNASequence(sequenceFileProxyLoader, compoundSet);
088        }
089
090        /**
091         * Should be able to extend the same concept to a remote URL call or database connection. Not supported yet
092         * @param proxyLoader
093         * @param index
094         * @return
095         */
096        @Override
097        public AbstractSequence<NucleotideCompound> getSequence(
098                        ProxySequenceReader<NucleotideCompound> proxyLoader, long index) {
099                throw new UnsupportedOperationException("Not supported yet.");
100        }
101
102        /**
103         * Not sure of use case and currently not supported
104         * @param list
105         * @return
106         */
107        @Override
108        public AbstractSequence<NucleotideCompound> getSequence(
109                        List<NucleotideCompound> list) {
110                throw new UnsupportedOperationException("Not supported yet.");
111        }
112}