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.consumeEntity(cifBlock.getEntity());
122        consumer.consumeEntityPoly(cifBlock.getEntityPoly());
123        consumer.consumeEntitySrcGen(cifBlock.getEntitySrcGen());
124        consumer.consumeEntitySrcNat(cifBlock.getEntitySrcNat());
125        consumer.consumeEntitySrcSyn(cifBlock.getPdbxEntitySrcSyn());
126        consumer.consumeEntityPolySeq(cifBlock.getEntityPolySeq());
127        consumer.consumeExptl(cifBlock.getExptl());
128        consumer.consumePdbxAuditRevisionHistory(cifBlock.getPdbxAuditRevisionHistory());
129        consumer.consumePdbxChemCompIdentifier(cifBlock.getPdbxChemCompIdentifier());
130        consumer.consumePdbxDatabaseStatus(cifBlock.getPdbxDatabaseStatus());
131        consumer.consumePdbxEntityBranchDescriptor(cifBlock.getPdbxEntityBranchDescriptor());
132        consumer.consumePdbxMolecule(cifBlock.getPdbxMolecule());
133        consumer.consumePdbxMoleculeFeatures(cifBlock.getPdbxMoleculeFeatures());
134        consumer.consumePdbxNonpolyScheme(cifBlock.getPdbxNonpolyScheme());
135        consumer.consumePdbxReferenceEntityLink(cifBlock.getPdbxReferenceEntityLink());
136        consumer.consumePdbxReferenceEntityList(cifBlock.getPdbxReferenceEntityList());
137        consumer.consumePdbxReferenceEntityPolyLink(cifBlock.getPdbxReferenceEntityPolyLink());
138        consumer.consumePdbxStructAssembly(cifBlock.getPdbxStructAssembly());
139        consumer.consumePdbxStructAssemblyGen(cifBlock.getPdbxStructAssemblyGen());
140        consumer.consumePdbxStructModResidue(cifBlock.getPdbxStructModResidue());
141        consumer.consumePdbxStructOperList(cifBlock.getPdbxStructOperList());
142        consumer.consumeRefine(cifBlock.getRefine());
143        consumer.consumeStruct(cifBlock.getStruct());
144        consumer.consumeStructAsym(cifBlock.getStructAsym());
145        consumer.consumeStructConf(cifBlock.getStructConf());
146        consumer.consumeStructConn(cifBlock.getStructConn());
147        consumer.consumeStructConnType(cifBlock.getStructConnType());
148        consumer.consumeStructKeywords(cifBlock.getStructKeywords());
149        consumer.consumeStructNcsOper(cifBlock.getStructNcsOper());
150        consumer.consumeStructRef(cifBlock.getStructRef());
151        consumer.consumeStructRefSeq(cifBlock.getStructRefSeq());
152        consumer.consumeStructRefSeqDif(cifBlock.getStructRefSeqDif());
153        consumer.consumeStructSheetRange(cifBlock.getStructSheetRange());
154        consumer.consumeStructSite(cifBlock.getStructSite());
155        consumer.consumeStructSiteGen(cifBlock.getStructSiteGen());
156        consumer.consumeSymmetry(cifBlock.getSymmetry());
157
158        // prepare structure to be retrieved
159        consumer.finish();
160
161        return consumer.getContainer();
162    }
163
164    /**
165     * Write a structure to a CIF file.
166     * @param structure the source
167     * @param path where to write to
168     * @throws IOException thrown when writing fails
169     */
170    public static void toTextFile(Structure structure, Path path) throws IOException {
171        CifIO.writeText(toCifFile(structure), path);
172    }
173
174    /**
175     * Write a structure to a BCIF file.
176     * @param structure the source
177     * @param path where to write to
178     * @throws IOException thrown when writing fails
179     */
180    public static void toBinaryFile(Structure structure, Path path) throws IOException {
181        CifIO.writeBinary(toCifFile(structure), path);
182    }
183
184    /**
185     * Convert a structure to BCIF format.
186     * @param structure the source
187     * @return the binary representation of the structure
188     */
189    public static byte[] toBinary(Structure structure) {
190        try {
191            return CifIO.writeText(toCifFile(structure));
192        } catch (IOException e) {
193            throw new UncheckedIOException(e);
194        }
195    }
196
197    /**
198     * Convert a structure to mmCIF format.
199     * @param structure the source
200     * @return the mmCIF String representation of the structure
201     */
202    public static String toText(Structure structure) {
203        try {
204            return new String(CifIO.writeText(toCifFile(structure)));
205        } catch (IOException e) {
206            throw new UncheckedIOException(e);
207        }
208    }
209
210    /**
211     * Convert a chain to mmCIF format.
212     * @param chain the source
213     * @return the mmCIF String representation of the chain
214     */
215    public static String toText(Chain chain) {
216        try {
217            return new String(CifIO.writeText(toCifFile(chain)));
218        } catch (IOException e) {
219            throw new UncheckedIOException(e);
220        }
221    }
222
223    /**
224     * Convert Structure to CifFile.
225     * @param structure the source
226     * @return the target
227     */
228    public static CifFile toCifFile(Structure structure) {
229        return new CifStructureSupplierImpl().get(structure);
230    }
231
232    /**
233     * Convert Chain to CifFile
234     * @param chain the source
235     * @return the target
236     */
237    public static CifFile toCifFile(Chain chain) {
238        return new CifChainSupplierImpl().get(chain);
239    }
240}