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 * Created on 16.03.2004
021 * @author Andreas Prlic
022 *
023 *
024 */
025package org.biojava.nbio.structure.io;
026
027
028import org.biojava.nbio.structure.Chain;
029import org.biojava.nbio.structure.Compound;
030import org.biojava.nbio.structure.Group;
031import org.biojava.nbio.structure.Structure;
032import org.biojava.nbio.structure.align.util.UserConfiguration;
033import java.io.IOException;
034import java.io.InputStream;
035import java.util.List;
036
037/**
038 * <p>
039 *  The wrapper class for parsing a PDB file.
040 *  </p>
041 *
042 *
043 *  <p>
044 *  Several flags can be set for this class
045 *  <ul>
046 *
047 * <li> {@link #setAutoFetch(boolean)} - if the PDB file can not be found locally, should it be fetched
048 *  from the PDB ftp servers? (default:false)</li>
049 *  <li> Other parameters can be set using the {@link #setFileParsingParameters(FileParsingParameters)}</li>
050 *  </ul>
051 *  </p>
052 *
053 *
054 *
055 *<h2>Example</h2>
056 * <p>
057 * Q: How can I get a Structure object from a PDB file?
058 * </p>
059 * <p>
060 * A:
061 * <pre>
062 * public {@link Structure} loadStructure(String pathToPDBFile){
063 *      {@link PDBFileReader} pdbreader = new {@link PDBFileReader}();
064 *
065 *      {@link Structure} structure = null;
066 *      try{
067 *              structure = pdbreader.getStructure(pathToPDBFile);
068 *              System.out.println(structure);
069 *      } catch (IOException e) {
070 *              e.printStackTrace();
071 *      }
072 *      return structure;
073 * }
074 * </pre>
075 *
076 * Access PDB files from a directory, take care of compressed PDB files
077 * <pre>
078 * public {@link Structure} loadStructureById() {
079 *      String path = "/path/to/PDB/directory/";
080 *
081 *      {@link PDBFileReader} pdbreader = new {@link PDBFileReader}();
082 *      pdbreader.setPath(path);
083 *      {@link Structure} structure = null;
084 *      try {
085 *              structure = pdbreader.getStructureById("5pti");
086 *      } catch (IOException e){
087 *              e.printStackTrace();
088 *      }
089 *      return structure;
090 *
091 * }
092 * </pre>
093 *
094 *
095 * @author Andreas Prlic
096 *
097 */
098public class PDBFileReader extends LocalPDBDirectory {
099
100        //private static final Logger logger = LoggerFactory.getLogger(PDBFileReader.class);
101
102        // a list of big pdb files for testing
103        //  "1htq",
104        //  "1c2w",
105        //  "1ffk",
106        //  "1giy",
107        //  "1j5a",
108        //  "1jj2",
109        //  "1jzx",
110        //  "1jzy",
111        //  "1jzz",
112        //  "1k01",
113        //  "1k73",
114        //  "1k8a",
115        //  "1k9m",
116        //  "1kc8",
117        //  "1kd1",
118        //  "1kqs",
119        //  "1m1k",
120        //  "1m90",
121        //  "1mkz",
122        //  "1ml5",
123        //  "1n8r",
124
125        public static final String[] PDB_SPLIT_DIR    = new String[]{"data","structures","divided" ,"pdb"};
126        public static final String[] PDB_OBSOLETE_DIR = new String[]{"data","structures","obsolete","pdb"};
127
128        public static void main(String[] args){
129
130
131                PDBFileReader pdbreader = new PDBFileReader();
132
133
134                // set the path to cache files
135                String property = "java.io.tmpdir";
136                String tempdir = System.getProperty(property);
137                // tempdir = "/path/to/local/PDB/installation/";
138                pdbreader.setPath(tempdir);
139
140
141                FileParsingParameters params = new FileParsingParameters();
142                pdbreader.setFileParsingParameters(params);
143
144
145                try{
146
147                        Structure struc = pdbreader.getStructureById("193D");
148                        System.out.println(struc);
149
150                        List<Compound>  compounds = struc.getCompounds();
151                        for (Compound comp : compounds  ){
152                                List<Chain> chains = comp.getChains();
153                                System.out.print(">Chains :" );
154                                for (Chain c : chains){
155                                        System.out.print(c.getChainID() + " " );
156                                }
157                                System.out.println();
158                                if ( chains.size() > 0) {
159                                        System.out.println(chains.get(0).getAtomSequence());
160                                        System.out.println(chains.get(0).getSeqResSequence());
161                                        System.out.print("  Atom Ligands: ");
162
163                                        for ( Group g: chains.get(0).getAtomLigands()){
164                                                System.out.print( g.getPDBName() + " ");
165                                        }
166
167                                        System.out.println(" ");
168                                }
169                        }
170
171
172                        /*
173                        GroupIterator gi = new GroupIterator(struc);
174                        while (gi.hasNext()){
175                                Group g = (Group) gi.next();
176                                Chain  c = g.getParent();
177                                if ( g instanceof AminoAcid ){
178
179                                        AminoAcid aa = (AminoAcid)g;
180
181                                        Map<String,String> sec = aa.getSecStruc();
182
183                                        //System.out.println(c.getName() + " " + g + " " + sec);
184
185                                        ChemComp cc = g.getChemComp();
186
187                                        System.out.println(c.getName() + " " + g.getPDBCode() + " " + g.getPDBName() + " " + cc + " " +sec);
188                                }
189
190                        }
191                         */
192                } catch (IOException e) {
193                        e.printStackTrace();
194                }
195        }
196
197
198
199
200
201        /**
202         * Constructs a new PDBFileReader, initializing the extensions member variable.
203         * The path is initialized in the same way as {@link UserConfiguration},
204         * i.e. to system property/environment variable {@link UserConfiguration#PDB_DIR}.
205         * Both autoFetch and splitDir are initialized to false
206         */
207        public PDBFileReader() {
208                this(null);
209        }
210
211        /**
212         * Constructs a new PDBFileReader, initializing the extensions member variable.
213         * The path is initialized to the given path, both autoFetch and splitDir are initialized to false.
214         *
215         * <p>If path is null, initialize using the system property/environment variable
216         * {@link UserConfiguration#PDB_DIR}.
217         * @param path Path to the PDB file directory
218         */
219        public PDBFileReader(String path) {
220                super(path);
221
222                addExtension(".ent");
223                addExtension(".pdb");
224                addExtension(".ent.gz");
225                addExtension(".pdb.gz");
226                addExtension(".ent.Z");
227                addExtension(".pdb.Z");
228        }
229
230
231        @Deprecated
232        public void downloadPDB(String pdbId) throws IOException {
233                downloadStructure(pdbId);
234        }
235
236        /**
237         * <b>N.B.</b> This feature won't work unless the structure wasn't found & autoFetch is set to <code>true</code>.
238         * @param fetchFileEvenIfObsolete the fetchFileEvenIfObsolete to set
239         * @deprecated Use {@link FileParsingParameters#setObsoleteBehavior(ObsoleteBehavior)}
240         */
241        @Deprecated
242        public void setFetchFileEvenIfObsolete(boolean fetchFileEvenIfObsolete) {
243                if(fetchFileEvenIfObsolete) {
244                        setObsoleteBehavior(ObsoleteBehavior.FETCH_OBSOLETE);
245                } else {
246                        if(getObsoleteBehavior() == ObsoleteBehavior.FETCH_OBSOLETE) {
247                                setObsoleteBehavior(ObsoleteBehavior.DEFAULT);
248                        }
249                }
250        }
251
252        /**forces the reader to fetch the file if its status is OBSOLETE.
253         * This feature has a higher priority than {@link #setFetchCurrent(boolean)}. <br>
254         * <b>N.B.</b> This feature won't work unless the structure wasn't found & autoFetch is set to <code>true</code>.
255         * @return the fetchFileEvenIfObsolete
256         * @author Amr AL-Hossary
257         * @see #fetchCurrent
258         * @since 3.0.2
259         * @deprecated Use {@link FileParsingParameters#getObsoleteBehavior()}
260         */
261        @Deprecated
262        public boolean isFetchFileEvenIfObsolete() {
263                return getObsoleteBehavior() == ObsoleteBehavior.FETCH_OBSOLETE;
264        }
265
266
267        /**if enabled, the reader searches for the newest possible PDB ID, if not present in he local installation.
268         * The {@link #setFetchFileEvenIfObsolete(boolean)} function has a higher priority than this function. <br>
269         * <b>N.B.</b> This feature won't work unless the structure wasn't found & autoFetch is set to <code>true</code>.
270         * @param fetchCurrent the fetchCurrent to set
271         * @author Amr AL-Hossary
272         * @see #setFetchFileEvenIfObsolete(boolean)
273         * @since 3.0.2
274         * @deprecated Use {@link FileParsingParameters#setObsoleteBehavior(ObsoleteBehavior)}
275         */
276        @Deprecated
277        public void setFetchCurrent(boolean fetchNewestCurrent) {
278                if(fetchNewestCurrent) {
279                        setObsoleteBehavior(ObsoleteBehavior.FETCH_CURRENT);
280                } else {
281                        if(getObsoleteBehavior() == ObsoleteBehavior.FETCH_CURRENT) {
282                                setObsoleteBehavior(ObsoleteBehavior.DEFAULT);
283                        }
284                }
285        }
286
287        /**
288         * <b>N.B.</b> This feature won't work unless the structure wasn't found & autoFetch is set to <code>true</code>.
289         * @return the fetchCurrent
290         * @deprecated Use {@link FileParsingParameters#getObsoleteBehavior()}
291         */
292        @Deprecated
293        public boolean isFetchCurrent() {
294                return getObsoleteBehavior() == ObsoleteBehavior.FETCH_CURRENT;
295        }
296
297        @Override
298        protected String getFilename(String pdbId) {
299                return "pdb"+pdbId.toLowerCase()+".ent.gz";
300        }
301
302        @Override
303        public Structure getStructure(InputStream inStream) throws IOException {
304                PDBFileParser pdbpars = new PDBFileParser();
305                pdbpars.setFileParsingParameters(getFileParsingParameters());
306
307                Structure struc = pdbpars.parsePDBFile(inStream) ;
308                return struc ;
309        }
310
311        @Override
312        protected String[] getSplitDirPath() {
313                return PDB_SPLIT_DIR;
314        }
315
316        @Override
317        protected String[] getObsoleteDirPath() {
318                return PDB_OBSOLETE_DIR;
319        }
320
321
322
323}