001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.nbio.structure.io.mmtf; 022 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.nio.file.Path; 027 028import org.biojava.nbio.structure.Structure; 029import org.rcsb.mmtf.decoder.GenericDecoder; 030import org.rcsb.mmtf.decoder.StructureDataToAdapter; 031import org.rcsb.mmtf.decoder.ReaderUtils; 032import org.rcsb.mmtf.encoder.AdapterToStructureData; 033import org.rcsb.mmtf.encoder.WriterUtils; 034 035/** 036 * A class of functions for reading and writing Biojava structures using MMTF 037 * @author Anthony Bradley 038 * 039 */ 040public class MmtfActions { 041 042 /** 043 * Get a Structure object from a mmtf file. 044 * @param filePath the mmtf file 045 * @return a Structure object relating to the input byte array. 046 * @throws IOException 047 */ 048 public static Structure readFromFile(Path filePath) throws IOException { 049 // Get the reader - this is the bit that people need to implement. 050 MmtfStructureReader mmtfStructureReader = new MmtfStructureReader(); 051 // Do the inflation 052 new StructureDataToAdapter(new GenericDecoder(ReaderUtils.getDataFromFile(filePath)), mmtfStructureReader); 053 // Get the structue 054 return mmtfStructureReader.getStructure(); 055 } 056 057 /** 058 * Write a Structure object to a file. 059 * @param structure the Structure to write 060 * @param path the file to write 061 * @throws IOException 062 */ 063 public static void writeToFile(Structure structure, Path path) throws IOException { 064 // Set up this writer 065 AdapterToStructureData writerToEncoder = new AdapterToStructureData(); 066 // Get the writer - this is what people implement 067 new MmtfStructureWriter(structure, writerToEncoder); 068 // Now write this data to file 069 WriterUtils.writeDataToFile(writerToEncoder, path); 070 } 071 072 /** 073 * Write a Structure object to an {@link OutputStream} 074 * @param structure the Structure to write 075 * @param outputStream the {@link OutputStream} to write to 076 * @throws IOException an error transferring the byte[] 077 */ 078 public static void writeToOutputStream(Structure structure, OutputStream outputStream) throws IOException{ 079 // Set up this writer 080 AdapterToStructureData writerToEncoder = new AdapterToStructureData(); 081 // Get the writer - this is what people implement 082 new MmtfStructureWriter(structure, writerToEncoder); 083 // Now write this data to file 084 byte[] outputBytes = WriterUtils.getDataAsByteArr(writerToEncoder); 085 outputStream.write(outputBytes,0,outputBytes.length); 086 } 087 088 089 /** 090 * Get a Biojava structure from the mmtf REST service. 091 * @param pdbId the PDB code of the required structure 092 * @return a Structure object relating to the input byte array 093 * @throws IOException 094 */ 095 public static Structure readFromWeb(String pdbId) throws IOException { 096 // Get the reader - this is the bit that people need to implement. 097 MmtfStructureReader mmtfStructureReader = new MmtfStructureReader(); 098 // Do the inflation 099 new StructureDataToAdapter(new GenericDecoder(ReaderUtils.getDataFromUrl(pdbId)), mmtfStructureReader); 100 // Get the structue 101 return mmtfStructureReader.getStructure(); 102 } 103 104 /** 105 * Read a Biojava structure from an {@link InputStream} 106 * @param inStream the {@link InputStream} to read from 107 * @return the parsed {@link Structure} 108 * @throws IOException 109 */ 110 public static Structure readFromInputStream(InputStream inStream) throws IOException { 111 // Get the reader - this is the bit that people need to implement. 112 MmtfStructureReader mmtfStructureReader = new MmtfStructureReader(); 113 // Do the inflation 114 new StructureDataToAdapter(new GenericDecoder(ReaderUtils.getDataFromInputStream(inStream)), mmtfStructureReader); 115 // Get the structue 116 return mmtfStructureReader.getStructure(); 117 } 118}