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_7 = "2.07";
057        public static final String VERSION_2_0_6 = "2.06";
058        public static final String VERSION_2_0_5 = "2.05";
059        public static final String VERSION_2_0_4 = "2.04";
060        public static final String VERSION_2_0_3 = "2.03";
061        public static final String VERSION_2_0_2 = "2.02";
062        public static final String VERSION_2_0_1 = "2.01";
063        // berkeley 1 (aliases of above)
064        public static final String VERSION_1_75C = VERSION_2_0_3;
065        public static final String VERSION_1_75B = VERSION_2_0_2;
066        public static final String VERSION_1_75A = VERSION_2_0_1;
067        // original SCOP
068        // latest SCOP release from SCOP website = 1.75;
069        public static final String VERSION_1_75 = "1.75";
070        // outdated stable versions
071        public static final String VERSION_1_73 = "1.73";
072        public static final String VERSION_1_71 = "1.71";
073        public static final String VERSION_1_69 = "1.69";
074        public static final String VERSION_1_67 = "1.67";
075        public static final String VERSION_1_65 = "1.65";
076        public static final String VERSION_1_63 = "1.63";
077        public static final String VERSION_1_61 = "1.61";
078        public static final String VERSION_1_59 = "1.59";
079        public static final String VERSION_1_57 = "1.57";
080        public static final String VERSION_1_55 = "1.55";
081
082        // The most recent version as of compilation time
083        public static final String LATEST_VERSION = VERSION_2_0_7;
084
085        // Hold one instance for each version
086        private static Map<String,ScopDatabase> versionedScopDBs = new HashMap<String, ScopDatabase>();
087        private static String defaultVersion = LATEST_VERSION;
088
089        /**
090         * Get the current default instance for the default version
091         * @return
092         */
093        public static ScopDatabase getSCOP(){
094                return getSCOP(defaultVersion);
095        }
096
097        /**
098         *
099         * @param forceLocalData Whether to use a local installation or a remote installation
100         * @return
101         * @see #getSCOP(String, boolean)
102         */
103        public static ScopDatabase getSCOP(boolean forceLocalData) {
104                return getSCOP(defaultVersion, forceLocalData);
105        }
106
107        /**
108         * requests a particular version of SCOP.
109         *
110         * Where possible, this will be the current default instance.
111         * Otherwise a new instance will be created.
112         * @param version
113         * @return
114         */
115        public static ScopDatabase getSCOP(String version){
116                // Default to a remote installation
117                return getSCOP(version,false);
118        }
119
120        /**
121         * Gets an instance of the specified scop version.
122         *
123         * <p>
124         * The particular implementation returned is influenced by the <tt>forceLocalData</tt>
125         * parameter. When false, the instance returned will generally be a
126         * {@link RemoteScopInstallation}, although this may be influenced by
127         * previous calls to this class. When true, the result is guaranteed to
128         * implement {@link LocalScopDatabase} (generally a {@link BerkeleyScopInstallation}).
129         *
130         * <p>
131         * Note that
132         * @param version A version number, such as {@link #VERSION_1_75A}
133         * @param forceLocalData Whether to use a local installation or a remote installation
134         * @return an
135         */
136        public static ScopDatabase getSCOP(String version, boolean forceLocalData){
137                if( version == null ) {
138                        version = defaultVersion;
139                }
140                ScopDatabase scop = versionedScopDBs.get(version);
141                if ( forceLocalData) {
142                        // Use a local installation
143                        if( scop == null || !(scop instanceof LocalScopDatabase) ) {
144                                logger.info("Creating new {}, version {}", BerkeleyScopInstallation.class.getSimpleName(), version);
145                                BerkeleyScopInstallation berkeley = new BerkeleyScopInstallation();
146                                berkeley.setScopVersion(version);
147                                versionedScopDBs.put(version,berkeley);
148                                return berkeley;
149                        }
150                        return scop;
151                } else {
152                        // Use a remote installation
153                        if( scop == null ) {
154                                logger.info("Creating new {}, version {}", RemoteScopInstallation.class.getSimpleName(), version);
155                                scop = new RemoteScopInstallation();
156                                scop.setScopVersion(version);
157                                versionedScopDBs.put(version,scop);
158                        }
159                        return scop;
160                }
161        }
162
163
164        /**
165         * Set the default scop version
166         * @param version A version number, such as {@link #VERSION_1_75A}
167         */
168        public static void setScopDatabase(String version) {
169                getSCOP(version);
170                defaultVersion = version;
171        }
172
173        /**
174         * Set the default scop version
175         * @param version A version number, such as {@link #VERSION_1_75A}
176         * @param forceLocalData Whether to use a local installation or a remote installation
177         */
178        public static void setScopDatabase(String version, boolean forceLocalData) {
179                logger.debug("ScopFactory: Setting ScopDatabase to version: {}, forced local: {}", version, forceLocalData);
180                getSCOP(version,forceLocalData);
181                defaultVersion = version;
182        }
183
184        /**
185         * Set the default scop version and instance
186         * @param scop
187         */
188        public static void setScopDatabase(ScopDatabase scop){
189                logger.debug("ScopFactory: Setting ScopDatabase to type: {}", scop.getClass().getName());
190                defaultVersion = scop.getScopVersion();
191                versionedScopDBs.put(defaultVersion,scop);
192        }
193}