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.io.mmcif; 022 023import java.io.BufferedReader; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.InputStreamReader; 027import java.util.zip.GZIPInputStream; 028 029import org.biojava.nbio.structure.io.mmcif.model.ChemComp; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033 034/** Unlike the {@link DownloadChemCompProvider}, this {@link ChemCompProvider} does not download any chem comp definitions. 035 * It has access to a limited set of files that are part of the biojava distribution. 036 * 037 * @author Andreas Prlic 038 * @since 3.0 039 */ 040public class ReducedChemCompProvider implements ChemCompProvider { 041 042 private static final Logger logger = LoggerFactory.getLogger(ReducedChemCompProvider.class); 043 044 public ReducedChemCompProvider(){ 045 logger.debug("Initialising ReducedChemCompProvider"); 046 } 047 048 049 @Override 050 public ChemComp getChemComp(String recordName) { 051 String name = recordName.toUpperCase().trim(); 052 try(InputStream inStream = this.getClass().getResourceAsStream("/chemcomp/"+name + ".cif.gz")) { 053 054 logger.debug("Reading chemcomp/"+name+".cif.gz"); 055 056 if ( inStream == null){ 057 //System.out.println("Could not find chem comp: " + name + " ... using generic Chem Comp"); 058 // could not find the chem comp definition for this in the jar file 059 logger.debug("Getting empty chem comp for {}",name); 060 ChemComp cc = ChemComp.getEmptyChemComp(); 061 cc.setId(name); 062 return cc; 063 } 064 065 MMcifParser parser = new SimpleMMcifParser(); 066 067 ChemCompConsumer consumer = new ChemCompConsumer(); 068 069 // The Consumer builds up the BioJava - structure object. 070 // you could also hook in your own and build up you own data model. 071 parser.addMMcifConsumer(consumer); 072 073 parser.parse(new BufferedReader(new InputStreamReader(new GZIPInputStream(inStream)))); 074 075 ChemicalComponentDictionary dict = consumer.getDictionary(); 076 077 ChemComp chemComp = dict.getChemComp(name); 078 079 return chemComp; 080 081 } catch (IOException e){ 082 logger.error("IOException caught while reading chem comp {}.",name,e); 083 } 084 logger.warn("Problem when loading chem comp {}, will use an empty chem comp for it", name); 085 ChemComp cc = ChemComp.getEmptyChemComp(); 086 cc.setId(name); 087 return cc; 088 } 089 090 091}