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