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 */
021package org.biojava.nbio.genome.parsers.twobit;
022
023import java.io.File;
024
025/**
026 * A facade that makes it easier to work with a 2bit file.
027 *
028 * Created by yana on 3/27/17.
029 */
030public class TwoBitFacade {
031
032        private TwoBitParser twoBitParser = null;
033
034
035        /**
036         *  Reads a genome from a locally stored .2bit file.
037         *
038         *  @param file the File to a .2bit file.
039         */
040        public TwoBitFacade(File file) throws Exception {
041                twoBitParser = new TwoBitParser(file);
042        }
043
044        /**
045         *  Closes .2bit file twoBitParser.
046         */
047        public void close() throws Exception {
048                if (twoBitParser != null)
049                        twoBitParser.close();
050
051        }
052
053        /**
054         * Sets a chromosome for TwoBitParser.
055         *
056         * @param chr The chromosome name (e.g. chr21)
057         */
058        public void setChromosome(String chr) throws Exception {
059                if ( twoBitParser == null){
060
061                }
062                twoBitParser.close();
063                String[] names = twoBitParser.getSequenceNames();
064                for(int i=0;i<names.length;i++) {
065                        if ( names[i].equalsIgnoreCase(chr) ) {
066                                twoBitParser.setCurrentSequence(names[i]);
067                                break;
068                        }
069                }
070        }
071
072        /**
073         * Extract a sequence from a chromosome, using chromosomal coordinates
074         *
075         * @param chromosomeName
076         * @param start
077         * @param end
078         * @return the DNASequence from the requested coordinates.
079         * @throws Exception
080         */
081        public String getSequence(String chromosomeName, int start, int end) throws Exception {
082                twoBitParser.close();
083                twoBitParser.setCurrentSequence(chromosomeName);
084                return twoBitParser.loadFragment(start,end-start);
085        }
086}