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