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>
035 * Example: Fetch the structure corresponding to an old version of scop
036 *
037 * <pre>
038 * ScopInstallation scop = new ScopInstallation();
039 * scop.setScopVersion("1.69");
040 * ScopFactory.setScopDatabase(scop);
041 * AtomCache cache = new AtomCache();
042 * cache.setFetchFileEvenIfObsolete(true); //fetch older PDBs
043 * cache.setStrictSCOP(false); // correct simple errors in domain names
044 * Structure s = cache.getStructure("d3hbia_");
045 * </pre>
046 * @author sbliven
047 *
048 */
049public class ScopFactory {
050
051        private static final Logger logger = LoggerFactory.getLogger(ScopFactory.class);
052
053        // berkeley 2
054        public static final String VERSION_2_0_8 = "2.08";
055        public static final String VERSION_2_0_7 = "2.07";
056        public static final String VERSION_2_0_6 = "2.06";
057        public static final String VERSION_2_0_5 = "2.05";
058        public static final String VERSION_2_0_4 = "2.04";
059        public static final String VERSION_2_0_3 = "2.03";
060        public static final String VERSION_2_0_2 = "2.02";
061        public static final String VERSION_2_0_1 = "2.01";
062        // berkeley 1 (aliases of above)
063        public static final String VERSION_1_75C = VERSION_2_0_3;
064        public static final String VERSION_1_75B = VERSION_2_0_2;
065        public static final String VERSION_1_75A = VERSION_2_0_1;
066        // original SCOP
067        // latest SCOP release from SCOP website = 1.75;
068        public static final String VERSION_1_75 = "1.75";
069        // outdated stable versions
070        public static final String VERSION_1_73 = "1.73";
071        public static final String VERSION_1_71 = "1.71";
072        public static final String VERSION_1_69 = "1.69";
073        public static final String VERSION_1_67 = "1.67";
074        public static final String VERSION_1_65 = "1.65";
075        public static final String VERSION_1_63 = "1.63";
076        public static final String VERSION_1_61 = "1.61";
077        public static final String VERSION_1_59 = "1.59";
078        public static final String VERSION_1_57 = "1.57";
079        public static final String VERSION_1_55 = "1.55";
080
081        // The most recent version as of compilation time
082        public static final String LATEST_VERSION = VERSION_2_0_8;
083
084        // Hold one instance for each version
085        private static Map<String,ScopDatabase> versionedScopDBs = new HashMap<>();
086        private static String defaultVersion = LATEST_VERSION;
087
088        /**
089         * Get the current default instance for the default version
090         * @return
091         */
092        public static ScopDatabase getSCOP(){
093                return getSCOP(defaultVersion);
094        }
095
096        /**
097         * Gets an instance of the specified scop version.
098         *
099         * <p>
100         * The particular implementation returned is guaranteed to
101         * implement {@link LocalScopDatabase} (generally a {@link BerkeleyScopInstallation}).
102         *
103         * @param version A version number, such as {@link #VERSION_1_75A}
104         * @return an
105         */
106        public static ScopDatabase getSCOP(String version){
107                if( version == null ) {
108                        version = defaultVersion;
109                }
110
111                ScopDatabase scop = versionedScopDBs.get(version);
112                if (scop == null) {
113                        // Use a local installation
114                        logger.info("Creating new {}, version {}", BerkeleyScopInstallation.class.getSimpleName(), version);
115                        BerkeleyScopInstallation berkeley = new BerkeleyScopInstallation();
116                        berkeley.setScopVersion(version);
117                        versionedScopDBs.put(version, berkeley);
118                        return berkeley;
119                }
120                return scop;
121        }
122
123        /**
124         * Set the default scop version
125         * @param version A version number, such as {@link #VERSION_1_75A}
126         */
127        public static void setScopDatabase(String version) {
128                getSCOP(version);
129                defaultVersion = version;
130        }
131
132        /**
133         * Set the default scop version and instance
134         * @param scop
135         */
136        public static void setScopDatabase(ScopDatabase scop){
137                logger.debug("ScopFactory: Setting ScopDatabase to type: {}", scop.getClass().getName());
138                defaultVersion = scop.getScopVersion();
139                versionedScopDBs.put(defaultVersion,scop);
140        }
141}