package org.biojava.nbio.structure.io.mmtf;

import java.io.IOException;
import java.nio.file.Path;

import org.biojava.nbio.structure.Structure;
import org.rcsb.mmtf.decoder.GenericDecoder;
import org.rcsb.mmtf.decoder.StructureDataToAdapter;
import org.rcsb.mmtf.decoder.ReaderUtils;
import org.rcsb.mmtf.encoder.AdapterToStructureData;
import org.rcsb.mmtf.encoder.WriterUtils;

/**
 * A class of functions for reading and writing Biojava structures using MMTF
 * @author Anthony Bradley
 *
 */
public class MmtfActions {
	
	/**
	 * Get a Structure object from a mmtf file.
	 * @param filePath the mmtf file
	 * @return a Structure object relating to the input byte array.
	 * @throws IOException 
	 */
	public static Structure readFromFile(Path filePath) throws IOException {
		// Get the reader - this is the bit that people need to implement.
		MmtfStructureReader mmtfStructureReader = new MmtfStructureReader();
		// Do the inflation
		new StructureDataToAdapter(new GenericDecoder(ReaderUtils.getDataFromFile(filePath)), mmtfStructureReader);
		// Get the structue
		return mmtfStructureReader.getStructure();
	}
	
	/**
	 * Write a Structure object to a file.
	 * @param structure the Structure to write
	 * @param path the file to write
	 * @throws IOException
	 */
	public static void writeToFile(Structure structure, Path path) throws IOException {
		// Set up this writer
		AdapterToStructureData writerToEncoder = new AdapterToStructureData();
		// Get the writer - this is what people implement
		new MmtfStructureWriter(structure, writerToEncoder);
		// Now write this data to file
		WriterUtils.writeDataToFile(writerToEncoder, path);
	}

	
	/**
	 * Get a Biojava structure from the mmtf REST service.
	 * @param pdbId the PDB code of the required structure
	 * @return a Structure object relating to the input byte array
	 * @throws IOException 
	 */
	public static Structure readFromWeb(String pdbId) throws IOException {
		// Get the reader - this is the bit that people need to implement.
		MmtfStructureReader mmtfStructureReader = new MmtfStructureReader();
		// Do the inflation
		new StructureDataToAdapter(new GenericDecoder(ReaderUtils.getDataFromUrl(pdbId)), mmtfStructureReader);
		// Get the structue
		return mmtfStructureReader.getStructure();
	}
}
@@ -53,27 +53,94 @@
 java.io.
-IOException
+ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStream
 ;%0Aimport
@@ -1714,16 +1714,835 @@
 ath);%0A%09%7D
+%0A%09%0A%09/**%0A%09 * Write a Structure object to an %7B@link OutputStream%7D%0A%09 * @param structure the Structure to write%0A%09 * @return the %7B@link OutputStream%7D of the MMTF structure%0A%09 * @throws IOException an error transferring the byte%5B%5D%0A%09 */%0A%09public static OutputStream writeToOutputStream(Structure structure) throws IOException%7B%0A%09%09// Set up this writer%0A%09%09AdapterToStructureData writerToEncoder = new AdapterToStructureData();%0A%09%09// Get the writer - this is what people implement%0A%09%09new MmtfStructureWriter(structure, writerToEncoder);%0A%09%09// Now write this data to file%0A%09%09byte%5B%5D outputBytes = WriterUtils.getDataAsByteArr(writerToEncoder);%0A%09%09ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(outputBytes.length);%0A%09%09byteArrayOutputStream.write(outputBytes,0,outputBytes.length);%0A%09%09return byteArrayOutputStream;%0A%09%7D
 %0A%0A%09%0A%09/**

Write MMTF to an OutputStream