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