001package org.biojava.nbio.structure.io.cif; 002 003import org.biojava.nbio.structure.chem.ChemicalComponentDictionary; 004import org.biojava.nbio.structure.io.FileParsingParameters; 005import org.rcsb.cif.CifIO; 006import org.rcsb.cif.model.CifFile; 007import org.rcsb.cif.schema.StandardSchemata; 008import org.rcsb.cif.schema.mm.MmCifBlock; 009import org.rcsb.cif.schema.mm.MmCifFile; 010 011import java.io.IOException; 012import java.io.InputStream; 013import java.net.URL; 014import java.nio.file.Files; 015import java.nio.file.Path; 016 017/** 018 * Convert CifFiles to chem comps. 019 * @author Sebastian Bittrich 020 * @since 6.0.0 021 */ 022public class ChemCompConverter { 023 /** 024 * Read data from a file and convert to chem comp dictionary. 025 * @param path the source of information - can be gzipped or binary or text data 026 * @return the target 027 */ 028 public static ChemicalComponentDictionary fromPath(Path path) throws IOException { 029 return fromInputStream(Files.newInputStream(path)); 030 } 031 032 /** 033 * Get data from a URL and convert to chem comp dictionary. 034 * @param url the source of information - can be gzipped or binary or text data 035 * @return the target 036 * @throws IOException thrown when reading fails 037 */ 038 public static ChemicalComponentDictionary fromURL(URL url) throws IOException { 039 return fromInputStream(url.openStream()); 040 } 041 042 /** 043 * Convert InputStream to chem comp dictionary. 044 * @param inputStream the InputStream of information - can be gzipped or binary or text data 045 * @return the target 046 * @throws IOException thrown when reading fails 047 * @see CifStructureConverter#fromInputStream(InputStream, FileParsingParameters) 048 */ 049 public static ChemicalComponentDictionary fromInputStream(InputStream inputStream) throws IOException { 050 return fromCifFile(CifIO.readFromInputStream(inputStream)); 051 } 052 053 /** 054 * Convert CifFile to chem comp dictionary. 055 * @param cifFile the source 056 * @return the target 057 */ 058 public static ChemicalComponentDictionary fromCifFile(CifFile cifFile) { 059 // initialize consumer 060 ChemCompConsumer consumer = new ChemCompConsumerImpl(); 061 062 // init structure 063 consumer.prepare(); 064 065 // feed individual categories to consumer 066 MmCifFile mmCifFile = cifFile.as(StandardSchemata.MMCIF); 067 for (MmCifBlock cifBlock : mmCifFile.getBlocks()) { 068 consumer.consumeChemComp(cifBlock.getChemComp()); 069 consumer.consumeChemCompAtom(cifBlock.getChemCompAtom()); 070 consumer.consumeChemCompBond(cifBlock.getChemCompBond()); 071 } 072 073 // prepare structure to be retrieved 074 consumer.finish(); 075 076 return consumer.getContainer(); 077 } 078}