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 demo;
022
023import org.biojava.nbio.structure.Chain;
024import org.biojava.nbio.structure.Group;
025import org.biojava.nbio.structure.Structure;
026import org.biojava.nbio.structure.io.FileParsingParameters;
027import org.biojava.nbio.structure.io.PDBFileReader;
028import org.biojava.nbio.structure.io.mmcif.AllChemCompProvider;
029import org.biojava.nbio.structure.io.mmcif.ChemCompGroupFactory;
030import org.biojava.nbio.structure.io.mmcif.ChemCompProvider;
031import org.biojava.nbio.structure.io.mmcif.DownloadChemCompProvider;
032
033import java.util.List;
034
035
036/**
037 *  This demo shows how to use an alternative ChemCompProvider. The default mechanism in BioJava is to access chemical components
038 *  by using the {@link DownloadChemCompProvider}. It fetches and locally caches chemical component definitions as they are encountered during file parsing.
039 *  It can be enabled by using the {@link FileParsingParameters#setLoadChemCompInfo(boolean)} method.
040 *
041 * The {@link AllChemCompProvider} downloads and unpacks all chemcomps. It is slower and requires more memory than the default {@link DownloadChemCompProvider},
042 * but it avoids network access to the FTP site, if a new chemcomp is detected, that has not been downloaded yet.
043 *
044 * Since all chemcomps will be kept in memory, the standard memory that is available to a JVM will not be sufficient
045 * in order to run this demo. Please start with -Xmx200M
046 *
047 * @author Andreas Prlic
048 *
049 */
050public class DemoChangeChemCompProvider {
051
052        public static void main(String[] args){
053                String pdbId = "1O1G";
054
055                boolean loadChemComp = true;
056
057                //////
058                // no need to change anything below here...
059                //////
060
061                PDBFileReader reader = new PDBFileReader();
062
063                // Set the system wide property where PDB and chemcomp files are being cached.
064                // you can set the path to the local PDB installation either like this
065//              reader.setPath(PDB_PATH);
066                // or via
067                // by setting the PDB_PATH environmental variable or system property
068                // when running the demo (e.g. -DPDB_DIR=/path/to/pdb)
069
070                if ( loadChemComp) {
071
072                        // The AllChemCompProvider loads all chem comps at startup.
073                        // This is slow (13 sec on my laptop) and requires more
074                        // memory than the default DownloadChemCompProvider.
075                        // In contrast to it it keeps all definitions in memory.
076                        ChemCompProvider all = new AllChemCompProvider();
077
078                        ChemCompGroupFactory.setChemCompProvider(all);
079                }
080
081                DemoChangeChemCompProvider demo = new DemoChangeChemCompProvider();
082
083                // run the demo
084                demo.basicLoad(reader,loadChemComp, pdbId);
085
086        }
087
088        public void basicLoad(PDBFileReader reader, boolean loadChemComp, String pdbId){
089
090                try {
091                        // configure the parameters of file parsing
092
093                        FileParsingParameters params = new FileParsingParameters();
094
095                        // should the ATOM and SEQRES residues be aligned when creating the internal data model?
096                        // only do this if you need to work with SEQRES sequences. If all you need are ATOMs, then
097                        // set it to false to have quicker file loading.
098                        params.setAlignSeqRes(true);
099
100                        //
101                        // should secondary structure get parsed from the file
102                        params.setParseSecStruc(false);
103
104                        reader.setFileParsingParameters(params);
105
106                        Structure struc = reader.getStructureById(pdbId);
107
108                        printStructure(struc);
109
110
111                } catch (Exception e){
112                        e.printStackTrace();
113                }
114
115        }
116
117        private void printStructure(Structure struc) {
118
119                System.out.println(struc);
120
121                //Chain c = struc.getChainByPDB("C");
122                String pdbid = struc.getPDBCode();
123                for (int i = 0; i < struc.nrModels(); i++) {
124
125                        // loop chain
126                        for (Chain ch : struc.getModel(i)) {
127                                if (! ch.getName().equals("A") )
128                                        continue;
129                                System.out.println(pdbid + ">>>" + ch.getName() + ">>>"
130                                                + ch.getAtomSequence());
131                                System.out.println(pdbid + ">>>" + ch.getName() + ">>>"
132                                                + ch.getSeqResSequence());
133                                // Test the getAtomGroups() and getSeqResGroups() method
134
135                                List<Group> group = ch.getSeqResGroups();
136                                int seqPos = 0;
137                                for (Group gp : group) {
138                                        System.out.println(ch.getName() + ":"+seqPos + ":" + gp.getResidueNumber() + ":"
139                                                        + gp.getPDBName() + " " + gp.getType());
140                                        seqPos++;
141                                }
142                        }
143                }
144
145
146        }
147}