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 org.biojava.nbio.core.util.FileDownloadUtils;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import java.io.File;
028import java.io.IOException;
029import java.net.MalformedURLException;
030import java.net.URL;
031import java.nio.file.Files;
032import java.nio.file.Path;
033import java.nio.file.Paths;
034
035/**
036 * Created by yana on 4/4/17.
037 */
038public class SimpleTwoBitFileProvider {
039        private static final Logger logger = LoggerFactory.getLogger(SimpleTwoBitFileProvider.class);
040
041        public static synchronized void downloadIfNoTwoBitFileExists(File twoBitFileLocalLocation, String genomeAssembly) throws IOException {
042
043                if ( ! twoBitFileLocalLocation.exists() ) {
044
045                        // download to a temporary file
046                        File tmp = File.createTempFile(genomeAssembly,".2bit");
047                        URL twoBitFileURL = getTwoBitURL(genomeAssembly);
048
049                        logger.info("downloading " + twoBitFileURL + " to " + tmp.getAbsolutePath());
050
051                        // 2bit files are large and take a while to download
052                        FileDownloadUtils.downloadFile(twoBitFileURL, tmp);
053
054                        // check the parent directory exists
055
056                        Path p = Paths.get(twoBitFileLocalLocation.getAbsolutePath());
057
058                        Path dir = p.getParent();
059                        if (! Files.exists(dir)) {
060                                Files.createDirectories(dir);
061                        }
062
063                        logger.info("renaming " + tmp.getAbsolutePath() +" to " + twoBitFileLocalLocation.getAbsolutePath());
064                        // after the download rename
065                        tmp.renameTo(twoBitFileLocalLocation);
066
067                }
068        }
069
070        public static URL getTwoBitURL(String genomeAssembly) throws MalformedURLException {
071
072                String url="";
073                if (genomeAssembly.equals("hg19") || genomeAssembly.equals("hg37") ) {
074                        url = "http://cdn.rcsb.org/gene/hg37/hg19.2bit";
075                }
076                else if (genomeAssembly.equals("hg38")) {
077                        url = "http://cdn.rcsb.org/gene/hg38/hg38.2bit";
078                }
079                return new URL(url);
080        }
081
082        public static void main(String[] args){
083                try {
084                        downloadIfNoTwoBitFileExists(new File("/Users/yana/spark/2bit/hg38.2bit"),"hg38");
085                } catch (IOException e) {
086                        e.printStackTrace();
087                }
088        }
089
090}