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.scop;
022
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import java.util.HashMap;
027import java.util.Map;
028
029
030
031/**
032 * Controls the global ScopDatabase being used.
033 *
034 * <p>Defaults to a {@link RemoteScopInstallation}, which is fast for small numbers
035 * of queries. For many queries, using {@link #getSCOP(String, boolean) getSCOP(version,true)}
036 * may be faster, since it makes only one network request.
037 *
038 * <p>Example: Fetch the structure corresponding to an old version of scop
039 *
040 * <pre>
041 * ScopInstallation scop = new ScopInstallation();
042 * scop.setScopVersion("1.69");
043 * ScopFactory.setScopDatabase(scop);
044 * AtomCache cache = new AtomCache();
045 * cache.setFetchFileEvenIfObsolete(true); //fetch older PDBs
046 * cache.setStrictSCOP(false); // correct simple errors in domain names
047 * Structure s = cache.getStructure("d3hbia_");
048 * @author sbliven
049 *
050 */
051public class ScopFactory {
052
053        private static final Logger logger = LoggerFactory.getLogger(ScopFactory.class);
054
055        // berkeley 2
056        public static final String VERSION_2_0_5 = "2.05";
057        public static final String VERSION_2_0_4 = "2.04";
058        public static final String VERSION_2_0_3 = "2.03";
059        public static final String VERSION_2_0_2 = "2.02";
060        public static final String VERSION_2_0_1 = "2.01";
061        // berkeley 1 (aliases of above)
062        public static final String VERSION_1_75C = VERSION_2_0_3;
063        public static final String VERSION_1_75B = VERSION_2_0_2;
064        public static final String VERSION_1_75A = VERSION_2_0_1;
065        // original SCOP
066        // latest SCOP release from SCOP website = 1.75;
067        public static final String VERSION_1_75 = "1.75";
068        // outdated stable versions
069        public static final String VERSION_1_73 = "1.73";
070        public static final String VERSION_1_71 = "1.71";
071        public static final String VERSION_1_69 = "1.69";
072        public static final String VERSION_1_67 = "1.67";
073        public static final String VERSION_1_65 = "1.65";
074        public static final String VERSION_1_63 = "1.63";
075        public static final String VERSION_1_61 = "1.61";
076        public static final String VERSION_1_59 = "1.59";
077        public static final String VERSION_1_57 = "1.57";
078        public static final String VERSION_1_55 = "1.55";
079
080        // The most recent version as of compilation time
081        public static final String LATEST_VERSION = VERSION_2_0_5;
082
083        // Hold one instance for each version
084        private static Map<String,ScopDatabase> versionedScopDBs = new HashMap<String, ScopDatabase>();
085        private static String defaultVersion = LATEST_VERSION;
086
087        /**
088         * Get the current default instance for the default version
089         * @return
090         */
091        public static ScopDatabase getSCOP(){
092                return getSCOP(defaultVersion);
093        }
094
095        /**
096         *
097         * @param forceLocalData Whether to use a local installation or a remote installation
098         * @return
099         * @see #getSCOP(String, boolean)
100         */
101        public static ScopDatabase getSCOP(boolean forceLocalData) {
102                return getSCOP(defaultVersion, forceLocalData);
103        }
104
105        /**
106         * requests a particular version of SCOP.
107         *
108         * Where possible, this will be the current default instance.
109         * Otherwise a new instance will be created.
110         * @param version
111         * @return
112         */
113        public static ScopDatabase getSCOP(String version){
114                // Default to a remote installation
115                return getSCOP(version,false);
116        }
117
118        /**
119         * Gets an instance of the specified scop version.
120         *
121         * <p>
122         * The particular implementation returned is influenced by the <tt>forceLocalData</tt>
123         * parameter. When false, the instance returned will generally be a
124         * {@link RemoteScopInstallation}, although this may be influenced by
125         * previous calls to this class. When true, the result is guaranteed to
126         * implement {@link LocalScopDatabase} (generally a {@link BerkeleyScopInstallation}).
127         *
128         * <p>
129         * Note that
130         * @param version A version number, such as {@link #VERSION_1_75A}
131         * @param forceLocalData Whether to use a local installation or a remote installation
132         * @return an
133         */
134        public static ScopDatabase getSCOP(String version, boolean forceLocalData){
135                if( version == null ) {
136                        version = defaultVersion;
137                }
138                ScopDatabase scop = versionedScopDBs.get(version);
139                if ( forceLocalData) {
140                        // Use a local installation
141                        if( scop == null || !(scop instanceof LocalScopDatabase) ) {
142                                logger.info("Creating new {}, version {}", BerkeleyScopInstallation.class.getSimpleName(), version);
143                                BerkeleyScopInstallation berkeley = new BerkeleyScopInstallation();
144                                berkeley.setScopVersion(version);
145                                versionedScopDBs.put(version,berkeley);
146                                return berkeley;
147                        }
148                        return scop;
149                } else {
150                        // Use a remote installation
151                        if( scop == null ) {
152                                logger.info("Creating new {}, version {}", RemoteScopInstallation.class.getSimpleName(), version);
153                                scop = new RemoteScopInstallation();
154                                scop.setScopVersion(version);
155                                versionedScopDBs.put(version,scop);
156                        }
157                        return scop;
158                }
159        }
160
161
162        /**
163         * Set the default scop version
164         * @param version A version number, such as {@link #VERSION_1_75A}
165         */
166        public static void setScopDatabase(String version) {
167                getSCOP(version);
168                defaultVersion = version;
169        }
170
171        /**
172         * Set the default scop version
173         * @param version A version number, such as {@link #VERSION_1_75A}
174         * @param forceLocalData Whether to use a local installation or a remote installation
175         */
176        public static void setScopDatabase(String version, boolean forceLocalData) {
177                logger.debug("ScopFactory: Setting ScopDatabase to version: {}, forced local: {}", version, forceLocalData);
178                getSCOP(version,forceLocalData);
179                defaultVersion = version;
180        }
181
182        /**
183         * Set the default scop version and instance
184         * @param scop
185         */
186        public static void setScopDatabase(ScopDatabase scop){
187                logger.debug("ScopFactory: Setting ScopDatabase to type: {}", scop.getClass().getName());
188                defaultVersion = scop.getScopVersion();
189                versionedScopDBs.put(defaultVersion,scop);
190        }
191}