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 at Oct 18, 2008
021 */
022package org.biojava.nbio.structure.io;
023
024import org.biojava.nbio.structure.Structure;
025import org.biojava.nbio.structure.align.util.UserConfiguration;
026import org.biojava.nbio.structure.io.mmcif.MMcifParser;
027import org.biojava.nbio.structure.io.mmcif.SimpleMMcifConsumer;
028import org.biojava.nbio.structure.io.mmcif.SimpleMMcifParser;
029
030import java.io.BufferedReader;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.InputStreamReader;
034
035
036/** How to parse an mmCif file:
037 * <pre>
038public static void main(String[] args) throws Exception {
039        String filename =  "/path/to/something.cif.gz" ;
040
041        StructureIOFile reader = new MMCIFFileReader();
042
043        Structure struc = reader.getStructure(filename);
044        System.out.println(struc);
045}
046</pre>
047 *
048 * @author Andreas Prlic
049 * @since 1.7
050 *
051 */
052public class MMCIFFileReader extends LocalPDBDirectory {
053
054        //private static final Logger logger = LoggerFactory.getLogger(MMCIFFileReader.class);
055
056        public static final String[] MMCIF_SPLIT_DIR    = new String[]{"data","structures","divided" ,"mmCIF"};
057        public static final String[] MMCIF_OBSOLETE_DIR = new String[]{"data","structures","obsolete","mmCIF"};
058
059        private SimpleMMcifConsumer consumer;
060
061        public static void main(String[] args) throws Exception {
062
063                MMCIFFileReader reader = new MMCIFFileReader();
064                FileParsingParameters params = new FileParsingParameters();
065                reader.setFileParsingParameters(params);
066
067
068                Structure struc = reader.getStructureById("1m4x");
069                System.out.println(struc);
070                System.out.println(struc.toPDB());
071
072
073        }
074
075        /**
076         * Constructs a new MMCIFFileReader, initializing the extensions member variable.
077         * The path is initialized in the same way as {@link UserConfiguration},
078         * i.e. to system property/environment variable {@link UserConfiguration#PDB_DIR}.
079         * Both autoFetch and splitDir are initialized to false
080         */
081        public MMCIFFileReader(){
082                this(null);
083        }
084
085        /**
086         * Constructs a new PDBFileReader, initializing the extensions member variable.
087         * The path is initialized to the given path, both autoFetch and splitDir are initialized to false.
088         */
089        public MMCIFFileReader(String path){
090                super(path);
091                addExtension(".cif");
092                addExtension(".mmcif");
093                addExtension(".cif.gz");
094                addExtension(".mmcif.gz");
095        }
096
097        @Override
098        public Structure getStructure(InputStream inStream) throws IOException{
099
100                MMcifParser parser = new SimpleMMcifParser();
101
102                consumer = new SimpleMMcifConsumer();
103
104                consumer.setFileParsingParameters(getFileParsingParameters());
105
106
107                // The Consumer builds up the BioJava - structure object.
108                // you could also hook in your own and build up you own data model.
109                parser.addMMcifConsumer(consumer);
110
111                parser.parse(new BufferedReader(new InputStreamReader(inStream)));
112
113
114                // now get the protein structure.
115                Structure cifStructure = consumer.getStructure();
116
117                return cifStructure;
118        }
119
120        public SimpleMMcifConsumer getMMcifConsumer(){
121                return consumer;
122        }
123
124//      public void setMMCifConsumer(SimpleMMcifConsumer consumer){
125//              this.consumer = consumer;
126//      }
127
128        @Override
129        protected String getFilename(String pdbId) {
130                return pdbId.toLowerCase()+".cif.gz";
131        }
132
133        @Override
134        protected String[] getSplitDirPath() {
135                return MMCIF_SPLIT_DIR;
136        }
137
138        @Override
139        protected String[] getObsoleteDirPath() {
140                return MMCIF_OBSOLETE_DIR;
141        }
142
143}