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 Feb 22, 2012
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package org.biojava.nbio.structure.io.sifts;
026
027import org.biojava.nbio.structure.align.util.AtomCache;
028import org.biojava.nbio.core.util.FileDownloadUtils;
029import org.biojava.nbio.core.util.InputStreamProvider;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import java.io.File;
034import java.io.IOException;
035import java.io.InputStream;
036import java.net.URL;
037import java.util.List;
038
039public class SiftsMappingProvider {
040
041        private final static Logger logger = LoggerFactory.getLogger(SiftsMappingProvider.class);
042
043
044        private static final String EBI_SIFTS_FILE_LOCATION = "https://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/%s.xml.gz";
045
046        private static String fileLoc = EBI_SIFTS_FILE_LOCATION;
047
048        public static void setFileLocation(String myFileLocation){
049                fileLoc = myFileLocation;
050        }
051
052        /**
053         * Return the SIFTS mappings by getting the info from individual SIFTS xml files at URL {@value EBI_SIFTS_FILE_LOCATION}
054         * @param pdbId the pdb identifier
055         * @return
056         * @throws IOException if problems downloading or parsing the file
057         */
058        public static List<SiftsEntity> getSiftsMapping(String pdbId) throws IOException{
059                // grab files from here:
060
061                AtomCache cache = new AtomCache();
062
063                String path = cache.getCachePath();
064
065                pdbId = pdbId.toLowerCase();
066
067                String dirHash = pdbId.substring(1,3);
068                File siftsDir = new File(path , "SIFTS");
069
070
071                if ( ! siftsDir.exists()) {
072                        logger.info("Creating directory {}", siftsDir.toString());
073                        siftsDir.mkdir();
074                }
075
076                File hashDir = new File(siftsDir, dirHash);
077
078                if ( ! hashDir.exists()){
079                        logger.info("Creating directory {}", hashDir.toString());
080                        hashDir.mkdir();
081                }
082                File dest = new File( hashDir, pdbId + ".sifts.xml.gz");
083
084                logger.debug("testing SIFTS file " + dest.getAbsolutePath());
085
086
087                if ( ! dest.exists()){
088                        String u = String.format(fileLoc,pdbId);
089                        URL url = new URL(u);
090                        logger.debug("Downloading SIFTS file {} to {}",url,dest);
091                        FileDownloadUtils.downloadFile(url, dest);
092                }
093
094                InputStreamProvider prov = new InputStreamProvider();
095                InputStream is = prov.getInputStream(dest);
096                SiftsXMLParser parser = new SiftsXMLParser();
097
098                parser.parseXmlFile(is);
099
100                //System.out.println(parser.getEntities());
101                return parser.getEntities();
102
103
104        }
105
106
107}