001package org.biojava.nbio.structure.io.cif;
002
003import org.biojava.nbio.structure.Chain;
004import org.biojava.nbio.structure.Structure;
005import org.biojava.nbio.structure.io.FileParsingParameters;
006import org.rcsb.cif.CifIO;
007import org.rcsb.cif.model.CifFile;
008import org.rcsb.cif.schema.StandardSchemata;
009import org.rcsb.cif.schema.mm.MmCifBlock;
010
011import java.io.IOException;
012import java.io.InputStream;
013import java.io.UncheckedIOException;
014import java.net.URL;
015import java.nio.file.Files;
016import java.nio.file.Path;
017
018/**
019 * Convert BioJava structures to CifFiles and vice versa.
020 * @author Sebastian Bittrich
021 * @since 6.0.0
022 */
023public class CifStructureConverter {
024    /**
025     * Read data from a file and convert to Structure without any FileParsingParameters.
026     * @param path the source of information - can be gzipped or binary or text data
027     * @return the target
028     */
029    public static Structure fromPath(Path path) throws IOException {
030        return fromInputStream(Files.newInputStream(path), new FileParsingParameters());
031    }
032
033    /**
034     * Read data from a file and convert to Structure.
035     * @param path the source of information - can be gzipped or binary or text data
036     * @param parameters parameters for parsing
037     * @return the target
038     */
039    public static Structure fromPath(Path path, FileParsingParameters parameters) throws IOException {
040        return fromInputStream(Files.newInputStream(path), parameters);
041    }
042
043    /**
044     * Get data from a URL and convert to Structure without any FileParsingParameters.
045     * @param url the source of information - can be gzipped or binary or text data
046     * @return the target
047     * @throws IOException thrown when reading fails
048     */
049    public static Structure fromURL(URL url) throws IOException {
050        return fromURL(url, new FileParsingParameters());
051    }
052
053    /**
054     * Get data from a URL and convert to Structure.
055     * @param url the source of information - can be gzipped or binary or text data
056     * @param parameters parameters for parsing
057     * @return the target
058     * @throws IOException thrown when reading fails
059     */
060    public static Structure fromURL(URL url, FileParsingParameters parameters) throws IOException {
061        return fromInputStream(url.openStream(), parameters);
062    }
063
064    /**
065     * Convert InputStream to Structure without any FileParsingParameters.
066     * @param inputStream the InputStream of information - can be gzipped or binary or text data
067     * @return the target
068     * @throws IOException thrown when reading fails
069     * @see CifStructureConverter#fromInputStream(InputStream, FileParsingParameters)
070     */
071    public static Structure fromInputStream(InputStream inputStream) throws IOException {
072        return fromInputStream(inputStream, new FileParsingParameters());
073    }
074
075    /**
076     * Convert InputStream to Structure.
077     * @param inputStream the InputStream of information - can be gzipped or binary or text data
078     * @param parameters parameters for parsing
079     * @return the target
080     * @throws IOException thrown when reading fails
081     */
082    public static Structure fromInputStream(InputStream inputStream, FileParsingParameters parameters) throws IOException {
083        return fromCifFile(CifIO.readFromInputStream(inputStream), parameters);
084    }
085
086    /**
087     * Convert CifFile to Structure without any FileParsingParameters.
088     * @param cifFile the source
089     * @return the target
090     * @see CifStructureConverter#fromCifFile(CifFile, FileParsingParameters)
091     */
092    public static Structure fromCifFile(CifFile cifFile) {
093        return fromCifFile(cifFile, new FileParsingParameters());
094    }
095
096    /**
097     * Convert CifFile to Structure.
098     * @param cifFile the source
099     * @param parameters parameters for parsing
100     * @return the target
101     */
102    public static Structure fromCifFile(CifFile cifFile, FileParsingParameters parameters) {
103        // initialize consumer
104        CifStructureConsumer consumer = new CifStructureConsumerImpl(parameters);
105
106        // init structure
107        consumer.prepare();
108
109        // feed individual categories to consumer
110        MmCifBlock cifBlock = cifFile.as(StandardSchemata.MMCIF).getFirstBlock();
111
112        consumer.consumeAuditAuthor(cifBlock.getAuditAuthor());
113        consumer.consumeAtomSite(cifBlock.getAtomSite());
114        consumer.consumeAtomSites(cifBlock.getAtomSites());
115        consumer.consumeCell(cifBlock.getCell());
116        consumer.consumeChemComp(cifBlock.getChemComp());
117        consumer.consumeChemCompBond(cifBlock.getChemCompBond());
118        consumer.consumeDatabasePDBRemark(cifBlock.getDatabasePDBRemark());
119        consumer.consumeDatabasePDBRev(cifBlock.getDatabasePDBRev());
120        consumer.consumeDatabasePDBRevRecord(cifBlock.getDatabasePDBRevRecord());
121        consumer.consumeEm3dReconstruction(cifBlock.getEm3dReconstruction());
122        consumer.consumeEntity(cifBlock.getEntity());
123        consumer.consumeEntityPoly(cifBlock.getEntityPoly());
124        consumer.consumeEntitySrcGen(cifBlock.getEntitySrcGen());
125        consumer.consumeEntitySrcNat(cifBlock.getEntitySrcNat());
126        consumer.consumeEntitySrcSyn(cifBlock.getPdbxEntitySrcSyn());
127        consumer.consumeEntityPolySeq(cifBlock.getEntityPolySeq());
128        consumer.consumeExptl(cifBlock.getExptl());
129        consumer.consumePdbxAuditRevisionHistory(cifBlock.getPdbxAuditRevisionHistory());
130        consumer.consumePdbxChemCompIdentifier(cifBlock.getPdbxChemCompIdentifier());
131        consumer.consumePdbxDatabaseStatus(cifBlock.getPdbxDatabaseStatus());
132        consumer.consumePdbxEntityBranchDescriptor(cifBlock.getPdbxEntityBranchDescriptor());
133        consumer.consumePdbxMolecule(cifBlock.getPdbxMolecule());
134        consumer.consumePdbxMoleculeFeatures(cifBlock.getPdbxMoleculeFeatures());
135        consumer.consumePdbxNonpolyScheme(cifBlock.getPdbxNonpolyScheme());
136        consumer.consumePdbxReferenceEntityLink(cifBlock.getPdbxReferenceEntityLink());
137        consumer.consumePdbxReferenceEntityList(cifBlock.getPdbxReferenceEntityList());
138        consumer.consumePdbxReferenceEntityPolyLink(cifBlock.getPdbxReferenceEntityPolyLink());
139        consumer.consumePdbxStructAssembly(cifBlock.getPdbxStructAssembly());
140        consumer.consumePdbxStructAssemblyGen(cifBlock.getPdbxStructAssemblyGen());
141        consumer.consumePdbxStructModResidue(cifBlock.getPdbxStructModResidue());
142        consumer.consumePdbxStructOperList(cifBlock.getPdbxStructOperList());
143        consumer.consumeRefine(cifBlock.getRefine());
144        consumer.consumeStruct(cifBlock.getStruct());
145        consumer.consumeStructAsym(cifBlock.getStructAsym());
146        consumer.consumeStructConf(cifBlock.getStructConf());
147        consumer.consumeStructConn(cifBlock.getStructConn());
148        consumer.consumeStructConnType(cifBlock.getStructConnType());
149        consumer.consumeStructKeywords(cifBlock.getStructKeywords());
150        consumer.consumeStructNcsOper(cifBlock.getStructNcsOper());
151        consumer.consumeStructRef(cifBlock.getStructRef());
152        consumer.consumeStructRefSeq(cifBlock.getStructRefSeq());
153        consumer.consumeStructRefSeqDif(cifBlock.getStructRefSeqDif());
154        consumer.consumeStructSheetRange(cifBlock.getStructSheetRange());
155        consumer.consumeStructSite(cifBlock.getStructSite());
156        consumer.consumeStructSiteGen(cifBlock.getStructSiteGen());
157        consumer.consumeSymmetry(cifBlock.getSymmetry());
158
159        // prepare structure to be retrieved
160        consumer.finish();
161
162        return consumer.getContainer();
163    }
164
165    /**
166     * Write a structure to a CIF file.
167     * @param structure the source
168     * @param path where to write to
169     * @throws IOException thrown when writing fails
170     */
171    public static void toTextFile(Structure structure, Path path) throws IOException {
172        CifIO.writeText(toCifFile(structure), path);
173    }
174
175    /**
176     * Write a structure to a BCIF file.
177     * @param structure the source
178     * @param path where to write to
179     * @throws IOException thrown when writing fails
180     */
181    public static void toBinaryFile(Structure structure, Path path) throws IOException {
182        CifIO.writeBinary(toCifFile(structure), path);
183    }
184
185    /**
186     * Convert a structure to BCIF format.
187     * @param structure the source
188     * @return the binary representation of the structure
189     */
190    public static byte[] toBinary(Structure structure) {
191        try {
192            return CifIO.writeText(toCifFile(structure));
193        } catch (IOException e) {
194            throw new UncheckedIOException(e);
195        }
196    }
197
198    /**
199     * Convert a structure to mmCIF format.
200     * @param structure the source
201     * @return the mmCIF String representation of the structure
202     */
203    public static String toText(Structure structure) {
204        try {
205            return new String(CifIO.writeText(toCifFile(structure)));
206        } catch (IOException e) {
207            throw new UncheckedIOException(e);
208        }
209    }
210
211    /**
212     * Convert a chain to mmCIF format.
213     * @param chain the source
214     * @return the mmCIF String representation of the chain
215     */
216    public static String toText(Chain chain) {
217        try {
218            return new String(CifIO.writeText(toCifFile(chain)));
219        } catch (IOException e) {
220            throw new UncheckedIOException(e);
221        }
222    }
223
224    /**
225     * Convert Structure to CifFile.
226     * @param structure the source
227     * @return the target
228     */
229    public static CifFile toCifFile(Structure structure) {
230        return new CifStructureSupplierImpl().get(structure);
231    }
232
233    /**
234     * Convert Chain to CifFile
235     * @param chain the source
236     * @return the target
237     */
238    public static CifFile toCifFile(Chain chain) {
239        return new CifChainSupplierImpl().get(chain);
240    }
241}