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.structure.rcsb;
022
023import java.io.BufferedReader;
024import java.io.IOException;
025import java.io.InputStreamReader;
026import java.net.URL;
027import java.net.URLConnection;
028import java.util.ArrayList;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033public class RCSBUpdates {
034
035        // The URL for acquiring the data
036        public static final String baseURL = "http://ftp.rcsb.org/pub/pdb/data/status/latest/";
037
038        /**
039         *
040         * @return A map mapping each field (defined by a separate FTP file) to the PDB ids in the field. The possible fields
041         * are: added.models, added.nmr, added.pdb, added.sf, modified.cs, modified.models, modified.nmr, modified.pdb, modified.sf,
042         * obsolete.cs, obsolete.models, obsolete.nmr, obsolete.pdb, obsolete.sf
043         * @throws IOException
044         */
045        public Map<String,String[]> getUpdates() throws IOException{
046
047                Map<String,String[]> outMap = new HashMap<String, String[]>();
048                // A list of files to get
049                String[] newStringList = {"added.models","added.nmr","added.pdb","added.sf","modified.cs","modified.models",
050                                "modified.nmr","modified.pdb","modified.sf","obsolete.cs","obsolete.models","obsolete.nmr","obsolete.pdb","obsolete.sf"};
051                for(String fileName: newStringList){
052                        String[] thisList = readURL(baseURL+""+fileName);
053                        outMap.put(fileName, thisList);
054                }
055                return outMap;
056
057        }
058
059
060        /**
061         *
062         * @param urlIn The url to be read
063         * @return A list of PDB ids as strings
064         * @throws IOException
065         */
066        private String[] readURL(String urlIn) throws IOException{
067                List<String> outList = new ArrayList<String>();
068                // create a url object
069                URL url = new URL(urlIn);
070
071                // create a urlconnection object
072                URLConnection urlConnection = url.openConnection();
073
074                // wrap the urlconnection in a bufferedreader
075                try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {
076
077                        String line;
078
079                        // read from the urlconnection via the bufferedreader
080                        while ((line = bufferedReader.readLine()) != null)
081                        {
082                                outList.add(line);
083                        }
084
085                }
086
087                return outList.toArray(new String[outList.size()]);
088        }
089}