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        static String EBI_SIFTS_FILE_LOCATION = "ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/%s.xml.gz";
045
046        static String RCSB_SIFTS_FILE_LOCATION = "http://www.rcsb.org/pdb/files/%s.sifts.xml.gz";
047
048        static String fileLoc = EBI_SIFTS_FILE_LOCATION;
049
050        public static void main(String[] args){
051                try {
052                        List<SiftsEntity> entities = getSiftsMapping("1gc1");
053
054                        for (SiftsEntity e : entities){
055                                System.out.println(e.getEntityId() + " " +e.getType());
056
057                                for ( SiftsSegment seg: e.getSegments()) {
058                                        System.out.println(" Segment: " + seg.getSegId() + " " + seg.getStart() + " " + seg.getEnd()) ;
059
060                                        for ( SiftsResidue res: seg.getResidues() ) {
061                                                System.out.println("  " + res);
062                                        }
063                                }
064
065                        }
066                } catch (Exception e){
067                        e.printStackTrace();
068                }
069        }
070
071        public static void setFileLocation(String myFileLocation){
072                fileLoc = myFileLocation;
073        }
074
075        public static List<SiftsEntity> getSiftsMapping(String pdbId) throws IOException{
076                // grab files from here:
077
078                AtomCache cache = new AtomCache();
079
080                String path = cache.getCachePath();
081
082                pdbId = pdbId.toLowerCase();
083
084                String dirHash = pdbId.substring(1,3);
085                File siftsDir = new File(path , "SIFTS");
086
087
088                if ( ! siftsDir.exists()) {
089                        logger.info("Creating directory {}", siftsDir.toString());
090                        siftsDir.mkdir();
091                }
092
093                File hashDir = new File(siftsDir, dirHash);
094
095                if ( ! hashDir.exists()){
096                        logger.info("Creating directory {}", hashDir.toString());
097                        hashDir.mkdir();
098                }
099                File dest = new File( hashDir, pdbId + ".sifts.xml.gz");
100
101                logger.debug("testing SIFTS file " + dest.getAbsolutePath());
102
103
104                if ( ! dest.exists()){
105                        String u = String.format(fileLoc,pdbId);
106                        URL url = new URL(u);
107                        logger.debug("Downloading SIFTS file {} to {}",url,dest);
108                        FileDownloadUtils.downloadFile(url, dest);
109                }
110
111                InputStreamProvider prov = new InputStreamProvider();
112                InputStream is = prov.getInputStream(dest);
113                SiftsXMLParser parser = new SiftsXMLParser();
114
115                parser.parseXmlFile(is);
116
117                //System.out.println(parser.getEntities());
118                return parser.getEntities();
119
120
121        }
122
123
124}