001package org.biojava.nbio.structure.io.cif;
002
003import org.biojava.nbio.structure.chem.MetalBondDistance;
004import org.rcsb.cif.CifIO;
005import org.rcsb.cif.model.Block;
006import org.rcsb.cif.model.CifFile;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010import java.io.InputStream;
011import java.util.List;
012import java.util.Map;
013
014/**
015 * Created by andreas on 6/6/16.
016 */
017public class MetalBondConverter {
018    private static final Logger logger = LoggerFactory.getLogger(MetalBondConverter.class);
019    private static final String BONDS_FILE = "org/biojava/nbio/structure/bond_distance_limits.cif.gz";
020    private static final Map<String, List<MetalBondDistance>> definitions;
021
022    static {
023        definitions = init();
024    }
025
026    public static Map<String,List<MetalBondDistance>> getMetalBondDefinitions() {
027        return definitions;
028    }
029
030    private static Map<String,List<MetalBondDistance>> init() {
031        InputStream inputStream = MetalBondConverter.class.getClassLoader().getResourceAsStream(BONDS_FILE);
032
033        if (inputStream == null) {
034            throw new RuntimeException("Could not find resource " + BONDS_FILE + ".  This probably means that your " +
035                    "biojava.jar file is corrupt or incorrectly built.");
036        }
037
038        try {
039            CifFile cifFile = CifIO.readFromInputStream(inputStream);
040            // initialize consumer
041            MetalBondConsumerImpl consumer = new MetalBondConsumerImpl();
042
043            // init structure
044            consumer.prepare();
045
046            // feed individual categories to consumer
047            for (Block cifBlock : cifFile.getBlocks()) {
048                cifBlock.categories().forEach(consumer::consume);
049            }
050
051            // prepare structure to be retrieved
052            consumer.finish();
053
054            return consumer.getContainer();
055        } catch (Exception e) {
056            logger.error(e.getMessage(), e);
057        }
058        return null;
059    }
060}